PowerPC64, support medium model access to common symbols
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2019 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 #include "sysdep.h"
29 #include <stdarg.h>
30 #include "bfd.h"
31 #include "bfdlink.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elf/ppc64.h"
35 #include "elf64-ppc.h"
36 #include "dwarf2.h"
37
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_prefix_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58 static bfd_vma opd_entry_value
59 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
60
61 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
62 #define TARGET_LITTLE_NAME "elf64-powerpcle"
63 #define TARGET_BIG_SYM powerpc_elf64_vec
64 #define TARGET_BIG_NAME "elf64-powerpc"
65 #define ELF_ARCH bfd_arch_powerpc
66 #define ELF_TARGET_ID PPC64_ELF_DATA
67 #define ELF_MACHINE_CODE EM_PPC64
68 #define ELF_MAXPAGESIZE 0x10000
69 #define ELF_COMMONPAGESIZE 0x1000
70 #define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
71 #define elf_info_to_howto ppc64_elf_info_to_howto
72
73 #define elf_backend_want_got_sym 0
74 #define elf_backend_want_plt_sym 0
75 #define elf_backend_plt_alignment 3
76 #define elf_backend_plt_not_loaded 1
77 #define elf_backend_got_header_size 8
78 #define elf_backend_want_dynrelro 1
79 #define elf_backend_can_gc_sections 1
80 #define elf_backend_can_refcount 1
81 #define elf_backend_rela_normal 1
82 #define elf_backend_dtrel_excludes_plt 1
83 #define elf_backend_default_execstack 0
84
85 #define bfd_elf64_mkobject ppc64_elf_mkobject
86 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
87 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
88 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
89 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
90 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
91 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
92 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
93 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
94 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
95
96 #define elf_backend_object_p ppc64_elf_object_p
97 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
98 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
99 #define elf_backend_write_core_note ppc64_elf_write_core_note
100 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
101 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
102 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
103 #define elf_backend_check_directives ppc64_elf_before_check_relocs
104 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
105 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
106 #define elf_backend_check_relocs ppc64_elf_check_relocs
107 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
108 #define elf_backend_gc_keep ppc64_elf_gc_keep
109 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
110 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
111 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
112 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
113 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
114 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
115 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
116 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
117 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
118 #define elf_backend_action_discarded ppc64_elf_action_discarded
119 #define elf_backend_relocate_section ppc64_elf_relocate_section
120 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
121 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
122 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
123 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
124 #define elf_backend_special_sections ppc64_elf_special_sections
125 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
126 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
127 #define elf_backend_get_reloc_section bfd_get_section_by_name
128
129 /* The name of the dynamic interpreter. This is put in the .interp
130 section. */
131 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
132
133 /* The size in bytes of an entry in the procedure linkage table. */
134 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
135 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
136
137 /* The initial size of the plt reserved for the dynamic linker. */
138 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
139
140 /* Offsets to some stack save slots. */
141 #define STK_LR 16
142 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
143 /* This one is dodgy. ELFv2 does not have a linker word, so use the
144 CR save slot. Used only by optimised __tls_get_addr call stub,
145 relying on __tls_get_addr_opt not saving CR.. */
146 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
147
148 /* TOC base pointers offset from start of TOC. */
149 #define TOC_BASE_OFF 0x8000
150 /* TOC base alignment. */
151 #define TOC_BASE_ALIGN 256
152
153 /* Offset of tp and dtp pointers from start of TLS block. */
154 #define TP_OFFSET 0x7000
155 #define DTP_OFFSET 0x8000
156
157 /* .plt call stub instructions. The normal stub is like this, but
158 sometimes the .plt entry crosses a 64k boundary and we need to
159 insert an addi to adjust r11. */
160 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
161 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
162 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
163 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
164 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
165 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
166 #define BCTR 0x4e800420 /* bctr */
167
168 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
169 #define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
170 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
171 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
172 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
173
174 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
175 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
176 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
177 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
178 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
179 #define BNECTR 0x4ca20420 /* bnectr+ */
180 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
181
182 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
183 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
184 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
185
186 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
187 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
188 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
189
190 #define LI_R11_0 0x39600000 /* li %r11,0 */
191 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
192 #define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
193 #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
194 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
195 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
196 #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
197 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
198 #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
199 #define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
200 #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
201 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
202 #define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
203 #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
204 #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
205 #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
206 #define PADDI_R12_PC 0x0610000039800000ULL
207 #define PLD_R12_PC 0x04100000e5800000ULL
208 #define PNOP 0x0700000000000000ULL
209
210 /* __glink_PLTresolve stub instructions. We enter with the index in R0. */
211 #define GLINK_PLTRESOLVE_SIZE(htab) \
212 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
213 /* 0: */
214 /* .quad plt0-1f */
215 /* __glink: */
216 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
217 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
218 /* 1: */
219 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
220 /* ld %2,(0b-1b)(%11) */
221 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
222 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
223 /* ld %12,0(%11) */
224 /* ld %2,8(%11) */
225 /* mtctr %12 */
226 /* ld %11,16(%11) */
227 /* bctr */
228 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
229 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
230 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
231 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
232 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
233
234 /* Pad with this. */
235 #define NOP 0x60000000
236
237 /* Some other nops. */
238 #define CROR_151515 0x4def7b82
239 #define CROR_313131 0x4ffffb82
240
241 /* .glink entries for the first 32k functions are two instructions. */
242 #define LI_R0_0 0x38000000 /* li %r0,0 */
243 #define B_DOT 0x48000000 /* b . */
244
245 /* After that, we need two instructions to load the index, followed by
246 a branch. */
247 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
248 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
249
250 /* Instructions used by the save and restore reg functions. */
251 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
252 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
253 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
254 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
255 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
256 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
257 #define LI_R12_0 0x39800000 /* li %r12,0 */
258 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
259 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
260 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
261 #define BLR 0x4e800020 /* blr */
262
263 /* Since .opd is an array of descriptors and each entry will end up
264 with identical R_PPC64_RELATIVE relocs, there is really no need to
265 propagate .opd relocs; The dynamic linker should be taught to
266 relocate .opd without reloc entries. */
267 #ifndef NO_OPD_RELOCS
268 #define NO_OPD_RELOCS 0
269 #endif
270
271 #ifndef ARRAY_SIZE
272 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
273 #endif
274
275 static inline int
276 abiversion (bfd *abfd)
277 {
278 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
279 }
280
281 static inline void
282 set_abiversion (bfd *abfd, int ver)
283 {
284 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
285 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
286 }
287 \f
288 /* Relocation HOWTO's. */
289 /* Like other ELF RELA targets that don't apply multiple
290 field-altering relocations to the same localation, src_mask is
291 always zero and pcrel_offset is the same as pc_relative.
292 PowerPC can always use a zero bitpos, even when the field is not at
293 the LSB. For example, a REL24 could use rightshift=2, bisize=24
294 and bitpos=2 which matches the ABI description, or as we do here,
295 rightshift=0, bitsize=26 and bitpos=0. */
296 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
297 complain, special_func) \
298 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
299 complain_overflow_ ## complain, special_func, \
300 #type, FALSE, 0, mask, pc_relative)
301
302 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
303
304 static reloc_howto_type ppc64_elf_howto_raw[] =
305 {
306 /* This reloc does nothing. */
307 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
308 bfd_elf_generic_reloc),
309
310 /* A standard 32 bit relocation. */
311 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
312 bfd_elf_generic_reloc),
313
314 /* An absolute 26 bit branch; the lower two bits must be zero.
315 FIXME: we don't check that, we just clear them. */
316 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
317 bfd_elf_generic_reloc),
318
319 /* A standard 16 bit relocation. */
320 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
321 bfd_elf_generic_reloc),
322
323 /* A 16 bit relocation without overflow. */
324 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
325 bfd_elf_generic_reloc),
326
327 /* Bits 16-31 of an address. */
328 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
329 bfd_elf_generic_reloc),
330
331 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
332 bits, treated as a signed number, is negative. */
333 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
334 ppc64_elf_ha_reloc),
335
336 /* An absolute 16 bit branch; the lower two bits must be zero.
337 FIXME: we don't check that, we just clear them. */
338 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
339 ppc64_elf_branch_reloc),
340
341 /* An absolute 16 bit branch, for which bit 10 should be set to
342 indicate that the branch is expected to be taken. The lower two
343 bits must be zero. */
344 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
345 ppc64_elf_brtaken_reloc),
346
347 /* An absolute 16 bit branch, for which bit 10 should be set to
348 indicate that the branch is not expected to be taken. The lower
349 two bits must be zero. */
350 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
351 ppc64_elf_brtaken_reloc),
352
353 /* A relative 26 bit branch; the lower two bits must be zero. */
354 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
355 ppc64_elf_branch_reloc),
356
357 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
358 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
359 ppc64_elf_branch_reloc),
360
361 /* A relative 16 bit branch; the lower two bits must be zero. */
362 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
363 ppc64_elf_branch_reloc),
364
365 /* A relative 16 bit branch. Bit 10 should be set to indicate that
366 the branch is expected to be taken. The lower two bits must be
367 zero. */
368 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
369 ppc64_elf_brtaken_reloc),
370
371 /* A relative 16 bit branch. Bit 10 should be set to indicate that
372 the branch is not expected to be taken. The lower two bits must
373 be zero. */
374 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
375 ppc64_elf_brtaken_reloc),
376
377 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
378 symbol. */
379 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
380 ppc64_elf_unhandled_reloc),
381
382 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
383 the symbol. */
384 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
385 ppc64_elf_unhandled_reloc),
386
387 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
388 the symbol. */
389 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
390 ppc64_elf_unhandled_reloc),
391
392 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
393 the symbol. */
394 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
395 ppc64_elf_unhandled_reloc),
396
397 /* This is used only by the dynamic linker. The symbol should exist
398 both in the object being run and in some shared library. The
399 dynamic linker copies the data addressed by the symbol from the
400 shared library into the object, because the object being
401 run has to have the data at some particular address. */
402 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
403 ppc64_elf_unhandled_reloc),
404
405 /* Like R_PPC64_ADDR64, but used when setting global offset table
406 entries. */
407 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
408 ppc64_elf_unhandled_reloc),
409
410 /* Created by the link editor. Marks a procedure linkage table
411 entry for a symbol. */
412 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
413 ppc64_elf_unhandled_reloc),
414
415 /* Used only by the dynamic linker. When the object is run, this
416 doubleword64 is set to the load address of the object, plus the
417 addend. */
418 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
419 bfd_elf_generic_reloc),
420
421 /* Like R_PPC64_ADDR32, but may be unaligned. */
422 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
423 bfd_elf_generic_reloc),
424
425 /* Like R_PPC64_ADDR16, but may be unaligned. */
426 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
427 bfd_elf_generic_reloc),
428
429 /* 32-bit PC relative. */
430 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
431 bfd_elf_generic_reloc),
432
433 /* 32-bit relocation to the symbol's procedure linkage table. */
434 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
435 ppc64_elf_unhandled_reloc),
436
437 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
438 FIXME: R_PPC64_PLTREL32 not supported. */
439 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
440 ppc64_elf_unhandled_reloc),
441
442 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
443 the symbol. */
444 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
445 ppc64_elf_unhandled_reloc),
446
447 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
448 the symbol. */
449 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
450 ppc64_elf_unhandled_reloc),
451
452 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
453 the symbol. */
454 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
455 ppc64_elf_unhandled_reloc),
456
457 /* 16-bit section relative relocation. */
458 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
459 ppc64_elf_sectoff_reloc),
460
461 /* Like R_PPC64_SECTOFF, but no overflow warning. */
462 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
463 ppc64_elf_sectoff_reloc),
464
465 /* 16-bit upper half section relative relocation. */
466 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
467 ppc64_elf_sectoff_reloc),
468
469 /* 16-bit upper half adjusted section relative relocation. */
470 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
471 ppc64_elf_sectoff_ha_reloc),
472
473 /* Like R_PPC64_REL24 without touching the two least significant bits. */
474 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
475 bfd_elf_generic_reloc),
476
477 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
478
479 /* A standard 64-bit relocation. */
480 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
481 bfd_elf_generic_reloc),
482
483 /* The bits 32-47 of an address. */
484 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
485 bfd_elf_generic_reloc),
486
487 /* The bits 32-47 of an address, plus 1 if the contents of the low
488 16 bits, treated as a signed number, is negative. */
489 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
490 ppc64_elf_ha_reloc),
491
492 /* The bits 48-63 of an address. */
493 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
494 bfd_elf_generic_reloc),
495
496 /* The bits 48-63 of an address, plus 1 if the contents of the low
497 16 bits, treated as a signed number, is negative. */
498 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
499 ppc64_elf_ha_reloc),
500
501 /* Like ADDR64, but may be unaligned. */
502 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
503 bfd_elf_generic_reloc),
504
505 /* 64-bit relative relocation. */
506 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
507 bfd_elf_generic_reloc),
508
509 /* 64-bit relocation to the symbol's procedure linkage table. */
510 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
511 ppc64_elf_unhandled_reloc),
512
513 /* 64-bit PC relative relocation to the symbol's procedure linkage
514 table. */
515 /* FIXME: R_PPC64_PLTREL64 not supported. */
516 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
517 ppc64_elf_unhandled_reloc),
518
519 /* 16 bit TOC-relative relocation. */
520 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
521 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
522 ppc64_elf_toc_reloc),
523
524 /* 16 bit TOC-relative relocation without overflow. */
525 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
526 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
527 ppc64_elf_toc_reloc),
528
529 /* 16 bit TOC-relative relocation, high 16 bits. */
530 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
531 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
532 ppc64_elf_toc_reloc),
533
534 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
535 contents of the low 16 bits, treated as a signed number, is
536 negative. */
537 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
538 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
539 ppc64_elf_toc_ha_reloc),
540
541 /* 64-bit relocation; insert value of TOC base (.TOC.). */
542 /* R_PPC64_TOC 51 doubleword64 .TOC. */
543 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
544 ppc64_elf_toc64_reloc),
545
546 /* Like R_PPC64_GOT16, but also informs the link editor that the
547 value to relocate may (!) refer to a PLT entry which the link
548 editor (a) may replace with the symbol value. If the link editor
549 is unable to fully resolve the symbol, it may (b) create a PLT
550 entry and store the address to the new PLT entry in the GOT.
551 This permits lazy resolution of function symbols at run time.
552 The link editor may also skip all of this and just (c) emit a
553 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
554 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
555 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
556 ppc64_elf_unhandled_reloc),
557
558 /* Like R_PPC64_PLTGOT16, but without overflow. */
559 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
560 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
561 ppc64_elf_unhandled_reloc),
562
563 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
564 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
565 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
566 ppc64_elf_unhandled_reloc),
567
568 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
569 1 if the contents of the low 16 bits, treated as a signed number,
570 is negative. */
571 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
572 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
573 ppc64_elf_unhandled_reloc),
574
575 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
576 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
577 bfd_elf_generic_reloc),
578
579 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
580 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
581 bfd_elf_generic_reloc),
582
583 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
584 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
585 ppc64_elf_unhandled_reloc),
586
587 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
588 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
589 ppc64_elf_unhandled_reloc),
590
591 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
592 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
593 ppc64_elf_unhandled_reloc),
594
595 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
596 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
597 ppc64_elf_sectoff_reloc),
598
599 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
600 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
601 ppc64_elf_sectoff_reloc),
602
603 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
604 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
605 ppc64_elf_toc_reloc),
606
607 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
608 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
609 ppc64_elf_toc_reloc),
610
611 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
612 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
613 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
614 ppc64_elf_unhandled_reloc),
615
616 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
617 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
618 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
619 ppc64_elf_unhandled_reloc),
620
621 /* Marker relocs for TLS. */
622 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
623 bfd_elf_generic_reloc),
624
625 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
626 bfd_elf_generic_reloc),
627
628 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
629 bfd_elf_generic_reloc),
630
631 /* Marker reloc for optimizing r2 save in prologue rather than on
632 each plt call stub. */
633 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
634 bfd_elf_generic_reloc),
635
636 /* Marker relocs on inline plt call instructions. */
637 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
638 bfd_elf_generic_reloc),
639
640 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
641 bfd_elf_generic_reloc),
642
643 /* Computes the load module index of the load module that contains the
644 definition of its TLS sym. */
645 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
646 ppc64_elf_unhandled_reloc),
647
648 /* Computes a dtv-relative displacement, the difference between the value
649 of sym+add and the base address of the thread-local storage block that
650 contains the definition of sym, minus 0x8000. */
651 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
652 ppc64_elf_unhandled_reloc),
653
654 /* A 16 bit dtprel reloc. */
655 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
656 ppc64_elf_unhandled_reloc),
657
658 /* Like DTPREL16, but no overflow. */
659 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
660 ppc64_elf_unhandled_reloc),
661
662 /* Like DTPREL16_LO, but next higher group of 16 bits. */
663 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
664 ppc64_elf_unhandled_reloc),
665
666 /* Like DTPREL16_HI, but adjust for low 16 bits. */
667 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
668 ppc64_elf_unhandled_reloc),
669
670 /* Like DTPREL16_HI, but next higher group of 16 bits. */
671 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
672 ppc64_elf_unhandled_reloc),
673
674 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
675 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
676 ppc64_elf_unhandled_reloc),
677
678 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
679 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
680 ppc64_elf_unhandled_reloc),
681
682 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
683 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
684 ppc64_elf_unhandled_reloc),
685
686 /* Like DTPREL16, but for insns with a DS field. */
687 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
688 ppc64_elf_unhandled_reloc),
689
690 /* Like DTPREL16_DS, but no overflow. */
691 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
692 ppc64_elf_unhandled_reloc),
693
694 /* Computes a tp-relative displacement, the difference between the value of
695 sym+add and the value of the thread pointer (r13). */
696 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
697 ppc64_elf_unhandled_reloc),
698
699 /* A 16 bit tprel reloc. */
700 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
701 ppc64_elf_unhandled_reloc),
702
703 /* Like TPREL16, but no overflow. */
704 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
705 ppc64_elf_unhandled_reloc),
706
707 /* Like TPREL16_LO, but next higher group of 16 bits. */
708 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
709 ppc64_elf_unhandled_reloc),
710
711 /* Like TPREL16_HI, but adjust for low 16 bits. */
712 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
713 ppc64_elf_unhandled_reloc),
714
715 /* Like TPREL16_HI, but next higher group of 16 bits. */
716 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
717 ppc64_elf_unhandled_reloc),
718
719 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
720 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
721 ppc64_elf_unhandled_reloc),
722
723 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
724 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
725 ppc64_elf_unhandled_reloc),
726
727 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
728 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
729 ppc64_elf_unhandled_reloc),
730
731 /* Like TPREL16, but for insns with a DS field. */
732 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
733 ppc64_elf_unhandled_reloc),
734
735 /* Like TPREL16_DS, but no overflow. */
736 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
737 ppc64_elf_unhandled_reloc),
738
739 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
740 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
741 to the first entry relative to the TOC base (r2). */
742 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
743 ppc64_elf_unhandled_reloc),
744
745 /* Like GOT_TLSGD16, but no overflow. */
746 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
747 ppc64_elf_unhandled_reloc),
748
749 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
750 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
751 ppc64_elf_unhandled_reloc),
752
753 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
754 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
755 ppc64_elf_unhandled_reloc),
756
757 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
758 with values (sym+add)@dtpmod and zero, and computes the offset to the
759 first entry relative to the TOC base (r2). */
760 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
761 ppc64_elf_unhandled_reloc),
762
763 /* Like GOT_TLSLD16, but no overflow. */
764 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
765 ppc64_elf_unhandled_reloc),
766
767 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
768 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
769 ppc64_elf_unhandled_reloc),
770
771 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
772 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
773 ppc64_elf_unhandled_reloc),
774
775 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
776 the offset to the entry relative to the TOC base (r2). */
777 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
778 ppc64_elf_unhandled_reloc),
779
780 /* Like GOT_DTPREL16_DS, but no overflow. */
781 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
782 ppc64_elf_unhandled_reloc),
783
784 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
785 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
786 ppc64_elf_unhandled_reloc),
787
788 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
789 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
790 ppc64_elf_unhandled_reloc),
791
792 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
793 offset to the entry relative to the TOC base (r2). */
794 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
795 ppc64_elf_unhandled_reloc),
796
797 /* Like GOT_TPREL16_DS, but no overflow. */
798 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
799 ppc64_elf_unhandled_reloc),
800
801 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
802 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
803 ppc64_elf_unhandled_reloc),
804
805 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
806 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
807 ppc64_elf_unhandled_reloc),
808
809 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
810 ppc64_elf_unhandled_reloc),
811
812 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
813 bfd_elf_generic_reloc),
814
815 /* A 16 bit relative relocation. */
816 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
817 bfd_elf_generic_reloc),
818
819 /* A 16 bit relative relocation without overflow. */
820 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
821 bfd_elf_generic_reloc),
822
823 /* The high order 16 bits of a relative address. */
824 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
825 bfd_elf_generic_reloc),
826
827 /* The high order 16 bits of a relative address, plus 1 if the contents of
828 the low 16 bits, treated as a signed number, is negative. */
829 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
830 ppc64_elf_ha_reloc),
831
832 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
833 bfd_elf_generic_reloc),
834
835 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
836 ppc64_elf_ha_reloc),
837
838 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
839 bfd_elf_generic_reloc),
840
841 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
842 ppc64_elf_ha_reloc),
843
844 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
845 bfd_elf_generic_reloc),
846
847 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
848 ppc64_elf_ha_reloc),
849
850 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
851 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
852 ppc64_elf_ha_reloc),
853
854 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
855 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
856 ppc64_elf_ha_reloc),
857
858 /* Like R_PPC64_ADDR16_HI, but no overflow. */
859 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
860 bfd_elf_generic_reloc),
861
862 /* Like R_PPC64_ADDR16_HA, but no overflow. */
863 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
864 ppc64_elf_ha_reloc),
865
866 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
867 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
868 ppc64_elf_unhandled_reloc),
869
870 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
871 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
872 ppc64_elf_unhandled_reloc),
873
874 /* Like R_PPC64_TPREL16_HI, but no overflow. */
875 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
876 ppc64_elf_unhandled_reloc),
877
878 /* Like R_PPC64_TPREL16_HA, but no overflow. */
879 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
880 ppc64_elf_unhandled_reloc),
881
882 /* Marker reloc on ELFv2 large-model function entry. */
883 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
884 bfd_elf_generic_reloc),
885
886 /* Like ADDR64, but use local entry point of function. */
887 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
888 bfd_elf_generic_reloc),
889
890 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, FALSE, dont,
891 bfd_elf_generic_reloc),
892
893 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, FALSE, dont,
894 bfd_elf_generic_reloc),
895
896 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, FALSE, dont,
897 bfd_elf_generic_reloc),
898
899 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
900 ppc64_elf_prefix_reloc),
901
902 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, dont,
903 ppc64_elf_prefix_reloc),
904
905 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
906 ppc64_elf_prefix_reloc),
907
908 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
909 ppc64_elf_prefix_reloc),
910
911 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
912 ppc64_elf_prefix_reloc),
913
914 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
915 ppc64_elf_unhandled_reloc),
916
917 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
918 ppc64_elf_unhandled_reloc),
919
920 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
921 ppc64_elf_unhandled_reloc),
922
923 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
924 ppc64_elf_unhandled_reloc),
925
926 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
927 ppc64_elf_unhandled_reloc),
928
929 HOW (R_PPC64_GOT_TLSGD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
930 ppc64_elf_unhandled_reloc),
931
932 HOW (R_PPC64_GOT_TLSLD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
933 ppc64_elf_unhandled_reloc),
934
935 HOW (R_PPC64_GOT_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
936 ppc64_elf_unhandled_reloc),
937
938 HOW (R_PPC64_GOT_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
939 ppc64_elf_unhandled_reloc),
940
941 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
942 bfd_elf_generic_reloc),
943
944 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
945 ppc64_elf_ha_reloc),
946
947 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
948 bfd_elf_generic_reloc),
949
950 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
951 ppc64_elf_ha_reloc),
952
953 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
954 bfd_elf_generic_reloc),
955
956 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
957 ppc64_elf_ha_reloc),
958
959 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
960 bfd_elf_generic_reloc),
961
962 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
963 ppc64_elf_ha_reloc),
964
965 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
966 ppc64_elf_prefix_reloc),
967
968 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
969 ppc64_elf_prefix_reloc),
970
971 /* GNU extension to record C++ vtable hierarchy. */
972 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
973 NULL),
974
975 /* GNU extension to record C++ vtable member usage. */
976 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
977 NULL),
978 };
979
980 \f
981 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
982 be done. */
983
984 static void
985 ppc_howto_init (void)
986 {
987 unsigned int i, type;
988
989 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
990 {
991 type = ppc64_elf_howto_raw[i].type;
992 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
993 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
994 }
995 }
996
997 static reloc_howto_type *
998 ppc64_elf_reloc_type_lookup (bfd *abfd,
999 bfd_reloc_code_real_type code)
1000 {
1001 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1002
1003 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1004 /* Initialize howto table if needed. */
1005 ppc_howto_init ();
1006
1007 switch (code)
1008 {
1009 default:
1010 /* xgettext:c-format */
1011 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1012 (int) code);
1013 bfd_set_error (bfd_error_bad_value);
1014 return NULL;
1015
1016 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1017 break;
1018 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1019 break;
1020 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1021 break;
1022 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1023 break;
1024 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1025 break;
1026 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1027 break;
1028 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1029 break;
1030 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1031 break;
1032 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1033 break;
1034 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1035 break;
1036 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1037 break;
1038 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1039 break;
1040 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1041 break;
1042 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1043 break;
1044 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1045 break;
1046 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1047 break;
1048 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1049 break;
1050 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1051 break;
1052 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1053 break;
1054 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1055 break;
1056 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1057 break;
1058 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1059 break;
1060 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1061 break;
1062 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1063 break;
1064 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1065 break;
1066 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1067 break;
1068 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1069 break;
1070 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1071 break;
1072 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1073 break;
1074 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1075 break;
1076 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1077 break;
1078 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1079 break;
1080 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1081 break;
1082 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1083 break;
1084 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1085 break;
1086 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1087 break;
1088 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1089 break;
1090 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1091 break;
1092 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1093 break;
1094 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1095 break;
1096 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1097 break;
1098 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1099 break;
1100 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1101 break;
1102 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1103 break;
1104 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1105 break;
1106 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1107 break;
1108 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1109 break;
1110 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1111 break;
1112 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1113 break;
1114 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1115 break;
1116 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1117 break;
1118 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1119 break;
1120 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1121 break;
1122 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1123 break;
1124 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1125 break;
1126 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1127 break;
1128 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1129 break;
1130 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1131 break;
1132 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1133 break;
1134 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1135 break;
1136 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1137 break;
1138 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1139 break;
1140 case BFD_RELOC_PPC64_TLS_PCREL:
1141 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1142 break;
1143 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1144 break;
1145 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1146 break;
1147 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1148 break;
1149 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1150 break;
1151 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1152 break;
1153 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1154 break;
1155 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1156 break;
1157 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1158 break;
1159 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1160 break;
1161 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1162 break;
1163 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1164 break;
1165 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1166 break;
1167 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1168 break;
1169 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1170 break;
1171 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1172 break;
1173 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1174 break;
1175 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1176 break;
1177 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1178 break;
1179 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1180 break;
1181 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1182 break;
1183 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1184 break;
1185 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1186 break;
1187 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1188 break;
1189 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1190 break;
1191 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1192 break;
1193 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1194 break;
1195 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1196 break;
1197 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1198 break;
1199 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1200 break;
1201 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1202 break;
1203 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1204 break;
1205 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1206 break;
1207 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1208 break;
1209 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1210 break;
1211 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1212 break;
1213 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1214 break;
1215 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1216 break;
1217 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1218 break;
1219 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1220 break;
1221 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1222 break;
1223 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1224 break;
1225 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1226 break;
1227 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1228 break;
1229 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1230 break;
1231 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1232 break;
1233 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1234 break;
1235 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1236 break;
1237 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1238 break;
1239 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1240 break;
1241 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1242 break;
1243 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1244 break;
1245 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1246 break;
1247 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1248 break;
1249 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1250 break;
1251 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1252 break;
1253 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1254 break;
1255 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1256 break;
1257 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1258 break;
1259 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1260 break;
1261 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1262 break;
1263 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1264 break;
1265 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1266 break;
1267 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1268 break;
1269 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1270 break;
1271 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1272 break;
1273 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1274 break;
1275 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1276 break;
1277 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1278 break;
1279 case BFD_RELOC_PPC64_GOT_TLSGD34: r = R_PPC64_GOT_TLSGD34;
1280 break;
1281 case BFD_RELOC_PPC64_GOT_TLSLD34: r = R_PPC64_GOT_TLSLD34;
1282 break;
1283 case BFD_RELOC_PPC64_GOT_TPREL34: r = R_PPC64_GOT_TPREL34;
1284 break;
1285 case BFD_RELOC_PPC64_GOT_DTPREL34: r = R_PPC64_GOT_DTPREL34;
1286 break;
1287 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1288 break;
1289 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1290 break;
1291 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1292 break;
1293 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1294 break;
1295 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1296 break;
1297 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1298 break;
1299 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1300 break;
1301 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1302 break;
1303 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1304 break;
1305 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1306 break;
1307 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1308 break;
1309 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1310 break;
1311 }
1312
1313 return ppc64_elf_howto_table[r];
1314 };
1315
1316 static reloc_howto_type *
1317 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1318 const char *r_name)
1319 {
1320 unsigned int i;
1321
1322 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1323 if (ppc64_elf_howto_raw[i].name != NULL
1324 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1325 return &ppc64_elf_howto_raw[i];
1326
1327 return NULL;
1328 }
1329
1330 /* Set the howto pointer for a PowerPC ELF reloc. */
1331
1332 static bfd_boolean
1333 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1334 Elf_Internal_Rela *dst)
1335 {
1336 unsigned int type;
1337
1338 /* Initialize howto table if needed. */
1339 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1340 ppc_howto_init ();
1341
1342 type = ELF64_R_TYPE (dst->r_info);
1343 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1344 {
1345 /* xgettext:c-format */
1346 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1347 abfd, type);
1348 bfd_set_error (bfd_error_bad_value);
1349 return FALSE;
1350 }
1351 cache_ptr->howto = ppc64_elf_howto_table[type];
1352 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1353 {
1354 /* xgettext:c-format */
1355 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1356 abfd, type);
1357 bfd_set_error (bfd_error_bad_value);
1358 return FALSE;
1359 }
1360
1361 return TRUE;
1362 }
1363
1364 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1365
1366 static bfd_reloc_status_type
1367 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1368 void *data, asection *input_section,
1369 bfd *output_bfd, char **error_message)
1370 {
1371 enum elf_ppc64_reloc_type r_type;
1372 long insn;
1373 bfd_size_type octets;
1374 bfd_vma value;
1375
1376 /* If this is a relocatable link (output_bfd test tells us), just
1377 call the generic function. Any adjustment will be done at final
1378 link time. */
1379 if (output_bfd != NULL)
1380 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1381 input_section, output_bfd, error_message);
1382
1383 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1384 We won't actually be using the low bits, so trashing them
1385 doesn't matter. */
1386 r_type = reloc_entry->howto->type;
1387 if (r_type == R_PPC64_ADDR16_HIGHERA34
1388 || r_type == R_PPC64_ADDR16_HIGHESTA34
1389 || r_type == R_PPC64_REL16_HIGHERA34
1390 || r_type == R_PPC64_REL16_HIGHESTA34)
1391 reloc_entry->addend += 1ULL << 33;
1392 else
1393 reloc_entry->addend += 1U << 15;
1394 if (r_type != R_PPC64_REL16DX_HA)
1395 return bfd_reloc_continue;
1396
1397 value = 0;
1398 if (!bfd_is_com_section (symbol->section))
1399 value = symbol->value;
1400 value += (reloc_entry->addend
1401 + symbol->section->output_offset
1402 + symbol->section->output_section->vma);
1403 value -= (reloc_entry->address
1404 + input_section->output_offset
1405 + input_section->output_section->vma);
1406 value = (bfd_signed_vma) value >> 16;
1407
1408 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1409 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1410 insn &= ~0x1fffc1;
1411 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1412 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1413 if (value + 0x8000 > 0xffff)
1414 return bfd_reloc_overflow;
1415 return bfd_reloc_ok;
1416 }
1417
1418 static bfd_reloc_status_type
1419 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1420 void *data, asection *input_section,
1421 bfd *output_bfd, char **error_message)
1422 {
1423 if (output_bfd != NULL)
1424 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1425 input_section, output_bfd, error_message);
1426
1427 if (strcmp (symbol->section->name, ".opd") == 0
1428 && (symbol->section->owner->flags & DYNAMIC) == 0)
1429 {
1430 bfd_vma dest = opd_entry_value (symbol->section,
1431 symbol->value + reloc_entry->addend,
1432 NULL, NULL, FALSE);
1433 if (dest != (bfd_vma) -1)
1434 reloc_entry->addend = dest - (symbol->value
1435 + symbol->section->output_section->vma
1436 + symbol->section->output_offset);
1437 }
1438 else
1439 {
1440 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1441
1442 if (symbol->section->owner != abfd
1443 && symbol->section->owner != NULL
1444 && abiversion (symbol->section->owner) >= 2)
1445 {
1446 unsigned int i;
1447
1448 for (i = 0; i < symbol->section->owner->symcount; ++i)
1449 {
1450 asymbol *symdef = symbol->section->owner->outsymbols[i];
1451
1452 if (strcmp (symdef->name, symbol->name) == 0)
1453 {
1454 elfsym = (elf_symbol_type *) symdef;
1455 break;
1456 }
1457 }
1458 }
1459 reloc_entry->addend
1460 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1461 }
1462 return bfd_reloc_continue;
1463 }
1464
1465 static bfd_reloc_status_type
1466 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1467 void *data, asection *input_section,
1468 bfd *output_bfd, char **error_message)
1469 {
1470 long insn;
1471 enum elf_ppc64_reloc_type r_type;
1472 bfd_size_type octets;
1473 /* Assume 'at' branch hints. */
1474 bfd_boolean is_isa_v2 = TRUE;
1475
1476 /* If this is a relocatable link (output_bfd test tells us), just
1477 call the generic function. Any adjustment will be done at final
1478 link time. */
1479 if (output_bfd != NULL)
1480 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1481 input_section, output_bfd, error_message);
1482
1483 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1484 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1485 insn &= ~(0x01 << 21);
1486 r_type = reloc_entry->howto->type;
1487 if (r_type == R_PPC64_ADDR14_BRTAKEN
1488 || r_type == R_PPC64_REL14_BRTAKEN)
1489 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1490
1491 if (is_isa_v2)
1492 {
1493 /* Set 'a' bit. This is 0b00010 in BO field for branch
1494 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1495 for branch on CTR insns (BO == 1a00t or 1a01t). */
1496 if ((insn & (0x14 << 21)) == (0x04 << 21))
1497 insn |= 0x02 << 21;
1498 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1499 insn |= 0x08 << 21;
1500 else
1501 goto out;
1502 }
1503 else
1504 {
1505 bfd_vma target = 0;
1506 bfd_vma from;
1507
1508 if (!bfd_is_com_section (symbol->section))
1509 target = symbol->value;
1510 target += symbol->section->output_section->vma;
1511 target += symbol->section->output_offset;
1512 target += reloc_entry->addend;
1513
1514 from = (reloc_entry->address
1515 + input_section->output_offset
1516 + input_section->output_section->vma);
1517
1518 /* Invert 'y' bit if not the default. */
1519 if ((bfd_signed_vma) (target - from) < 0)
1520 insn ^= 0x01 << 21;
1521 }
1522 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1523 out:
1524 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1525 input_section, output_bfd, error_message);
1526 }
1527
1528 static bfd_reloc_status_type
1529 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1530 void *data, asection *input_section,
1531 bfd *output_bfd, char **error_message)
1532 {
1533 /* If this is a relocatable link (output_bfd test tells us), just
1534 call the generic function. Any adjustment will be done at final
1535 link time. */
1536 if (output_bfd != NULL)
1537 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1538 input_section, output_bfd, error_message);
1539
1540 /* Subtract the symbol section base address. */
1541 reloc_entry->addend -= symbol->section->output_section->vma;
1542 return bfd_reloc_continue;
1543 }
1544
1545 static bfd_reloc_status_type
1546 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1547 void *data, asection *input_section,
1548 bfd *output_bfd, char **error_message)
1549 {
1550 /* If this is a relocatable link (output_bfd test tells us), just
1551 call the generic function. Any adjustment will be done at final
1552 link time. */
1553 if (output_bfd != NULL)
1554 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1555 input_section, output_bfd, error_message);
1556
1557 /* Subtract the symbol section base address. */
1558 reloc_entry->addend -= symbol->section->output_section->vma;
1559
1560 /* Adjust the addend for sign extension of the low 16 bits. */
1561 reloc_entry->addend += 0x8000;
1562 return bfd_reloc_continue;
1563 }
1564
1565 static bfd_reloc_status_type
1566 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1567 void *data, asection *input_section,
1568 bfd *output_bfd, char **error_message)
1569 {
1570 bfd_vma TOCstart;
1571
1572 /* If this is a relocatable link (output_bfd test tells us), just
1573 call the generic function. Any adjustment will be done at final
1574 link time. */
1575 if (output_bfd != NULL)
1576 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1577 input_section, output_bfd, error_message);
1578
1579 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1580 if (TOCstart == 0)
1581 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1582
1583 /* Subtract the TOC base address. */
1584 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1585 return bfd_reloc_continue;
1586 }
1587
1588 static bfd_reloc_status_type
1589 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1590 void *data, asection *input_section,
1591 bfd *output_bfd, char **error_message)
1592 {
1593 bfd_vma TOCstart;
1594
1595 /* If this is a relocatable link (output_bfd test tells us), just
1596 call the generic function. Any adjustment will be done at final
1597 link time. */
1598 if (output_bfd != NULL)
1599 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1600 input_section, output_bfd, error_message);
1601
1602 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1603 if (TOCstart == 0)
1604 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1605
1606 /* Subtract the TOC base address. */
1607 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1608
1609 /* Adjust the addend for sign extension of the low 16 bits. */
1610 reloc_entry->addend += 0x8000;
1611 return bfd_reloc_continue;
1612 }
1613
1614 static bfd_reloc_status_type
1615 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1616 void *data, asection *input_section,
1617 bfd *output_bfd, char **error_message)
1618 {
1619 bfd_vma TOCstart;
1620 bfd_size_type octets;
1621
1622 /* If this is a relocatable link (output_bfd test tells us), just
1623 call the generic function. Any adjustment will be done at final
1624 link time. */
1625 if (output_bfd != NULL)
1626 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1627 input_section, output_bfd, error_message);
1628
1629 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1630 if (TOCstart == 0)
1631 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1632
1633 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1634 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1635 return bfd_reloc_ok;
1636 }
1637
1638 static bfd_reloc_status_type
1639 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1640 void *data, asection *input_section,
1641 bfd *output_bfd, char **error_message)
1642 {
1643 uint64_t insn;
1644 bfd_vma targ;
1645
1646 if (output_bfd != NULL)
1647 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1648 input_section, output_bfd, error_message);
1649
1650 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1651 insn <<= 32;
1652 insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1653
1654 targ = (symbol->section->output_section->vma
1655 + symbol->section->output_offset
1656 + reloc_entry->addend);
1657 if (!bfd_is_com_section (symbol->section))
1658 targ += symbol->value;
1659 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1660 targ += 1ULL << 33;
1661 if (reloc_entry->howto->pc_relative)
1662 {
1663 bfd_vma from = (reloc_entry->address
1664 + input_section->output_offset
1665 + input_section->output_section->vma);
1666 targ -=from;
1667 }
1668 targ >>= reloc_entry->howto->rightshift;
1669 insn &= ~reloc_entry->howto->dst_mask;
1670 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1671 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1672 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1673 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1674 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1675 >= 1ULL << reloc_entry->howto->bitsize))
1676 return bfd_reloc_overflow;
1677 return bfd_reloc_ok;
1678 }
1679
1680 static bfd_reloc_status_type
1681 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1682 void *data, asection *input_section,
1683 bfd *output_bfd, char **error_message)
1684 {
1685 /* If this is a relocatable link (output_bfd test tells us), just
1686 call the generic function. Any adjustment will be done at final
1687 link time. */
1688 if (output_bfd != NULL)
1689 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1690 input_section, output_bfd, error_message);
1691
1692 if (error_message != NULL)
1693 {
1694 static char buf[60];
1695 sprintf (buf, "generic linker can't handle %s",
1696 reloc_entry->howto->name);
1697 *error_message = buf;
1698 }
1699 return bfd_reloc_dangerous;
1700 }
1701
1702 /* Track GOT entries needed for a given symbol. We might need more
1703 than one got entry per symbol. */
1704 struct got_entry
1705 {
1706 struct got_entry *next;
1707
1708 /* The symbol addend that we'll be placing in the GOT. */
1709 bfd_vma addend;
1710
1711 /* Unlike other ELF targets, we use separate GOT entries for the same
1712 symbol referenced from different input files. This is to support
1713 automatic multiple TOC/GOT sections, where the TOC base can vary
1714 from one input file to another. After partitioning into TOC groups
1715 we merge entries within the group.
1716
1717 Point to the BFD owning this GOT entry. */
1718 bfd *owner;
1719
1720 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1721 TLS_TPREL or TLS_DTPREL for tls entries. */
1722 unsigned char tls_type;
1723
1724 /* Non-zero if got.ent points to real entry. */
1725 unsigned char is_indirect;
1726
1727 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1728 union
1729 {
1730 bfd_signed_vma refcount;
1731 bfd_vma offset;
1732 struct got_entry *ent;
1733 } got;
1734 };
1735
1736 /* The same for PLT. */
1737 struct plt_entry
1738 {
1739 struct plt_entry *next;
1740
1741 bfd_vma addend;
1742
1743 union
1744 {
1745 bfd_signed_vma refcount;
1746 bfd_vma offset;
1747 } plt;
1748 };
1749
1750 struct ppc64_elf_obj_tdata
1751 {
1752 struct elf_obj_tdata elf;
1753
1754 /* Shortcuts to dynamic linker sections. */
1755 asection *got;
1756 asection *relgot;
1757
1758 /* Used during garbage collection. We attach global symbols defined
1759 on removed .opd entries to this section so that the sym is removed. */
1760 asection *deleted_section;
1761
1762 /* TLS local dynamic got entry handling. Support for multiple GOT
1763 sections means we potentially need one of these for each input bfd. */
1764 struct got_entry tlsld_got;
1765
1766 union
1767 {
1768 /* A copy of relocs before they are modified for --emit-relocs. */
1769 Elf_Internal_Rela *relocs;
1770
1771 /* Section contents. */
1772 bfd_byte *contents;
1773 } opd;
1774
1775 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1776 the reloc to be in the range -32768 to 32767. */
1777 unsigned int has_small_toc_reloc : 1;
1778
1779 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1780 instruction not one we handle. */
1781 unsigned int unexpected_toc_insn : 1;
1782
1783 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1784 this file. */
1785 unsigned int has_optrel : 1;
1786 };
1787
1788 #define ppc64_elf_tdata(bfd) \
1789 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1790
1791 #define ppc64_tlsld_got(bfd) \
1792 (&ppc64_elf_tdata (bfd)->tlsld_got)
1793
1794 #define is_ppc64_elf(bfd) \
1795 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1796 && elf_object_id (bfd) == PPC64_ELF_DATA)
1797
1798 /* Override the generic function because we store some extras. */
1799
1800 static bfd_boolean
1801 ppc64_elf_mkobject (bfd *abfd)
1802 {
1803 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1804 PPC64_ELF_DATA);
1805 }
1806
1807 /* Fix bad default arch selected for a 64 bit input bfd when the
1808 default is 32 bit. Also select arch based on apuinfo. */
1809
1810 static bfd_boolean
1811 ppc64_elf_object_p (bfd *abfd)
1812 {
1813 if (!abfd->arch_info->the_default)
1814 return TRUE;
1815
1816 if (abfd->arch_info->bits_per_word == 32)
1817 {
1818 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1819
1820 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1821 {
1822 /* Relies on arch after 32 bit default being 64 bit default. */
1823 abfd->arch_info = abfd->arch_info->next;
1824 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1825 }
1826 }
1827 return _bfd_elf_ppc_set_arch (abfd);
1828 }
1829
1830 /* Support for core dump NOTE sections. */
1831
1832 static bfd_boolean
1833 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1834 {
1835 size_t offset, size;
1836
1837 if (note->descsz != 504)
1838 return FALSE;
1839
1840 /* pr_cursig */
1841 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1842
1843 /* pr_pid */
1844 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1845
1846 /* pr_reg */
1847 offset = 112;
1848 size = 384;
1849
1850 /* Make a ".reg/999" section. */
1851 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1852 size, note->descpos + offset);
1853 }
1854
1855 static bfd_boolean
1856 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1857 {
1858 if (note->descsz != 136)
1859 return FALSE;
1860
1861 elf_tdata (abfd)->core->pid
1862 = bfd_get_32 (abfd, note->descdata + 24);
1863 elf_tdata (abfd)->core->program
1864 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1865 elf_tdata (abfd)->core->command
1866 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1867
1868 return TRUE;
1869 }
1870
1871 static char *
1872 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1873 ...)
1874 {
1875 switch (note_type)
1876 {
1877 default:
1878 return NULL;
1879
1880 case NT_PRPSINFO:
1881 {
1882 char data[136] ATTRIBUTE_NONSTRING;
1883 va_list ap;
1884
1885 va_start (ap, note_type);
1886 memset (data, 0, sizeof (data));
1887 strncpy (data + 40, va_arg (ap, const char *), 16);
1888 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1889 DIAGNOSTIC_PUSH;
1890 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1891 -Wstringop-truncation:
1892 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1893 */
1894 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1895 #endif
1896 strncpy (data + 56, va_arg (ap, const char *), 80);
1897 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1898 DIAGNOSTIC_POP;
1899 #endif
1900 va_end (ap);
1901 return elfcore_write_note (abfd, buf, bufsiz,
1902 "CORE", note_type, data, sizeof (data));
1903 }
1904
1905 case NT_PRSTATUS:
1906 {
1907 char data[504];
1908 va_list ap;
1909 long pid;
1910 int cursig;
1911 const void *greg;
1912
1913 va_start (ap, note_type);
1914 memset (data, 0, 112);
1915 pid = va_arg (ap, long);
1916 bfd_put_32 (abfd, pid, data + 32);
1917 cursig = va_arg (ap, int);
1918 bfd_put_16 (abfd, cursig, data + 12);
1919 greg = va_arg (ap, const void *);
1920 memcpy (data + 112, greg, 384);
1921 memset (data + 496, 0, 8);
1922 va_end (ap);
1923 return elfcore_write_note (abfd, buf, bufsiz,
1924 "CORE", note_type, data, sizeof (data));
1925 }
1926 }
1927 }
1928
1929 /* Add extra PPC sections. */
1930
1931 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1932 {
1933 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1934 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1935 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1936 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1937 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1938 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1939 { NULL, 0, 0, 0, 0 }
1940 };
1941
1942 enum _ppc64_sec_type {
1943 sec_normal = 0,
1944 sec_opd = 1,
1945 sec_toc = 2
1946 };
1947
1948 struct _ppc64_elf_section_data
1949 {
1950 struct bfd_elf_section_data elf;
1951
1952 union
1953 {
1954 /* An array with one entry for each opd function descriptor,
1955 and some spares since opd entries may be either 16 or 24 bytes. */
1956 #define OPD_NDX(OFF) ((OFF) >> 4)
1957 struct _opd_sec_data
1958 {
1959 /* Points to the function code section for local opd entries. */
1960 asection **func_sec;
1961
1962 /* After editing .opd, adjust references to opd local syms. */
1963 long *adjust;
1964 } opd;
1965
1966 /* An array for toc sections, indexed by offset/8. */
1967 struct _toc_sec_data
1968 {
1969 /* Specifies the relocation symbol index used at a given toc offset. */
1970 unsigned *symndx;
1971
1972 /* And the relocation addend. */
1973 bfd_vma *add;
1974 } toc;
1975 } u;
1976
1977 enum _ppc64_sec_type sec_type:2;
1978
1979 /* Flag set when small branches are detected. Used to
1980 select suitable defaults for the stub group size. */
1981 unsigned int has_14bit_branch:1;
1982
1983 /* Flag set when PLTCALL relocs are detected. */
1984 unsigned int has_pltcall:1;
1985
1986 /* Flag set when section has PLT/GOT/TOC relocations that can be
1987 optimised. */
1988 unsigned int has_optrel:1;
1989 };
1990
1991 #define ppc64_elf_section_data(sec) \
1992 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
1993
1994 static bfd_boolean
1995 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
1996 {
1997 if (!sec->used_by_bfd)
1998 {
1999 struct _ppc64_elf_section_data *sdata;
2000 bfd_size_type amt = sizeof (*sdata);
2001
2002 sdata = bfd_zalloc (abfd, amt);
2003 if (sdata == NULL)
2004 return FALSE;
2005 sec->used_by_bfd = sdata;
2006 }
2007
2008 return _bfd_elf_new_section_hook (abfd, sec);
2009 }
2010
2011 static struct _opd_sec_data *
2012 get_opd_info (asection * sec)
2013 {
2014 if (sec != NULL
2015 && ppc64_elf_section_data (sec) != NULL
2016 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2017 return &ppc64_elf_section_data (sec)->u.opd;
2018 return NULL;
2019 }
2020 \f
2021 /* Parameters for the qsort hook. */
2022 static bfd_boolean synthetic_relocatable;
2023 static asection *synthetic_opd;
2024
2025 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2026
2027 static int
2028 compare_symbols (const void *ap, const void *bp)
2029 {
2030 const asymbol *a = *(const asymbol **) ap;
2031 const asymbol *b = *(const asymbol **) bp;
2032
2033 /* Section symbols first. */
2034 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2035 return -1;
2036 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2037 return 1;
2038
2039 /* then .opd symbols. */
2040 if (synthetic_opd != NULL)
2041 {
2042 if (strcmp (a->section->name, ".opd") == 0
2043 && strcmp (b->section->name, ".opd") != 0)
2044 return -1;
2045 if (strcmp (a->section->name, ".opd") != 0
2046 && strcmp (b->section->name, ".opd") == 0)
2047 return 1;
2048 }
2049
2050 /* then other code symbols. */
2051 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2052 == (SEC_CODE | SEC_ALLOC))
2053 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2054 != (SEC_CODE | SEC_ALLOC)))
2055 return -1;
2056
2057 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2058 != (SEC_CODE | SEC_ALLOC))
2059 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2060 == (SEC_CODE | SEC_ALLOC)))
2061 return 1;
2062
2063 if (synthetic_relocatable)
2064 {
2065 if (a->section->id < b->section->id)
2066 return -1;
2067
2068 if (a->section->id > b->section->id)
2069 return 1;
2070 }
2071
2072 if (a->value + a->section->vma < b->value + b->section->vma)
2073 return -1;
2074
2075 if (a->value + a->section->vma > b->value + b->section->vma)
2076 return 1;
2077
2078 /* For syms with the same value, prefer strong dynamic global function
2079 syms over other syms. */
2080 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2081 return -1;
2082
2083 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2084 return 1;
2085
2086 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2087 return -1;
2088
2089 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2090 return 1;
2091
2092 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2093 return -1;
2094
2095 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2096 return 1;
2097
2098 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2099 return -1;
2100
2101 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2102 return 1;
2103
2104 return a > b;
2105 }
2106
2107 /* Search SYMS for a symbol of the given VALUE. */
2108
2109 static asymbol *
2110 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
2111 {
2112 long mid;
2113
2114 if (id == (unsigned) -1)
2115 {
2116 while (lo < hi)
2117 {
2118 mid = (lo + hi) >> 1;
2119 if (syms[mid]->value + syms[mid]->section->vma < value)
2120 lo = mid + 1;
2121 else if (syms[mid]->value + syms[mid]->section->vma > value)
2122 hi = mid;
2123 else
2124 return syms[mid];
2125 }
2126 }
2127 else
2128 {
2129 while (lo < hi)
2130 {
2131 mid = (lo + hi) >> 1;
2132 if (syms[mid]->section->id < id)
2133 lo = mid + 1;
2134 else if (syms[mid]->section->id > id)
2135 hi = mid;
2136 else if (syms[mid]->value < value)
2137 lo = mid + 1;
2138 else if (syms[mid]->value > value)
2139 hi = mid;
2140 else
2141 return syms[mid];
2142 }
2143 }
2144 return NULL;
2145 }
2146
2147 static bfd_boolean
2148 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2149 {
2150 bfd_vma vma = *(bfd_vma *) ptr;
2151 return ((section->flags & SEC_ALLOC) != 0
2152 && section->vma <= vma
2153 && vma < section->vma + section->size);
2154 }
2155
2156 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2157 entry syms. Also generate @plt symbols for the glink branch table.
2158 Returns count of synthetic symbols in RET or -1 on error. */
2159
2160 static long
2161 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2162 long static_count, asymbol **static_syms,
2163 long dyn_count, asymbol **dyn_syms,
2164 asymbol **ret)
2165 {
2166 asymbol *s;
2167 size_t i, j, count;
2168 char *names;
2169 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2170 asection *opd = NULL;
2171 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2172 asymbol **syms;
2173 int abi = abiversion (abfd);
2174
2175 *ret = NULL;
2176
2177 if (abi < 2)
2178 {
2179 opd = bfd_get_section_by_name (abfd, ".opd");
2180 if (opd == NULL && abi == 1)
2181 return 0;
2182 }
2183
2184 syms = NULL;
2185 codesecsym = 0;
2186 codesecsymend = 0;
2187 secsymend = 0;
2188 opdsymend = 0;
2189 symcount = 0;
2190 if (opd != NULL)
2191 {
2192 symcount = static_count;
2193 if (!relocatable)
2194 symcount += dyn_count;
2195 if (symcount == 0)
2196 return 0;
2197
2198 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2199 if (syms == NULL)
2200 return -1;
2201
2202 if (!relocatable && static_count != 0 && dyn_count != 0)
2203 {
2204 /* Use both symbol tables. */
2205 memcpy (syms, static_syms, static_count * sizeof (*syms));
2206 memcpy (syms + static_count, dyn_syms,
2207 (dyn_count + 1) * sizeof (*syms));
2208 }
2209 else if (!relocatable && static_count == 0)
2210 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2211 else
2212 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2213
2214 /* Trim uninteresting symbols. Interesting symbols are section,
2215 function, and notype symbols. */
2216 for (i = 0, j = 0; i < symcount; ++i)
2217 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2218 | BSF_RELC | BSF_SRELC)) == 0)
2219 syms[j++] = syms[i];
2220 symcount = j;
2221
2222 synthetic_relocatable = relocatable;
2223 synthetic_opd = opd;
2224 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2225
2226 if (!relocatable && symcount > 1)
2227 {
2228 /* Trim duplicate syms, since we may have merged the normal
2229 and dynamic symbols. Actually, we only care about syms
2230 that have different values, so trim any with the same
2231 value. Don't consider ifunc and ifunc resolver symbols
2232 duplicates however, because GDB wants to know whether a
2233 text symbol is an ifunc resolver. */
2234 for (i = 1, j = 1; i < symcount; ++i)
2235 {
2236 const asymbol *s0 = syms[i - 1];
2237 const asymbol *s1 = syms[i];
2238
2239 if ((s0->value + s0->section->vma
2240 != s1->value + s1->section->vma)
2241 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2242 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2243 syms[j++] = syms[i];
2244 }
2245 symcount = j;
2246 }
2247
2248 i = 0;
2249 /* Note that here and in compare_symbols we can't compare opd and
2250 sym->section directly. With separate debug info files, the
2251 symbols will be extracted from the debug file while abfd passed
2252 to this function is the real binary. */
2253 if (strcmp (syms[i]->section->name, ".opd") == 0)
2254 ++i;
2255 codesecsym = i;
2256
2257 for (; i < symcount; ++i)
2258 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2259 | SEC_THREAD_LOCAL))
2260 != (SEC_CODE | SEC_ALLOC))
2261 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2262 break;
2263 codesecsymend = i;
2264
2265 for (; i < symcount; ++i)
2266 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2267 break;
2268 secsymend = i;
2269
2270 for (; i < symcount; ++i)
2271 if (strcmp (syms[i]->section->name, ".opd") != 0)
2272 break;
2273 opdsymend = i;
2274
2275 for (; i < symcount; ++i)
2276 if (((syms[i]->section->flags
2277 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2278 != (SEC_CODE | SEC_ALLOC))
2279 break;
2280 symcount = i;
2281 }
2282 count = 0;
2283
2284 if (relocatable)
2285 {
2286 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2287 arelent *r;
2288 size_t size;
2289 size_t relcount;
2290
2291 if (opdsymend == secsymend)
2292 goto done;
2293
2294 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2295 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2296 if (relcount == 0)
2297 goto done;
2298
2299 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2300 {
2301 count = -1;
2302 goto done;
2303 }
2304
2305 size = 0;
2306 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2307 {
2308 asymbol *sym;
2309
2310 while (r < opd->relocation + relcount
2311 && r->address < syms[i]->value + opd->vma)
2312 ++r;
2313
2314 if (r == opd->relocation + relcount)
2315 break;
2316
2317 if (r->address != syms[i]->value + opd->vma)
2318 continue;
2319
2320 if (r->howto->type != R_PPC64_ADDR64)
2321 continue;
2322
2323 sym = *r->sym_ptr_ptr;
2324 if (!sym_exists_at (syms, opdsymend, symcount,
2325 sym->section->id, sym->value + r->addend))
2326 {
2327 ++count;
2328 size += sizeof (asymbol);
2329 size += strlen (syms[i]->name) + 2;
2330 }
2331 }
2332
2333 if (size == 0)
2334 goto done;
2335 s = *ret = bfd_malloc (size);
2336 if (s == NULL)
2337 {
2338 count = -1;
2339 goto done;
2340 }
2341
2342 names = (char *) (s + count);
2343
2344 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2345 {
2346 asymbol *sym;
2347
2348 while (r < opd->relocation + relcount
2349 && r->address < syms[i]->value + opd->vma)
2350 ++r;
2351
2352 if (r == opd->relocation + relcount)
2353 break;
2354
2355 if (r->address != syms[i]->value + opd->vma)
2356 continue;
2357
2358 if (r->howto->type != R_PPC64_ADDR64)
2359 continue;
2360
2361 sym = *r->sym_ptr_ptr;
2362 if (!sym_exists_at (syms, opdsymend, symcount,
2363 sym->section->id, sym->value + r->addend))
2364 {
2365 size_t len;
2366
2367 *s = *syms[i];
2368 s->flags |= BSF_SYNTHETIC;
2369 s->section = sym->section;
2370 s->value = sym->value + r->addend;
2371 s->name = names;
2372 *names++ = '.';
2373 len = strlen (syms[i]->name);
2374 memcpy (names, syms[i]->name, len + 1);
2375 names += len + 1;
2376 /* Have udata.p point back to the original symbol this
2377 synthetic symbol was derived from. */
2378 s->udata.p = syms[i];
2379 s++;
2380 }
2381 }
2382 }
2383 else
2384 {
2385 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2386 bfd_byte *contents = NULL;
2387 size_t size;
2388 size_t plt_count = 0;
2389 bfd_vma glink_vma = 0, resolv_vma = 0;
2390 asection *dynamic, *glink = NULL, *relplt = NULL;
2391 arelent *p;
2392
2393 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2394 {
2395 free_contents_and_exit_err:
2396 count = -1;
2397 free_contents_and_exit:
2398 if (contents)
2399 free (contents);
2400 goto done;
2401 }
2402
2403 size = 0;
2404 for (i = secsymend; i < opdsymend; ++i)
2405 {
2406 bfd_vma ent;
2407
2408 /* Ignore bogus symbols. */
2409 if (syms[i]->value > opd->size - 8)
2410 continue;
2411
2412 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2413 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2414 {
2415 ++count;
2416 size += sizeof (asymbol);
2417 size += strlen (syms[i]->name) + 2;
2418 }
2419 }
2420
2421 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2422 if (dyn_count != 0
2423 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2424 {
2425 bfd_byte *dynbuf, *extdyn, *extdynend;
2426 size_t extdynsize;
2427 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2428
2429 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2430 goto free_contents_and_exit_err;
2431
2432 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2433 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2434
2435 extdyn = dynbuf;
2436 extdynend = extdyn + dynamic->size;
2437 for (; extdyn < extdynend; extdyn += extdynsize)
2438 {
2439 Elf_Internal_Dyn dyn;
2440 (*swap_dyn_in) (abfd, extdyn, &dyn);
2441
2442 if (dyn.d_tag == DT_NULL)
2443 break;
2444
2445 if (dyn.d_tag == DT_PPC64_GLINK)
2446 {
2447 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2448 See comment in ppc64_elf_finish_dynamic_sections. */
2449 glink_vma = dyn.d_un.d_val + 8 * 4;
2450 /* The .glink section usually does not survive the final
2451 link; search for the section (usually .text) where the
2452 glink stubs now reside. */
2453 glink = bfd_sections_find_if (abfd, section_covers_vma,
2454 &glink_vma);
2455 break;
2456 }
2457 }
2458
2459 free (dynbuf);
2460 }
2461
2462 if (glink != NULL)
2463 {
2464 /* Determine __glink trampoline by reading the relative branch
2465 from the first glink stub. */
2466 bfd_byte buf[4];
2467 unsigned int off = 0;
2468
2469 while (bfd_get_section_contents (abfd, glink, buf,
2470 glink_vma + off - glink->vma, 4))
2471 {
2472 unsigned int insn = bfd_get_32 (abfd, buf);
2473 insn ^= B_DOT;
2474 if ((insn & ~0x3fffffc) == 0)
2475 {
2476 resolv_vma
2477 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2478 break;
2479 }
2480 off += 4;
2481 if (off > 4)
2482 break;
2483 }
2484
2485 if (resolv_vma)
2486 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2487
2488 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2489 if (relplt != NULL)
2490 {
2491 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2492 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
2493 goto free_contents_and_exit_err;
2494
2495 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2496 size += plt_count * sizeof (asymbol);
2497
2498 p = relplt->relocation;
2499 for (i = 0; i < plt_count; i++, p++)
2500 {
2501 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2502 if (p->addend != 0)
2503 size += sizeof ("+0x") - 1 + 16;
2504 }
2505 }
2506 }
2507
2508 if (size == 0)
2509 goto free_contents_and_exit;
2510 s = *ret = bfd_malloc (size);
2511 if (s == NULL)
2512 goto free_contents_and_exit_err;
2513
2514 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2515
2516 for (i = secsymend; i < opdsymend; ++i)
2517 {
2518 bfd_vma ent;
2519
2520 if (syms[i]->value > opd->size - 8)
2521 continue;
2522
2523 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2524 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2525 {
2526 size_t lo, hi;
2527 size_t len;
2528 asection *sec = abfd->sections;
2529
2530 *s = *syms[i];
2531 lo = codesecsym;
2532 hi = codesecsymend;
2533 while (lo < hi)
2534 {
2535 size_t mid = (lo + hi) >> 1;
2536 if (syms[mid]->section->vma < ent)
2537 lo = mid + 1;
2538 else if (syms[mid]->section->vma > ent)
2539 hi = mid;
2540 else
2541 {
2542 sec = syms[mid]->section;
2543 break;
2544 }
2545 }
2546
2547 if (lo >= hi && lo > codesecsym)
2548 sec = syms[lo - 1]->section;
2549
2550 for (; sec != NULL; sec = sec->next)
2551 {
2552 if (sec->vma > ent)
2553 break;
2554 /* SEC_LOAD may not be set if SEC is from a separate debug
2555 info file. */
2556 if ((sec->flags & SEC_ALLOC) == 0)
2557 break;
2558 if ((sec->flags & SEC_CODE) != 0)
2559 s->section = sec;
2560 }
2561 s->flags |= BSF_SYNTHETIC;
2562 s->value = ent - s->section->vma;
2563 s->name = names;
2564 *names++ = '.';
2565 len = strlen (syms[i]->name);
2566 memcpy (names, syms[i]->name, len + 1);
2567 names += len + 1;
2568 /* Have udata.p point back to the original symbol this
2569 synthetic symbol was derived from. */
2570 s->udata.p = syms[i];
2571 s++;
2572 }
2573 }
2574 free (contents);
2575
2576 if (glink != NULL && relplt != NULL)
2577 {
2578 if (resolv_vma)
2579 {
2580 /* Add a symbol for the main glink trampoline. */
2581 memset (s, 0, sizeof *s);
2582 s->the_bfd = abfd;
2583 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2584 s->section = glink;
2585 s->value = resolv_vma - glink->vma;
2586 s->name = names;
2587 memcpy (names, "__glink_PLTresolve",
2588 sizeof ("__glink_PLTresolve"));
2589 names += sizeof ("__glink_PLTresolve");
2590 s++;
2591 count++;
2592 }
2593
2594 /* FIXME: It would be very much nicer to put sym@plt on the
2595 stub rather than on the glink branch table entry. The
2596 objdump disassembler would then use a sensible symbol
2597 name on plt calls. The difficulty in doing so is
2598 a) finding the stubs, and,
2599 b) matching stubs against plt entries, and,
2600 c) there can be multiple stubs for a given plt entry.
2601
2602 Solving (a) could be done by code scanning, but older
2603 ppc64 binaries used different stubs to current code.
2604 (b) is the tricky one since you need to known the toc
2605 pointer for at least one function that uses a pic stub to
2606 be able to calculate the plt address referenced.
2607 (c) means gdb would need to set multiple breakpoints (or
2608 find the glink branch itself) when setting breakpoints
2609 for pending shared library loads. */
2610 p = relplt->relocation;
2611 for (i = 0; i < plt_count; i++, p++)
2612 {
2613 size_t len;
2614
2615 *s = **p->sym_ptr_ptr;
2616 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2617 we are defining a symbol, ensure one of them is set. */
2618 if ((s->flags & BSF_LOCAL) == 0)
2619 s->flags |= BSF_GLOBAL;
2620 s->flags |= BSF_SYNTHETIC;
2621 s->section = glink;
2622 s->value = glink_vma - glink->vma;
2623 s->name = names;
2624 s->udata.p = NULL;
2625 len = strlen ((*p->sym_ptr_ptr)->name);
2626 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2627 names += len;
2628 if (p->addend != 0)
2629 {
2630 memcpy (names, "+0x", sizeof ("+0x") - 1);
2631 names += sizeof ("+0x") - 1;
2632 bfd_sprintf_vma (abfd, names, p->addend);
2633 names += strlen (names);
2634 }
2635 memcpy (names, "@plt", sizeof ("@plt"));
2636 names += sizeof ("@plt");
2637 s++;
2638 if (abi < 2)
2639 {
2640 glink_vma += 8;
2641 if (i >= 0x8000)
2642 glink_vma += 4;
2643 }
2644 else
2645 glink_vma += 4;
2646 }
2647 count += plt_count;
2648 }
2649 }
2650
2651 done:
2652 free (syms);
2653 return count;
2654 }
2655 \f
2656 /* The following functions are specific to the ELF linker, while
2657 functions above are used generally. Those named ppc64_elf_* are
2658 called by the main ELF linker code. They appear in this file more
2659 or less in the order in which they are called. eg.
2660 ppc64_elf_check_relocs is called early in the link process,
2661 ppc64_elf_finish_dynamic_sections is one of the last functions
2662 called.
2663
2664 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2665 functions have both a function code symbol and a function descriptor
2666 symbol. A call to foo in a relocatable object file looks like:
2667
2668 . .text
2669 . x:
2670 . bl .foo
2671 . nop
2672
2673 The function definition in another object file might be:
2674
2675 . .section .opd
2676 . foo: .quad .foo
2677 . .quad .TOC.@tocbase
2678 . .quad 0
2679 .
2680 . .text
2681 . .foo: blr
2682
2683 When the linker resolves the call during a static link, the branch
2684 unsurprisingly just goes to .foo and the .opd information is unused.
2685 If the function definition is in a shared library, things are a little
2686 different: The call goes via a plt call stub, the opd information gets
2687 copied to the plt, and the linker patches the nop.
2688
2689 . x:
2690 . bl .foo_stub
2691 . ld 2,40(1)
2692 .
2693 .
2694 . .foo_stub:
2695 . std 2,40(1) # in practice, the call stub
2696 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2697 . addi 11,11,Lfoo@toc@l # this is the general idea
2698 . ld 12,0(11)
2699 . ld 2,8(11)
2700 . mtctr 12
2701 . ld 11,16(11)
2702 . bctr
2703 .
2704 . .section .plt
2705 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2706
2707 The "reloc ()" notation is supposed to indicate that the linker emits
2708 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2709 copying.
2710
2711 What are the difficulties here? Well, firstly, the relocations
2712 examined by the linker in check_relocs are against the function code
2713 sym .foo, while the dynamic relocation in the plt is emitted against
2714 the function descriptor symbol, foo. Somewhere along the line, we need
2715 to carefully copy dynamic link information from one symbol to the other.
2716 Secondly, the generic part of the elf linker will make .foo a dynamic
2717 symbol as is normal for most other backends. We need foo dynamic
2718 instead, at least for an application final link. However, when
2719 creating a shared library containing foo, we need to have both symbols
2720 dynamic so that references to .foo are satisfied during the early
2721 stages of linking. Otherwise the linker might decide to pull in a
2722 definition from some other object, eg. a static library.
2723
2724 Update: As of August 2004, we support a new convention. Function
2725 calls may use the function descriptor symbol, ie. "bl foo". This
2726 behaves exactly as "bl .foo". */
2727
2728 /* Of those relocs that might be copied as dynamic relocs, this
2729 function selects those that must be copied when linking a shared
2730 library or PIE, even when the symbol is local. */
2731
2732 static int
2733 must_be_dyn_reloc (struct bfd_link_info *info,
2734 enum elf_ppc64_reloc_type r_type)
2735 {
2736 switch (r_type)
2737 {
2738 default:
2739 /* Only relative relocs can be resolved when the object load
2740 address isn't fixed. DTPREL64 is excluded because the
2741 dynamic linker needs to differentiate global dynamic from
2742 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2743 return 1;
2744
2745 case R_PPC64_REL32:
2746 case R_PPC64_REL64:
2747 case R_PPC64_REL30:
2748 case R_PPC64_TOC16:
2749 case R_PPC64_TOC16_DS:
2750 case R_PPC64_TOC16_LO:
2751 case R_PPC64_TOC16_HI:
2752 case R_PPC64_TOC16_HA:
2753 case R_PPC64_TOC16_LO_DS:
2754 return 0;
2755
2756 case R_PPC64_TPREL16:
2757 case R_PPC64_TPREL16_LO:
2758 case R_PPC64_TPREL16_HI:
2759 case R_PPC64_TPREL16_HA:
2760 case R_PPC64_TPREL16_DS:
2761 case R_PPC64_TPREL16_LO_DS:
2762 case R_PPC64_TPREL16_HIGH:
2763 case R_PPC64_TPREL16_HIGHA:
2764 case R_PPC64_TPREL16_HIGHER:
2765 case R_PPC64_TPREL16_HIGHERA:
2766 case R_PPC64_TPREL16_HIGHEST:
2767 case R_PPC64_TPREL16_HIGHESTA:
2768 case R_PPC64_TPREL64:
2769 case R_PPC64_TPREL34:
2770 /* These relocations are relative but in a shared library the
2771 linker doesn't know the thread pointer base. */
2772 return bfd_link_dll (info);
2773 }
2774 }
2775
2776 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2777 copying dynamic variables from a shared lib into an app's dynbss
2778 section, and instead use a dynamic relocation to point into the
2779 shared lib. With code that gcc generates, it's vital that this be
2780 enabled; In the PowerPC64 ABI, the address of a function is actually
2781 the address of a function descriptor, which resides in the .opd
2782 section. gcc uses the descriptor directly rather than going via the
2783 GOT as some other ABI's do, which means that initialized function
2784 pointers must reference the descriptor. Thus, a function pointer
2785 initialized to the address of a function in a shared library will
2786 either require a copy reloc, or a dynamic reloc. Using a copy reloc
2787 redefines the function descriptor symbol to point to the copy. This
2788 presents a problem as a plt entry for that function is also
2789 initialized from the function descriptor symbol and the copy reloc
2790 may not be initialized first. */
2791 #define ELIMINATE_COPY_RELOCS 1
2792
2793 /* Section name for stubs is the associated section name plus this
2794 string. */
2795 #define STUB_SUFFIX ".stub"
2796
2797 /* Linker stubs.
2798 ppc_stub_long_branch:
2799 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2800 destination, but a 24 bit branch in a stub section will reach.
2801 . b dest
2802
2803 ppc_stub_plt_branch:
2804 Similar to the above, but a 24 bit branch in the stub section won't
2805 reach its destination.
2806 . addis %r11,%r2,xxx@toc@ha
2807 . ld %r12,xxx@toc@l(%r11)
2808 . mtctr %r12
2809 . bctr
2810
2811 ppc_stub_plt_call:
2812 Used to call a function in a shared library. If it so happens that
2813 the plt entry referenced crosses a 64k boundary, then an extra
2814 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2815 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
2816 . addis %r11,%r2,xxx@toc@ha
2817 . ld %r12,xxx+0@toc@l(%r11)
2818 . mtctr %r12
2819 . ld %r2,xxx+8@toc@l(%r11)
2820 . ld %r11,xxx+16@toc@l(%r11)
2821 . bctr
2822
2823 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2824 code to adjust the value and save r2 to support multiple toc sections.
2825 A ppc_stub_long_branch with an r2 offset looks like:
2826 . std %r2,40(%r1)
2827 . addis %r2,%r2,off@ha
2828 . addi %r2,%r2,off@l
2829 . b dest
2830
2831 A ppc_stub_plt_branch with an r2 offset looks like:
2832 . std %r2,40(%r1)
2833 . addis %r11,%r2,xxx@toc@ha
2834 . ld %r12,xxx@toc@l(%r11)
2835 . addis %r2,%r2,off@ha
2836 . addi %r2,%r2,off@l
2837 . mtctr %r12
2838 . bctr
2839
2840 All of the above stubs are shown as their ELFv1 variants. ELFv2
2841 variants exist too, simpler for plt calls since a new toc pointer
2842 and static chain are not loaded by the stub. In addition, ELFv2
2843 has some more complex stubs to handle calls marked with NOTOC
2844 relocs from functions where r2 is not a valid toc pointer. These
2845 come in two flavours, the ones shown below, and _both variants that
2846 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2847 one call is from a function where r2 is used as the toc pointer but
2848 needs a toc adjusting stub for small-model multi-toc, and another
2849 call is from a function where r2 is not valid.
2850 ppc_stub_long_branch_notoc:
2851 . mflr %r12
2852 . bcl 20,31,1f
2853 . 1:
2854 . mflr %r11
2855 . mtlr %r12
2856 . addis %r12,%r11,dest-1b@ha
2857 . addi %r12,%r12,dest-1b@l
2858 . b dest
2859
2860 ppc_stub_plt_branch_notoc:
2861 . mflr %r12
2862 . bcl 20,31,1f
2863 . 1:
2864 . mflr %r11
2865 . mtlr %r12
2866 . lis %r12,xxx-1b@highest
2867 . ori %r12,%r12,xxx-1b@higher
2868 . sldi %r12,%r12,32
2869 . oris %r12,%r12,xxx-1b@high
2870 . ori %r12,%r12,xxx-1b@l
2871 . add %r12,%r11,%r12
2872 . mtctr %r12
2873 . bctr
2874
2875 ppc_stub_plt_call_notoc:
2876 . mflr %r12
2877 . bcl 20,31,1f
2878 . 1:
2879 . mflr %r11
2880 . mtlr %r12
2881 . lis %r12,xxx-1b@highest
2882 . ori %r12,%r12,xxx-1b@higher
2883 . sldi %r12,%r12,32
2884 . oris %r12,%r12,xxx-1b@high
2885 . ori %r12,%r12,xxx-1b@l
2886 . ldx %r12,%r11,%r12
2887 . mtctr %r12
2888 . bctr
2889
2890 There are also ELFv1 powerxx variants of these stubs.
2891 ppc_stub_long_branch_notoc:
2892 . pla %r12,dest@pcrel
2893 . b dest
2894 ppc_stub_plt_branch_notoc:
2895 . lis %r11,(dest-1f)@highesta34
2896 . ori %r11,%r11,(dest-1f)@highera34
2897 . sldi %r11,%r11,34
2898 . 1: pla %r12,dest@pcrel
2899 . add %r12,%r11,%r12
2900 . mtctr %r12
2901 . bctr
2902 ppc_stub_plt_call_notoc:
2903 . lis %r11,(xxx-1f)@highesta34
2904 . ori %r11,%r11,(xxx-1f)@highera34
2905 . sldi %r11,%r11,34
2906 . 1: pla %r12,xxx@pcrel
2907 . ldx %r12,%r11,%r12
2908 . mtctr %r12
2909 . bctr
2910
2911 In cases where the high instructions would add zero, they are
2912 omitted and following instructions modified in some cases.
2913 For example, a powerxx ppc_stub_plt_call_notoc might simplify down
2914 to
2915 . pld %r12,xxx@pcrel
2916 . mtctr %r12
2917 . bctr
2918
2919 For a given stub group (a set of sections all using the same toc
2920 pointer value) there will be just one stub type used for any
2921 particular function symbol. For example, if printf is called from
2922 code with the tocsave optimization (ie. r2 saved in function
2923 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2924 and from other code without the tocsave optimization requiring a
2925 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2926 type will be created. Calls with the tocsave optimization will
2927 enter this stub after the instruction saving r2. A similar
2928 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2929 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2930 to call an external function like printf. If other calls to printf
2931 require a ppc_stub_plt_call linkage stub then a single
2932 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2933 call. If other calls to printf require a ppc_stub_plt_call_r2save
2934 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2935 be created and calls not requiring r2 to be saved will enter the
2936 stub after the r2 save instruction. There is an analogous
2937 hierarchy of long branch and plt branch stubs for local call
2938 linkage. */
2939
2940 enum ppc_stub_type
2941 {
2942 ppc_stub_none,
2943 ppc_stub_long_branch,
2944 ppc_stub_long_branch_r2off,
2945 ppc_stub_long_branch_notoc,
2946 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
2947 ppc_stub_plt_branch,
2948 ppc_stub_plt_branch_r2off,
2949 ppc_stub_plt_branch_notoc,
2950 ppc_stub_plt_branch_both,
2951 ppc_stub_plt_call,
2952 ppc_stub_plt_call_r2save,
2953 ppc_stub_plt_call_notoc,
2954 ppc_stub_plt_call_both,
2955 ppc_stub_global_entry,
2956 ppc_stub_save_res
2957 };
2958
2959 /* Information on stub grouping. */
2960 struct map_stub
2961 {
2962 /* The stub section. */
2963 asection *stub_sec;
2964 /* This is the section to which stubs in the group will be attached. */
2965 asection *link_sec;
2966 /* Next group. */
2967 struct map_stub *next;
2968 /* Whether to emit a copy of register save/restore functions in this
2969 group. */
2970 int needs_save_res;
2971 /* Current offset within stubs after the insn restoring lr in a
2972 _notoc or _both stub using bcl for pc-relative addressing, or
2973 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2974 unsigned int lr_restore;
2975 /* Accumulated size of EH info emitted to describe return address
2976 if stubs modify lr. Does not include 17 byte FDE header. */
2977 unsigned int eh_size;
2978 /* Offset in glink_eh_frame to the start of EH info for this group. */
2979 unsigned int eh_base;
2980 };
2981
2982 struct ppc_stub_hash_entry
2983 {
2984 /* Base hash table entry structure. */
2985 struct bfd_hash_entry root;
2986
2987 enum ppc_stub_type stub_type;
2988
2989 /* Group information. */
2990 struct map_stub *group;
2991
2992 /* Offset within stub_sec of the beginning of this stub. */
2993 bfd_vma stub_offset;
2994
2995 /* Given the symbol's value and its section we can determine its final
2996 value when building the stubs (so the stub knows where to jump. */
2997 bfd_vma target_value;
2998 asection *target_section;
2999
3000 /* The symbol table entry, if any, that this was derived from. */
3001 struct ppc_link_hash_entry *h;
3002 struct plt_entry *plt_ent;
3003
3004 /* Symbol type. */
3005 unsigned char symtype;
3006
3007 /* Symbol st_other. */
3008 unsigned char other;
3009 };
3010
3011 struct ppc_branch_hash_entry
3012 {
3013 /* Base hash table entry structure. */
3014 struct bfd_hash_entry root;
3015
3016 /* Offset within branch lookup table. */
3017 unsigned int offset;
3018
3019 /* Generation marker. */
3020 unsigned int iter;
3021 };
3022
3023 /* Used to track dynamic relocations for local symbols. */
3024 struct ppc_dyn_relocs
3025 {
3026 struct ppc_dyn_relocs *next;
3027
3028 /* The input section of the reloc. */
3029 asection *sec;
3030
3031 /* Total number of relocs copied for the input section. */
3032 unsigned int count : 31;
3033
3034 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3035 unsigned int ifunc : 1;
3036 };
3037
3038 struct ppc_link_hash_entry
3039 {
3040 struct elf_link_hash_entry elf;
3041
3042 union
3043 {
3044 /* A pointer to the most recently used stub hash entry against this
3045 symbol. */
3046 struct ppc_stub_hash_entry *stub_cache;
3047
3048 /* A pointer to the next symbol starting with a '.' */
3049 struct ppc_link_hash_entry *next_dot_sym;
3050 } u;
3051
3052 /* Track dynamic relocs copied for this symbol. */
3053 struct elf_dyn_relocs *dyn_relocs;
3054
3055 /* Link between function code and descriptor symbols. */
3056 struct ppc_link_hash_entry *oh;
3057
3058 /* Flag function code and descriptor symbols. */
3059 unsigned int is_func:1;
3060 unsigned int is_func_descriptor:1;
3061 unsigned int fake:1;
3062
3063 /* Whether global opd/toc sym has been adjusted or not.
3064 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3065 should be set for all globals defined in any opd/toc section. */
3066 unsigned int adjust_done:1;
3067
3068 /* Set if this is an out-of-line register save/restore function,
3069 with non-standard calling convention. */
3070 unsigned int save_res:1;
3071
3072 /* Set if a duplicate symbol with non-zero localentry is detected,
3073 even when the duplicate symbol does not provide a definition. */
3074 unsigned int non_zero_localentry:1;
3075
3076 /* Contexts in which symbol is used in the GOT (or TOC).
3077 Bits are or'd into the mask as the corresponding relocs are
3078 encountered during check_relocs, with TLS_TLS being set when any
3079 of the other TLS bits are set. tls_optimize clears bits when
3080 optimizing to indicate the corresponding GOT entry type is not
3081 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3082 set TLS_GDIE when a GD reloc turns into an IE one.
3083 These flags are also kept for local symbols. */
3084 #define TLS_TLS 1 /* Any TLS reloc. */
3085 #define TLS_GD 2 /* GD reloc. */
3086 #define TLS_LD 4 /* LD reloc. */
3087 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
3088 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3089 #define TLS_MARK 32 /* __tls_get_addr call marked. */
3090 #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
3091 #define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
3092 unsigned char tls_mask;
3093
3094 /* The above field is also used to mark function symbols. In which
3095 case TLS_TLS will be 0. */
3096 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3097 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
3098 #define NON_GOT 256 /* local symbol plt, not stored. */
3099 };
3100
3101 /* ppc64 ELF linker hash table. */
3102
3103 struct ppc_link_hash_table
3104 {
3105 struct elf_link_hash_table elf;
3106
3107 /* The stub hash table. */
3108 struct bfd_hash_table stub_hash_table;
3109
3110 /* Another hash table for plt_branch stubs. */
3111 struct bfd_hash_table branch_hash_table;
3112
3113 /* Hash table for function prologue tocsave. */
3114 htab_t tocsave_htab;
3115
3116 /* Various options and other info passed from the linker. */
3117 struct ppc64_elf_params *params;
3118
3119 /* The size of sec_info below. */
3120 unsigned int sec_info_arr_size;
3121
3122 /* Per-section array of extra section info. Done this way rather
3123 than as part of ppc64_elf_section_data so we have the info for
3124 non-ppc64 sections. */
3125 struct
3126 {
3127 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3128 bfd_vma toc_off;
3129
3130 union
3131 {
3132 /* The section group that this section belongs to. */
3133 struct map_stub *group;
3134 /* A temp section list pointer. */
3135 asection *list;
3136 } u;
3137 } *sec_info;
3138
3139 /* Linked list of groups. */
3140 struct map_stub *group;
3141
3142 /* Temp used when calculating TOC pointers. */
3143 bfd_vma toc_curr;
3144 bfd *toc_bfd;
3145 asection *toc_first_sec;
3146
3147 /* Used when adding symbols. */
3148 struct ppc_link_hash_entry *dot_syms;
3149
3150 /* Shortcuts to get to dynamic linker sections. */
3151 asection *glink;
3152 asection *global_entry;
3153 asection *sfpr;
3154 asection *pltlocal;
3155 asection *relpltlocal;
3156 asection *brlt;
3157 asection *relbrlt;
3158 asection *glink_eh_frame;
3159
3160 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3161 struct ppc_link_hash_entry *tls_get_addr;
3162 struct ppc_link_hash_entry *tls_get_addr_fd;
3163
3164 /* The size of reliplt used by got entry relocs. */
3165 bfd_size_type got_reli_size;
3166
3167 /* Statistics. */
3168 unsigned long stub_count[ppc_stub_global_entry];
3169
3170 /* Number of stubs against global syms. */
3171 unsigned long stub_globals;
3172
3173 /* Set if we're linking code with function descriptors. */
3174 unsigned int opd_abi:1;
3175
3176 /* Support for multiple toc sections. */
3177 unsigned int do_multi_toc:1;
3178 unsigned int multi_toc_needed:1;
3179 unsigned int second_toc_pass:1;
3180 unsigned int do_toc_opt:1;
3181
3182 /* Set if tls optimization is enabled. */
3183 unsigned int do_tls_opt:1;
3184
3185 /* Set if inline plt calls should be converted to direct calls. */
3186 unsigned int can_convert_all_inline_plt:1;
3187
3188 /* Set on error. */
3189 unsigned int stub_error:1;
3190
3191 /* Whether func_desc_adjust needs to be run over symbols. */
3192 unsigned int need_func_desc_adj:1;
3193
3194 /* Whether there exist local gnu indirect function resolvers,
3195 referenced by dynamic relocations. */
3196 unsigned int local_ifunc_resolver:1;
3197 unsigned int maybe_local_ifunc_resolver:1;
3198
3199 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3200 unsigned int has_plt_localentry0:1;
3201
3202 /* Whether calls are made via the PLT from NOTOC functions. */
3203 unsigned int notoc_plt:1;
3204
3205 /* Whether to use powerxx instructions in linkage stubs. */
3206 unsigned int powerxx_stubs:1;
3207
3208 /* Incremented every time we size stubs. */
3209 unsigned int stub_iteration;
3210
3211 /* Small local sym cache. */
3212 struct sym_cache sym_cache;
3213 };
3214
3215 /* Rename some of the generic section flags to better document how they
3216 are used here. */
3217
3218 /* Nonzero if this section has TLS related relocations. */
3219 #define has_tls_reloc sec_flg0
3220
3221 /* Nonzero if this section has an old-style call to __tls_get_addr. */
3222 #define has_tls_get_addr_call sec_flg1
3223
3224 /* Nonzero if this section has any toc or got relocs. */
3225 #define has_toc_reloc sec_flg2
3226
3227 /* Nonzero if this section has a call to another section that uses
3228 the toc or got. */
3229 #define makes_toc_func_call sec_flg3
3230
3231 /* Recursion protection when determining above flag. */
3232 #define call_check_in_progress sec_flg4
3233 #define call_check_done sec_flg5
3234
3235 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3236
3237 #define ppc_hash_table(p) \
3238 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3239 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3240
3241 #define ppc_stub_hash_lookup(table, string, create, copy) \
3242 ((struct ppc_stub_hash_entry *) \
3243 bfd_hash_lookup ((table), (string), (create), (copy)))
3244
3245 #define ppc_branch_hash_lookup(table, string, create, copy) \
3246 ((struct ppc_branch_hash_entry *) \
3247 bfd_hash_lookup ((table), (string), (create), (copy)))
3248
3249 /* Create an entry in the stub hash table. */
3250
3251 static struct bfd_hash_entry *
3252 stub_hash_newfunc (struct bfd_hash_entry *entry,
3253 struct bfd_hash_table *table,
3254 const char *string)
3255 {
3256 /* Allocate the structure if it has not already been allocated by a
3257 subclass. */
3258 if (entry == NULL)
3259 {
3260 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3261 if (entry == NULL)
3262 return entry;
3263 }
3264
3265 /* Call the allocation method of the superclass. */
3266 entry = bfd_hash_newfunc (entry, table, string);
3267 if (entry != NULL)
3268 {
3269 struct ppc_stub_hash_entry *eh;
3270
3271 /* Initialize the local fields. */
3272 eh = (struct ppc_stub_hash_entry *) entry;
3273 eh->stub_type = ppc_stub_none;
3274 eh->group = NULL;
3275 eh->stub_offset = 0;
3276 eh->target_value = 0;
3277 eh->target_section = NULL;
3278 eh->h = NULL;
3279 eh->plt_ent = NULL;
3280 eh->other = 0;
3281 }
3282
3283 return entry;
3284 }
3285
3286 /* Create an entry in the branch hash table. */
3287
3288 static struct bfd_hash_entry *
3289 branch_hash_newfunc (struct bfd_hash_entry *entry,
3290 struct bfd_hash_table *table,
3291 const char *string)
3292 {
3293 /* Allocate the structure if it has not already been allocated by a
3294 subclass. */
3295 if (entry == NULL)
3296 {
3297 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3298 if (entry == NULL)
3299 return entry;
3300 }
3301
3302 /* Call the allocation method of the superclass. */
3303 entry = bfd_hash_newfunc (entry, table, string);
3304 if (entry != NULL)
3305 {
3306 struct ppc_branch_hash_entry *eh;
3307
3308 /* Initialize the local fields. */
3309 eh = (struct ppc_branch_hash_entry *) entry;
3310 eh->offset = 0;
3311 eh->iter = 0;
3312 }
3313
3314 return entry;
3315 }
3316
3317 /* Create an entry in a ppc64 ELF linker hash table. */
3318
3319 static struct bfd_hash_entry *
3320 link_hash_newfunc (struct bfd_hash_entry *entry,
3321 struct bfd_hash_table *table,
3322 const char *string)
3323 {
3324 /* Allocate the structure if it has not already been allocated by a
3325 subclass. */
3326 if (entry == NULL)
3327 {
3328 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3329 if (entry == NULL)
3330 return entry;
3331 }
3332
3333 /* Call the allocation method of the superclass. */
3334 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3335 if (entry != NULL)
3336 {
3337 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3338
3339 memset (&eh->u.stub_cache, 0,
3340 (sizeof (struct ppc_link_hash_entry)
3341 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3342
3343 /* When making function calls, old ABI code references function entry
3344 points (dot symbols), while new ABI code references the function
3345 descriptor symbol. We need to make any combination of reference and
3346 definition work together, without breaking archive linking.
3347
3348 For a defined function "foo" and an undefined call to "bar":
3349 An old object defines "foo" and ".foo", references ".bar" (possibly
3350 "bar" too).
3351 A new object defines "foo" and references "bar".
3352
3353 A new object thus has no problem with its undefined symbols being
3354 satisfied by definitions in an old object. On the other hand, the
3355 old object won't have ".bar" satisfied by a new object.
3356
3357 Keep a list of newly added dot-symbols. */
3358
3359 if (string[0] == '.')
3360 {
3361 struct ppc_link_hash_table *htab;
3362
3363 htab = (struct ppc_link_hash_table *) table;
3364 eh->u.next_dot_sym = htab->dot_syms;
3365 htab->dot_syms = eh;
3366 }
3367 }
3368
3369 return entry;
3370 }
3371
3372 struct tocsave_entry
3373 {
3374 asection *sec;
3375 bfd_vma offset;
3376 };
3377
3378 static hashval_t
3379 tocsave_htab_hash (const void *p)
3380 {
3381 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3382 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3383 }
3384
3385 static int
3386 tocsave_htab_eq (const void *p1, const void *p2)
3387 {
3388 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3389 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3390 return e1->sec == e2->sec && e1->offset == e2->offset;
3391 }
3392
3393 /* Destroy a ppc64 ELF linker hash table. */
3394
3395 static void
3396 ppc64_elf_link_hash_table_free (bfd *obfd)
3397 {
3398 struct ppc_link_hash_table *htab;
3399
3400 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3401 if (htab->tocsave_htab)
3402 htab_delete (htab->tocsave_htab);
3403 bfd_hash_table_free (&htab->branch_hash_table);
3404 bfd_hash_table_free (&htab->stub_hash_table);
3405 _bfd_elf_link_hash_table_free (obfd);
3406 }
3407
3408 /* Create a ppc64 ELF linker hash table. */
3409
3410 static struct bfd_link_hash_table *
3411 ppc64_elf_link_hash_table_create (bfd *abfd)
3412 {
3413 struct ppc_link_hash_table *htab;
3414 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3415
3416 htab = bfd_zmalloc (amt);
3417 if (htab == NULL)
3418 return NULL;
3419
3420 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3421 sizeof (struct ppc_link_hash_entry),
3422 PPC64_ELF_DATA))
3423 {
3424 free (htab);
3425 return NULL;
3426 }
3427
3428 /* Init the stub hash table too. */
3429 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3430 sizeof (struct ppc_stub_hash_entry)))
3431 {
3432 _bfd_elf_link_hash_table_free (abfd);
3433 return NULL;
3434 }
3435
3436 /* And the branch hash table. */
3437 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3438 sizeof (struct ppc_branch_hash_entry)))
3439 {
3440 bfd_hash_table_free (&htab->stub_hash_table);
3441 _bfd_elf_link_hash_table_free (abfd);
3442 return NULL;
3443 }
3444
3445 htab->tocsave_htab = htab_try_create (1024,
3446 tocsave_htab_hash,
3447 tocsave_htab_eq,
3448 NULL);
3449 if (htab->tocsave_htab == NULL)
3450 {
3451 ppc64_elf_link_hash_table_free (abfd);
3452 return NULL;
3453 }
3454 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3455
3456 /* Initializing two fields of the union is just cosmetic. We really
3457 only care about glist, but when compiled on a 32-bit host the
3458 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3459 debugger inspection of these fields look nicer. */
3460 htab->elf.init_got_refcount.refcount = 0;
3461 htab->elf.init_got_refcount.glist = NULL;
3462 htab->elf.init_plt_refcount.refcount = 0;
3463 htab->elf.init_plt_refcount.glist = NULL;
3464 htab->elf.init_got_offset.offset = 0;
3465 htab->elf.init_got_offset.glist = NULL;
3466 htab->elf.init_plt_offset.offset = 0;
3467 htab->elf.init_plt_offset.glist = NULL;
3468
3469 return &htab->elf.root;
3470 }
3471
3472 /* Create sections for linker generated code. */
3473
3474 static bfd_boolean
3475 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3476 {
3477 struct ppc_link_hash_table *htab;
3478 flagword flags;
3479
3480 htab = ppc_hash_table (info);
3481
3482 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3483 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3484 if (htab->params->save_restore_funcs)
3485 {
3486 /* Create .sfpr for code to save and restore fp regs. */
3487 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3488 flags);
3489 if (htab->sfpr == NULL
3490 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3491 return FALSE;
3492 }
3493
3494 if (bfd_link_relocatable (info))
3495 return TRUE;
3496
3497 /* Create .glink for lazy dynamic linking support. */
3498 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3499 flags);
3500 if (htab->glink == NULL
3501 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
3502 return FALSE;
3503
3504 /* The part of .glink used by global entry stubs, separate so that
3505 it can be aligned appropriately without affecting htab->glink. */
3506 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3507 flags);
3508 if (htab->global_entry == NULL
3509 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
3510 return FALSE;
3511
3512 if (!info->no_ld_generated_unwind_info)
3513 {
3514 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3515 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3516 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3517 ".eh_frame",
3518 flags);
3519 if (htab->glink_eh_frame == NULL
3520 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3521 return FALSE;
3522 }
3523
3524 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3525 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3526 if (htab->elf.iplt == NULL
3527 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
3528 return FALSE;
3529
3530 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3531 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3532 htab->elf.irelplt
3533 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3534 if (htab->elf.irelplt == NULL
3535 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
3536 return FALSE;
3537
3538 /* Create branch lookup table for plt_branch stubs. */
3539 flags = (SEC_ALLOC | SEC_LOAD
3540 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3541 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3542 flags);
3543 if (htab->brlt == NULL
3544 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
3545 return FALSE;
3546
3547 /* Local plt entries, put in .branch_lt but a separate section for
3548 convenience. */
3549 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3550 flags);
3551 if (htab->pltlocal == NULL
3552 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
3553 return FALSE;
3554
3555 if (!bfd_link_pic (info))
3556 return TRUE;
3557
3558 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3559 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3560 htab->relbrlt
3561 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3562 if (htab->relbrlt == NULL
3563 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3564 return FALSE;
3565
3566 htab->relpltlocal
3567 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3568 if (htab->relpltlocal == NULL
3569 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
3570 return FALSE;
3571
3572 return TRUE;
3573 }
3574
3575 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3576
3577 bfd_boolean
3578 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3579 struct ppc64_elf_params *params)
3580 {
3581 struct ppc_link_hash_table *htab;
3582
3583 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3584
3585 /* Always hook our dynamic sections into the first bfd, which is the
3586 linker created stub bfd. This ensures that the GOT header is at
3587 the start of the output TOC section. */
3588 htab = ppc_hash_table (info);
3589 htab->elf.dynobj = params->stub_bfd;
3590 htab->params = params;
3591
3592 return create_linkage_sections (htab->elf.dynobj, info);
3593 }
3594
3595 /* Build a name for an entry in the stub hash table. */
3596
3597 static char *
3598 ppc_stub_name (const asection *input_section,
3599 const asection *sym_sec,
3600 const struct ppc_link_hash_entry *h,
3601 const Elf_Internal_Rela *rel)
3602 {
3603 char *stub_name;
3604 ssize_t len;
3605
3606 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3607 offsets from a sym as a branch target? In fact, we could
3608 probably assume the addend is always zero. */
3609 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3610
3611 if (h)
3612 {
3613 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3614 stub_name = bfd_malloc (len);
3615 if (stub_name == NULL)
3616 return stub_name;
3617
3618 len = sprintf (stub_name, "%08x.%s+%x",
3619 input_section->id & 0xffffffff,
3620 h->elf.root.root.string,
3621 (int) rel->r_addend & 0xffffffff);
3622 }
3623 else
3624 {
3625 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3626 stub_name = bfd_malloc (len);
3627 if (stub_name == NULL)
3628 return stub_name;
3629
3630 len = sprintf (stub_name, "%08x.%x:%x+%x",
3631 input_section->id & 0xffffffff,
3632 sym_sec->id & 0xffffffff,
3633 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3634 (int) rel->r_addend & 0xffffffff);
3635 }
3636 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3637 stub_name[len - 2] = 0;
3638 return stub_name;
3639 }
3640
3641 /* Look up an entry in the stub hash. Stub entries are cached because
3642 creating the stub name takes a bit of time. */
3643
3644 static struct ppc_stub_hash_entry *
3645 ppc_get_stub_entry (const asection *input_section,
3646 const asection *sym_sec,
3647 struct ppc_link_hash_entry *h,
3648 const Elf_Internal_Rela *rel,
3649 struct ppc_link_hash_table *htab)
3650 {
3651 struct ppc_stub_hash_entry *stub_entry;
3652 struct map_stub *group;
3653
3654 /* If this input section is part of a group of sections sharing one
3655 stub section, then use the id of the first section in the group.
3656 Stub names need to include a section id, as there may well be
3657 more than one stub used to reach say, printf, and we need to
3658 distinguish between them. */
3659 group = htab->sec_info[input_section->id].u.group;
3660 if (group == NULL)
3661 return NULL;
3662
3663 if (h != NULL && h->u.stub_cache != NULL
3664 && h->u.stub_cache->h == h
3665 && h->u.stub_cache->group == group)
3666 {
3667 stub_entry = h->u.stub_cache;
3668 }
3669 else
3670 {
3671 char *stub_name;
3672
3673 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3674 if (stub_name == NULL)
3675 return NULL;
3676
3677 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3678 stub_name, FALSE, FALSE);
3679 if (h != NULL)
3680 h->u.stub_cache = stub_entry;
3681
3682 free (stub_name);
3683 }
3684
3685 return stub_entry;
3686 }
3687
3688 /* Add a new stub entry to the stub hash. Not all fields of the new
3689 stub entry are initialised. */
3690
3691 static struct ppc_stub_hash_entry *
3692 ppc_add_stub (const char *stub_name,
3693 asection *section,
3694 struct bfd_link_info *info)
3695 {
3696 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3697 struct map_stub *group;
3698 asection *link_sec;
3699 asection *stub_sec;
3700 struct ppc_stub_hash_entry *stub_entry;
3701
3702 group = htab->sec_info[section->id].u.group;
3703 link_sec = group->link_sec;
3704 stub_sec = group->stub_sec;
3705 if (stub_sec == NULL)
3706 {
3707 size_t namelen;
3708 bfd_size_type len;
3709 char *s_name;
3710
3711 namelen = strlen (link_sec->name);
3712 len = namelen + sizeof (STUB_SUFFIX);
3713 s_name = bfd_alloc (htab->params->stub_bfd, len);
3714 if (s_name == NULL)
3715 return NULL;
3716
3717 memcpy (s_name, link_sec->name, namelen);
3718 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3719 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3720 if (stub_sec == NULL)
3721 return NULL;
3722 group->stub_sec = stub_sec;
3723 }
3724
3725 /* Enter this entry into the linker stub hash table. */
3726 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3727 TRUE, FALSE);
3728 if (stub_entry == NULL)
3729 {
3730 /* xgettext:c-format */
3731 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3732 section->owner, stub_name);
3733 return NULL;
3734 }
3735
3736 stub_entry->group = group;
3737 stub_entry->stub_offset = 0;
3738 return stub_entry;
3739 }
3740
3741 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3742 not already done. */
3743
3744 static bfd_boolean
3745 create_got_section (bfd *abfd, struct bfd_link_info *info)
3746 {
3747 asection *got, *relgot;
3748 flagword flags;
3749 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3750
3751 if (!is_ppc64_elf (abfd))
3752 return FALSE;
3753 if (htab == NULL)
3754 return FALSE;
3755
3756 if (!htab->elf.sgot
3757 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3758 return FALSE;
3759
3760 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3761 | SEC_LINKER_CREATED);
3762
3763 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3764 if (!got
3765 || !bfd_set_section_alignment (abfd, got, 3))
3766 return FALSE;
3767
3768 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3769 flags | SEC_READONLY);
3770 if (!relgot
3771 || !bfd_set_section_alignment (abfd, relgot, 3))
3772 return FALSE;
3773
3774 ppc64_elf_tdata (abfd)->got = got;
3775 ppc64_elf_tdata (abfd)->relgot = relgot;
3776 return TRUE;
3777 }
3778
3779 /* Follow indirect and warning symbol links. */
3780
3781 static inline struct bfd_link_hash_entry *
3782 follow_link (struct bfd_link_hash_entry *h)
3783 {
3784 while (h->type == bfd_link_hash_indirect
3785 || h->type == bfd_link_hash_warning)
3786 h = h->u.i.link;
3787 return h;
3788 }
3789
3790 static inline struct elf_link_hash_entry *
3791 elf_follow_link (struct elf_link_hash_entry *h)
3792 {
3793 return (struct elf_link_hash_entry *) follow_link (&h->root);
3794 }
3795
3796 static inline struct ppc_link_hash_entry *
3797 ppc_follow_link (struct ppc_link_hash_entry *h)
3798 {
3799 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3800 }
3801
3802 /* Merge PLT info on FROM with that on TO. */
3803
3804 static void
3805 move_plt_plist (struct ppc_link_hash_entry *from,
3806 struct ppc_link_hash_entry *to)
3807 {
3808 if (from->elf.plt.plist != NULL)
3809 {
3810 if (to->elf.plt.plist != NULL)
3811 {
3812 struct plt_entry **entp;
3813 struct plt_entry *ent;
3814
3815 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3816 {
3817 struct plt_entry *dent;
3818
3819 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3820 if (dent->addend == ent->addend)
3821 {
3822 dent->plt.refcount += ent->plt.refcount;
3823 *entp = ent->next;
3824 break;
3825 }
3826 if (dent == NULL)
3827 entp = &ent->next;
3828 }
3829 *entp = to->elf.plt.plist;
3830 }
3831
3832 to->elf.plt.plist = from->elf.plt.plist;
3833 from->elf.plt.plist = NULL;
3834 }
3835 }
3836
3837 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3838
3839 static void
3840 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3841 struct elf_link_hash_entry *dir,
3842 struct elf_link_hash_entry *ind)
3843 {
3844 struct ppc_link_hash_entry *edir, *eind;
3845
3846 edir = (struct ppc_link_hash_entry *) dir;
3847 eind = (struct ppc_link_hash_entry *) ind;
3848
3849 edir->is_func |= eind->is_func;
3850 edir->is_func_descriptor |= eind->is_func_descriptor;
3851 edir->tls_mask |= eind->tls_mask;
3852 if (eind->oh != NULL)
3853 edir->oh = ppc_follow_link (eind->oh);
3854
3855 if (edir->elf.versioned != versioned_hidden)
3856 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3857 edir->elf.ref_regular |= eind->elf.ref_regular;
3858 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3859 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3860 edir->elf.needs_plt |= eind->elf.needs_plt;
3861 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3862
3863 /* If we were called to copy over info for a weak sym, don't copy
3864 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3865 in order to simplify readonly_dynrelocs and save a field in the
3866 symbol hash entry, but that means dyn_relocs can't be used in any
3867 tests about a specific symbol, or affect other symbol flags which
3868 are then tested. */
3869 if (eind->elf.root.type != bfd_link_hash_indirect)
3870 return;
3871
3872 /* Copy over any dynamic relocs we may have on the indirect sym. */
3873 if (eind->dyn_relocs != NULL)
3874 {
3875 if (edir->dyn_relocs != NULL)
3876 {
3877 struct elf_dyn_relocs **pp;
3878 struct elf_dyn_relocs *p;
3879
3880 /* Add reloc counts against the indirect sym to the direct sym
3881 list. Merge any entries against the same section. */
3882 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3883 {
3884 struct elf_dyn_relocs *q;
3885
3886 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3887 if (q->sec == p->sec)
3888 {
3889 q->pc_count += p->pc_count;
3890 q->count += p->count;
3891 *pp = p->next;
3892 break;
3893 }
3894 if (q == NULL)
3895 pp = &p->next;
3896 }
3897 *pp = edir->dyn_relocs;
3898 }
3899
3900 edir->dyn_relocs = eind->dyn_relocs;
3901 eind->dyn_relocs = NULL;
3902 }
3903
3904 /* Copy over got entries that we may have already seen to the
3905 symbol which just became indirect. */
3906 if (eind->elf.got.glist != NULL)
3907 {
3908 if (edir->elf.got.glist != NULL)
3909 {
3910 struct got_entry **entp;
3911 struct got_entry *ent;
3912
3913 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3914 {
3915 struct got_entry *dent;
3916
3917 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3918 if (dent->addend == ent->addend
3919 && dent->owner == ent->owner
3920 && dent->tls_type == ent->tls_type)
3921 {
3922 dent->got.refcount += ent->got.refcount;
3923 *entp = ent->next;
3924 break;
3925 }
3926 if (dent == NULL)
3927 entp = &ent->next;
3928 }
3929 *entp = edir->elf.got.glist;
3930 }
3931
3932 edir->elf.got.glist = eind->elf.got.glist;
3933 eind->elf.got.glist = NULL;
3934 }
3935
3936 /* And plt entries. */
3937 move_plt_plist (eind, edir);
3938
3939 if (eind->elf.dynindx != -1)
3940 {
3941 if (edir->elf.dynindx != -1)
3942 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3943 edir->elf.dynstr_index);
3944 edir->elf.dynindx = eind->elf.dynindx;
3945 edir->elf.dynstr_index = eind->elf.dynstr_index;
3946 eind->elf.dynindx = -1;
3947 eind->elf.dynstr_index = 0;
3948 }
3949 }
3950
3951 /* Find the function descriptor hash entry from the given function code
3952 hash entry FH. Link the entries via their OH fields. */
3953
3954 static struct ppc_link_hash_entry *
3955 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3956 {
3957 struct ppc_link_hash_entry *fdh = fh->oh;
3958
3959 if (fdh == NULL)
3960 {
3961 const char *fd_name = fh->elf.root.root.string + 1;
3962
3963 fdh = (struct ppc_link_hash_entry *)
3964 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3965 if (fdh == NULL)
3966 return fdh;
3967
3968 fdh->is_func_descriptor = 1;
3969 fdh->oh = fh;
3970 fh->is_func = 1;
3971 fh->oh = fdh;
3972 }
3973
3974 fdh = ppc_follow_link (fdh);
3975 fdh->is_func_descriptor = 1;
3976 fdh->oh = fh;
3977 return fdh;
3978 }
3979
3980 /* Make a fake function descriptor sym for the undefined code sym FH. */
3981
3982 static struct ppc_link_hash_entry *
3983 make_fdh (struct bfd_link_info *info,
3984 struct ppc_link_hash_entry *fh)
3985 {
3986 bfd *abfd = fh->elf.root.u.undef.abfd;
3987 struct bfd_link_hash_entry *bh = NULL;
3988 struct ppc_link_hash_entry *fdh;
3989 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3990 ? BSF_WEAK
3991 : BSF_GLOBAL);
3992
3993 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3994 fh->elf.root.root.string + 1,
3995 flags, bfd_und_section_ptr, 0,
3996 NULL, FALSE, FALSE, &bh))
3997 return NULL;
3998
3999 fdh = (struct ppc_link_hash_entry *) bh;
4000 fdh->elf.non_elf = 0;
4001 fdh->fake = 1;
4002 fdh->is_func_descriptor = 1;
4003 fdh->oh = fh;
4004 fh->is_func = 1;
4005 fh->oh = fdh;
4006 return fdh;
4007 }
4008
4009 /* Fix function descriptor symbols defined in .opd sections to be
4010 function type. */
4011
4012 static bfd_boolean
4013 ppc64_elf_add_symbol_hook (bfd *ibfd,
4014 struct bfd_link_info *info,
4015 Elf_Internal_Sym *isym,
4016 const char **name,
4017 flagword *flags ATTRIBUTE_UNUSED,
4018 asection **sec,
4019 bfd_vma *value)
4020 {
4021 if (*sec != NULL
4022 && strcmp ((*sec)->name, ".opd") == 0)
4023 {
4024 asection *code_sec;
4025
4026 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4027 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4028 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4029
4030 /* If the symbol is a function defined in .opd, and the function
4031 code is in a discarded group, let it appear to be undefined. */
4032 if (!bfd_link_relocatable (info)
4033 && (*sec)->reloc_count != 0
4034 && opd_entry_value (*sec, *value, &code_sec, NULL,
4035 FALSE) != (bfd_vma) -1
4036 && discarded_section (code_sec))
4037 {
4038 *sec = bfd_und_section_ptr;
4039 isym->st_shndx = SHN_UNDEF;
4040 }
4041 }
4042 else if (*sec != NULL
4043 && strcmp ((*sec)->name, ".toc") == 0
4044 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4045 {
4046 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4047 if (htab != NULL)
4048 htab->params->object_in_toc = 1;
4049 }
4050
4051 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4052 {
4053 if (abiversion (ibfd) == 0)
4054 set_abiversion (ibfd, 2);
4055 else if (abiversion (ibfd) == 1)
4056 {
4057 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4058 " for ABI version 1"), *name);
4059 bfd_set_error (bfd_error_bad_value);
4060 return FALSE;
4061 }
4062 }
4063
4064 return TRUE;
4065 }
4066
4067 /* Merge non-visibility st_other attributes: local entry point. */
4068
4069 static void
4070 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4071 const Elf_Internal_Sym *isym,
4072 bfd_boolean definition,
4073 bfd_boolean dynamic)
4074 {
4075 if (definition && (!dynamic || !h->def_regular))
4076 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4077 | ELF_ST_VISIBILITY (h->other));
4078 }
4079
4080 /* Hook called on merging a symbol. We use this to clear "fake" since
4081 we now have a real symbol. */
4082
4083 static bfd_boolean
4084 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4085 const Elf_Internal_Sym *isym,
4086 asection **psec ATTRIBUTE_UNUSED,
4087 bfd_boolean newdef ATTRIBUTE_UNUSED,
4088 bfd_boolean olddef ATTRIBUTE_UNUSED,
4089 bfd *oldbfd ATTRIBUTE_UNUSED,
4090 const asection *oldsec ATTRIBUTE_UNUSED)
4091 {
4092 ((struct ppc_link_hash_entry *) h)->fake = 0;
4093 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4094 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
4095 return TRUE;
4096 }
4097
4098 /* This function makes an old ABI object reference to ".bar" cause the
4099 inclusion of a new ABI object archive that defines "bar".
4100 NAME is a symbol defined in an archive. Return a symbol in the hash
4101 table that might be satisfied by the archive symbols. */
4102
4103 static struct elf_link_hash_entry *
4104 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4105 struct bfd_link_info *info,
4106 const char *name)
4107 {
4108 struct elf_link_hash_entry *h;
4109 char *dot_name;
4110 size_t len;
4111
4112 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4113 if (h != NULL
4114 /* Don't return this sym if it is a fake function descriptor
4115 created by add_symbol_adjust. */
4116 && !((struct ppc_link_hash_entry *) h)->fake)
4117 return h;
4118
4119 if (name[0] == '.')
4120 return h;
4121
4122 len = strlen (name);
4123 dot_name = bfd_alloc (abfd, len + 2);
4124 if (dot_name == NULL)
4125 return (struct elf_link_hash_entry *) -1;
4126 dot_name[0] = '.';
4127 memcpy (dot_name + 1, name, len + 1);
4128 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4129 bfd_release (abfd, dot_name);
4130 return h;
4131 }
4132
4133 /* This function satisfies all old ABI object references to ".bar" if a
4134 new ABI object defines "bar". Well, at least, undefined dot symbols
4135 are made weak. This stops later archive searches from including an
4136 object if we already have a function descriptor definition. It also
4137 prevents the linker complaining about undefined symbols.
4138 We also check and correct mismatched symbol visibility here. The
4139 most restrictive visibility of the function descriptor and the
4140 function entry symbol is used. */
4141
4142 static bfd_boolean
4143 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4144 {
4145 struct ppc_link_hash_table *htab;
4146 struct ppc_link_hash_entry *fdh;
4147
4148 if (eh->elf.root.type == bfd_link_hash_warning)
4149 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4150
4151 if (eh->elf.root.type == bfd_link_hash_indirect)
4152 return TRUE;
4153
4154 if (eh->elf.root.root.string[0] != '.')
4155 abort ();
4156
4157 htab = ppc_hash_table (info);
4158 if (htab == NULL)
4159 return FALSE;
4160
4161 fdh = lookup_fdh (eh, htab);
4162 if (fdh == NULL
4163 && !bfd_link_relocatable (info)
4164 && (eh->elf.root.type == bfd_link_hash_undefined
4165 || eh->elf.root.type == bfd_link_hash_undefweak)
4166 && eh->elf.ref_regular)
4167 {
4168 /* Make an undefined function descriptor sym, in order to
4169 pull in an --as-needed shared lib. Archives are handled
4170 elsewhere. */
4171 fdh = make_fdh (info, eh);
4172 if (fdh == NULL)
4173 return FALSE;
4174 }
4175
4176 if (fdh != NULL)
4177 {
4178 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4179 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4180
4181 /* Make both descriptor and entry symbol have the most
4182 constraining visibility of either symbol. */
4183 if (entry_vis < descr_vis)
4184 fdh->elf.other += entry_vis - descr_vis;
4185 else if (entry_vis > descr_vis)
4186 eh->elf.other += descr_vis - entry_vis;
4187
4188 /* Propagate reference flags from entry symbol to function
4189 descriptor symbol. */
4190 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4191 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4192 fdh->elf.ref_regular |= eh->elf.ref_regular;
4193 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4194
4195 if (!fdh->elf.forced_local
4196 && fdh->elf.dynindx == -1
4197 && fdh->elf.versioned != versioned_hidden
4198 && (bfd_link_dll (info)
4199 || fdh->elf.def_dynamic
4200 || fdh->elf.ref_dynamic)
4201 && (eh->elf.ref_regular
4202 || eh->elf.def_regular))
4203 {
4204 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4205 return FALSE;
4206 }
4207 }
4208
4209 return TRUE;
4210 }
4211
4212 /* Set up opd section info and abiversion for IBFD, and process list
4213 of dot-symbols we made in link_hash_newfunc. */
4214
4215 static bfd_boolean
4216 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4217 {
4218 struct ppc_link_hash_table *htab;
4219 struct ppc_link_hash_entry **p, *eh;
4220 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4221
4222 if (opd != NULL && opd->size != 0)
4223 {
4224 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4225 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4226
4227 if (abiversion (ibfd) == 0)
4228 set_abiversion (ibfd, 1);
4229 else if (abiversion (ibfd) >= 2)
4230 {
4231 /* xgettext:c-format */
4232 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4233 ibfd, abiversion (ibfd));
4234 bfd_set_error (bfd_error_bad_value);
4235 return FALSE;
4236 }
4237 }
4238
4239 if (is_ppc64_elf (info->output_bfd))
4240 {
4241 /* For input files without an explicit abiversion in e_flags
4242 we should have flagged any with symbol st_other bits set
4243 as ELFv1 and above flagged those with .opd as ELFv2.
4244 Set the output abiversion if not yet set, and for any input
4245 still ambiguous, take its abiversion from the output.
4246 Differences in ABI are reported later. */
4247 if (abiversion (info->output_bfd) == 0)
4248 set_abiversion (info->output_bfd, abiversion (ibfd));
4249 else if (abiversion (ibfd) == 0)
4250 set_abiversion (ibfd, abiversion (info->output_bfd));
4251 }
4252
4253 htab = ppc_hash_table (info);
4254 if (htab == NULL)
4255 return TRUE;
4256
4257 if (opd != NULL && opd->size != 0
4258 && (ibfd->flags & DYNAMIC) == 0
4259 && (opd->flags & SEC_RELOC) != 0
4260 && opd->reloc_count != 0
4261 && !bfd_is_abs_section (opd->output_section)
4262 && info->gc_sections)
4263 {
4264 /* Garbage collection needs some extra help with .opd sections.
4265 We don't want to necessarily keep everything referenced by
4266 relocs in .opd, as that would keep all functions. Instead,
4267 if we reference an .opd symbol (a function descriptor), we
4268 want to keep the function code symbol's section. This is
4269 easy for global symbols, but for local syms we need to keep
4270 information about the associated function section. */
4271 bfd_size_type amt;
4272 asection **opd_sym_map;
4273 Elf_Internal_Shdr *symtab_hdr;
4274 Elf_Internal_Rela *relocs, *rel_end, *rel;
4275
4276 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4277 opd_sym_map = bfd_zalloc (ibfd, amt);
4278 if (opd_sym_map == NULL)
4279 return FALSE;
4280 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4281 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4282 info->keep_memory);
4283 if (relocs == NULL)
4284 return FALSE;
4285 symtab_hdr = &elf_symtab_hdr (ibfd);
4286 rel_end = relocs + opd->reloc_count - 1;
4287 for (rel = relocs; rel < rel_end; rel++)
4288 {
4289 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4290 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4291
4292 if (r_type == R_PPC64_ADDR64
4293 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4294 && r_symndx < symtab_hdr->sh_info)
4295 {
4296 Elf_Internal_Sym *isym;
4297 asection *s;
4298
4299 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4300 if (isym == NULL)
4301 {
4302 if (elf_section_data (opd)->relocs != relocs)
4303 free (relocs);
4304 return FALSE;
4305 }
4306
4307 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4308 if (s != NULL && s != opd)
4309 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4310 }
4311 }
4312 if (elf_section_data (opd)->relocs != relocs)
4313 free (relocs);
4314 }
4315
4316 p = &htab->dot_syms;
4317 while ((eh = *p) != NULL)
4318 {
4319 *p = NULL;
4320 if (&eh->elf == htab->elf.hgot)
4321 ;
4322 else if (htab->elf.hgot == NULL
4323 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4324 htab->elf.hgot = &eh->elf;
4325 else if (abiversion (ibfd) <= 1)
4326 {
4327 htab->need_func_desc_adj = 1;
4328 if (!add_symbol_adjust (eh, info))
4329 return FALSE;
4330 }
4331 p = &eh->u.next_dot_sym;
4332 }
4333 return TRUE;
4334 }
4335
4336 /* Undo hash table changes when an --as-needed input file is determined
4337 not to be needed. */
4338
4339 static bfd_boolean
4340 ppc64_elf_notice_as_needed (bfd *ibfd,
4341 struct bfd_link_info *info,
4342 enum notice_asneeded_action act)
4343 {
4344 if (act == notice_not_needed)
4345 {
4346 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4347
4348 if (htab == NULL)
4349 return FALSE;
4350
4351 htab->dot_syms = NULL;
4352 }
4353 return _bfd_elf_notice_as_needed (ibfd, info, act);
4354 }
4355
4356 /* If --just-symbols against a final linked binary, then assume we need
4357 toc adjusting stubs when calling functions defined there. */
4358
4359 static void
4360 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4361 {
4362 if ((sec->flags & SEC_CODE) != 0
4363 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4364 && is_ppc64_elf (sec->owner))
4365 {
4366 if (abiversion (sec->owner) >= 2
4367 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4368 sec->has_toc_reloc = 1;
4369 }
4370 _bfd_elf_link_just_syms (sec, info);
4371 }
4372
4373 static struct plt_entry **
4374 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4375 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4376 {
4377 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4378 struct plt_entry **local_plt;
4379 unsigned char *local_got_tls_masks;
4380
4381 if (local_got_ents == NULL)
4382 {
4383 bfd_size_type size = symtab_hdr->sh_info;
4384
4385 size *= (sizeof (*local_got_ents)
4386 + sizeof (*local_plt)
4387 + sizeof (*local_got_tls_masks));
4388 local_got_ents = bfd_zalloc (abfd, size);
4389 if (local_got_ents == NULL)
4390 return NULL;
4391 elf_local_got_ents (abfd) = local_got_ents;
4392 }
4393
4394 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4395 {
4396 struct got_entry *ent;
4397
4398 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4399 if (ent->addend == r_addend
4400 && ent->owner == abfd
4401 && ent->tls_type == tls_type)
4402 break;
4403 if (ent == NULL)
4404 {
4405 bfd_size_type amt = sizeof (*ent);
4406 ent = bfd_alloc (abfd, amt);
4407 if (ent == NULL)
4408 return FALSE;
4409 ent->next = local_got_ents[r_symndx];
4410 ent->addend = r_addend;
4411 ent->owner = abfd;
4412 ent->tls_type = tls_type;
4413 ent->is_indirect = FALSE;
4414 ent->got.refcount = 0;
4415 local_got_ents[r_symndx] = ent;
4416 }
4417 ent->got.refcount += 1;
4418 }
4419
4420 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4421 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4422 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4423
4424 return local_plt + r_symndx;
4425 }
4426
4427 static bfd_boolean
4428 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4429 {
4430 struct plt_entry *ent;
4431
4432 for (ent = *plist; ent != NULL; ent = ent->next)
4433 if (ent->addend == addend)
4434 break;
4435 if (ent == NULL)
4436 {
4437 bfd_size_type amt = sizeof (*ent);
4438 ent = bfd_alloc (abfd, amt);
4439 if (ent == NULL)
4440 return FALSE;
4441 ent->next = *plist;
4442 ent->addend = addend;
4443 ent->plt.refcount = 0;
4444 *plist = ent;
4445 }
4446 ent->plt.refcount += 1;
4447 return TRUE;
4448 }
4449
4450 static bfd_boolean
4451 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4452 {
4453 return (r_type == R_PPC64_REL24
4454 || r_type == R_PPC64_REL24_NOTOC
4455 || r_type == R_PPC64_REL14
4456 || r_type == R_PPC64_REL14_BRTAKEN
4457 || r_type == R_PPC64_REL14_BRNTAKEN
4458 || r_type == R_PPC64_ADDR24
4459 || r_type == R_PPC64_ADDR14
4460 || r_type == R_PPC64_ADDR14_BRTAKEN
4461 || r_type == R_PPC64_ADDR14_BRNTAKEN
4462 || r_type == R_PPC64_PLTCALL
4463 || r_type == R_PPC64_PLTCALL_NOTOC);
4464 }
4465
4466 /* Relocs on inline plt call sequence insns prior to the call. */
4467
4468 static bfd_boolean
4469 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4470 {
4471 return (r_type == R_PPC64_PLT16_HA
4472 || r_type == R_PPC64_PLT16_HI
4473 || r_type == R_PPC64_PLT16_LO
4474 || r_type == R_PPC64_PLT16_LO_DS
4475 || r_type == R_PPC64_PLT_PCREL34
4476 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4477 || r_type == R_PPC64_PLTSEQ
4478 || r_type == R_PPC64_PLTSEQ_NOTOC);
4479 }
4480
4481 /* Look through the relocs for a section during the first phase, and
4482 calculate needed space in the global offset table, procedure
4483 linkage table, and dynamic reloc sections. */
4484
4485 static bfd_boolean
4486 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4487 asection *sec, const Elf_Internal_Rela *relocs)
4488 {
4489 struct ppc_link_hash_table *htab;
4490 Elf_Internal_Shdr *symtab_hdr;
4491 struct elf_link_hash_entry **sym_hashes;
4492 const Elf_Internal_Rela *rel;
4493 const Elf_Internal_Rela *rel_end;
4494 asection *sreloc;
4495 struct elf_link_hash_entry *tga, *dottga;
4496 bfd_boolean is_opd;
4497
4498 if (bfd_link_relocatable (info))
4499 return TRUE;
4500
4501 /* Don't do anything special with non-loaded, non-alloced sections.
4502 In particular, any relocs in such sections should not affect GOT
4503 and PLT reference counting (ie. we don't allow them to create GOT
4504 or PLT entries), there's no possibility or desire to optimize TLS
4505 relocs, and there's not much point in propagating relocs to shared
4506 libs that the dynamic linker won't relocate. */
4507 if ((sec->flags & SEC_ALLOC) == 0)
4508 return TRUE;
4509
4510 BFD_ASSERT (is_ppc64_elf (abfd));
4511
4512 htab = ppc_hash_table (info);
4513 if (htab == NULL)
4514 return FALSE;
4515
4516 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4517 FALSE, FALSE, TRUE);
4518 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4519 FALSE, FALSE, TRUE);
4520 symtab_hdr = &elf_symtab_hdr (abfd);
4521 sym_hashes = elf_sym_hashes (abfd);
4522 sreloc = NULL;
4523 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4524 rel_end = relocs + sec->reloc_count;
4525 for (rel = relocs; rel < rel_end; rel++)
4526 {
4527 unsigned long r_symndx;
4528 struct elf_link_hash_entry *h;
4529 enum elf_ppc64_reloc_type r_type;
4530 int tls_type;
4531 struct _ppc64_elf_section_data *ppc64_sec;
4532 struct plt_entry **ifunc, **plt_list;
4533
4534 r_symndx = ELF64_R_SYM (rel->r_info);
4535 if (r_symndx < symtab_hdr->sh_info)
4536 h = NULL;
4537 else
4538 {
4539 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4540 h = elf_follow_link (h);
4541
4542 if (h == htab->elf.hgot)
4543 sec->has_toc_reloc = 1;
4544 }
4545
4546 tls_type = 0;
4547 ifunc = NULL;
4548 r_type = ELF64_R_TYPE (rel->r_info);
4549 switch (r_type)
4550 {
4551 case R_PPC64_D34:
4552 case R_PPC64_D34_LO:
4553 case R_PPC64_D34_HI30:
4554 case R_PPC64_D34_HA30:
4555 case R_PPC64_D28:
4556 case R_PPC64_TPREL34:
4557 case R_PPC64_DTPREL34:
4558 case R_PPC64_PCREL34:
4559 case R_PPC64_GOT_PCREL34:
4560 case R_PPC64_GOT_TLSGD34:
4561 case R_PPC64_GOT_TLSLD34:
4562 case R_PPC64_GOT_TPREL34:
4563 case R_PPC64_GOT_DTPREL34:
4564 case R_PPC64_PLT_PCREL34:
4565 case R_PPC64_PLT_PCREL34_NOTOC:
4566 case R_PPC64_PCREL28:
4567 htab->powerxx_stubs = 1;
4568 break;
4569 default:
4570 break;
4571 }
4572
4573 switch (r_type)
4574 {
4575 case R_PPC64_PLT16_HA:
4576 case R_PPC64_GOT_TLSLD16_HA:
4577 case R_PPC64_GOT_TLSGD16_HA:
4578 case R_PPC64_GOT_TPREL16_HA:
4579 case R_PPC64_GOT_DTPREL16_HA:
4580 case R_PPC64_GOT16_HA:
4581 case R_PPC64_TOC16_HA:
4582 case R_PPC64_PLT16_LO:
4583 case R_PPC64_PLT16_LO_DS:
4584 case R_PPC64_GOT_TLSLD16_LO:
4585 case R_PPC64_GOT_TLSGD16_LO:
4586 case R_PPC64_GOT_TPREL16_LO_DS:
4587 case R_PPC64_GOT_DTPREL16_LO_DS:
4588 case R_PPC64_GOT16_LO:
4589 case R_PPC64_GOT16_LO_DS:
4590 case R_PPC64_TOC16_LO:
4591 case R_PPC64_TOC16_LO_DS:
4592 case R_PPC64_GOT_PCREL34:
4593 ppc64_elf_tdata (abfd)->has_optrel = 1;
4594 ppc64_elf_section_data (sec)->has_optrel = 1;
4595 break;
4596 default:
4597 break;
4598 }
4599
4600 if (h != NULL)
4601 {
4602 if (h->type == STT_GNU_IFUNC)
4603 {
4604 h->needs_plt = 1;
4605 ifunc = &h->plt.plist;
4606 }
4607 }
4608 else
4609 {
4610 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4611 abfd, r_symndx);
4612 if (isym == NULL)
4613 return FALSE;
4614
4615 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4616 {
4617 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4618 rel->r_addend,
4619 NON_GOT | PLT_IFUNC);
4620 if (ifunc == NULL)
4621 return FALSE;
4622 }
4623 }
4624
4625 switch (r_type)
4626 {
4627 case R_PPC64_TLSGD:
4628 case R_PPC64_TLSLD:
4629 /* These special tls relocs tie a call to __tls_get_addr with
4630 its parameter symbol. */
4631 if (h != NULL)
4632 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4633 else
4634 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4635 rel->r_addend,
4636 NON_GOT | TLS_TLS | TLS_MARK))
4637 return FALSE;
4638 sec->has_tls_reloc = 1;
4639 break;
4640
4641 case R_PPC64_GOT_TLSLD16:
4642 case R_PPC64_GOT_TLSLD16_LO:
4643 case R_PPC64_GOT_TLSLD16_HI:
4644 case R_PPC64_GOT_TLSLD16_HA:
4645 case R_PPC64_GOT_TLSLD34:
4646 tls_type = TLS_TLS | TLS_LD;
4647 goto dogottls;
4648
4649 case R_PPC64_GOT_TLSGD16:
4650 case R_PPC64_GOT_TLSGD16_LO:
4651 case R_PPC64_GOT_TLSGD16_HI:
4652 case R_PPC64_GOT_TLSGD16_HA:
4653 case R_PPC64_GOT_TLSGD34:
4654 tls_type = TLS_TLS | TLS_GD;
4655 goto dogottls;
4656
4657 case R_PPC64_GOT_TPREL16_DS:
4658 case R_PPC64_GOT_TPREL16_LO_DS:
4659 case R_PPC64_GOT_TPREL16_HI:
4660 case R_PPC64_GOT_TPREL16_HA:
4661 case R_PPC64_GOT_TPREL34:
4662 if (bfd_link_dll (info))
4663 info->flags |= DF_STATIC_TLS;
4664 tls_type = TLS_TLS | TLS_TPREL;
4665 goto dogottls;
4666
4667 case R_PPC64_GOT_DTPREL16_DS:
4668 case R_PPC64_GOT_DTPREL16_LO_DS:
4669 case R_PPC64_GOT_DTPREL16_HI:
4670 case R_PPC64_GOT_DTPREL16_HA:
4671 case R_PPC64_GOT_DTPREL34:
4672 tls_type = TLS_TLS | TLS_DTPREL;
4673 dogottls:
4674 sec->has_tls_reloc = 1;
4675 goto dogot;
4676
4677 case R_PPC64_GOT16:
4678 case R_PPC64_GOT16_LO:
4679 case R_PPC64_GOT16_HI:
4680 case R_PPC64_GOT16_HA:
4681 case R_PPC64_GOT16_DS:
4682 case R_PPC64_GOT16_LO_DS:
4683 case R_PPC64_GOT_PCREL34:
4684 dogot:
4685 /* This symbol requires a global offset table entry. */
4686 sec->has_toc_reloc = 1;
4687 if (r_type == R_PPC64_GOT_TLSLD16
4688 || r_type == R_PPC64_GOT_TLSGD16
4689 || r_type == R_PPC64_GOT_TPREL16_DS
4690 || r_type == R_PPC64_GOT_DTPREL16_DS
4691 || r_type == R_PPC64_GOT16
4692 || r_type == R_PPC64_GOT16_DS)
4693 {
4694 htab->do_multi_toc = 1;
4695 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4696 }
4697
4698 if (ppc64_elf_tdata (abfd)->got == NULL
4699 && !create_got_section (abfd, info))
4700 return FALSE;
4701
4702 if (h != NULL)
4703 {
4704 struct ppc_link_hash_entry *eh;
4705 struct got_entry *ent;
4706
4707 eh = (struct ppc_link_hash_entry *) h;
4708 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4709 if (ent->addend == rel->r_addend
4710 && ent->owner == abfd
4711 && ent->tls_type == tls_type)
4712 break;
4713 if (ent == NULL)
4714 {
4715 bfd_size_type amt = sizeof (*ent);
4716 ent = bfd_alloc (abfd, amt);
4717 if (ent == NULL)
4718 return FALSE;
4719 ent->next = eh->elf.got.glist;
4720 ent->addend = rel->r_addend;
4721 ent->owner = abfd;
4722 ent->tls_type = tls_type;
4723 ent->is_indirect = FALSE;
4724 ent->got.refcount = 0;
4725 eh->elf.got.glist = ent;
4726 }
4727 ent->got.refcount += 1;
4728 eh->tls_mask |= tls_type;
4729 }
4730 else
4731 /* This is a global offset table entry for a local symbol. */
4732 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4733 rel->r_addend, tls_type))
4734 return FALSE;
4735
4736 /* We may also need a plt entry if the symbol turns out to be
4737 an ifunc. */
4738 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
4739 {
4740 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4741 return FALSE;
4742 }
4743 break;
4744
4745 case R_PPC64_PLT16_HA:
4746 case R_PPC64_PLT16_HI:
4747 case R_PPC64_PLT16_LO:
4748 case R_PPC64_PLT16_LO_DS:
4749 case R_PPC64_PLT_PCREL34:
4750 case R_PPC64_PLT_PCREL34_NOTOC:
4751 case R_PPC64_PLT32:
4752 case R_PPC64_PLT64:
4753 /* This symbol requires a procedure linkage table entry. */
4754 plt_list = ifunc;
4755 if (h != NULL)
4756 {
4757 h->needs_plt = 1;
4758 if (h->root.root.string[0] == '.'
4759 && h->root.root.string[1] != '\0')
4760 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4761 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
4762 plt_list = &h->plt.plist;
4763 }
4764 if (plt_list == NULL)
4765 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4766 rel->r_addend,
4767 NON_GOT | PLT_KEEP);
4768 if (!update_plt_info (abfd, plt_list, rel->r_addend))
4769 return FALSE;
4770 break;
4771
4772 /* The following relocations don't need to propagate the
4773 relocation if linking a shared object since they are
4774 section relative. */
4775 case R_PPC64_SECTOFF:
4776 case R_PPC64_SECTOFF_LO:
4777 case R_PPC64_SECTOFF_HI:
4778 case R_PPC64_SECTOFF_HA:
4779 case R_PPC64_SECTOFF_DS:
4780 case R_PPC64_SECTOFF_LO_DS:
4781 case R_PPC64_DTPREL16:
4782 case R_PPC64_DTPREL16_LO:
4783 case R_PPC64_DTPREL16_HI:
4784 case R_PPC64_DTPREL16_HA:
4785 case R_PPC64_DTPREL16_DS:
4786 case R_PPC64_DTPREL16_LO_DS:
4787 case R_PPC64_DTPREL16_HIGH:
4788 case R_PPC64_DTPREL16_HIGHA:
4789 case R_PPC64_DTPREL16_HIGHER:
4790 case R_PPC64_DTPREL16_HIGHERA:
4791 case R_PPC64_DTPREL16_HIGHEST:
4792 case R_PPC64_DTPREL16_HIGHESTA:
4793 break;
4794
4795 /* Nor do these. */
4796 case R_PPC64_REL16:
4797 case R_PPC64_REL16_LO:
4798 case R_PPC64_REL16_HI:
4799 case R_PPC64_REL16_HA:
4800 case R_PPC64_REL16_HIGH:
4801 case R_PPC64_REL16_HIGHA:
4802 case R_PPC64_REL16_HIGHER:
4803 case R_PPC64_REL16_HIGHERA:
4804 case R_PPC64_REL16_HIGHEST:
4805 case R_PPC64_REL16_HIGHESTA:
4806 case R_PPC64_REL16_HIGHER34:
4807 case R_PPC64_REL16_HIGHERA34:
4808 case R_PPC64_REL16_HIGHEST34:
4809 case R_PPC64_REL16_HIGHESTA34:
4810 case R_PPC64_REL16DX_HA:
4811 break;
4812
4813 /* Not supported as a dynamic relocation. */
4814 case R_PPC64_ADDR64_LOCAL:
4815 if (bfd_link_pic (info))
4816 {
4817 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4818 ppc_howto_init ();
4819 /* xgettext:c-format */
4820 info->callbacks->einfo (_("%H: %s reloc unsupported "
4821 "in shared libraries and PIEs\n"),
4822 abfd, sec, rel->r_offset,
4823 ppc64_elf_howto_table[r_type]->name);
4824 bfd_set_error (bfd_error_bad_value);
4825 return FALSE;
4826 }
4827 break;
4828
4829 case R_PPC64_TOC16:
4830 case R_PPC64_TOC16_DS:
4831 htab->do_multi_toc = 1;
4832 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4833 /* Fall through. */
4834 case R_PPC64_TOC16_LO:
4835 case R_PPC64_TOC16_HI:
4836 case R_PPC64_TOC16_HA:
4837 case R_PPC64_TOC16_LO_DS:
4838 sec->has_toc_reloc = 1;
4839 if (h != NULL && !bfd_link_pic (info))
4840 {
4841 /* We may need a copy reloc. */
4842 h->non_got_ref = 1;
4843 /* Strongly prefer a copy reloc over a dynamic reloc.
4844 glibc ld.so as of 2019-08 will error out if one of
4845 these relocations is emitted. */
4846 h->needs_copy = 1;
4847 goto dodyn;
4848 }
4849 break;
4850
4851 /* Marker reloc. */
4852 case R_PPC64_ENTRY:
4853 break;
4854
4855 /* This relocation describes the C++ object vtable hierarchy.
4856 Reconstruct it for later use during GC. */
4857 case R_PPC64_GNU_VTINHERIT:
4858 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4859 return FALSE;
4860 break;
4861
4862 /* This relocation describes which C++ vtable entries are actually
4863 used. Record for later use during GC. */
4864 case R_PPC64_GNU_VTENTRY:
4865 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4866 return FALSE;
4867 break;
4868
4869 case R_PPC64_REL14:
4870 case R_PPC64_REL14_BRTAKEN:
4871 case R_PPC64_REL14_BRNTAKEN:
4872 {
4873 asection *dest = NULL;
4874
4875 /* Heuristic: If jumping outside our section, chances are
4876 we are going to need a stub. */
4877 if (h != NULL)
4878 {
4879 /* If the sym is weak it may be overridden later, so
4880 don't assume we know where a weak sym lives. */
4881 if (h->root.type == bfd_link_hash_defined)
4882 dest = h->root.u.def.section;
4883 }
4884 else
4885 {
4886 Elf_Internal_Sym *isym;
4887
4888 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4889 abfd, r_symndx);
4890 if (isym == NULL)
4891 return FALSE;
4892
4893 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4894 }
4895
4896 if (dest != sec)
4897 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4898 }
4899 goto rel24;
4900
4901 case R_PPC64_PLTCALL:
4902 case R_PPC64_PLTCALL_NOTOC:
4903 ppc64_elf_section_data (sec)->has_pltcall = 1;
4904 /* Fall through. */
4905
4906 case R_PPC64_REL24:
4907 case R_PPC64_REL24_NOTOC:
4908 rel24:
4909 plt_list = ifunc;
4910 if (h != NULL)
4911 {
4912 h->needs_plt = 1;
4913 if (h->root.root.string[0] == '.'
4914 && h->root.root.string[1] != '\0')
4915 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4916
4917 if (h == tga || h == dottga)
4918 {
4919 sec->has_tls_reloc = 1;
4920 if (rel != relocs
4921 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4922 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4923 /* We have a new-style __tls_get_addr call with
4924 a marker reloc. */
4925 ;
4926 else
4927 /* Mark this section as having an old-style call. */
4928 sec->has_tls_get_addr_call = 1;
4929 }
4930 plt_list = &h->plt.plist;
4931 }
4932
4933 /* We may need a .plt entry if the function this reloc
4934 refers to is in a shared lib. */
4935 if (plt_list
4936 && !update_plt_info (abfd, plt_list, rel->r_addend))
4937 return FALSE;
4938 break;
4939
4940 case R_PPC64_ADDR14:
4941 case R_PPC64_ADDR14_BRNTAKEN:
4942 case R_PPC64_ADDR14_BRTAKEN:
4943 case R_PPC64_ADDR24:
4944 goto dodyn;
4945
4946 case R_PPC64_TPREL64:
4947 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4948 if (bfd_link_dll (info))
4949 info->flags |= DF_STATIC_TLS;
4950 goto dotlstoc;
4951
4952 case R_PPC64_DTPMOD64:
4953 if (rel + 1 < rel_end
4954 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4955 && rel[1].r_offset == rel->r_offset + 8)
4956 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4957 else
4958 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4959 goto dotlstoc;
4960
4961 case R_PPC64_DTPREL64:
4962 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4963 if (rel != relocs
4964 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4965 && rel[-1].r_offset == rel->r_offset - 8)
4966 /* This is the second reloc of a dtpmod, dtprel pair.
4967 Don't mark with TLS_DTPREL. */
4968 goto dodyn;
4969
4970 dotlstoc:
4971 sec->has_tls_reloc = 1;
4972 if (h != NULL)
4973 {
4974 struct ppc_link_hash_entry *eh;
4975 eh = (struct ppc_link_hash_entry *) h;
4976 eh->tls_mask |= tls_type & 0xff;
4977 }
4978 else
4979 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4980 rel->r_addend, tls_type))
4981 return FALSE;
4982
4983 ppc64_sec = ppc64_elf_section_data (sec);
4984 if (ppc64_sec->sec_type != sec_toc)
4985 {
4986 bfd_size_type amt;
4987
4988 /* One extra to simplify get_tls_mask. */
4989 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4990 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4991 if (ppc64_sec->u.toc.symndx == NULL)
4992 return FALSE;
4993 amt = sec->size * sizeof (bfd_vma) / 8;
4994 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4995 if (ppc64_sec->u.toc.add == NULL)
4996 return FALSE;
4997 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4998 ppc64_sec->sec_type = sec_toc;
4999 }
5000 BFD_ASSERT (rel->r_offset % 8 == 0);
5001 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5002 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5003
5004 /* Mark the second slot of a GD or LD entry.
5005 -1 to indicate GD and -2 to indicate LD. */
5006 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5007 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5008 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5009 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5010 goto dodyn;
5011
5012 case R_PPC64_TPREL16:
5013 case R_PPC64_TPREL16_LO:
5014 case R_PPC64_TPREL16_HI:
5015 case R_PPC64_TPREL16_HA:
5016 case R_PPC64_TPREL16_DS:
5017 case R_PPC64_TPREL16_LO_DS:
5018 case R_PPC64_TPREL16_HIGH:
5019 case R_PPC64_TPREL16_HIGHA:
5020 case R_PPC64_TPREL16_HIGHER:
5021 case R_PPC64_TPREL16_HIGHERA:
5022 case R_PPC64_TPREL16_HIGHEST:
5023 case R_PPC64_TPREL16_HIGHESTA:
5024 case R_PPC64_TPREL34:
5025 if (bfd_link_dll (info))
5026 info->flags |= DF_STATIC_TLS;
5027 goto dodyn;
5028
5029 case R_PPC64_ADDR64:
5030 if (is_opd
5031 && rel + 1 < rel_end
5032 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5033 {
5034 if (h != NULL)
5035 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5036 }
5037 /* Fall through. */
5038
5039 case R_PPC64_ADDR16:
5040 case R_PPC64_ADDR16_DS:
5041 case R_PPC64_ADDR16_HA:
5042 case R_PPC64_ADDR16_HI:
5043 case R_PPC64_ADDR16_HIGH:
5044 case R_PPC64_ADDR16_HIGHA:
5045 case R_PPC64_ADDR16_HIGHER:
5046 case R_PPC64_ADDR16_HIGHERA:
5047 case R_PPC64_ADDR16_HIGHEST:
5048 case R_PPC64_ADDR16_HIGHESTA:
5049 case R_PPC64_ADDR16_LO:
5050 case R_PPC64_ADDR16_LO_DS:
5051 case R_PPC64_D34:
5052 case R_PPC64_D34_LO:
5053 case R_PPC64_D34_HI30:
5054 case R_PPC64_D34_HA30:
5055 case R_PPC64_ADDR16_HIGHER34:
5056 case R_PPC64_ADDR16_HIGHERA34:
5057 case R_PPC64_ADDR16_HIGHEST34:
5058 case R_PPC64_ADDR16_HIGHESTA34:
5059 case R_PPC64_D28:
5060 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5061 && rel->r_addend == 0)
5062 {
5063 /* We may need a .plt entry if this reloc refers to a
5064 function in a shared lib. */
5065 if (!update_plt_info (abfd, &h->plt.plist, 0))
5066 return FALSE;
5067 h->pointer_equality_needed = 1;
5068 }
5069 /* Fall through. */
5070
5071 case R_PPC64_REL30:
5072 case R_PPC64_REL32:
5073 case R_PPC64_REL64:
5074 case R_PPC64_ADDR32:
5075 case R_PPC64_UADDR16:
5076 case R_PPC64_UADDR32:
5077 case R_PPC64_UADDR64:
5078 case R_PPC64_TOC:
5079 if (h != NULL && !bfd_link_pic (info))
5080 /* We may need a copy reloc. */
5081 h->non_got_ref = 1;
5082
5083 /* Don't propagate .opd relocs. */
5084 if (NO_OPD_RELOCS && is_opd)
5085 break;
5086
5087 /* If we are creating a shared library, and this is a reloc
5088 against a global symbol, or a non PC relative reloc
5089 against a local symbol, then we need to copy the reloc
5090 into the shared library. However, if we are linking with
5091 -Bsymbolic, we do not need to copy a reloc against a
5092 global symbol which is defined in an object we are
5093 including in the link (i.e., DEF_REGULAR is set). At
5094 this point we have not seen all the input files, so it is
5095 possible that DEF_REGULAR is not set now but will be set
5096 later (it is never cleared). In case of a weak definition,
5097 DEF_REGULAR may be cleared later by a strong definition in
5098 a shared library. We account for that possibility below by
5099 storing information in the dyn_relocs field of the hash
5100 table entry. A similar situation occurs when creating
5101 shared libraries and symbol visibility changes render the
5102 symbol local.
5103
5104 If on the other hand, we are creating an executable, we
5105 may need to keep relocations for symbols satisfied by a
5106 dynamic library if we manage to avoid copy relocs for the
5107 symbol. */
5108 dodyn:
5109 if ((bfd_link_pic (info)
5110 && (must_be_dyn_reloc (info, r_type)
5111 || (h != NULL
5112 && (!SYMBOLIC_BIND (info, h)
5113 || h->root.type == bfd_link_hash_defweak
5114 || !h->def_regular))))
5115 || (ELIMINATE_COPY_RELOCS
5116 && !bfd_link_pic (info)
5117 && h != NULL
5118 && (h->root.type == bfd_link_hash_defweak
5119 || !h->def_regular))
5120 || (!bfd_link_pic (info)
5121 && ifunc != NULL))
5122 {
5123 /* We must copy these reloc types into the output file.
5124 Create a reloc section in dynobj and make room for
5125 this reloc. */
5126 if (sreloc == NULL)
5127 {
5128 sreloc = _bfd_elf_make_dynamic_reloc_section
5129 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5130
5131 if (sreloc == NULL)
5132 return FALSE;
5133 }
5134
5135 /* If this is a global symbol, we count the number of
5136 relocations we need for this symbol. */
5137 if (h != NULL)
5138 {
5139 struct elf_dyn_relocs *p;
5140 struct elf_dyn_relocs **head;
5141
5142 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5143 p = *head;
5144 if (p == NULL || p->sec != sec)
5145 {
5146 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5147 if (p == NULL)
5148 return FALSE;
5149 p->next = *head;
5150 *head = p;
5151 p->sec = sec;
5152 p->count = 0;
5153 p->pc_count = 0;
5154 }
5155 p->count += 1;
5156 if (!must_be_dyn_reloc (info, r_type))
5157 p->pc_count += 1;
5158 }
5159 else
5160 {
5161 /* Track dynamic relocs needed for local syms too.
5162 We really need local syms available to do this
5163 easily. Oh well. */
5164 struct ppc_dyn_relocs *p;
5165 struct ppc_dyn_relocs **head;
5166 bfd_boolean is_ifunc;
5167 asection *s;
5168 void *vpp;
5169 Elf_Internal_Sym *isym;
5170
5171 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5172 abfd, r_symndx);
5173 if (isym == NULL)
5174 return FALSE;
5175
5176 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5177 if (s == NULL)
5178 s = sec;
5179
5180 vpp = &elf_section_data (s)->local_dynrel;
5181 head = (struct ppc_dyn_relocs **) vpp;
5182 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5183 p = *head;
5184 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5185 p = p->next;
5186 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5187 {
5188 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5189 if (p == NULL)
5190 return FALSE;
5191 p->next = *head;
5192 *head = p;
5193 p->sec = sec;
5194 p->ifunc = is_ifunc;
5195 p->count = 0;
5196 }
5197 p->count += 1;
5198 }
5199 }
5200 break;
5201
5202 default:
5203 break;
5204 }
5205 }
5206
5207 return TRUE;
5208 }
5209
5210 /* Merge backend specific data from an object file to the output
5211 object file when linking. */
5212
5213 static bfd_boolean
5214 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5215 {
5216 bfd *obfd = info->output_bfd;
5217 unsigned long iflags, oflags;
5218
5219 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5220 return TRUE;
5221
5222 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5223 return TRUE;
5224
5225 if (!_bfd_generic_verify_endian_match (ibfd, info))
5226 return FALSE;
5227
5228 iflags = elf_elfheader (ibfd)->e_flags;
5229 oflags = elf_elfheader (obfd)->e_flags;
5230
5231 if (iflags & ~EF_PPC64_ABI)
5232 {
5233 _bfd_error_handler
5234 /* xgettext:c-format */
5235 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5236 bfd_set_error (bfd_error_bad_value);
5237 return FALSE;
5238 }
5239 else if (iflags != oflags && iflags != 0)
5240 {
5241 _bfd_error_handler
5242 /* xgettext:c-format */
5243 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5244 ibfd, iflags, oflags);
5245 bfd_set_error (bfd_error_bad_value);
5246 return FALSE;
5247 }
5248
5249 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5250 return FALSE;
5251
5252 /* Merge Tag_compatibility attributes and any common GNU ones. */
5253 return _bfd_elf_merge_object_attributes (ibfd, info);
5254 }
5255
5256 static bfd_boolean
5257 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5258 {
5259 /* Print normal ELF private data. */
5260 _bfd_elf_print_private_bfd_data (abfd, ptr);
5261
5262 if (elf_elfheader (abfd)->e_flags != 0)
5263 {
5264 FILE *file = ptr;
5265
5266 fprintf (file, _("private flags = 0x%lx:"),
5267 elf_elfheader (abfd)->e_flags);
5268
5269 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5270 fprintf (file, _(" [abiv%ld]"),
5271 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5272 fputc ('\n', file);
5273 }
5274
5275 return TRUE;
5276 }
5277
5278 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5279 of the code entry point, and its section, which must be in the same
5280 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5281
5282 static bfd_vma
5283 opd_entry_value (asection *opd_sec,
5284 bfd_vma offset,
5285 asection **code_sec,
5286 bfd_vma *code_off,
5287 bfd_boolean in_code_sec)
5288 {
5289 bfd *opd_bfd = opd_sec->owner;
5290 Elf_Internal_Rela *relocs;
5291 Elf_Internal_Rela *lo, *hi, *look;
5292 bfd_vma val;
5293
5294 /* No relocs implies we are linking a --just-symbols object, or looking
5295 at a final linked executable with addr2line or somesuch. */
5296 if (opd_sec->reloc_count == 0)
5297 {
5298 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5299
5300 if (contents == NULL)
5301 {
5302 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5303 return (bfd_vma) -1;
5304 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5305 }
5306
5307 /* PR 17512: file: 64b9dfbb. */
5308 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5309 return (bfd_vma) -1;
5310
5311 val = bfd_get_64 (opd_bfd, contents + offset);
5312 if (code_sec != NULL)
5313 {
5314 asection *sec, *likely = NULL;
5315
5316 if (in_code_sec)
5317 {
5318 sec = *code_sec;
5319 if (sec->vma <= val
5320 && val < sec->vma + sec->size)
5321 likely = sec;
5322 else
5323 val = -1;
5324 }
5325 else
5326 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5327 if (sec->vma <= val
5328 && (sec->flags & SEC_LOAD) != 0
5329 && (sec->flags & SEC_ALLOC) != 0)
5330 likely = sec;
5331 if (likely != NULL)
5332 {
5333 *code_sec = likely;
5334 if (code_off != NULL)
5335 *code_off = val - likely->vma;
5336 }
5337 }
5338 return val;
5339 }
5340
5341 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5342
5343 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5344 if (relocs == NULL)
5345 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5346 /* PR 17512: file: df8e1fd6. */
5347 if (relocs == NULL)
5348 return (bfd_vma) -1;
5349
5350 /* Go find the opd reloc at the sym address. */
5351 lo = relocs;
5352 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5353 val = (bfd_vma) -1;
5354 while (lo < hi)
5355 {
5356 look = lo + (hi - lo) / 2;
5357 if (look->r_offset < offset)
5358 lo = look + 1;
5359 else if (look->r_offset > offset)
5360 hi = look;
5361 else
5362 {
5363 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5364
5365 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5366 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5367 {
5368 unsigned long symndx = ELF64_R_SYM (look->r_info);
5369 asection *sec = NULL;
5370
5371 if (symndx >= symtab_hdr->sh_info
5372 && elf_sym_hashes (opd_bfd) != NULL)
5373 {
5374 struct elf_link_hash_entry **sym_hashes;
5375 struct elf_link_hash_entry *rh;
5376
5377 sym_hashes = elf_sym_hashes (opd_bfd);
5378 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5379 if (rh != NULL)
5380 {
5381 rh = elf_follow_link (rh);
5382 if (rh->root.type != bfd_link_hash_defined
5383 && rh->root.type != bfd_link_hash_defweak)
5384 break;
5385 if (rh->root.u.def.section->owner == opd_bfd)
5386 {
5387 val = rh->root.u.def.value;
5388 sec = rh->root.u.def.section;
5389 }
5390 }
5391 }
5392
5393 if (sec == NULL)
5394 {
5395 Elf_Internal_Sym *sym;
5396
5397 if (symndx < symtab_hdr->sh_info)
5398 {
5399 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5400 if (sym == NULL)
5401 {
5402 size_t symcnt = symtab_hdr->sh_info;
5403 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5404 symcnt, 0,
5405 NULL, NULL, NULL);
5406 if (sym == NULL)
5407 break;
5408 symtab_hdr->contents = (bfd_byte *) sym;
5409 }
5410 sym += symndx;
5411 }
5412 else
5413 {
5414 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5415 1, symndx,
5416 NULL, NULL, NULL);
5417 if (sym == NULL)
5418 break;
5419 }
5420 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5421 if (sec == NULL)
5422 break;
5423 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5424 val = sym->st_value;
5425 }
5426
5427 val += look->r_addend;
5428 if (code_off != NULL)
5429 *code_off = val;
5430 if (code_sec != NULL)
5431 {
5432 if (in_code_sec && *code_sec != sec)
5433 return -1;
5434 else
5435 *code_sec = sec;
5436 }
5437 if (sec->output_section != NULL)
5438 val += sec->output_section->vma + sec->output_offset;
5439 }
5440 break;
5441 }
5442 }
5443
5444 return val;
5445 }
5446
5447 /* If the ELF symbol SYM might be a function in SEC, return the
5448 function size and set *CODE_OFF to the function's entry point,
5449 otherwise return zero. */
5450
5451 static bfd_size_type
5452 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5453 bfd_vma *code_off)
5454 {
5455 bfd_size_type size;
5456
5457 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5458 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5459 return 0;
5460
5461 size = 0;
5462 if (!(sym->flags & BSF_SYNTHETIC))
5463 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5464
5465 if (strcmp (sym->section->name, ".opd") == 0)
5466 {
5467 struct _opd_sec_data *opd = get_opd_info (sym->section);
5468 bfd_vma symval = sym->value;
5469
5470 if (opd != NULL
5471 && opd->adjust != NULL
5472 && elf_section_data (sym->section)->relocs != NULL)
5473 {
5474 /* opd_entry_value will use cached relocs that have been
5475 adjusted, but with raw symbols. That means both local
5476 and global symbols need adjusting. */
5477 long adjust = opd->adjust[OPD_NDX (symval)];
5478 if (adjust == -1)
5479 return 0;
5480 symval += adjust;
5481 }
5482
5483 if (opd_entry_value (sym->section, symval,
5484 &sec, code_off, TRUE) == (bfd_vma) -1)
5485 return 0;
5486 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5487 symbol. This size has nothing to do with the code size of the
5488 function, which is what we're supposed to return, but the
5489 code size isn't available without looking up the dot-sym.
5490 However, doing that would be a waste of time particularly
5491 since elf_find_function will look at the dot-sym anyway.
5492 Now, elf_find_function will keep the largest size of any
5493 function sym found at the code address of interest, so return
5494 1 here to avoid it incorrectly caching a larger function size
5495 for a small function. This does mean we return the wrong
5496 size for a new-ABI function of size 24, but all that does is
5497 disable caching for such functions. */
5498 if (size == 24)
5499 size = 1;
5500 }
5501 else
5502 {
5503 if (sym->section != sec)
5504 return 0;
5505 *code_off = sym->value;
5506 }
5507 if (size == 0)
5508 size = 1;
5509 return size;
5510 }
5511
5512 /* Return true if symbol is a strong function defined in an ELFv2
5513 object with st_other localentry bits of zero, ie. its local entry
5514 point coincides with its global entry point. */
5515
5516 static bfd_boolean
5517 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5518 {
5519 return (h != NULL
5520 && h->type == STT_FUNC
5521 && h->root.type == bfd_link_hash_defined
5522 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5523 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
5524 && is_ppc64_elf (h->root.u.def.section->owner)
5525 && abiversion (h->root.u.def.section->owner) >= 2);
5526 }
5527
5528 /* Return true if symbol is defined in a regular object file. */
5529
5530 static bfd_boolean
5531 is_static_defined (struct elf_link_hash_entry *h)
5532 {
5533 return ((h->root.type == bfd_link_hash_defined
5534 || h->root.type == bfd_link_hash_defweak)
5535 && h->root.u.def.section != NULL
5536 && h->root.u.def.section->output_section != NULL);
5537 }
5538
5539 /* If FDH is a function descriptor symbol, return the associated code
5540 entry symbol if it is defined. Return NULL otherwise. */
5541
5542 static struct ppc_link_hash_entry *
5543 defined_code_entry (struct ppc_link_hash_entry *fdh)
5544 {
5545 if (fdh->is_func_descriptor)
5546 {
5547 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5548 if (fh->elf.root.type == bfd_link_hash_defined
5549 || fh->elf.root.type == bfd_link_hash_defweak)
5550 return fh;
5551 }
5552 return NULL;
5553 }
5554
5555 /* If FH is a function code entry symbol, return the associated
5556 function descriptor symbol if it is defined. Return NULL otherwise. */
5557
5558 static struct ppc_link_hash_entry *
5559 defined_func_desc (struct ppc_link_hash_entry *fh)
5560 {
5561 if (fh->oh != NULL
5562 && fh->oh->is_func_descriptor)
5563 {
5564 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5565 if (fdh->elf.root.type == bfd_link_hash_defined
5566 || fdh->elf.root.type == bfd_link_hash_defweak)
5567 return fdh;
5568 }
5569 return NULL;
5570 }
5571
5572 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5573
5574 /* Garbage collect sections, after first dealing with dot-symbols. */
5575
5576 static bfd_boolean
5577 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5578 {
5579 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5580
5581 if (htab != NULL && htab->need_func_desc_adj)
5582 {
5583 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5584 htab->need_func_desc_adj = 0;
5585 }
5586 return bfd_elf_gc_sections (abfd, info);
5587 }
5588
5589 /* Mark all our entry sym sections, both opd and code section. */
5590
5591 static void
5592 ppc64_elf_gc_keep (struct bfd_link_info *info)
5593 {
5594 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5595 struct bfd_sym_chain *sym;
5596
5597 if (htab == NULL)
5598 return;
5599
5600 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5601 {
5602 struct ppc_link_hash_entry *eh, *fh;
5603 asection *sec;
5604
5605 eh = (struct ppc_link_hash_entry *)
5606 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5607 if (eh == NULL)
5608 continue;
5609 if (eh->elf.root.type != bfd_link_hash_defined
5610 && eh->elf.root.type != bfd_link_hash_defweak)
5611 continue;
5612
5613 fh = defined_code_entry (eh);
5614 if (fh != NULL)
5615 {
5616 sec = fh->elf.root.u.def.section;
5617 sec->flags |= SEC_KEEP;
5618 }
5619 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5620 && opd_entry_value (eh->elf.root.u.def.section,
5621 eh->elf.root.u.def.value,
5622 &sec, NULL, FALSE) != (bfd_vma) -1)
5623 sec->flags |= SEC_KEEP;
5624
5625 sec = eh->elf.root.u.def.section;
5626 sec->flags |= SEC_KEEP;
5627 }
5628 }
5629
5630 /* Mark sections containing dynamically referenced symbols. When
5631 building shared libraries, we must assume that any visible symbol is
5632 referenced. */
5633
5634 static bfd_boolean
5635 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5636 {
5637 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5638 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5639 struct ppc_link_hash_entry *fdh;
5640 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5641
5642 /* Dynamic linking info is on the func descriptor sym. */
5643 fdh = defined_func_desc (eh);
5644 if (fdh != NULL)
5645 eh = fdh;
5646
5647 if ((eh->elf.root.type == bfd_link_hash_defined
5648 || eh->elf.root.type == bfd_link_hash_defweak)
5649 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5650 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5651 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5652 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5653 && (!bfd_link_executable (info)
5654 || info->gc_keep_exported
5655 || info->export_dynamic
5656 || (eh->elf.dynamic
5657 && d != NULL
5658 && (*d->match) (&d->head, NULL,
5659 eh->elf.root.root.string)))
5660 && (eh->elf.versioned >= versioned
5661 || !bfd_hide_sym_by_version (info->version_info,
5662 eh->elf.root.root.string)))))
5663 {
5664 asection *code_sec;
5665 struct ppc_link_hash_entry *fh;
5666
5667 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5668
5669 /* Function descriptor syms cause the associated
5670 function code sym section to be marked. */
5671 fh = defined_code_entry (eh);
5672 if (fh != NULL)
5673 {
5674 code_sec = fh->elf.root.u.def.section;
5675 code_sec->flags |= SEC_KEEP;
5676 }
5677 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5678 && opd_entry_value (eh->elf.root.u.def.section,
5679 eh->elf.root.u.def.value,
5680 &code_sec, NULL, FALSE) != (bfd_vma) -1)
5681 code_sec->flags |= SEC_KEEP;
5682 }
5683
5684 return TRUE;
5685 }
5686
5687 /* Return the section that should be marked against GC for a given
5688 relocation. */
5689
5690 static asection *
5691 ppc64_elf_gc_mark_hook (asection *sec,
5692 struct bfd_link_info *info,
5693 Elf_Internal_Rela *rel,
5694 struct elf_link_hash_entry *h,
5695 Elf_Internal_Sym *sym)
5696 {
5697 asection *rsec;
5698
5699 /* Syms return NULL if we're marking .opd, so we avoid marking all
5700 function sections, as all functions are referenced in .opd. */
5701 rsec = NULL;
5702 if (get_opd_info (sec) != NULL)
5703 return rsec;
5704
5705 if (h != NULL)
5706 {
5707 enum elf_ppc64_reloc_type r_type;
5708 struct ppc_link_hash_entry *eh, *fh, *fdh;
5709
5710 r_type = ELF64_R_TYPE (rel->r_info);
5711 switch (r_type)
5712 {
5713 case R_PPC64_GNU_VTINHERIT:
5714 case R_PPC64_GNU_VTENTRY:
5715 break;
5716
5717 default:
5718 switch (h->root.type)
5719 {
5720 case bfd_link_hash_defined:
5721 case bfd_link_hash_defweak:
5722 eh = (struct ppc_link_hash_entry *) h;
5723 fdh = defined_func_desc (eh);
5724 if (fdh != NULL)
5725 {
5726 /* -mcall-aixdesc code references the dot-symbol on
5727 a call reloc. Mark the function descriptor too
5728 against garbage collection. */
5729 fdh->elf.mark = 1;
5730 if (fdh->elf.is_weakalias)
5731 weakdef (&fdh->elf)->mark = 1;
5732 eh = fdh;
5733 }
5734
5735 /* Function descriptor syms cause the associated
5736 function code sym section to be marked. */
5737 fh = defined_code_entry (eh);
5738 if (fh != NULL)
5739 {
5740 /* They also mark their opd section. */
5741 eh->elf.root.u.def.section->gc_mark = 1;
5742
5743 rsec = fh->elf.root.u.def.section;
5744 }
5745 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5746 && opd_entry_value (eh->elf.root.u.def.section,
5747 eh->elf.root.u.def.value,
5748 &rsec, NULL, FALSE) != (bfd_vma) -1)
5749 eh->elf.root.u.def.section->gc_mark = 1;
5750 else
5751 rsec = h->root.u.def.section;
5752 break;
5753
5754 case bfd_link_hash_common:
5755 rsec = h->root.u.c.p->section;
5756 break;
5757
5758 default:
5759 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5760 }
5761 }
5762 }
5763 else
5764 {
5765 struct _opd_sec_data *opd;
5766
5767 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5768 opd = get_opd_info (rsec);
5769 if (opd != NULL && opd->func_sec != NULL)
5770 {
5771 rsec->gc_mark = 1;
5772
5773 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
5774 }
5775 }
5776
5777 return rsec;
5778 }
5779
5780 /* The maximum size of .sfpr. */
5781 #define SFPR_MAX (218*4)
5782
5783 struct sfpr_def_parms
5784 {
5785 const char name[12];
5786 unsigned char lo, hi;
5787 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5788 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
5789 };
5790
5791 /* Auto-generate _save*, _rest* functions in .sfpr.
5792 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5793 instead. */
5794
5795 static bfd_boolean
5796 sfpr_define (struct bfd_link_info *info,
5797 const struct sfpr_def_parms *parm,
5798 asection *stub_sec)
5799 {
5800 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5801 unsigned int i;
5802 size_t len = strlen (parm->name);
5803 bfd_boolean writing = FALSE;
5804 char sym[16];
5805
5806 if (htab == NULL)
5807 return FALSE;
5808
5809 memcpy (sym, parm->name, len);
5810 sym[len + 2] = 0;
5811
5812 for (i = parm->lo; i <= parm->hi; i++)
5813 {
5814 struct ppc_link_hash_entry *h;
5815
5816 sym[len + 0] = i / 10 + '0';
5817 sym[len + 1] = i % 10 + '0';
5818 h = (struct ppc_link_hash_entry *)
5819 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
5820 if (stub_sec != NULL)
5821 {
5822 if (h != NULL
5823 && h->elf.root.type == bfd_link_hash_defined
5824 && h->elf.root.u.def.section == htab->sfpr)
5825 {
5826 struct elf_link_hash_entry *s;
5827 char buf[32];
5828 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5829 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5830 if (s == NULL)
5831 return FALSE;
5832 if (s->root.type == bfd_link_hash_new
5833 || (s->root.type = bfd_link_hash_defined
5834 && s->root.u.def.section == stub_sec))
5835 {
5836 s->root.type = bfd_link_hash_defined;
5837 s->root.u.def.section = stub_sec;
5838 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
5839 + h->elf.root.u.def.value);
5840 s->ref_regular = 1;
5841 s->def_regular = 1;
5842 s->ref_regular_nonweak = 1;
5843 s->forced_local = 1;
5844 s->non_elf = 0;
5845 s->root.linker_def = 1;
5846 }
5847 }
5848 continue;
5849 }
5850 if (h != NULL)
5851 {
5852 h->save_res = 1;
5853 if (!h->elf.def_regular)
5854 {
5855 h->elf.root.type = bfd_link_hash_defined;
5856 h->elf.root.u.def.section = htab->sfpr;
5857 h->elf.root.u.def.value = htab->sfpr->size;
5858 h->elf.type = STT_FUNC;
5859 h->elf.def_regular = 1;
5860 h->elf.non_elf = 0;
5861 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5862 writing = TRUE;
5863 if (htab->sfpr->contents == NULL)
5864 {
5865 htab->sfpr->contents
5866 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5867 if (htab->sfpr->contents == NULL)
5868 return FALSE;
5869 }
5870 }
5871 }
5872 if (writing)
5873 {
5874 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5875 if (i != parm->hi)
5876 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5877 else
5878 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5879 htab->sfpr->size = p - htab->sfpr->contents;
5880 }
5881 }
5882
5883 return TRUE;
5884 }
5885
5886 static bfd_byte *
5887 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5888 {
5889 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5890 return p + 4;
5891 }
5892
5893 static bfd_byte *
5894 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5895 {
5896 p = savegpr0 (abfd, p, r);
5897 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5898 p = p + 4;
5899 bfd_put_32 (abfd, BLR, p);
5900 return p + 4;
5901 }
5902
5903 static bfd_byte *
5904 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5905 {
5906 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5907 return p + 4;
5908 }
5909
5910 static bfd_byte *
5911 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5912 {
5913 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5914 p = p + 4;
5915 p = restgpr0 (abfd, p, r);
5916 bfd_put_32 (abfd, MTLR_R0, p);
5917 p = p + 4;
5918 if (r == 29)
5919 {
5920 p = restgpr0 (abfd, p, 30);
5921 p = restgpr0 (abfd, p, 31);
5922 }
5923 bfd_put_32 (abfd, BLR, p);
5924 return p + 4;
5925 }
5926
5927 static bfd_byte *
5928 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5929 {
5930 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5931 return p + 4;
5932 }
5933
5934 static bfd_byte *
5935 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5936 {
5937 p = savegpr1 (abfd, p, r);
5938 bfd_put_32 (abfd, BLR, p);
5939 return p + 4;
5940 }
5941
5942 static bfd_byte *
5943 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5944 {
5945 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5946 return p + 4;
5947 }
5948
5949 static bfd_byte *
5950 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5951 {
5952 p = restgpr1 (abfd, p, r);
5953 bfd_put_32 (abfd, BLR, p);
5954 return p + 4;
5955 }
5956
5957 static bfd_byte *
5958 savefpr (bfd *abfd, bfd_byte *p, int r)
5959 {
5960 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5961 return p + 4;
5962 }
5963
5964 static bfd_byte *
5965 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5966 {
5967 p = savefpr (abfd, p, r);
5968 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5969 p = p + 4;
5970 bfd_put_32 (abfd, BLR, p);
5971 return p + 4;
5972 }
5973
5974 static bfd_byte *
5975 restfpr (bfd *abfd, bfd_byte *p, int r)
5976 {
5977 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5978 return p + 4;
5979 }
5980
5981 static bfd_byte *
5982 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5983 {
5984 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5985 p = p + 4;
5986 p = restfpr (abfd, p, r);
5987 bfd_put_32 (abfd, MTLR_R0, p);
5988 p = p + 4;
5989 if (r == 29)
5990 {
5991 p = restfpr (abfd, p, 30);
5992 p = restfpr (abfd, p, 31);
5993 }
5994 bfd_put_32 (abfd, BLR, p);
5995 return p + 4;
5996 }
5997
5998 static bfd_byte *
5999 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6000 {
6001 p = savefpr (abfd, p, r);
6002 bfd_put_32 (abfd, BLR, p);
6003 return p + 4;
6004 }
6005
6006 static bfd_byte *
6007 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6008 {
6009 p = restfpr (abfd, p, r);
6010 bfd_put_32 (abfd, BLR, p);
6011 return p + 4;
6012 }
6013
6014 static bfd_byte *
6015 savevr (bfd *abfd, bfd_byte *p, int r)
6016 {
6017 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6018 p = p + 4;
6019 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6020 return p + 4;
6021 }
6022
6023 static bfd_byte *
6024 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6025 {
6026 p = savevr (abfd, p, r);
6027 bfd_put_32 (abfd, BLR, p);
6028 return p + 4;
6029 }
6030
6031 static bfd_byte *
6032 restvr (bfd *abfd, bfd_byte *p, int r)
6033 {
6034 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6035 p = p + 4;
6036 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6037 return p + 4;
6038 }
6039
6040 static bfd_byte *
6041 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6042 {
6043 p = restvr (abfd, p, r);
6044 bfd_put_32 (abfd, BLR, p);
6045 return p + 4;
6046 }
6047
6048 /* Called via elf_link_hash_traverse to transfer dynamic linking
6049 information on function code symbol entries to their corresponding
6050 function descriptor symbol entries. */
6051
6052 static bfd_boolean
6053 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6054 {
6055 struct bfd_link_info *info;
6056 struct ppc_link_hash_table *htab;
6057 struct ppc_link_hash_entry *fh;
6058 struct ppc_link_hash_entry *fdh;
6059 bfd_boolean force_local;
6060
6061 fh = (struct ppc_link_hash_entry *) h;
6062 if (fh->elf.root.type == bfd_link_hash_indirect)
6063 return TRUE;
6064
6065 if (!fh->is_func)
6066 return TRUE;
6067
6068 if (fh->elf.root.root.string[0] != '.'
6069 || fh->elf.root.root.string[1] == '\0')
6070 return TRUE;
6071
6072 info = inf;
6073 htab = ppc_hash_table (info);
6074 if (htab == NULL)
6075 return FALSE;
6076
6077 /* Find the corresponding function descriptor symbol. */
6078 fdh = lookup_fdh (fh, htab);
6079
6080 /* Resolve undefined references to dot-symbols as the value
6081 in the function descriptor, if we have one in a regular object.
6082 This is to satisfy cases like ".quad .foo". Calls to functions
6083 in dynamic objects are handled elsewhere. */
6084 if ((fh->elf.root.type == bfd_link_hash_undefined
6085 || fh->elf.root.type == bfd_link_hash_undefweak)
6086 && (fdh->elf.root.type == bfd_link_hash_defined
6087 || fdh->elf.root.type == bfd_link_hash_defweak)
6088 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6089 && opd_entry_value (fdh->elf.root.u.def.section,
6090 fdh->elf.root.u.def.value,
6091 &fh->elf.root.u.def.section,
6092 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6093 {
6094 fh->elf.root.type = fdh->elf.root.type;
6095 fh->elf.forced_local = 1;
6096 fh->elf.def_regular = fdh->elf.def_regular;
6097 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6098 }
6099
6100 if (!fh->elf.dynamic)
6101 {
6102 struct plt_entry *ent;
6103
6104 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6105 if (ent->plt.refcount > 0)
6106 break;
6107 if (ent == NULL)
6108 return TRUE;
6109 }
6110
6111 /* Create a descriptor as undefined if necessary. */
6112 if (fdh == NULL
6113 && !bfd_link_executable (info)
6114 && (fh->elf.root.type == bfd_link_hash_undefined
6115 || fh->elf.root.type == bfd_link_hash_undefweak))
6116 {
6117 fdh = make_fdh (info, fh);
6118 if (fdh == NULL)
6119 return FALSE;
6120 }
6121
6122 /* We can't support overriding of symbols on a fake descriptor. */
6123 if (fdh != NULL
6124 && fdh->fake
6125 && (fh->elf.root.type == bfd_link_hash_defined
6126 || fh->elf.root.type == bfd_link_hash_defweak))
6127 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6128
6129 /* Transfer dynamic linking information to the function descriptor. */
6130 if (fdh != NULL)
6131 {
6132 fdh->elf.ref_regular |= fh->elf.ref_regular;
6133 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6134 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6135 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6136 fdh->elf.dynamic |= fh->elf.dynamic;
6137 fdh->elf.needs_plt |= (fh->elf.needs_plt
6138 || fh->elf.type == STT_FUNC
6139 || fh->elf.type == STT_GNU_IFUNC);
6140 move_plt_plist (fh, fdh);
6141
6142 if (!fdh->elf.forced_local
6143 && fh->elf.dynindx != -1)
6144 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6145 return FALSE;
6146 }
6147
6148 /* Now that the info is on the function descriptor, clear the
6149 function code sym info. Any function code syms for which we
6150 don't have a definition in a regular file, we force local.
6151 This prevents a shared library from exporting syms that have
6152 been imported from another library. Function code syms that
6153 are really in the library we must leave global to prevent the
6154 linker dragging in a definition from a static library. */
6155 force_local = (!fh->elf.def_regular
6156 || fdh == NULL
6157 || !fdh->elf.def_regular
6158 || fdh->elf.forced_local);
6159 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6160
6161 return TRUE;
6162 }
6163
6164 static const struct sfpr_def_parms save_res_funcs[] =
6165 {
6166 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6167 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6168 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6169 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6170 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6171 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6172 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6173 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6174 { "._savef", 14, 31, savefpr, savefpr1_tail },
6175 { "._restf", 14, 31, restfpr, restfpr1_tail },
6176 { "_savevr_", 20, 31, savevr, savevr_tail },
6177 { "_restvr_", 20, 31, restvr, restvr_tail }
6178 };
6179
6180 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6181 this hook to a) provide some gcc support functions, and b) transfer
6182 dynamic linking information gathered so far on function code symbol
6183 entries, to their corresponding function descriptor symbol entries. */
6184
6185 static bfd_boolean
6186 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6187 struct bfd_link_info *info)
6188 {
6189 struct ppc_link_hash_table *htab;
6190
6191 htab = ppc_hash_table (info);
6192 if (htab == NULL)
6193 return FALSE;
6194
6195 /* Provide any missing _save* and _rest* functions. */
6196 if (htab->sfpr != NULL)
6197 {
6198 unsigned int i;
6199
6200 htab->sfpr->size = 0;
6201 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6202 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6203 return FALSE;
6204 if (htab->sfpr->size == 0)
6205 htab->sfpr->flags |= SEC_EXCLUDE;
6206 }
6207
6208 if (bfd_link_relocatable (info))
6209 return TRUE;
6210
6211 if (htab->elf.hgot != NULL)
6212 {
6213 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6214 /* Make .TOC. defined so as to prevent it being made dynamic.
6215 The wrong value here is fixed later in ppc64_elf_set_toc. */
6216 if (!htab->elf.hgot->def_regular
6217 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6218 {
6219 htab->elf.hgot->root.type = bfd_link_hash_defined;
6220 htab->elf.hgot->root.u.def.value = 0;
6221 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6222 htab->elf.hgot->def_regular = 1;
6223 htab->elf.hgot->root.linker_def = 1;
6224 }
6225 htab->elf.hgot->type = STT_OBJECT;
6226 htab->elf.hgot->other
6227 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6228 }
6229
6230 if (htab->need_func_desc_adj)
6231 {
6232 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6233 htab->need_func_desc_adj = 0;
6234 }
6235
6236 return TRUE;
6237 }
6238
6239 /* Find dynamic relocs for H that apply to read-only sections. */
6240
6241 static asection *
6242 readonly_dynrelocs (struct elf_link_hash_entry *h)
6243 {
6244 struct ppc_link_hash_entry *eh;
6245 struct elf_dyn_relocs *p;
6246
6247 eh = (struct ppc_link_hash_entry *) h;
6248 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6249 {
6250 asection *s = p->sec->output_section;
6251
6252 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6253 return p->sec;
6254 }
6255 return NULL;
6256 }
6257
6258 /* Return true if we have dynamic relocs against H or any of its weak
6259 aliases, that apply to read-only sections. Cannot be used after
6260 size_dynamic_sections. */
6261
6262 static bfd_boolean
6263 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6264 {
6265 struct ppc_link_hash_entry *eh;
6266
6267 eh = (struct ppc_link_hash_entry *) h;
6268 do
6269 {
6270 if (readonly_dynrelocs (&eh->elf))
6271 return TRUE;
6272 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
6273 }
6274 while (eh != NULL && &eh->elf != h);
6275
6276 return FALSE;
6277 }
6278
6279 /* Return whether EH has pc-relative dynamic relocs. */
6280
6281 static bfd_boolean
6282 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6283 {
6284 struct elf_dyn_relocs *p;
6285
6286 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6287 if (p->pc_count != 0)
6288 return TRUE;
6289 return FALSE;
6290 }
6291
6292 /* Return true if a global entry stub will be created for H. Valid
6293 for ELFv2 before plt entries have been allocated. */
6294
6295 static bfd_boolean
6296 global_entry_stub (struct elf_link_hash_entry *h)
6297 {
6298 struct plt_entry *pent;
6299
6300 if (!h->pointer_equality_needed
6301 || h->def_regular)
6302 return FALSE;
6303
6304 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6305 if (pent->plt.refcount > 0
6306 && pent->addend == 0)
6307 return TRUE;
6308
6309 return FALSE;
6310 }
6311
6312 /* Adjust a symbol defined by a dynamic object and referenced by a
6313 regular object. The current definition is in some section of the
6314 dynamic object, but we're not including those sections. We have to
6315 change the definition to something the rest of the link can
6316 understand. */
6317
6318 static bfd_boolean
6319 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6320 struct elf_link_hash_entry *h)
6321 {
6322 struct ppc_link_hash_table *htab;
6323 asection *s, *srel;
6324
6325 htab = ppc_hash_table (info);
6326 if (htab == NULL)
6327 return FALSE;
6328
6329 /* Deal with function syms. */
6330 if (h->type == STT_FUNC
6331 || h->type == STT_GNU_IFUNC
6332 || h->needs_plt)
6333 {
6334 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6335 || SYMBOL_CALLS_LOCAL (info, h)
6336 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6337 /* Discard dyn_relocs when non-pic if we've decided that a
6338 function symbol is local and not an ifunc. We keep dynamic
6339 relocs for ifuncs when local rather than always emitting a
6340 plt call stub for them and defining the symbol on the call
6341 stub. We can't do that for ELFv1 anyway (a function symbol
6342 is defined on a descriptor, not code) and it can be faster at
6343 run-time due to not needing to bounce through a stub. The
6344 dyn_relocs for ifuncs will be applied even in a static
6345 executable. */
6346 if (!bfd_link_pic (info)
6347 && h->type != STT_GNU_IFUNC
6348 && local)
6349 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6350
6351 /* Clear procedure linkage table information for any symbol that
6352 won't need a .plt entry. */
6353 struct plt_entry *ent;
6354 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6355 if (ent->plt.refcount > 0)
6356 break;
6357 if (ent == NULL
6358 || (h->type != STT_GNU_IFUNC
6359 && local
6360 && (htab->can_convert_all_inline_plt
6361 || (((struct ppc_link_hash_entry *) h)->tls_mask
6362 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6363 {
6364 h->plt.plist = NULL;
6365 h->needs_plt = 0;
6366 h->pointer_equality_needed = 0;
6367 }
6368 else if (abiversion (info->output_bfd) >= 2)
6369 {
6370 /* Taking a function's address in a read/write section
6371 doesn't require us to define the function symbol in the
6372 executable on a global entry stub. A dynamic reloc can
6373 be used instead. The reason we prefer a few more dynamic
6374 relocs is that calling via a global entry stub costs a
6375 few more instructions, and pointer_equality_needed causes
6376 extra work in ld.so when resolving these symbols. */
6377 if (global_entry_stub (h))
6378 {
6379 if (!readonly_dynrelocs (h))
6380 {
6381 h->pointer_equality_needed = 0;
6382 /* If we haven't seen a branch reloc and the symbol
6383 isn't an ifunc then we don't need a plt entry. */
6384 if (!h->needs_plt)
6385 h->plt.plist = NULL;
6386 }
6387 else if (!bfd_link_pic (info))
6388 /* We are going to be defining the function symbol on the
6389 plt stub, so no dyn_relocs needed when non-pic. */
6390 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6391 }
6392
6393 /* ELFv2 function symbols can't have copy relocs. */
6394 return TRUE;
6395 }
6396 else if (!h->needs_plt
6397 && !readonly_dynrelocs (h))
6398 {
6399 /* If we haven't seen a branch reloc and the symbol isn't an
6400 ifunc then we don't need a plt entry. */
6401 h->plt.plist = NULL;
6402 h->pointer_equality_needed = 0;
6403 return TRUE;
6404 }
6405 }
6406 else
6407 h->plt.plist = NULL;
6408
6409 /* If this is a weak symbol, and there is a real definition, the
6410 processor independent code will have arranged for us to see the
6411 real definition first, and we can just use the same value. */
6412 if (h->is_weakalias)
6413 {
6414 struct elf_link_hash_entry *def = weakdef (h);
6415 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6416 h->root.u.def.section = def->root.u.def.section;
6417 h->root.u.def.value = def->root.u.def.value;
6418 if (def->root.u.def.section == htab->elf.sdynbss
6419 || def->root.u.def.section == htab->elf.sdynrelro)
6420 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6421 return TRUE;
6422 }
6423
6424 /* If we are creating a shared library, we must presume that the
6425 only references to the symbol are via the global offset table.
6426 For such cases we need not do anything here; the relocations will
6427 be handled correctly by relocate_section. */
6428 if (bfd_link_pic (info))
6429 return TRUE;
6430
6431 /* If there are no references to this symbol that do not use the
6432 GOT, we don't need to generate a copy reloc. */
6433 if (!h->non_got_ref)
6434 return TRUE;
6435
6436 /* Don't generate a copy reloc for symbols defined in the executable. */
6437 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6438
6439 /* If -z nocopyreloc was given, don't generate them either. */
6440 || info->nocopyreloc
6441
6442 /* If we don't find any dynamic relocs in read-only sections, then
6443 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6444 || (ELIMINATE_COPY_RELOCS
6445 && !h->needs_copy
6446 && !alias_readonly_dynrelocs (h))
6447
6448 /* Protected variables do not work with .dynbss. The copy in
6449 .dynbss won't be used by the shared library with the protected
6450 definition for the variable. Text relocations are preferable
6451 to an incorrect program. */
6452 || h->protected_def)
6453 return TRUE;
6454
6455 if (h->plt.plist != NULL)
6456 {
6457 /* We should never get here, but unfortunately there are versions
6458 of gcc out there that improperly (for this ABI) put initialized
6459 function pointers, vtable refs and suchlike in read-only
6460 sections. Allow them to proceed, but warn that this might
6461 break at runtime. */
6462 info->callbacks->einfo
6463 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6464 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6465 h->root.root.string);
6466 }
6467
6468 /* This is a reference to a symbol defined by a dynamic object which
6469 is not a function. */
6470
6471 /* We must allocate the symbol in our .dynbss section, which will
6472 become part of the .bss section of the executable. There will be
6473 an entry for this symbol in the .dynsym section. The dynamic
6474 object will contain position independent code, so all references
6475 from the dynamic object to this symbol will go through the global
6476 offset table. The dynamic linker will use the .dynsym entry to
6477 determine the address it must put in the global offset table, so
6478 both the dynamic object and the regular object will refer to the
6479 same memory location for the variable. */
6480 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6481 {
6482 s = htab->elf.sdynrelro;
6483 srel = htab->elf.sreldynrelro;
6484 }
6485 else
6486 {
6487 s = htab->elf.sdynbss;
6488 srel = htab->elf.srelbss;
6489 }
6490 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6491 {
6492 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6493 linker to copy the initial value out of the dynamic object
6494 and into the runtime process image. */
6495 srel->size += sizeof (Elf64_External_Rela);
6496 h->needs_copy = 1;
6497 }
6498
6499 /* We no longer want dyn_relocs. */
6500 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6501 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6502 }
6503
6504 /* If given a function descriptor symbol, hide both the function code
6505 sym and the descriptor. */
6506 static void
6507 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6508 struct elf_link_hash_entry *h,
6509 bfd_boolean force_local)
6510 {
6511 struct ppc_link_hash_entry *eh;
6512 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6513
6514 if (ppc_hash_table (info) == NULL)
6515 return;
6516
6517 eh = (struct ppc_link_hash_entry *) h;
6518 if (eh->is_func_descriptor)
6519 {
6520 struct ppc_link_hash_entry *fh = eh->oh;
6521
6522 if (fh == NULL)
6523 {
6524 const char *p, *q;
6525 struct elf_link_hash_table *htab = elf_hash_table (info);
6526 char save;
6527
6528 /* We aren't supposed to use alloca in BFD because on
6529 systems which do not have alloca the version in libiberty
6530 calls xmalloc, which might cause the program to crash
6531 when it runs out of memory. This function doesn't have a
6532 return status, so there's no way to gracefully return an
6533 error. So cheat. We know that string[-1] can be safely
6534 accessed; It's either a string in an ELF string table,
6535 or allocated in an objalloc structure. */
6536
6537 p = eh->elf.root.root.string - 1;
6538 save = *p;
6539 *(char *) p = '.';
6540 fh = (struct ppc_link_hash_entry *)
6541 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6542 *(char *) p = save;
6543
6544 /* Unfortunately, if it so happens that the string we were
6545 looking for was allocated immediately before this string,
6546 then we overwrote the string terminator. That's the only
6547 reason the lookup should fail. */
6548 if (fh == NULL)
6549 {
6550 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6551 while (q >= eh->elf.root.root.string && *q == *p)
6552 --q, --p;
6553 if (q < eh->elf.root.root.string && *p == '.')
6554 fh = (struct ppc_link_hash_entry *)
6555 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6556 }
6557 if (fh != NULL)
6558 {
6559 eh->oh = fh;
6560 fh->oh = eh;
6561 }
6562 }
6563 if (fh != NULL)
6564 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6565 }
6566 }
6567
6568 static bfd_boolean
6569 get_sym_h (struct elf_link_hash_entry **hp,
6570 Elf_Internal_Sym **symp,
6571 asection **symsecp,
6572 unsigned char **tls_maskp,
6573 Elf_Internal_Sym **locsymsp,
6574 unsigned long r_symndx,
6575 bfd *ibfd)
6576 {
6577 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6578
6579 if (r_symndx >= symtab_hdr->sh_info)
6580 {
6581 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6582 struct elf_link_hash_entry *h;
6583
6584 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6585 h = elf_follow_link (h);
6586
6587 if (hp != NULL)
6588 *hp = h;
6589
6590 if (symp != NULL)
6591 *symp = NULL;
6592
6593 if (symsecp != NULL)
6594 {
6595 asection *symsec = NULL;
6596 if (h->root.type == bfd_link_hash_defined
6597 || h->root.type == bfd_link_hash_defweak)
6598 symsec = h->root.u.def.section;
6599 *symsecp = symsec;
6600 }
6601
6602 if (tls_maskp != NULL)
6603 {
6604 struct ppc_link_hash_entry *eh;
6605
6606 eh = (struct ppc_link_hash_entry *) h;
6607 *tls_maskp = &eh->tls_mask;
6608 }
6609 }
6610 else
6611 {
6612 Elf_Internal_Sym *sym;
6613 Elf_Internal_Sym *locsyms = *locsymsp;
6614
6615 if (locsyms == NULL)
6616 {
6617 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6618 if (locsyms == NULL)
6619 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6620 symtab_hdr->sh_info,
6621 0, NULL, NULL, NULL);
6622 if (locsyms == NULL)
6623 return FALSE;
6624 *locsymsp = locsyms;
6625 }
6626 sym = locsyms + r_symndx;
6627
6628 if (hp != NULL)
6629 *hp = NULL;
6630
6631 if (symp != NULL)
6632 *symp = sym;
6633
6634 if (symsecp != NULL)
6635 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6636
6637 if (tls_maskp != NULL)
6638 {
6639 struct got_entry **lgot_ents;
6640 unsigned char *tls_mask;
6641
6642 tls_mask = NULL;
6643 lgot_ents = elf_local_got_ents (ibfd);
6644 if (lgot_ents != NULL)
6645 {
6646 struct plt_entry **local_plt = (struct plt_entry **)
6647 (lgot_ents + symtab_hdr->sh_info);
6648 unsigned char *lgot_masks = (unsigned char *)
6649 (local_plt + symtab_hdr->sh_info);
6650 tls_mask = &lgot_masks[r_symndx];
6651 }
6652 *tls_maskp = tls_mask;
6653 }
6654 }
6655 return TRUE;
6656 }
6657
6658 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6659 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6660 type suitable for optimization, and 1 otherwise. */
6661
6662 static int
6663 get_tls_mask (unsigned char **tls_maskp,
6664 unsigned long *toc_symndx,
6665 bfd_vma *toc_addend,
6666 Elf_Internal_Sym **locsymsp,
6667 const Elf_Internal_Rela *rel,
6668 bfd *ibfd)
6669 {
6670 unsigned long r_symndx;
6671 int next_r;
6672 struct elf_link_hash_entry *h;
6673 Elf_Internal_Sym *sym;
6674 asection *sec;
6675 bfd_vma off;
6676
6677 r_symndx = ELF64_R_SYM (rel->r_info);
6678 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6679 return 0;
6680
6681 if ((*tls_maskp != NULL
6682 && (**tls_maskp & TLS_TLS) != 0
6683 && **tls_maskp != (TLS_TLS | TLS_MARK))
6684 || sec == NULL
6685 || ppc64_elf_section_data (sec) == NULL
6686 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6687 return 1;
6688
6689 /* Look inside a TOC section too. */
6690 if (h != NULL)
6691 {
6692 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6693 off = h->root.u.def.value;
6694 }
6695 else
6696 off = sym->st_value;
6697 off += rel->r_addend;
6698 BFD_ASSERT (off % 8 == 0);
6699 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6700 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6701 if (toc_symndx != NULL)
6702 *toc_symndx = r_symndx;
6703 if (toc_addend != NULL)
6704 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6705 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6706 return 0;
6707 if ((h == NULL || is_static_defined (h))
6708 && (next_r == -1 || next_r == -2))
6709 return 1 - next_r;
6710 return 1;
6711 }
6712
6713 /* Find (or create) an entry in the tocsave hash table. */
6714
6715 static struct tocsave_entry *
6716 tocsave_find (struct ppc_link_hash_table *htab,
6717 enum insert_option insert,
6718 Elf_Internal_Sym **local_syms,
6719 const Elf_Internal_Rela *irela,
6720 bfd *ibfd)
6721 {
6722 unsigned long r_indx;
6723 struct elf_link_hash_entry *h;
6724 Elf_Internal_Sym *sym;
6725 struct tocsave_entry ent, *p;
6726 hashval_t hash;
6727 struct tocsave_entry **slot;
6728
6729 r_indx = ELF64_R_SYM (irela->r_info);
6730 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6731 return NULL;
6732 if (ent.sec == NULL || ent.sec->output_section == NULL)
6733 {
6734 _bfd_error_handler
6735 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
6736 return NULL;
6737 }
6738
6739 if (h != NULL)
6740 ent.offset = h->root.u.def.value;
6741 else
6742 ent.offset = sym->st_value;
6743 ent.offset += irela->r_addend;
6744
6745 hash = tocsave_htab_hash (&ent);
6746 slot = ((struct tocsave_entry **)
6747 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6748 if (slot == NULL)
6749 return NULL;
6750
6751 if (*slot == NULL)
6752 {
6753 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6754 if (p == NULL)
6755 return NULL;
6756 *p = ent;
6757 *slot = p;
6758 }
6759 return *slot;
6760 }
6761
6762 /* Adjust all global syms defined in opd sections. In gcc generated
6763 code for the old ABI, these will already have been done. */
6764
6765 static bfd_boolean
6766 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6767 {
6768 struct ppc_link_hash_entry *eh;
6769 asection *sym_sec;
6770 struct _opd_sec_data *opd;
6771
6772 if (h->root.type == bfd_link_hash_indirect)
6773 return TRUE;
6774
6775 if (h->root.type != bfd_link_hash_defined
6776 && h->root.type != bfd_link_hash_defweak)
6777 return TRUE;
6778
6779 eh = (struct ppc_link_hash_entry *) h;
6780 if (eh->adjust_done)
6781 return TRUE;
6782
6783 sym_sec = eh->elf.root.u.def.section;
6784 opd = get_opd_info (sym_sec);
6785 if (opd != NULL && opd->adjust != NULL)
6786 {
6787 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
6788 if (adjust == -1)
6789 {
6790 /* This entry has been deleted. */
6791 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6792 if (dsec == NULL)
6793 {
6794 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6795 if (discarded_section (dsec))
6796 {
6797 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6798 break;
6799 }
6800 }
6801 eh->elf.root.u.def.value = 0;
6802 eh->elf.root.u.def.section = dsec;
6803 }
6804 else
6805 eh->elf.root.u.def.value += adjust;
6806 eh->adjust_done = 1;
6807 }
6808 return TRUE;
6809 }
6810
6811 /* Handles decrementing dynamic reloc counts for the reloc specified by
6812 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
6813 have already been determined. */
6814
6815 static bfd_boolean
6816 dec_dynrel_count (bfd_vma r_info,
6817 asection *sec,
6818 struct bfd_link_info *info,
6819 Elf_Internal_Sym **local_syms,
6820 struct elf_link_hash_entry *h,
6821 Elf_Internal_Sym *sym)
6822 {
6823 enum elf_ppc64_reloc_type r_type;
6824 asection *sym_sec = NULL;
6825
6826 /* Can this reloc be dynamic? This switch, and later tests here
6827 should be kept in sync with the code in check_relocs. */
6828 r_type = ELF64_R_TYPE (r_info);
6829 switch (r_type)
6830 {
6831 default:
6832 return TRUE;
6833
6834 case R_PPC64_TOC16:
6835 case R_PPC64_TOC16_DS:
6836 case R_PPC64_TOC16_LO:
6837 case R_PPC64_TOC16_HI:
6838 case R_PPC64_TOC16_HA:
6839 case R_PPC64_TOC16_LO_DS:
6840 if (h == NULL)
6841 return TRUE;
6842 break;
6843
6844 case R_PPC64_TPREL16:
6845 case R_PPC64_TPREL16_LO:
6846 case R_PPC64_TPREL16_HI:
6847 case R_PPC64_TPREL16_HA:
6848 case R_PPC64_TPREL16_DS:
6849 case R_PPC64_TPREL16_LO_DS:
6850 case R_PPC64_TPREL16_HIGH:
6851 case R_PPC64_TPREL16_HIGHA:
6852 case R_PPC64_TPREL16_HIGHER:
6853 case R_PPC64_TPREL16_HIGHERA:
6854 case R_PPC64_TPREL16_HIGHEST:
6855 case R_PPC64_TPREL16_HIGHESTA:
6856 case R_PPC64_TPREL64:
6857 case R_PPC64_TPREL34:
6858 case R_PPC64_DTPMOD64:
6859 case R_PPC64_DTPREL64:
6860 case R_PPC64_ADDR64:
6861 case R_PPC64_REL30:
6862 case R_PPC64_REL32:
6863 case R_PPC64_REL64:
6864 case R_PPC64_ADDR14:
6865 case R_PPC64_ADDR14_BRNTAKEN:
6866 case R_PPC64_ADDR14_BRTAKEN:
6867 case R_PPC64_ADDR16:
6868 case R_PPC64_ADDR16_DS:
6869 case R_PPC64_ADDR16_HA:
6870 case R_PPC64_ADDR16_HI:
6871 case R_PPC64_ADDR16_HIGH:
6872 case R_PPC64_ADDR16_HIGHA:
6873 case R_PPC64_ADDR16_HIGHER:
6874 case R_PPC64_ADDR16_HIGHERA:
6875 case R_PPC64_ADDR16_HIGHEST:
6876 case R_PPC64_ADDR16_HIGHESTA:
6877 case R_PPC64_ADDR16_LO:
6878 case R_PPC64_ADDR16_LO_DS:
6879 case R_PPC64_ADDR24:
6880 case R_PPC64_ADDR32:
6881 case R_PPC64_UADDR16:
6882 case R_PPC64_UADDR32:
6883 case R_PPC64_UADDR64:
6884 case R_PPC64_TOC:
6885 case R_PPC64_D34:
6886 case R_PPC64_D34_LO:
6887 case R_PPC64_D34_HI30:
6888 case R_PPC64_D34_HA30:
6889 case R_PPC64_ADDR16_HIGHER34:
6890 case R_PPC64_ADDR16_HIGHERA34:
6891 case R_PPC64_ADDR16_HIGHEST34:
6892 case R_PPC64_ADDR16_HIGHESTA34:
6893 case R_PPC64_D28:
6894 break;
6895 }
6896
6897 if (local_syms != NULL)
6898 {
6899 unsigned long r_symndx;
6900 bfd *ibfd = sec->owner;
6901
6902 r_symndx = ELF64_R_SYM (r_info);
6903 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6904 return FALSE;
6905 }
6906
6907 if ((bfd_link_pic (info)
6908 && (must_be_dyn_reloc (info, r_type)
6909 || (h != NULL
6910 && (!SYMBOLIC_BIND (info, h)
6911 || h->root.type == bfd_link_hash_defweak
6912 || !h->def_regular))))
6913 || (ELIMINATE_COPY_RELOCS
6914 && !bfd_link_pic (info)
6915 && h != NULL
6916 && (h->root.type == bfd_link_hash_defweak
6917 || !h->def_regular)))
6918 ;
6919 else
6920 return TRUE;
6921
6922 if (h != NULL)
6923 {
6924 struct elf_dyn_relocs *p;
6925 struct elf_dyn_relocs **pp;
6926 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6927
6928 /* elf_gc_sweep may have already removed all dyn relocs associated
6929 with local syms for a given section. Also, symbol flags are
6930 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6931 report a dynreloc miscount. */
6932 if (*pp == NULL && info->gc_sections)
6933 return TRUE;
6934
6935 while ((p = *pp) != NULL)
6936 {
6937 if (p->sec == sec)
6938 {
6939 if (!must_be_dyn_reloc (info, r_type))
6940 p->pc_count -= 1;
6941 p->count -= 1;
6942 if (p->count == 0)
6943 *pp = p->next;
6944 return TRUE;
6945 }
6946 pp = &p->next;
6947 }
6948 }
6949 else
6950 {
6951 struct ppc_dyn_relocs *p;
6952 struct ppc_dyn_relocs **pp;
6953 void *vpp;
6954 bfd_boolean is_ifunc;
6955
6956 if (local_syms == NULL)
6957 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6958 if (sym_sec == NULL)
6959 sym_sec = sec;
6960
6961 vpp = &elf_section_data (sym_sec)->local_dynrel;
6962 pp = (struct ppc_dyn_relocs **) vpp;
6963
6964 if (*pp == NULL && info->gc_sections)
6965 return TRUE;
6966
6967 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6968 while ((p = *pp) != NULL)
6969 {
6970 if (p->sec == sec && p->ifunc == is_ifunc)
6971 {
6972 p->count -= 1;
6973 if (p->count == 0)
6974 *pp = p->next;
6975 return TRUE;
6976 }
6977 pp = &p->next;
6978 }
6979 }
6980
6981 /* xgettext:c-format */
6982 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6983 sec->owner, sec);
6984 bfd_set_error (bfd_error_bad_value);
6985 return FALSE;
6986 }
6987
6988 /* Remove unused Official Procedure Descriptor entries. Currently we
6989 only remove those associated with functions in discarded link-once
6990 sections, or weakly defined functions that have been overridden. It
6991 would be possible to remove many more entries for statically linked
6992 applications. */
6993
6994 bfd_boolean
6995 ppc64_elf_edit_opd (struct bfd_link_info *info)
6996 {
6997 bfd *ibfd;
6998 bfd_boolean some_edited = FALSE;
6999 asection *need_pad = NULL;
7000 struct ppc_link_hash_table *htab;
7001
7002 htab = ppc_hash_table (info);
7003 if (htab == NULL)
7004 return FALSE;
7005
7006 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7007 {
7008 asection *sec;
7009 Elf_Internal_Rela *relstart, *rel, *relend;
7010 Elf_Internal_Shdr *symtab_hdr;
7011 Elf_Internal_Sym *local_syms;
7012 struct _opd_sec_data *opd;
7013 bfd_boolean need_edit, add_aux_fields, broken;
7014 bfd_size_type cnt_16b = 0;
7015
7016 if (!is_ppc64_elf (ibfd))
7017 continue;
7018
7019 sec = bfd_get_section_by_name (ibfd, ".opd");
7020 if (sec == NULL || sec->size == 0)
7021 continue;
7022
7023 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7024 continue;
7025
7026 if (sec->output_section == bfd_abs_section_ptr)
7027 continue;
7028
7029 /* Look through the section relocs. */
7030 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7031 continue;
7032
7033 local_syms = NULL;
7034 symtab_hdr = &elf_symtab_hdr (ibfd);
7035
7036 /* Read the relocations. */
7037 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7038 info->keep_memory);
7039 if (relstart == NULL)
7040 return FALSE;
7041
7042 /* First run through the relocs to check they are sane, and to
7043 determine whether we need to edit this opd section. */
7044 need_edit = FALSE;
7045 broken = FALSE;
7046 need_pad = sec;
7047 relend = relstart + sec->reloc_count;
7048 for (rel = relstart; rel < relend; )
7049 {
7050 enum elf_ppc64_reloc_type r_type;
7051 unsigned long r_symndx;
7052 asection *sym_sec;
7053 struct elf_link_hash_entry *h;
7054 Elf_Internal_Sym *sym;
7055 bfd_vma offset;
7056
7057 /* .opd contains an array of 16 or 24 byte entries. We're
7058 only interested in the reloc pointing to a function entry
7059 point. */
7060 offset = rel->r_offset;
7061 if (rel + 1 == relend
7062 || rel[1].r_offset != offset + 8)
7063 {
7064 /* If someone messes with .opd alignment then after a
7065 "ld -r" we might have padding in the middle of .opd.
7066 Also, there's nothing to prevent someone putting
7067 something silly in .opd with the assembler. No .opd
7068 optimization for them! */
7069 broken_opd:
7070 _bfd_error_handler
7071 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7072 broken = TRUE;
7073 break;
7074 }
7075
7076 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7077 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7078 {
7079 _bfd_error_handler
7080 /* xgettext:c-format */
7081 (_("%pB: unexpected reloc type %u in .opd section"),
7082 ibfd, r_type);
7083 broken = TRUE;
7084 break;
7085 }
7086
7087 r_symndx = ELF64_R_SYM (rel->r_info);
7088 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7089 r_symndx, ibfd))
7090 goto error_ret;
7091
7092 if (sym_sec == NULL || sym_sec->owner == NULL)
7093 {
7094 const char *sym_name;
7095 if (h != NULL)
7096 sym_name = h->root.root.string;
7097 else
7098 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7099 sym_sec);
7100
7101 _bfd_error_handler
7102 /* xgettext:c-format */
7103 (_("%pB: undefined sym `%s' in .opd section"),
7104 ibfd, sym_name);
7105 broken = TRUE;
7106 break;
7107 }
7108
7109 /* opd entries are always for functions defined in the
7110 current input bfd. If the symbol isn't defined in the
7111 input bfd, then we won't be using the function in this
7112 bfd; It must be defined in a linkonce section in another
7113 bfd, or is weak. It's also possible that we are
7114 discarding the function due to a linker script /DISCARD/,
7115 which we test for via the output_section. */
7116 if (sym_sec->owner != ibfd
7117 || sym_sec->output_section == bfd_abs_section_ptr)
7118 need_edit = TRUE;
7119
7120 rel += 2;
7121 if (rel + 1 == relend
7122 || (rel + 2 < relend
7123 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7124 ++rel;
7125
7126 if (rel == relend)
7127 {
7128 if (sec->size == offset + 24)
7129 {
7130 need_pad = NULL;
7131 break;
7132 }
7133 if (sec->size == offset + 16)
7134 {
7135 cnt_16b++;
7136 break;
7137 }
7138 goto broken_opd;
7139 }
7140 else if (rel + 1 < relend
7141 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7142 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7143 {
7144 if (rel[0].r_offset == offset + 16)
7145 cnt_16b++;
7146 else if (rel[0].r_offset != offset + 24)
7147 goto broken_opd;
7148 }
7149 else
7150 goto broken_opd;
7151 }
7152
7153 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7154
7155 if (!broken && (need_edit || add_aux_fields))
7156 {
7157 Elf_Internal_Rela *write_rel;
7158 Elf_Internal_Shdr *rel_hdr;
7159 bfd_byte *rptr, *wptr;
7160 bfd_byte *new_contents;
7161 bfd_size_type amt;
7162
7163 new_contents = NULL;
7164 amt = OPD_NDX (sec->size) * sizeof (long);
7165 opd = &ppc64_elf_section_data (sec)->u.opd;
7166 opd->adjust = bfd_zalloc (sec->owner, amt);
7167 if (opd->adjust == NULL)
7168 return FALSE;
7169
7170 /* This seems a waste of time as input .opd sections are all
7171 zeros as generated by gcc, but I suppose there's no reason
7172 this will always be so. We might start putting something in
7173 the third word of .opd entries. */
7174 if ((sec->flags & SEC_IN_MEMORY) == 0)
7175 {
7176 bfd_byte *loc;
7177 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7178 {
7179 if (loc != NULL)
7180 free (loc);
7181 error_ret:
7182 if (local_syms != NULL
7183 && symtab_hdr->contents != (unsigned char *) local_syms)
7184 free (local_syms);
7185 if (elf_section_data (sec)->relocs != relstart)
7186 free (relstart);
7187 return FALSE;
7188 }
7189 sec->contents = loc;
7190 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7191 }
7192
7193 elf_section_data (sec)->relocs = relstart;
7194
7195 new_contents = sec->contents;
7196 if (add_aux_fields)
7197 {
7198 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7199 if (new_contents == NULL)
7200 return FALSE;
7201 need_pad = NULL;
7202 }
7203 wptr = new_contents;
7204 rptr = sec->contents;
7205 write_rel = relstart;
7206 for (rel = relstart; rel < relend; )
7207 {
7208 unsigned long r_symndx;
7209 asection *sym_sec;
7210 struct elf_link_hash_entry *h;
7211 struct ppc_link_hash_entry *fdh = NULL;
7212 Elf_Internal_Sym *sym;
7213 long opd_ent_size;
7214 Elf_Internal_Rela *next_rel;
7215 bfd_boolean skip;
7216
7217 r_symndx = ELF64_R_SYM (rel->r_info);
7218 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7219 r_symndx, ibfd))
7220 goto error_ret;
7221
7222 next_rel = rel + 2;
7223 if (next_rel + 1 == relend
7224 || (next_rel + 2 < relend
7225 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7226 ++next_rel;
7227
7228 /* See if the .opd entry is full 24 byte or
7229 16 byte (with fd_aux entry overlapped with next
7230 fd_func). */
7231 opd_ent_size = 24;
7232 if (next_rel == relend)
7233 {
7234 if (sec->size == rel->r_offset + 16)
7235 opd_ent_size = 16;
7236 }
7237 else if (next_rel->r_offset == rel->r_offset + 16)
7238 opd_ent_size = 16;
7239
7240 if (h != NULL
7241 && h->root.root.string[0] == '.')
7242 {
7243 fdh = ((struct ppc_link_hash_entry *) h)->oh;
7244 if (fdh != NULL)
7245 {
7246 fdh = ppc_follow_link (fdh);
7247 if (fdh->elf.root.type != bfd_link_hash_defined
7248 && fdh->elf.root.type != bfd_link_hash_defweak)
7249 fdh = NULL;
7250 }
7251 }
7252
7253 skip = (sym_sec->owner != ibfd
7254 || sym_sec->output_section == bfd_abs_section_ptr);
7255 if (skip)
7256 {
7257 if (fdh != NULL && sym_sec->owner == ibfd)
7258 {
7259 /* Arrange for the function descriptor sym
7260 to be dropped. */
7261 fdh->elf.root.u.def.value = 0;
7262 fdh->elf.root.u.def.section = sym_sec;
7263 }
7264 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7265
7266 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7267 rel = next_rel;
7268 else
7269 while (1)
7270 {
7271 if (!dec_dynrel_count (rel->r_info, sec, info,
7272 NULL, h, sym))
7273 goto error_ret;
7274
7275 if (++rel == next_rel)
7276 break;
7277
7278 r_symndx = ELF64_R_SYM (rel->r_info);
7279 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7280 r_symndx, ibfd))
7281 goto error_ret;
7282 }
7283 }
7284 else
7285 {
7286 /* We'll be keeping this opd entry. */
7287 long adjust;
7288
7289 if (fdh != NULL)
7290 {
7291 /* Redefine the function descriptor symbol to
7292 this location in the opd section. It is
7293 necessary to update the value here rather
7294 than using an array of adjustments as we do
7295 for local symbols, because various places
7296 in the generic ELF code use the value
7297 stored in u.def.value. */
7298 fdh->elf.root.u.def.value = wptr - new_contents;
7299 fdh->adjust_done = 1;
7300 }
7301
7302 /* Local syms are a bit tricky. We could
7303 tweak them as they can be cached, but
7304 we'd need to look through the local syms
7305 for the function descriptor sym which we
7306 don't have at the moment. So keep an
7307 array of adjustments. */
7308 adjust = (wptr - new_contents) - (rptr - sec->contents);
7309 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7310
7311 if (wptr != rptr)
7312 memcpy (wptr, rptr, opd_ent_size);
7313 wptr += opd_ent_size;
7314 if (add_aux_fields && opd_ent_size == 16)
7315 {
7316 memset (wptr, '\0', 8);
7317 wptr += 8;
7318 }
7319
7320 /* We need to adjust any reloc offsets to point to the
7321 new opd entries. */
7322 for ( ; rel != next_rel; ++rel)
7323 {
7324 rel->r_offset += adjust;
7325 if (write_rel != rel)
7326 memcpy (write_rel, rel, sizeof (*rel));
7327 ++write_rel;
7328 }
7329 }
7330
7331 rptr += opd_ent_size;
7332 }
7333
7334 sec->size = wptr - new_contents;
7335 sec->reloc_count = write_rel - relstart;
7336 if (add_aux_fields)
7337 {
7338 free (sec->contents);
7339 sec->contents = new_contents;
7340 }
7341
7342 /* Fudge the header size too, as this is used later in
7343 elf_bfd_final_link if we are emitting relocs. */
7344 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7345 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7346 some_edited = TRUE;
7347 }
7348 else if (elf_section_data (sec)->relocs != relstart)
7349 free (relstart);
7350
7351 if (local_syms != NULL
7352 && symtab_hdr->contents != (unsigned char *) local_syms)
7353 {
7354 if (!info->keep_memory)
7355 free (local_syms);
7356 else
7357 symtab_hdr->contents = (unsigned char *) local_syms;
7358 }
7359 }
7360
7361 if (some_edited)
7362 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7363
7364 /* If we are doing a final link and the last .opd entry is just 16 byte
7365 long, add a 8 byte padding after it. */
7366 if (need_pad != NULL && !bfd_link_relocatable (info))
7367 {
7368 bfd_byte *p;
7369
7370 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7371 {
7372 BFD_ASSERT (need_pad->size > 0);
7373
7374 p = bfd_malloc (need_pad->size + 8);
7375 if (p == NULL)
7376 return FALSE;
7377
7378 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7379 p, 0, need_pad->size))
7380 return FALSE;
7381
7382 need_pad->contents = p;
7383 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7384 }
7385 else
7386 {
7387 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7388 if (p == NULL)
7389 return FALSE;
7390
7391 need_pad->contents = p;
7392 }
7393
7394 memset (need_pad->contents + need_pad->size, 0, 8);
7395 need_pad->size += 8;
7396 }
7397
7398 return TRUE;
7399 }
7400
7401 /* Analyze inline PLT call relocations to see whether calls to locally
7402 defined functions can be converted to direct calls. */
7403
7404 bfd_boolean
7405 ppc64_elf_inline_plt (struct bfd_link_info *info)
7406 {
7407 struct ppc_link_hash_table *htab;
7408 bfd *ibfd;
7409 asection *sec;
7410 bfd_vma low_vma, high_vma, limit;
7411
7412 htab = ppc_hash_table (info);
7413 if (htab == NULL)
7414 return FALSE;
7415
7416 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7417 reduced somewhat to cater for possible stubs that might be added
7418 between the call and its destination. */
7419 if (htab->params->group_size < 0)
7420 {
7421 limit = -htab->params->group_size;
7422 if (limit == 1)
7423 limit = 0x1e00000;
7424 }
7425 else
7426 {
7427 limit = htab->params->group_size;
7428 if (limit == 1)
7429 limit = 0x1c00000;
7430 }
7431
7432 low_vma = -1;
7433 high_vma = 0;
7434 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7435 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7436 {
7437 if (low_vma > sec->vma)
7438 low_vma = sec->vma;
7439 if (high_vma < sec->vma + sec->size)
7440 high_vma = sec->vma + sec->size;
7441 }
7442
7443 /* If a "bl" can reach anywhere in local code sections, then we can
7444 convert all inline PLT sequences to direct calls when the symbol
7445 is local. */
7446 if (high_vma - low_vma < limit)
7447 {
7448 htab->can_convert_all_inline_plt = 1;
7449 return TRUE;
7450 }
7451
7452 /* Otherwise, go looking through relocs for cases where a direct
7453 call won't reach. Mark the symbol on any such reloc to disable
7454 the optimization and keep the PLT entry as it seems likely that
7455 this will be better than creating trampolines. Note that this
7456 will disable the optimization for all inline PLT calls to a
7457 particular symbol, not just those that won't reach. The
7458 difficulty in doing a more precise optimization is that the
7459 linker needs to make a decision depending on whether a
7460 particular R_PPC64_PLTCALL insn can be turned into a direct
7461 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7462 the sequence, and there is nothing that ties those relocs
7463 together except their symbol. */
7464
7465 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7466 {
7467 Elf_Internal_Shdr *symtab_hdr;
7468 Elf_Internal_Sym *local_syms;
7469
7470 if (!is_ppc64_elf (ibfd))
7471 continue;
7472
7473 local_syms = NULL;
7474 symtab_hdr = &elf_symtab_hdr (ibfd);
7475
7476 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7477 if (ppc64_elf_section_data (sec)->has_pltcall
7478 && !bfd_is_abs_section (sec->output_section))
7479 {
7480 Elf_Internal_Rela *relstart, *rel, *relend;
7481
7482 /* Read the relocations. */
7483 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7484 info->keep_memory);
7485 if (relstart == NULL)
7486 return FALSE;
7487
7488 relend = relstart + sec->reloc_count;
7489 for (rel = relstart; rel < relend; )
7490 {
7491 enum elf_ppc64_reloc_type r_type;
7492 unsigned long r_symndx;
7493 asection *sym_sec;
7494 struct elf_link_hash_entry *h;
7495 Elf_Internal_Sym *sym;
7496 unsigned char *tls_maskp;
7497
7498 r_type = ELF64_R_TYPE (rel->r_info);
7499 if (r_type != R_PPC64_PLTCALL
7500 && r_type != R_PPC64_PLTCALL_NOTOC)
7501 continue;
7502
7503 r_symndx = ELF64_R_SYM (rel->r_info);
7504 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7505 r_symndx, ibfd))
7506 {
7507 if (elf_section_data (sec)->relocs != relstart)
7508 free (relstart);
7509 if (local_syms != NULL
7510 && symtab_hdr->contents != (bfd_byte *) local_syms)
7511 free (local_syms);
7512 return FALSE;
7513 }
7514
7515 if (sym_sec != NULL && sym_sec->output_section != NULL)
7516 {
7517 bfd_vma from, to;
7518 if (h != NULL)
7519 to = h->root.u.def.value;
7520 else
7521 to = sym->st_value;
7522 to += (rel->r_addend
7523 + sym_sec->output_offset
7524 + sym_sec->output_section->vma);
7525 from = (rel->r_offset
7526 + sec->output_offset
7527 + sec->output_section->vma);
7528 if (to - from + limit < 2 * limit
7529 && !(r_type == R_PPC64_PLTCALL_NOTOC
7530 && (((h ? h->other : sym->st_other)
7531 & STO_PPC64_LOCAL_MASK)
7532 > 1 << STO_PPC64_LOCAL_BIT)))
7533 *tls_maskp &= ~PLT_KEEP;
7534 }
7535 }
7536 if (elf_section_data (sec)->relocs != relstart)
7537 free (relstart);
7538 }
7539
7540 if (local_syms != NULL
7541 && symtab_hdr->contents != (unsigned char *) local_syms)
7542 {
7543 if (!info->keep_memory)
7544 free (local_syms);
7545 else
7546 symtab_hdr->contents = (unsigned char *) local_syms;
7547 }
7548 }
7549
7550 return TRUE;
7551 }
7552
7553 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7554
7555 asection *
7556 ppc64_elf_tls_setup (struct bfd_link_info *info)
7557 {
7558 struct ppc_link_hash_table *htab;
7559
7560 htab = ppc_hash_table (info);
7561 if (htab == NULL)
7562 return NULL;
7563
7564 if (abiversion (info->output_bfd) == 1)
7565 htab->opd_abi = 1;
7566
7567 if (htab->params->no_multi_toc)
7568 htab->do_multi_toc = 0;
7569 else if (!htab->do_multi_toc)
7570 htab->params->no_multi_toc = 1;
7571
7572 /* Default to --no-plt-localentry, as this option can cause problems
7573 with symbol interposition. For example, glibc libpthread.so and
7574 libc.so duplicate many pthread symbols, with a fallback
7575 implementation in libc.so. In some cases the fallback does more
7576 work than the pthread implementation. __pthread_condattr_destroy
7577 is one such symbol: the libpthread.so implementation is
7578 localentry:0 while the libc.so implementation is localentry:8.
7579 An app that "cleverly" uses dlopen to only load necessary
7580 libraries at runtime may omit loading libpthread.so when not
7581 running multi-threaded, which then results in the libc.so
7582 fallback symbols being used and ld.so complaining. Now there
7583 are workarounds in ld (see non_zero_localentry) to detect the
7584 pthread situation, but that may not be the only case where
7585 --plt-localentry can cause trouble. */
7586 if (htab->params->plt_localentry0 < 0)
7587 htab->params->plt_localentry0 = 0;
7588 if (htab->params->plt_localentry0
7589 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7590 FALSE, FALSE, FALSE) == NULL)
7591 _bfd_error_handler
7592 (_("warning: --plt-localentry is especially dangerous without "
7593 "ld.so support to detect ABI violations"));
7594
7595 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7596 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7597 FALSE, FALSE, TRUE));
7598 /* Move dynamic linking info to the function descriptor sym. */
7599 if (htab->tls_get_addr != NULL)
7600 func_desc_adjust (&htab->tls_get_addr->elf, info);
7601 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7602 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7603 FALSE, FALSE, TRUE));
7604 if (htab->params->tls_get_addr_opt)
7605 {
7606 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7607
7608 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7609 FALSE, FALSE, TRUE);
7610 if (opt != NULL)
7611 func_desc_adjust (opt, info);
7612 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7613 FALSE, FALSE, TRUE);
7614 if (opt_fd != NULL
7615 && (opt_fd->root.type == bfd_link_hash_defined
7616 || opt_fd->root.type == bfd_link_hash_defweak))
7617 {
7618 /* If glibc supports an optimized __tls_get_addr call stub,
7619 signalled by the presence of __tls_get_addr_opt, and we'll
7620 be calling __tls_get_addr via a plt call stub, then
7621 make __tls_get_addr point to __tls_get_addr_opt. */
7622 tga_fd = &htab->tls_get_addr_fd->elf;
7623 if (htab->elf.dynamic_sections_created
7624 && tga_fd != NULL
7625 && (tga_fd->type == STT_FUNC
7626 || tga_fd->needs_plt)
7627 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7628 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
7629 {
7630 struct plt_entry *ent;
7631
7632 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7633 if (ent->plt.refcount > 0)
7634 break;
7635 if (ent != NULL)
7636 {
7637 tga_fd->root.type = bfd_link_hash_indirect;
7638 tga_fd->root.u.i.link = &opt_fd->root;
7639 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7640 opt_fd->mark = 1;
7641 if (opt_fd->dynindx != -1)
7642 {
7643 /* Use __tls_get_addr_opt in dynamic relocations. */
7644 opt_fd->dynindx = -1;
7645 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7646 opt_fd->dynstr_index);
7647 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7648 return NULL;
7649 }
7650 htab->tls_get_addr_fd
7651 = (struct ppc_link_hash_entry *) opt_fd;
7652 tga = &htab->tls_get_addr->elf;
7653 if (opt != NULL && tga != NULL)
7654 {
7655 tga->root.type = bfd_link_hash_indirect;
7656 tga->root.u.i.link = &opt->root;
7657 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7658 opt->mark = 1;
7659 _bfd_elf_link_hash_hide_symbol (info, opt,
7660 tga->forced_local);
7661 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7662 }
7663 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7664 htab->tls_get_addr_fd->is_func_descriptor = 1;
7665 if (htab->tls_get_addr != NULL)
7666 {
7667 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7668 htab->tls_get_addr->is_func = 1;
7669 }
7670 }
7671 }
7672 }
7673 else if (htab->params->tls_get_addr_opt < 0)
7674 htab->params->tls_get_addr_opt = 0;
7675 }
7676 return _bfd_elf_tls_setup (info->output_bfd, info);
7677 }
7678
7679 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7680 HASH1 or HASH2. */
7681
7682 static bfd_boolean
7683 branch_reloc_hash_match (const bfd *ibfd,
7684 const Elf_Internal_Rela *rel,
7685 const struct ppc_link_hash_entry *hash1,
7686 const struct ppc_link_hash_entry *hash2)
7687 {
7688 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7689 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7690 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7691
7692 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7693 {
7694 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7695 struct elf_link_hash_entry *h;
7696
7697 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7698 h = elf_follow_link (h);
7699 if (h == &hash1->elf || h == &hash2->elf)
7700 return TRUE;
7701 }
7702 return FALSE;
7703 }
7704
7705 /* Run through all the TLS relocs looking for optimization
7706 opportunities. The linker has been hacked (see ppc64elf.em) to do
7707 a preliminary section layout so that we know the TLS segment
7708 offsets. We can't optimize earlier because some optimizations need
7709 to know the tp offset, and we need to optimize before allocating
7710 dynamic relocations. */
7711
7712 bfd_boolean
7713 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7714 {
7715 bfd *ibfd;
7716 asection *sec;
7717 struct ppc_link_hash_table *htab;
7718 unsigned char *toc_ref;
7719 int pass;
7720
7721 if (!bfd_link_executable (info))
7722 return TRUE;
7723
7724 htab = ppc_hash_table (info);
7725 if (htab == NULL)
7726 return FALSE;
7727
7728 /* Make two passes over the relocs. On the first pass, mark toc
7729 entries involved with tls relocs, and check that tls relocs
7730 involved in setting up a tls_get_addr call are indeed followed by
7731 such a call. If they are not, we can't do any tls optimization.
7732 On the second pass twiddle tls_mask flags to notify
7733 relocate_section that optimization can be done, and adjust got
7734 and plt refcounts. */
7735 toc_ref = NULL;
7736 for (pass = 0; pass < 2; ++pass)
7737 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7738 {
7739 Elf_Internal_Sym *locsyms = NULL;
7740 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7741
7742 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7743 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7744 {
7745 Elf_Internal_Rela *relstart, *rel, *relend;
7746 bfd_boolean found_tls_get_addr_arg = 0;
7747
7748 /* Read the relocations. */
7749 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7750 info->keep_memory);
7751 if (relstart == NULL)
7752 {
7753 free (toc_ref);
7754 return FALSE;
7755 }
7756
7757 relend = relstart + sec->reloc_count;
7758 for (rel = relstart; rel < relend; rel++)
7759 {
7760 enum elf_ppc64_reloc_type r_type;
7761 unsigned long r_symndx;
7762 struct elf_link_hash_entry *h;
7763 Elf_Internal_Sym *sym;
7764 asection *sym_sec;
7765 unsigned char *tls_mask;
7766 unsigned int tls_set, tls_clear, tls_type = 0;
7767 bfd_vma value;
7768 bfd_boolean ok_tprel, is_local;
7769 long toc_ref_index = 0;
7770 int expecting_tls_get_addr = 0;
7771 bfd_boolean ret = FALSE;
7772
7773 r_symndx = ELF64_R_SYM (rel->r_info);
7774 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7775 r_symndx, ibfd))
7776 {
7777 err_free_rel:
7778 if (elf_section_data (sec)->relocs != relstart)
7779 free (relstart);
7780 if (toc_ref != NULL)
7781 free (toc_ref);
7782 if (locsyms != NULL
7783 && (elf_symtab_hdr (ibfd).contents
7784 != (unsigned char *) locsyms))
7785 free (locsyms);
7786 return ret;
7787 }
7788
7789 if (h != NULL)
7790 {
7791 if (h->root.type == bfd_link_hash_defined
7792 || h->root.type == bfd_link_hash_defweak)
7793 value = h->root.u.def.value;
7794 else if (h->root.type == bfd_link_hash_undefweak)
7795 value = 0;
7796 else
7797 {
7798 found_tls_get_addr_arg = 0;
7799 continue;
7800 }
7801 }
7802 else
7803 /* Symbols referenced by TLS relocs must be of type
7804 STT_TLS. So no need for .opd local sym adjust. */
7805 value = sym->st_value;
7806
7807 ok_tprel = FALSE;
7808 is_local = FALSE;
7809 if (h == NULL
7810 || !h->def_dynamic)
7811 {
7812 is_local = TRUE;
7813 if (h != NULL
7814 && h->root.type == bfd_link_hash_undefweak)
7815 ok_tprel = TRUE;
7816 else if (sym_sec != NULL
7817 && sym_sec->output_section != NULL)
7818 {
7819 value += sym_sec->output_offset;
7820 value += sym_sec->output_section->vma;
7821 value -= htab->elf.tls_sec->vma + TP_OFFSET;
7822 /* Note that even though the prefix insns
7823 allow a 1<<33 offset we use the same test
7824 as for addis;addi. There may be a mix of
7825 pcrel and non-pcrel code and the decision
7826 to optimise is per symbol, not per TLS
7827 sequence. */
7828 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
7829 }
7830 }
7831
7832 r_type = ELF64_R_TYPE (rel->r_info);
7833 /* If this section has old-style __tls_get_addr calls
7834 without marker relocs, then check that each
7835 __tls_get_addr call reloc is preceded by a reloc
7836 that conceivably belongs to the __tls_get_addr arg
7837 setup insn. If we don't find matching arg setup
7838 relocs, don't do any tls optimization. */
7839 if (pass == 0
7840 && sec->has_tls_get_addr_call
7841 && h != NULL
7842 && (h == &htab->tls_get_addr->elf
7843 || h == &htab->tls_get_addr_fd->elf)
7844 && !found_tls_get_addr_arg
7845 && is_branch_reloc (r_type))
7846 {
7847 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7848 "TLS optimization disabled\n"),
7849 ibfd, sec, rel->r_offset);
7850 ret = TRUE;
7851 goto err_free_rel;
7852 }
7853
7854 found_tls_get_addr_arg = 0;
7855 switch (r_type)
7856 {
7857 case R_PPC64_GOT_TLSLD16:
7858 case R_PPC64_GOT_TLSLD16_LO:
7859 case R_PPC64_GOT_TLSLD34:
7860 expecting_tls_get_addr = 1;
7861 found_tls_get_addr_arg = 1;
7862 /* Fall through. */
7863
7864 case R_PPC64_GOT_TLSLD16_HI:
7865 case R_PPC64_GOT_TLSLD16_HA:
7866 /* These relocs should never be against a symbol
7867 defined in a shared lib. Leave them alone if
7868 that turns out to be the case. */
7869 if (!is_local)
7870 continue;
7871
7872 /* LD -> LE */
7873 tls_set = 0;
7874 tls_clear = TLS_LD;
7875 tls_type = TLS_TLS | TLS_LD;
7876 break;
7877
7878 case R_PPC64_GOT_TLSGD16:
7879 case R_PPC64_GOT_TLSGD16_LO:
7880 case R_PPC64_GOT_TLSGD34:
7881 expecting_tls_get_addr = 1;
7882 found_tls_get_addr_arg = 1;
7883 /* Fall through. */
7884
7885 case R_PPC64_GOT_TLSGD16_HI:
7886 case R_PPC64_GOT_TLSGD16_HA:
7887 if (ok_tprel)
7888 /* GD -> LE */
7889 tls_set = 0;
7890 else
7891 /* GD -> IE */
7892 tls_set = TLS_TLS | TLS_GDIE;
7893 tls_clear = TLS_GD;
7894 tls_type = TLS_TLS | TLS_GD;
7895 break;
7896
7897 case R_PPC64_GOT_TPREL34:
7898 case R_PPC64_GOT_TPREL16_DS:
7899 case R_PPC64_GOT_TPREL16_LO_DS:
7900 case R_PPC64_GOT_TPREL16_HI:
7901 case R_PPC64_GOT_TPREL16_HA:
7902 if (ok_tprel)
7903 {
7904 /* IE -> LE */
7905 tls_set = 0;
7906 tls_clear = TLS_TPREL;
7907 tls_type = TLS_TLS | TLS_TPREL;
7908 break;
7909 }
7910 continue;
7911
7912 case R_PPC64_TLSGD:
7913 case R_PPC64_TLSLD:
7914 if (rel + 1 < relend
7915 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7916 {
7917 if (pass != 0
7918 && (ELF64_R_TYPE (rel[1].r_info)
7919 != R_PPC64_PLTSEQ)
7920 && (ELF64_R_TYPE (rel[1].r_info)
7921 != R_PPC64_PLTSEQ_NOTOC))
7922 {
7923 r_symndx = ELF64_R_SYM (rel[1].r_info);
7924 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
7925 r_symndx, ibfd))
7926 goto err_free_rel;
7927 if (h != NULL)
7928 {
7929 struct plt_entry *ent = NULL;
7930
7931 for (ent = h->plt.plist;
7932 ent != NULL;
7933 ent = ent->next)
7934 if (ent->addend == rel[1].r_addend)
7935 break;
7936
7937 if (ent != NULL
7938 && ent->plt.refcount > 0)
7939 ent->plt.refcount -= 1;
7940 }
7941 }
7942 continue;
7943 }
7944 found_tls_get_addr_arg = 1;
7945 /* Fall through. */
7946
7947 case R_PPC64_TLS:
7948 case R_PPC64_TOC16:
7949 case R_PPC64_TOC16_LO:
7950 if (sym_sec == NULL || sym_sec != toc)
7951 continue;
7952
7953 /* Mark this toc entry as referenced by a TLS
7954 code sequence. We can do that now in the
7955 case of R_PPC64_TLS, and after checking for
7956 tls_get_addr for the TOC16 relocs. */
7957 if (toc_ref == NULL)
7958 toc_ref
7959 = bfd_zmalloc (toc->output_section->rawsize / 8);
7960 if (toc_ref == NULL)
7961 goto err_free_rel;
7962
7963 if (h != NULL)
7964 value = h->root.u.def.value;
7965 else
7966 value = sym->st_value;
7967 value += rel->r_addend;
7968 if (value % 8 != 0)
7969 continue;
7970 BFD_ASSERT (value < toc->size
7971 && toc->output_offset % 8 == 0);
7972 toc_ref_index = (value + toc->output_offset) / 8;
7973 if (r_type == R_PPC64_TLS
7974 || r_type == R_PPC64_TLSGD
7975 || r_type == R_PPC64_TLSLD)
7976 {
7977 toc_ref[toc_ref_index] = 1;
7978 continue;
7979 }
7980
7981 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7982 continue;
7983
7984 tls_set = 0;
7985 tls_clear = 0;
7986 expecting_tls_get_addr = 2;
7987 break;
7988
7989 case R_PPC64_TPREL64:
7990 if (pass == 0
7991 || sec != toc
7992 || toc_ref == NULL
7993 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7994 continue;
7995 if (ok_tprel)
7996 {
7997 /* IE -> LE */
7998 tls_set = TLS_EXPLICIT;
7999 tls_clear = TLS_TPREL;
8000 break;
8001 }
8002 continue;
8003
8004 case R_PPC64_DTPMOD64:
8005 if (pass == 0
8006 || sec != toc
8007 || toc_ref == NULL
8008 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8009 continue;
8010 if (rel + 1 < relend
8011 && (rel[1].r_info
8012 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8013 && rel[1].r_offset == rel->r_offset + 8)
8014 {
8015 if (ok_tprel)
8016 /* GD -> LE */
8017 tls_set = TLS_EXPLICIT | TLS_GD;
8018 else
8019 /* GD -> IE */
8020 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8021 tls_clear = TLS_GD;
8022 }
8023 else
8024 {
8025 if (!is_local)
8026 continue;
8027
8028 /* LD -> LE */
8029 tls_set = TLS_EXPLICIT;
8030 tls_clear = TLS_LD;
8031 }
8032 break;
8033
8034 default:
8035 continue;
8036 }
8037
8038 if (pass == 0)
8039 {
8040 if (!expecting_tls_get_addr
8041 || !sec->has_tls_get_addr_call)
8042 continue;
8043
8044 if (rel + 1 < relend
8045 && branch_reloc_hash_match (ibfd, rel + 1,
8046 htab->tls_get_addr,
8047 htab->tls_get_addr_fd))
8048 {
8049 if (expecting_tls_get_addr == 2)
8050 {
8051 /* Check for toc tls entries. */
8052 unsigned char *toc_tls;
8053 int retval;
8054
8055 retval = get_tls_mask (&toc_tls, NULL, NULL,
8056 &locsyms,
8057 rel, ibfd);
8058 if (retval == 0)
8059 goto err_free_rel;
8060 if (toc_tls != NULL)
8061 {
8062 if ((*toc_tls & TLS_TLS) != 0
8063 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8064 found_tls_get_addr_arg = 1;
8065 if (retval > 1)
8066 toc_ref[toc_ref_index] = 1;
8067 }
8068 }
8069 continue;
8070 }
8071
8072 /* Uh oh, we didn't find the expected call. We
8073 could just mark this symbol to exclude it
8074 from tls optimization but it's safer to skip
8075 the entire optimization. */
8076 /* xgettext:c-format */
8077 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8078 "TLS optimization disabled\n"),
8079 ibfd, sec, rel->r_offset);
8080 ret = TRUE;
8081 goto err_free_rel;
8082 }
8083
8084 /* If we don't have old-style __tls_get_addr calls
8085 without TLSGD/TLSLD marker relocs, and we haven't
8086 found a new-style __tls_get_addr call with a
8087 marker for this symbol, then we either have a
8088 broken object file or an -mlongcall style
8089 indirect call to __tls_get_addr without a marker.
8090 Disable optimization in this case. */
8091 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8092 && (tls_set & TLS_EXPLICIT) == 0
8093 && !sec->has_tls_get_addr_call
8094 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8095 != (TLS_TLS | TLS_MARK)))
8096 continue;
8097
8098 if (expecting_tls_get_addr)
8099 {
8100 struct plt_entry *ent = NULL;
8101
8102 if (htab->tls_get_addr != NULL)
8103 for (ent = htab->tls_get_addr->elf.plt.plist;
8104 ent != NULL;
8105 ent = ent->next)
8106 if (ent->addend == 0)
8107 break;
8108
8109 if (ent == NULL && htab->tls_get_addr_fd != NULL)
8110 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8111 ent != NULL;
8112 ent = ent->next)
8113 if (ent->addend == 0)
8114 break;
8115
8116 if (ent != NULL
8117 && ent->plt.refcount > 0)
8118 ent->plt.refcount -= 1;
8119 }
8120
8121 if (tls_clear == 0)
8122 continue;
8123
8124 if ((tls_set & TLS_EXPLICIT) == 0)
8125 {
8126 struct got_entry *ent;
8127
8128 /* Adjust got entry for this reloc. */
8129 if (h != NULL)
8130 ent = h->got.glist;
8131 else
8132 ent = elf_local_got_ents (ibfd)[r_symndx];
8133
8134 for (; ent != NULL; ent = ent->next)
8135 if (ent->addend == rel->r_addend
8136 && ent->owner == ibfd
8137 && ent->tls_type == tls_type)
8138 break;
8139 if (ent == NULL)
8140 abort ();
8141
8142 if (tls_set == 0)
8143 {
8144 /* We managed to get rid of a got entry. */
8145 if (ent->got.refcount > 0)
8146 ent->got.refcount -= 1;
8147 }
8148 }
8149 else
8150 {
8151 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8152 we'll lose one or two dyn relocs. */
8153 if (!dec_dynrel_count (rel->r_info, sec, info,
8154 NULL, h, sym))
8155 return FALSE;
8156
8157 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8158 {
8159 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8160 NULL, h, sym))
8161 return FALSE;
8162 }
8163 }
8164
8165 *tls_mask |= tls_set & 0xff;
8166 *tls_mask &= ~tls_clear;
8167 }
8168
8169 if (elf_section_data (sec)->relocs != relstart)
8170 free (relstart);
8171 }
8172
8173 if (locsyms != NULL
8174 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8175 {
8176 if (!info->keep_memory)
8177 free (locsyms);
8178 else
8179 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8180 }
8181 }
8182
8183 if (toc_ref != NULL)
8184 free (toc_ref);
8185 htab->do_tls_opt = 1;
8186 return TRUE;
8187 }
8188
8189 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8190 the values of any global symbols in a toc section that has been
8191 edited. Globals in toc sections should be a rarity, so this function
8192 sets a flag if any are found in toc sections other than the one just
8193 edited, so that further hash table traversals can be avoided. */
8194
8195 struct adjust_toc_info
8196 {
8197 asection *toc;
8198 unsigned long *skip;
8199 bfd_boolean global_toc_syms;
8200 };
8201
8202 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8203
8204 static bfd_boolean
8205 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8206 {
8207 struct ppc_link_hash_entry *eh;
8208 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8209 unsigned long i;
8210
8211 if (h->root.type != bfd_link_hash_defined
8212 && h->root.type != bfd_link_hash_defweak)
8213 return TRUE;
8214
8215 eh = (struct ppc_link_hash_entry *) h;
8216 if (eh->adjust_done)
8217 return TRUE;
8218
8219 if (eh->elf.root.u.def.section == toc_inf->toc)
8220 {
8221 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8222 i = toc_inf->toc->rawsize >> 3;
8223 else
8224 i = eh->elf.root.u.def.value >> 3;
8225
8226 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8227 {
8228 _bfd_error_handler
8229 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8230 do
8231 ++i;
8232 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8233 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8234 }
8235
8236 eh->elf.root.u.def.value -= toc_inf->skip[i];
8237 eh->adjust_done = 1;
8238 }
8239 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8240 toc_inf->global_toc_syms = TRUE;
8241
8242 return TRUE;
8243 }
8244
8245 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8246 on a _LO variety toc/got reloc. */
8247
8248 static bfd_boolean
8249 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8250 {
8251 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8252 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8253 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8254 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8255 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8256 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8257 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8258 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8259 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8260 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8261 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8262 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8263 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8264 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8265 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8266 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8267 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8268 /* Exclude lfqu by testing reloc. If relocs are ever
8269 defined for the reduced D field in psq_lu then those
8270 will need testing too. */
8271 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8272 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8273 && (insn & 1) == 0)
8274 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8275 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8276 /* Exclude stfqu. psq_stu as above for psq_lu. */
8277 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8278 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8279 && (insn & 1) == 0));
8280 }
8281
8282 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8283 pld ra,symbol@got@pcrel
8284 load/store rt,off(ra)
8285 or
8286 pla ra,symbol@pcrel
8287 load/store rt,off(ra)
8288 may be translated to
8289 pload/pstore rt,symbol+off@pcrel
8290 nop.
8291 This function returns true if the optimization is possible, placing
8292 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8293
8294 On entry to this function, the linker has already determined that
8295 the pld can be replaced with pla: *PINSN1 is that pla insn,
8296 while *PINSN2 is the second instruction. */
8297
8298 static bfd_boolean
8299 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8300 {
8301 uint64_t insn1 = *pinsn1;
8302 uint64_t insn2 = *pinsn2;
8303 bfd_signed_vma off;
8304
8305 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8306 {
8307 /* Check that regs match. */
8308 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8309 return FALSE;
8310
8311 /* P8LS or PMLS form, non-pcrel. */
8312 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8313 return FALSE;
8314
8315 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8316 *pinsn2 = PNOP;
8317 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8318 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8319 return TRUE;
8320 }
8321
8322 insn2 >>= 32;
8323
8324 /* Check that regs match. */
8325 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8326 return FALSE;
8327
8328 switch ((insn2 >> 26) & 63)
8329 {
8330 default:
8331 return FALSE;
8332
8333 case 32: /* lwz */
8334 case 34: /* lbz */
8335 case 36: /* stw */
8336 case 38: /* stb */
8337 case 40: /* lhz */
8338 case 42: /* lha */
8339 case 44: /* sth */
8340 case 48: /* lfs */
8341 case 50: /* lfd */
8342 case 52: /* stfs */
8343 case 54: /* stfd */
8344 /* These are the PMLS cases, where we just need to tack a prefix
8345 on the insn. */
8346 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8347 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8348 off = insn2 & 0xffff;
8349 break;
8350
8351 case 58: /* lwa, ld */
8352 if ((insn2 & 1) != 0)
8353 return FALSE;
8354 insn1 = ((1ULL << 58) | (1ULL << 52)
8355 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8356 | (insn2 & (31ULL << 21)));
8357 off = insn2 & 0xfffc;
8358 break;
8359
8360 case 57: /* lxsd, lxssp */
8361 if ((insn2 & 3) < 2)
8362 return FALSE;
8363 insn1 = ((1ULL << 58) | (1ULL << 52)
8364 | ((40ULL | (insn2 & 3)) << 26)
8365 | (insn2 & (31ULL << 21)));
8366 off = insn2 & 0xfffc;
8367 break;
8368
8369 case 61: /* stxsd, stxssp, lxv, stxv */
8370 if ((insn2 & 3) == 0)
8371 return FALSE;
8372 else if ((insn2 & 3) >= 2)
8373 {
8374 insn1 = ((1ULL << 58) | (1ULL << 52)
8375 | ((44ULL | (insn2 & 3)) << 26)
8376 | (insn2 & (31ULL << 21)));
8377 off = insn2 & 0xfffc;
8378 }
8379 else
8380 {
8381 insn1 = ((1ULL << 58) | (1ULL << 52)
8382 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8383 | (insn2 & (31ULL << 21)));
8384 off = insn2 & 0xfff0;
8385 }
8386 break;
8387
8388 case 56: /* lq */
8389 insn1 = ((1ULL << 58) | (1ULL << 52)
8390 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8391 off = insn2 & 0xffff;
8392 break;
8393
8394 case 62: /* std, stq */
8395 if ((insn2 & 1) != 0)
8396 return FALSE;
8397 insn1 = ((1ULL << 58) | (1ULL << 52)
8398 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8399 | (insn2 & (31ULL << 21)));
8400 off = insn2 & 0xfffc;
8401 break;
8402 }
8403
8404 *pinsn1 = insn1;
8405 *pinsn2 = (uint64_t) NOP << 32;
8406 *poff = (off ^ 0x8000) - 0x8000;
8407 return TRUE;
8408 }
8409
8410 /* Examine all relocs referencing .toc sections in order to remove
8411 unused .toc entries. */
8412
8413 bfd_boolean
8414 ppc64_elf_edit_toc (struct bfd_link_info *info)
8415 {
8416 bfd *ibfd;
8417 struct adjust_toc_info toc_inf;
8418 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8419
8420 htab->do_toc_opt = 1;
8421 toc_inf.global_toc_syms = TRUE;
8422 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8423 {
8424 asection *toc, *sec;
8425 Elf_Internal_Shdr *symtab_hdr;
8426 Elf_Internal_Sym *local_syms;
8427 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8428 unsigned long *skip, *drop;
8429 unsigned char *used;
8430 unsigned char *keep, last, some_unused;
8431
8432 if (!is_ppc64_elf (ibfd))
8433 continue;
8434
8435 toc = bfd_get_section_by_name (ibfd, ".toc");
8436 if (toc == NULL
8437 || toc->size == 0
8438 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8439 || discarded_section (toc))
8440 continue;
8441
8442 toc_relocs = NULL;
8443 local_syms = NULL;
8444 symtab_hdr = &elf_symtab_hdr (ibfd);
8445
8446 /* Look at sections dropped from the final link. */
8447 skip = NULL;
8448 relstart = NULL;
8449 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8450 {
8451 if (sec->reloc_count == 0
8452 || !discarded_section (sec)
8453 || get_opd_info (sec)
8454 || (sec->flags & SEC_ALLOC) == 0
8455 || (sec->flags & SEC_DEBUGGING) != 0)
8456 continue;
8457
8458 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8459 if (relstart == NULL)
8460 goto error_ret;
8461
8462 /* Run through the relocs to see which toc entries might be
8463 unused. */
8464 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8465 {
8466 enum elf_ppc64_reloc_type r_type;
8467 unsigned long r_symndx;
8468 asection *sym_sec;
8469 struct elf_link_hash_entry *h;
8470 Elf_Internal_Sym *sym;
8471 bfd_vma val;
8472
8473 r_type = ELF64_R_TYPE (rel->r_info);
8474 switch (r_type)
8475 {
8476 default:
8477 continue;
8478
8479 case R_PPC64_TOC16:
8480 case R_PPC64_TOC16_LO:
8481 case R_PPC64_TOC16_HI:
8482 case R_PPC64_TOC16_HA:
8483 case R_PPC64_TOC16_DS:
8484 case R_PPC64_TOC16_LO_DS:
8485 break;
8486 }
8487
8488 r_symndx = ELF64_R_SYM (rel->r_info);
8489 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8490 r_symndx, ibfd))
8491 goto error_ret;
8492
8493 if (sym_sec != toc)
8494 continue;
8495
8496 if (h != NULL)
8497 val = h->root.u.def.value;
8498 else
8499 val = sym->st_value;
8500 val += rel->r_addend;
8501
8502 if (val >= toc->size)
8503 continue;
8504
8505 /* Anything in the toc ought to be aligned to 8 bytes.
8506 If not, don't mark as unused. */
8507 if (val & 7)
8508 continue;
8509
8510 if (skip == NULL)
8511 {
8512 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8513 if (skip == NULL)
8514 goto error_ret;
8515 }
8516
8517 skip[val >> 3] = ref_from_discarded;
8518 }
8519
8520 if (elf_section_data (sec)->relocs != relstart)
8521 free (relstart);
8522 }
8523
8524 /* For largetoc loads of address constants, we can convert
8525 . addis rx,2,addr@got@ha
8526 . ld ry,addr@got@l(rx)
8527 to
8528 . addis rx,2,addr@toc@ha
8529 . addi ry,rx,addr@toc@l
8530 when addr is within 2G of the toc pointer. This then means
8531 that the word storing "addr" in the toc is no longer needed. */
8532
8533 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8534 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8535 && toc->reloc_count != 0)
8536 {
8537 /* Read toc relocs. */
8538 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8539 info->keep_memory);
8540 if (toc_relocs == NULL)
8541 goto error_ret;
8542
8543 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8544 {
8545 enum elf_ppc64_reloc_type r_type;
8546 unsigned long r_symndx;
8547 asection *sym_sec;
8548 struct elf_link_hash_entry *h;
8549 Elf_Internal_Sym *sym;
8550 bfd_vma val, addr;
8551
8552 r_type = ELF64_R_TYPE (rel->r_info);
8553 if (r_type != R_PPC64_ADDR64)
8554 continue;
8555
8556 r_symndx = ELF64_R_SYM (rel->r_info);
8557 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8558 r_symndx, ibfd))
8559 goto error_ret;
8560
8561 if (sym_sec == NULL
8562 || sym_sec->output_section == NULL
8563 || discarded_section (sym_sec))
8564 continue;
8565
8566 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8567 continue;
8568
8569 if (h != NULL)
8570 {
8571 if (h->type == STT_GNU_IFUNC)
8572 continue;
8573 val = h->root.u.def.value;
8574 }
8575 else
8576 {
8577 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8578 continue;
8579 val = sym->st_value;
8580 }
8581 val += rel->r_addend;
8582 val += sym_sec->output_section->vma + sym_sec->output_offset;
8583
8584 /* We don't yet know the exact toc pointer value, but we
8585 know it will be somewhere in the toc section. Don't
8586 optimize if the difference from any possible toc
8587 pointer is outside [ff..f80008000, 7fff7fff]. */
8588 addr = toc->output_section->vma + TOC_BASE_OFF;
8589 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8590 continue;
8591
8592 addr = toc->output_section->vma + toc->output_section->rawsize;
8593 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8594 continue;
8595
8596 if (skip == NULL)
8597 {
8598 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8599 if (skip == NULL)
8600 goto error_ret;
8601 }
8602
8603 skip[rel->r_offset >> 3]
8604 |= can_optimize | ((rel - toc_relocs) << 2);
8605 }
8606 }
8607
8608 if (skip == NULL)
8609 continue;
8610
8611 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8612 if (used == NULL)
8613 {
8614 error_ret:
8615 if (local_syms != NULL
8616 && symtab_hdr->contents != (unsigned char *) local_syms)
8617 free (local_syms);
8618 if (sec != NULL
8619 && relstart != NULL
8620 && elf_section_data (sec)->relocs != relstart)
8621 free (relstart);
8622 if (toc_relocs != NULL
8623 && elf_section_data (toc)->relocs != toc_relocs)
8624 free (toc_relocs);
8625 if (skip != NULL)
8626 free (skip);
8627 return FALSE;
8628 }
8629
8630 /* Now check all kept sections that might reference the toc.
8631 Check the toc itself last. */
8632 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8633 : ibfd->sections);
8634 sec != NULL;
8635 sec = (sec == toc ? NULL
8636 : sec->next == NULL ? toc
8637 : sec->next == toc && toc->next ? toc->next
8638 : sec->next))
8639 {
8640 int repeat;
8641
8642 if (sec->reloc_count == 0
8643 || discarded_section (sec)
8644 || get_opd_info (sec)
8645 || (sec->flags & SEC_ALLOC) == 0
8646 || (sec->flags & SEC_DEBUGGING) != 0)
8647 continue;
8648
8649 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8650 info->keep_memory);
8651 if (relstart == NULL)
8652 {
8653 free (used);
8654 goto error_ret;
8655 }
8656
8657 /* Mark toc entries referenced as used. */
8658 do
8659 {
8660 repeat = 0;
8661 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8662 {
8663 enum elf_ppc64_reloc_type r_type;
8664 unsigned long r_symndx;
8665 asection *sym_sec;
8666 struct elf_link_hash_entry *h;
8667 Elf_Internal_Sym *sym;
8668 bfd_vma val;
8669
8670 r_type = ELF64_R_TYPE (rel->r_info);
8671 switch (r_type)
8672 {
8673 case R_PPC64_TOC16:
8674 case R_PPC64_TOC16_LO:
8675 case R_PPC64_TOC16_HI:
8676 case R_PPC64_TOC16_HA:
8677 case R_PPC64_TOC16_DS:
8678 case R_PPC64_TOC16_LO_DS:
8679 /* In case we're taking addresses of toc entries. */
8680 case R_PPC64_ADDR64:
8681 break;
8682
8683 default:
8684 continue;
8685 }
8686
8687 r_symndx = ELF64_R_SYM (rel->r_info);
8688 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8689 r_symndx, ibfd))
8690 {
8691 free (used);
8692 goto error_ret;
8693 }
8694
8695 if (sym_sec != toc)
8696 continue;
8697
8698 if (h != NULL)
8699 val = h->root.u.def.value;
8700 else
8701 val = sym->st_value;
8702 val += rel->r_addend;
8703
8704 if (val >= toc->size)
8705 continue;
8706
8707 if ((skip[val >> 3] & can_optimize) != 0)
8708 {
8709 bfd_vma off;
8710 unsigned char opc;
8711
8712 switch (r_type)
8713 {
8714 case R_PPC64_TOC16_HA:
8715 break;
8716
8717 case R_PPC64_TOC16_LO_DS:
8718 off = rel->r_offset;
8719 off += (bfd_big_endian (ibfd) ? -2 : 3);
8720 if (!bfd_get_section_contents (ibfd, sec, &opc,
8721 off, 1))
8722 {
8723 free (used);
8724 goto error_ret;
8725 }
8726 if ((opc & (0x3f << 2)) == (58u << 2))
8727 break;
8728 /* Fall through. */
8729
8730 default:
8731 /* Wrong sort of reloc, or not a ld. We may
8732 as well clear ref_from_discarded too. */
8733 skip[val >> 3] = 0;
8734 }
8735 }
8736
8737 if (sec != toc)
8738 used[val >> 3] = 1;
8739 /* For the toc section, we only mark as used if this
8740 entry itself isn't unused. */
8741 else if ((used[rel->r_offset >> 3]
8742 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8743 && !used[val >> 3])
8744 {
8745 /* Do all the relocs again, to catch reference
8746 chains. */
8747 repeat = 1;
8748 used[val >> 3] = 1;
8749 }
8750 }
8751 }
8752 while (repeat);
8753
8754 if (elf_section_data (sec)->relocs != relstart)
8755 free (relstart);
8756 }
8757
8758 /* Merge the used and skip arrays. Assume that TOC
8759 doublewords not appearing as either used or unused belong
8760 to an entry more than one doubleword in size. */
8761 for (drop = skip, keep = used, last = 0, some_unused = 0;
8762 drop < skip + (toc->size + 7) / 8;
8763 ++drop, ++keep)
8764 {
8765 if (*keep)
8766 {
8767 *drop &= ~ref_from_discarded;
8768 if ((*drop & can_optimize) != 0)
8769 some_unused = 1;
8770 last = 0;
8771 }
8772 else if ((*drop & ref_from_discarded) != 0)
8773 {
8774 some_unused = 1;
8775 last = ref_from_discarded;
8776 }
8777 else
8778 *drop = last;
8779 }
8780
8781 free (used);
8782
8783 if (some_unused)
8784 {
8785 bfd_byte *contents, *src;
8786 unsigned long off;
8787 Elf_Internal_Sym *sym;
8788 bfd_boolean local_toc_syms = FALSE;
8789
8790 /* Shuffle the toc contents, and at the same time convert the
8791 skip array from booleans into offsets. */
8792 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8793 goto error_ret;
8794
8795 elf_section_data (toc)->this_hdr.contents = contents;
8796
8797 for (src = contents, off = 0, drop = skip;
8798 src < contents + toc->size;
8799 src += 8, ++drop)
8800 {
8801 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8802 off += 8;
8803 else if (off != 0)
8804 {
8805 *drop = off;
8806 memcpy (src - off, src, 8);
8807 }
8808 }
8809 *drop = off;
8810 toc->rawsize = toc->size;
8811 toc->size = src - contents - off;
8812
8813 /* Adjust addends for relocs against the toc section sym,
8814 and optimize any accesses we can. */
8815 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8816 {
8817 if (sec->reloc_count == 0
8818 || discarded_section (sec))
8819 continue;
8820
8821 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8822 info->keep_memory);
8823 if (relstart == NULL)
8824 goto error_ret;
8825
8826 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8827 {
8828 enum elf_ppc64_reloc_type r_type;
8829 unsigned long r_symndx;
8830 asection *sym_sec;
8831 struct elf_link_hash_entry *h;
8832 bfd_vma val;
8833
8834 r_type = ELF64_R_TYPE (rel->r_info);
8835 switch (r_type)
8836 {
8837 default:
8838 continue;
8839
8840 case R_PPC64_TOC16:
8841 case R_PPC64_TOC16_LO:
8842 case R_PPC64_TOC16_HI:
8843 case R_PPC64_TOC16_HA:
8844 case R_PPC64_TOC16_DS:
8845 case R_PPC64_TOC16_LO_DS:
8846 case R_PPC64_ADDR64:
8847 break;
8848 }
8849
8850 r_symndx = ELF64_R_SYM (rel->r_info);
8851 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8852 r_symndx, ibfd))
8853 goto error_ret;
8854
8855 if (sym_sec != toc)
8856 continue;
8857
8858 if (h != NULL)
8859 val = h->root.u.def.value;
8860 else
8861 {
8862 val = sym->st_value;
8863 if (val != 0)
8864 local_toc_syms = TRUE;
8865 }
8866
8867 val += rel->r_addend;
8868
8869 if (val > toc->rawsize)
8870 val = toc->rawsize;
8871 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8872 continue;
8873 else if ((skip[val >> 3] & can_optimize) != 0)
8874 {
8875 Elf_Internal_Rela *tocrel
8876 = toc_relocs + (skip[val >> 3] >> 2);
8877 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8878
8879 switch (r_type)
8880 {
8881 case R_PPC64_TOC16_HA:
8882 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8883 break;
8884
8885 case R_PPC64_TOC16_LO_DS:
8886 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8887 break;
8888
8889 default:
8890 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8891 ppc_howto_init ();
8892 info->callbacks->einfo
8893 /* xgettext:c-format */
8894 (_("%H: %s references "
8895 "optimized away TOC entry\n"),
8896 ibfd, sec, rel->r_offset,
8897 ppc64_elf_howto_table[r_type]->name);
8898 bfd_set_error (bfd_error_bad_value);
8899 goto error_ret;
8900 }
8901 rel->r_addend = tocrel->r_addend;
8902 elf_section_data (sec)->relocs = relstart;
8903 continue;
8904 }
8905
8906 if (h != NULL || sym->st_value != 0)
8907 continue;
8908
8909 rel->r_addend -= skip[val >> 3];
8910 elf_section_data (sec)->relocs = relstart;
8911 }
8912
8913 if (elf_section_data (sec)->relocs != relstart)
8914 free (relstart);
8915 }
8916
8917 /* We shouldn't have local or global symbols defined in the TOC,
8918 but handle them anyway. */
8919 if (local_syms != NULL)
8920 for (sym = local_syms;
8921 sym < local_syms + symtab_hdr->sh_info;
8922 ++sym)
8923 if (sym->st_value != 0
8924 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8925 {
8926 unsigned long i;
8927
8928 if (sym->st_value > toc->rawsize)
8929 i = toc->rawsize >> 3;
8930 else
8931 i = sym->st_value >> 3;
8932
8933 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8934 {
8935 if (local_toc_syms)
8936 _bfd_error_handler
8937 (_("%s defined on removed toc entry"),
8938 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8939 do
8940 ++i;
8941 while ((skip[i] & (ref_from_discarded | can_optimize)));
8942 sym->st_value = (bfd_vma) i << 3;
8943 }
8944
8945 sym->st_value -= skip[i];
8946 symtab_hdr->contents = (unsigned char *) local_syms;
8947 }
8948
8949 /* Adjust any global syms defined in this toc input section. */
8950 if (toc_inf.global_toc_syms)
8951 {
8952 toc_inf.toc = toc;
8953 toc_inf.skip = skip;
8954 toc_inf.global_toc_syms = FALSE;
8955 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8956 &toc_inf);
8957 }
8958
8959 if (toc->reloc_count != 0)
8960 {
8961 Elf_Internal_Shdr *rel_hdr;
8962 Elf_Internal_Rela *wrel;
8963 bfd_size_type sz;
8964
8965 /* Remove unused toc relocs, and adjust those we keep. */
8966 if (toc_relocs == NULL)
8967 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8968 info->keep_memory);
8969 if (toc_relocs == NULL)
8970 goto error_ret;
8971
8972 wrel = toc_relocs;
8973 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8974 if ((skip[rel->r_offset >> 3]
8975 & (ref_from_discarded | can_optimize)) == 0)
8976 {
8977 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8978 wrel->r_info = rel->r_info;
8979 wrel->r_addend = rel->r_addend;
8980 ++wrel;
8981 }
8982 else if (!dec_dynrel_count (rel->r_info, toc, info,
8983 &local_syms, NULL, NULL))
8984 goto error_ret;
8985
8986 elf_section_data (toc)->relocs = toc_relocs;
8987 toc->reloc_count = wrel - toc_relocs;
8988 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8989 sz = rel_hdr->sh_entsize;
8990 rel_hdr->sh_size = toc->reloc_count * sz;
8991 }
8992 }
8993 else if (toc_relocs != NULL
8994 && elf_section_data (toc)->relocs != toc_relocs)
8995 free (toc_relocs);
8996
8997 if (local_syms != NULL
8998 && symtab_hdr->contents != (unsigned char *) local_syms)
8999 {
9000 if (!info->keep_memory)
9001 free (local_syms);
9002 else
9003 symtab_hdr->contents = (unsigned char *) local_syms;
9004 }
9005 free (skip);
9006 }
9007
9008 /* Look for cases where we can change an indirect GOT access to
9009 a GOT relative or PC relative access, possibly reducing the
9010 number of GOT entries. */
9011 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9012 {
9013 asection *sec;
9014 Elf_Internal_Shdr *symtab_hdr;
9015 Elf_Internal_Sym *local_syms;
9016 Elf_Internal_Rela *relstart, *rel;
9017 bfd_vma got;
9018
9019 if (!is_ppc64_elf (ibfd))
9020 continue;
9021
9022 if (!ppc64_elf_tdata (ibfd)->has_optrel)
9023 continue;
9024
9025 sec = ppc64_elf_tdata (ibfd)->got;
9026 got = 0;
9027 if (sec != NULL)
9028 got = sec->output_section->vma + sec->output_offset + 0x8000;
9029
9030 local_syms = NULL;
9031 symtab_hdr = &elf_symtab_hdr (ibfd);
9032
9033 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9034 {
9035 if (sec->reloc_count == 0
9036 || !ppc64_elf_section_data (sec)->has_optrel
9037 || discarded_section (sec))
9038 continue;
9039
9040 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9041 info->keep_memory);
9042 if (relstart == NULL)
9043 {
9044 got_error_ret:
9045 if (local_syms != NULL
9046 && symtab_hdr->contents != (unsigned char *) local_syms)
9047 free (local_syms);
9048 if (sec != NULL
9049 && relstart != NULL
9050 && elf_section_data (sec)->relocs != relstart)
9051 free (relstart);
9052 return FALSE;
9053 }
9054
9055 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9056 {
9057 enum elf_ppc64_reloc_type r_type;
9058 unsigned long r_symndx;
9059 Elf_Internal_Sym *sym;
9060 asection *sym_sec;
9061 struct elf_link_hash_entry *h;
9062 struct got_entry *ent;
9063 bfd_vma val, pc;
9064 unsigned char buf[8];
9065 unsigned int insn;
9066 enum {no_check, check_lo, check_ha} insn_check;
9067
9068 r_type = ELF64_R_TYPE (rel->r_info);
9069 switch (r_type)
9070 {
9071 default:
9072 insn_check = no_check;
9073 break;
9074
9075 case R_PPC64_PLT16_HA:
9076 case R_PPC64_GOT_TLSLD16_HA:
9077 case R_PPC64_GOT_TLSGD16_HA:
9078 case R_PPC64_GOT_TPREL16_HA:
9079 case R_PPC64_GOT_DTPREL16_HA:
9080 case R_PPC64_GOT16_HA:
9081 case R_PPC64_TOC16_HA:
9082 insn_check = check_ha;
9083 break;
9084
9085 case R_PPC64_PLT16_LO:
9086 case R_PPC64_PLT16_LO_DS:
9087 case R_PPC64_GOT_TLSLD16_LO:
9088 case R_PPC64_GOT_TLSGD16_LO:
9089 case R_PPC64_GOT_TPREL16_LO_DS:
9090 case R_PPC64_GOT_DTPREL16_LO_DS:
9091 case R_PPC64_GOT16_LO:
9092 case R_PPC64_GOT16_LO_DS:
9093 case R_PPC64_TOC16_LO:
9094 case R_PPC64_TOC16_LO_DS:
9095 insn_check = check_lo;
9096 break;
9097 }
9098
9099 if (insn_check != no_check)
9100 {
9101 bfd_vma off = rel->r_offset & ~3;
9102
9103 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9104 goto got_error_ret;
9105
9106 insn = bfd_get_32 (ibfd, buf);
9107 if (insn_check == check_lo
9108 ? !ok_lo_toc_insn (insn, r_type)
9109 : ((insn & ((0x3f << 26) | 0x1f << 16))
9110 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9111 {
9112 char str[12];
9113
9114 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9115 sprintf (str, "%#08x", insn);
9116 info->callbacks->einfo
9117 /* xgettext:c-format */
9118 (_("%H: got/toc optimization is not supported for"
9119 " %s instruction\n"),
9120 ibfd, sec, rel->r_offset & ~3, str);
9121 continue;
9122 }
9123 }
9124
9125 switch (r_type)
9126 {
9127 /* Note that we don't delete GOT entries for
9128 R_PPC64_GOT16_DS since we'd need a lot more
9129 analysis. For starters, the preliminary layout is
9130 before the GOT, PLT, dynamic sections and stubs are
9131 laid out. Then we'd need to allow for changes in
9132 distance between sections caused by alignment. */
9133 default:
9134 continue;
9135
9136 case R_PPC64_GOT16_HA:
9137 case R_PPC64_GOT16_LO_DS:
9138 case R_PPC64_GOT_PCREL34:
9139 break;
9140 }
9141
9142 r_symndx = ELF64_R_SYM (rel->r_info);
9143 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9144 r_symndx, ibfd))
9145 goto got_error_ret;
9146
9147 if (sym_sec == NULL
9148 || sym_sec->output_section == NULL
9149 || discarded_section (sym_sec))
9150 continue;
9151
9152 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9153 continue;
9154
9155 if (h != NULL)
9156 val = h->root.u.def.value;
9157 else
9158 val = sym->st_value;
9159 val += rel->r_addend;
9160 val += sym_sec->output_section->vma + sym_sec->output_offset;
9161
9162 /* Fudge factor to allow for the fact that the preliminary layout
9163 isn't exact. Reduce limits by this factor. */
9164 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9165
9166 switch (r_type)
9167 {
9168 default:
9169 continue;
9170
9171 case R_PPC64_GOT16_HA:
9172 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9173 >= LIMIT_ADJUST (0x100000000ULL))
9174 continue;
9175
9176 if (!bfd_get_section_contents (ibfd, sec, buf,
9177 rel->r_offset & ~3, 4))
9178 goto got_error_ret;
9179 insn = bfd_get_32 (ibfd, buf);
9180 if (((insn & ((0x3f << 26) | 0x1f << 16))
9181 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9182 continue;
9183 break;
9184
9185 case R_PPC64_GOT16_LO_DS:
9186 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9187 >= LIMIT_ADJUST (0x100000000ULL))
9188 continue;
9189 if (!bfd_get_section_contents (ibfd, sec, buf,
9190 rel->r_offset & ~3, 4))
9191 goto got_error_ret;
9192 insn = bfd_get_32 (ibfd, buf);
9193 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9194 continue;
9195 break;
9196
9197 case R_PPC64_GOT_PCREL34:
9198 pc = rel->r_offset;
9199 pc += sec->output_section->vma + sec->output_offset;
9200 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9201 >= LIMIT_ADJUST (1ULL << 34))
9202 continue;
9203 if (!bfd_get_section_contents (ibfd, sec, buf,
9204 rel->r_offset & ~3, 8))
9205 goto got_error_ret;
9206 insn = bfd_get_32 (ibfd, buf);
9207 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9208 continue;
9209 insn = bfd_get_32 (ibfd, buf + 4);
9210 if ((insn & (0x3f << 26)) != 57u << 26)
9211 continue;
9212 break;
9213 }
9214 #undef LIMIT_ADJUST
9215
9216 if (h != NULL)
9217 ent = h->got.glist;
9218 else
9219 {
9220 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9221 ent = local_got_ents[r_symndx];
9222 }
9223 for (; ent != NULL; ent = ent->next)
9224 if (ent->addend == rel->r_addend
9225 && ent->owner == ibfd
9226 && ent->tls_type == 0)
9227 break;
9228 BFD_ASSERT (ent && ent->got.refcount > 0);
9229 ent->got.refcount -= 1;
9230 }
9231
9232 if (elf_section_data (sec)->relocs != relstart)
9233 free (relstart);
9234 }
9235
9236 if (local_syms != NULL
9237 && symtab_hdr->contents != (unsigned char *) local_syms)
9238 {
9239 if (!info->keep_memory)
9240 free (local_syms);
9241 else
9242 symtab_hdr->contents = (unsigned char *) local_syms;
9243 }
9244 }
9245
9246 return TRUE;
9247 }
9248
9249 /* Return true iff input section I references the TOC using
9250 instructions limited to +/-32k offsets. */
9251
9252 bfd_boolean
9253 ppc64_elf_has_small_toc_reloc (asection *i)
9254 {
9255 return (is_ppc64_elf (i->owner)
9256 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9257 }
9258
9259 /* Allocate space for one GOT entry. */
9260
9261 static void
9262 allocate_got (struct elf_link_hash_entry *h,
9263 struct bfd_link_info *info,
9264 struct got_entry *gent)
9265 {
9266 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9267 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9268 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9269 ? 16 : 8);
9270 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9271 ? 2 : 1) * sizeof (Elf64_External_Rela);
9272 asection *got = ppc64_elf_tdata (gent->owner)->got;
9273
9274 gent->got.offset = got->size;
9275 got->size += entsize;
9276
9277 if (h->type == STT_GNU_IFUNC)
9278 {
9279 htab->elf.irelplt->size += rentsize;
9280 htab->got_reli_size += rentsize;
9281 }
9282 else if (((bfd_link_pic (info)
9283 && !((gent->tls_type & TLS_TPREL) != 0
9284 && bfd_link_executable (info)
9285 && SYMBOL_REFERENCES_LOCAL (info, h)))
9286 || (htab->elf.dynamic_sections_created
9287 && h->dynindx != -1
9288 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9289 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9290 {
9291 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9292 relgot->size += rentsize;
9293 }
9294 }
9295
9296 /* This function merges got entries in the same toc group. */
9297
9298 static void
9299 merge_got_entries (struct got_entry **pent)
9300 {
9301 struct got_entry *ent, *ent2;
9302
9303 for (ent = *pent; ent != NULL; ent = ent->next)
9304 if (!ent->is_indirect)
9305 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9306 if (!ent2->is_indirect
9307 && ent2->addend == ent->addend
9308 && ent2->tls_type == ent->tls_type
9309 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9310 {
9311 ent2->is_indirect = TRUE;
9312 ent2->got.ent = ent;
9313 }
9314 }
9315
9316 /* If H is undefined, make it dynamic if that makes sense. */
9317
9318 static bfd_boolean
9319 ensure_undef_dynamic (struct bfd_link_info *info,
9320 struct elf_link_hash_entry *h)
9321 {
9322 struct elf_link_hash_table *htab = elf_hash_table (info);
9323
9324 if (htab->dynamic_sections_created
9325 && ((info->dynamic_undefined_weak != 0
9326 && h->root.type == bfd_link_hash_undefweak)
9327 || h->root.type == bfd_link_hash_undefined)
9328 && h->dynindx == -1
9329 && !h->forced_local
9330 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9331 return bfd_elf_link_record_dynamic_symbol (info, h);
9332 return TRUE;
9333 }
9334
9335 /* Allocate space in .plt, .got and associated reloc sections for
9336 dynamic relocs. */
9337
9338 static bfd_boolean
9339 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9340 {
9341 struct bfd_link_info *info;
9342 struct ppc_link_hash_table *htab;
9343 asection *s;
9344 struct ppc_link_hash_entry *eh;
9345 struct got_entry **pgent, *gent;
9346
9347 if (h->root.type == bfd_link_hash_indirect)
9348 return TRUE;
9349
9350 info = (struct bfd_link_info *) inf;
9351 htab = ppc_hash_table (info);
9352 if (htab == NULL)
9353 return FALSE;
9354
9355 eh = (struct ppc_link_hash_entry *) h;
9356 /* Run through the TLS GD got entries first if we're changing them
9357 to TPREL. */
9358 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9359 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9360 if (gent->got.refcount > 0
9361 && (gent->tls_type & TLS_GD) != 0)
9362 {
9363 /* This was a GD entry that has been converted to TPREL. If
9364 there happens to be a TPREL entry we can use that one. */
9365 struct got_entry *ent;
9366 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9367 if (ent->got.refcount > 0
9368 && (ent->tls_type & TLS_TPREL) != 0
9369 && ent->addend == gent->addend
9370 && ent->owner == gent->owner)
9371 {
9372 gent->got.refcount = 0;
9373 break;
9374 }
9375
9376 /* If not, then we'll be using our own TPREL entry. */
9377 if (gent->got.refcount != 0)
9378 gent->tls_type = TLS_TLS | TLS_TPREL;
9379 }
9380
9381 /* Remove any list entry that won't generate a word in the GOT before
9382 we call merge_got_entries. Otherwise we risk merging to empty
9383 entries. */
9384 pgent = &h->got.glist;
9385 while ((gent = *pgent) != NULL)
9386 if (gent->got.refcount > 0)
9387 {
9388 if ((gent->tls_type & TLS_LD) != 0
9389 && !h->def_dynamic)
9390 {
9391 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9392 *pgent = gent->next;
9393 }
9394 else
9395 pgent = &gent->next;
9396 }
9397 else
9398 *pgent = gent->next;
9399
9400 if (!htab->do_multi_toc)
9401 merge_got_entries (&h->got.glist);
9402
9403 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9404 if (!gent->is_indirect)
9405 {
9406 /* Make sure this symbol is output as a dynamic symbol. */
9407 if (!ensure_undef_dynamic (info, h))
9408 return FALSE;
9409
9410 if (!is_ppc64_elf (gent->owner))
9411 abort ();
9412
9413 allocate_got (h, info, gent);
9414 }
9415
9416 /* If no dynamic sections we can't have dynamic relocs, except for
9417 IFUNCs which are handled even in static executables. */
9418 if (!htab->elf.dynamic_sections_created
9419 && h->type != STT_GNU_IFUNC)
9420 eh->dyn_relocs = NULL;
9421
9422 /* Discard relocs on undefined symbols that must be local. */
9423 else if (h->root.type == bfd_link_hash_undefined
9424 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9425 eh->dyn_relocs = NULL;
9426
9427 /* Also discard relocs on undefined weak syms with non-default
9428 visibility, or when dynamic_undefined_weak says so. */
9429 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9430 eh->dyn_relocs = NULL;
9431
9432 if (eh->dyn_relocs != NULL)
9433 {
9434 struct elf_dyn_relocs *p, **pp;
9435
9436 /* In the shared -Bsymbolic case, discard space allocated for
9437 dynamic pc-relative relocs against symbols which turn out to
9438 be defined in regular objects. For the normal shared case,
9439 discard space for relocs that have become local due to symbol
9440 visibility changes. */
9441
9442 if (bfd_link_pic (info))
9443 {
9444 /* Relocs that use pc_count are those that appear on a call
9445 insn, or certain REL relocs (see must_be_dyn_reloc) that
9446 can be generated via assembly. We want calls to
9447 protected symbols to resolve directly to the function
9448 rather than going via the plt. If people want function
9449 pointer comparisons to work as expected then they should
9450 avoid writing weird assembly. */
9451 if (SYMBOL_CALLS_LOCAL (info, h))
9452 {
9453 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9454 {
9455 p->count -= p->pc_count;
9456 p->pc_count = 0;
9457 if (p->count == 0)
9458 *pp = p->next;
9459 else
9460 pp = &p->next;
9461 }
9462 }
9463
9464 if (eh->dyn_relocs != NULL)
9465 {
9466 /* Make sure this symbol is output as a dynamic symbol. */
9467 if (!ensure_undef_dynamic (info, h))
9468 return FALSE;
9469 }
9470 }
9471 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
9472 {
9473 /* For the non-pic case, discard space for relocs against
9474 symbols which turn out to need copy relocs or are not
9475 dynamic. */
9476 if (h->dynamic_adjusted
9477 && !h->def_regular
9478 && !ELF_COMMON_DEF_P (h))
9479 {
9480 /* Make sure this symbol is output as a dynamic symbol. */
9481 if (!ensure_undef_dynamic (info, h))
9482 return FALSE;
9483
9484 if (h->dynindx == -1)
9485 eh->dyn_relocs = NULL;
9486 }
9487 else
9488 eh->dyn_relocs = NULL;
9489 }
9490
9491 /* Finally, allocate space. */
9492 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9493 {
9494 asection *sreloc = elf_section_data (p->sec)->sreloc;
9495 if (eh->elf.type == STT_GNU_IFUNC)
9496 sreloc = htab->elf.irelplt;
9497 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9498 }
9499 }
9500
9501 /* We might need a PLT entry when the symbol
9502 a) is dynamic, or
9503 b) is an ifunc, or
9504 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9505 d) has plt16 relocs and we are linking statically. */
9506 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9507 || h->type == STT_GNU_IFUNC
9508 || (h->needs_plt && h->dynamic_adjusted)
9509 || (h->needs_plt
9510 && h->def_regular
9511 && !htab->elf.dynamic_sections_created
9512 && !htab->can_convert_all_inline_plt
9513 && (((struct ppc_link_hash_entry *) h)->tls_mask
9514 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
9515 {
9516 struct plt_entry *pent;
9517 bfd_boolean doneone = FALSE;
9518 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9519 if (pent->plt.refcount > 0)
9520 {
9521 if (!htab->elf.dynamic_sections_created
9522 || h->dynindx == -1)
9523 {
9524 if (h->type == STT_GNU_IFUNC)
9525 {
9526 s = htab->elf.iplt;
9527 pent->plt.offset = s->size;
9528 s->size += PLT_ENTRY_SIZE (htab);
9529 s = htab->elf.irelplt;
9530 }
9531 else
9532 {
9533 s = htab->pltlocal;
9534 pent->plt.offset = s->size;
9535 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9536 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9537 }
9538 }
9539 else
9540 {
9541 /* If this is the first .plt entry, make room for the special
9542 first entry. */
9543 s = htab->elf.splt;
9544 if (s->size == 0)
9545 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9546
9547 pent->plt.offset = s->size;
9548
9549 /* Make room for this entry. */
9550 s->size += PLT_ENTRY_SIZE (htab);
9551
9552 /* Make room for the .glink code. */
9553 s = htab->glink;
9554 if (s->size == 0)
9555 s->size += GLINK_PLTRESOLVE_SIZE (htab);
9556 if (htab->opd_abi)
9557 {
9558 /* We need bigger stubs past index 32767. */
9559 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9560 s->size += 4;
9561 s->size += 2*4;
9562 }
9563 else
9564 s->size += 4;
9565
9566 /* We also need to make an entry in the .rela.plt section. */
9567 s = htab->elf.srelplt;
9568 }
9569 if (s != NULL)
9570 s->size += sizeof (Elf64_External_Rela);
9571 doneone = TRUE;
9572 }
9573 else
9574 pent->plt.offset = (bfd_vma) -1;
9575 if (!doneone)
9576 {
9577 h->plt.plist = NULL;
9578 h->needs_plt = 0;
9579 }
9580 }
9581 else
9582 {
9583 h->plt.plist = NULL;
9584 h->needs_plt = 0;
9585 }
9586
9587 return TRUE;
9588 }
9589
9590 #define PPC_LO(v) ((v) & 0xffff)
9591 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9592 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9593 #define D34(v) \
9594 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9595 #define HA34(v) ((v + (1ULL << 33)) >> 34)
9596
9597 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9598 to set up space for global entry stubs. These are put in glink,
9599 after the branch table. */
9600
9601 static bfd_boolean
9602 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9603 {
9604 struct bfd_link_info *info;
9605 struct ppc_link_hash_table *htab;
9606 struct plt_entry *pent;
9607 asection *s, *plt;
9608
9609 if (h->root.type == bfd_link_hash_indirect)
9610 return TRUE;
9611
9612 if (!h->pointer_equality_needed)
9613 return TRUE;
9614
9615 if (h->def_regular)
9616 return TRUE;
9617
9618 info = inf;
9619 htab = ppc_hash_table (info);
9620 if (htab == NULL)
9621 return FALSE;
9622
9623 s = htab->global_entry;
9624 plt = htab->elf.splt;
9625 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9626 if (pent->plt.offset != (bfd_vma) -1
9627 && pent->addend == 0)
9628 {
9629 /* For ELFv2, if this symbol is not defined in a regular file
9630 and we are not generating a shared library or pie, then we
9631 need to define the symbol in the executable on a call stub.
9632 This is to avoid text relocations. */
9633 bfd_vma off, stub_align, stub_off, stub_size;
9634 unsigned int align_power;
9635
9636 stub_size = 16;
9637 stub_off = s->size;
9638 if (htab->params->plt_stub_align >= 0)
9639 align_power = htab->params->plt_stub_align;
9640 else
9641 align_power = -htab->params->plt_stub_align;
9642 /* Setting section alignment is delayed until we know it is
9643 non-empty. Otherwise the .text output section will be
9644 aligned at least to plt_stub_align even when no global
9645 entry stubs are needed. */
9646 if (s->alignment_power < align_power)
9647 s->alignment_power = align_power;
9648 stub_align = (bfd_vma) 1 << align_power;
9649 if (htab->params->plt_stub_align >= 0
9650 || ((((stub_off + stub_size - 1) & -stub_align)
9651 - (stub_off & -stub_align))
9652 > ((stub_size - 1) & -stub_align)))
9653 stub_off = (stub_off + stub_align - 1) & -stub_align;
9654 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9655 off -= stub_off + s->output_offset + s->output_section->vma;
9656 /* Note that for --plt-stub-align negative we have a possible
9657 dependency between stub offset and size. Break that
9658 dependency by assuming the max stub size when calculating
9659 the stub offset. */
9660 if (PPC_HA (off) == 0)
9661 stub_size -= 4;
9662 h->root.type = bfd_link_hash_defined;
9663 h->root.u.def.section = s;
9664 h->root.u.def.value = stub_off;
9665 s->size = stub_off + stub_size;
9666 break;
9667 }
9668 return TRUE;
9669 }
9670
9671 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9672 read-only sections. */
9673
9674 static bfd_boolean
9675 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
9676 {
9677 asection *sec;
9678
9679 if (h->root.type == bfd_link_hash_indirect)
9680 return TRUE;
9681
9682 sec = readonly_dynrelocs (h);
9683 if (sec != NULL)
9684 {
9685 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9686
9687 info->flags |= DF_TEXTREL;
9688 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9689 " in read-only section `%pA'\n"),
9690 sec->owner, h->root.root.string, sec);
9691
9692 /* Not an error, just cut short the traversal. */
9693 return FALSE;
9694 }
9695 return TRUE;
9696 }
9697
9698 /* Set the sizes of the dynamic sections. */
9699
9700 static bfd_boolean
9701 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9702 struct bfd_link_info *info)
9703 {
9704 struct ppc_link_hash_table *htab;
9705 bfd *dynobj;
9706 asection *s;
9707 bfd_boolean relocs;
9708 bfd *ibfd;
9709 struct got_entry *first_tlsld;
9710
9711 htab = ppc_hash_table (info);
9712 if (htab == NULL)
9713 return FALSE;
9714
9715 dynobj = htab->elf.dynobj;
9716 if (dynobj == NULL)
9717 abort ();
9718
9719 if (htab->elf.dynamic_sections_created)
9720 {
9721 /* Set the contents of the .interp section to the interpreter. */
9722 if (bfd_link_executable (info) && !info->nointerp)
9723 {
9724 s = bfd_get_linker_section (dynobj, ".interp");
9725 if (s == NULL)
9726 abort ();
9727 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9728 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9729 }
9730 }
9731
9732 /* Set up .got offsets for local syms, and space for local dynamic
9733 relocs. */
9734 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9735 {
9736 struct got_entry **lgot_ents;
9737 struct got_entry **end_lgot_ents;
9738 struct plt_entry **local_plt;
9739 struct plt_entry **end_local_plt;
9740 unsigned char *lgot_masks;
9741 bfd_size_type locsymcount;
9742 Elf_Internal_Shdr *symtab_hdr;
9743
9744 if (!is_ppc64_elf (ibfd))
9745 continue;
9746
9747 for (s = ibfd->sections; s != NULL; s = s->next)
9748 {
9749 struct ppc_dyn_relocs *p;
9750
9751 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9752 {
9753 if (!bfd_is_abs_section (p->sec)
9754 && bfd_is_abs_section (p->sec->output_section))
9755 {
9756 /* Input section has been discarded, either because
9757 it is a copy of a linkonce section or due to
9758 linker script /DISCARD/, so we'll be discarding
9759 the relocs too. */
9760 }
9761 else if (p->count != 0)
9762 {
9763 asection *srel = elf_section_data (p->sec)->sreloc;
9764 if (p->ifunc)
9765 srel = htab->elf.irelplt;
9766 srel->size += p->count * sizeof (Elf64_External_Rela);
9767 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9768 info->flags |= DF_TEXTREL;
9769 }
9770 }
9771 }
9772
9773 lgot_ents = elf_local_got_ents (ibfd);
9774 if (!lgot_ents)
9775 continue;
9776
9777 symtab_hdr = &elf_symtab_hdr (ibfd);
9778 locsymcount = symtab_hdr->sh_info;
9779 end_lgot_ents = lgot_ents + locsymcount;
9780 local_plt = (struct plt_entry **) end_lgot_ents;
9781 end_local_plt = local_plt + locsymcount;
9782 lgot_masks = (unsigned char *) end_local_plt;
9783 s = ppc64_elf_tdata (ibfd)->got;
9784 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9785 {
9786 struct got_entry **pent, *ent;
9787
9788 pent = lgot_ents;
9789 while ((ent = *pent) != NULL)
9790 if (ent->got.refcount > 0)
9791 {
9792 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9793 {
9794 ppc64_tlsld_got (ibfd)->got.refcount += 1;
9795 *pent = ent->next;
9796 }
9797 else
9798 {
9799 unsigned int ent_size = 8;
9800 unsigned int rel_size = sizeof (Elf64_External_Rela);
9801
9802 ent->got.offset = s->size;
9803 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9804 {
9805 ent_size *= 2;
9806 rel_size *= 2;
9807 }
9808 s->size += ent_size;
9809 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9810 {
9811 htab->elf.irelplt->size += rel_size;
9812 htab->got_reli_size += rel_size;
9813 }
9814 else if (bfd_link_pic (info)
9815 && !((ent->tls_type & TLS_TPREL) != 0
9816 && bfd_link_executable (info)))
9817 {
9818 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9819 srel->size += rel_size;
9820 }
9821 pent = &ent->next;
9822 }
9823 }
9824 else
9825 *pent = ent->next;
9826 }
9827
9828 /* Allocate space for plt calls to local syms. */
9829 lgot_masks = (unsigned char *) end_local_plt;
9830 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
9831 {
9832 struct plt_entry *ent;
9833
9834 for (ent = *local_plt; ent != NULL; ent = ent->next)
9835 if (ent->plt.refcount > 0)
9836 {
9837 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9838 {
9839 s = htab->elf.iplt;
9840 ent->plt.offset = s->size;
9841 s->size += PLT_ENTRY_SIZE (htab);
9842 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9843 }
9844 else if (htab->can_convert_all_inline_plt
9845 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
9846 ent->plt.offset = (bfd_vma) -1;
9847 else
9848 {
9849 s = htab->pltlocal;
9850 ent->plt.offset = s->size;
9851 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9852 if (bfd_link_pic (info))
9853 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9854 }
9855 }
9856 else
9857 ent->plt.offset = (bfd_vma) -1;
9858 }
9859 }
9860
9861 /* Allocate global sym .plt and .got entries, and space for global
9862 sym dynamic relocs. */
9863 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9864
9865 if (!htab->opd_abi && !bfd_link_pic (info))
9866 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9867
9868 first_tlsld = NULL;
9869 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9870 {
9871 struct got_entry *ent;
9872
9873 if (!is_ppc64_elf (ibfd))
9874 continue;
9875
9876 ent = ppc64_tlsld_got (ibfd);
9877 if (ent->got.refcount > 0)
9878 {
9879 if (!htab->do_multi_toc && first_tlsld != NULL)
9880 {
9881 ent->is_indirect = TRUE;
9882 ent->got.ent = first_tlsld;
9883 }
9884 else
9885 {
9886 if (first_tlsld == NULL)
9887 first_tlsld = ent;
9888 s = ppc64_elf_tdata (ibfd)->got;
9889 ent->got.offset = s->size;
9890 ent->owner = ibfd;
9891 s->size += 16;
9892 if (bfd_link_pic (info))
9893 {
9894 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9895 srel->size += sizeof (Elf64_External_Rela);
9896 }
9897 }
9898 }
9899 else
9900 ent->got.offset = (bfd_vma) -1;
9901 }
9902
9903 /* We now have determined the sizes of the various dynamic sections.
9904 Allocate memory for them. */
9905 relocs = FALSE;
9906 for (s = dynobj->sections; s != NULL; s = s->next)
9907 {
9908 if ((s->flags & SEC_LINKER_CREATED) == 0)
9909 continue;
9910
9911 if (s == htab->brlt || s == htab->relbrlt)
9912 /* These haven't been allocated yet; don't strip. */
9913 continue;
9914 else if (s == htab->elf.sgot
9915 || s == htab->elf.splt
9916 || s == htab->elf.iplt
9917 || s == htab->pltlocal
9918 || s == htab->glink
9919 || s == htab->global_entry
9920 || s == htab->elf.sdynbss
9921 || s == htab->elf.sdynrelro)
9922 {
9923 /* Strip this section if we don't need it; see the
9924 comment below. */
9925 }
9926 else if (s == htab->glink_eh_frame)
9927 {
9928 if (!bfd_is_abs_section (s->output_section))
9929 /* Not sized yet. */
9930 continue;
9931 }
9932 else if (CONST_STRNEQ (s->name, ".rela"))
9933 {
9934 if (s->size != 0)
9935 {
9936 if (s != htab->elf.srelplt)
9937 relocs = TRUE;
9938
9939 /* We use the reloc_count field as a counter if we need
9940 to copy relocs into the output file. */
9941 s->reloc_count = 0;
9942 }
9943 }
9944 else
9945 {
9946 /* It's not one of our sections, so don't allocate space. */
9947 continue;
9948 }
9949
9950 if (s->size == 0)
9951 {
9952 /* If we don't need this section, strip it from the
9953 output file. This is mostly to handle .rela.bss and
9954 .rela.plt. We must create both sections in
9955 create_dynamic_sections, because they must be created
9956 before the linker maps input sections to output
9957 sections. The linker does that before
9958 adjust_dynamic_symbol is called, and it is that
9959 function which decides whether anything needs to go
9960 into these sections. */
9961 s->flags |= SEC_EXCLUDE;
9962 continue;
9963 }
9964
9965 if (bfd_is_abs_section (s->output_section))
9966 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9967 s->name);
9968
9969 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9970 continue;
9971
9972 /* Allocate memory for the section contents. We use bfd_zalloc
9973 here in case unused entries are not reclaimed before the
9974 section's contents are written out. This should not happen,
9975 but this way if it does we get a R_PPC64_NONE reloc in .rela
9976 sections instead of garbage.
9977 We also rely on the section contents being zero when writing
9978 the GOT and .dynrelro. */
9979 s->contents = bfd_zalloc (dynobj, s->size);
9980 if (s->contents == NULL)
9981 return FALSE;
9982 }
9983
9984 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9985 {
9986 if (!is_ppc64_elf (ibfd))
9987 continue;
9988
9989 s = ppc64_elf_tdata (ibfd)->got;
9990 if (s != NULL && s != htab->elf.sgot)
9991 {
9992 if (s->size == 0)
9993 s->flags |= SEC_EXCLUDE;
9994 else
9995 {
9996 s->contents = bfd_zalloc (ibfd, s->size);
9997 if (s->contents == NULL)
9998 return FALSE;
9999 }
10000 }
10001 s = ppc64_elf_tdata (ibfd)->relgot;
10002 if (s != NULL)
10003 {
10004 if (s->size == 0)
10005 s->flags |= SEC_EXCLUDE;
10006 else
10007 {
10008 s->contents = bfd_zalloc (ibfd, s->size);
10009 if (s->contents == NULL)
10010 return FALSE;
10011 relocs = TRUE;
10012 s->reloc_count = 0;
10013 }
10014 }
10015 }
10016
10017 if (htab->elf.dynamic_sections_created)
10018 {
10019 bfd_boolean tls_opt;
10020
10021 /* Add some entries to the .dynamic section. We fill in the
10022 values later, in ppc64_elf_finish_dynamic_sections, but we
10023 must add the entries now so that we get the correct size for
10024 the .dynamic section. The DT_DEBUG entry is filled in by the
10025 dynamic linker and used by the debugger. */
10026 #define add_dynamic_entry(TAG, VAL) \
10027 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10028
10029 if (bfd_link_executable (info))
10030 {
10031 if (!add_dynamic_entry (DT_DEBUG, 0))
10032 return FALSE;
10033 }
10034
10035 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10036 {
10037 if (!add_dynamic_entry (DT_PLTGOT, 0)
10038 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10039 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10040 || !add_dynamic_entry (DT_JMPREL, 0)
10041 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10042 return FALSE;
10043 }
10044
10045 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10046 {
10047 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10048 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10049 return FALSE;
10050 }
10051
10052 tls_opt = (htab->params->tls_get_addr_opt
10053 && htab->tls_get_addr_fd != NULL
10054 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10055 if (tls_opt || !htab->opd_abi)
10056 {
10057 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10058 return FALSE;
10059 }
10060
10061 if (relocs)
10062 {
10063 if (!add_dynamic_entry (DT_RELA, 0)
10064 || !add_dynamic_entry (DT_RELASZ, 0)
10065 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10066 return FALSE;
10067
10068 /* If any dynamic relocs apply to a read-only section,
10069 then we need a DT_TEXTREL entry. */
10070 if ((info->flags & DF_TEXTREL) == 0)
10071 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10072
10073 if ((info->flags & DF_TEXTREL) != 0)
10074 {
10075 if (!add_dynamic_entry (DT_TEXTREL, 0))
10076 return FALSE;
10077 }
10078 }
10079 }
10080 #undef add_dynamic_entry
10081
10082 return TRUE;
10083 }
10084
10085 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10086
10087 static bfd_boolean
10088 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10089 {
10090 if (h->plt.plist != NULL
10091 && !h->def_regular
10092 && !h->pointer_equality_needed)
10093 return FALSE;
10094
10095 return _bfd_elf_hash_symbol (h);
10096 }
10097
10098 /* Determine the type of stub needed, if any, for a call. */
10099
10100 static inline enum ppc_stub_type
10101 ppc_type_of_stub (asection *input_sec,
10102 const Elf_Internal_Rela *rel,
10103 struct ppc_link_hash_entry **hash,
10104 struct plt_entry **plt_ent,
10105 bfd_vma destination,
10106 unsigned long local_off)
10107 {
10108 struct ppc_link_hash_entry *h = *hash;
10109 bfd_vma location;
10110 bfd_vma branch_offset;
10111 bfd_vma max_branch_offset;
10112 enum elf_ppc64_reloc_type r_type;
10113
10114 if (h != NULL)
10115 {
10116 struct plt_entry *ent;
10117 struct ppc_link_hash_entry *fdh = h;
10118 if (h->oh != NULL
10119 && h->oh->is_func_descriptor)
10120 {
10121 fdh = ppc_follow_link (h->oh);
10122 *hash = fdh;
10123 }
10124
10125 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10126 if (ent->addend == rel->r_addend
10127 && ent->plt.offset != (bfd_vma) -1)
10128 {
10129 *plt_ent = ent;
10130 return ppc_stub_plt_call;
10131 }
10132
10133 /* Here, we know we don't have a plt entry. If we don't have a
10134 either a defined function descriptor or a defined entry symbol
10135 in a regular object file, then it is pointless trying to make
10136 any other type of stub. */
10137 if (!is_static_defined (&fdh->elf)
10138 && !is_static_defined (&h->elf))
10139 return ppc_stub_none;
10140 }
10141 else if (elf_local_got_ents (input_sec->owner) != NULL)
10142 {
10143 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10144 struct plt_entry **local_plt = (struct plt_entry **)
10145 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10146 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10147
10148 if (local_plt[r_symndx] != NULL)
10149 {
10150 struct plt_entry *ent;
10151
10152 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10153 if (ent->addend == rel->r_addend
10154 && ent->plt.offset != (bfd_vma) -1)
10155 {
10156 *plt_ent = ent;
10157 return ppc_stub_plt_call;
10158 }
10159 }
10160 }
10161
10162 /* Determine where the call point is. */
10163 location = (input_sec->output_offset
10164 + input_sec->output_section->vma
10165 + rel->r_offset);
10166
10167 branch_offset = destination - location;
10168 r_type = ELF64_R_TYPE (rel->r_info);
10169
10170 /* Determine if a long branch stub is needed. */
10171 max_branch_offset = 1 << 25;
10172 if (r_type == R_PPC64_REL14
10173 || r_type == R_PPC64_REL14_BRTAKEN
10174 || r_type == R_PPC64_REL14_BRNTAKEN)
10175 max_branch_offset = 1 << 15;
10176
10177 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10178 /* We need a stub. Figure out whether a long_branch or plt_branch
10179 is needed later. */
10180 return ppc_stub_long_branch;
10181
10182 return ppc_stub_none;
10183 }
10184
10185 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10186 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10187 . mflr %r12
10188 . bcl 20,31,1f
10189 .1: mflr %r11
10190 . mtlr %r12
10191 . lis %r12,xxx-1b@highest
10192 . ori %r12,%r12,xxx-1b@higher
10193 . sldi %r12,%r12,32
10194 . oris %r12,%r12,xxx-1b@high
10195 . ori %r12,%r12,xxx-1b@l
10196 . add/ldx %r12,%r11,%r12 */
10197
10198 static bfd_byte *
10199 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10200 {
10201 bfd_put_32 (abfd, MFLR_R12, p);
10202 p += 4;
10203 bfd_put_32 (abfd, BCL_20_31, p);
10204 p += 4;
10205 bfd_put_32 (abfd, MFLR_R11, p);
10206 p += 4;
10207 bfd_put_32 (abfd, MTLR_R12, p);
10208 p += 4;
10209 if (off + 0x8000 < 0x10000)
10210 {
10211 if (load)
10212 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10213 else
10214 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10215 p += 4;
10216 }
10217 else if (off + 0x80008000ULL < 0x100000000ULL)
10218 {
10219 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10220 p += 4;
10221 if (load)
10222 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10223 else
10224 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10225 p += 4;
10226 }
10227 else
10228 {
10229 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10230 {
10231 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10232 p += 4;
10233 }
10234 else
10235 {
10236 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10237 p += 4;
10238 if (((off >> 32) & 0xffff) != 0)
10239 {
10240 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10241 p += 4;
10242 }
10243 }
10244 if (((off >> 32) & 0xffffffffULL) != 0)
10245 {
10246 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10247 p += 4;
10248 }
10249 if (PPC_HI (off) != 0)
10250 {
10251 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10252 p += 4;
10253 }
10254 if (PPC_LO (off) != 0)
10255 {
10256 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10257 p += 4;
10258 }
10259 if (load)
10260 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10261 else
10262 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10263 p += 4;
10264 }
10265 return p;
10266 }
10267
10268 static unsigned int
10269 size_offset (bfd_vma off)
10270 {
10271 unsigned int size;
10272 if (off + 0x8000 < 0x10000)
10273 size = 4;
10274 else if (off + 0x80008000ULL < 0x100000000ULL)
10275 size = 8;
10276 else
10277 {
10278 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10279 size = 4;
10280 else
10281 {
10282 size = 4;
10283 if (((off >> 32) & 0xffff) != 0)
10284 size += 4;
10285 }
10286 if (((off >> 32) & 0xffffffffULL) != 0)
10287 size += 4;
10288 if (PPC_HI (off) != 0)
10289 size += 4;
10290 if (PPC_LO (off) != 0)
10291 size += 4;
10292 size += 4;
10293 }
10294 return size + 16;
10295 }
10296
10297 static unsigned int
10298 num_relocs_for_offset (bfd_vma off)
10299 {
10300 unsigned int num_rel;
10301 if (off + 0x8000 < 0x10000)
10302 num_rel = 1;
10303 else if (off + 0x80008000ULL < 0x100000000ULL)
10304 num_rel = 2;
10305 else
10306 {
10307 num_rel = 1;
10308 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10309 && ((off >> 32) & 0xffff) != 0)
10310 num_rel += 1;
10311 if (PPC_HI (off) != 0)
10312 num_rel += 1;
10313 if (PPC_LO (off) != 0)
10314 num_rel += 1;
10315 }
10316 return num_rel;
10317 }
10318
10319 static Elf_Internal_Rela *
10320 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10321 bfd_vma roff, bfd_vma targ, bfd_vma off)
10322 {
10323 bfd_vma relative_targ = targ - (roff - 8);
10324 if (bfd_big_endian (info->output_bfd))
10325 roff += 2;
10326 r->r_offset = roff;
10327 r->r_addend = relative_targ + roff;
10328 if (off + 0x8000 < 0x10000)
10329 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10330 else if (off + 0x80008000ULL < 0x100000000ULL)
10331 {
10332 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10333 ++r;
10334 roff += 4;
10335 r->r_offset = roff;
10336 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10337 r->r_addend = relative_targ + roff;
10338 }
10339 else
10340 {
10341 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10342 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10343 else
10344 {
10345 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10346 if (((off >> 32) & 0xffff) != 0)
10347 {
10348 ++r;
10349 roff += 4;
10350 r->r_offset = roff;
10351 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10352 r->r_addend = relative_targ + roff;
10353 }
10354 }
10355 if (((off >> 32) & 0xffffffffULL) != 0)
10356 roff += 4;
10357 if (PPC_HI (off) != 0)
10358 {
10359 ++r;
10360 roff += 4;
10361 r->r_offset = roff;
10362 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10363 r->r_addend = relative_targ + roff;
10364 }
10365 if (PPC_LO (off) != 0)
10366 {
10367 ++r;
10368 roff += 4;
10369 r->r_offset = roff;
10370 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10371 r->r_addend = relative_targ + roff;
10372 }
10373 }
10374 return r;
10375 }
10376
10377 static bfd_byte *
10378 build_powerxx_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10379 bfd_boolean load)
10380 {
10381 uint64_t insn;
10382 if (off - odd + (1ULL << 33) < 1ULL << 34)
10383 {
10384 off -= odd;
10385 if (odd)
10386 {
10387 bfd_put_32 (abfd, NOP, p);
10388 p += 4;
10389 }
10390 if (load)
10391 insn = PLD_R12_PC;
10392 else
10393 insn = PADDI_R12_PC;
10394 insn |= D34 (off);
10395 bfd_put_32 (abfd, insn >> 32, p);
10396 p += 4;
10397 bfd_put_32 (abfd, insn, p);
10398 }
10399 /* The minimum value for paddi is -0x200000000. The minimum value
10400 for li is -0x8000, which when shifted by 34 and added gives a
10401 minimum value of -0x2000200000000. The maximum value is
10402 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10403 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10404 {
10405 off -= 8 - odd;
10406 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10407 p += 4;
10408 if (!odd)
10409 {
10410 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10411 p += 4;
10412 }
10413 insn = PADDI_R12_PC | D34 (off);
10414 bfd_put_32 (abfd, insn >> 32, p);
10415 p += 4;
10416 bfd_put_32 (abfd, insn, p);
10417 p += 4;
10418 if (odd)
10419 {
10420 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10421 p += 4;
10422 }
10423 if (load)
10424 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10425 else
10426 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10427 }
10428 else
10429 {
10430 off -= odd + 8;
10431 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10432 p += 4;
10433 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10434 p += 4;
10435 if (odd)
10436 {
10437 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10438 p += 4;
10439 }
10440 insn = PADDI_R12_PC | D34 (off);
10441 bfd_put_32 (abfd, insn >> 32, p);
10442 p += 4;
10443 bfd_put_32 (abfd, insn, p);
10444 p += 4;
10445 if (!odd)
10446 {
10447 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10448 p += 4;
10449 }
10450 if (load)
10451 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10452 else
10453 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10454 }
10455 p += 4;
10456 return p;
10457 }
10458
10459 static unsigned int
10460 size_powerxx_offset (bfd_vma off, int odd)
10461 {
10462 if (off - odd + (1ULL << 33) < 1ULL << 34)
10463 return odd + 8;
10464 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10465 return 20;
10466 else
10467 return 24;
10468 }
10469
10470 static unsigned int
10471 num_relocs_for_powerxx_offset (bfd_vma off, int odd)
10472 {
10473 if (off - odd + (1ULL << 33) < 1ULL << 34)
10474 return 1;
10475 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10476 return 2;
10477 else
10478 return 3;
10479 }
10480
10481 static Elf_Internal_Rela *
10482 emit_relocs_for_powerxx_offset (struct bfd_link_info *info,
10483 Elf_Internal_Rela *r, bfd_vma roff,
10484 bfd_vma targ, bfd_vma off, int odd)
10485 {
10486 if (off - odd + (1ULL << 33) < 1ULL << 34)
10487 roff += odd;
10488 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10489 {
10490 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10491 r->r_offset = roff + d_offset;
10492 r->r_addend = targ + 8 - odd - d_offset;
10493 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10494 ++r;
10495 roff += 8 - odd;
10496 }
10497 else
10498 {
10499 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10500 r->r_offset = roff + d_offset;
10501 r->r_addend = targ + 8 + odd - d_offset;
10502 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10503 ++r;
10504 roff += 4;
10505 r->r_offset = roff + d_offset;
10506 r->r_addend = targ + 4 + odd - d_offset;
10507 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10508 ++r;
10509 roff += 4 + odd;
10510 }
10511 r->r_offset = roff;
10512 r->r_addend = targ;
10513 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10514 return r;
10515 }
10516
10517 /* Emit .eh_frame opcode to advance pc by DELTA. */
10518
10519 static bfd_byte *
10520 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10521 {
10522 delta /= 4;
10523 if (delta < 64)
10524 *eh++ = DW_CFA_advance_loc + delta;
10525 else if (delta < 256)
10526 {
10527 *eh++ = DW_CFA_advance_loc1;
10528 *eh++ = delta;
10529 }
10530 else if (delta < 65536)
10531 {
10532 *eh++ = DW_CFA_advance_loc2;
10533 bfd_put_16 (abfd, delta, eh);
10534 eh += 2;
10535 }
10536 else
10537 {
10538 *eh++ = DW_CFA_advance_loc4;
10539 bfd_put_32 (abfd, delta, eh);
10540 eh += 4;
10541 }
10542 return eh;
10543 }
10544
10545 /* Size of required .eh_frame opcode to advance pc by DELTA. */
10546
10547 static unsigned int
10548 eh_advance_size (unsigned int delta)
10549 {
10550 if (delta < 64 * 4)
10551 /* DW_CFA_advance_loc+[1..63]. */
10552 return 1;
10553 if (delta < 256 * 4)
10554 /* DW_CFA_advance_loc1, byte. */
10555 return 2;
10556 if (delta < 65536 * 4)
10557 /* DW_CFA_advance_loc2, 2 bytes. */
10558 return 3;
10559 /* DW_CFA_advance_loc4, 4 bytes. */
10560 return 5;
10561 }
10562
10563 /* With power7 weakly ordered memory model, it is possible for ld.so
10564 to update a plt entry in one thread and have another thread see a
10565 stale zero toc entry. To avoid this we need some sort of acquire
10566 barrier in the call stub. One solution is to make the load of the
10567 toc word seem to appear to depend on the load of the function entry
10568 word. Another solution is to test for r2 being zero, and branch to
10569 the appropriate glink entry if so.
10570
10571 . fake dep barrier compare
10572 . ld 12,xxx(2) ld 12,xxx(2)
10573 . mtctr 12 mtctr 12
10574 . xor 11,12,12 ld 2,xxx+8(2)
10575 . add 2,2,11 cmpldi 2,0
10576 . ld 2,xxx+8(2) bnectr+
10577 . bctr b <glink_entry>
10578
10579 The solution involving the compare turns out to be faster, so
10580 that's what we use unless the branch won't reach. */
10581
10582 #define ALWAYS_USE_FAKE_DEP 0
10583 #define ALWAYS_EMIT_R2SAVE 0
10584
10585 static inline unsigned int
10586 plt_stub_size (struct ppc_link_hash_table *htab,
10587 struct ppc_stub_hash_entry *stub_entry,
10588 bfd_vma off)
10589 {
10590 unsigned size;
10591
10592 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10593 {
10594 if (htab->powerxx_stubs)
10595 {
10596 bfd_vma start = (stub_entry->stub_offset
10597 + stub_entry->group->stub_sec->output_offset
10598 + stub_entry->group->stub_sec->output_section->vma);
10599 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10600 start += 4;
10601 size = 8 + size_powerxx_offset (off, start & 4);
10602 }
10603 else
10604 size = 8 + size_offset (off - 8);
10605 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10606 size += 4;
10607 return size;
10608 }
10609
10610 size = 12;
10611 if (ALWAYS_EMIT_R2SAVE
10612 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10613 size += 4;
10614 if (PPC_HA (off) != 0)
10615 size += 4;
10616 if (htab->opd_abi)
10617 {
10618 size += 4;
10619 if (htab->params->plt_static_chain)
10620 size += 4;
10621 if (htab->params->plt_thread_safe
10622 && htab->elf.dynamic_sections_created
10623 && stub_entry->h != NULL
10624 && stub_entry->h->elf.dynindx != -1)
10625 size += 8;
10626 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10627 size += 4;
10628 }
10629 if (stub_entry->h != NULL
10630 && (stub_entry->h == htab->tls_get_addr_fd
10631 || stub_entry->h == htab->tls_get_addr)
10632 && htab->params->tls_get_addr_opt)
10633 {
10634 size += 7 * 4;
10635 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
10636 size += 6 * 4;
10637 }
10638 return size;
10639 }
10640
10641 /* Depending on the sign of plt_stub_align:
10642 If positive, return the padding to align to a 2**plt_stub_align
10643 boundary.
10644 If negative, if this stub would cross fewer 2**plt_stub_align
10645 boundaries if we align, then return the padding needed to do so. */
10646
10647 static inline unsigned int
10648 plt_stub_pad (struct ppc_link_hash_table *htab,
10649 struct ppc_stub_hash_entry *stub_entry,
10650 bfd_vma plt_off)
10651 {
10652 int stub_align;
10653 unsigned stub_size;
10654 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10655
10656 if (htab->params->plt_stub_align >= 0)
10657 {
10658 stub_align = 1 << htab->params->plt_stub_align;
10659 if ((stub_off & (stub_align - 1)) != 0)
10660 return stub_align - (stub_off & (stub_align - 1));
10661 return 0;
10662 }
10663
10664 stub_align = 1 << -htab->params->plt_stub_align;
10665 stub_size = plt_stub_size (htab, stub_entry, plt_off);
10666 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10667 > ((stub_size - 1) & -stub_align))
10668 return stub_align - (stub_off & (stub_align - 1));
10669 return 0;
10670 }
10671
10672 /* Build a .plt call stub. */
10673
10674 static inline bfd_byte *
10675 build_plt_stub (struct ppc_link_hash_table *htab,
10676 struct ppc_stub_hash_entry *stub_entry,
10677 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10678 {
10679 bfd *obfd = htab->params->stub_bfd;
10680 bfd_boolean plt_load_toc = htab->opd_abi;
10681 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10682 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10683 && htab->elf.dynamic_sections_created
10684 && stub_entry->h != NULL
10685 && stub_entry->h->elf.dynindx != -1);
10686 bfd_boolean use_fake_dep = plt_thread_safe;
10687 bfd_vma cmp_branch_off = 0;
10688
10689 if (!ALWAYS_USE_FAKE_DEP
10690 && plt_load_toc
10691 && plt_thread_safe
10692 && !((stub_entry->h == htab->tls_get_addr_fd
10693 || stub_entry->h == htab->tls_get_addr)
10694 && htab->params->tls_get_addr_opt))
10695 {
10696 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10697 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10698 / PLT_ENTRY_SIZE (htab));
10699 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10700 bfd_vma to, from;
10701
10702 if (pltindex > 32768)
10703 glinkoff += (pltindex - 32768) * 4;
10704 to = (glinkoff
10705 + htab->glink->output_offset
10706 + htab->glink->output_section->vma);
10707 from = (p - stub_entry->group->stub_sec->contents
10708 + 4 * (ALWAYS_EMIT_R2SAVE
10709 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10710 + 4 * (PPC_HA (offset) != 0)
10711 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10712 != PPC_HA (offset))
10713 + 4 * (plt_static_chain != 0)
10714 + 20
10715 + stub_entry->group->stub_sec->output_offset
10716 + stub_entry->group->stub_sec->output_section->vma);
10717 cmp_branch_off = to - from;
10718 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10719 }
10720
10721 if (PPC_HA (offset) != 0)
10722 {
10723 if (r != NULL)
10724 {
10725 if (ALWAYS_EMIT_R2SAVE
10726 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10727 r[0].r_offset += 4;
10728 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10729 r[1].r_offset = r[0].r_offset + 4;
10730 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10731 r[1].r_addend = r[0].r_addend;
10732 if (plt_load_toc)
10733 {
10734 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10735 {
10736 r[2].r_offset = r[1].r_offset + 4;
10737 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10738 r[2].r_addend = r[0].r_addend;
10739 }
10740 else
10741 {
10742 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10743 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10744 r[2].r_addend = r[0].r_addend + 8;
10745 if (plt_static_chain)
10746 {
10747 r[3].r_offset = r[2].r_offset + 4;
10748 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10749 r[3].r_addend = r[0].r_addend + 16;
10750 }
10751 }
10752 }
10753 }
10754 if (ALWAYS_EMIT_R2SAVE
10755 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10756 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10757 if (plt_load_toc)
10758 {
10759 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10760 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10761 }
10762 else
10763 {
10764 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10765 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10766 }
10767 if (plt_load_toc
10768 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10769 {
10770 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10771 offset = 0;
10772 }
10773 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10774 if (plt_load_toc)
10775 {
10776 if (use_fake_dep)
10777 {
10778 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10779 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10780 }
10781 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10782 if (plt_static_chain)
10783 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10784 }
10785 }
10786 else
10787 {
10788 if (r != NULL)
10789 {
10790 if (ALWAYS_EMIT_R2SAVE
10791 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10792 r[0].r_offset += 4;
10793 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10794 if (plt_load_toc)
10795 {
10796 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10797 {
10798 r[1].r_offset = r[0].r_offset + 4;
10799 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10800 r[1].r_addend = r[0].r_addend;
10801 }
10802 else
10803 {
10804 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10805 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10806 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10807 if (plt_static_chain)
10808 {
10809 r[2].r_offset = r[1].r_offset + 4;
10810 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10811 r[2].r_addend = r[0].r_addend + 8;
10812 }
10813 }
10814 }
10815 }
10816 if (ALWAYS_EMIT_R2SAVE
10817 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10818 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10819 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10820 if (plt_load_toc
10821 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10822 {
10823 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10824 offset = 0;
10825 }
10826 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10827 if (plt_load_toc)
10828 {
10829 if (use_fake_dep)
10830 {
10831 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10832 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10833 }
10834 if (plt_static_chain)
10835 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10836 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10837 }
10838 }
10839 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10840 {
10841 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10842 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10843 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10844 }
10845 else
10846 bfd_put_32 (obfd, BCTR, p), p += 4;
10847 return p;
10848 }
10849
10850 /* Build a special .plt call stub for __tls_get_addr. */
10851
10852 #define LD_R11_0R3 0xe9630000
10853 #define LD_R12_0R3 0xe9830000
10854 #define MR_R0_R3 0x7c601b78
10855 #define CMPDI_R11_0 0x2c2b0000
10856 #define ADD_R3_R12_R13 0x7c6c6a14
10857 #define BEQLR 0x4d820020
10858 #define MR_R3_R0 0x7c030378
10859 #define STD_R11_0R1 0xf9610000
10860 #define BCTRL 0x4e800421
10861 #define LD_R11_0R1 0xe9610000
10862 #define MTLR_R11 0x7d6803a6
10863
10864 static inline bfd_byte *
10865 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10866 struct ppc_stub_hash_entry *stub_entry,
10867 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10868 {
10869 bfd *obfd = htab->params->stub_bfd;
10870 bfd_byte *loc = p;
10871
10872 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10873 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10874 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10875 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10876 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10877 bfd_put_32 (obfd, BEQLR, p), p += 4;
10878 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10879 if (r != NULL)
10880 r[0].r_offset += 7 * 4;
10881 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
10882 return build_plt_stub (htab, stub_entry, p, offset, r);
10883
10884 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10885 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10886
10887 if (r != NULL)
10888 r[0].r_offset += 2 * 4;
10889 p = build_plt_stub (htab, stub_entry, p, offset, r);
10890 bfd_put_32 (obfd, BCTRL, p - 4);
10891
10892 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10893 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10894 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10895 bfd_put_32 (obfd, BLR, p), p += 4;
10896
10897 if (htab->glink_eh_frame != NULL
10898 && htab->glink_eh_frame->size != 0)
10899 {
10900 bfd_byte *base, *eh;
10901 unsigned int lr_used, delta;
10902
10903 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10904 eh = base + stub_entry->group->eh_size;
10905 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10906 delta = lr_used - stub_entry->group->lr_restore;
10907 stub_entry->group->lr_restore = lr_used + 16;
10908 eh = eh_advance (htab->elf.dynobj, eh, delta);
10909 *eh++ = DW_CFA_offset_extended_sf;
10910 *eh++ = 65;
10911 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10912 *eh++ = DW_CFA_advance_loc + 4;
10913 *eh++ = DW_CFA_restore_extended;
10914 *eh++ = 65;
10915 stub_entry->group->eh_size = eh - base;
10916 }
10917 return p;
10918 }
10919
10920 static Elf_Internal_Rela *
10921 get_relocs (asection *sec, int count)
10922 {
10923 Elf_Internal_Rela *relocs;
10924 struct bfd_elf_section_data *elfsec_data;
10925
10926 elfsec_data = elf_section_data (sec);
10927 relocs = elfsec_data->relocs;
10928 if (relocs == NULL)
10929 {
10930 bfd_size_type relsize;
10931 relsize = sec->reloc_count * sizeof (*relocs);
10932 relocs = bfd_alloc (sec->owner, relsize);
10933 if (relocs == NULL)
10934 return NULL;
10935 elfsec_data->relocs = relocs;
10936 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10937 sizeof (Elf_Internal_Shdr));
10938 if (elfsec_data->rela.hdr == NULL)
10939 return NULL;
10940 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10941 * sizeof (Elf64_External_Rela));
10942 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10943 sec->reloc_count = 0;
10944 }
10945 relocs += sec->reloc_count;
10946 sec->reloc_count += count;
10947 return relocs;
10948 }
10949
10950 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10951 forms, to the equivalent relocs against the global symbol given by
10952 STUB_ENTRY->H. */
10953
10954 static bfd_boolean
10955 use_global_in_relocs (struct ppc_link_hash_table *htab,
10956 struct ppc_stub_hash_entry *stub_entry,
10957 Elf_Internal_Rela *r, unsigned int num_rel)
10958 {
10959 struct elf_link_hash_entry **hashes;
10960 unsigned long symndx;
10961 struct ppc_link_hash_entry *h;
10962 bfd_vma symval;
10963
10964 /* Relocs are always against symbols in their own object file. Fake
10965 up global sym hashes for the stub bfd (which has no symbols). */
10966 hashes = elf_sym_hashes (htab->params->stub_bfd);
10967 if (hashes == NULL)
10968 {
10969 bfd_size_type hsize;
10970
10971 /* When called the first time, stub_globals will contain the
10972 total number of symbols seen during stub sizing. After
10973 allocating, stub_globals is used as an index to fill the
10974 hashes array. */
10975 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10976 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10977 if (hashes == NULL)
10978 return FALSE;
10979 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10980 htab->stub_globals = 1;
10981 }
10982 symndx = htab->stub_globals++;
10983 h = stub_entry->h;
10984 hashes[symndx] = &h->elf;
10985 if (h->oh != NULL && h->oh->is_func)
10986 h = ppc_follow_link (h->oh);
10987 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10988 || h->elf.root.type == bfd_link_hash_defweak);
10989 symval = (h->elf.root.u.def.value
10990 + h->elf.root.u.def.section->output_offset
10991 + h->elf.root.u.def.section->output_section->vma);
10992 while (num_rel-- != 0)
10993 {
10994 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10995 if (h->elf.root.u.def.section != stub_entry->target_section)
10996 {
10997 /* H is an opd symbol. The addend must be zero, and the
10998 branch reloc is the only one we can convert. */
10999 r->r_addend = 0;
11000 break;
11001 }
11002 else
11003 r->r_addend -= symval;
11004 --r;
11005 }
11006 return TRUE;
11007 }
11008
11009 static bfd_vma
11010 get_r2off (struct bfd_link_info *info,
11011 struct ppc_stub_hash_entry *stub_entry)
11012 {
11013 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11014 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11015
11016 if (r2off == 0)
11017 {
11018 /* Support linking -R objects. Get the toc pointer from the
11019 opd entry. */
11020 char buf[8];
11021 if (!htab->opd_abi)
11022 return r2off;
11023 asection *opd = stub_entry->h->elf.root.u.def.section;
11024 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11025
11026 if (strcmp (opd->name, ".opd") != 0
11027 || opd->reloc_count != 0)
11028 {
11029 info->callbacks->einfo
11030 (_("%P: cannot find opd entry toc for `%pT'\n"),
11031 stub_entry->h->elf.root.root.string);
11032 bfd_set_error (bfd_error_bad_value);
11033 return (bfd_vma) -1;
11034 }
11035 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11036 return (bfd_vma) -1;
11037 r2off = bfd_get_64 (opd->owner, buf);
11038 r2off -= elf_gp (info->output_bfd);
11039 }
11040 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11041 return r2off;
11042 }
11043
11044 static bfd_boolean
11045 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11046 {
11047 struct ppc_stub_hash_entry *stub_entry;
11048 struct ppc_branch_hash_entry *br_entry;
11049 struct bfd_link_info *info;
11050 struct ppc_link_hash_table *htab;
11051 bfd_byte *loc;
11052 bfd_byte *p, *relp;
11053 bfd_vma targ, off;
11054 Elf_Internal_Rela *r;
11055 asection *plt;
11056 int num_rel;
11057 int odd;
11058
11059 /* Massage our args to the form they really have. */
11060 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11061 info = in_arg;
11062
11063 htab = ppc_hash_table (info);
11064 if (htab == NULL)
11065 return FALSE;
11066
11067 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
11068 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11069
11070 htab->stub_count[stub_entry->stub_type - 1] += 1;
11071 switch (stub_entry->stub_type)
11072 {
11073 case ppc_stub_long_branch:
11074 case ppc_stub_long_branch_r2off:
11075 /* Branches are relative. This is where we are going to. */
11076 targ = (stub_entry->target_value
11077 + stub_entry->target_section->output_offset
11078 + stub_entry->target_section->output_section->vma);
11079 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11080
11081 /* And this is where we are coming from. */
11082 off = (stub_entry->stub_offset
11083 + stub_entry->group->stub_sec->output_offset
11084 + stub_entry->group->stub_sec->output_section->vma);
11085 off = targ - off;
11086
11087 p = loc;
11088 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11089 {
11090 bfd_vma r2off = get_r2off (info, stub_entry);
11091
11092 if (r2off == (bfd_vma) -1)
11093 {
11094 htab->stub_error = TRUE;
11095 return FALSE;
11096 }
11097 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11098 p += 4;
11099 if (PPC_HA (r2off) != 0)
11100 {
11101 bfd_put_32 (htab->params->stub_bfd,
11102 ADDIS_R2_R2 | PPC_HA (r2off), p);
11103 p += 4;
11104 }
11105 if (PPC_LO (r2off) != 0)
11106 {
11107 bfd_put_32 (htab->params->stub_bfd,
11108 ADDI_R2_R2 | PPC_LO (r2off), p);
11109 p += 4;
11110 }
11111 off -= p - loc;
11112 }
11113 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11114 p += 4;
11115
11116 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11117 {
11118 _bfd_error_handler
11119 (_("long branch stub `%s' offset overflow"),
11120 stub_entry->root.string);
11121 htab->stub_error = TRUE;
11122 return FALSE;
11123 }
11124
11125 if (info->emitrelocations)
11126 {
11127 r = get_relocs (stub_entry->group->stub_sec, 1);
11128 if (r == NULL)
11129 return FALSE;
11130 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11131 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11132 r->r_addend = targ;
11133 if (stub_entry->h != NULL
11134 && !use_global_in_relocs (htab, stub_entry, r, 1))
11135 return FALSE;
11136 }
11137 break;
11138
11139 case ppc_stub_plt_branch:
11140 case ppc_stub_plt_branch_r2off:
11141 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11142 stub_entry->root.string + 9,
11143 FALSE, FALSE);
11144 if (br_entry == NULL)
11145 {
11146 _bfd_error_handler (_("can't find branch stub `%s'"),
11147 stub_entry->root.string);
11148 htab->stub_error = TRUE;
11149 return FALSE;
11150 }
11151
11152 targ = (stub_entry->target_value
11153 + stub_entry->target_section->output_offset
11154 + stub_entry->target_section->output_section->vma);
11155 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11156 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11157
11158 bfd_put_64 (htab->brlt->owner, targ,
11159 htab->brlt->contents + br_entry->offset);
11160
11161 if (br_entry->iter == htab->stub_iteration)
11162 {
11163 br_entry->iter = 0;
11164
11165 if (htab->relbrlt != NULL)
11166 {
11167 /* Create a reloc for the branch lookup table entry. */
11168 Elf_Internal_Rela rela;
11169 bfd_byte *rl;
11170
11171 rela.r_offset = (br_entry->offset
11172 + htab->brlt->output_offset
11173 + htab->brlt->output_section->vma);
11174 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11175 rela.r_addend = targ;
11176
11177 rl = htab->relbrlt->contents;
11178 rl += (htab->relbrlt->reloc_count++
11179 * sizeof (Elf64_External_Rela));
11180 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11181 }
11182 else if (info->emitrelocations)
11183 {
11184 r = get_relocs (htab->brlt, 1);
11185 if (r == NULL)
11186 return FALSE;
11187 /* brlt, being SEC_LINKER_CREATED does not go through the
11188 normal reloc processing. Symbols and offsets are not
11189 translated from input file to output file form, so
11190 set up the offset per the output file. */
11191 r->r_offset = (br_entry->offset
11192 + htab->brlt->output_offset
11193 + htab->brlt->output_section->vma);
11194 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11195 r->r_addend = targ;
11196 }
11197 }
11198
11199 targ = (br_entry->offset
11200 + htab->brlt->output_offset
11201 + htab->brlt->output_section->vma);
11202
11203 off = (elf_gp (info->output_bfd)
11204 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11205 off = targ - off;
11206
11207 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11208 {
11209 info->callbacks->einfo
11210 (_("%P: linkage table error against `%pT'\n"),
11211 stub_entry->root.string);
11212 bfd_set_error (bfd_error_bad_value);
11213 htab->stub_error = TRUE;
11214 return FALSE;
11215 }
11216
11217 if (info->emitrelocations)
11218 {
11219 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11220 if (r == NULL)
11221 return FALSE;
11222 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11223 if (bfd_big_endian (info->output_bfd))
11224 r[0].r_offset += 2;
11225 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11226 r[0].r_offset += 4;
11227 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11228 r[0].r_addend = targ;
11229 if (PPC_HA (off) != 0)
11230 {
11231 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11232 r[1].r_offset = r[0].r_offset + 4;
11233 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11234 r[1].r_addend = r[0].r_addend;
11235 }
11236 }
11237
11238 p = loc;
11239 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11240 {
11241 if (PPC_HA (off) != 0)
11242 {
11243 bfd_put_32 (htab->params->stub_bfd,
11244 ADDIS_R12_R2 | PPC_HA (off), p);
11245 p += 4;
11246 bfd_put_32 (htab->params->stub_bfd,
11247 LD_R12_0R12 | PPC_LO (off), p);
11248 }
11249 else
11250 bfd_put_32 (htab->params->stub_bfd,
11251 LD_R12_0R2 | PPC_LO (off), p);
11252 }
11253 else
11254 {
11255 bfd_vma r2off = get_r2off (info, stub_entry);
11256
11257 if (r2off == (bfd_vma) -1)
11258 {
11259 htab->stub_error = TRUE;
11260 return FALSE;
11261 }
11262
11263 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11264 p += 4;
11265 if (PPC_HA (off) != 0)
11266 {
11267 bfd_put_32 (htab->params->stub_bfd,
11268 ADDIS_R12_R2 | PPC_HA (off), p);
11269 p += 4;
11270 bfd_put_32 (htab->params->stub_bfd,
11271 LD_R12_0R12 | PPC_LO (off), p);
11272 }
11273 else
11274 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11275
11276 if (PPC_HA (r2off) != 0)
11277 {
11278 p += 4;
11279 bfd_put_32 (htab->params->stub_bfd,
11280 ADDIS_R2_R2 | PPC_HA (r2off), p);
11281 }
11282 if (PPC_LO (r2off) != 0)
11283 {
11284 p += 4;
11285 bfd_put_32 (htab->params->stub_bfd,
11286 ADDI_R2_R2 | PPC_LO (r2off), p);
11287 }
11288 }
11289 p += 4;
11290 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11291 p += 4;
11292 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11293 p += 4;
11294 break;
11295
11296 case ppc_stub_long_branch_notoc:
11297 case ppc_stub_long_branch_both:
11298 case ppc_stub_plt_branch_notoc:
11299 case ppc_stub_plt_branch_both:
11300 case ppc_stub_plt_call_notoc:
11301 case ppc_stub_plt_call_both:
11302 p = loc;
11303 off = (stub_entry->stub_offset
11304 + stub_entry->group->stub_sec->output_offset
11305 + stub_entry->group->stub_sec->output_section->vma);
11306 if (stub_entry->stub_type == ppc_stub_long_branch_both
11307 || stub_entry->stub_type == ppc_stub_plt_branch_both
11308 || stub_entry->stub_type == ppc_stub_plt_call_both)
11309 {
11310 off += 4;
11311 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11312 p += 4;
11313 }
11314 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11315 {
11316 targ = stub_entry->plt_ent->plt.offset & ~1;
11317 if (targ >= (bfd_vma) -2)
11318 abort ();
11319
11320 plt = htab->elf.splt;
11321 if (!htab->elf.dynamic_sections_created
11322 || stub_entry->h == NULL
11323 || stub_entry->h->elf.dynindx == -1)
11324 {
11325 if (stub_entry->symtype == STT_GNU_IFUNC)
11326 plt = htab->elf.iplt;
11327 else
11328 plt = htab->pltlocal;
11329 }
11330 targ += plt->output_offset + plt->output_section->vma;
11331 }
11332 else
11333 targ = (stub_entry->target_value
11334 + stub_entry->target_section->output_offset
11335 + stub_entry->target_section->output_section->vma);
11336 odd = off & 4;
11337 off = targ - off;
11338
11339 relp = p;
11340 num_rel = 0;
11341 if (htab->powerxx_stubs)
11342 {
11343 bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
11344 p = build_powerxx_offset (htab->params->stub_bfd, p, off, odd, load);
11345 }
11346 else
11347 {
11348 /* The notoc stubs calculate their target (either a PLT entry or
11349 the global entry point of a function) relative to the PC
11350 returned by the "bcl" two instructions past the start of the
11351 sequence emitted by build_offset. The offset is therefore 8
11352 less than calculated from the start of the sequence. */
11353 off -= 8;
11354 p = build_offset (htab->params->stub_bfd, p, off,
11355 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11356 }
11357
11358 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
11359 {
11360 bfd_vma from;
11361 num_rel = 1;
11362 from = (stub_entry->stub_offset
11363 + stub_entry->group->stub_sec->output_offset
11364 + stub_entry->group->stub_sec->output_section->vma
11365 + (p - loc));
11366 bfd_put_32 (htab->params->stub_bfd,
11367 B_DOT | ((targ - from) & 0x3fffffc), p);
11368 }
11369 else
11370 {
11371 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11372 p += 4;
11373 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11374 }
11375 p += 4;
11376
11377 if (info->emitrelocations)
11378 {
11379 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
11380 if (htab->powerxx_stubs)
11381 num_rel += num_relocs_for_powerxx_offset (off, odd);
11382 else
11383 {
11384 num_rel += num_relocs_for_offset (off);
11385 roff += 16;
11386 }
11387 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11388 if (r == NULL)
11389 return FALSE;
11390 if (htab->powerxx_stubs)
11391 r = emit_relocs_for_powerxx_offset (info, r, roff, targ, off, odd);
11392 else
11393 r = emit_relocs_for_offset (info, r, roff, targ, off);
11394 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11395 || stub_entry->stub_type == ppc_stub_long_branch_both)
11396 {
11397 ++r;
11398 roff = p - 4 - stub_entry->group->stub_sec->contents;
11399 r->r_offset = roff;
11400 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11401 r->r_addend = targ;
11402 if (stub_entry->h != NULL
11403 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11404 return FALSE;
11405 }
11406 }
11407
11408 if (!htab->powerxx_stubs
11409 && htab->glink_eh_frame != NULL
11410 && htab->glink_eh_frame->size != 0)
11411 {
11412 bfd_byte *base, *eh;
11413 unsigned int lr_used, delta;
11414
11415 base = (htab->glink_eh_frame->contents
11416 + stub_entry->group->eh_base + 17);
11417 eh = base + stub_entry->group->eh_size;
11418 lr_used = stub_entry->stub_offset + 8;
11419 if (stub_entry->stub_type == ppc_stub_long_branch_both
11420 || stub_entry->stub_type == ppc_stub_plt_branch_both
11421 || stub_entry->stub_type == ppc_stub_plt_call_both)
11422 lr_used += 4;
11423 delta = lr_used - stub_entry->group->lr_restore;
11424 stub_entry->group->lr_restore = lr_used + 8;
11425 eh = eh_advance (htab->elf.dynobj, eh, delta);
11426 *eh++ = DW_CFA_register;
11427 *eh++ = 65;
11428 *eh++ = 12;
11429 *eh++ = DW_CFA_advance_loc + 2;
11430 *eh++ = DW_CFA_restore_extended;
11431 *eh++ = 65;
11432 stub_entry->group->eh_size = eh - base;
11433 }
11434 break;
11435
11436 case ppc_stub_plt_call:
11437 case ppc_stub_plt_call_r2save:
11438 if (stub_entry->h != NULL
11439 && stub_entry->h->is_func_descriptor
11440 && stub_entry->h->oh != NULL)
11441 {
11442 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11443
11444 /* If the old-ABI "dot-symbol" is undefined make it weak so
11445 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11446 if (fh->elf.root.type == bfd_link_hash_undefined
11447 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11448 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11449 fh->elf.root.type = bfd_link_hash_undefweak;
11450 }
11451
11452 /* Now build the stub. */
11453 targ = stub_entry->plt_ent->plt.offset & ~1;
11454 if (targ >= (bfd_vma) -2)
11455 abort ();
11456
11457 plt = htab->elf.splt;
11458 if (!htab->elf.dynamic_sections_created
11459 || stub_entry->h == NULL
11460 || stub_entry->h->elf.dynindx == -1)
11461 {
11462 if (stub_entry->symtype == STT_GNU_IFUNC)
11463 plt = htab->elf.iplt;
11464 else
11465 plt = htab->pltlocal;
11466 }
11467 targ += plt->output_offset + plt->output_section->vma;
11468
11469 off = (elf_gp (info->output_bfd)
11470 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11471 off = targ - off;
11472
11473 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11474 {
11475 info->callbacks->einfo
11476 /* xgettext:c-format */
11477 (_("%P: linkage table error against `%pT'\n"),
11478 stub_entry->h != NULL
11479 ? stub_entry->h->elf.root.root.string
11480 : "<local sym>");
11481 bfd_set_error (bfd_error_bad_value);
11482 htab->stub_error = TRUE;
11483 return FALSE;
11484 }
11485
11486 r = NULL;
11487 if (info->emitrelocations)
11488 {
11489 r = get_relocs (stub_entry->group->stub_sec,
11490 ((PPC_HA (off) != 0)
11491 + (htab->opd_abi
11492 ? 2 + (htab->params->plt_static_chain
11493 && PPC_HA (off + 16) == PPC_HA (off))
11494 : 1)));
11495 if (r == NULL)
11496 return FALSE;
11497 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11498 if (bfd_big_endian (info->output_bfd))
11499 r[0].r_offset += 2;
11500 r[0].r_addend = targ;
11501 }
11502 if (stub_entry->h != NULL
11503 && (stub_entry->h == htab->tls_get_addr_fd
11504 || stub_entry->h == htab->tls_get_addr)
11505 && htab->params->tls_get_addr_opt)
11506 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11507 else
11508 p = build_plt_stub (htab, stub_entry, loc, off, r);
11509 break;
11510
11511 case ppc_stub_save_res:
11512 return TRUE;
11513
11514 default:
11515 BFD_FAIL ();
11516 return FALSE;
11517 }
11518
11519 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11520
11521 if (htab->params->emit_stub_syms)
11522 {
11523 struct elf_link_hash_entry *h;
11524 size_t len1, len2;
11525 char *name;
11526 const char *const stub_str[] = { "long_branch",
11527 "long_branch",
11528 "long_branch",
11529 "long_branch",
11530 "plt_branch",
11531 "plt_branch",
11532 "plt_branch",
11533 "plt_branch",
11534 "plt_call",
11535 "plt_call",
11536 "plt_call",
11537 "plt_call" };
11538
11539 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11540 len2 = strlen (stub_entry->root.string);
11541 name = bfd_malloc (len1 + len2 + 2);
11542 if (name == NULL)
11543 return FALSE;
11544 memcpy (name, stub_entry->root.string, 9);
11545 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11546 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11547 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11548 if (h == NULL)
11549 return FALSE;
11550 if (h->root.type == bfd_link_hash_new)
11551 {
11552 h->root.type = bfd_link_hash_defined;
11553 h->root.u.def.section = stub_entry->group->stub_sec;
11554 h->root.u.def.value = stub_entry->stub_offset;
11555 h->ref_regular = 1;
11556 h->def_regular = 1;
11557 h->ref_regular_nonweak = 1;
11558 h->forced_local = 1;
11559 h->non_elf = 0;
11560 h->root.linker_def = 1;
11561 }
11562 }
11563
11564 return TRUE;
11565 }
11566
11567 /* As above, but don't actually build the stub. Just bump offset so
11568 we know stub section sizes, and select plt_branch stubs where
11569 long_branch stubs won't do. */
11570
11571 static bfd_boolean
11572 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11573 {
11574 struct ppc_stub_hash_entry *stub_entry;
11575 struct bfd_link_info *info;
11576 struct ppc_link_hash_table *htab;
11577 asection *plt;
11578 bfd_vma targ, off, r2off;
11579 unsigned int size, extra, lr_used, delta, odd;
11580
11581 /* Massage our args to the form they really have. */
11582 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11583 info = in_arg;
11584
11585 htab = ppc_hash_table (info);
11586 if (htab == NULL)
11587 return FALSE;
11588
11589 /* Make a note of the offset within the stubs for this entry. */
11590 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11591
11592 if (stub_entry->h != NULL
11593 && stub_entry->h->save_res
11594 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11595 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11596 {
11597 /* Don't make stubs to out-of-line register save/restore
11598 functions. Instead, emit copies of the functions. */
11599 stub_entry->group->needs_save_res = 1;
11600 stub_entry->stub_type = ppc_stub_save_res;
11601 return TRUE;
11602 }
11603
11604 switch (stub_entry->stub_type)
11605 {
11606 case ppc_stub_plt_branch:
11607 case ppc_stub_plt_branch_r2off:
11608 /* Reset the stub type from the plt branch variant in case we now
11609 can reach with a shorter stub. */
11610 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11611 /* Fall through. */
11612 case ppc_stub_long_branch:
11613 case ppc_stub_long_branch_r2off:
11614 targ = (stub_entry->target_value
11615 + stub_entry->target_section->output_offset
11616 + stub_entry->target_section->output_section->vma);
11617 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11618 off = (stub_entry->stub_offset
11619 + stub_entry->group->stub_sec->output_offset
11620 + stub_entry->group->stub_sec->output_section->vma);
11621
11622 size = 4;
11623 r2off = 0;
11624 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11625 {
11626 r2off = get_r2off (info, stub_entry);
11627 if (r2off == (bfd_vma) -1)
11628 {
11629 htab->stub_error = TRUE;
11630 return FALSE;
11631 }
11632 size = 8;
11633 if (PPC_HA (r2off) != 0)
11634 size += 4;
11635 if (PPC_LO (r2off) != 0)
11636 size += 4;
11637 off += size - 4;
11638 }
11639 off = targ - off;
11640
11641 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11642 Do the same for -R objects without function descriptors. */
11643 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11644 && r2off == 0
11645 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11646 || off + (1 << 25) >= (bfd_vma) (1 << 26))
11647 {
11648 struct ppc_branch_hash_entry *br_entry;
11649
11650 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11651 stub_entry->root.string + 9,
11652 TRUE, FALSE);
11653 if (br_entry == NULL)
11654 {
11655 _bfd_error_handler (_("can't build branch stub `%s'"),
11656 stub_entry->root.string);
11657 htab->stub_error = TRUE;
11658 return FALSE;
11659 }
11660
11661 if (br_entry->iter != htab->stub_iteration)
11662 {
11663 br_entry->iter = htab->stub_iteration;
11664 br_entry->offset = htab->brlt->size;
11665 htab->brlt->size += 8;
11666
11667 if (htab->relbrlt != NULL)
11668 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11669 else if (info->emitrelocations)
11670 {
11671 htab->brlt->reloc_count += 1;
11672 htab->brlt->flags |= SEC_RELOC;
11673 }
11674 }
11675
11676 targ = (br_entry->offset
11677 + htab->brlt->output_offset
11678 + htab->brlt->output_section->vma);
11679 off = (elf_gp (info->output_bfd)
11680 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11681 off = targ - off;
11682
11683 if (info->emitrelocations)
11684 {
11685 stub_entry->group->stub_sec->reloc_count
11686 += 1 + (PPC_HA (off) != 0);
11687 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11688 }
11689
11690 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11691 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11692 {
11693 size = 12;
11694 if (PPC_HA (off) != 0)
11695 size = 16;
11696 }
11697 else
11698 {
11699 size = 16;
11700 if (PPC_HA (off) != 0)
11701 size += 4;
11702
11703 if (PPC_HA (r2off) != 0)
11704 size += 4;
11705 if (PPC_LO (r2off) != 0)
11706 size += 4;
11707 }
11708 }
11709 else if (info->emitrelocations)
11710 {
11711 stub_entry->group->stub_sec->reloc_count += 1;
11712 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11713 }
11714 break;
11715
11716 case ppc_stub_plt_branch_notoc:
11717 case ppc_stub_plt_branch_both:
11718 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11719 /* Fall through. */
11720 case ppc_stub_long_branch_notoc:
11721 case ppc_stub_long_branch_both:
11722 off = (stub_entry->stub_offset
11723 + stub_entry->group->stub_sec->output_offset
11724 + stub_entry->group->stub_sec->output_section->vma);
11725 size = 0;
11726 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11727 size = 4;
11728 off += size;
11729 targ = (stub_entry->target_value
11730 + stub_entry->target_section->output_offset
11731 + stub_entry->target_section->output_section->vma);
11732 odd = off & 4;
11733 off = targ - off;
11734
11735 if (info->emitrelocations)
11736 {
11737 unsigned int num_rel;
11738 if (htab->powerxx_stubs)
11739 num_rel = num_relocs_for_powerxx_offset (off, odd);
11740 else
11741 num_rel = num_relocs_for_offset (off - 8);
11742 stub_entry->group->stub_sec->reloc_count += num_rel;
11743 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11744 }
11745
11746 if (htab->powerxx_stubs)
11747 extra = size_powerxx_offset (off, odd);
11748 else
11749 extra = size_offset (off - 8);
11750 /* Include branch insn plus those in the offset sequence. */
11751 size += 4 + extra;
11752 /* The branch insn is at the end, or "extra" bytes along. So
11753 its offset will be "extra" bytes less that that already
11754 calculated. */
11755 off -= extra;
11756
11757 if (!htab->powerxx_stubs)
11758 {
11759 /* After the bcl, lr has been modified so we need to emit
11760 .eh_frame info saying the return address is in r12. */
11761 lr_used = stub_entry->stub_offset + 8;
11762 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11763 lr_used += 4;
11764 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11765 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11766 DW_CFA_restore_extended 65. */
11767 delta = lr_used - stub_entry->group->lr_restore;
11768 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11769 stub_entry->group->lr_restore = lr_used + 8;
11770 }
11771
11772 /* If the branch can't reach, use a plt_branch. */
11773 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11774 {
11775 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11776 - ppc_stub_long_branch_notoc);
11777 size += 4;
11778 }
11779 else if (info->emitrelocations)
11780 stub_entry->group->stub_sec->reloc_count +=1;
11781 break;
11782
11783 case ppc_stub_plt_call_notoc:
11784 case ppc_stub_plt_call_both:
11785 off = (stub_entry->stub_offset
11786 + stub_entry->group->stub_sec->output_offset
11787 + stub_entry->group->stub_sec->output_section->vma);
11788 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11789 off += 4;
11790 targ = stub_entry->plt_ent->plt.offset & ~1;
11791 if (targ >= (bfd_vma) -2)
11792 abort ();
11793
11794 plt = htab->elf.splt;
11795 if (!htab->elf.dynamic_sections_created
11796 || stub_entry->h == NULL
11797 || stub_entry->h->elf.dynindx == -1)
11798 {
11799 if (stub_entry->symtype == STT_GNU_IFUNC)
11800 plt = htab->elf.iplt;
11801 else
11802 plt = htab->pltlocal;
11803 }
11804 targ += plt->output_offset + plt->output_section->vma;
11805 odd = off & 4;
11806 off = targ - off;
11807
11808 if (htab->params->plt_stub_align != 0)
11809 {
11810 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11811
11812 stub_entry->group->stub_sec->size += pad;
11813 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11814 off -= pad;
11815 }
11816
11817 if (info->emitrelocations)
11818 {
11819 unsigned int num_rel;
11820 if (htab->powerxx_stubs)
11821 num_rel = num_relocs_for_powerxx_offset (off, odd);
11822 else
11823 num_rel = num_relocs_for_offset (off - 8);
11824 stub_entry->group->stub_sec->reloc_count += num_rel;
11825 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11826 }
11827
11828 size = plt_stub_size (htab, stub_entry, off);
11829
11830 if (!htab->powerxx_stubs)
11831 {
11832 /* After the bcl, lr has been modified so we need to emit
11833 .eh_frame info saying the return address is in r12. */
11834 lr_used = stub_entry->stub_offset + 8;
11835 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11836 lr_used += 4;
11837 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11838 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11839 DW_CFA_restore_extended 65. */
11840 delta = lr_used - stub_entry->group->lr_restore;
11841 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11842 stub_entry->group->lr_restore = lr_used + 8;
11843 }
11844 break;
11845
11846 case ppc_stub_plt_call:
11847 case ppc_stub_plt_call_r2save:
11848 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11849 if (targ >= (bfd_vma) -2)
11850 abort ();
11851 plt = htab->elf.splt;
11852 if (!htab->elf.dynamic_sections_created
11853 || stub_entry->h == NULL
11854 || stub_entry->h->elf.dynindx == -1)
11855 {
11856 if (stub_entry->symtype == STT_GNU_IFUNC)
11857 plt = htab->elf.iplt;
11858 else
11859 plt = htab->pltlocal;
11860 }
11861 targ += plt->output_offset + plt->output_section->vma;
11862
11863 off = (elf_gp (info->output_bfd)
11864 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11865 off = targ - off;
11866
11867 if (htab->params->plt_stub_align != 0)
11868 {
11869 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11870
11871 stub_entry->group->stub_sec->size += pad;
11872 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11873 }
11874
11875 if (info->emitrelocations)
11876 {
11877 stub_entry->group->stub_sec->reloc_count
11878 += ((PPC_HA (off) != 0)
11879 + (htab->opd_abi
11880 ? 2 + (htab->params->plt_static_chain
11881 && PPC_HA (off + 16) == PPC_HA (off))
11882 : 1));
11883 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11884 }
11885
11886 size = plt_stub_size (htab, stub_entry, off);
11887
11888 if (stub_entry->h != NULL
11889 && (stub_entry->h == htab->tls_get_addr_fd
11890 || stub_entry->h == htab->tls_get_addr)
11891 && htab->params->tls_get_addr_opt
11892 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11893 {
11894 /* After the bctrl, lr has been modified so we need to
11895 emit .eh_frame info saying the return address is
11896 on the stack. In fact we put the EH info specifying
11897 that the return address is on the stack *at* the
11898 call rather than after it, because the EH info for a
11899 call needs to be specified by that point.
11900 See libgcc/unwind-dw2.c execute_cfa_program. */
11901 lr_used = stub_entry->stub_offset + size - 20;
11902 /* The eh_frame info will consist of a DW_CFA_advance_loc
11903 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11904 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11905 delta = lr_used - stub_entry->group->lr_restore;
11906 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11907 stub_entry->group->lr_restore = size - 4;
11908 }
11909 break;
11910
11911 default:
11912 BFD_FAIL ();
11913 return FALSE;
11914 }
11915
11916 stub_entry->group->stub_sec->size += size;
11917 return TRUE;
11918 }
11919
11920 /* Set up various things so that we can make a list of input sections
11921 for each output section included in the link. Returns -1 on error,
11922 0 when no stubs will be needed, and 1 on success. */
11923
11924 int
11925 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11926 {
11927 unsigned int id;
11928 bfd_size_type amt;
11929 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11930
11931 if (htab == NULL)
11932 return -1;
11933
11934 htab->sec_info_arr_size = _bfd_section_id;
11935 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11936 htab->sec_info = bfd_zmalloc (amt);
11937 if (htab->sec_info == NULL)
11938 return -1;
11939
11940 /* Set toc_off for com, und, abs and ind sections. */
11941 for (id = 0; id < 3; id++)
11942 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11943
11944 return 1;
11945 }
11946
11947 /* Set up for first pass at multitoc partitioning. */
11948
11949 void
11950 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11951 {
11952 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11953
11954 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11955 htab->toc_bfd = NULL;
11956 htab->toc_first_sec = NULL;
11957 }
11958
11959 /* The linker repeatedly calls this function for each TOC input section
11960 and linker generated GOT section. Group input bfds such that the toc
11961 within a group is less than 64k in size. */
11962
11963 bfd_boolean
11964 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11965 {
11966 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11967 bfd_vma addr, off, limit;
11968
11969 if (htab == NULL)
11970 return FALSE;
11971
11972 if (!htab->second_toc_pass)
11973 {
11974 /* Keep track of the first .toc or .got section for this input bfd. */
11975 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11976
11977 if (new_bfd)
11978 {
11979 htab->toc_bfd = isec->owner;
11980 htab->toc_first_sec = isec;
11981 }
11982
11983 addr = isec->output_offset + isec->output_section->vma;
11984 off = addr - htab->toc_curr;
11985 limit = 0x80008000;
11986 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11987 limit = 0x10000;
11988 if (off + isec->size > limit)
11989 {
11990 addr = (htab->toc_first_sec->output_offset
11991 + htab->toc_first_sec->output_section->vma);
11992 htab->toc_curr = addr;
11993 htab->toc_curr &= -TOC_BASE_ALIGN;
11994 }
11995
11996 /* toc_curr is the base address of this toc group. Set elf_gp
11997 for the input section to be the offset relative to the
11998 output toc base plus 0x8000. Making the input elf_gp an
11999 offset allows us to move the toc as a whole without
12000 recalculating input elf_gp. */
12001 off = htab->toc_curr - elf_gp (info->output_bfd);
12002 off += TOC_BASE_OFF;
12003
12004 /* Die if someone uses a linker script that doesn't keep input
12005 file .toc and .got together. */
12006 if (new_bfd
12007 && elf_gp (isec->owner) != 0
12008 && elf_gp (isec->owner) != off)
12009 return FALSE;
12010
12011 elf_gp (isec->owner) = off;
12012 return TRUE;
12013 }
12014
12015 /* During the second pass toc_first_sec points to the start of
12016 a toc group, and toc_curr is used to track the old elf_gp.
12017 We use toc_bfd to ensure we only look at each bfd once. */
12018 if (htab->toc_bfd == isec->owner)
12019 return TRUE;
12020 htab->toc_bfd = isec->owner;
12021
12022 if (htab->toc_first_sec == NULL
12023 || htab->toc_curr != elf_gp (isec->owner))
12024 {
12025 htab->toc_curr = elf_gp (isec->owner);
12026 htab->toc_first_sec = isec;
12027 }
12028 addr = (htab->toc_first_sec->output_offset
12029 + htab->toc_first_sec->output_section->vma);
12030 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12031 elf_gp (isec->owner) = off;
12032
12033 return TRUE;
12034 }
12035
12036 /* Called via elf_link_hash_traverse to merge GOT entries for global
12037 symbol H. */
12038
12039 static bfd_boolean
12040 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12041 {
12042 if (h->root.type == bfd_link_hash_indirect)
12043 return TRUE;
12044
12045 merge_got_entries (&h->got.glist);
12046
12047 return TRUE;
12048 }
12049
12050 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12051 symbol H. */
12052
12053 static bfd_boolean
12054 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12055 {
12056 struct got_entry *gent;
12057
12058 if (h->root.type == bfd_link_hash_indirect)
12059 return TRUE;
12060
12061 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12062 if (!gent->is_indirect)
12063 allocate_got (h, (struct bfd_link_info *) inf, gent);
12064 return TRUE;
12065 }
12066
12067 /* Called on the first multitoc pass after the last call to
12068 ppc64_elf_next_toc_section. This function removes duplicate GOT
12069 entries. */
12070
12071 bfd_boolean
12072 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12073 {
12074 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12075 struct bfd *ibfd, *ibfd2;
12076 bfd_boolean done_something;
12077
12078 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12079
12080 if (!htab->do_multi_toc)
12081 return FALSE;
12082
12083 /* Merge global sym got entries within a toc group. */
12084 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12085
12086 /* And tlsld_got. */
12087 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12088 {
12089 struct got_entry *ent, *ent2;
12090
12091 if (!is_ppc64_elf (ibfd))
12092 continue;
12093
12094 ent = ppc64_tlsld_got (ibfd);
12095 if (!ent->is_indirect
12096 && ent->got.offset != (bfd_vma) -1)
12097 {
12098 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12099 {
12100 if (!is_ppc64_elf (ibfd2))
12101 continue;
12102
12103 ent2 = ppc64_tlsld_got (ibfd2);
12104 if (!ent2->is_indirect
12105 && ent2->got.offset != (bfd_vma) -1
12106 && elf_gp (ibfd2) == elf_gp (ibfd))
12107 {
12108 ent2->is_indirect = TRUE;
12109 ent2->got.ent = ent;
12110 }
12111 }
12112 }
12113 }
12114
12115 /* Zap sizes of got sections. */
12116 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12117 htab->elf.irelplt->size -= htab->got_reli_size;
12118 htab->got_reli_size = 0;
12119
12120 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12121 {
12122 asection *got, *relgot;
12123
12124 if (!is_ppc64_elf (ibfd))
12125 continue;
12126
12127 got = ppc64_elf_tdata (ibfd)->got;
12128 if (got != NULL)
12129 {
12130 got->rawsize = got->size;
12131 got->size = 0;
12132 relgot = ppc64_elf_tdata (ibfd)->relgot;
12133 relgot->rawsize = relgot->size;
12134 relgot->size = 0;
12135 }
12136 }
12137
12138 /* Now reallocate the got, local syms first. We don't need to
12139 allocate section contents again since we never increase size. */
12140 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12141 {
12142 struct got_entry **lgot_ents;
12143 struct got_entry **end_lgot_ents;
12144 struct plt_entry **local_plt;
12145 struct plt_entry **end_local_plt;
12146 unsigned char *lgot_masks;
12147 bfd_size_type locsymcount;
12148 Elf_Internal_Shdr *symtab_hdr;
12149 asection *s;
12150
12151 if (!is_ppc64_elf (ibfd))
12152 continue;
12153
12154 lgot_ents = elf_local_got_ents (ibfd);
12155 if (!lgot_ents)
12156 continue;
12157
12158 symtab_hdr = &elf_symtab_hdr (ibfd);
12159 locsymcount = symtab_hdr->sh_info;
12160 end_lgot_ents = lgot_ents + locsymcount;
12161 local_plt = (struct plt_entry **) end_lgot_ents;
12162 end_local_plt = local_plt + locsymcount;
12163 lgot_masks = (unsigned char *) end_local_plt;
12164 s = ppc64_elf_tdata (ibfd)->got;
12165 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12166 {
12167 struct got_entry *ent;
12168
12169 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12170 {
12171 unsigned int ent_size = 8;
12172 unsigned int rel_size = sizeof (Elf64_External_Rela);
12173
12174 ent->got.offset = s->size;
12175 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12176 {
12177 ent_size *= 2;
12178 rel_size *= 2;
12179 }
12180 s->size += ent_size;
12181 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12182 {
12183 htab->elf.irelplt->size += rel_size;
12184 htab->got_reli_size += rel_size;
12185 }
12186 else if (bfd_link_pic (info)
12187 && !((ent->tls_type & TLS_TPREL) != 0
12188 && bfd_link_executable (info)))
12189 {
12190 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12191 srel->size += rel_size;
12192 }
12193 }
12194 }
12195 }
12196
12197 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12198
12199 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12200 {
12201 struct got_entry *ent;
12202
12203 if (!is_ppc64_elf (ibfd))
12204 continue;
12205
12206 ent = ppc64_tlsld_got (ibfd);
12207 if (!ent->is_indirect
12208 && ent->got.offset != (bfd_vma) -1)
12209 {
12210 asection *s = ppc64_elf_tdata (ibfd)->got;
12211 ent->got.offset = s->size;
12212 s->size += 16;
12213 if (bfd_link_pic (info))
12214 {
12215 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12216 srel->size += sizeof (Elf64_External_Rela);
12217 }
12218 }
12219 }
12220
12221 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12222 if (!done_something)
12223 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12224 {
12225 asection *got;
12226
12227 if (!is_ppc64_elf (ibfd))
12228 continue;
12229
12230 got = ppc64_elf_tdata (ibfd)->got;
12231 if (got != NULL)
12232 {
12233 done_something = got->rawsize != got->size;
12234 if (done_something)
12235 break;
12236 }
12237 }
12238
12239 if (done_something)
12240 (*htab->params->layout_sections_again) ();
12241
12242 /* Set up for second pass over toc sections to recalculate elf_gp
12243 on input sections. */
12244 htab->toc_bfd = NULL;
12245 htab->toc_first_sec = NULL;
12246 htab->second_toc_pass = TRUE;
12247 return done_something;
12248 }
12249
12250 /* Called after second pass of multitoc partitioning. */
12251
12252 void
12253 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12254 {
12255 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12256
12257 /* After the second pass, toc_curr tracks the TOC offset used
12258 for code sections below in ppc64_elf_next_input_section. */
12259 htab->toc_curr = TOC_BASE_OFF;
12260 }
12261
12262 /* No toc references were found in ISEC. If the code in ISEC makes no
12263 calls, then there's no need to use toc adjusting stubs when branching
12264 into ISEC. Actually, indirect calls from ISEC are OK as they will
12265 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12266 needed, and 2 if a cyclical call-graph was found but no other reason
12267 for a stub was detected. If called from the top level, a return of
12268 2 means the same as a return of 0. */
12269
12270 static int
12271 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12272 {
12273 int ret;
12274
12275 /* Mark this section as checked. */
12276 isec->call_check_done = 1;
12277
12278 /* We know none of our code bearing sections will need toc stubs. */
12279 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12280 return 0;
12281
12282 if (isec->size == 0)
12283 return 0;
12284
12285 if (isec->output_section == NULL)
12286 return 0;
12287
12288 ret = 0;
12289 if (isec->reloc_count != 0)
12290 {
12291 Elf_Internal_Rela *relstart, *rel;
12292 Elf_Internal_Sym *local_syms;
12293 struct ppc_link_hash_table *htab;
12294
12295 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12296 info->keep_memory);
12297 if (relstart == NULL)
12298 return -1;
12299
12300 /* Look for branches to outside of this section. */
12301 local_syms = NULL;
12302 htab = ppc_hash_table (info);
12303 if (htab == NULL)
12304 return -1;
12305
12306 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12307 {
12308 enum elf_ppc64_reloc_type r_type;
12309 unsigned long r_symndx;
12310 struct elf_link_hash_entry *h;
12311 struct ppc_link_hash_entry *eh;
12312 Elf_Internal_Sym *sym;
12313 asection *sym_sec;
12314 struct _opd_sec_data *opd;
12315 bfd_vma sym_value;
12316 bfd_vma dest;
12317
12318 r_type = ELF64_R_TYPE (rel->r_info);
12319 if (r_type != R_PPC64_REL24
12320 && r_type != R_PPC64_REL24_NOTOC
12321 && r_type != R_PPC64_REL14
12322 && r_type != R_PPC64_REL14_BRTAKEN
12323 && r_type != R_PPC64_REL14_BRNTAKEN
12324 && r_type != R_PPC64_PLTCALL
12325 && r_type != R_PPC64_PLTCALL_NOTOC)
12326 continue;
12327
12328 r_symndx = ELF64_R_SYM (rel->r_info);
12329 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12330 isec->owner))
12331 {
12332 ret = -1;
12333 break;
12334 }
12335
12336 /* Calls to dynamic lib functions go through a plt call stub
12337 that uses r2. */
12338 eh = (struct ppc_link_hash_entry *) h;
12339 if (eh != NULL
12340 && (eh->elf.plt.plist != NULL
12341 || (eh->oh != NULL
12342 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12343 {
12344 ret = 1;
12345 break;
12346 }
12347
12348 if (sym_sec == NULL)
12349 /* Ignore other undefined symbols. */
12350 continue;
12351
12352 /* Assume branches to other sections not included in the
12353 link need stubs too, to cover -R and absolute syms. */
12354 if (sym_sec->output_section == NULL)
12355 {
12356 ret = 1;
12357 break;
12358 }
12359
12360 if (h == NULL)
12361 sym_value = sym->st_value;
12362 else
12363 {
12364 if (h->root.type != bfd_link_hash_defined
12365 && h->root.type != bfd_link_hash_defweak)
12366 abort ();
12367 sym_value = h->root.u.def.value;
12368 }
12369 sym_value += rel->r_addend;
12370
12371 /* If this branch reloc uses an opd sym, find the code section. */
12372 opd = get_opd_info (sym_sec);
12373 if (opd != NULL)
12374 {
12375 if (h == NULL && opd->adjust != NULL)
12376 {
12377 long adjust;
12378
12379 adjust = opd->adjust[OPD_NDX (sym_value)];
12380 if (adjust == -1)
12381 /* Assume deleted functions won't ever be called. */
12382 continue;
12383 sym_value += adjust;
12384 }
12385
12386 dest = opd_entry_value (sym_sec, sym_value,
12387 &sym_sec, NULL, FALSE);
12388 if (dest == (bfd_vma) -1)
12389 continue;
12390 }
12391 else
12392 dest = (sym_value
12393 + sym_sec->output_offset
12394 + sym_sec->output_section->vma);
12395
12396 /* Ignore branch to self. */
12397 if (sym_sec == isec)
12398 continue;
12399
12400 /* If the called function uses the toc, we need a stub. */
12401 if (sym_sec->has_toc_reloc
12402 || sym_sec->makes_toc_func_call)
12403 {
12404 ret = 1;
12405 break;
12406 }
12407
12408 /* Assume any branch that needs a long branch stub might in fact
12409 need a plt_branch stub. A plt_branch stub uses r2. */
12410 else if (dest - (isec->output_offset
12411 + isec->output_section->vma
12412 + rel->r_offset) + (1 << 25)
12413 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12414 ? h->other
12415 : sym->st_other))
12416 {
12417 ret = 1;
12418 break;
12419 }
12420
12421 /* If calling back to a section in the process of being
12422 tested, we can't say for sure that no toc adjusting stubs
12423 are needed, so don't return zero. */
12424 else if (sym_sec->call_check_in_progress)
12425 ret = 2;
12426
12427 /* Branches to another section that itself doesn't have any TOC
12428 references are OK. Recursively call ourselves to check. */
12429 else if (!sym_sec->call_check_done)
12430 {
12431 int recur;
12432
12433 /* Mark current section as indeterminate, so that other
12434 sections that call back to current won't be marked as
12435 known. */
12436 isec->call_check_in_progress = 1;
12437 recur = toc_adjusting_stub_needed (info, sym_sec);
12438 isec->call_check_in_progress = 0;
12439
12440 if (recur != 0)
12441 {
12442 ret = recur;
12443 if (recur != 2)
12444 break;
12445 }
12446 }
12447 }
12448
12449 if (local_syms != NULL
12450 && (elf_symtab_hdr (isec->owner).contents
12451 != (unsigned char *) local_syms))
12452 free (local_syms);
12453 if (elf_section_data (isec)->relocs != relstart)
12454 free (relstart);
12455 }
12456
12457 if ((ret & 1) == 0
12458 && isec->map_head.s != NULL
12459 && (strcmp (isec->output_section->name, ".init") == 0
12460 || strcmp (isec->output_section->name, ".fini") == 0))
12461 {
12462 if (isec->map_head.s->has_toc_reloc
12463 || isec->map_head.s->makes_toc_func_call)
12464 ret = 1;
12465 else if (!isec->map_head.s->call_check_done)
12466 {
12467 int recur;
12468 isec->call_check_in_progress = 1;
12469 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12470 isec->call_check_in_progress = 0;
12471 if (recur != 0)
12472 ret = recur;
12473 }
12474 }
12475
12476 if (ret == 1)
12477 isec->makes_toc_func_call = 1;
12478
12479 return ret;
12480 }
12481
12482 /* The linker repeatedly calls this function for each input section,
12483 in the order that input sections are linked into output sections.
12484 Build lists of input sections to determine groupings between which
12485 we may insert linker stubs. */
12486
12487 bfd_boolean
12488 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12489 {
12490 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12491
12492 if (htab == NULL)
12493 return FALSE;
12494
12495 if ((isec->output_section->flags & SEC_CODE) != 0
12496 && isec->output_section->id < htab->sec_info_arr_size)
12497 {
12498 /* This happens to make the list in reverse order,
12499 which is what we want. */
12500 htab->sec_info[isec->id].u.list
12501 = htab->sec_info[isec->output_section->id].u.list;
12502 htab->sec_info[isec->output_section->id].u.list = isec;
12503 }
12504
12505 if (htab->multi_toc_needed)
12506 {
12507 /* Analyse sections that aren't already flagged as needing a
12508 valid toc pointer. Exclude .fixup for the linux kernel.
12509 .fixup contains branches, but only back to the function that
12510 hit an exception. */
12511 if (!(isec->has_toc_reloc
12512 || (isec->flags & SEC_CODE) == 0
12513 || strcmp (isec->name, ".fixup") == 0
12514 || isec->call_check_done))
12515 {
12516 if (toc_adjusting_stub_needed (info, isec) < 0)
12517 return FALSE;
12518 }
12519 /* Make all sections use the TOC assigned for this object file.
12520 This will be wrong for pasted sections; We fix that in
12521 check_pasted_section(). */
12522 if (elf_gp (isec->owner) != 0)
12523 htab->toc_curr = elf_gp (isec->owner);
12524 }
12525
12526 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12527 return TRUE;
12528 }
12529
12530 /* Check that all .init and .fini sections use the same toc, if they
12531 have toc relocs. */
12532
12533 static bfd_boolean
12534 check_pasted_section (struct bfd_link_info *info, const char *name)
12535 {
12536 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12537
12538 if (o != NULL)
12539 {
12540 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12541 bfd_vma toc_off = 0;
12542 asection *i;
12543
12544 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12545 if (i->has_toc_reloc)
12546 {
12547 if (toc_off == 0)
12548 toc_off = htab->sec_info[i->id].toc_off;
12549 else if (toc_off != htab->sec_info[i->id].toc_off)
12550 return FALSE;
12551 }
12552
12553 if (toc_off == 0)
12554 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12555 if (i->makes_toc_func_call)
12556 {
12557 toc_off = htab->sec_info[i->id].toc_off;
12558 break;
12559 }
12560
12561 /* Make sure the whole pasted function uses the same toc offset. */
12562 if (toc_off != 0)
12563 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12564 htab->sec_info[i->id].toc_off = toc_off;
12565 }
12566 return TRUE;
12567 }
12568
12569 bfd_boolean
12570 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12571 {
12572 return (check_pasted_section (info, ".init")
12573 & check_pasted_section (info, ".fini"));
12574 }
12575
12576 /* See whether we can group stub sections together. Grouping stub
12577 sections may result in fewer stubs. More importantly, we need to
12578 put all .init* and .fini* stubs at the beginning of the .init or
12579 .fini output sections respectively, because glibc splits the
12580 _init and _fini functions into multiple parts. Putting a stub in
12581 the middle of a function is not a good idea. */
12582
12583 static bfd_boolean
12584 group_sections (struct bfd_link_info *info,
12585 bfd_size_type stub_group_size,
12586 bfd_boolean stubs_always_before_branch)
12587 {
12588 struct ppc_link_hash_table *htab;
12589 asection *osec;
12590 bfd_boolean suppress_size_errors;
12591
12592 htab = ppc_hash_table (info);
12593 if (htab == NULL)
12594 return FALSE;
12595
12596 suppress_size_errors = FALSE;
12597 if (stub_group_size == 1)
12598 {
12599 /* Default values. */
12600 if (stubs_always_before_branch)
12601 stub_group_size = 0x1e00000;
12602 else
12603 stub_group_size = 0x1c00000;
12604 suppress_size_errors = TRUE;
12605 }
12606
12607 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12608 {
12609 asection *tail;
12610
12611 if (osec->id >= htab->sec_info_arr_size)
12612 continue;
12613
12614 tail = htab->sec_info[osec->id].u.list;
12615 while (tail != NULL)
12616 {
12617 asection *curr;
12618 asection *prev;
12619 bfd_size_type total;
12620 bfd_boolean big_sec;
12621 bfd_vma curr_toc;
12622 struct map_stub *group;
12623 bfd_size_type group_size;
12624
12625 curr = tail;
12626 total = tail->size;
12627 group_size = (ppc64_elf_section_data (tail) != NULL
12628 && ppc64_elf_section_data (tail)->has_14bit_branch
12629 ? stub_group_size >> 10 : stub_group_size);
12630
12631 big_sec = total > group_size;
12632 if (big_sec && !suppress_size_errors)
12633 /* xgettext:c-format */
12634 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12635 tail->owner, tail);
12636 curr_toc = htab->sec_info[tail->id].toc_off;
12637
12638 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12639 && ((total += curr->output_offset - prev->output_offset)
12640 < (ppc64_elf_section_data (prev) != NULL
12641 && ppc64_elf_section_data (prev)->has_14bit_branch
12642 ? (group_size = stub_group_size >> 10) : group_size))
12643 && htab->sec_info[prev->id].toc_off == curr_toc)
12644 curr = prev;
12645
12646 /* OK, the size from the start of CURR to the end is less
12647 than group_size and thus can be handled by one stub
12648 section. (or the tail section is itself larger than
12649 group_size, in which case we may be toast.) We should
12650 really be keeping track of the total size of stubs added
12651 here, as stubs contribute to the final output section
12652 size. That's a little tricky, and this way will only
12653 break if stubs added make the total size more than 2^25,
12654 ie. for the default stub_group_size, if stubs total more
12655 than 2097152 bytes, or nearly 75000 plt call stubs. */
12656 group = bfd_alloc (curr->owner, sizeof (*group));
12657 if (group == NULL)
12658 return FALSE;
12659 group->link_sec = curr;
12660 group->stub_sec = NULL;
12661 group->needs_save_res = 0;
12662 group->lr_restore = 0;
12663 group->eh_size = 0;
12664 group->eh_base = 0;
12665 group->next = htab->group;
12666 htab->group = group;
12667 do
12668 {
12669 prev = htab->sec_info[tail->id].u.list;
12670 /* Set up this stub group. */
12671 htab->sec_info[tail->id].u.group = group;
12672 }
12673 while (tail != curr && (tail = prev) != NULL);
12674
12675 /* But wait, there's more! Input sections up to group_size
12676 bytes before the stub section can be handled by it too.
12677 Don't do this if we have a really large section after the
12678 stubs, as adding more stubs increases the chance that
12679 branches may not reach into the stub section. */
12680 if (!stubs_always_before_branch && !big_sec)
12681 {
12682 total = 0;
12683 while (prev != NULL
12684 && ((total += tail->output_offset - prev->output_offset)
12685 < (ppc64_elf_section_data (prev) != NULL
12686 && ppc64_elf_section_data (prev)->has_14bit_branch
12687 ? (group_size = stub_group_size >> 10)
12688 : group_size))
12689 && htab->sec_info[prev->id].toc_off == curr_toc)
12690 {
12691 tail = prev;
12692 prev = htab->sec_info[tail->id].u.list;
12693 htab->sec_info[tail->id].u.group = group;
12694 }
12695 }
12696 tail = prev;
12697 }
12698 }
12699 return TRUE;
12700 }
12701
12702 static const unsigned char glink_eh_frame_cie[] =
12703 {
12704 0, 0, 0, 16, /* length. */
12705 0, 0, 0, 0, /* id. */
12706 1, /* CIE version. */
12707 'z', 'R', 0, /* Augmentation string. */
12708 4, /* Code alignment. */
12709 0x78, /* Data alignment. */
12710 65, /* RA reg. */
12711 1, /* Augmentation size. */
12712 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12713 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12714 };
12715
12716 /* Stripping output sections is normally done before dynamic section
12717 symbols have been allocated. This function is called later, and
12718 handles cases like htab->brlt which is mapped to its own output
12719 section. */
12720
12721 static void
12722 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12723 {
12724 if (isec->size == 0
12725 && isec->output_section->size == 0
12726 && !(isec->output_section->flags & SEC_KEEP)
12727 && !bfd_section_removed_from_list (info->output_bfd,
12728 isec->output_section)
12729 && elf_section_data (isec->output_section)->dynindx == 0)
12730 {
12731 isec->output_section->flags |= SEC_EXCLUDE;
12732 bfd_section_list_remove (info->output_bfd, isec->output_section);
12733 info->output_bfd->section_count--;
12734 }
12735 }
12736
12737 /* Determine and set the size of the stub section for a final link.
12738
12739 The basic idea here is to examine all the relocations looking for
12740 PC-relative calls to a target that is unreachable with a "bl"
12741 instruction. */
12742
12743 bfd_boolean
12744 ppc64_elf_size_stubs (struct bfd_link_info *info)
12745 {
12746 bfd_size_type stub_group_size;
12747 bfd_boolean stubs_always_before_branch;
12748 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12749
12750 if (htab == NULL)
12751 return FALSE;
12752
12753 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12754 htab->params->plt_thread_safe = 1;
12755 if (!htab->opd_abi)
12756 htab->params->plt_thread_safe = 0;
12757 else if (htab->params->plt_thread_safe == -1)
12758 {
12759 static const char *const thread_starter[] =
12760 {
12761 "pthread_create",
12762 /* libstdc++ */
12763 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12764 /* librt */
12765 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12766 "mq_notify", "create_timer",
12767 /* libanl */
12768 "getaddrinfo_a",
12769 /* libgomp */
12770 "GOMP_parallel",
12771 "GOMP_parallel_start",
12772 "GOMP_parallel_loop_static",
12773 "GOMP_parallel_loop_static_start",
12774 "GOMP_parallel_loop_dynamic",
12775 "GOMP_parallel_loop_dynamic_start",
12776 "GOMP_parallel_loop_guided",
12777 "GOMP_parallel_loop_guided_start",
12778 "GOMP_parallel_loop_runtime",
12779 "GOMP_parallel_loop_runtime_start",
12780 "GOMP_parallel_sections",
12781 "GOMP_parallel_sections_start",
12782 /* libgo */
12783 "__go_go",
12784 };
12785 unsigned i;
12786
12787 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12788 {
12789 struct elf_link_hash_entry *h;
12790 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12791 FALSE, FALSE, TRUE);
12792 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12793 if (htab->params->plt_thread_safe)
12794 break;
12795 }
12796 }
12797 stubs_always_before_branch = htab->params->group_size < 0;
12798 if (htab->params->group_size < 0)
12799 stub_group_size = -htab->params->group_size;
12800 else
12801 stub_group_size = htab->params->group_size;
12802
12803 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12804 return FALSE;
12805
12806 #define STUB_SHRINK_ITER 20
12807 /* Loop until no stubs added. After iteration 20 of this loop we may
12808 exit on a stub section shrinking. This is to break out of a
12809 pathological case where adding stubs on one iteration decreases
12810 section gaps (perhaps due to alignment), which then requires
12811 fewer or smaller stubs on the next iteration. */
12812
12813 while (1)
12814 {
12815 bfd *input_bfd;
12816 unsigned int bfd_indx;
12817 struct map_stub *group;
12818
12819 htab->stub_iteration += 1;
12820
12821 for (input_bfd = info->input_bfds, bfd_indx = 0;
12822 input_bfd != NULL;
12823 input_bfd = input_bfd->link.next, bfd_indx++)
12824 {
12825 Elf_Internal_Shdr *symtab_hdr;
12826 asection *section;
12827 Elf_Internal_Sym *local_syms = NULL;
12828
12829 if (!is_ppc64_elf (input_bfd))
12830 continue;
12831
12832 /* We'll need the symbol table in a second. */
12833 symtab_hdr = &elf_symtab_hdr (input_bfd);
12834 if (symtab_hdr->sh_info == 0)
12835 continue;
12836
12837 /* Walk over each section attached to the input bfd. */
12838 for (section = input_bfd->sections;
12839 section != NULL;
12840 section = section->next)
12841 {
12842 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12843
12844 /* If there aren't any relocs, then there's nothing more
12845 to do. */
12846 if ((section->flags & SEC_RELOC) == 0
12847 || (section->flags & SEC_ALLOC) == 0
12848 || (section->flags & SEC_LOAD) == 0
12849 || (section->flags & SEC_CODE) == 0
12850 || section->reloc_count == 0)
12851 continue;
12852
12853 /* If this section is a link-once section that will be
12854 discarded, then don't create any stubs. */
12855 if (section->output_section == NULL
12856 || section->output_section->owner != info->output_bfd)
12857 continue;
12858
12859 /* Get the relocs. */
12860 internal_relocs
12861 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12862 info->keep_memory);
12863 if (internal_relocs == NULL)
12864 goto error_ret_free_local;
12865
12866 /* Now examine each relocation. */
12867 irela = internal_relocs;
12868 irelaend = irela + section->reloc_count;
12869 for (; irela < irelaend; irela++)
12870 {
12871 enum elf_ppc64_reloc_type r_type;
12872 unsigned int r_indx;
12873 enum ppc_stub_type stub_type;
12874 struct ppc_stub_hash_entry *stub_entry;
12875 asection *sym_sec, *code_sec;
12876 bfd_vma sym_value, code_value;
12877 bfd_vma destination;
12878 unsigned long local_off;
12879 bfd_boolean ok_dest;
12880 struct ppc_link_hash_entry *hash;
12881 struct ppc_link_hash_entry *fdh;
12882 struct elf_link_hash_entry *h;
12883 Elf_Internal_Sym *sym;
12884 char *stub_name;
12885 const asection *id_sec;
12886 struct _opd_sec_data *opd;
12887 struct plt_entry *plt_ent;
12888
12889 r_type = ELF64_R_TYPE (irela->r_info);
12890 r_indx = ELF64_R_SYM (irela->r_info);
12891
12892 if (r_type >= R_PPC64_max)
12893 {
12894 bfd_set_error (bfd_error_bad_value);
12895 goto error_ret_free_internal;
12896 }
12897
12898 /* Only look for stubs on branch instructions. */
12899 if (r_type != R_PPC64_REL24
12900 && r_type != R_PPC64_REL24_NOTOC
12901 && r_type != R_PPC64_REL14
12902 && r_type != R_PPC64_REL14_BRTAKEN
12903 && r_type != R_PPC64_REL14_BRNTAKEN)
12904 continue;
12905
12906 /* Now determine the call target, its name, value,
12907 section. */
12908 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12909 r_indx, input_bfd))
12910 goto error_ret_free_internal;
12911 hash = (struct ppc_link_hash_entry *) h;
12912
12913 ok_dest = FALSE;
12914 fdh = NULL;
12915 sym_value = 0;
12916 if (hash == NULL)
12917 {
12918 sym_value = sym->st_value;
12919 if (sym_sec != NULL
12920 && sym_sec->output_section != NULL)
12921 ok_dest = TRUE;
12922 }
12923 else if (hash->elf.root.type == bfd_link_hash_defined
12924 || hash->elf.root.type == bfd_link_hash_defweak)
12925 {
12926 sym_value = hash->elf.root.u.def.value;
12927 if (sym_sec->output_section != NULL)
12928 ok_dest = TRUE;
12929 }
12930 else if (hash->elf.root.type == bfd_link_hash_undefweak
12931 || hash->elf.root.type == bfd_link_hash_undefined)
12932 {
12933 /* Recognise an old ABI func code entry sym, and
12934 use the func descriptor sym instead if it is
12935 defined. */
12936 if (hash->elf.root.root.string[0] == '.'
12937 && hash->oh != NULL)
12938 {
12939 fdh = ppc_follow_link (hash->oh);
12940 if (fdh->elf.root.type == bfd_link_hash_defined
12941 || fdh->elf.root.type == bfd_link_hash_defweak)
12942 {
12943 sym_sec = fdh->elf.root.u.def.section;
12944 sym_value = fdh->elf.root.u.def.value;
12945 if (sym_sec->output_section != NULL)
12946 ok_dest = TRUE;
12947 }
12948 else
12949 fdh = NULL;
12950 }
12951 }
12952 else
12953 {
12954 bfd_set_error (bfd_error_bad_value);
12955 goto error_ret_free_internal;
12956 }
12957
12958 destination = 0;
12959 local_off = 0;
12960 if (ok_dest)
12961 {
12962 sym_value += irela->r_addend;
12963 destination = (sym_value
12964 + sym_sec->output_offset
12965 + sym_sec->output_section->vma);
12966 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12967 ? hash->elf.other
12968 : sym->st_other);
12969 }
12970
12971 code_sec = sym_sec;
12972 code_value = sym_value;
12973 opd = get_opd_info (sym_sec);
12974 if (opd != NULL)
12975 {
12976 bfd_vma dest;
12977
12978 if (hash == NULL && opd->adjust != NULL)
12979 {
12980 long adjust = opd->adjust[OPD_NDX (sym_value)];
12981 if (adjust == -1)
12982 continue;
12983 code_value += adjust;
12984 sym_value += adjust;
12985 }
12986 dest = opd_entry_value (sym_sec, sym_value,
12987 &code_sec, &code_value, FALSE);
12988 if (dest != (bfd_vma) -1)
12989 {
12990 destination = dest;
12991 if (fdh != NULL)
12992 {
12993 /* Fixup old ABI sym to point at code
12994 entry. */
12995 hash->elf.root.type = bfd_link_hash_defweak;
12996 hash->elf.root.u.def.section = code_sec;
12997 hash->elf.root.u.def.value = code_value;
12998 }
12999 }
13000 }
13001
13002 /* Determine what (if any) linker stub is needed. */
13003 plt_ent = NULL;
13004 stub_type = ppc_type_of_stub (section, irela, &hash,
13005 &plt_ent, destination,
13006 local_off);
13007
13008 if (r_type == R_PPC64_REL24_NOTOC)
13009 {
13010 if (stub_type == ppc_stub_plt_call)
13011 stub_type = ppc_stub_plt_call_notoc;
13012 else if (stub_type == ppc_stub_long_branch
13013 || (code_sec != NULL
13014 && code_sec->output_section != NULL
13015 && (((hash ? hash->elf.other : sym->st_other)
13016 & STO_PPC64_LOCAL_MASK)
13017 > 1 << STO_PPC64_LOCAL_BIT)))
13018 stub_type = ppc_stub_long_branch_notoc;
13019 }
13020 else if (stub_type != ppc_stub_plt_call)
13021 {
13022 /* Check whether we need a TOC adjusting stub.
13023 Since the linker pastes together pieces from
13024 different object files when creating the
13025 _init and _fini functions, it may be that a
13026 call to what looks like a local sym is in
13027 fact a call needing a TOC adjustment. */
13028 if ((code_sec != NULL
13029 && code_sec->output_section != NULL
13030 && (htab->sec_info[code_sec->id].toc_off
13031 != htab->sec_info[section->id].toc_off)
13032 && (code_sec->has_toc_reloc
13033 || code_sec->makes_toc_func_call))
13034 || (((hash ? hash->elf.other : sym->st_other)
13035 & STO_PPC64_LOCAL_MASK)
13036 == 1 << STO_PPC64_LOCAL_BIT))
13037 stub_type = ppc_stub_long_branch_r2off;
13038 }
13039
13040 if (stub_type == ppc_stub_none)
13041 continue;
13042
13043 /* __tls_get_addr calls might be eliminated. */
13044 if (stub_type != ppc_stub_plt_call
13045 && stub_type != ppc_stub_plt_call_notoc
13046 && hash != NULL
13047 && (hash == htab->tls_get_addr
13048 || hash == htab->tls_get_addr_fd)
13049 && section->has_tls_reloc
13050 && irela != internal_relocs)
13051 {
13052 /* Get tls info. */
13053 unsigned char *tls_mask;
13054
13055 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
13056 irela - 1, input_bfd))
13057 goto error_ret_free_internal;
13058 if ((*tls_mask & TLS_TLS) != 0)
13059 continue;
13060 }
13061
13062 if (stub_type == ppc_stub_plt_call)
13063 {
13064 if (!htab->opd_abi
13065 && htab->params->plt_localentry0 != 0
13066 && is_elfv2_localentry0 (&hash->elf))
13067 htab->has_plt_localentry0 = 1;
13068 else if (irela + 1 < irelaend
13069 && irela[1].r_offset == irela->r_offset + 4
13070 && (ELF64_R_TYPE (irela[1].r_info)
13071 == R_PPC64_TOCSAVE))
13072 {
13073 if (!tocsave_find (htab, INSERT,
13074 &local_syms, irela + 1, input_bfd))
13075 goto error_ret_free_internal;
13076 }
13077 else
13078 stub_type = ppc_stub_plt_call_r2save;
13079 }
13080
13081 /* Support for grouping stub sections. */
13082 id_sec = htab->sec_info[section->id].u.group->link_sec;
13083
13084 /* Get the name of this stub. */
13085 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13086 if (!stub_name)
13087 goto error_ret_free_internal;
13088
13089 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
13090 stub_name, FALSE, FALSE);
13091 if (stub_entry != NULL)
13092 {
13093 enum ppc_stub_type old_type;
13094 /* A stub has already been created, but it may
13095 not be the required type. We shouldn't be
13096 transitioning from plt_call to long_branch
13097 stubs or vice versa, but we might be
13098 upgrading from plt_call to plt_call_r2save or
13099 from long_branch to long_branch_r2off. */
13100 free (stub_name);
13101 old_type = stub_entry->stub_type;
13102 switch (old_type)
13103 {
13104 default:
13105 abort ();
13106
13107 case ppc_stub_save_res:
13108 continue;
13109
13110 case ppc_stub_plt_call:
13111 case ppc_stub_plt_call_r2save:
13112 case ppc_stub_plt_call_notoc:
13113 case ppc_stub_plt_call_both:
13114 if (stub_type == ppc_stub_plt_call)
13115 continue;
13116 else if (stub_type == ppc_stub_plt_call_r2save)
13117 {
13118 if (old_type == ppc_stub_plt_call_notoc)
13119 stub_type = ppc_stub_plt_call_both;
13120 }
13121 else if (stub_type == ppc_stub_plt_call_notoc)
13122 {
13123 if (old_type == ppc_stub_plt_call_r2save)
13124 stub_type = ppc_stub_plt_call_both;
13125 }
13126 else
13127 abort ();
13128 break;
13129
13130 case ppc_stub_plt_branch:
13131 case ppc_stub_plt_branch_r2off:
13132 case ppc_stub_plt_branch_notoc:
13133 case ppc_stub_plt_branch_both:
13134 old_type += (ppc_stub_long_branch
13135 - ppc_stub_plt_branch);
13136 /* Fall through. */
13137 case ppc_stub_long_branch:
13138 case ppc_stub_long_branch_r2off:
13139 case ppc_stub_long_branch_notoc:
13140 case ppc_stub_long_branch_both:
13141 if (stub_type == ppc_stub_long_branch)
13142 continue;
13143 else if (stub_type == ppc_stub_long_branch_r2off)
13144 {
13145 if (old_type == ppc_stub_long_branch_notoc)
13146 stub_type = ppc_stub_long_branch_both;
13147 }
13148 else if (stub_type == ppc_stub_long_branch_notoc)
13149 {
13150 if (old_type == ppc_stub_long_branch_r2off)
13151 stub_type = ppc_stub_long_branch_both;
13152 }
13153 else
13154 abort ();
13155 break;
13156 }
13157 if (old_type < stub_type)
13158 stub_entry->stub_type = stub_type;
13159 continue;
13160 }
13161
13162 stub_entry = ppc_add_stub (stub_name, section, info);
13163 if (stub_entry == NULL)
13164 {
13165 free (stub_name);
13166 error_ret_free_internal:
13167 if (elf_section_data (section)->relocs == NULL)
13168 free (internal_relocs);
13169 error_ret_free_local:
13170 if (local_syms != NULL
13171 && (symtab_hdr->contents
13172 != (unsigned char *) local_syms))
13173 free (local_syms);
13174 return FALSE;
13175 }
13176
13177 stub_entry->stub_type = stub_type;
13178 if (stub_type >= ppc_stub_plt_call
13179 && stub_type <= ppc_stub_plt_call_both)
13180 {
13181 stub_entry->target_value = sym_value;
13182 stub_entry->target_section = sym_sec;
13183 }
13184 else
13185 {
13186 stub_entry->target_value = code_value;
13187 stub_entry->target_section = code_sec;
13188 }
13189 stub_entry->h = hash;
13190 stub_entry->plt_ent = plt_ent;
13191 stub_entry->symtype
13192 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
13193 stub_entry->other = hash ? hash->elf.other : sym->st_other;
13194
13195 if (hash != NULL
13196 && (hash->elf.root.type == bfd_link_hash_defined
13197 || hash->elf.root.type == bfd_link_hash_defweak))
13198 htab->stub_globals += 1;
13199 }
13200
13201 /* We're done with the internal relocs, free them. */
13202 if (elf_section_data (section)->relocs != internal_relocs)
13203 free (internal_relocs);
13204 }
13205
13206 if (local_syms != NULL
13207 && symtab_hdr->contents != (unsigned char *) local_syms)
13208 {
13209 if (!info->keep_memory)
13210 free (local_syms);
13211 else
13212 symtab_hdr->contents = (unsigned char *) local_syms;
13213 }
13214 }
13215
13216 /* We may have added some stubs. Find out the new size of the
13217 stub sections. */
13218 for (group = htab->group; group != NULL; group = group->next)
13219 {
13220 group->lr_restore = 0;
13221 group->eh_size = 0;
13222 if (group->stub_sec != NULL)
13223 {
13224 asection *stub_sec = group->stub_sec;
13225
13226 if (htab->stub_iteration <= STUB_SHRINK_ITER
13227 || stub_sec->rawsize < stub_sec->size)
13228 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13229 stub_sec->rawsize = stub_sec->size;
13230 stub_sec->size = 0;
13231 stub_sec->reloc_count = 0;
13232 stub_sec->flags &= ~SEC_RELOC;
13233 }
13234 }
13235
13236 if (htab->stub_iteration <= STUB_SHRINK_ITER
13237 || htab->brlt->rawsize < htab->brlt->size)
13238 htab->brlt->rawsize = htab->brlt->size;
13239 htab->brlt->size = 0;
13240 htab->brlt->reloc_count = 0;
13241 htab->brlt->flags &= ~SEC_RELOC;
13242 if (htab->relbrlt != NULL)
13243 htab->relbrlt->size = 0;
13244
13245 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
13246
13247 for (group = htab->group; group != NULL; group = group->next)
13248 if (group->needs_save_res)
13249 group->stub_sec->size += htab->sfpr->size;
13250
13251 if (info->emitrelocations
13252 && htab->glink != NULL && htab->glink->size != 0)
13253 {
13254 htab->glink->reloc_count = 1;
13255 htab->glink->flags |= SEC_RELOC;
13256 }
13257
13258 if (htab->glink_eh_frame != NULL
13259 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13260 && htab->glink_eh_frame->output_section->size > 8)
13261 {
13262 size_t size = 0, align = 4;
13263
13264 for (group = htab->group; group != NULL; group = group->next)
13265 if (group->eh_size != 0)
13266 size += (group->eh_size + 17 + align - 1) & -align;
13267 if (htab->glink != NULL && htab->glink->size != 0)
13268 size += (24 + align - 1) & -align;
13269 if (size != 0)
13270 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13271 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13272 size = (size + align - 1) & -align;
13273 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13274 htab->glink_eh_frame->size = size;
13275 }
13276
13277 if (htab->params->plt_stub_align != 0)
13278 for (group = htab->group; group != NULL; group = group->next)
13279 if (group->stub_sec != NULL)
13280 {
13281 int align = abs (htab->params->plt_stub_align);
13282 group->stub_sec->size
13283 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13284 }
13285
13286 for (group = htab->group; group != NULL; group = group->next)
13287 if (group->stub_sec != NULL
13288 && group->stub_sec->rawsize != group->stub_sec->size
13289 && (htab->stub_iteration <= STUB_SHRINK_ITER
13290 || group->stub_sec->rawsize < group->stub_sec->size))
13291 break;
13292
13293 if (group == NULL
13294 && (htab->brlt->rawsize == htab->brlt->size
13295 || (htab->stub_iteration > STUB_SHRINK_ITER
13296 && htab->brlt->rawsize > htab->brlt->size))
13297 && (htab->glink_eh_frame == NULL
13298 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
13299 break;
13300
13301 /* Ask the linker to do its stuff. */
13302 (*htab->params->layout_sections_again) ();
13303 }
13304
13305 if (htab->glink_eh_frame != NULL
13306 && htab->glink_eh_frame->size != 0)
13307 {
13308 bfd_vma val;
13309 bfd_byte *p, *last_fde;
13310 size_t last_fde_len, size, align, pad;
13311 struct map_stub *group;
13312
13313 /* It is necessary to at least have a rough outline of the
13314 linker generated CIEs and FDEs written before
13315 bfd_elf_discard_info is run, in order for these FDEs to be
13316 indexed in .eh_frame_hdr. */
13317 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13318 if (p == NULL)
13319 return FALSE;
13320 htab->glink_eh_frame->contents = p;
13321 last_fde = p;
13322 align = 4;
13323
13324 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13325 /* CIE length (rewrite in case little-endian). */
13326 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13327 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13328 p += last_fde_len + 4;
13329
13330 for (group = htab->group; group != NULL; group = group->next)
13331 if (group->eh_size != 0)
13332 {
13333 group->eh_base = p - htab->glink_eh_frame->contents;
13334 last_fde = p;
13335 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
13336 /* FDE length. */
13337 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13338 p += 4;
13339 /* CIE pointer. */
13340 val = p - htab->glink_eh_frame->contents;
13341 bfd_put_32 (htab->elf.dynobj, val, p);
13342 p += 4;
13343 /* Offset to stub section, written later. */
13344 p += 4;
13345 /* stub section size. */
13346 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13347 p += 4;
13348 /* Augmentation. */
13349 p += 1;
13350 /* Make sure we don't have all nops. This is enough for
13351 elf-eh-frame.c to detect the last non-nop opcode. */
13352 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
13353 p = last_fde + last_fde_len + 4;
13354 }
13355 if (htab->glink != NULL && htab->glink->size != 0)
13356 {
13357 last_fde = p;
13358 last_fde_len = ((24 + align - 1) & -align) - 4;
13359 /* FDE length. */
13360 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13361 p += 4;
13362 /* CIE pointer. */
13363 val = p - htab->glink_eh_frame->contents;
13364 bfd_put_32 (htab->elf.dynobj, val, p);
13365 p += 4;
13366 /* Offset to .glink, written later. */
13367 p += 4;
13368 /* .glink size. */
13369 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13370 p += 4;
13371 /* Augmentation. */
13372 p += 1;
13373
13374 *p++ = DW_CFA_advance_loc + 1;
13375 *p++ = DW_CFA_register;
13376 *p++ = 65;
13377 *p++ = htab->opd_abi ? 12 : 0;
13378 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13379 *p++ = DW_CFA_restore_extended;
13380 *p++ = 65;
13381 p += ((24 + align - 1) & -align) - 24;
13382 }
13383 /* Subsume any padding into the last FDE if user .eh_frame
13384 sections are aligned more than glink_eh_frame. Otherwise any
13385 zero padding will be seen as a terminator. */
13386 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13387 size = p - htab->glink_eh_frame->contents;
13388 pad = ((size + align - 1) & -align) - size;
13389 htab->glink_eh_frame->size = size + pad;
13390 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13391 }
13392
13393 maybe_strip_output (info, htab->brlt);
13394 if (htab->glink_eh_frame != NULL)
13395 maybe_strip_output (info, htab->glink_eh_frame);
13396
13397 return TRUE;
13398 }
13399
13400 /* Called after we have determined section placement. If sections
13401 move, we'll be called again. Provide a value for TOCstart. */
13402
13403 bfd_vma
13404 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13405 {
13406 asection *s;
13407 bfd_vma TOCstart, adjust;
13408
13409 if (info != NULL)
13410 {
13411 struct elf_link_hash_entry *h;
13412 struct elf_link_hash_table *htab = elf_hash_table (info);
13413
13414 if (is_elf_hash_table (htab)
13415 && htab->hgot != NULL)
13416 h = htab->hgot;
13417 else
13418 {
13419 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13420 if (is_elf_hash_table (htab))
13421 htab->hgot = h;
13422 }
13423 if (h != NULL
13424 && h->root.type == bfd_link_hash_defined
13425 && !h->root.linker_def
13426 && (!is_elf_hash_table (htab)
13427 || h->def_regular))
13428 {
13429 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13430 + h->root.u.def.section->output_offset
13431 + h->root.u.def.section->output_section->vma);
13432 _bfd_set_gp_value (obfd, TOCstart);
13433 return TOCstart;
13434 }
13435 }
13436
13437 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13438 order. The TOC starts where the first of these sections starts. */
13439 s = bfd_get_section_by_name (obfd, ".got");
13440 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13441 s = bfd_get_section_by_name (obfd, ".toc");
13442 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13443 s = bfd_get_section_by_name (obfd, ".tocbss");
13444 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13445 s = bfd_get_section_by_name (obfd, ".plt");
13446 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13447 {
13448 /* This may happen for
13449 o references to TOC base (SYM@toc / TOC[tc0]) without a
13450 .toc directive
13451 o bad linker script
13452 o --gc-sections and empty TOC sections
13453
13454 FIXME: Warn user? */
13455
13456 /* Look for a likely section. We probably won't even be
13457 using TOCstart. */
13458 for (s = obfd->sections; s != NULL; s = s->next)
13459 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13460 | SEC_EXCLUDE))
13461 == (SEC_ALLOC | SEC_SMALL_DATA))
13462 break;
13463 if (s == NULL)
13464 for (s = obfd->sections; s != NULL; s = s->next)
13465 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13466 == (SEC_ALLOC | SEC_SMALL_DATA))
13467 break;
13468 if (s == NULL)
13469 for (s = obfd->sections; s != NULL; s = s->next)
13470 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13471 == SEC_ALLOC)
13472 break;
13473 if (s == NULL)
13474 for (s = obfd->sections; s != NULL; s = s->next)
13475 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13476 break;
13477 }
13478
13479 TOCstart = 0;
13480 if (s != NULL)
13481 TOCstart = s->output_section->vma + s->output_offset;
13482
13483 /* Force alignment. */
13484 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13485 TOCstart -= adjust;
13486 _bfd_set_gp_value (obfd, TOCstart);
13487
13488 if (info != NULL && s != NULL)
13489 {
13490 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13491
13492 if (htab != NULL)
13493 {
13494 if (htab->elf.hgot != NULL)
13495 {
13496 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13497 htab->elf.hgot->root.u.def.section = s;
13498 }
13499 }
13500 else
13501 {
13502 struct bfd_link_hash_entry *bh = NULL;
13503 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13504 s, TOC_BASE_OFF - adjust,
13505 NULL, FALSE, FALSE, &bh);
13506 }
13507 }
13508 return TOCstart;
13509 }
13510
13511 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13512 write out any global entry stubs, and PLT relocations. */
13513
13514 static bfd_boolean
13515 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13516 {
13517 struct bfd_link_info *info;
13518 struct ppc_link_hash_table *htab;
13519 struct plt_entry *ent;
13520 asection *s;
13521
13522 if (h->root.type == bfd_link_hash_indirect)
13523 return TRUE;
13524
13525 info = inf;
13526 htab = ppc_hash_table (info);
13527 if (htab == NULL)
13528 return FALSE;
13529
13530 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13531 if (ent->plt.offset != (bfd_vma) -1)
13532 {
13533 /* This symbol has an entry in the procedure linkage
13534 table. Set it up. */
13535 Elf_Internal_Rela rela;
13536 asection *plt, *relplt;
13537 bfd_byte *loc;
13538
13539 if (!htab->elf.dynamic_sections_created
13540 || h->dynindx == -1)
13541 {
13542 if (!(h->def_regular
13543 && (h->root.type == bfd_link_hash_defined
13544 || h->root.type == bfd_link_hash_defweak)))
13545 continue;
13546 if (h->type == STT_GNU_IFUNC)
13547 {
13548 plt = htab->elf.iplt;
13549 relplt = htab->elf.irelplt;
13550 htab->local_ifunc_resolver = 1;
13551 if (htab->opd_abi)
13552 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13553 else
13554 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13555 }
13556 else
13557 {
13558 plt = htab->pltlocal;
13559 if (bfd_link_pic (info))
13560 {
13561 relplt = htab->relpltlocal;
13562 if (htab->opd_abi)
13563 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13564 else
13565 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13566 }
13567 else
13568 relplt = NULL;
13569 }
13570 rela.r_addend = (h->root.u.def.value
13571 + h->root.u.def.section->output_offset
13572 + h->root.u.def.section->output_section->vma
13573 + ent->addend);
13574
13575 if (relplt == NULL)
13576 {
13577 loc = plt->contents + ent->plt.offset;
13578 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13579 if (htab->opd_abi)
13580 {
13581 bfd_vma toc = elf_gp (info->output_bfd);
13582 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13583 bfd_put_64 (info->output_bfd, toc, loc + 8);
13584 }
13585 }
13586 else
13587 {
13588 rela.r_offset = (plt->output_section->vma
13589 + plt->output_offset
13590 + ent->plt.offset);
13591 loc = relplt->contents + (relplt->reloc_count++
13592 * sizeof (Elf64_External_Rela));
13593 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13594 }
13595 }
13596 else
13597 {
13598 rela.r_offset = (htab->elf.splt->output_section->vma
13599 + htab->elf.splt->output_offset
13600 + ent->plt.offset);
13601 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13602 rela.r_addend = ent->addend;
13603 loc = (htab->elf.srelplt->contents
13604 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13605 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13606 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13607 htab->maybe_local_ifunc_resolver = 1;
13608 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13609 }
13610 }
13611
13612 if (!h->pointer_equality_needed)
13613 return TRUE;
13614
13615 if (h->def_regular)
13616 return TRUE;
13617
13618 s = htab->global_entry;
13619 if (s == NULL || s->size == 0)
13620 return TRUE;
13621
13622 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13623 if (ent->plt.offset != (bfd_vma) -1
13624 && ent->addend == 0)
13625 {
13626 bfd_byte *p;
13627 asection *plt;
13628 bfd_vma off;
13629
13630 p = s->contents + h->root.u.def.value;
13631 plt = htab->elf.splt;
13632 if (!htab->elf.dynamic_sections_created
13633 || h->dynindx == -1)
13634 {
13635 if (h->type == STT_GNU_IFUNC)
13636 plt = htab->elf.iplt;
13637 else
13638 plt = htab->pltlocal;
13639 }
13640 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13641 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13642
13643 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13644 {
13645 info->callbacks->einfo
13646 (_("%P: linkage table error against `%pT'\n"),
13647 h->root.root.string);
13648 bfd_set_error (bfd_error_bad_value);
13649 htab->stub_error = TRUE;
13650 }
13651
13652 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13653 if (htab->params->emit_stub_syms)
13654 {
13655 size_t len = strlen (h->root.root.string);
13656 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13657
13658 if (name == NULL)
13659 return FALSE;
13660
13661 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13662 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13663 if (h == NULL)
13664 return FALSE;
13665 if (h->root.type == bfd_link_hash_new)
13666 {
13667 h->root.type = bfd_link_hash_defined;
13668 h->root.u.def.section = s;
13669 h->root.u.def.value = p - s->contents;
13670 h->ref_regular = 1;
13671 h->def_regular = 1;
13672 h->ref_regular_nonweak = 1;
13673 h->forced_local = 1;
13674 h->non_elf = 0;
13675 h->root.linker_def = 1;
13676 }
13677 }
13678
13679 if (PPC_HA (off) != 0)
13680 {
13681 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13682 p += 4;
13683 }
13684 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13685 p += 4;
13686 bfd_put_32 (s->owner, MTCTR_R12, p);
13687 p += 4;
13688 bfd_put_32 (s->owner, BCTR, p);
13689 break;
13690 }
13691 return TRUE;
13692 }
13693
13694 /* Write PLT relocs for locals. */
13695
13696 static bfd_boolean
13697 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13698 {
13699 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13700 bfd *ibfd;
13701
13702 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13703 {
13704 struct got_entry **lgot_ents, **end_lgot_ents;
13705 struct plt_entry **local_plt, **lplt, **end_local_plt;
13706 Elf_Internal_Shdr *symtab_hdr;
13707 bfd_size_type locsymcount;
13708 Elf_Internal_Sym *local_syms = NULL;
13709 struct plt_entry *ent;
13710
13711 if (!is_ppc64_elf (ibfd))
13712 continue;
13713
13714 lgot_ents = elf_local_got_ents (ibfd);
13715 if (!lgot_ents)
13716 continue;
13717
13718 symtab_hdr = &elf_symtab_hdr (ibfd);
13719 locsymcount = symtab_hdr->sh_info;
13720 end_lgot_ents = lgot_ents + locsymcount;
13721 local_plt = (struct plt_entry **) end_lgot_ents;
13722 end_local_plt = local_plt + locsymcount;
13723 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13724 for (ent = *lplt; ent != NULL; ent = ent->next)
13725 if (ent->plt.offset != (bfd_vma) -1)
13726 {
13727 Elf_Internal_Sym *sym;
13728 asection *sym_sec;
13729 asection *plt, *relplt;
13730 bfd_byte *loc;
13731 bfd_vma val;
13732
13733 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13734 lplt - local_plt, ibfd))
13735 {
13736 if (local_syms != NULL
13737 && symtab_hdr->contents != (unsigned char *) local_syms)
13738 free (local_syms);
13739 return FALSE;
13740 }
13741
13742 val = sym->st_value + ent->addend;
13743 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13744 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13745 if (sym_sec != NULL && sym_sec->output_section != NULL)
13746 val += sym_sec->output_offset + sym_sec->output_section->vma;
13747
13748 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13749 {
13750 htab->local_ifunc_resolver = 1;
13751 plt = htab->elf.iplt;
13752 relplt = htab->elf.irelplt;
13753 }
13754 else
13755 {
13756 plt = htab->pltlocal;
13757 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13758 }
13759
13760 if (relplt == NULL)
13761 {
13762 loc = plt->contents + ent->plt.offset;
13763 bfd_put_64 (info->output_bfd, val, loc);
13764 if (htab->opd_abi)
13765 {
13766 bfd_vma toc = elf_gp (ibfd);
13767 bfd_put_64 (info->output_bfd, toc, loc + 8);
13768 }
13769 }
13770 else
13771 {
13772 Elf_Internal_Rela rela;
13773 rela.r_offset = (ent->plt.offset
13774 + plt->output_offset
13775 + plt->output_section->vma);
13776 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13777 {
13778 if (htab->opd_abi)
13779 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13780 else
13781 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13782 }
13783 else
13784 {
13785 if (htab->opd_abi)
13786 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13787 else
13788 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13789 }
13790 rela.r_addend = val;
13791 loc = relplt->contents + (relplt->reloc_count++
13792 * sizeof (Elf64_External_Rela));
13793 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13794 }
13795 }
13796
13797 if (local_syms != NULL
13798 && symtab_hdr->contents != (unsigned char *) local_syms)
13799 {
13800 if (!info->keep_memory)
13801 free (local_syms);
13802 else
13803 symtab_hdr->contents = (unsigned char *) local_syms;
13804 }
13805 }
13806 return TRUE;
13807 }
13808
13809 /* Build all the stubs associated with the current output file.
13810 The stubs are kept in a hash table attached to the main linker
13811 hash table. This function is called via gldelf64ppc_finish. */
13812
13813 bfd_boolean
13814 ppc64_elf_build_stubs (struct bfd_link_info *info,
13815 char **stats)
13816 {
13817 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13818 struct map_stub *group;
13819 asection *stub_sec;
13820 bfd_byte *p;
13821 int stub_sec_count = 0;
13822
13823 if (htab == NULL)
13824 return FALSE;
13825
13826 /* Allocate memory to hold the linker stubs. */
13827 for (group = htab->group; group != NULL; group = group->next)
13828 {
13829 group->eh_size = 0;
13830 group->lr_restore = 0;
13831 if ((stub_sec = group->stub_sec) != NULL
13832 && stub_sec->size != 0)
13833 {
13834 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13835 stub_sec->size);
13836 if (stub_sec->contents == NULL)
13837 return FALSE;
13838 stub_sec->size = 0;
13839 }
13840 }
13841
13842 if (htab->glink != NULL && htab->glink->size != 0)
13843 {
13844 unsigned int indx;
13845 bfd_vma plt0;
13846
13847 /* Build the .glink plt call stub. */
13848 if (htab->params->emit_stub_syms)
13849 {
13850 struct elf_link_hash_entry *h;
13851 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13852 TRUE, FALSE, FALSE);
13853 if (h == NULL)
13854 return FALSE;
13855 if (h->root.type == bfd_link_hash_new)
13856 {
13857 h->root.type = bfd_link_hash_defined;
13858 h->root.u.def.section = htab->glink;
13859 h->root.u.def.value = 8;
13860 h->ref_regular = 1;
13861 h->def_regular = 1;
13862 h->ref_regular_nonweak = 1;
13863 h->forced_local = 1;
13864 h->non_elf = 0;
13865 h->root.linker_def = 1;
13866 }
13867 }
13868 plt0 = (htab->elf.splt->output_section->vma
13869 + htab->elf.splt->output_offset
13870 - 16);
13871 if (info->emitrelocations)
13872 {
13873 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13874 if (r == NULL)
13875 return FALSE;
13876 r->r_offset = (htab->glink->output_offset
13877 + htab->glink->output_section->vma);
13878 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13879 r->r_addend = plt0;
13880 }
13881 p = htab->glink->contents;
13882 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13883 bfd_put_64 (htab->glink->owner, plt0, p);
13884 p += 8;
13885 if (htab->opd_abi)
13886 {
13887 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13888 p += 4;
13889 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13890 p += 4;
13891 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13892 p += 4;
13893 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13894 p += 4;
13895 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13896 p += 4;
13897 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13898 p += 4;
13899 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13900 p += 4;
13901 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13902 p += 4;
13903 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13904 p += 4;
13905 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13906 p += 4;
13907 }
13908 else
13909 {
13910 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13911 p += 4;
13912 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13913 p += 4;
13914 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13915 p += 4;
13916 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13917 p += 4;
13918 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13919 p += 4;
13920 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13921 p += 4;
13922 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13923 p += 4;
13924 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13925 p += 4;
13926 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13927 p += 4;
13928 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13929 p += 4;
13930 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13931 p += 4;
13932 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13933 p += 4;
13934 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13935 p += 4;
13936 }
13937 bfd_put_32 (htab->glink->owner, BCTR, p);
13938 p += 4;
13939 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13940
13941 /* Build the .glink lazy link call stubs. */
13942 indx = 0;
13943 while (p < htab->glink->contents + htab->glink->size)
13944 {
13945 if (htab->opd_abi)
13946 {
13947 if (indx < 0x8000)
13948 {
13949 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13950 p += 4;
13951 }
13952 else
13953 {
13954 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13955 p += 4;
13956 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13957 p);
13958 p += 4;
13959 }
13960 }
13961 bfd_put_32 (htab->glink->owner,
13962 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13963 indx++;
13964 p += 4;
13965 }
13966 }
13967
13968 /* Build .glink global entry stubs, and PLT relocs for globals. */
13969 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13970
13971 if (!write_plt_relocs_for_local_syms (info))
13972 return FALSE;
13973
13974 if (htab->brlt != NULL && htab->brlt->size != 0)
13975 {
13976 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13977 htab->brlt->size);
13978 if (htab->brlt->contents == NULL)
13979 return FALSE;
13980 }
13981 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13982 {
13983 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13984 htab->relbrlt->size);
13985 if (htab->relbrlt->contents == NULL)
13986 return FALSE;
13987 }
13988
13989 /* Build the stubs as directed by the stub hash table. */
13990 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13991
13992 for (group = htab->group; group != NULL; group = group->next)
13993 if (group->needs_save_res)
13994 group->stub_sec->size += htab->sfpr->size;
13995
13996 if (htab->relbrlt != NULL)
13997 htab->relbrlt->reloc_count = 0;
13998
13999 if (htab->params->plt_stub_align != 0)
14000 for (group = htab->group; group != NULL; group = group->next)
14001 if ((stub_sec = group->stub_sec) != NULL)
14002 {
14003 int align = abs (htab->params->plt_stub_align);
14004 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14005 }
14006
14007 for (group = htab->group; group != NULL; group = group->next)
14008 if (group->needs_save_res)
14009 {
14010 stub_sec = group->stub_sec;
14011 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14012 htab->sfpr->contents, htab->sfpr->size);
14013 if (htab->params->emit_stub_syms)
14014 {
14015 unsigned int i;
14016
14017 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14018 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14019 return FALSE;
14020 }
14021 }
14022
14023 if (htab->glink_eh_frame != NULL
14024 && htab->glink_eh_frame->size != 0)
14025 {
14026 bfd_vma val;
14027 size_t align = 4;
14028
14029 p = htab->glink_eh_frame->contents;
14030 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14031
14032 for (group = htab->group; group != NULL; group = group->next)
14033 if (group->eh_size != 0)
14034 {
14035 /* Offset to stub section. */
14036 val = (group->stub_sec->output_section->vma
14037 + group->stub_sec->output_offset);
14038 val -= (htab->glink_eh_frame->output_section->vma
14039 + htab->glink_eh_frame->output_offset
14040 + (p + 8 - htab->glink_eh_frame->contents));
14041 if (val + 0x80000000 > 0xffffffff)
14042 {
14043 _bfd_error_handler
14044 (_("%s offset too large for .eh_frame sdata4 encoding"),
14045 group->stub_sec->name);
14046 return FALSE;
14047 }
14048 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14049 p += (group->eh_size + 17 + 3) & -4;
14050 }
14051 if (htab->glink != NULL && htab->glink->size != 0)
14052 {
14053 /* Offset to .glink. */
14054 val = (htab->glink->output_section->vma
14055 + htab->glink->output_offset
14056 + 8);
14057 val -= (htab->glink_eh_frame->output_section->vma
14058 + htab->glink_eh_frame->output_offset
14059 + (p + 8 - htab->glink_eh_frame->contents));
14060 if (val + 0x80000000 > 0xffffffff)
14061 {
14062 _bfd_error_handler
14063 (_("%s offset too large for .eh_frame sdata4 encoding"),
14064 htab->glink->name);
14065 return FALSE;
14066 }
14067 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14068 p += (24 + align - 1) & -align;
14069 }
14070 }
14071
14072 for (group = htab->group; group != NULL; group = group->next)
14073 if ((stub_sec = group->stub_sec) != NULL)
14074 {
14075 stub_sec_count += 1;
14076 if (stub_sec->rawsize != stub_sec->size
14077 && (htab->stub_iteration <= STUB_SHRINK_ITER
14078 || stub_sec->rawsize < stub_sec->size))
14079 break;
14080 }
14081
14082 if (group != NULL)
14083 {
14084 htab->stub_error = TRUE;
14085 _bfd_error_handler (_("stubs don't match calculated size"));
14086 }
14087
14088 if (htab->stub_error)
14089 return FALSE;
14090
14091 if (stats != NULL)
14092 {
14093 size_t len;
14094 *stats = bfd_malloc (500);
14095 if (*stats == NULL)
14096 return FALSE;
14097
14098 len = sprintf (*stats,
14099 ngettext ("linker stubs in %u group\n",
14100 "linker stubs in %u groups\n",
14101 stub_sec_count),
14102 stub_sec_count);
14103 sprintf (*stats + len, _(" branch %lu\n"
14104 " branch toc adj %lu\n"
14105 " branch notoc %lu\n"
14106 " branch both %lu\n"
14107 " long branch %lu\n"
14108 " long toc adj %lu\n"
14109 " long notoc %lu\n"
14110 " long both %lu\n"
14111 " plt call %lu\n"
14112 " plt call save %lu\n"
14113 " plt call notoc %lu\n"
14114 " plt call both %lu\n"
14115 " global entry %lu"),
14116 htab->stub_count[ppc_stub_long_branch - 1],
14117 htab->stub_count[ppc_stub_long_branch_r2off - 1],
14118 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14119 htab->stub_count[ppc_stub_long_branch_both - 1],
14120 htab->stub_count[ppc_stub_plt_branch - 1],
14121 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14122 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14123 htab->stub_count[ppc_stub_plt_branch_both - 1],
14124 htab->stub_count[ppc_stub_plt_call - 1],
14125 htab->stub_count[ppc_stub_plt_call_r2save - 1],
14126 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14127 htab->stub_count[ppc_stub_plt_call_both - 1],
14128 htab->stub_count[ppc_stub_global_entry - 1]);
14129 }
14130 return TRUE;
14131 }
14132
14133 /* What to do when ld finds relocations against symbols defined in
14134 discarded sections. */
14135
14136 static unsigned int
14137 ppc64_elf_action_discarded (asection *sec)
14138 {
14139 if (strcmp (".opd", sec->name) == 0)
14140 return 0;
14141
14142 if (strcmp (".toc", sec->name) == 0)
14143 return 0;
14144
14145 if (strcmp (".toc1", sec->name) == 0)
14146 return 0;
14147
14148 return _bfd_elf_default_action_discarded (sec);
14149 }
14150
14151 /* The RELOCATE_SECTION function is called by the ELF backend linker
14152 to handle the relocations for a section.
14153
14154 The relocs are always passed as Rela structures; if the section
14155 actually uses Rel structures, the r_addend field will always be
14156 zero.
14157
14158 This function is responsible for adjust the section contents as
14159 necessary, and (if using Rela relocs and generating a
14160 relocatable output file) adjusting the reloc addend as
14161 necessary.
14162
14163 This function does not have to worry about setting the reloc
14164 address or the reloc symbol index.
14165
14166 LOCAL_SYMS is a pointer to the swapped in local symbols.
14167
14168 LOCAL_SECTIONS is an array giving the section in the input file
14169 corresponding to the st_shndx field of each local symbol.
14170
14171 The global hash table entry for the global symbols can be found
14172 via elf_sym_hashes (input_bfd).
14173
14174 When generating relocatable output, this function must handle
14175 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14176 going to be the section symbol corresponding to the output
14177 section, which means that the addend must be adjusted
14178 accordingly. */
14179
14180 static bfd_boolean
14181 ppc64_elf_relocate_section (bfd *output_bfd,
14182 struct bfd_link_info *info,
14183 bfd *input_bfd,
14184 asection *input_section,
14185 bfd_byte *contents,
14186 Elf_Internal_Rela *relocs,
14187 Elf_Internal_Sym *local_syms,
14188 asection **local_sections)
14189 {
14190 struct ppc_link_hash_table *htab;
14191 Elf_Internal_Shdr *symtab_hdr;
14192 struct elf_link_hash_entry **sym_hashes;
14193 Elf_Internal_Rela *rel;
14194 Elf_Internal_Rela *wrel;
14195 Elf_Internal_Rela *relend;
14196 Elf_Internal_Rela outrel;
14197 bfd_byte *loc;
14198 struct got_entry **local_got_ents;
14199 bfd_vma TOCstart;
14200 bfd_boolean ret = TRUE;
14201 bfd_boolean is_opd;
14202 /* Assume 'at' branch hints. */
14203 bfd_boolean is_isa_v2 = TRUE;
14204 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
14205
14206 /* Initialize howto table if needed. */
14207 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
14208 ppc_howto_init ();
14209
14210 htab = ppc_hash_table (info);
14211 if (htab == NULL)
14212 return FALSE;
14213
14214 /* Don't relocate stub sections. */
14215 if (input_section->owner == htab->params->stub_bfd)
14216 return TRUE;
14217
14218 if (!is_ppc64_elf (input_bfd))
14219 {
14220 bfd_set_error (bfd_error_wrong_format);
14221 return FALSE;
14222 }
14223
14224 local_got_ents = elf_local_got_ents (input_bfd);
14225 TOCstart = elf_gp (output_bfd);
14226 symtab_hdr = &elf_symtab_hdr (input_bfd);
14227 sym_hashes = elf_sym_hashes (input_bfd);
14228 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
14229
14230 rel = wrel = relocs;
14231 relend = relocs + input_section->reloc_count;
14232 for (; rel < relend; wrel++, rel++)
14233 {
14234 enum elf_ppc64_reloc_type r_type;
14235 bfd_vma addend;
14236 bfd_reloc_status_type r;
14237 Elf_Internal_Sym *sym;
14238 asection *sec;
14239 struct elf_link_hash_entry *h_elf;
14240 struct ppc_link_hash_entry *h;
14241 struct ppc_link_hash_entry *fdh;
14242 const char *sym_name;
14243 unsigned long r_symndx, toc_symndx;
14244 bfd_vma toc_addend;
14245 unsigned char tls_mask, tls_gd, tls_type;
14246 unsigned char sym_type;
14247 bfd_vma relocation;
14248 bfd_boolean unresolved_reloc, save_unresolved_reloc;
14249 bfd_boolean warned;
14250 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
14251 unsigned int insn;
14252 unsigned int mask;
14253 struct ppc_stub_hash_entry *stub_entry;
14254 bfd_vma max_br_offset;
14255 bfd_vma from;
14256 Elf_Internal_Rela orig_rel;
14257 reloc_howto_type *howto;
14258 struct reloc_howto_struct alt_howto;
14259 uint64_t pinsn;
14260 bfd_vma offset;
14261
14262 again:
14263 orig_rel = *rel;
14264
14265 r_type = ELF64_R_TYPE (rel->r_info);
14266 r_symndx = ELF64_R_SYM (rel->r_info);
14267
14268 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14269 symbol of the previous ADDR64 reloc. The symbol gives us the
14270 proper TOC base to use. */
14271 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
14272 && wrel != relocs
14273 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
14274 && is_opd)
14275 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
14276
14277 sym = NULL;
14278 sec = NULL;
14279 h_elf = NULL;
14280 sym_name = NULL;
14281 unresolved_reloc = FALSE;
14282 warned = FALSE;
14283
14284 if (r_symndx < symtab_hdr->sh_info)
14285 {
14286 /* It's a local symbol. */
14287 struct _opd_sec_data *opd;
14288
14289 sym = local_syms + r_symndx;
14290 sec = local_sections[r_symndx];
14291 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
14292 sym_type = ELF64_ST_TYPE (sym->st_info);
14293 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
14294 opd = get_opd_info (sec);
14295 if (opd != NULL && opd->adjust != NULL)
14296 {
14297 long adjust = opd->adjust[OPD_NDX (sym->st_value
14298 + rel->r_addend)];
14299 if (adjust == -1)
14300 relocation = 0;
14301 else
14302 {
14303 /* If this is a relocation against the opd section sym
14304 and we have edited .opd, adjust the reloc addend so
14305 that ld -r and ld --emit-relocs output is correct.
14306 If it is a reloc against some other .opd symbol,
14307 then the symbol value will be adjusted later. */
14308 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14309 rel->r_addend += adjust;
14310 else
14311 relocation += adjust;
14312 }
14313 }
14314 }
14315 else
14316 {
14317 bfd_boolean ignored;
14318
14319 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14320 r_symndx, symtab_hdr, sym_hashes,
14321 h_elf, sec, relocation,
14322 unresolved_reloc, warned, ignored);
14323 sym_name = h_elf->root.root.string;
14324 sym_type = h_elf->type;
14325 if (sec != NULL
14326 && sec->owner == output_bfd
14327 && strcmp (sec->name, ".opd") == 0)
14328 {
14329 /* This is a symbol defined in a linker script. All
14330 such are defined in output sections, even those
14331 defined by simple assignment from a symbol defined in
14332 an input section. Transfer the symbol to an
14333 appropriate input .opd section, so that a branch to
14334 this symbol will be mapped to the location specified
14335 by the opd entry. */
14336 struct bfd_link_order *lo;
14337 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14338 if (lo->type == bfd_indirect_link_order)
14339 {
14340 asection *isec = lo->u.indirect.section;
14341 if (h_elf->root.u.def.value >= isec->output_offset
14342 && h_elf->root.u.def.value < (isec->output_offset
14343 + isec->size))
14344 {
14345 h_elf->root.u.def.value -= isec->output_offset;
14346 h_elf->root.u.def.section = isec;
14347 sec = isec;
14348 break;
14349 }
14350 }
14351 }
14352 }
14353 h = (struct ppc_link_hash_entry *) h_elf;
14354
14355 if (sec != NULL && discarded_section (sec))
14356 {
14357 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14358 input_bfd, input_section,
14359 contents, rel->r_offset);
14360 wrel->r_offset = rel->r_offset;
14361 wrel->r_info = 0;
14362 wrel->r_addend = 0;
14363
14364 /* For ld -r, remove relocations in debug sections against
14365 symbols defined in discarded sections. Not done for
14366 non-debug to preserve relocs in .eh_frame which the
14367 eh_frame editing code expects to be present. */
14368 if (bfd_link_relocatable (info)
14369 && (input_section->flags & SEC_DEBUGGING))
14370 wrel--;
14371
14372 continue;
14373 }
14374
14375 if (bfd_link_relocatable (info))
14376 goto copy_reloc;
14377
14378 if (h != NULL && &h->elf == htab->elf.hgot)
14379 {
14380 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14381 sec = bfd_abs_section_ptr;
14382 unresolved_reloc = FALSE;
14383 }
14384
14385 /* TLS optimizations. Replace instruction sequences and relocs
14386 based on information we collected in tls_optimize. We edit
14387 RELOCS so that --emit-relocs will output something sensible
14388 for the final instruction stream. */
14389 tls_mask = 0;
14390 tls_gd = 0;
14391 toc_symndx = 0;
14392 if (h != NULL)
14393 tls_mask = h->tls_mask;
14394 else if (local_got_ents != NULL)
14395 {
14396 struct plt_entry **local_plt = (struct plt_entry **)
14397 (local_got_ents + symtab_hdr->sh_info);
14398 unsigned char *lgot_masks = (unsigned char *)
14399 (local_plt + symtab_hdr->sh_info);
14400 tls_mask = lgot_masks[r_symndx];
14401 }
14402 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14403 && (r_type == R_PPC64_TLS
14404 || r_type == R_PPC64_TLSGD
14405 || r_type == R_PPC64_TLSLD))
14406 {
14407 /* Check for toc tls entries. */
14408 unsigned char *toc_tls;
14409
14410 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14411 &local_syms, rel, input_bfd))
14412 return FALSE;
14413
14414 if (toc_tls)
14415 tls_mask = *toc_tls;
14416 }
14417
14418 /* Check that tls relocs are used with tls syms, and non-tls
14419 relocs are used with non-tls syms. */
14420 if (r_symndx != STN_UNDEF
14421 && r_type != R_PPC64_NONE
14422 && (h == NULL
14423 || h->elf.root.type == bfd_link_hash_defined
14424 || h->elf.root.type == bfd_link_hash_defweak)
14425 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
14426 {
14427 if ((tls_mask & TLS_TLS) != 0
14428 && (r_type == R_PPC64_TLS
14429 || r_type == R_PPC64_TLSGD
14430 || r_type == R_PPC64_TLSLD))
14431 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14432 ;
14433 else
14434 info->callbacks->einfo
14435 (!IS_PPC64_TLS_RELOC (r_type)
14436 /* xgettext:c-format */
14437 ? _("%H: %s used with TLS symbol `%pT'\n")
14438 /* xgettext:c-format */
14439 : _("%H: %s used with non-TLS symbol `%pT'\n"),
14440 input_bfd, input_section, rel->r_offset,
14441 ppc64_elf_howto_table[r_type]->name,
14442 sym_name);
14443 }
14444
14445 /* Ensure reloc mapping code below stays sane. */
14446 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14447 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14448 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14449 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14450 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14451 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14452 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14453 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14454 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14455 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14456 abort ();
14457
14458 switch (r_type)
14459 {
14460 default:
14461 break;
14462
14463 case R_PPC64_LO_DS_OPT:
14464 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
14465 if ((insn & (0x3f << 26)) != 58u << 26)
14466 abort ();
14467 insn += (14u << 26) - (58u << 26);
14468 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
14469 r_type = R_PPC64_TOC16_LO;
14470 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14471 break;
14472
14473 case R_PPC64_TOC16:
14474 case R_PPC64_TOC16_LO:
14475 case R_PPC64_TOC16_DS:
14476 case R_PPC64_TOC16_LO_DS:
14477 {
14478 /* Check for toc tls entries. */
14479 unsigned char *toc_tls;
14480 int retval;
14481
14482 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14483 &local_syms, rel, input_bfd);
14484 if (retval == 0)
14485 return FALSE;
14486
14487 if (toc_tls)
14488 {
14489 tls_mask = *toc_tls;
14490 if (r_type == R_PPC64_TOC16_DS
14491 || r_type == R_PPC64_TOC16_LO_DS)
14492 {
14493 if ((tls_mask & TLS_TLS) != 0
14494 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14495 goto toctprel;
14496 }
14497 else
14498 {
14499 /* If we found a GD reloc pair, then we might be
14500 doing a GD->IE transition. */
14501 if (retval == 2)
14502 {
14503 tls_gd = TLS_GDIE;
14504 if ((tls_mask & TLS_TLS) != 0
14505 && (tls_mask & TLS_GD) == 0)
14506 goto tls_ldgd_opt;
14507 }
14508 else if (retval == 3)
14509 {
14510 if ((tls_mask & TLS_TLS) != 0
14511 && (tls_mask & TLS_LD) == 0)
14512 goto tls_ldgd_opt;
14513 }
14514 }
14515 }
14516 }
14517 break;
14518
14519 case R_PPC64_GOT_TPREL16_HI:
14520 case R_PPC64_GOT_TPREL16_HA:
14521 if ((tls_mask & TLS_TLS) != 0
14522 && (tls_mask & TLS_TPREL) == 0)
14523 {
14524 rel->r_offset -= d_offset;
14525 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14526 r_type = R_PPC64_NONE;
14527 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14528 }
14529 break;
14530
14531 case R_PPC64_GOT_TPREL16_DS:
14532 case R_PPC64_GOT_TPREL16_LO_DS:
14533 if ((tls_mask & TLS_TLS) != 0
14534 && (tls_mask & TLS_TPREL) == 0)
14535 {
14536 toctprel:
14537 insn = bfd_get_32 (input_bfd,
14538 contents + rel->r_offset - d_offset);
14539 insn &= 31 << 21;
14540 insn |= 0x3c0d0000; /* addis 0,13,0 */
14541 bfd_put_32 (input_bfd, insn,
14542 contents + rel->r_offset - d_offset);
14543 r_type = R_PPC64_TPREL16_HA;
14544 if (toc_symndx != 0)
14545 {
14546 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14547 rel->r_addend = toc_addend;
14548 /* We changed the symbol. Start over in order to
14549 get h, sym, sec etc. right. */
14550 goto again;
14551 }
14552 else
14553 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14554 }
14555 break;
14556
14557 case R_PPC64_GOT_TPREL34:
14558 if ((tls_mask & TLS_TLS) != 0
14559 && (tls_mask & TLS_TPREL) == 0)
14560 {
14561 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
14562 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14563 pinsn <<= 32;
14564 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14565 pinsn += ((2ULL << 56) + (-1ULL << 52)
14566 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
14567 bfd_put_32 (input_bfd, pinsn >> 32,
14568 contents + rel->r_offset);
14569 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14570 contents + rel->r_offset + 4);
14571 r_type = R_PPC64_TPREL34;
14572 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14573 }
14574 break;
14575
14576 case R_PPC64_TLS:
14577 if ((tls_mask & TLS_TLS) != 0
14578 && (tls_mask & TLS_TPREL) == 0)
14579 {
14580 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14581 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14582 if (insn == 0)
14583 break;
14584 if ((rel->r_offset & 3) == 0)
14585 {
14586 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14587 /* Was PPC64_TLS which sits on insn boundary, now
14588 PPC64_TPREL16_LO which is at low-order half-word. */
14589 rel->r_offset += d_offset;
14590 r_type = R_PPC64_TPREL16_LO;
14591 if (toc_symndx != 0)
14592 {
14593 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14594 rel->r_addend = toc_addend;
14595 /* We changed the symbol. Start over in order to
14596 get h, sym, sec etc. right. */
14597 goto again;
14598 }
14599 else
14600 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14601 }
14602 else if ((rel->r_offset & 3) == 1)
14603 {
14604 /* For pcrel IE to LE we already have the full
14605 offset and thus don't need an addi here. A nop
14606 or mr will do. */
14607 if ((insn & (0x3f << 26)) == 14 << 26)
14608 {
14609 /* Extract regs from addi rt,ra,si. */
14610 unsigned int rt = (insn >> 21) & 0x1f;
14611 unsigned int ra = (insn >> 16) & 0x1f;
14612 if (rt == ra)
14613 insn = NOP;
14614 else
14615 {
14616 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
14617 insn = (rt << 16) | (ra << 21) | (ra << 11);
14618 insn |= (31u << 26) | (444u << 1);
14619 }
14620 }
14621 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
14622 }
14623 }
14624 break;
14625
14626 case R_PPC64_GOT_TLSGD16_HI:
14627 case R_PPC64_GOT_TLSGD16_HA:
14628 tls_gd = TLS_GDIE;
14629 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14630 goto tls_gdld_hi;
14631 break;
14632
14633 case R_PPC64_GOT_TLSLD16_HI:
14634 case R_PPC64_GOT_TLSLD16_HA:
14635 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14636 {
14637 tls_gdld_hi:
14638 if ((tls_mask & tls_gd) != 0)
14639 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14640 + R_PPC64_GOT_TPREL16_DS);
14641 else
14642 {
14643 rel->r_offset -= d_offset;
14644 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14645 r_type = R_PPC64_NONE;
14646 }
14647 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14648 }
14649 break;
14650
14651 case R_PPC64_GOT_TLSGD16:
14652 case R_PPC64_GOT_TLSGD16_LO:
14653 tls_gd = TLS_GDIE;
14654 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14655 goto tls_ldgd_opt;
14656 break;
14657
14658 case R_PPC64_GOT_TLSLD16:
14659 case R_PPC64_GOT_TLSLD16_LO:
14660 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14661 {
14662 unsigned int insn1, insn2;
14663
14664 tls_ldgd_opt:
14665 offset = (bfd_vma) -1;
14666 /* If not using the newer R_PPC64_TLSGD/LD to mark
14667 __tls_get_addr calls, we must trust that the call
14668 stays with its arg setup insns, ie. that the next
14669 reloc is the __tls_get_addr call associated with
14670 the current reloc. Edit both insns. */
14671 if (input_section->has_tls_get_addr_call
14672 && rel + 1 < relend
14673 && branch_reloc_hash_match (input_bfd, rel + 1,
14674 htab->tls_get_addr,
14675 htab->tls_get_addr_fd))
14676 offset = rel[1].r_offset;
14677 /* We read the low GOT_TLS (or TOC16) insn because we
14678 need to keep the destination reg. It may be
14679 something other than the usual r3, and moved to r3
14680 before the call by intervening code. */
14681 insn1 = bfd_get_32 (input_bfd,
14682 contents + rel->r_offset - d_offset);
14683 if ((tls_mask & tls_gd) != 0)
14684 {
14685 /* IE */
14686 insn1 &= (0x1f << 21) | (0x1f << 16);
14687 insn1 |= 58 << 26; /* ld */
14688 insn2 = 0x7c636a14; /* add 3,3,13 */
14689 if (offset != (bfd_vma) -1)
14690 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14691 if (r_type == R_PPC64_TOC16
14692 || r_type == R_PPC64_TOC16_LO)
14693 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14694 else
14695 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
14696 + R_PPC64_GOT_TPREL16_DS);
14697 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14698 }
14699 else
14700 {
14701 /* LE */
14702 insn1 &= 0x1f << 21;
14703 insn1 |= 0x3c0d0000; /* addis r,13,0 */
14704 insn2 = 0x38630000; /* addi 3,3,0 */
14705 if (tls_gd == 0)
14706 {
14707 /* Was an LD reloc. */
14708 r_symndx = STN_UNDEF;
14709 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14710 }
14711 else if (toc_symndx != 0)
14712 {
14713 r_symndx = toc_symndx;
14714 rel->r_addend = toc_addend;
14715 }
14716 r_type = R_PPC64_TPREL16_HA;
14717 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14718 if (offset != (bfd_vma) -1)
14719 {
14720 rel[1].r_info = ELF64_R_INFO (r_symndx,
14721 R_PPC64_TPREL16_LO);
14722 rel[1].r_offset = offset + d_offset;
14723 rel[1].r_addend = rel->r_addend;
14724 }
14725 }
14726 bfd_put_32 (input_bfd, insn1,
14727 contents + rel->r_offset - d_offset);
14728 if (offset != (bfd_vma) -1)
14729 {
14730 bfd_put_32 (input_bfd, insn2, contents + offset);
14731 if (offset + 8 <= input_section->size)
14732 {
14733 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14734 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14735 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14736 }
14737 }
14738 if ((tls_mask & tls_gd) == 0
14739 && (tls_gd == 0 || toc_symndx != 0))
14740 {
14741 /* We changed the symbol. Start over in order
14742 to get h, sym, sec etc. right. */
14743 goto again;
14744 }
14745 }
14746 break;
14747
14748 case R_PPC64_GOT_TLSGD34:
14749 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14750 {
14751 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14752 pinsn <<= 32;
14753 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14754 if ((tls_mask & TLS_GDIE) != 0)
14755 {
14756 /* IE, pla -> pld */
14757 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
14758 r_type = R_PPC64_GOT_TPREL34;
14759 }
14760 else
14761 {
14762 /* LE, pla pcrel -> paddi r13 */
14763 pinsn += (-1ULL << 52) + (13ULL << 16);
14764 r_type = R_PPC64_TPREL34;
14765 }
14766 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14767 bfd_put_32 (input_bfd, pinsn >> 32,
14768 contents + rel->r_offset);
14769 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14770 contents + rel->r_offset + 4);
14771 }
14772 break;
14773
14774 case R_PPC64_GOT_TLSLD34:
14775 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14776 {
14777 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14778 pinsn <<= 32;
14779 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14780 pinsn += (-1ULL << 52) + (13ULL << 16);
14781 bfd_put_32 (input_bfd, pinsn >> 32,
14782 contents + rel->r_offset);
14783 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14784 contents + rel->r_offset + 4);
14785 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14786 r_symndx = STN_UNDEF;
14787 r_type = R_PPC64_TPREL34;
14788 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14789 goto again;
14790 }
14791 break;
14792
14793 case R_PPC64_TLSGD:
14794 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14795 && rel + 1 < relend)
14796 {
14797 unsigned int insn2;
14798 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14799
14800 offset = rel->r_offset;
14801 if (is_plt_seq_reloc (r_type1))
14802 {
14803 bfd_put_32 (output_bfd, NOP, contents + offset);
14804 if (r_type1 == R_PPC64_PLT_PCREL34
14805 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14806 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14807 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14808 break;
14809 }
14810
14811 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14812 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14813
14814 if ((tls_mask & TLS_GDIE) != 0)
14815 {
14816 /* IE */
14817 r_type = R_PPC64_NONE;
14818 insn2 = 0x7c636a14; /* add 3,3,13 */
14819 }
14820 else
14821 {
14822 /* LE */
14823 if (toc_symndx != 0)
14824 {
14825 r_symndx = toc_symndx;
14826 rel->r_addend = toc_addend;
14827 }
14828 if (r_type1 == R_PPC64_REL24_NOTOC
14829 || r_type1 == R_PPC64_PLTCALL_NOTOC)
14830 {
14831 r_type = R_PPC64_NONE;
14832 insn2 = NOP;
14833 }
14834 else
14835 {
14836 rel->r_offset = offset + d_offset;
14837 r_type = R_PPC64_TPREL16_LO;
14838 insn2 = 0x38630000; /* addi 3,3,0 */
14839 }
14840 }
14841 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14842 /* Zap the reloc on the _tls_get_addr call too. */
14843 BFD_ASSERT (offset == rel[1].r_offset);
14844 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14845 bfd_put_32 (input_bfd, insn2, contents + offset);
14846 if ((tls_mask & TLS_GDIE) == 0
14847 && toc_symndx != 0
14848 && r_type != R_PPC64_NONE)
14849 goto again;
14850 }
14851 break;
14852
14853 case R_PPC64_TLSLD:
14854 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14855 && rel + 1 < relend)
14856 {
14857 unsigned int insn2;
14858 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14859
14860 offset = rel->r_offset;
14861 if (is_plt_seq_reloc (r_type1))
14862 {
14863 bfd_put_32 (output_bfd, NOP, contents + offset);
14864 if (r_type1 == R_PPC64_PLT_PCREL34
14865 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14866 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14867 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14868 break;
14869 }
14870
14871 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14872 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14873
14874 if (r_type1 == R_PPC64_REL24_NOTOC
14875 || r_type1 == R_PPC64_PLTCALL_NOTOC)
14876 {
14877 r_type = R_PPC64_NONE;
14878 insn2 = NOP;
14879 }
14880 else
14881 {
14882 rel->r_offset = offset + d_offset;
14883 r_symndx = STN_UNDEF;
14884 r_type = R_PPC64_TPREL16_LO;
14885 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14886 insn2 = 0x38630000; /* addi 3,3,0 */
14887 }
14888 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14889 /* Zap the reloc on the _tls_get_addr call too. */
14890 BFD_ASSERT (offset == rel[1].r_offset);
14891 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14892 bfd_put_32 (input_bfd, insn2, contents + offset);
14893 if (r_type != R_PPC64_NONE)
14894 goto again;
14895 }
14896 break;
14897
14898 case R_PPC64_DTPMOD64:
14899 if (rel + 1 < relend
14900 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14901 && rel[1].r_offset == rel->r_offset + 8)
14902 {
14903 if ((tls_mask & TLS_GD) == 0)
14904 {
14905 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14906 if ((tls_mask & TLS_GDIE) != 0)
14907 r_type = R_PPC64_TPREL64;
14908 else
14909 {
14910 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14911 r_type = R_PPC64_NONE;
14912 }
14913 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14914 }
14915 }
14916 else
14917 {
14918 if ((tls_mask & TLS_LD) == 0)
14919 {
14920 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14921 r_type = R_PPC64_NONE;
14922 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14923 }
14924 }
14925 break;
14926
14927 case R_PPC64_TPREL64:
14928 if ((tls_mask & TLS_TPREL) == 0)
14929 {
14930 r_type = R_PPC64_NONE;
14931 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14932 }
14933 break;
14934
14935 case R_PPC64_ENTRY:
14936 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14937 if (!bfd_link_pic (info)
14938 && !info->traditional_format
14939 && relocation + 0x80008000 <= 0xffffffff)
14940 {
14941 unsigned int insn1, insn2;
14942
14943 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14944 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14945 if ((insn1 & ~0xfffc) == LD_R2_0R12
14946 && insn2 == ADD_R2_R2_R12)
14947 {
14948 bfd_put_32 (input_bfd,
14949 LIS_R2 + PPC_HA (relocation),
14950 contents + rel->r_offset);
14951 bfd_put_32 (input_bfd,
14952 ADDI_R2_R2 + PPC_LO (relocation),
14953 contents + rel->r_offset + 4);
14954 }
14955 }
14956 else
14957 {
14958 relocation -= (rel->r_offset
14959 + input_section->output_offset
14960 + input_section->output_section->vma);
14961 if (relocation + 0x80008000 <= 0xffffffff)
14962 {
14963 unsigned int insn1, insn2;
14964
14965 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14966 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14967 if ((insn1 & ~0xfffc) == LD_R2_0R12
14968 && insn2 == ADD_R2_R2_R12)
14969 {
14970 bfd_put_32 (input_bfd,
14971 ADDIS_R2_R12 + PPC_HA (relocation),
14972 contents + rel->r_offset);
14973 bfd_put_32 (input_bfd,
14974 ADDI_R2_R2 + PPC_LO (relocation),
14975 contents + rel->r_offset + 4);
14976 }
14977 }
14978 }
14979 break;
14980
14981 case R_PPC64_REL16_HA:
14982 /* If we are generating a non-PIC executable, edit
14983 . 0: addis 2,12,.TOC.-0b@ha
14984 . addi 2,2,.TOC.-0b@l
14985 used by ELFv2 global entry points to set up r2, to
14986 . lis 2,.TOC.@ha
14987 . addi 2,2,.TOC.@l
14988 if .TOC. is in range. */
14989 if (!bfd_link_pic (info)
14990 && !info->traditional_format
14991 && !htab->opd_abi
14992 && rel->r_addend == d_offset
14993 && h != NULL && &h->elf == htab->elf.hgot
14994 && rel + 1 < relend
14995 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14996 && rel[1].r_offset == rel->r_offset + 4
14997 && rel[1].r_addend == rel->r_addend + 4
14998 && relocation + 0x80008000 <= 0xffffffff)
14999 {
15000 unsigned int insn1, insn2;
15001 offset = rel->r_offset - d_offset;
15002 insn1 = bfd_get_32 (input_bfd, contents + offset);
15003 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15004 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15005 && (insn2 & 0xffff0000) == ADDI_R2_R2)
15006 {
15007 r_type = R_PPC64_ADDR16_HA;
15008 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15009 rel->r_addend -= d_offset;
15010 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15011 rel[1].r_addend -= d_offset + 4;
15012 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
15013 }
15014 }
15015 break;
15016 }
15017
15018 /* Handle other relocations that tweak non-addend part of insn. */
15019 insn = 0;
15020 max_br_offset = 1 << 25;
15021 addend = rel->r_addend;
15022 reloc_dest = DEST_NORMAL;
15023 switch (r_type)
15024 {
15025 default:
15026 break;
15027
15028 case R_PPC64_TOCSAVE:
15029 if (relocation + addend == (rel->r_offset
15030 + input_section->output_offset
15031 + input_section->output_section->vma)
15032 && tocsave_find (htab, NO_INSERT,
15033 &local_syms, rel, input_bfd))
15034 {
15035 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15036 if (insn == NOP
15037 || insn == CROR_151515 || insn == CROR_313131)
15038 bfd_put_32 (input_bfd,
15039 STD_R2_0R1 + STK_TOC (htab),
15040 contents + rel->r_offset);
15041 }
15042 break;
15043
15044 /* Branch taken prediction relocations. */
15045 case R_PPC64_ADDR14_BRTAKEN:
15046 case R_PPC64_REL14_BRTAKEN:
15047 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
15048 /* Fall through. */
15049
15050 /* Branch not taken prediction relocations. */
15051 case R_PPC64_ADDR14_BRNTAKEN:
15052 case R_PPC64_REL14_BRNTAKEN:
15053 insn |= bfd_get_32 (input_bfd,
15054 contents + rel->r_offset) & ~(0x01 << 21);
15055 /* Fall through. */
15056
15057 case R_PPC64_REL14:
15058 max_br_offset = 1 << 15;
15059 /* Fall through. */
15060
15061 case R_PPC64_REL24:
15062 case R_PPC64_REL24_NOTOC:
15063 case R_PPC64_PLTCALL:
15064 case R_PPC64_PLTCALL_NOTOC:
15065 /* Calls to functions with a different TOC, such as calls to
15066 shared objects, need to alter the TOC pointer. This is
15067 done using a linkage stub. A REL24 branching to these
15068 linkage stubs needs to be followed by a nop, as the nop
15069 will be replaced with an instruction to restore the TOC
15070 base pointer. */
15071 fdh = h;
15072 if (h != NULL
15073 && h->oh != NULL
15074 && h->oh->is_func_descriptor)
15075 fdh = ppc_follow_link (h->oh);
15076 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15077 htab);
15078 if ((r_type == R_PPC64_PLTCALL
15079 || r_type == R_PPC64_PLTCALL_NOTOC)
15080 && stub_entry != NULL
15081 && stub_entry->stub_type >= ppc_stub_plt_call
15082 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15083 stub_entry = NULL;
15084
15085 if (stub_entry != NULL
15086 && ((stub_entry->stub_type >= ppc_stub_plt_call
15087 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15088 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15089 || stub_entry->stub_type == ppc_stub_plt_branch_both
15090 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15091 || stub_entry->stub_type == ppc_stub_long_branch_both))
15092 {
15093 bfd_boolean can_plt_call = FALSE;
15094
15095 if (stub_entry->stub_type == ppc_stub_plt_call
15096 && !htab->opd_abi
15097 && htab->params->plt_localentry0 != 0
15098 && is_elfv2_localentry0 (&h->elf))
15099 {
15100 /* The function doesn't use or change r2. */
15101 can_plt_call = TRUE;
15102 }
15103 else if (r_type == R_PPC64_REL24_NOTOC)
15104 {
15105 /* NOTOC calls don't need to restore r2. */
15106 can_plt_call = TRUE;
15107 }
15108
15109 /* All of these stubs may modify r2, so there must be a
15110 branch and link followed by a nop. The nop is
15111 replaced by an insn to restore r2. */
15112 else if (rel->r_offset + 8 <= input_section->size)
15113 {
15114 unsigned long br;
15115
15116 br = bfd_get_32 (input_bfd,
15117 contents + rel->r_offset);
15118 if ((br & 1) != 0)
15119 {
15120 unsigned long nop;
15121
15122 nop = bfd_get_32 (input_bfd,
15123 contents + rel->r_offset + 4);
15124 if (nop == LD_R2_0R1 + STK_TOC (htab))
15125 can_plt_call = TRUE;
15126 else if (nop == NOP
15127 || nop == CROR_151515
15128 || nop == CROR_313131)
15129 {
15130 if (h != NULL
15131 && (h == htab->tls_get_addr_fd
15132 || h == htab->tls_get_addr)
15133 && htab->params->tls_get_addr_opt)
15134 {
15135 /* Special stub used, leave nop alone. */
15136 }
15137 else
15138 bfd_put_32 (input_bfd,
15139 LD_R2_0R1 + STK_TOC (htab),
15140 contents + rel->r_offset + 4);
15141 can_plt_call = TRUE;
15142 }
15143 }
15144 }
15145
15146 if (!can_plt_call && h != NULL)
15147 {
15148 const char *name = h->elf.root.root.string;
15149
15150 if (*name == '.')
15151 ++name;
15152
15153 if (strncmp (name, "__libc_start_main", 17) == 0
15154 && (name[17] == 0 || name[17] == '@'))
15155 {
15156 /* Allow crt1 branch to go via a toc adjusting
15157 stub. Other calls that never return could do
15158 the same, if we could detect such. */
15159 can_plt_call = TRUE;
15160 }
15161 }
15162
15163 if (!can_plt_call)
15164 {
15165 /* g++ as of 20130507 emits self-calls without a
15166 following nop. This is arguably wrong since we
15167 have conflicting information. On the one hand a
15168 global symbol and on the other a local call
15169 sequence, but don't error for this special case.
15170 It isn't possible to cheaply verify we have
15171 exactly such a call. Allow all calls to the same
15172 section. */
15173 asection *code_sec = sec;
15174
15175 if (get_opd_info (sec) != NULL)
15176 {
15177 bfd_vma off = (relocation + addend
15178 - sec->output_section->vma
15179 - sec->output_offset);
15180
15181 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
15182 }
15183 if (code_sec == input_section)
15184 can_plt_call = TRUE;
15185 }
15186
15187 if (!can_plt_call)
15188 {
15189 if (stub_entry->stub_type >= ppc_stub_plt_call
15190 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15191 info->callbacks->einfo
15192 /* xgettext:c-format */
15193 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15194 "(plt call stub)\n"),
15195 input_bfd, input_section, rel->r_offset, sym_name);
15196 else
15197 info->callbacks->einfo
15198 /* xgettext:c-format */
15199 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15200 "(toc save/adjust stub)\n"),
15201 input_bfd, input_section, rel->r_offset, sym_name);
15202
15203 bfd_set_error (bfd_error_bad_value);
15204 ret = FALSE;
15205 }
15206
15207 if (can_plt_call
15208 && stub_entry->stub_type >= ppc_stub_plt_call
15209 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15210 unresolved_reloc = FALSE;
15211 }
15212
15213 if ((stub_entry == NULL
15214 || stub_entry->stub_type == ppc_stub_long_branch
15215 || stub_entry->stub_type == ppc_stub_plt_branch)
15216 && get_opd_info (sec) != NULL)
15217 {
15218 /* The branch destination is the value of the opd entry. */
15219 bfd_vma off = (relocation + addend
15220 - sec->output_section->vma
15221 - sec->output_offset);
15222 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
15223 if (dest != (bfd_vma) -1)
15224 {
15225 relocation = dest;
15226 addend = 0;
15227 reloc_dest = DEST_OPD;
15228 }
15229 }
15230
15231 /* If the branch is out of reach we ought to have a long
15232 branch stub. */
15233 from = (rel->r_offset
15234 + input_section->output_offset
15235 + input_section->output_section->vma);
15236
15237 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15238 ? fdh->elf.other
15239 : sym->st_other);
15240
15241 if (stub_entry != NULL
15242 && (stub_entry->stub_type == ppc_stub_long_branch
15243 || stub_entry->stub_type == ppc_stub_plt_branch)
15244 && (r_type == R_PPC64_ADDR14_BRTAKEN
15245 || r_type == R_PPC64_ADDR14_BRNTAKEN
15246 || (relocation + addend - from + max_br_offset
15247 < 2 * max_br_offset)))
15248 /* Don't use the stub if this branch is in range. */
15249 stub_entry = NULL;
15250
15251 if (stub_entry != NULL
15252 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15253 || stub_entry->stub_type == ppc_stub_long_branch_both
15254 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15255 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15256 && (r_type != R_PPC64_REL24_NOTOC
15257 || ((fdh ? fdh->elf.other : sym->st_other)
15258 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
15259 && (relocation + addend - from + max_br_offset
15260 < 2 * max_br_offset))
15261 stub_entry = NULL;
15262
15263 if (stub_entry != NULL
15264 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15265 || stub_entry->stub_type == ppc_stub_long_branch_both
15266 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15267 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15268 && r_type == R_PPC64_REL24_NOTOC
15269 && (relocation + addend - from + max_br_offset
15270 < 2 * max_br_offset))
15271 stub_entry = NULL;
15272
15273 if (stub_entry != NULL)
15274 {
15275 /* Munge up the value and addend so that we call the stub
15276 rather than the procedure directly. */
15277 asection *stub_sec = stub_entry->group->stub_sec;
15278
15279 if (stub_entry->stub_type == ppc_stub_save_res)
15280 relocation += (stub_sec->output_offset
15281 + stub_sec->output_section->vma
15282 + stub_sec->size - htab->sfpr->size
15283 - htab->sfpr->output_offset
15284 - htab->sfpr->output_section->vma);
15285 else
15286 relocation = (stub_entry->stub_offset
15287 + stub_sec->output_offset
15288 + stub_sec->output_section->vma);
15289 addend = 0;
15290 reloc_dest = DEST_STUB;
15291
15292 if (((stub_entry->stub_type == ppc_stub_plt_call
15293 && ALWAYS_EMIT_R2SAVE)
15294 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15295 || stub_entry->stub_type == ppc_stub_plt_call_both)
15296 && !(h != NULL
15297 && (h == htab->tls_get_addr_fd
15298 || h == htab->tls_get_addr)
15299 && htab->params->tls_get_addr_opt)
15300 && rel + 1 < relend
15301 && rel[1].r_offset == rel->r_offset + 4
15302 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15303 relocation += 4;
15304 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15305 || stub_entry->stub_type == ppc_stub_plt_branch_both
15306 || stub_entry->stub_type == ppc_stub_plt_call_both)
15307 && r_type == R_PPC64_REL24_NOTOC)
15308 relocation += 4;
15309
15310 if (r_type == R_PPC64_REL24_NOTOC
15311 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
15312 || stub_entry->stub_type == ppc_stub_plt_call_both))
15313 htab->notoc_plt = 1;
15314 }
15315
15316 if (insn != 0)
15317 {
15318 if (is_isa_v2)
15319 {
15320 /* Set 'a' bit. This is 0b00010 in BO field for branch
15321 on CR(BI) insns (BO == 001at or 011at), and 0b01000
15322 for branch on CTR insns (BO == 1a00t or 1a01t). */
15323 if ((insn & (0x14 << 21)) == (0x04 << 21))
15324 insn |= 0x02 << 21;
15325 else if ((insn & (0x14 << 21)) == (0x10 << 21))
15326 insn |= 0x08 << 21;
15327 else
15328 break;
15329 }
15330 else
15331 {
15332 /* Invert 'y' bit if not the default. */
15333 if ((bfd_signed_vma) (relocation + addend - from) < 0)
15334 insn ^= 0x01 << 21;
15335 }
15336
15337 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15338 }
15339
15340 /* NOP out calls to undefined weak functions.
15341 We can thus call a weak function without first
15342 checking whether the function is defined. */
15343 else if (h != NULL
15344 && h->elf.root.type == bfd_link_hash_undefweak
15345 && h->elf.dynindx == -1
15346 && (r_type == R_PPC64_REL24
15347 || r_type == R_PPC64_REL24_NOTOC)
15348 && relocation == 0
15349 && addend == 0)
15350 {
15351 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15352 goto copy_reloc;
15353 }
15354 break;
15355
15356 case R_PPC64_GOT16_DS:
15357 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15358 if (relocation + addend - from + 0x8000 < 0x10000
15359 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15360 {
15361 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15362 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15363 {
15364 insn += (14u << 26) - (58u << 26);
15365 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15366 r_type = R_PPC64_TOC16;
15367 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15368 }
15369 }
15370 break;
15371
15372 case R_PPC64_GOT16_LO_DS:
15373 case R_PPC64_GOT16_HA:
15374 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15375 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
15376 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15377 {
15378 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15379 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15380 {
15381 insn += (14u << 26) - (58u << 26);
15382 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15383 r_type = R_PPC64_TOC16_LO;
15384 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15385 }
15386 else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
15387 {
15388 r_type = R_PPC64_TOC16_HA;
15389 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15390 }
15391 }
15392 break;
15393
15394 case R_PPC64_GOT_PCREL34:
15395 from = (rel->r_offset
15396 + input_section->output_section->vma
15397 + input_section->output_offset);
15398 if (relocation - from + (1ULL << 33) < 1ULL << 34
15399 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15400 {
15401 offset = rel->r_offset;
15402 pinsn = bfd_get_32 (input_bfd, contents + offset);
15403 pinsn <<= 32;
15404 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15405 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15406 == ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
15407 {
15408 /* Replace with paddi. */
15409 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
15410 r_type = R_PPC64_PCREL34;
15411 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15412 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
15413 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
15414 goto pcrelopt;
15415 }
15416 }
15417 break;
15418
15419 case R_PPC64_PCREL34:
15420 if (SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15421 {
15422 offset = rel->r_offset;
15423 pinsn = bfd_get_32 (input_bfd, contents + offset);
15424 pinsn <<= 32;
15425 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15426 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15427 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
15428 | (14ULL << 26) /* paddi */))
15429 {
15430 pcrelopt:
15431 if (rel + 1 < relend
15432 && rel[1].r_offset == offset
15433 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT))
15434 {
15435 bfd_vma off2 = rel[1].r_addend;
15436 if (off2 == 0)
15437 /* zero means next insn. */
15438 off2 = 8;
15439 off2 += offset;
15440 if (off2 + 4 <= input_section->size)
15441 {
15442 uint64_t pinsn2;
15443 bfd_signed_vma addend_off;
15444 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
15445 pinsn2 <<= 32;
15446 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15447 {
15448 if (off2 + 8 > input_section->size)
15449 break;
15450 pinsn2 |= bfd_get_32 (input_bfd,
15451 contents + off2 + 4);
15452 }
15453 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
15454 {
15455 addend += addend_off;
15456 rel->r_addend = addend;
15457 bfd_put_32 (input_bfd, pinsn >> 32,
15458 contents + offset);
15459 bfd_put_32 (input_bfd, pinsn,
15460 contents + offset + 4);
15461 bfd_put_32 (input_bfd, pinsn2 >> 32,
15462 contents + off2);
15463 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15464 bfd_put_32 (input_bfd, pinsn2,
15465 contents + off2 + 4);
15466 }
15467 }
15468 }
15469 }
15470 }
15471 break;
15472 }
15473
15474 tls_type = 0;
15475 save_unresolved_reloc = unresolved_reloc;
15476 switch (r_type)
15477 {
15478 default:
15479 /* xgettext:c-format */
15480 _bfd_error_handler (_("%pB: %s unsupported"),
15481 input_bfd, ppc64_elf_howto_table[r_type]->name);
15482
15483 bfd_set_error (bfd_error_bad_value);
15484 ret = FALSE;
15485 goto copy_reloc;
15486
15487 case R_PPC64_NONE:
15488 case R_PPC64_TLS:
15489 case R_PPC64_TLSGD:
15490 case R_PPC64_TLSLD:
15491 case R_PPC64_TOCSAVE:
15492 case R_PPC64_GNU_VTINHERIT:
15493 case R_PPC64_GNU_VTENTRY:
15494 case R_PPC64_ENTRY:
15495 case R_PPC64_PCREL_OPT:
15496 goto copy_reloc;
15497
15498 /* GOT16 relocations. Like an ADDR16 using the symbol's
15499 address in the GOT as relocation value instead of the
15500 symbol's value itself. Also, create a GOT entry for the
15501 symbol and put the symbol value there. */
15502 case R_PPC64_GOT_TLSGD16:
15503 case R_PPC64_GOT_TLSGD16_LO:
15504 case R_PPC64_GOT_TLSGD16_HI:
15505 case R_PPC64_GOT_TLSGD16_HA:
15506 case R_PPC64_GOT_TLSGD34:
15507 tls_type = TLS_TLS | TLS_GD;
15508 goto dogot;
15509
15510 case R_PPC64_GOT_TLSLD16:
15511 case R_PPC64_GOT_TLSLD16_LO:
15512 case R_PPC64_GOT_TLSLD16_HI:
15513 case R_PPC64_GOT_TLSLD16_HA:
15514 case R_PPC64_GOT_TLSLD34:
15515 tls_type = TLS_TLS | TLS_LD;
15516 goto dogot;
15517
15518 case R_PPC64_GOT_TPREL16_DS:
15519 case R_PPC64_GOT_TPREL16_LO_DS:
15520 case R_PPC64_GOT_TPREL16_HI:
15521 case R_PPC64_GOT_TPREL16_HA:
15522 case R_PPC64_GOT_TPREL34:
15523 tls_type = TLS_TLS | TLS_TPREL;
15524 goto dogot;
15525
15526 case R_PPC64_GOT_DTPREL16_DS:
15527 case R_PPC64_GOT_DTPREL16_LO_DS:
15528 case R_PPC64_GOT_DTPREL16_HI:
15529 case R_PPC64_GOT_DTPREL16_HA:
15530 case R_PPC64_GOT_DTPREL34:
15531 tls_type = TLS_TLS | TLS_DTPREL;
15532 goto dogot;
15533
15534 case R_PPC64_GOT16:
15535 case R_PPC64_GOT16_LO:
15536 case R_PPC64_GOT16_HI:
15537 case R_PPC64_GOT16_HA:
15538 case R_PPC64_GOT16_DS:
15539 case R_PPC64_GOT16_LO_DS:
15540 case R_PPC64_GOT_PCREL34:
15541 dogot:
15542 {
15543 /* Relocation is to the entry for this symbol in the global
15544 offset table. */
15545 asection *got;
15546 bfd_vma *offp;
15547 bfd_vma off;
15548 unsigned long indx = 0;
15549 struct got_entry *ent;
15550
15551 if (tls_type == (TLS_TLS | TLS_LD)
15552 && (h == NULL
15553 || !h->elf.def_dynamic))
15554 ent = ppc64_tlsld_got (input_bfd);
15555 else
15556 {
15557 if (h != NULL)
15558 {
15559 if (!htab->elf.dynamic_sections_created
15560 || h->elf.dynindx == -1
15561 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15562 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15563 /* This is actually a static link, or it is a
15564 -Bsymbolic link and the symbol is defined
15565 locally, or the symbol was forced to be local
15566 because of a version file. */
15567 ;
15568 else
15569 {
15570 indx = h->elf.dynindx;
15571 unresolved_reloc = FALSE;
15572 }
15573 ent = h->elf.got.glist;
15574 }
15575 else
15576 {
15577 if (local_got_ents == NULL)
15578 abort ();
15579 ent = local_got_ents[r_symndx];
15580 }
15581
15582 for (; ent != NULL; ent = ent->next)
15583 if (ent->addend == orig_rel.r_addend
15584 && ent->owner == input_bfd
15585 && ent->tls_type == tls_type)
15586 break;
15587 }
15588
15589 if (ent == NULL)
15590 abort ();
15591 if (ent->is_indirect)
15592 ent = ent->got.ent;
15593 offp = &ent->got.offset;
15594 got = ppc64_elf_tdata (ent->owner)->got;
15595 if (got == NULL)
15596 abort ();
15597
15598 /* The offset must always be a multiple of 8. We use the
15599 least significant bit to record whether we have already
15600 processed this entry. */
15601 off = *offp;
15602 if ((off & 1) != 0)
15603 off &= ~1;
15604 else
15605 {
15606 /* Generate relocs for the dynamic linker, except in
15607 the case of TLSLD where we'll use one entry per
15608 module. */
15609 asection *relgot;
15610 bfd_boolean ifunc;
15611
15612 *offp = off | 1;
15613 relgot = NULL;
15614 ifunc = (h != NULL
15615 ? h->elf.type == STT_GNU_IFUNC
15616 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
15617 if (ifunc)
15618 {
15619 relgot = htab->elf.irelplt;
15620 if (indx == 0)
15621 htab->local_ifunc_resolver = 1;
15622 else if (is_static_defined (&h->elf))
15623 htab->maybe_local_ifunc_resolver = 1;
15624 }
15625 else if (indx != 0
15626 || (bfd_link_pic (info)
15627 && (h == NULL
15628 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
15629 || (tls_type == (TLS_TLS | TLS_LD)
15630 && !h->elf.def_dynamic))
15631 && !(tls_type == (TLS_TLS | TLS_TPREL)
15632 && bfd_link_executable (info)
15633 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
15634 relgot = ppc64_elf_tdata (ent->owner)->relgot;
15635 if (relgot != NULL)
15636 {
15637 outrel.r_offset = (got->output_section->vma
15638 + got->output_offset
15639 + off);
15640 outrel.r_addend = orig_rel.r_addend;
15641 if (tls_type & (TLS_LD | TLS_GD))
15642 {
15643 outrel.r_addend = 0;
15644 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
15645 if (tls_type == (TLS_TLS | TLS_GD))
15646 {
15647 loc = relgot->contents;
15648 loc += (relgot->reloc_count++
15649 * sizeof (Elf64_External_Rela));
15650 bfd_elf64_swap_reloca_out (output_bfd,
15651 &outrel, loc);
15652 outrel.r_offset += 8;
15653 outrel.r_addend = orig_rel.r_addend;
15654 outrel.r_info
15655 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15656 }
15657 }
15658 else if (tls_type == (TLS_TLS | TLS_DTPREL))
15659 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15660 else if (tls_type == (TLS_TLS | TLS_TPREL))
15661 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15662 else if (indx != 0)
15663 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15664 else
15665 {
15666 if (ifunc)
15667 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15668 else
15669 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15670
15671 /* Write the .got section contents for the sake
15672 of prelink. */
15673 loc = got->contents + off;
15674 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15675 loc);
15676 }
15677
15678 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15679 {
15680 outrel.r_addend += relocation;
15681 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15682 {
15683 if (htab->elf.tls_sec == NULL)
15684 outrel.r_addend = 0;
15685 else
15686 outrel.r_addend -= htab->elf.tls_sec->vma;
15687 }
15688 }
15689 loc = relgot->contents;
15690 loc += (relgot->reloc_count++
15691 * sizeof (Elf64_External_Rela));
15692 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15693 }
15694
15695 /* Init the .got section contents here if we're not
15696 emitting a reloc. */
15697 else
15698 {
15699 relocation += orig_rel.r_addend;
15700 if (tls_type != 0)
15701 {
15702 if (htab->elf.tls_sec == NULL)
15703 relocation = 0;
15704 else
15705 {
15706 if (tls_type & TLS_LD)
15707 relocation = 0;
15708 else
15709 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15710 if (tls_type & TLS_TPREL)
15711 relocation += DTP_OFFSET - TP_OFFSET;
15712 }
15713
15714 if (tls_type & (TLS_GD | TLS_LD))
15715 {
15716 bfd_put_64 (output_bfd, relocation,
15717 got->contents + off + 8);
15718 relocation = 1;
15719 }
15720 }
15721 bfd_put_64 (output_bfd, relocation,
15722 got->contents + off);
15723 }
15724 }
15725
15726 if (off >= (bfd_vma) -2)
15727 abort ();
15728
15729 relocation = got->output_section->vma + got->output_offset + off;
15730 addend = 0;
15731 if (!(r_type == R_PPC64_GOT_PCREL34
15732 || r_type == R_PPC64_GOT_TLSGD34
15733 || r_type == R_PPC64_GOT_TLSLD34
15734 || r_type == R_PPC64_GOT_TPREL34
15735 || r_type == R_PPC64_GOT_DTPREL34))
15736 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15737 }
15738 break;
15739
15740 case R_PPC64_PLT16_HA:
15741 case R_PPC64_PLT16_HI:
15742 case R_PPC64_PLT16_LO:
15743 case R_PPC64_PLT16_LO_DS:
15744 case R_PPC64_PLT_PCREL34:
15745 case R_PPC64_PLT_PCREL34_NOTOC:
15746 case R_PPC64_PLT32:
15747 case R_PPC64_PLT64:
15748 case R_PPC64_PLTSEQ:
15749 case R_PPC64_PLTSEQ_NOTOC:
15750 case R_PPC64_PLTCALL:
15751 case R_PPC64_PLTCALL_NOTOC:
15752 /* Relocation is to the entry for this symbol in the
15753 procedure linkage table. */
15754 unresolved_reloc = TRUE;
15755 {
15756 struct plt_entry **plt_list = NULL;
15757 if (h != NULL)
15758 plt_list = &h->elf.plt.plist;
15759 else if (local_got_ents != NULL)
15760 {
15761 struct plt_entry **local_plt = (struct plt_entry **)
15762 (local_got_ents + symtab_hdr->sh_info);
15763 plt_list = local_plt + r_symndx;
15764 }
15765 if (plt_list)
15766 {
15767 struct plt_entry *ent;
15768
15769 for (ent = *plt_list; ent != NULL; ent = ent->next)
15770 if (ent->plt.offset != (bfd_vma) -1
15771 && ent->addend == orig_rel.r_addend)
15772 {
15773 asection *plt;
15774 bfd_vma got;
15775
15776 plt = htab->elf.splt;
15777 if (!htab->elf.dynamic_sections_created
15778 || h == NULL
15779 || h->elf.dynindx == -1)
15780 {
15781 if (h != NULL
15782 ? h->elf.type == STT_GNU_IFUNC
15783 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15784 plt = htab->elf.iplt;
15785 else
15786 plt = htab->pltlocal;
15787 }
15788 relocation = (plt->output_section->vma
15789 + plt->output_offset
15790 + ent->plt.offset);
15791 if (r_type == R_PPC64_PLT16_HA
15792 || r_type == R_PPC64_PLT16_HI
15793 || r_type == R_PPC64_PLT16_LO
15794 || r_type == R_PPC64_PLT16_LO_DS)
15795 {
15796 got = (elf_gp (output_bfd)
15797 + htab->sec_info[input_section->id].toc_off);
15798 relocation -= got;
15799 }
15800 addend = 0;
15801 unresolved_reloc = FALSE;
15802 break;
15803 }
15804 }
15805 }
15806 break;
15807
15808 case R_PPC64_TOC:
15809 /* Relocation value is TOC base. */
15810 relocation = TOCstart;
15811 if (r_symndx == STN_UNDEF)
15812 relocation += htab->sec_info[input_section->id].toc_off;
15813 else if (unresolved_reloc)
15814 ;
15815 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15816 relocation += htab->sec_info[sec->id].toc_off;
15817 else
15818 unresolved_reloc = TRUE;
15819 goto dodyn;
15820
15821 /* TOC16 relocs. We want the offset relative to the TOC base,
15822 which is the address of the start of the TOC plus 0x8000.
15823 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15824 in this order. */
15825 case R_PPC64_TOC16:
15826 case R_PPC64_TOC16_LO:
15827 case R_PPC64_TOC16_HI:
15828 case R_PPC64_TOC16_DS:
15829 case R_PPC64_TOC16_LO_DS:
15830 case R_PPC64_TOC16_HA:
15831 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15832 if (h != NULL)
15833 goto dodyn;
15834 break;
15835
15836 /* Relocate against the beginning of the section. */
15837 case R_PPC64_SECTOFF:
15838 case R_PPC64_SECTOFF_LO:
15839 case R_PPC64_SECTOFF_HI:
15840 case R_PPC64_SECTOFF_DS:
15841 case R_PPC64_SECTOFF_LO_DS:
15842 case R_PPC64_SECTOFF_HA:
15843 if (sec != NULL)
15844 addend -= sec->output_section->vma;
15845 break;
15846
15847 case R_PPC64_REL16:
15848 case R_PPC64_REL16_LO:
15849 case R_PPC64_REL16_HI:
15850 case R_PPC64_REL16_HA:
15851 case R_PPC64_REL16_HIGH:
15852 case R_PPC64_REL16_HIGHA:
15853 case R_PPC64_REL16_HIGHER:
15854 case R_PPC64_REL16_HIGHERA:
15855 case R_PPC64_REL16_HIGHEST:
15856 case R_PPC64_REL16_HIGHESTA:
15857 case R_PPC64_REL16_HIGHER34:
15858 case R_PPC64_REL16_HIGHERA34:
15859 case R_PPC64_REL16_HIGHEST34:
15860 case R_PPC64_REL16_HIGHESTA34:
15861 case R_PPC64_REL16DX_HA:
15862 case R_PPC64_REL14:
15863 case R_PPC64_REL14_BRNTAKEN:
15864 case R_PPC64_REL14_BRTAKEN:
15865 case R_PPC64_REL24:
15866 case R_PPC64_REL24_NOTOC:
15867 case R_PPC64_PCREL34:
15868 case R_PPC64_PCREL28:
15869 break;
15870
15871 case R_PPC64_TPREL16:
15872 case R_PPC64_TPREL16_LO:
15873 case R_PPC64_TPREL16_HI:
15874 case R_PPC64_TPREL16_HA:
15875 case R_PPC64_TPREL16_DS:
15876 case R_PPC64_TPREL16_LO_DS:
15877 case R_PPC64_TPREL16_HIGH:
15878 case R_PPC64_TPREL16_HIGHA:
15879 case R_PPC64_TPREL16_HIGHER:
15880 case R_PPC64_TPREL16_HIGHERA:
15881 case R_PPC64_TPREL16_HIGHEST:
15882 case R_PPC64_TPREL16_HIGHESTA:
15883 case R_PPC64_TPREL34:
15884 if (h != NULL
15885 && h->elf.root.type == bfd_link_hash_undefweak
15886 && h->elf.dynindx == -1)
15887 {
15888 /* Make this relocation against an undefined weak symbol
15889 resolve to zero. This is really just a tweak, since
15890 code using weak externs ought to check that they are
15891 defined before using them. */
15892 bfd_byte *p = contents + rel->r_offset - d_offset;
15893
15894 insn = bfd_get_32 (input_bfd, p);
15895 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15896 if (insn != 0)
15897 bfd_put_32 (input_bfd, insn, p);
15898 break;
15899 }
15900 if (htab->elf.tls_sec != NULL)
15901 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15902 /* The TPREL16 relocs shouldn't really be used in shared
15903 libs or with non-local symbols as that will result in
15904 DT_TEXTREL being set, but support them anyway. */
15905 goto dodyn;
15906
15907 case R_PPC64_DTPREL16:
15908 case R_PPC64_DTPREL16_LO:
15909 case R_PPC64_DTPREL16_HI:
15910 case R_PPC64_DTPREL16_HA:
15911 case R_PPC64_DTPREL16_DS:
15912 case R_PPC64_DTPREL16_LO_DS:
15913 case R_PPC64_DTPREL16_HIGH:
15914 case R_PPC64_DTPREL16_HIGHA:
15915 case R_PPC64_DTPREL16_HIGHER:
15916 case R_PPC64_DTPREL16_HIGHERA:
15917 case R_PPC64_DTPREL16_HIGHEST:
15918 case R_PPC64_DTPREL16_HIGHESTA:
15919 case R_PPC64_DTPREL34:
15920 if (htab->elf.tls_sec != NULL)
15921 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15922 break;
15923
15924 case R_PPC64_ADDR64_LOCAL:
15925 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15926 ? h->elf.other
15927 : sym->st_other);
15928 break;
15929
15930 case R_PPC64_DTPMOD64:
15931 relocation = 1;
15932 addend = 0;
15933 goto dodyn;
15934
15935 case R_PPC64_TPREL64:
15936 if (htab->elf.tls_sec != NULL)
15937 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15938 goto dodyn;
15939
15940 case R_PPC64_DTPREL64:
15941 if (htab->elf.tls_sec != NULL)
15942 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15943 /* Fall through. */
15944
15945 /* Relocations that may need to be propagated if this is a
15946 dynamic object. */
15947 case R_PPC64_REL30:
15948 case R_PPC64_REL32:
15949 case R_PPC64_REL64:
15950 case R_PPC64_ADDR14:
15951 case R_PPC64_ADDR14_BRNTAKEN:
15952 case R_PPC64_ADDR14_BRTAKEN:
15953 case R_PPC64_ADDR16:
15954 case R_PPC64_ADDR16_DS:
15955 case R_PPC64_ADDR16_HA:
15956 case R_PPC64_ADDR16_HI:
15957 case R_PPC64_ADDR16_HIGH:
15958 case R_PPC64_ADDR16_HIGHA:
15959 case R_PPC64_ADDR16_HIGHER:
15960 case R_PPC64_ADDR16_HIGHERA:
15961 case R_PPC64_ADDR16_HIGHEST:
15962 case R_PPC64_ADDR16_HIGHESTA:
15963 case R_PPC64_ADDR16_LO:
15964 case R_PPC64_ADDR16_LO_DS:
15965 case R_PPC64_ADDR16_HIGHER34:
15966 case R_PPC64_ADDR16_HIGHERA34:
15967 case R_PPC64_ADDR16_HIGHEST34:
15968 case R_PPC64_ADDR16_HIGHESTA34:
15969 case R_PPC64_ADDR24:
15970 case R_PPC64_ADDR32:
15971 case R_PPC64_ADDR64:
15972 case R_PPC64_UADDR16:
15973 case R_PPC64_UADDR32:
15974 case R_PPC64_UADDR64:
15975 case R_PPC64_D34:
15976 case R_PPC64_D34_LO:
15977 case R_PPC64_D34_HI30:
15978 case R_PPC64_D34_HA30:
15979 case R_PPC64_D28:
15980 dodyn:
15981 if ((input_section->flags & SEC_ALLOC) == 0)
15982 break;
15983
15984 if (NO_OPD_RELOCS && is_opd)
15985 break;
15986
15987 if (bfd_link_pic (info)
15988 ? ((h == NULL
15989 || h->dyn_relocs != NULL)
15990 && ((h != NULL && pc_dynrelocs (h))
15991 || must_be_dyn_reloc (info, r_type)))
15992 : (h != NULL
15993 ? h->dyn_relocs != NULL
15994 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15995 {
15996 bfd_boolean skip, relocate;
15997 asection *sreloc;
15998 bfd_vma out_off;
15999 long indx = 0;
16000
16001 /* When generating a dynamic object, these relocations
16002 are copied into the output file to be resolved at run
16003 time. */
16004
16005 skip = FALSE;
16006 relocate = FALSE;
16007
16008 out_off = _bfd_elf_section_offset (output_bfd, info,
16009 input_section, rel->r_offset);
16010 if (out_off == (bfd_vma) -1)
16011 skip = TRUE;
16012 else if (out_off == (bfd_vma) -2)
16013 skip = TRUE, relocate = TRUE;
16014 out_off += (input_section->output_section->vma
16015 + input_section->output_offset);
16016 outrel.r_offset = out_off;
16017 outrel.r_addend = rel->r_addend;
16018
16019 /* Optimize unaligned reloc use. */
16020 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
16021 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
16022 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
16023 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
16024 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
16025 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
16026 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16027 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16028 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16029
16030 if (skip)
16031 memset (&outrel, 0, sizeof outrel);
16032 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16033 && !is_opd
16034 && r_type != R_PPC64_TOC)
16035 {
16036 indx = h->elf.dynindx;
16037 BFD_ASSERT (indx != -1);
16038 outrel.r_info = ELF64_R_INFO (indx, r_type);
16039 }
16040 else
16041 {
16042 /* This symbol is local, or marked to become local,
16043 or this is an opd section reloc which must point
16044 at a local function. */
16045 outrel.r_addend += relocation;
16046 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
16047 {
16048 if (is_opd && h != NULL)
16049 {
16050 /* Lie about opd entries. This case occurs
16051 when building shared libraries and we
16052 reference a function in another shared
16053 lib. The same thing happens for a weak
16054 definition in an application that's
16055 overridden by a strong definition in a
16056 shared lib. (I believe this is a generic
16057 bug in binutils handling of weak syms.)
16058 In these cases we won't use the opd
16059 entry in this lib. */
16060 unresolved_reloc = FALSE;
16061 }
16062 if (!is_opd
16063 && r_type == R_PPC64_ADDR64
16064 && (h != NULL
16065 ? h->elf.type == STT_GNU_IFUNC
16066 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16067 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16068 else
16069 {
16070 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16071
16072 /* We need to relocate .opd contents for ld.so.
16073 Prelink also wants simple and consistent rules
16074 for relocs. This make all RELATIVE relocs have
16075 *r_offset equal to r_addend. */
16076 relocate = TRUE;
16077 }
16078 }
16079 else
16080 {
16081 if (h != NULL
16082 ? h->elf.type == STT_GNU_IFUNC
16083 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16084 {
16085 info->callbacks->einfo
16086 /* xgettext:c-format */
16087 (_("%H: %s for indirect "
16088 "function `%pT' unsupported\n"),
16089 input_bfd, input_section, rel->r_offset,
16090 ppc64_elf_howto_table[r_type]->name,
16091 sym_name);
16092 ret = FALSE;
16093 }
16094 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
16095 ;
16096 else if (sec == NULL || sec->owner == NULL)
16097 {
16098 bfd_set_error (bfd_error_bad_value);
16099 return FALSE;
16100 }
16101 else
16102 {
16103 asection *osec = sec->output_section;
16104
16105 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16106 {
16107 /* TLS symbol values are relative to the
16108 TLS segment. Dynamic relocations for
16109 local TLS symbols therefore can't be
16110 reduced to a relocation against their
16111 section symbol because it holds the
16112 address of the section, not a value
16113 relative to the TLS segment. We could
16114 change the .tdata dynamic section symbol
16115 to be zero value but STN_UNDEF works
16116 and is used elsewhere, eg. for TPREL64
16117 GOT relocs against local TLS symbols. */
16118 osec = htab->elf.tls_sec;
16119 indx = 0;
16120 }
16121 else
16122 {
16123 indx = elf_section_data (osec)->dynindx;
16124 if (indx == 0)
16125 {
16126 if ((osec->flags & SEC_READONLY) == 0
16127 && htab->elf.data_index_section != NULL)
16128 osec = htab->elf.data_index_section;
16129 else
16130 osec = htab->elf.text_index_section;
16131 indx = elf_section_data (osec)->dynindx;
16132 }
16133 BFD_ASSERT (indx != 0);
16134 }
16135
16136 /* We are turning this relocation into one
16137 against a section symbol, so subtract out
16138 the output section's address but not the
16139 offset of the input section in the output
16140 section. */
16141 outrel.r_addend -= osec->vma;
16142 }
16143
16144 outrel.r_info = ELF64_R_INFO (indx, r_type);
16145 }
16146 }
16147
16148 sreloc = elf_section_data (input_section)->sreloc;
16149 if (h != NULL
16150 ? h->elf.type == STT_GNU_IFUNC
16151 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16152 {
16153 sreloc = htab->elf.irelplt;
16154 if (indx == 0)
16155 htab->local_ifunc_resolver = 1;
16156 else if (is_static_defined (&h->elf))
16157 htab->maybe_local_ifunc_resolver = 1;
16158 }
16159 if (sreloc == NULL)
16160 abort ();
16161
16162 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16163 >= sreloc->size)
16164 abort ();
16165 loc = sreloc->contents;
16166 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
16167 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16168
16169 /* If this reloc is against an external symbol, it will
16170 be computed at runtime, so there's no need to do
16171 anything now. However, for the sake of prelink ensure
16172 that the section contents are a known value. */
16173 if (!relocate)
16174 {
16175 unresolved_reloc = FALSE;
16176 /* The value chosen here is quite arbitrary as ld.so
16177 ignores section contents except for the special
16178 case of .opd where the contents might be accessed
16179 before relocation. Choose zero, as that won't
16180 cause reloc overflow. */
16181 relocation = 0;
16182 addend = 0;
16183 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16184 to improve backward compatibility with older
16185 versions of ld. */
16186 if (r_type == R_PPC64_ADDR64)
16187 addend = outrel.r_addend;
16188 /* Adjust pc_relative relocs to have zero in *r_offset. */
16189 else if (ppc64_elf_howto_table[r_type]->pc_relative)
16190 addend = outrel.r_offset;
16191 }
16192 }
16193 break;
16194
16195 case R_PPC64_COPY:
16196 case R_PPC64_GLOB_DAT:
16197 case R_PPC64_JMP_SLOT:
16198 case R_PPC64_JMP_IREL:
16199 case R_PPC64_RELATIVE:
16200 /* We shouldn't ever see these dynamic relocs in relocatable
16201 files. */
16202 /* Fall through. */
16203
16204 case R_PPC64_PLTGOT16:
16205 case R_PPC64_PLTGOT16_DS:
16206 case R_PPC64_PLTGOT16_HA:
16207 case R_PPC64_PLTGOT16_HI:
16208 case R_PPC64_PLTGOT16_LO:
16209 case R_PPC64_PLTGOT16_LO_DS:
16210 case R_PPC64_PLTREL32:
16211 case R_PPC64_PLTREL64:
16212 /* These ones haven't been implemented yet. */
16213
16214 info->callbacks->einfo
16215 /* xgettext:c-format */
16216 (_("%P: %pB: %s is not supported for `%pT'\n"),
16217 input_bfd,
16218 ppc64_elf_howto_table[r_type]->name, sym_name);
16219
16220 bfd_set_error (bfd_error_invalid_operation);
16221 ret = FALSE;
16222 goto copy_reloc;
16223 }
16224
16225 /* Multi-instruction sequences that access the TOC can be
16226 optimized, eg. addis ra,r2,0; addi rb,ra,x;
16227 to nop; addi rb,r2,x; */
16228 switch (r_type)
16229 {
16230 default:
16231 break;
16232
16233 case R_PPC64_GOT_TLSLD16_HI:
16234 case R_PPC64_GOT_TLSGD16_HI:
16235 case R_PPC64_GOT_TPREL16_HI:
16236 case R_PPC64_GOT_DTPREL16_HI:
16237 case R_PPC64_GOT16_HI:
16238 case R_PPC64_TOC16_HI:
16239 /* These relocs would only be useful if building up an
16240 offset to later add to r2, perhaps in an indexed
16241 addressing mode instruction. Don't try to optimize.
16242 Unfortunately, the possibility of someone building up an
16243 offset like this or even with the HA relocs, means that
16244 we need to check the high insn when optimizing the low
16245 insn. */
16246 break;
16247
16248 case R_PPC64_PLTCALL_NOTOC:
16249 if (!unresolved_reloc)
16250 htab->notoc_plt = 1;
16251 /* Fall through. */
16252 case R_PPC64_PLTCALL:
16253 if (unresolved_reloc)
16254 {
16255 /* No plt entry. Make this into a direct call. */
16256 bfd_byte *p = contents + rel->r_offset;
16257 insn = bfd_get_32 (input_bfd, p);
16258 insn &= 1;
16259 bfd_put_32 (input_bfd, B_DOT | insn, p);
16260 if (r_type == R_PPC64_PLTCALL)
16261 bfd_put_32 (input_bfd, NOP, p + 4);
16262 unresolved_reloc = save_unresolved_reloc;
16263 r_type = R_PPC64_REL24;
16264 }
16265 break;
16266
16267 case R_PPC64_PLTSEQ_NOTOC:
16268 case R_PPC64_PLTSEQ:
16269 if (unresolved_reloc)
16270 {
16271 unresolved_reloc = FALSE;
16272 goto nop_it;
16273 }
16274 break;
16275
16276 case R_PPC64_PLT_PCREL34_NOTOC:
16277 if (!unresolved_reloc)
16278 htab->notoc_plt = 1;
16279 /* Fall through. */
16280 case R_PPC64_PLT_PCREL34:
16281 if (unresolved_reloc)
16282 {
16283 bfd_byte *p = contents + rel->r_offset;
16284 bfd_put_32 (input_bfd, PNOP >> 32, p);
16285 bfd_put_32 (input_bfd, PNOP, p + 4);
16286 unresolved_reloc = FALSE;
16287 goto copy_reloc;
16288 }
16289 break;
16290
16291 case R_PPC64_PLT16_HA:
16292 if (unresolved_reloc)
16293 {
16294 unresolved_reloc = FALSE;
16295 goto nop_it;
16296 }
16297 /* Fall through. */
16298 case R_PPC64_GOT_TLSLD16_HA:
16299 case R_PPC64_GOT_TLSGD16_HA:
16300 case R_PPC64_GOT_TPREL16_HA:
16301 case R_PPC64_GOT_DTPREL16_HA:
16302 case R_PPC64_GOT16_HA:
16303 case R_PPC64_TOC16_HA:
16304 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16305 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16306 {
16307 bfd_byte *p;
16308 nop_it:
16309 p = contents + (rel->r_offset & ~3);
16310 bfd_put_32 (input_bfd, NOP, p);
16311 goto copy_reloc;
16312 }
16313 break;
16314
16315 case R_PPC64_PLT16_LO:
16316 case R_PPC64_PLT16_LO_DS:
16317 if (unresolved_reloc)
16318 {
16319 unresolved_reloc = FALSE;
16320 goto nop_it;
16321 }
16322 /* Fall through. */
16323 case R_PPC64_GOT_TLSLD16_LO:
16324 case R_PPC64_GOT_TLSGD16_LO:
16325 case R_PPC64_GOT_TPREL16_LO_DS:
16326 case R_PPC64_GOT_DTPREL16_LO_DS:
16327 case R_PPC64_GOT16_LO:
16328 case R_PPC64_GOT16_LO_DS:
16329 case R_PPC64_TOC16_LO:
16330 case R_PPC64_TOC16_LO_DS:
16331 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16332 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16333 {
16334 bfd_byte *p = contents + (rel->r_offset & ~3);
16335 insn = bfd_get_32 (input_bfd, p);
16336 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16337 {
16338 /* Transform addic to addi when we change reg. */
16339 insn &= ~((0x3f << 26) | (0x1f << 16));
16340 insn |= (14u << 26) | (2 << 16);
16341 }
16342 else
16343 {
16344 insn &= ~(0x1f << 16);
16345 insn |= 2 << 16;
16346 }
16347 bfd_put_32 (input_bfd, insn, p);
16348 }
16349 break;
16350
16351 case R_PPC64_TPREL16_HA:
16352 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16353 {
16354 bfd_byte *p = contents + (rel->r_offset & ~3);
16355 insn = bfd_get_32 (input_bfd, p);
16356 if ((insn & ((0x3f << 26) | 0x1f << 16))
16357 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16358 /* xgettext:c-format */
16359 info->callbacks->minfo
16360 (_("%H: warning: %s unexpected insn %#x.\n"),
16361 input_bfd, input_section, rel->r_offset,
16362 ppc64_elf_howto_table[r_type]->name, insn);
16363 else
16364 {
16365 bfd_put_32 (input_bfd, NOP, p);
16366 goto copy_reloc;
16367 }
16368 }
16369 break;
16370
16371 case R_PPC64_TPREL16_LO:
16372 case R_PPC64_TPREL16_LO_DS:
16373 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16374 {
16375 bfd_byte *p = contents + (rel->r_offset & ~3);
16376 insn = bfd_get_32 (input_bfd, p);
16377 insn &= ~(0x1f << 16);
16378 insn |= 13 << 16;
16379 bfd_put_32 (input_bfd, insn, p);
16380 }
16381 break;
16382 }
16383
16384 /* Do any further special processing. */
16385 switch (r_type)
16386 {
16387 default:
16388 break;
16389
16390 case R_PPC64_REL16_HA:
16391 case R_PPC64_REL16_HIGHA:
16392 case R_PPC64_REL16_HIGHERA:
16393 case R_PPC64_REL16_HIGHESTA:
16394 case R_PPC64_REL16DX_HA:
16395 case R_PPC64_ADDR16_HA:
16396 case R_PPC64_ADDR16_HIGHA:
16397 case R_PPC64_ADDR16_HIGHERA:
16398 case R_PPC64_ADDR16_HIGHESTA:
16399 case R_PPC64_TOC16_HA:
16400 case R_PPC64_SECTOFF_HA:
16401 case R_PPC64_TPREL16_HA:
16402 case R_PPC64_TPREL16_HIGHA:
16403 case R_PPC64_TPREL16_HIGHERA:
16404 case R_PPC64_TPREL16_HIGHESTA:
16405 case R_PPC64_DTPREL16_HA:
16406 case R_PPC64_DTPREL16_HIGHA:
16407 case R_PPC64_DTPREL16_HIGHERA:
16408 case R_PPC64_DTPREL16_HIGHESTA:
16409 /* It's just possible that this symbol is a weak symbol
16410 that's not actually defined anywhere. In that case,
16411 'sec' would be NULL, and we should leave the symbol
16412 alone (it will be set to zero elsewhere in the link). */
16413 if (sec == NULL)
16414 break;
16415 /* Fall through. */
16416
16417 case R_PPC64_GOT16_HA:
16418 case R_PPC64_PLTGOT16_HA:
16419 case R_PPC64_PLT16_HA:
16420 case R_PPC64_GOT_TLSGD16_HA:
16421 case R_PPC64_GOT_TLSLD16_HA:
16422 case R_PPC64_GOT_TPREL16_HA:
16423 case R_PPC64_GOT_DTPREL16_HA:
16424 /* Add 0x10000 if sign bit in 0:15 is set.
16425 Bits 0:15 are not used. */
16426 addend += 0x8000;
16427 break;
16428
16429 case R_PPC64_D34_HA30:
16430 case R_PPC64_ADDR16_HIGHERA34:
16431 case R_PPC64_ADDR16_HIGHESTA34:
16432 case R_PPC64_REL16_HIGHERA34:
16433 case R_PPC64_REL16_HIGHESTA34:
16434 if (sec != NULL)
16435 addend += 1ULL << 33;
16436 break;
16437
16438 case R_PPC64_ADDR16_DS:
16439 case R_PPC64_ADDR16_LO_DS:
16440 case R_PPC64_GOT16_DS:
16441 case R_PPC64_GOT16_LO_DS:
16442 case R_PPC64_PLT16_LO_DS:
16443 case R_PPC64_SECTOFF_DS:
16444 case R_PPC64_SECTOFF_LO_DS:
16445 case R_PPC64_TOC16_DS:
16446 case R_PPC64_TOC16_LO_DS:
16447 case R_PPC64_PLTGOT16_DS:
16448 case R_PPC64_PLTGOT16_LO_DS:
16449 case R_PPC64_GOT_TPREL16_DS:
16450 case R_PPC64_GOT_TPREL16_LO_DS:
16451 case R_PPC64_GOT_DTPREL16_DS:
16452 case R_PPC64_GOT_DTPREL16_LO_DS:
16453 case R_PPC64_TPREL16_DS:
16454 case R_PPC64_TPREL16_LO_DS:
16455 case R_PPC64_DTPREL16_DS:
16456 case R_PPC64_DTPREL16_LO_DS:
16457 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16458 mask = 3;
16459 /* If this reloc is against an lq, lxv, or stxv insn, then
16460 the value must be a multiple of 16. This is somewhat of
16461 a hack, but the "correct" way to do this by defining _DQ
16462 forms of all the _DS relocs bloats all reloc switches in
16463 this file. It doesn't make much sense to use these
16464 relocs in data, so testing the insn should be safe. */
16465 if ((insn & (0x3f << 26)) == (56u << 26)
16466 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
16467 mask = 15;
16468 relocation += addend;
16469 addend = insn & (mask ^ 3);
16470 if ((relocation & mask) != 0)
16471 {
16472 relocation ^= relocation & mask;
16473 info->callbacks->einfo
16474 /* xgettext:c-format */
16475 (_("%H: error: %s not a multiple of %u\n"),
16476 input_bfd, input_section, rel->r_offset,
16477 ppc64_elf_howto_table[r_type]->name,
16478 mask + 1);
16479 bfd_set_error (bfd_error_bad_value);
16480 ret = FALSE;
16481 goto copy_reloc;
16482 }
16483 break;
16484 }
16485
16486 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16487 because such sections are not SEC_ALLOC and thus ld.so will
16488 not process them. */
16489 howto = ppc64_elf_howto_table[(int) r_type];
16490 if (unresolved_reloc
16491 && !((input_section->flags & SEC_DEBUGGING) != 0
16492 && h->elf.def_dynamic)
16493 && _bfd_elf_section_offset (output_bfd, info, input_section,
16494 rel->r_offset) != (bfd_vma) -1)
16495 {
16496 info->callbacks->einfo
16497 /* xgettext:c-format */
16498 (_("%H: unresolvable %s against `%pT'\n"),
16499 input_bfd, input_section, rel->r_offset,
16500 howto->name,
16501 h->elf.root.root.string);
16502 ret = FALSE;
16503 }
16504
16505 /* 16-bit fields in insns mostly have signed values, but a
16506 few insns have 16-bit unsigned values. Really, we should
16507 have different reloc types. */
16508 if (howto->complain_on_overflow != complain_overflow_dont
16509 && howto->dst_mask == 0xffff
16510 && (input_section->flags & SEC_CODE) != 0)
16511 {
16512 enum complain_overflow complain = complain_overflow_signed;
16513
16514 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16515 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16516 complain = complain_overflow_bitfield;
16517 else if (howto->rightshift == 0
16518 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16519 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16520 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16521 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16522 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16523 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
16524 complain = complain_overflow_unsigned;
16525 if (howto->complain_on_overflow != complain)
16526 {
16527 alt_howto = *howto;
16528 alt_howto.complain_on_overflow = complain;
16529 howto = &alt_howto;
16530 }
16531 }
16532
16533 switch (r_type)
16534 {
16535 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
16536 case R_PPC64_D34:
16537 case R_PPC64_D34_LO:
16538 case R_PPC64_D34_HI30:
16539 case R_PPC64_D34_HA30:
16540 case R_PPC64_PCREL34:
16541 case R_PPC64_GOT_PCREL34:
16542 case R_PPC64_TPREL34:
16543 case R_PPC64_DTPREL34:
16544 case R_PPC64_GOT_TLSGD34:
16545 case R_PPC64_GOT_TLSLD34:
16546 case R_PPC64_GOT_TPREL34:
16547 case R_PPC64_GOT_DTPREL34:
16548 case R_PPC64_PLT_PCREL34:
16549 case R_PPC64_PLT_PCREL34_NOTOC:
16550 case R_PPC64_D28:
16551 case R_PPC64_PCREL28:
16552 if (rel->r_offset + 8 > input_section->size)
16553 r = bfd_reloc_outofrange;
16554 else
16555 {
16556 relocation += addend;
16557 if (howto->pc_relative)
16558 relocation -= (rel->r_offset
16559 + input_section->output_offset
16560 + input_section->output_section->vma);
16561 relocation >>= howto->rightshift;
16562
16563 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16564 pinsn <<= 32;
16565 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16566
16567 pinsn &= ~howto->dst_mask;
16568 pinsn |= (((relocation << 16) | (relocation & 0xffff))
16569 & howto->dst_mask);
16570 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
16571 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
16572 r = bfd_reloc_ok;
16573 if (howto->complain_on_overflow == complain_overflow_signed
16574 && (relocation + (1ULL << (howto->bitsize - 1))
16575 >= 1ULL << howto->bitsize))
16576 r = bfd_reloc_overflow;
16577 }
16578 break;
16579
16580 case R_PPC64_REL16DX_HA:
16581 if (rel->r_offset + 4 > input_section->size)
16582 r = bfd_reloc_outofrange;
16583 else
16584 {
16585 relocation += addend;
16586 relocation -= (rel->r_offset
16587 + input_section->output_offset
16588 + input_section->output_section->vma);
16589 relocation = (bfd_signed_vma) relocation >> 16;
16590 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16591 insn &= ~0x1fffc1;
16592 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
16593 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16594 r = bfd_reloc_ok;
16595 if (relocation + 0x8000 > 0xffff)
16596 r = bfd_reloc_overflow;
16597 }
16598 break;
16599
16600 default:
16601 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
16602 contents, rel->r_offset,
16603 relocation, addend);
16604 }
16605
16606 if (r != bfd_reloc_ok)
16607 {
16608 char *more_info = NULL;
16609 const char *reloc_name = howto->name;
16610
16611 if (reloc_dest != DEST_NORMAL)
16612 {
16613 more_info = bfd_malloc (strlen (reloc_name) + 8);
16614 if (more_info != NULL)
16615 {
16616 strcpy (more_info, reloc_name);
16617 strcat (more_info, (reloc_dest == DEST_OPD
16618 ? " (OPD)" : " (stub)"));
16619 reloc_name = more_info;
16620 }
16621 }
16622
16623 if (r == bfd_reloc_overflow)
16624 {
16625 /* On code like "if (foo) foo();" don't report overflow
16626 on a branch to zero when foo is undefined. */
16627 if (!warned
16628 && (reloc_dest == DEST_STUB
16629 || !(h != NULL
16630 && (h->elf.root.type == bfd_link_hash_undefweak
16631 || h->elf.root.type == bfd_link_hash_undefined)
16632 && is_branch_reloc (r_type))))
16633 info->callbacks->reloc_overflow (info, &h->elf.root,
16634 sym_name, reloc_name,
16635 orig_rel.r_addend,
16636 input_bfd, input_section,
16637 rel->r_offset);
16638 }
16639 else
16640 {
16641 info->callbacks->einfo
16642 /* xgettext:c-format */
16643 (_("%H: %s against `%pT': error %d\n"),
16644 input_bfd, input_section, rel->r_offset,
16645 reloc_name, sym_name, (int) r);
16646 ret = FALSE;
16647 }
16648 if (more_info != NULL)
16649 free (more_info);
16650 }
16651 copy_reloc:
16652 if (wrel != rel)
16653 *wrel = *rel;
16654 }
16655
16656 if (wrel != rel)
16657 {
16658 Elf_Internal_Shdr *rel_hdr;
16659 size_t deleted = rel - wrel;
16660
16661 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16662 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16663 if (rel_hdr->sh_size == 0)
16664 {
16665 /* It is too late to remove an empty reloc section. Leave
16666 one NONE reloc.
16667 ??? What is wrong with an empty section??? */
16668 rel_hdr->sh_size = rel_hdr->sh_entsize;
16669 deleted -= 1;
16670 }
16671 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16672 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16673 input_section->reloc_count -= deleted;
16674 }
16675
16676 /* If we're emitting relocations, then shortly after this function
16677 returns, reloc offsets and addends for this section will be
16678 adjusted. Worse, reloc symbol indices will be for the output
16679 file rather than the input. Save a copy of the relocs for
16680 opd_entry_value. */
16681 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
16682 {
16683 bfd_size_type amt;
16684 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16685 rel = bfd_alloc (input_bfd, amt);
16686 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16687 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
16688 if (rel == NULL)
16689 return FALSE;
16690 memcpy (rel, relocs, amt);
16691 }
16692 return ret;
16693 }
16694
16695 /* Adjust the value of any local symbols in opd sections. */
16696
16697 static int
16698 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16699 const char *name ATTRIBUTE_UNUSED,
16700 Elf_Internal_Sym *elfsym,
16701 asection *input_sec,
16702 struct elf_link_hash_entry *h)
16703 {
16704 struct _opd_sec_data *opd;
16705 long adjust;
16706 bfd_vma value;
16707
16708 if (h != NULL)
16709 return 1;
16710
16711 opd = get_opd_info (input_sec);
16712 if (opd == NULL || opd->adjust == NULL)
16713 return 1;
16714
16715 value = elfsym->st_value - input_sec->output_offset;
16716 if (!bfd_link_relocatable (info))
16717 value -= input_sec->output_section->vma;
16718
16719 adjust = opd->adjust[OPD_NDX (value)];
16720 if (adjust == -1)
16721 return 2;
16722
16723 elfsym->st_value += adjust;
16724 return 1;
16725 }
16726
16727 /* Finish up dynamic symbol handling. We set the contents of various
16728 dynamic sections here. */
16729
16730 static bfd_boolean
16731 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16732 struct bfd_link_info *info,
16733 struct elf_link_hash_entry *h,
16734 Elf_Internal_Sym *sym)
16735 {
16736 struct ppc_link_hash_table *htab;
16737 struct plt_entry *ent;
16738
16739 htab = ppc_hash_table (info);
16740 if (htab == NULL)
16741 return FALSE;
16742
16743 if (!htab->opd_abi && !h->def_regular)
16744 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16745 if (ent->plt.offset != (bfd_vma) -1)
16746 {
16747 /* Mark the symbol as undefined, rather than as
16748 defined in glink. Leave the value if there were
16749 any relocations where pointer equality matters
16750 (this is a clue for the dynamic linker, to make
16751 function pointer comparisons work between an
16752 application and shared library), otherwise set it
16753 to zero. */
16754 sym->st_shndx = SHN_UNDEF;
16755 if (!h->pointer_equality_needed)
16756 sym->st_value = 0;
16757 else if (!h->ref_regular_nonweak)
16758 {
16759 /* This breaks function pointer comparisons, but
16760 that is better than breaking tests for a NULL
16761 function pointer. */
16762 sym->st_value = 0;
16763 }
16764 break;
16765 }
16766
16767 if (h->needs_copy
16768 && (h->root.type == bfd_link_hash_defined
16769 || h->root.type == bfd_link_hash_defweak)
16770 && (h->root.u.def.section == htab->elf.sdynbss
16771 || h->root.u.def.section == htab->elf.sdynrelro))
16772 {
16773 /* This symbol needs a copy reloc. Set it up. */
16774 Elf_Internal_Rela rela;
16775 asection *srel;
16776 bfd_byte *loc;
16777
16778 if (h->dynindx == -1)
16779 abort ();
16780
16781 rela.r_offset = (h->root.u.def.value
16782 + h->root.u.def.section->output_section->vma
16783 + h->root.u.def.section->output_offset);
16784 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16785 rela.r_addend = 0;
16786 if (h->root.u.def.section == htab->elf.sdynrelro)
16787 srel = htab->elf.sreldynrelro;
16788 else
16789 srel = htab->elf.srelbss;
16790 loc = srel->contents;
16791 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16792 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16793 }
16794
16795 return TRUE;
16796 }
16797
16798 /* Used to decide how to sort relocs in an optimal manner for the
16799 dynamic linker, before writing them out. */
16800
16801 static enum elf_reloc_type_class
16802 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16803 const asection *rel_sec,
16804 const Elf_Internal_Rela *rela)
16805 {
16806 enum elf_ppc64_reloc_type r_type;
16807 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16808
16809 if (rel_sec == htab->elf.irelplt)
16810 return reloc_class_ifunc;
16811
16812 r_type = ELF64_R_TYPE (rela->r_info);
16813 switch (r_type)
16814 {
16815 case R_PPC64_RELATIVE:
16816 return reloc_class_relative;
16817 case R_PPC64_JMP_SLOT:
16818 return reloc_class_plt;
16819 case R_PPC64_COPY:
16820 return reloc_class_copy;
16821 default:
16822 return reloc_class_normal;
16823 }
16824 }
16825
16826 /* Finish up the dynamic sections. */
16827
16828 static bfd_boolean
16829 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16830 struct bfd_link_info *info)
16831 {
16832 struct ppc_link_hash_table *htab;
16833 bfd *dynobj;
16834 asection *sdyn;
16835
16836 htab = ppc_hash_table (info);
16837 if (htab == NULL)
16838 return FALSE;
16839
16840 dynobj = htab->elf.dynobj;
16841 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16842
16843 if (htab->elf.dynamic_sections_created)
16844 {
16845 Elf64_External_Dyn *dyncon, *dynconend;
16846
16847 if (sdyn == NULL || htab->elf.sgot == NULL)
16848 abort ();
16849
16850 dyncon = (Elf64_External_Dyn *) sdyn->contents;
16851 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16852 for (; dyncon < dynconend; dyncon++)
16853 {
16854 Elf_Internal_Dyn dyn;
16855 asection *s;
16856
16857 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16858
16859 switch (dyn.d_tag)
16860 {
16861 default:
16862 continue;
16863
16864 case DT_PPC64_GLINK:
16865 s = htab->glink;
16866 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16867 /* We stupidly defined DT_PPC64_GLINK to be the start
16868 of glink rather than the first entry point, which is
16869 what ld.so needs, and now have a bigger stub to
16870 support automatic multiple TOCs. */
16871 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16872 break;
16873
16874 case DT_PPC64_OPD:
16875 s = bfd_get_section_by_name (output_bfd, ".opd");
16876 if (s == NULL)
16877 continue;
16878 dyn.d_un.d_ptr = s->vma;
16879 break;
16880
16881 case DT_PPC64_OPT:
16882 if ((htab->do_multi_toc && htab->multi_toc_needed)
16883 || htab->notoc_plt)
16884 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16885 if (htab->has_plt_localentry0)
16886 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16887 break;
16888
16889 case DT_PPC64_OPDSZ:
16890 s = bfd_get_section_by_name (output_bfd, ".opd");
16891 if (s == NULL)
16892 continue;
16893 dyn.d_un.d_val = s->size;
16894 break;
16895
16896 case DT_PLTGOT:
16897 s = htab->elf.splt;
16898 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16899 break;
16900
16901 case DT_JMPREL:
16902 s = htab->elf.srelplt;
16903 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16904 break;
16905
16906 case DT_PLTRELSZ:
16907 dyn.d_un.d_val = htab->elf.srelplt->size;
16908 break;
16909
16910 case DT_TEXTREL:
16911 if (htab->local_ifunc_resolver)
16912 info->callbacks->einfo
16913 (_("%X%P: text relocations and GNU indirect "
16914 "functions will result in a segfault at runtime\n"));
16915 else if (htab->maybe_local_ifunc_resolver)
16916 info->callbacks->einfo
16917 (_("%P: warning: text relocations and GNU indirect "
16918 "functions may result in a segfault at runtime\n"));
16919 continue;
16920 }
16921
16922 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16923 }
16924 }
16925
16926 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16927 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16928 {
16929 /* Fill in the first entry in the global offset table.
16930 We use it to hold the link-time TOCbase. */
16931 bfd_put_64 (output_bfd,
16932 elf_gp (output_bfd) + TOC_BASE_OFF,
16933 htab->elf.sgot->contents);
16934
16935 /* Set .got entry size. */
16936 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16937 = 8;
16938 }
16939
16940 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16941 && htab->elf.splt->output_section != bfd_abs_section_ptr)
16942 {
16943 /* Set .plt entry size. */
16944 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16945 = PLT_ENTRY_SIZE (htab);
16946 }
16947
16948 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16949 brlt ourselves if emitrelocations. */
16950 if (htab->brlt != NULL
16951 && htab->brlt->reloc_count != 0
16952 && !_bfd_elf_link_output_relocs (output_bfd,
16953 htab->brlt,
16954 elf_section_data (htab->brlt)->rela.hdr,
16955 elf_section_data (htab->brlt)->relocs,
16956 NULL))
16957 return FALSE;
16958
16959 if (htab->glink != NULL
16960 && htab->glink->reloc_count != 0
16961 && !_bfd_elf_link_output_relocs (output_bfd,
16962 htab->glink,
16963 elf_section_data (htab->glink)->rela.hdr,
16964 elf_section_data (htab->glink)->relocs,
16965 NULL))
16966 return FALSE;
16967
16968
16969 if (htab->glink_eh_frame != NULL
16970 && htab->glink_eh_frame->size != 0
16971 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16972 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16973 htab->glink_eh_frame,
16974 htab->glink_eh_frame->contents))
16975 return FALSE;
16976
16977 /* We need to handle writing out multiple GOT sections ourselves,
16978 since we didn't add them to DYNOBJ. We know dynobj is the first
16979 bfd. */
16980 while ((dynobj = dynobj->link.next) != NULL)
16981 {
16982 asection *s;
16983
16984 if (!is_ppc64_elf (dynobj))
16985 continue;
16986
16987 s = ppc64_elf_tdata (dynobj)->got;
16988 if (s != NULL
16989 && s->size != 0
16990 && s->output_section != bfd_abs_section_ptr
16991 && !bfd_set_section_contents (output_bfd, s->output_section,
16992 s->contents, s->output_offset,
16993 s->size))
16994 return FALSE;
16995 s = ppc64_elf_tdata (dynobj)->relgot;
16996 if (s != NULL
16997 && s->size != 0
16998 && s->output_section != bfd_abs_section_ptr
16999 && !bfd_set_section_contents (output_bfd, s->output_section,
17000 s->contents, s->output_offset,
17001 s->size))
17002 return FALSE;
17003 }
17004
17005 return TRUE;
17006 }
17007
17008 #include "elf64-target.h"
17009
17010 /* FreeBSD support */
17011
17012 #undef TARGET_LITTLE_SYM
17013 #undef TARGET_LITTLE_NAME
17014
17015 #undef TARGET_BIG_SYM
17016 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
17017 #undef TARGET_BIG_NAME
17018 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
17019
17020 #undef ELF_OSABI
17021 #define ELF_OSABI ELFOSABI_FREEBSD
17022
17023 #undef elf64_bed
17024 #define elf64_bed elf64_powerpc_fbsd_bed
17025
17026 #include "elf64-target.h"
This page took 0.397835 seconds and 5 git commands to generate.