PowerPC64 GOT indirect to GOT relative optimisation
[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_unhandled_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_vma opd_entry_value
57 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
58
59 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
60 #define TARGET_LITTLE_NAME "elf64-powerpcle"
61 #define TARGET_BIG_SYM powerpc_elf64_vec
62 #define TARGET_BIG_NAME "elf64-powerpc"
63 #define ELF_ARCH bfd_arch_powerpc
64 #define ELF_TARGET_ID PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE EM_PPC64
66 #define ELF_MAXPAGESIZE 0x10000
67 #define ELF_COMMONPAGESIZE 0x1000
68 #define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
69 #define elf_info_to_howto ppc64_elf_info_to_howto
70
71 #define elf_backend_want_got_sym 0
72 #define elf_backend_want_plt_sym 0
73 #define elf_backend_plt_alignment 3
74 #define elf_backend_plt_not_loaded 1
75 #define elf_backend_got_header_size 8
76 #define elf_backend_want_dynrelro 1
77 #define elf_backend_can_gc_sections 1
78 #define elf_backend_can_refcount 1
79 #define elf_backend_rela_normal 1
80 #define elf_backend_dtrel_excludes_plt 1
81 #define elf_backend_default_execstack 0
82
83 #define bfd_elf64_mkobject ppc64_elf_mkobject
84 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
85 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
86 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
87 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
88 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
89 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
91 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
92 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
93
94 #define elf_backend_object_p ppc64_elf_object_p
95 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
96 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
97 #define elf_backend_write_core_note ppc64_elf_write_core_note
98 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
99 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
100 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
101 #define elf_backend_check_directives ppc64_elf_before_check_relocs
102 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
103 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
104 #define elf_backend_check_relocs ppc64_elf_check_relocs
105 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
106 #define elf_backend_gc_keep ppc64_elf_gc_keep
107 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
108 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
109 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
110 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
111 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
112 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
113 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
114 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
115 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
116 #define elf_backend_action_discarded ppc64_elf_action_discarded
117 #define elf_backend_relocate_section ppc64_elf_relocate_section
118 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
119 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
120 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
121 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
122 #define elf_backend_special_sections ppc64_elf_special_sections
123 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
124 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
125 #define elf_backend_get_reloc_section bfd_get_section_by_name
126
127 /* The name of the dynamic interpreter. This is put in the .interp
128 section. */
129 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
130
131 /* The size in bytes of an entry in the procedure linkage table. */
132 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
133 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
134
135 /* The initial size of the plt reserved for the dynamic linker. */
136 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
137
138 /* Offsets to some stack save slots. */
139 #define STK_LR 16
140 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
141 /* This one is dodgy. ELFv2 does not have a linker word, so use the
142 CR save slot. Used only by optimised __tls_get_addr call stub,
143 relying on __tls_get_addr_opt not saving CR.. */
144 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
145
146 /* TOC base pointers offset from start of TOC. */
147 #define TOC_BASE_OFF 0x8000
148 /* TOC base alignment. */
149 #define TOC_BASE_ALIGN 256
150
151 /* Offset of tp and dtp pointers from start of TLS block. */
152 #define TP_OFFSET 0x7000
153 #define DTP_OFFSET 0x8000
154
155 /* .plt call stub instructions. The normal stub is like this, but
156 sometimes the .plt entry crosses a 64k boundary and we need to
157 insert an addi to adjust r11. */
158 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
159 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
160 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
161 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
162 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
163 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
164 #define BCTR 0x4e800420 /* bctr */
165
166 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
167 #define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
168 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
169 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
170 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
171
172 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
173 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
174 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
175 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
176 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
177 #define BNECTR 0x4ca20420 /* bnectr+ */
178 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
179
180 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
181 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
182 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
183
184 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
185 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
186 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
187
188 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
189 #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
190 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
191 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
192 #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
193 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
194 #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
195 #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
196 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
197 #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
198 #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
199 #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
200
201 /* __glink_PLTresolve stub instructions. We enter with the index in R0. */
202 #define GLINK_PLTRESOLVE_SIZE(htab) \
203 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
204 /* 0: */
205 /* .quad plt0-1f */
206 /* __glink: */
207 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
208 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
209 /* 1: */
210 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
211 /* ld %2,(0b-1b)(%11) */
212 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
213 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
214 /* ld %12,0(%11) */
215 /* ld %2,8(%11) */
216 /* mtctr %12 */
217 /* ld %11,16(%11) */
218 /* bctr */
219 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
220 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
221 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
222 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
223 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
224
225 /* Pad with this. */
226 #define NOP 0x60000000
227
228 /* Some other nops. */
229 #define CROR_151515 0x4def7b82
230 #define CROR_313131 0x4ffffb82
231
232 /* .glink entries for the first 32k functions are two instructions. */
233 #define LI_R0_0 0x38000000 /* li %r0,0 */
234 #define B_DOT 0x48000000 /* b . */
235
236 /* After that, we need two instructions to load the index, followed by
237 a branch. */
238 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
239 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
240
241 /* Instructions used by the save and restore reg functions. */
242 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
243 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
244 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
245 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
246 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
247 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
248 #define LI_R12_0 0x39800000 /* li %r12,0 */
249 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
250 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
251 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
252 #define BLR 0x4e800020 /* blr */
253
254 /* Since .opd is an array of descriptors and each entry will end up
255 with identical R_PPC64_RELATIVE relocs, there is really no need to
256 propagate .opd relocs; The dynamic linker should be taught to
257 relocate .opd without reloc entries. */
258 #ifndef NO_OPD_RELOCS
259 #define NO_OPD_RELOCS 0
260 #endif
261
262 #ifndef ARRAY_SIZE
263 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
264 #endif
265
266 static inline int
267 abiversion (bfd *abfd)
268 {
269 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
270 }
271
272 static inline void
273 set_abiversion (bfd *abfd, int ver)
274 {
275 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
276 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
277 }
278 \f
279 /* Relocation HOWTO's. */
280 /* Like other ELF RELA targets that don't apply multiple
281 field-altering relocations to the same localation, src_mask is
282 always zero and pcrel_offset is the same as pc_relative.
283 PowerPC can always use a zero bitpos, even when the field is not at
284 the LSB. For example, a REL24 could use rightshift=2, bisize=24
285 and bitpos=2 which matches the ABI description, or as we do here,
286 rightshift=0, bitsize=26 and bitpos=0. */
287 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
288 complain, special_func) \
289 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
290 complain_overflow_ ## complain, special_func, \
291 #type, FALSE, 0, mask, pc_relative)
292
293 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
294
295 static reloc_howto_type ppc64_elf_howto_raw[] =
296 {
297 /* This reloc does nothing. */
298 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
299 bfd_elf_generic_reloc),
300
301 /* A standard 32 bit relocation. */
302 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
303 bfd_elf_generic_reloc),
304
305 /* An absolute 26 bit branch; the lower two bits must be zero.
306 FIXME: we don't check that, we just clear them. */
307 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
308 bfd_elf_generic_reloc),
309
310 /* A standard 16 bit relocation. */
311 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
312 bfd_elf_generic_reloc),
313
314 /* A 16 bit relocation without overflow. */
315 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
316 bfd_elf_generic_reloc),
317
318 /* Bits 16-31 of an address. */
319 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
320 bfd_elf_generic_reloc),
321
322 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
323 bits, treated as a signed number, is negative. */
324 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
325 ppc64_elf_ha_reloc),
326
327 /* An absolute 16 bit branch; the lower two bits must be zero.
328 FIXME: we don't check that, we just clear them. */
329 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
330 ppc64_elf_branch_reloc),
331
332 /* An absolute 16 bit branch, for which bit 10 should be set to
333 indicate that the branch is expected to be taken. The lower two
334 bits must be zero. */
335 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
336 ppc64_elf_brtaken_reloc),
337
338 /* An absolute 16 bit branch, for which bit 10 should be set to
339 indicate that the branch is not expected to be taken. The lower
340 two bits must be zero. */
341 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
342 ppc64_elf_brtaken_reloc),
343
344 /* A relative 26 bit branch; the lower two bits must be zero. */
345 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
346 ppc64_elf_branch_reloc),
347
348 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
349 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
350 ppc64_elf_branch_reloc),
351
352 /* A relative 16 bit branch; the lower two bits must be zero. */
353 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
354 ppc64_elf_branch_reloc),
355
356 /* A relative 16 bit branch. Bit 10 should be set to indicate that
357 the branch is expected to be taken. The lower two bits must be
358 zero. */
359 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
360 ppc64_elf_brtaken_reloc),
361
362 /* A relative 16 bit branch. Bit 10 should be set to indicate that
363 the branch is not expected to be taken. The lower two bits must
364 be zero. */
365 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
366 ppc64_elf_brtaken_reloc),
367
368 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
369 symbol. */
370 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
371 ppc64_elf_unhandled_reloc),
372
373 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
374 the symbol. */
375 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
376 ppc64_elf_unhandled_reloc),
377
378 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
379 the symbol. */
380 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
381 ppc64_elf_unhandled_reloc),
382
383 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
384 the symbol. */
385 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
386 ppc64_elf_unhandled_reloc),
387
388 /* This is used only by the dynamic linker. The symbol should exist
389 both in the object being run and in some shared library. The
390 dynamic linker copies the data addressed by the symbol from the
391 shared library into the object, because the object being
392 run has to have the data at some particular address. */
393 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
394 ppc64_elf_unhandled_reloc),
395
396 /* Like R_PPC64_ADDR64, but used when setting global offset table
397 entries. */
398 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
399 ppc64_elf_unhandled_reloc),
400
401 /* Created by the link editor. Marks a procedure linkage table
402 entry for a symbol. */
403 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
404 ppc64_elf_unhandled_reloc),
405
406 /* Used only by the dynamic linker. When the object is run, this
407 doubleword64 is set to the load address of the object, plus the
408 addend. */
409 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
410 bfd_elf_generic_reloc),
411
412 /* Like R_PPC64_ADDR32, but may be unaligned. */
413 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
414 bfd_elf_generic_reloc),
415
416 /* Like R_PPC64_ADDR16, but may be unaligned. */
417 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
418 bfd_elf_generic_reloc),
419
420 /* 32-bit PC relative. */
421 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
422 bfd_elf_generic_reloc),
423
424 /* 32-bit relocation to the symbol's procedure linkage table. */
425 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
426 ppc64_elf_unhandled_reloc),
427
428 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
429 FIXME: R_PPC64_PLTREL32 not supported. */
430 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
431 ppc64_elf_unhandled_reloc),
432
433 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
434 the symbol. */
435 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
436 ppc64_elf_unhandled_reloc),
437
438 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
439 the symbol. */
440 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
441 ppc64_elf_unhandled_reloc),
442
443 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
444 the symbol. */
445 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
446 ppc64_elf_unhandled_reloc),
447
448 /* 16-bit section relative relocation. */
449 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
450 ppc64_elf_sectoff_reloc),
451
452 /* Like R_PPC64_SECTOFF, but no overflow warning. */
453 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
454 ppc64_elf_sectoff_reloc),
455
456 /* 16-bit upper half section relative relocation. */
457 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
458 ppc64_elf_sectoff_reloc),
459
460 /* 16-bit upper half adjusted section relative relocation. */
461 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
462 ppc64_elf_sectoff_ha_reloc),
463
464 /* Like R_PPC64_REL24 without touching the two least significant bits. */
465 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
466 bfd_elf_generic_reloc),
467
468 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
469
470 /* A standard 64-bit relocation. */
471 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
472 bfd_elf_generic_reloc),
473
474 /* The bits 32-47 of an address. */
475 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
476 bfd_elf_generic_reloc),
477
478 /* The bits 32-47 of an address, plus 1 if the contents of the low
479 16 bits, treated as a signed number, is negative. */
480 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
481 ppc64_elf_ha_reloc),
482
483 /* The bits 48-63 of an address. */
484 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
485 bfd_elf_generic_reloc),
486
487 /* The bits 48-63 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_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
490 ppc64_elf_ha_reloc),
491
492 /* Like ADDR64, but may be unaligned. */
493 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
494 bfd_elf_generic_reloc),
495
496 /* 64-bit relative relocation. */
497 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
498 bfd_elf_generic_reloc),
499
500 /* 64-bit relocation to the symbol's procedure linkage table. */
501 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
502 ppc64_elf_unhandled_reloc),
503
504 /* 64-bit PC relative relocation to the symbol's procedure linkage
505 table. */
506 /* FIXME: R_PPC64_PLTREL64 not supported. */
507 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
508 ppc64_elf_unhandled_reloc),
509
510 /* 16 bit TOC-relative relocation. */
511 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
512 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
513 ppc64_elf_toc_reloc),
514
515 /* 16 bit TOC-relative relocation without overflow. */
516 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
517 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
518 ppc64_elf_toc_reloc),
519
520 /* 16 bit TOC-relative relocation, high 16 bits. */
521 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
522 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
523 ppc64_elf_toc_reloc),
524
525 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
526 contents of the low 16 bits, treated as a signed number, is
527 negative. */
528 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
529 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
530 ppc64_elf_toc_ha_reloc),
531
532 /* 64-bit relocation; insert value of TOC base (.TOC.). */
533 /* R_PPC64_TOC 51 doubleword64 .TOC. */
534 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
535 ppc64_elf_toc64_reloc),
536
537 /* Like R_PPC64_GOT16, but also informs the link editor that the
538 value to relocate may (!) refer to a PLT entry which the link
539 editor (a) may replace with the symbol value. If the link editor
540 is unable to fully resolve the symbol, it may (b) create a PLT
541 entry and store the address to the new PLT entry in the GOT.
542 This permits lazy resolution of function symbols at run time.
543 The link editor may also skip all of this and just (c) emit a
544 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
545 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
546 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
547 ppc64_elf_unhandled_reloc),
548
549 /* Like R_PPC64_PLTGOT16, but without overflow. */
550 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
551 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
552 ppc64_elf_unhandled_reloc),
553
554 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
555 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
556 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
557 ppc64_elf_unhandled_reloc),
558
559 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
560 1 if the contents of the low 16 bits, treated as a signed number,
561 is negative. */
562 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
563 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
564 ppc64_elf_unhandled_reloc),
565
566 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
567 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
568 bfd_elf_generic_reloc),
569
570 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
571 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
572 bfd_elf_generic_reloc),
573
574 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
575 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
576 ppc64_elf_unhandled_reloc),
577
578 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
579 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
580 ppc64_elf_unhandled_reloc),
581
582 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
583 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
584 ppc64_elf_unhandled_reloc),
585
586 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
587 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
588 ppc64_elf_sectoff_reloc),
589
590 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
591 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
592 ppc64_elf_sectoff_reloc),
593
594 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
595 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
596 ppc64_elf_toc_reloc),
597
598 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
599 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
600 ppc64_elf_toc_reloc),
601
602 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
603 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
604 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
605 ppc64_elf_unhandled_reloc),
606
607 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
608 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
609 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
610 ppc64_elf_unhandled_reloc),
611
612 /* Marker relocs for TLS. */
613 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
614 bfd_elf_generic_reloc),
615
616 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
617 bfd_elf_generic_reloc),
618
619 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
620 bfd_elf_generic_reloc),
621
622 /* Marker reloc for optimizing r2 save in prologue rather than on
623 each plt call stub. */
624 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
625 bfd_elf_generic_reloc),
626
627 /* Marker relocs on inline plt call instructions. */
628 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
629 bfd_elf_generic_reloc),
630
631 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
632 bfd_elf_generic_reloc),
633
634 /* Computes the load module index of the load module that contains the
635 definition of its TLS sym. */
636 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
637 ppc64_elf_unhandled_reloc),
638
639 /* Computes a dtv-relative displacement, the difference between the value
640 of sym+add and the base address of the thread-local storage block that
641 contains the definition of sym, minus 0x8000. */
642 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
643 ppc64_elf_unhandled_reloc),
644
645 /* A 16 bit dtprel reloc. */
646 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
647 ppc64_elf_unhandled_reloc),
648
649 /* Like DTPREL16, but no overflow. */
650 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
651 ppc64_elf_unhandled_reloc),
652
653 /* Like DTPREL16_LO, but next higher group of 16 bits. */
654 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
655 ppc64_elf_unhandled_reloc),
656
657 /* Like DTPREL16_HI, but adjust for low 16 bits. */
658 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
659 ppc64_elf_unhandled_reloc),
660
661 /* Like DTPREL16_HI, but next higher group of 16 bits. */
662 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
663 ppc64_elf_unhandled_reloc),
664
665 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
666 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
667 ppc64_elf_unhandled_reloc),
668
669 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
670 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
671 ppc64_elf_unhandled_reloc),
672
673 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
674 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
675 ppc64_elf_unhandled_reloc),
676
677 /* Like DTPREL16, but for insns with a DS field. */
678 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
679 ppc64_elf_unhandled_reloc),
680
681 /* Like DTPREL16_DS, but no overflow. */
682 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
683 ppc64_elf_unhandled_reloc),
684
685 /* Computes a tp-relative displacement, the difference between the value of
686 sym+add and the value of the thread pointer (r13). */
687 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
688 ppc64_elf_unhandled_reloc),
689
690 /* A 16 bit tprel reloc. */
691 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
692 ppc64_elf_unhandled_reloc),
693
694 /* Like TPREL16, but no overflow. */
695 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
696 ppc64_elf_unhandled_reloc),
697
698 /* Like TPREL16_LO, but next higher group of 16 bits. */
699 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
700 ppc64_elf_unhandled_reloc),
701
702 /* Like TPREL16_HI, but adjust for low 16 bits. */
703 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
704 ppc64_elf_unhandled_reloc),
705
706 /* Like TPREL16_HI, but next higher group of 16 bits. */
707 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
708 ppc64_elf_unhandled_reloc),
709
710 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
711 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
712 ppc64_elf_unhandled_reloc),
713
714 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
715 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
716 ppc64_elf_unhandled_reloc),
717
718 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
719 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
720 ppc64_elf_unhandled_reloc),
721
722 /* Like TPREL16, but for insns with a DS field. */
723 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
724 ppc64_elf_unhandled_reloc),
725
726 /* Like TPREL16_DS, but no overflow. */
727 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
728 ppc64_elf_unhandled_reloc),
729
730 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
731 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
732 to the first entry relative to the TOC base (r2). */
733 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
734 ppc64_elf_unhandled_reloc),
735
736 /* Like GOT_TLSGD16, but no overflow. */
737 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
738 ppc64_elf_unhandled_reloc),
739
740 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
741 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
742 ppc64_elf_unhandled_reloc),
743
744 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
745 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
746 ppc64_elf_unhandled_reloc),
747
748 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
749 with values (sym+add)@dtpmod and zero, and computes the offset to the
750 first entry relative to the TOC base (r2). */
751 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
752 ppc64_elf_unhandled_reloc),
753
754 /* Like GOT_TLSLD16, but no overflow. */
755 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
756 ppc64_elf_unhandled_reloc),
757
758 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
759 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
760 ppc64_elf_unhandled_reloc),
761
762 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
763 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
764 ppc64_elf_unhandled_reloc),
765
766 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
767 the offset to the entry relative to the TOC base (r2). */
768 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
769 ppc64_elf_unhandled_reloc),
770
771 /* Like GOT_DTPREL16_DS, but no overflow. */
772 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
773 ppc64_elf_unhandled_reloc),
774
775 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
776 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
777 ppc64_elf_unhandled_reloc),
778
779 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
780 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
781 ppc64_elf_unhandled_reloc),
782
783 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
784 offset to the entry relative to the TOC base (r2). */
785 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
786 ppc64_elf_unhandled_reloc),
787
788 /* Like GOT_TPREL16_DS, but no overflow. */
789 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
790 ppc64_elf_unhandled_reloc),
791
792 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
793 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
794 ppc64_elf_unhandled_reloc),
795
796 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
797 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
798 ppc64_elf_unhandled_reloc),
799
800 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
801 ppc64_elf_unhandled_reloc),
802
803 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
804 bfd_elf_generic_reloc),
805
806 /* A 16 bit relative relocation. */
807 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
808 bfd_elf_generic_reloc),
809
810 /* A 16 bit relative relocation without overflow. */
811 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
812 bfd_elf_generic_reloc),
813
814 /* The high order 16 bits of a relative address. */
815 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
816 bfd_elf_generic_reloc),
817
818 /* The high order 16 bits of a relative address, plus 1 if the contents of
819 the low 16 bits, treated as a signed number, is negative. */
820 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
821 ppc64_elf_ha_reloc),
822
823 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
824 bfd_elf_generic_reloc),
825
826 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
827 ppc64_elf_ha_reloc),
828
829 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
830 bfd_elf_generic_reloc),
831
832 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
833 ppc64_elf_ha_reloc),
834
835 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
836 bfd_elf_generic_reloc),
837
838 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
839 ppc64_elf_ha_reloc),
840
841 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
842 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
843 ppc64_elf_ha_reloc),
844
845 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
846 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
847 ppc64_elf_ha_reloc),
848
849 /* Like R_PPC64_ADDR16_HI, but no overflow. */
850 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
851 bfd_elf_generic_reloc),
852
853 /* Like R_PPC64_ADDR16_HA, but no overflow. */
854 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
855 ppc64_elf_ha_reloc),
856
857 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
858 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
859 ppc64_elf_unhandled_reloc),
860
861 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
862 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
863 ppc64_elf_unhandled_reloc),
864
865 /* Like R_PPC64_TPREL16_HI, but no overflow. */
866 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
867 ppc64_elf_unhandled_reloc),
868
869 /* Like R_PPC64_TPREL16_HA, but no overflow. */
870 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
871 ppc64_elf_unhandled_reloc),
872
873 /* Marker reloc on ELFv2 large-model function entry. */
874 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
875 bfd_elf_generic_reloc),
876
877 /* Like ADDR64, but use local entry point of function. */
878 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
879 bfd_elf_generic_reloc),
880
881 /* GNU extension to record C++ vtable hierarchy. */
882 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
883 NULL),
884
885 /* GNU extension to record C++ vtable member usage. */
886 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
887 NULL),
888 };
889
890 \f
891 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
892 be done. */
893
894 static void
895 ppc_howto_init (void)
896 {
897 unsigned int i, type;
898
899 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
900 {
901 type = ppc64_elf_howto_raw[i].type;
902 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
903 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
904 }
905 }
906
907 static reloc_howto_type *
908 ppc64_elf_reloc_type_lookup (bfd *abfd,
909 bfd_reloc_code_real_type code)
910 {
911 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
912
913 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
914 /* Initialize howto table if needed. */
915 ppc_howto_init ();
916
917 switch (code)
918 {
919 default:
920 /* xgettext:c-format */
921 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
922 (int) code);
923 bfd_set_error (bfd_error_bad_value);
924 return NULL;
925
926 case BFD_RELOC_NONE: r = R_PPC64_NONE;
927 break;
928 case BFD_RELOC_32: r = R_PPC64_ADDR32;
929 break;
930 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
931 break;
932 case BFD_RELOC_16: r = R_PPC64_ADDR16;
933 break;
934 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
935 break;
936 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
937 break;
938 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
939 break;
940 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
941 break;
942 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
943 break;
944 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
945 break;
946 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
947 break;
948 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
949 break;
950 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
951 break;
952 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
953 break;
954 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
955 break;
956 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
957 break;
958 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
959 break;
960 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
961 break;
962 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
963 break;
964 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
965 break;
966 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
967 break;
968 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
969 break;
970 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
971 break;
972 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
973 break;
974 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
975 break;
976 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
977 break;
978 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
979 break;
980 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
981 break;
982 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
983 break;
984 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
985 break;
986 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
987 break;
988 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
989 break;
990 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
991 break;
992 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
993 break;
994 case BFD_RELOC_64: r = R_PPC64_ADDR64;
995 break;
996 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
997 break;
998 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
999 break;
1000 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1001 break;
1002 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1003 break;
1004 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1005 break;
1006 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1007 break;
1008 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1009 break;
1010 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1011 break;
1012 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1013 break;
1014 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1015 break;
1016 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1017 break;
1018 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1019 break;
1020 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1021 break;
1022 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1023 break;
1024 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1025 break;
1026 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1027 break;
1028 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1029 break;
1030 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1031 break;
1032 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1033 break;
1034 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1035 break;
1036 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1037 break;
1038 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1039 break;
1040 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1041 break;
1042 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1043 break;
1044 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1045 break;
1046 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1047 break;
1048 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1049 break;
1050 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1051 break;
1052 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1053 break;
1054 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1055 break;
1056 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1057 break;
1058 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1059 break;
1060 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1061 break;
1062 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1063 break;
1064 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1065 break;
1066 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1067 break;
1068 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1069 break;
1070 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1071 break;
1072 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1073 break;
1074 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1075 break;
1076 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1077 break;
1078 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1079 break;
1080 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1081 break;
1082 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1083 break;
1084 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1085 break;
1086 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1087 break;
1088 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1089 break;
1090 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1091 break;
1092 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1093 break;
1094 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1095 break;
1096 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1097 break;
1098 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1099 break;
1100 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1101 break;
1102 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1103 break;
1104 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1105 break;
1106 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1107 break;
1108 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1109 break;
1110 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1111 break;
1112 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1113 break;
1114 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1115 break;
1116 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1117 break;
1118 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1119 break;
1120 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1121 break;
1122 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1123 break;
1124 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1125 break;
1126 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1127 break;
1128 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1129 break;
1130 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1131 break;
1132 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1133 break;
1134 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1135 break;
1136 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1137 break;
1138 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1139 break;
1140 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1141 break;
1142 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1143 break;
1144 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1145 break;
1146 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1147 break;
1148 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1149 break;
1150 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1151 break;
1152 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1153 break;
1154 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1155 break;
1156 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1157 break;
1158 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1159 break;
1160 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1161 break;
1162 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1163 break;
1164 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1165 break;
1166 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1167 break;
1168 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1169 break;
1170 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1171 break;
1172 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1173 break;
1174 }
1175
1176 return ppc64_elf_howto_table[r];
1177 };
1178
1179 static reloc_howto_type *
1180 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1181 const char *r_name)
1182 {
1183 unsigned int i;
1184
1185 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1186 if (ppc64_elf_howto_raw[i].name != NULL
1187 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1188 return &ppc64_elf_howto_raw[i];
1189
1190 return NULL;
1191 }
1192
1193 /* Set the howto pointer for a PowerPC ELF reloc. */
1194
1195 static bfd_boolean
1196 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1197 Elf_Internal_Rela *dst)
1198 {
1199 unsigned int type;
1200
1201 /* Initialize howto table if needed. */
1202 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1203 ppc_howto_init ();
1204
1205 type = ELF64_R_TYPE (dst->r_info);
1206 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1207 {
1208 /* xgettext:c-format */
1209 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1210 abfd, type);
1211 bfd_set_error (bfd_error_bad_value);
1212 return FALSE;
1213 }
1214 cache_ptr->howto = ppc64_elf_howto_table[type];
1215 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1216 {
1217 /* xgettext:c-format */
1218 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1219 abfd, type);
1220 bfd_set_error (bfd_error_bad_value);
1221 return FALSE;
1222 }
1223
1224 return TRUE;
1225 }
1226
1227 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1228
1229 static bfd_reloc_status_type
1230 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1231 void *data, asection *input_section,
1232 bfd *output_bfd, char **error_message)
1233 {
1234 enum elf_ppc64_reloc_type r_type;
1235 long insn;
1236 bfd_size_type octets;
1237 bfd_vma value;
1238
1239 /* If this is a relocatable link (output_bfd test tells us), just
1240 call the generic function. Any adjustment will be done at final
1241 link time. */
1242 if (output_bfd != NULL)
1243 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1244 input_section, output_bfd, error_message);
1245
1246 /* Adjust the addend for sign extension of the low 16 bits.
1247 We won't actually be using the low 16 bits, so trashing them
1248 doesn't matter. */
1249 reloc_entry->addend += 0x8000;
1250 r_type = reloc_entry->howto->type;
1251 if (r_type != R_PPC64_REL16DX_HA)
1252 return bfd_reloc_continue;
1253
1254 value = 0;
1255 if (!bfd_is_com_section (symbol->section))
1256 value = symbol->value;
1257 value += (reloc_entry->addend
1258 + symbol->section->output_offset
1259 + symbol->section->output_section->vma);
1260 value -= (reloc_entry->address
1261 + input_section->output_offset
1262 + input_section->output_section->vma);
1263 value = (bfd_signed_vma) value >> 16;
1264
1265 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1266 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1267 insn &= ~0x1fffc1;
1268 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1269 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1270 if (value + 0x8000 > 0xffff)
1271 return bfd_reloc_overflow;
1272 return bfd_reloc_ok;
1273 }
1274
1275 static bfd_reloc_status_type
1276 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1277 void *data, asection *input_section,
1278 bfd *output_bfd, char **error_message)
1279 {
1280 if (output_bfd != NULL)
1281 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1282 input_section, output_bfd, error_message);
1283
1284 if (strcmp (symbol->section->name, ".opd") == 0
1285 && (symbol->section->owner->flags & DYNAMIC) == 0)
1286 {
1287 bfd_vma dest = opd_entry_value (symbol->section,
1288 symbol->value + reloc_entry->addend,
1289 NULL, NULL, FALSE);
1290 if (dest != (bfd_vma) -1)
1291 reloc_entry->addend = dest - (symbol->value
1292 + symbol->section->output_section->vma
1293 + symbol->section->output_offset);
1294 }
1295 else
1296 {
1297 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1298
1299 if (symbol->section->owner != abfd
1300 && symbol->section->owner != NULL
1301 && abiversion (symbol->section->owner) >= 2)
1302 {
1303 unsigned int i;
1304
1305 for (i = 0; i < symbol->section->owner->symcount; ++i)
1306 {
1307 asymbol *symdef = symbol->section->owner->outsymbols[i];
1308
1309 if (strcmp (symdef->name, symbol->name) == 0)
1310 {
1311 elfsym = (elf_symbol_type *) symdef;
1312 break;
1313 }
1314 }
1315 }
1316 reloc_entry->addend
1317 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1318 }
1319 return bfd_reloc_continue;
1320 }
1321
1322 static bfd_reloc_status_type
1323 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1324 void *data, asection *input_section,
1325 bfd *output_bfd, char **error_message)
1326 {
1327 long insn;
1328 enum elf_ppc64_reloc_type r_type;
1329 bfd_size_type octets;
1330 /* Assume 'at' branch hints. */
1331 bfd_boolean is_isa_v2 = TRUE;
1332
1333 /* If this is a relocatable link (output_bfd test tells us), just
1334 call the generic function. Any adjustment will be done at final
1335 link time. */
1336 if (output_bfd != NULL)
1337 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1338 input_section, output_bfd, error_message);
1339
1340 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1341 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1342 insn &= ~(0x01 << 21);
1343 r_type = reloc_entry->howto->type;
1344 if (r_type == R_PPC64_ADDR14_BRTAKEN
1345 || r_type == R_PPC64_REL14_BRTAKEN)
1346 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1347
1348 if (is_isa_v2)
1349 {
1350 /* Set 'a' bit. This is 0b00010 in BO field for branch
1351 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1352 for branch on CTR insns (BO == 1a00t or 1a01t). */
1353 if ((insn & (0x14 << 21)) == (0x04 << 21))
1354 insn |= 0x02 << 21;
1355 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1356 insn |= 0x08 << 21;
1357 else
1358 goto out;
1359 }
1360 else
1361 {
1362 bfd_vma target = 0;
1363 bfd_vma from;
1364
1365 if (!bfd_is_com_section (symbol->section))
1366 target = symbol->value;
1367 target += symbol->section->output_section->vma;
1368 target += symbol->section->output_offset;
1369 target += reloc_entry->addend;
1370
1371 from = (reloc_entry->address
1372 + input_section->output_offset
1373 + input_section->output_section->vma);
1374
1375 /* Invert 'y' bit if not the default. */
1376 if ((bfd_signed_vma) (target - from) < 0)
1377 insn ^= 0x01 << 21;
1378 }
1379 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1380 out:
1381 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1382 input_section, output_bfd, error_message);
1383 }
1384
1385 static bfd_reloc_status_type
1386 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1387 void *data, asection *input_section,
1388 bfd *output_bfd, char **error_message)
1389 {
1390 /* If this is a relocatable link (output_bfd test tells us), just
1391 call the generic function. Any adjustment will be done at final
1392 link time. */
1393 if (output_bfd != NULL)
1394 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1395 input_section, output_bfd, error_message);
1396
1397 /* Subtract the symbol section base address. */
1398 reloc_entry->addend -= symbol->section->output_section->vma;
1399 return bfd_reloc_continue;
1400 }
1401
1402 static bfd_reloc_status_type
1403 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1404 void *data, asection *input_section,
1405 bfd *output_bfd, char **error_message)
1406 {
1407 /* If this is a relocatable link (output_bfd test tells us), just
1408 call the generic function. Any adjustment will be done at final
1409 link time. */
1410 if (output_bfd != NULL)
1411 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1412 input_section, output_bfd, error_message);
1413
1414 /* Subtract the symbol section base address. */
1415 reloc_entry->addend -= symbol->section->output_section->vma;
1416
1417 /* Adjust the addend for sign extension of the low 16 bits. */
1418 reloc_entry->addend += 0x8000;
1419 return bfd_reloc_continue;
1420 }
1421
1422 static bfd_reloc_status_type
1423 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1424 void *data, asection *input_section,
1425 bfd *output_bfd, char **error_message)
1426 {
1427 bfd_vma TOCstart;
1428
1429 /* If this is a relocatable link (output_bfd test tells us), just
1430 call the generic function. Any adjustment will be done at final
1431 link time. */
1432 if (output_bfd != NULL)
1433 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1434 input_section, output_bfd, error_message);
1435
1436 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1437 if (TOCstart == 0)
1438 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1439
1440 /* Subtract the TOC base address. */
1441 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1442 return bfd_reloc_continue;
1443 }
1444
1445 static bfd_reloc_status_type
1446 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1447 void *data, asection *input_section,
1448 bfd *output_bfd, char **error_message)
1449 {
1450 bfd_vma TOCstart;
1451
1452 /* If this is a relocatable link (output_bfd test tells us), just
1453 call the generic function. Any adjustment will be done at final
1454 link time. */
1455 if (output_bfd != NULL)
1456 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1457 input_section, output_bfd, error_message);
1458
1459 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1460 if (TOCstart == 0)
1461 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1462
1463 /* Subtract the TOC base address. */
1464 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1465
1466 /* Adjust the addend for sign extension of the low 16 bits. */
1467 reloc_entry->addend += 0x8000;
1468 return bfd_reloc_continue;
1469 }
1470
1471 static bfd_reloc_status_type
1472 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1473 void *data, asection *input_section,
1474 bfd *output_bfd, char **error_message)
1475 {
1476 bfd_vma TOCstart;
1477 bfd_size_type octets;
1478
1479 /* If this is a relocatable link (output_bfd test tells us), just
1480 call the generic function. Any adjustment will be done at final
1481 link time. */
1482 if (output_bfd != NULL)
1483 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1484 input_section, output_bfd, error_message);
1485
1486 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1487 if (TOCstart == 0)
1488 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1489
1490 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1491 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1492 return bfd_reloc_ok;
1493 }
1494
1495 static bfd_reloc_status_type
1496 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1497 void *data, asection *input_section,
1498 bfd *output_bfd, char **error_message)
1499 {
1500 /* If this is a relocatable link (output_bfd test tells us), just
1501 call the generic function. Any adjustment will be done at final
1502 link time. */
1503 if (output_bfd != NULL)
1504 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1505 input_section, output_bfd, error_message);
1506
1507 if (error_message != NULL)
1508 {
1509 static char buf[60];
1510 sprintf (buf, "generic linker can't handle %s",
1511 reloc_entry->howto->name);
1512 *error_message = buf;
1513 }
1514 return bfd_reloc_dangerous;
1515 }
1516
1517 /* Track GOT entries needed for a given symbol. We might need more
1518 than one got entry per symbol. */
1519 struct got_entry
1520 {
1521 struct got_entry *next;
1522
1523 /* The symbol addend that we'll be placing in the GOT. */
1524 bfd_vma addend;
1525
1526 /* Unlike other ELF targets, we use separate GOT entries for the same
1527 symbol referenced from different input files. This is to support
1528 automatic multiple TOC/GOT sections, where the TOC base can vary
1529 from one input file to another. After partitioning into TOC groups
1530 we merge entries within the group.
1531
1532 Point to the BFD owning this GOT entry. */
1533 bfd *owner;
1534
1535 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1536 TLS_TPREL or TLS_DTPREL for tls entries. */
1537 unsigned char tls_type;
1538
1539 /* Non-zero if got.ent points to real entry. */
1540 unsigned char is_indirect;
1541
1542 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1543 union
1544 {
1545 bfd_signed_vma refcount;
1546 bfd_vma offset;
1547 struct got_entry *ent;
1548 } got;
1549 };
1550
1551 /* The same for PLT. */
1552 struct plt_entry
1553 {
1554 struct plt_entry *next;
1555
1556 bfd_vma addend;
1557
1558 union
1559 {
1560 bfd_signed_vma refcount;
1561 bfd_vma offset;
1562 } plt;
1563 };
1564
1565 struct ppc64_elf_obj_tdata
1566 {
1567 struct elf_obj_tdata elf;
1568
1569 /* Shortcuts to dynamic linker sections. */
1570 asection *got;
1571 asection *relgot;
1572
1573 /* Used during garbage collection. We attach global symbols defined
1574 on removed .opd entries to this section so that the sym is removed. */
1575 asection *deleted_section;
1576
1577 /* TLS local dynamic got entry handling. Support for multiple GOT
1578 sections means we potentially need one of these for each input bfd. */
1579 struct got_entry tlsld_got;
1580
1581 union
1582 {
1583 /* A copy of relocs before they are modified for --emit-relocs. */
1584 Elf_Internal_Rela *relocs;
1585
1586 /* Section contents. */
1587 bfd_byte *contents;
1588 } opd;
1589
1590 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1591 the reloc to be in the range -32768 to 32767. */
1592 unsigned int has_small_toc_reloc : 1;
1593
1594 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1595 instruction not one we handle. */
1596 unsigned int unexpected_toc_insn : 1;
1597
1598 /* Set if got relocs that can be optimised are present in this file. */
1599 unsigned int has_gotrel : 1;
1600 };
1601
1602 #define ppc64_elf_tdata(bfd) \
1603 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1604
1605 #define ppc64_tlsld_got(bfd) \
1606 (&ppc64_elf_tdata (bfd)->tlsld_got)
1607
1608 #define is_ppc64_elf(bfd) \
1609 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1610 && elf_object_id (bfd) == PPC64_ELF_DATA)
1611
1612 /* Override the generic function because we store some extras. */
1613
1614 static bfd_boolean
1615 ppc64_elf_mkobject (bfd *abfd)
1616 {
1617 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1618 PPC64_ELF_DATA);
1619 }
1620
1621 /* Fix bad default arch selected for a 64 bit input bfd when the
1622 default is 32 bit. Also select arch based on apuinfo. */
1623
1624 static bfd_boolean
1625 ppc64_elf_object_p (bfd *abfd)
1626 {
1627 if (!abfd->arch_info->the_default)
1628 return TRUE;
1629
1630 if (abfd->arch_info->bits_per_word == 32)
1631 {
1632 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1633
1634 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1635 {
1636 /* Relies on arch after 32 bit default being 64 bit default. */
1637 abfd->arch_info = abfd->arch_info->next;
1638 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1639 }
1640 }
1641 return _bfd_elf_ppc_set_arch (abfd);
1642 }
1643
1644 /* Support for core dump NOTE sections. */
1645
1646 static bfd_boolean
1647 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1648 {
1649 size_t offset, size;
1650
1651 if (note->descsz != 504)
1652 return FALSE;
1653
1654 /* pr_cursig */
1655 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1656
1657 /* pr_pid */
1658 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1659
1660 /* pr_reg */
1661 offset = 112;
1662 size = 384;
1663
1664 /* Make a ".reg/999" section. */
1665 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1666 size, note->descpos + offset);
1667 }
1668
1669 static bfd_boolean
1670 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1671 {
1672 if (note->descsz != 136)
1673 return FALSE;
1674
1675 elf_tdata (abfd)->core->pid
1676 = bfd_get_32 (abfd, note->descdata + 24);
1677 elf_tdata (abfd)->core->program
1678 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1679 elf_tdata (abfd)->core->command
1680 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1681
1682 return TRUE;
1683 }
1684
1685 static char *
1686 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1687 ...)
1688 {
1689 switch (note_type)
1690 {
1691 default:
1692 return NULL;
1693
1694 case NT_PRPSINFO:
1695 {
1696 char data[136] ATTRIBUTE_NONSTRING;
1697 va_list ap;
1698
1699 va_start (ap, note_type);
1700 memset (data, 0, sizeof (data));
1701 strncpy (data + 40, va_arg (ap, const char *), 16);
1702 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1703 DIAGNOSTIC_PUSH;
1704 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1705 -Wstringop-truncation:
1706 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1707 */
1708 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1709 #endif
1710 strncpy (data + 56, va_arg (ap, const char *), 80);
1711 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1712 DIAGNOSTIC_POP;
1713 #endif
1714 va_end (ap);
1715 return elfcore_write_note (abfd, buf, bufsiz,
1716 "CORE", note_type, data, sizeof (data));
1717 }
1718
1719 case NT_PRSTATUS:
1720 {
1721 char data[504];
1722 va_list ap;
1723 long pid;
1724 int cursig;
1725 const void *greg;
1726
1727 va_start (ap, note_type);
1728 memset (data, 0, 112);
1729 pid = va_arg (ap, long);
1730 bfd_put_32 (abfd, pid, data + 32);
1731 cursig = va_arg (ap, int);
1732 bfd_put_16 (abfd, cursig, data + 12);
1733 greg = va_arg (ap, const void *);
1734 memcpy (data + 112, greg, 384);
1735 memset (data + 496, 0, 8);
1736 va_end (ap);
1737 return elfcore_write_note (abfd, buf, bufsiz,
1738 "CORE", note_type, data, sizeof (data));
1739 }
1740 }
1741 }
1742
1743 /* Add extra PPC sections. */
1744
1745 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1746 {
1747 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1748 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1749 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1750 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1751 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1752 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1753 { NULL, 0, 0, 0, 0 }
1754 };
1755
1756 enum _ppc64_sec_type {
1757 sec_normal = 0,
1758 sec_opd = 1,
1759 sec_toc = 2
1760 };
1761
1762 struct _ppc64_elf_section_data
1763 {
1764 struct bfd_elf_section_data elf;
1765
1766 union
1767 {
1768 /* An array with one entry for each opd function descriptor,
1769 and some spares since opd entries may be either 16 or 24 bytes. */
1770 #define OPD_NDX(OFF) ((OFF) >> 4)
1771 struct _opd_sec_data
1772 {
1773 /* Points to the function code section for local opd entries. */
1774 asection **func_sec;
1775
1776 /* After editing .opd, adjust references to opd local syms. */
1777 long *adjust;
1778 } opd;
1779
1780 /* An array for toc sections, indexed by offset/8. */
1781 struct _toc_sec_data
1782 {
1783 /* Specifies the relocation symbol index used at a given toc offset. */
1784 unsigned *symndx;
1785
1786 /* And the relocation addend. */
1787 bfd_vma *add;
1788 } toc;
1789 } u;
1790
1791 enum _ppc64_sec_type sec_type:2;
1792
1793 /* Flag set when small branches are detected. Used to
1794 select suitable defaults for the stub group size. */
1795 unsigned int has_14bit_branch:1;
1796
1797 /* Flag set when PLTCALL relocs are detected. */
1798 unsigned int has_pltcall:1;
1799
1800 /* Flag set when section has GOT relocations that can be optimised. */
1801 unsigned int has_gotrel:1;
1802 };
1803
1804 #define ppc64_elf_section_data(sec) \
1805 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
1806
1807 static bfd_boolean
1808 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
1809 {
1810 if (!sec->used_by_bfd)
1811 {
1812 struct _ppc64_elf_section_data *sdata;
1813 bfd_size_type amt = sizeof (*sdata);
1814
1815 sdata = bfd_zalloc (abfd, amt);
1816 if (sdata == NULL)
1817 return FALSE;
1818 sec->used_by_bfd = sdata;
1819 }
1820
1821 return _bfd_elf_new_section_hook (abfd, sec);
1822 }
1823
1824 static struct _opd_sec_data *
1825 get_opd_info (asection * sec)
1826 {
1827 if (sec != NULL
1828 && ppc64_elf_section_data (sec) != NULL
1829 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
1830 return &ppc64_elf_section_data (sec)->u.opd;
1831 return NULL;
1832 }
1833 \f
1834 /* Parameters for the qsort hook. */
1835 static bfd_boolean synthetic_relocatable;
1836 static asection *synthetic_opd;
1837
1838 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
1839
1840 static int
1841 compare_symbols (const void *ap, const void *bp)
1842 {
1843 const asymbol *a = *(const asymbol **) ap;
1844 const asymbol *b = *(const asymbol **) bp;
1845
1846 /* Section symbols first. */
1847 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
1848 return -1;
1849 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
1850 return 1;
1851
1852 /* then .opd symbols. */
1853 if (synthetic_opd != NULL)
1854 {
1855 if (strcmp (a->section->name, ".opd") == 0
1856 && strcmp (b->section->name, ".opd") != 0)
1857 return -1;
1858 if (strcmp (a->section->name, ".opd") != 0
1859 && strcmp (b->section->name, ".opd") == 0)
1860 return 1;
1861 }
1862
1863 /* then other code symbols. */
1864 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1865 == (SEC_CODE | SEC_ALLOC))
1866 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1867 != (SEC_CODE | SEC_ALLOC)))
1868 return -1;
1869
1870 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1871 != (SEC_CODE | SEC_ALLOC))
1872 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1873 == (SEC_CODE | SEC_ALLOC)))
1874 return 1;
1875
1876 if (synthetic_relocatable)
1877 {
1878 if (a->section->id < b->section->id)
1879 return -1;
1880
1881 if (a->section->id > b->section->id)
1882 return 1;
1883 }
1884
1885 if (a->value + a->section->vma < b->value + b->section->vma)
1886 return -1;
1887
1888 if (a->value + a->section->vma > b->value + b->section->vma)
1889 return 1;
1890
1891 /* For syms with the same value, prefer strong dynamic global function
1892 syms over other syms. */
1893 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
1894 return -1;
1895
1896 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
1897 return 1;
1898
1899 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
1900 return -1;
1901
1902 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
1903 return 1;
1904
1905 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
1906 return -1;
1907
1908 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
1909 return 1;
1910
1911 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
1912 return -1;
1913
1914 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
1915 return 1;
1916
1917 return a > b;
1918 }
1919
1920 /* Search SYMS for a symbol of the given VALUE. */
1921
1922 static asymbol *
1923 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
1924 {
1925 long mid;
1926
1927 if (id == (unsigned) -1)
1928 {
1929 while (lo < hi)
1930 {
1931 mid = (lo + hi) >> 1;
1932 if (syms[mid]->value + syms[mid]->section->vma < value)
1933 lo = mid + 1;
1934 else if (syms[mid]->value + syms[mid]->section->vma > value)
1935 hi = mid;
1936 else
1937 return syms[mid];
1938 }
1939 }
1940 else
1941 {
1942 while (lo < hi)
1943 {
1944 mid = (lo + hi) >> 1;
1945 if (syms[mid]->section->id < id)
1946 lo = mid + 1;
1947 else if (syms[mid]->section->id > id)
1948 hi = mid;
1949 else if (syms[mid]->value < value)
1950 lo = mid + 1;
1951 else if (syms[mid]->value > value)
1952 hi = mid;
1953 else
1954 return syms[mid];
1955 }
1956 }
1957 return NULL;
1958 }
1959
1960 static bfd_boolean
1961 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
1962 {
1963 bfd_vma vma = *(bfd_vma *) ptr;
1964 return ((section->flags & SEC_ALLOC) != 0
1965 && section->vma <= vma
1966 && vma < section->vma + section->size);
1967 }
1968
1969 /* Create synthetic symbols, effectively restoring "dot-symbol" function
1970 entry syms. Also generate @plt symbols for the glink branch table.
1971 Returns count of synthetic symbols in RET or -1 on error. */
1972
1973 static long
1974 ppc64_elf_get_synthetic_symtab (bfd *abfd,
1975 long static_count, asymbol **static_syms,
1976 long dyn_count, asymbol **dyn_syms,
1977 asymbol **ret)
1978 {
1979 asymbol *s;
1980 size_t i, j, count;
1981 char *names;
1982 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
1983 asection *opd = NULL;
1984 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
1985 asymbol **syms;
1986 int abi = abiversion (abfd);
1987
1988 *ret = NULL;
1989
1990 if (abi < 2)
1991 {
1992 opd = bfd_get_section_by_name (abfd, ".opd");
1993 if (opd == NULL && abi == 1)
1994 return 0;
1995 }
1996
1997 syms = NULL;
1998 codesecsym = 0;
1999 codesecsymend = 0;
2000 secsymend = 0;
2001 opdsymend = 0;
2002 symcount = 0;
2003 if (opd != NULL)
2004 {
2005 symcount = static_count;
2006 if (!relocatable)
2007 symcount += dyn_count;
2008 if (symcount == 0)
2009 return 0;
2010
2011 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2012 if (syms == NULL)
2013 return -1;
2014
2015 if (!relocatable && static_count != 0 && dyn_count != 0)
2016 {
2017 /* Use both symbol tables. */
2018 memcpy (syms, static_syms, static_count * sizeof (*syms));
2019 memcpy (syms + static_count, dyn_syms,
2020 (dyn_count + 1) * sizeof (*syms));
2021 }
2022 else if (!relocatable && static_count == 0)
2023 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2024 else
2025 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2026
2027 /* Trim uninteresting symbols. Interesting symbols are section,
2028 function, and notype symbols. */
2029 for (i = 0, j = 0; i < symcount; ++i)
2030 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2031 | BSF_RELC | BSF_SRELC)) == 0)
2032 syms[j++] = syms[i];
2033 symcount = j;
2034
2035 synthetic_relocatable = relocatable;
2036 synthetic_opd = opd;
2037 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2038
2039 if (!relocatable && symcount > 1)
2040 {
2041 /* Trim duplicate syms, since we may have merged the normal
2042 and dynamic symbols. Actually, we only care about syms
2043 that have different values, so trim any with the same
2044 value. Don't consider ifunc and ifunc resolver symbols
2045 duplicates however, because GDB wants to know whether a
2046 text symbol is an ifunc resolver. */
2047 for (i = 1, j = 1; i < symcount; ++i)
2048 {
2049 const asymbol *s0 = syms[i - 1];
2050 const asymbol *s1 = syms[i];
2051
2052 if ((s0->value + s0->section->vma
2053 != s1->value + s1->section->vma)
2054 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2055 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2056 syms[j++] = syms[i];
2057 }
2058 symcount = j;
2059 }
2060
2061 i = 0;
2062 /* Note that here and in compare_symbols we can't compare opd and
2063 sym->section directly. With separate debug info files, the
2064 symbols will be extracted from the debug file while abfd passed
2065 to this function is the real binary. */
2066 if (strcmp (syms[i]->section->name, ".opd") == 0)
2067 ++i;
2068 codesecsym = i;
2069
2070 for (; i < symcount; ++i)
2071 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2072 | SEC_THREAD_LOCAL))
2073 != (SEC_CODE | SEC_ALLOC))
2074 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2075 break;
2076 codesecsymend = i;
2077
2078 for (; i < symcount; ++i)
2079 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2080 break;
2081 secsymend = i;
2082
2083 for (; i < symcount; ++i)
2084 if (strcmp (syms[i]->section->name, ".opd") != 0)
2085 break;
2086 opdsymend = i;
2087
2088 for (; i < symcount; ++i)
2089 if (((syms[i]->section->flags
2090 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2091 != (SEC_CODE | SEC_ALLOC))
2092 break;
2093 symcount = i;
2094 }
2095 count = 0;
2096
2097 if (relocatable)
2098 {
2099 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2100 arelent *r;
2101 size_t size;
2102 size_t relcount;
2103
2104 if (opdsymend == secsymend)
2105 goto done;
2106
2107 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2108 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2109 if (relcount == 0)
2110 goto done;
2111
2112 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2113 {
2114 count = -1;
2115 goto done;
2116 }
2117
2118 size = 0;
2119 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2120 {
2121 asymbol *sym;
2122
2123 while (r < opd->relocation + relcount
2124 && r->address < syms[i]->value + opd->vma)
2125 ++r;
2126
2127 if (r == opd->relocation + relcount)
2128 break;
2129
2130 if (r->address != syms[i]->value + opd->vma)
2131 continue;
2132
2133 if (r->howto->type != R_PPC64_ADDR64)
2134 continue;
2135
2136 sym = *r->sym_ptr_ptr;
2137 if (!sym_exists_at (syms, opdsymend, symcount,
2138 sym->section->id, sym->value + r->addend))
2139 {
2140 ++count;
2141 size += sizeof (asymbol);
2142 size += strlen (syms[i]->name) + 2;
2143 }
2144 }
2145
2146 if (size == 0)
2147 goto done;
2148 s = *ret = bfd_malloc (size);
2149 if (s == NULL)
2150 {
2151 count = -1;
2152 goto done;
2153 }
2154
2155 names = (char *) (s + count);
2156
2157 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2158 {
2159 asymbol *sym;
2160
2161 while (r < opd->relocation + relcount
2162 && r->address < syms[i]->value + opd->vma)
2163 ++r;
2164
2165 if (r == opd->relocation + relcount)
2166 break;
2167
2168 if (r->address != syms[i]->value + opd->vma)
2169 continue;
2170
2171 if (r->howto->type != R_PPC64_ADDR64)
2172 continue;
2173
2174 sym = *r->sym_ptr_ptr;
2175 if (!sym_exists_at (syms, opdsymend, symcount,
2176 sym->section->id, sym->value + r->addend))
2177 {
2178 size_t len;
2179
2180 *s = *syms[i];
2181 s->flags |= BSF_SYNTHETIC;
2182 s->section = sym->section;
2183 s->value = sym->value + r->addend;
2184 s->name = names;
2185 *names++ = '.';
2186 len = strlen (syms[i]->name);
2187 memcpy (names, syms[i]->name, len + 1);
2188 names += len + 1;
2189 /* Have udata.p point back to the original symbol this
2190 synthetic symbol was derived from. */
2191 s->udata.p = syms[i];
2192 s++;
2193 }
2194 }
2195 }
2196 else
2197 {
2198 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2199 bfd_byte *contents = NULL;
2200 size_t size;
2201 size_t plt_count = 0;
2202 bfd_vma glink_vma = 0, resolv_vma = 0;
2203 asection *dynamic, *glink = NULL, *relplt = NULL;
2204 arelent *p;
2205
2206 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2207 {
2208 free_contents_and_exit_err:
2209 count = -1;
2210 free_contents_and_exit:
2211 if (contents)
2212 free (contents);
2213 goto done;
2214 }
2215
2216 size = 0;
2217 for (i = secsymend; i < opdsymend; ++i)
2218 {
2219 bfd_vma ent;
2220
2221 /* Ignore bogus symbols. */
2222 if (syms[i]->value > opd->size - 8)
2223 continue;
2224
2225 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2226 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2227 {
2228 ++count;
2229 size += sizeof (asymbol);
2230 size += strlen (syms[i]->name) + 2;
2231 }
2232 }
2233
2234 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2235 if (dyn_count != 0
2236 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2237 {
2238 bfd_byte *dynbuf, *extdyn, *extdynend;
2239 size_t extdynsize;
2240 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2241
2242 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2243 goto free_contents_and_exit_err;
2244
2245 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2246 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2247
2248 extdyn = dynbuf;
2249 extdynend = extdyn + dynamic->size;
2250 for (; extdyn < extdynend; extdyn += extdynsize)
2251 {
2252 Elf_Internal_Dyn dyn;
2253 (*swap_dyn_in) (abfd, extdyn, &dyn);
2254
2255 if (dyn.d_tag == DT_NULL)
2256 break;
2257
2258 if (dyn.d_tag == DT_PPC64_GLINK)
2259 {
2260 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2261 See comment in ppc64_elf_finish_dynamic_sections. */
2262 glink_vma = dyn.d_un.d_val + 8 * 4;
2263 /* The .glink section usually does not survive the final
2264 link; search for the section (usually .text) where the
2265 glink stubs now reside. */
2266 glink = bfd_sections_find_if (abfd, section_covers_vma,
2267 &glink_vma);
2268 break;
2269 }
2270 }
2271
2272 free (dynbuf);
2273 }
2274
2275 if (glink != NULL)
2276 {
2277 /* Determine __glink trampoline by reading the relative branch
2278 from the first glink stub. */
2279 bfd_byte buf[4];
2280 unsigned int off = 0;
2281
2282 while (bfd_get_section_contents (abfd, glink, buf,
2283 glink_vma + off - glink->vma, 4))
2284 {
2285 unsigned int insn = bfd_get_32 (abfd, buf);
2286 insn ^= B_DOT;
2287 if ((insn & ~0x3fffffc) == 0)
2288 {
2289 resolv_vma
2290 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2291 break;
2292 }
2293 off += 4;
2294 if (off > 4)
2295 break;
2296 }
2297
2298 if (resolv_vma)
2299 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2300
2301 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2302 if (relplt != NULL)
2303 {
2304 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2305 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
2306 goto free_contents_and_exit_err;
2307
2308 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2309 size += plt_count * sizeof (asymbol);
2310
2311 p = relplt->relocation;
2312 for (i = 0; i < plt_count; i++, p++)
2313 {
2314 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2315 if (p->addend != 0)
2316 size += sizeof ("+0x") - 1 + 16;
2317 }
2318 }
2319 }
2320
2321 if (size == 0)
2322 goto free_contents_and_exit;
2323 s = *ret = bfd_malloc (size);
2324 if (s == NULL)
2325 goto free_contents_and_exit_err;
2326
2327 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2328
2329 for (i = secsymend; i < opdsymend; ++i)
2330 {
2331 bfd_vma ent;
2332
2333 if (syms[i]->value > opd->size - 8)
2334 continue;
2335
2336 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2337 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2338 {
2339 size_t lo, hi;
2340 size_t len;
2341 asection *sec = abfd->sections;
2342
2343 *s = *syms[i];
2344 lo = codesecsym;
2345 hi = codesecsymend;
2346 while (lo < hi)
2347 {
2348 size_t mid = (lo + hi) >> 1;
2349 if (syms[mid]->section->vma < ent)
2350 lo = mid + 1;
2351 else if (syms[mid]->section->vma > ent)
2352 hi = mid;
2353 else
2354 {
2355 sec = syms[mid]->section;
2356 break;
2357 }
2358 }
2359
2360 if (lo >= hi && lo > codesecsym)
2361 sec = syms[lo - 1]->section;
2362
2363 for (; sec != NULL; sec = sec->next)
2364 {
2365 if (sec->vma > ent)
2366 break;
2367 /* SEC_LOAD may not be set if SEC is from a separate debug
2368 info file. */
2369 if ((sec->flags & SEC_ALLOC) == 0)
2370 break;
2371 if ((sec->flags & SEC_CODE) != 0)
2372 s->section = sec;
2373 }
2374 s->flags |= BSF_SYNTHETIC;
2375 s->value = ent - s->section->vma;
2376 s->name = names;
2377 *names++ = '.';
2378 len = strlen (syms[i]->name);
2379 memcpy (names, syms[i]->name, len + 1);
2380 names += len + 1;
2381 /* Have udata.p point back to the original symbol this
2382 synthetic symbol was derived from. */
2383 s->udata.p = syms[i];
2384 s++;
2385 }
2386 }
2387 free (contents);
2388
2389 if (glink != NULL && relplt != NULL)
2390 {
2391 if (resolv_vma)
2392 {
2393 /* Add a symbol for the main glink trampoline. */
2394 memset (s, 0, sizeof *s);
2395 s->the_bfd = abfd;
2396 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2397 s->section = glink;
2398 s->value = resolv_vma - glink->vma;
2399 s->name = names;
2400 memcpy (names, "__glink_PLTresolve",
2401 sizeof ("__glink_PLTresolve"));
2402 names += sizeof ("__glink_PLTresolve");
2403 s++;
2404 count++;
2405 }
2406
2407 /* FIXME: It would be very much nicer to put sym@plt on the
2408 stub rather than on the glink branch table entry. The
2409 objdump disassembler would then use a sensible symbol
2410 name on plt calls. The difficulty in doing so is
2411 a) finding the stubs, and,
2412 b) matching stubs against plt entries, and,
2413 c) there can be multiple stubs for a given plt entry.
2414
2415 Solving (a) could be done by code scanning, but older
2416 ppc64 binaries used different stubs to current code.
2417 (b) is the tricky one since you need to known the toc
2418 pointer for at least one function that uses a pic stub to
2419 be able to calculate the plt address referenced.
2420 (c) means gdb would need to set multiple breakpoints (or
2421 find the glink branch itself) when setting breakpoints
2422 for pending shared library loads. */
2423 p = relplt->relocation;
2424 for (i = 0; i < plt_count; i++, p++)
2425 {
2426 size_t len;
2427
2428 *s = **p->sym_ptr_ptr;
2429 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2430 we are defining a symbol, ensure one of them is set. */
2431 if ((s->flags & BSF_LOCAL) == 0)
2432 s->flags |= BSF_GLOBAL;
2433 s->flags |= BSF_SYNTHETIC;
2434 s->section = glink;
2435 s->value = glink_vma - glink->vma;
2436 s->name = names;
2437 s->udata.p = NULL;
2438 len = strlen ((*p->sym_ptr_ptr)->name);
2439 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2440 names += len;
2441 if (p->addend != 0)
2442 {
2443 memcpy (names, "+0x", sizeof ("+0x") - 1);
2444 names += sizeof ("+0x") - 1;
2445 bfd_sprintf_vma (abfd, names, p->addend);
2446 names += strlen (names);
2447 }
2448 memcpy (names, "@plt", sizeof ("@plt"));
2449 names += sizeof ("@plt");
2450 s++;
2451 if (abi < 2)
2452 {
2453 glink_vma += 8;
2454 if (i >= 0x8000)
2455 glink_vma += 4;
2456 }
2457 else
2458 glink_vma += 4;
2459 }
2460 count += plt_count;
2461 }
2462 }
2463
2464 done:
2465 free (syms);
2466 return count;
2467 }
2468 \f
2469 /* The following functions are specific to the ELF linker, while
2470 functions above are used generally. Those named ppc64_elf_* are
2471 called by the main ELF linker code. They appear in this file more
2472 or less in the order in which they are called. eg.
2473 ppc64_elf_check_relocs is called early in the link process,
2474 ppc64_elf_finish_dynamic_sections is one of the last functions
2475 called.
2476
2477 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2478 functions have both a function code symbol and a function descriptor
2479 symbol. A call to foo in a relocatable object file looks like:
2480
2481 . .text
2482 . x:
2483 . bl .foo
2484 . nop
2485
2486 The function definition in another object file might be:
2487
2488 . .section .opd
2489 . foo: .quad .foo
2490 . .quad .TOC.@tocbase
2491 . .quad 0
2492 .
2493 . .text
2494 . .foo: blr
2495
2496 When the linker resolves the call during a static link, the branch
2497 unsurprisingly just goes to .foo and the .opd information is unused.
2498 If the function definition is in a shared library, things are a little
2499 different: The call goes via a plt call stub, the opd information gets
2500 copied to the plt, and the linker patches the nop.
2501
2502 . x:
2503 . bl .foo_stub
2504 . ld 2,40(1)
2505 .
2506 .
2507 . .foo_stub:
2508 . std 2,40(1) # in practice, the call stub
2509 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2510 . addi 11,11,Lfoo@toc@l # this is the general idea
2511 . ld 12,0(11)
2512 . ld 2,8(11)
2513 . mtctr 12
2514 . ld 11,16(11)
2515 . bctr
2516 .
2517 . .section .plt
2518 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2519
2520 The "reloc ()" notation is supposed to indicate that the linker emits
2521 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2522 copying.
2523
2524 What are the difficulties here? Well, firstly, the relocations
2525 examined by the linker in check_relocs are against the function code
2526 sym .foo, while the dynamic relocation in the plt is emitted against
2527 the function descriptor symbol, foo. Somewhere along the line, we need
2528 to carefully copy dynamic link information from one symbol to the other.
2529 Secondly, the generic part of the elf linker will make .foo a dynamic
2530 symbol as is normal for most other backends. We need foo dynamic
2531 instead, at least for an application final link. However, when
2532 creating a shared library containing foo, we need to have both symbols
2533 dynamic so that references to .foo are satisfied during the early
2534 stages of linking. Otherwise the linker might decide to pull in a
2535 definition from some other object, eg. a static library.
2536
2537 Update: As of August 2004, we support a new convention. Function
2538 calls may use the function descriptor symbol, ie. "bl foo". This
2539 behaves exactly as "bl .foo". */
2540
2541 /* Of those relocs that might be copied as dynamic relocs, this
2542 function selects those that must be copied when linking a shared
2543 library or PIE, even when the symbol is local. */
2544
2545 static int
2546 must_be_dyn_reloc (struct bfd_link_info *info,
2547 enum elf_ppc64_reloc_type r_type)
2548 {
2549 switch (r_type)
2550 {
2551 default:
2552 /* Only relative relocs can be resolved when the object load
2553 address isn't fixed. DTPREL64 is excluded because the
2554 dynamic linker needs to differentiate global dynamic from
2555 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2556 return 1;
2557
2558 case R_PPC64_REL32:
2559 case R_PPC64_REL64:
2560 case R_PPC64_REL30:
2561 return 0;
2562
2563 case R_PPC64_TPREL16:
2564 case R_PPC64_TPREL16_LO:
2565 case R_PPC64_TPREL16_HI:
2566 case R_PPC64_TPREL16_HA:
2567 case R_PPC64_TPREL16_DS:
2568 case R_PPC64_TPREL16_LO_DS:
2569 case R_PPC64_TPREL16_HIGH:
2570 case R_PPC64_TPREL16_HIGHA:
2571 case R_PPC64_TPREL16_HIGHER:
2572 case R_PPC64_TPREL16_HIGHERA:
2573 case R_PPC64_TPREL16_HIGHEST:
2574 case R_PPC64_TPREL16_HIGHESTA:
2575 case R_PPC64_TPREL64:
2576 /* These relocations are relative but in a shared library the
2577 linker doesn't know the thread pointer base. */
2578 return bfd_link_dll (info);
2579 }
2580 }
2581
2582 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2583 copying dynamic variables from a shared lib into an app's dynbss
2584 section, and instead use a dynamic relocation to point into the
2585 shared lib. With code that gcc generates, it's vital that this be
2586 enabled; In the PowerPC64 ABI, the address of a function is actually
2587 the address of a function descriptor, which resides in the .opd
2588 section. gcc uses the descriptor directly rather than going via the
2589 GOT as some other ABI's do, which means that initialized function
2590 pointers must reference the descriptor. Thus, a function pointer
2591 initialized to the address of a function in a shared library will
2592 either require a copy reloc, or a dynamic reloc. Using a copy reloc
2593 redefines the function descriptor symbol to point to the copy. This
2594 presents a problem as a plt entry for that function is also
2595 initialized from the function descriptor symbol and the copy reloc
2596 may not be initialized first. */
2597 #define ELIMINATE_COPY_RELOCS 1
2598
2599 /* Section name for stubs is the associated section name plus this
2600 string. */
2601 #define STUB_SUFFIX ".stub"
2602
2603 /* Linker stubs.
2604 ppc_stub_long_branch:
2605 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2606 destination, but a 24 bit branch in a stub section will reach.
2607 . b dest
2608
2609 ppc_stub_plt_branch:
2610 Similar to the above, but a 24 bit branch in the stub section won't
2611 reach its destination.
2612 . addis %r11,%r2,xxx@toc@ha
2613 . ld %r12,xxx@toc@l(%r11)
2614 . mtctr %r12
2615 . bctr
2616
2617 ppc_stub_plt_call:
2618 Used to call a function in a shared library. If it so happens that
2619 the plt entry referenced crosses a 64k boundary, then an extra
2620 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2621 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
2622 . addis %r11,%r2,xxx@toc@ha
2623 . ld %r12,xxx+0@toc@l(%r11)
2624 . mtctr %r12
2625 . ld %r2,xxx+8@toc@l(%r11)
2626 . ld %r11,xxx+16@toc@l(%r11)
2627 . bctr
2628
2629 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2630 code to adjust the value and save r2 to support multiple toc sections.
2631 A ppc_stub_long_branch with an r2 offset looks like:
2632 . std %r2,40(%r1)
2633 . addis %r2,%r2,off@ha
2634 . addi %r2,%r2,off@l
2635 . b dest
2636
2637 A ppc_stub_plt_branch with an r2 offset looks like:
2638 . std %r2,40(%r1)
2639 . addis %r11,%r2,xxx@toc@ha
2640 . ld %r12,xxx@toc@l(%r11)
2641 . addis %r2,%r2,off@ha
2642 . addi %r2,%r2,off@l
2643 . mtctr %r12
2644 . bctr
2645
2646 All of the above stubs are shown as their ELFv1 variants. ELFv2
2647 variants exist too, simpler for plt calls since a new toc pointer
2648 and static chain are not loaded by the stub. In addition, ELFv2
2649 has some more complex stubs to handle calls marked with NOTOC
2650 relocs from functions where r2 is not a valid toc pointer. These
2651 come in two flavours, the ones shown below, and _both variants that
2652 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2653 one call is from a function where r2 is used as the toc pointer but
2654 needs a toc adjusting stub for small-model multi-toc, and another
2655 call is from a function where r2 is not valid.
2656 ppc_stub_long_branch_notoc:
2657 . mflr %r12
2658 . bcl 20,31,1f
2659 . 1:
2660 . mflr %r11
2661 . mtlr %r12
2662 . addis %r12,%r11,dest-1b@ha
2663 . addi %r12,%r12,dest-1b@l
2664 . b dest
2665
2666 ppc_stub_plt_branch_notoc:
2667 . mflr %r12
2668 . bcl 20,31,1f
2669 . 1:
2670 . mflr %r11
2671 . mtlr %r12
2672 . lis %r12,xxx-1b@highest
2673 . ori %r12,%r12,xxx-1b@higher
2674 . sldi %r12,%r12,32
2675 . oris %r12,%r12,xxx-1b@high
2676 . ori %r12,%r12,xxx-1b@l
2677 . add %r12,%r11,%r12
2678 . mtctr %r12
2679 . bctr
2680
2681 ppc_stub_plt_call_notoc:
2682 . mflr %r12
2683 . bcl 20,31,1f
2684 . 1:
2685 . mflr %r11
2686 . mtlr %r12
2687 . lis %r12,xxx-1b@highest
2688 . ori %r12,%r12,xxx-1b@higher
2689 . sldi %r12,%r12,32
2690 . oris %r12,%r12,xxx-1b@high
2691 . ori %r12,%r12,xxx-1b@l
2692 . ldx %r12,%r11,%r12
2693 . mtctr %r12
2694 . bctr
2695
2696 In cases where the high instructions would add zero, they are
2697 omitted and following instructions modified in some cases.
2698
2699 For a given stub group (a set of sections all using the same toc
2700 pointer value) there will be just one stub type used for any
2701 particular function symbol. For example, if printf is called from
2702 code with the tocsave optimization (ie. r2 saved in function
2703 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2704 and from other code without the tocsave optimization requiring a
2705 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2706 type will be created. Calls with the tocsave optimization will
2707 enter this stub after the instruction saving r2. A similar
2708 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2709 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2710 to call an external function like printf. If other calls to printf
2711 require a ppc_stub_plt_call linkage stub then a single
2712 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2713 call. If other calls to printf require a ppc_stub_plt_call_r2save
2714 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2715 be created and calls not requiring r2 to be saved will enter the
2716 stub after the r2 save instruction. There is an analogous
2717 hierarchy of long branch and plt branch stubs for local call
2718 linkage. */
2719
2720 enum ppc_stub_type
2721 {
2722 ppc_stub_none,
2723 ppc_stub_long_branch,
2724 ppc_stub_long_branch_r2off,
2725 ppc_stub_long_branch_notoc,
2726 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
2727 ppc_stub_plt_branch,
2728 ppc_stub_plt_branch_r2off,
2729 ppc_stub_plt_branch_notoc,
2730 ppc_stub_plt_branch_both,
2731 ppc_stub_plt_call,
2732 ppc_stub_plt_call_r2save,
2733 ppc_stub_plt_call_notoc,
2734 ppc_stub_plt_call_both,
2735 ppc_stub_global_entry,
2736 ppc_stub_save_res
2737 };
2738
2739 /* Information on stub grouping. */
2740 struct map_stub
2741 {
2742 /* The stub section. */
2743 asection *stub_sec;
2744 /* This is the section to which stubs in the group will be attached. */
2745 asection *link_sec;
2746 /* Next group. */
2747 struct map_stub *next;
2748 /* Whether to emit a copy of register save/restore functions in this
2749 group. */
2750 int needs_save_res;
2751 /* Current offset within stubs after the insn restoring lr in a
2752 _notoc or _both stub using bcl for pc-relative addressing, or
2753 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2754 unsigned int lr_restore;
2755 /* Accumulated size of EH info emitted to describe return address
2756 if stubs modify lr. Does not include 17 byte FDE header. */
2757 unsigned int eh_size;
2758 /* Offset in glink_eh_frame to the start of EH info for this group. */
2759 unsigned int eh_base;
2760 };
2761
2762 struct ppc_stub_hash_entry
2763 {
2764 /* Base hash table entry structure. */
2765 struct bfd_hash_entry root;
2766
2767 enum ppc_stub_type stub_type;
2768
2769 /* Group information. */
2770 struct map_stub *group;
2771
2772 /* Offset within stub_sec of the beginning of this stub. */
2773 bfd_vma stub_offset;
2774
2775 /* Given the symbol's value and its section we can determine its final
2776 value when building the stubs (so the stub knows where to jump. */
2777 bfd_vma target_value;
2778 asection *target_section;
2779
2780 /* The symbol table entry, if any, that this was derived from. */
2781 struct ppc_link_hash_entry *h;
2782 struct plt_entry *plt_ent;
2783
2784 /* Symbol type. */
2785 unsigned char symtype;
2786
2787 /* Symbol st_other. */
2788 unsigned char other;
2789 };
2790
2791 struct ppc_branch_hash_entry
2792 {
2793 /* Base hash table entry structure. */
2794 struct bfd_hash_entry root;
2795
2796 /* Offset within branch lookup table. */
2797 unsigned int offset;
2798
2799 /* Generation marker. */
2800 unsigned int iter;
2801 };
2802
2803 /* Used to track dynamic relocations for local symbols. */
2804 struct ppc_dyn_relocs
2805 {
2806 struct ppc_dyn_relocs *next;
2807
2808 /* The input section of the reloc. */
2809 asection *sec;
2810
2811 /* Total number of relocs copied for the input section. */
2812 unsigned int count : 31;
2813
2814 /* Whether this entry is for STT_GNU_IFUNC symbols. */
2815 unsigned int ifunc : 1;
2816 };
2817
2818 struct ppc_link_hash_entry
2819 {
2820 struct elf_link_hash_entry elf;
2821
2822 union
2823 {
2824 /* A pointer to the most recently used stub hash entry against this
2825 symbol. */
2826 struct ppc_stub_hash_entry *stub_cache;
2827
2828 /* A pointer to the next symbol starting with a '.' */
2829 struct ppc_link_hash_entry *next_dot_sym;
2830 } u;
2831
2832 /* Track dynamic relocs copied for this symbol. */
2833 struct elf_dyn_relocs *dyn_relocs;
2834
2835 /* Link between function code and descriptor symbols. */
2836 struct ppc_link_hash_entry *oh;
2837
2838 /* Flag function code and descriptor symbols. */
2839 unsigned int is_func:1;
2840 unsigned int is_func_descriptor:1;
2841 unsigned int fake:1;
2842
2843 /* Whether global opd/toc sym has been adjusted or not.
2844 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
2845 should be set for all globals defined in any opd/toc section. */
2846 unsigned int adjust_done:1;
2847
2848 /* Set if this is an out-of-line register save/restore function,
2849 with non-standard calling convention. */
2850 unsigned int save_res:1;
2851
2852 /* Set if a duplicate symbol with non-zero localentry is detected,
2853 even when the duplicate symbol does not provide a definition. */
2854 unsigned int non_zero_localentry:1;
2855
2856 /* Contexts in which symbol is used in the GOT (or TOC).
2857 Bits are or'd into the mask as the corresponding relocs are
2858 encountered during check_relocs, with TLS_TLS being set when any
2859 of the other TLS bits are set. tls_optimize clears bits when
2860 optimizing to indicate the corresponding GOT entry type is not
2861 needed. If set, TLS_TLS is never cleared. tls_optimize may also
2862 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
2863 separate flag rather than setting TPREL just for convenience in
2864 distinguishing the two cases.
2865 These flags are also kept for local symbols. */
2866 #define TLS_TLS 1 /* Any TLS reloc. */
2867 #define TLS_GD 2 /* GD reloc. */
2868 #define TLS_LD 4 /* LD reloc. */
2869 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
2870 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
2871 #define TLS_MARK 32 /* __tls_get_addr call marked. */
2872 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
2873 #define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
2874 unsigned char tls_mask;
2875
2876 /* The above field is also used to mark function symbols. In which
2877 case TLS_TLS will be 0. */
2878 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2879 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
2880 #define NON_GOT 256 /* local symbol plt, not stored. */
2881 };
2882
2883 /* ppc64 ELF linker hash table. */
2884
2885 struct ppc_link_hash_table
2886 {
2887 struct elf_link_hash_table elf;
2888
2889 /* The stub hash table. */
2890 struct bfd_hash_table stub_hash_table;
2891
2892 /* Another hash table for plt_branch stubs. */
2893 struct bfd_hash_table branch_hash_table;
2894
2895 /* Hash table for function prologue tocsave. */
2896 htab_t tocsave_htab;
2897
2898 /* Various options and other info passed from the linker. */
2899 struct ppc64_elf_params *params;
2900
2901 /* The size of sec_info below. */
2902 unsigned int sec_info_arr_size;
2903
2904 /* Per-section array of extra section info. Done this way rather
2905 than as part of ppc64_elf_section_data so we have the info for
2906 non-ppc64 sections. */
2907 struct
2908 {
2909 /* Along with elf_gp, specifies the TOC pointer used by this section. */
2910 bfd_vma toc_off;
2911
2912 union
2913 {
2914 /* The section group that this section belongs to. */
2915 struct map_stub *group;
2916 /* A temp section list pointer. */
2917 asection *list;
2918 } u;
2919 } *sec_info;
2920
2921 /* Linked list of groups. */
2922 struct map_stub *group;
2923
2924 /* Temp used when calculating TOC pointers. */
2925 bfd_vma toc_curr;
2926 bfd *toc_bfd;
2927 asection *toc_first_sec;
2928
2929 /* Used when adding symbols. */
2930 struct ppc_link_hash_entry *dot_syms;
2931
2932 /* Shortcuts to get to dynamic linker sections. */
2933 asection *glink;
2934 asection *global_entry;
2935 asection *sfpr;
2936 asection *pltlocal;
2937 asection *relpltlocal;
2938 asection *brlt;
2939 asection *relbrlt;
2940 asection *glink_eh_frame;
2941
2942 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
2943 struct ppc_link_hash_entry *tls_get_addr;
2944 struct ppc_link_hash_entry *tls_get_addr_fd;
2945
2946 /* The size of reliplt used by got entry relocs. */
2947 bfd_size_type got_reli_size;
2948
2949 /* Statistics. */
2950 unsigned long stub_count[ppc_stub_global_entry];
2951
2952 /* Number of stubs against global syms. */
2953 unsigned long stub_globals;
2954
2955 /* Set if we're linking code with function descriptors. */
2956 unsigned int opd_abi:1;
2957
2958 /* Support for multiple toc sections. */
2959 unsigned int do_multi_toc:1;
2960 unsigned int multi_toc_needed:1;
2961 unsigned int second_toc_pass:1;
2962 unsigned int do_toc_opt:1;
2963
2964 /* Set if tls optimization is enabled. */
2965 unsigned int do_tls_opt:1;
2966
2967 /* Set if inline plt calls should be converted to direct calls. */
2968 unsigned int can_convert_all_inline_plt:1;
2969
2970 /* Set on error. */
2971 unsigned int stub_error:1;
2972
2973 /* Whether func_desc_adjust needs to be run over symbols. */
2974 unsigned int need_func_desc_adj:1;
2975
2976 /* Whether there exist local gnu indirect function resolvers,
2977 referenced by dynamic relocations. */
2978 unsigned int local_ifunc_resolver:1;
2979 unsigned int maybe_local_ifunc_resolver:1;
2980
2981 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
2982 unsigned int has_plt_localentry0:1;
2983
2984 /* Incremented every time we size stubs. */
2985 unsigned int stub_iteration;
2986
2987 /* Small local sym cache. */
2988 struct sym_cache sym_cache;
2989 };
2990
2991 /* Rename some of the generic section flags to better document how they
2992 are used here. */
2993
2994 /* Nonzero if this section has TLS related relocations. */
2995 #define has_tls_reloc sec_flg0
2996
2997 /* Nonzero if this section has an old-style call to __tls_get_addr. */
2998 #define has_tls_get_addr_call sec_flg1
2999
3000 /* Nonzero if this section has any toc or got relocs. */
3001 #define has_toc_reloc sec_flg2
3002
3003 /* Nonzero if this section has a call to another section that uses
3004 the toc or got. */
3005 #define makes_toc_func_call sec_flg3
3006
3007 /* Recursion protection when determining above flag. */
3008 #define call_check_in_progress sec_flg4
3009 #define call_check_done sec_flg5
3010
3011 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3012
3013 #define ppc_hash_table(p) \
3014 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3015 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3016
3017 #define ppc_stub_hash_lookup(table, string, create, copy) \
3018 ((struct ppc_stub_hash_entry *) \
3019 bfd_hash_lookup ((table), (string), (create), (copy)))
3020
3021 #define ppc_branch_hash_lookup(table, string, create, copy) \
3022 ((struct ppc_branch_hash_entry *) \
3023 bfd_hash_lookup ((table), (string), (create), (copy)))
3024
3025 /* Create an entry in the stub hash table. */
3026
3027 static struct bfd_hash_entry *
3028 stub_hash_newfunc (struct bfd_hash_entry *entry,
3029 struct bfd_hash_table *table,
3030 const char *string)
3031 {
3032 /* Allocate the structure if it has not already been allocated by a
3033 subclass. */
3034 if (entry == NULL)
3035 {
3036 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3037 if (entry == NULL)
3038 return entry;
3039 }
3040
3041 /* Call the allocation method of the superclass. */
3042 entry = bfd_hash_newfunc (entry, table, string);
3043 if (entry != NULL)
3044 {
3045 struct ppc_stub_hash_entry *eh;
3046
3047 /* Initialize the local fields. */
3048 eh = (struct ppc_stub_hash_entry *) entry;
3049 eh->stub_type = ppc_stub_none;
3050 eh->group = NULL;
3051 eh->stub_offset = 0;
3052 eh->target_value = 0;
3053 eh->target_section = NULL;
3054 eh->h = NULL;
3055 eh->plt_ent = NULL;
3056 eh->other = 0;
3057 }
3058
3059 return entry;
3060 }
3061
3062 /* Create an entry in the branch hash table. */
3063
3064 static struct bfd_hash_entry *
3065 branch_hash_newfunc (struct bfd_hash_entry *entry,
3066 struct bfd_hash_table *table,
3067 const char *string)
3068 {
3069 /* Allocate the structure if it has not already been allocated by a
3070 subclass. */
3071 if (entry == NULL)
3072 {
3073 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3074 if (entry == NULL)
3075 return entry;
3076 }
3077
3078 /* Call the allocation method of the superclass. */
3079 entry = bfd_hash_newfunc (entry, table, string);
3080 if (entry != NULL)
3081 {
3082 struct ppc_branch_hash_entry *eh;
3083
3084 /* Initialize the local fields. */
3085 eh = (struct ppc_branch_hash_entry *) entry;
3086 eh->offset = 0;
3087 eh->iter = 0;
3088 }
3089
3090 return entry;
3091 }
3092
3093 /* Create an entry in a ppc64 ELF linker hash table. */
3094
3095 static struct bfd_hash_entry *
3096 link_hash_newfunc (struct bfd_hash_entry *entry,
3097 struct bfd_hash_table *table,
3098 const char *string)
3099 {
3100 /* Allocate the structure if it has not already been allocated by a
3101 subclass. */
3102 if (entry == NULL)
3103 {
3104 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3105 if (entry == NULL)
3106 return entry;
3107 }
3108
3109 /* Call the allocation method of the superclass. */
3110 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3111 if (entry != NULL)
3112 {
3113 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3114
3115 memset (&eh->u.stub_cache, 0,
3116 (sizeof (struct ppc_link_hash_entry)
3117 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3118
3119 /* When making function calls, old ABI code references function entry
3120 points (dot symbols), while new ABI code references the function
3121 descriptor symbol. We need to make any combination of reference and
3122 definition work together, without breaking archive linking.
3123
3124 For a defined function "foo" and an undefined call to "bar":
3125 An old object defines "foo" and ".foo", references ".bar" (possibly
3126 "bar" too).
3127 A new object defines "foo" and references "bar".
3128
3129 A new object thus has no problem with its undefined symbols being
3130 satisfied by definitions in an old object. On the other hand, the
3131 old object won't have ".bar" satisfied by a new object.
3132
3133 Keep a list of newly added dot-symbols. */
3134
3135 if (string[0] == '.')
3136 {
3137 struct ppc_link_hash_table *htab;
3138
3139 htab = (struct ppc_link_hash_table *) table;
3140 eh->u.next_dot_sym = htab->dot_syms;
3141 htab->dot_syms = eh;
3142 }
3143 }
3144
3145 return entry;
3146 }
3147
3148 struct tocsave_entry
3149 {
3150 asection *sec;
3151 bfd_vma offset;
3152 };
3153
3154 static hashval_t
3155 tocsave_htab_hash (const void *p)
3156 {
3157 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3158 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3159 }
3160
3161 static int
3162 tocsave_htab_eq (const void *p1, const void *p2)
3163 {
3164 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3165 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3166 return e1->sec == e2->sec && e1->offset == e2->offset;
3167 }
3168
3169 /* Destroy a ppc64 ELF linker hash table. */
3170
3171 static void
3172 ppc64_elf_link_hash_table_free (bfd *obfd)
3173 {
3174 struct ppc_link_hash_table *htab;
3175
3176 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3177 if (htab->tocsave_htab)
3178 htab_delete (htab->tocsave_htab);
3179 bfd_hash_table_free (&htab->branch_hash_table);
3180 bfd_hash_table_free (&htab->stub_hash_table);
3181 _bfd_elf_link_hash_table_free (obfd);
3182 }
3183
3184 /* Create a ppc64 ELF linker hash table. */
3185
3186 static struct bfd_link_hash_table *
3187 ppc64_elf_link_hash_table_create (bfd *abfd)
3188 {
3189 struct ppc_link_hash_table *htab;
3190 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3191
3192 htab = bfd_zmalloc (amt);
3193 if (htab == NULL)
3194 return NULL;
3195
3196 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3197 sizeof (struct ppc_link_hash_entry),
3198 PPC64_ELF_DATA))
3199 {
3200 free (htab);
3201 return NULL;
3202 }
3203
3204 /* Init the stub hash table too. */
3205 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3206 sizeof (struct ppc_stub_hash_entry)))
3207 {
3208 _bfd_elf_link_hash_table_free (abfd);
3209 return NULL;
3210 }
3211
3212 /* And the branch hash table. */
3213 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3214 sizeof (struct ppc_branch_hash_entry)))
3215 {
3216 bfd_hash_table_free (&htab->stub_hash_table);
3217 _bfd_elf_link_hash_table_free (abfd);
3218 return NULL;
3219 }
3220
3221 htab->tocsave_htab = htab_try_create (1024,
3222 tocsave_htab_hash,
3223 tocsave_htab_eq,
3224 NULL);
3225 if (htab->tocsave_htab == NULL)
3226 {
3227 ppc64_elf_link_hash_table_free (abfd);
3228 return NULL;
3229 }
3230 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3231
3232 /* Initializing two fields of the union is just cosmetic. We really
3233 only care about glist, but when compiled on a 32-bit host the
3234 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3235 debugger inspection of these fields look nicer. */
3236 htab->elf.init_got_refcount.refcount = 0;
3237 htab->elf.init_got_refcount.glist = NULL;
3238 htab->elf.init_plt_refcount.refcount = 0;
3239 htab->elf.init_plt_refcount.glist = NULL;
3240 htab->elf.init_got_offset.offset = 0;
3241 htab->elf.init_got_offset.glist = NULL;
3242 htab->elf.init_plt_offset.offset = 0;
3243 htab->elf.init_plt_offset.glist = NULL;
3244
3245 return &htab->elf.root;
3246 }
3247
3248 /* Create sections for linker generated code. */
3249
3250 static bfd_boolean
3251 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3252 {
3253 struct ppc_link_hash_table *htab;
3254 flagword flags;
3255
3256 htab = ppc_hash_table (info);
3257
3258 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3259 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3260 if (htab->params->save_restore_funcs)
3261 {
3262 /* Create .sfpr for code to save and restore fp regs. */
3263 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3264 flags);
3265 if (htab->sfpr == NULL
3266 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3267 return FALSE;
3268 }
3269
3270 if (bfd_link_relocatable (info))
3271 return TRUE;
3272
3273 /* Create .glink for lazy dynamic linking support. */
3274 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3275 flags);
3276 if (htab->glink == NULL
3277 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
3278 return FALSE;
3279
3280 /* The part of .glink used by global entry stubs, separate so that
3281 it can be aligned appropriately without affecting htab->glink. */
3282 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3283 flags);
3284 if (htab->global_entry == NULL
3285 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
3286 return FALSE;
3287
3288 if (!info->no_ld_generated_unwind_info)
3289 {
3290 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3291 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3292 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3293 ".eh_frame",
3294 flags);
3295 if (htab->glink_eh_frame == NULL
3296 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3297 return FALSE;
3298 }
3299
3300 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3301 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3302 if (htab->elf.iplt == NULL
3303 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
3304 return FALSE;
3305
3306 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3307 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3308 htab->elf.irelplt
3309 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3310 if (htab->elf.irelplt == NULL
3311 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
3312 return FALSE;
3313
3314 /* Create branch lookup table for plt_branch stubs. */
3315 flags = (SEC_ALLOC | SEC_LOAD
3316 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3317 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3318 flags);
3319 if (htab->brlt == NULL
3320 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
3321 return FALSE;
3322
3323 /* Local plt entries, put in .branch_lt but a separate section for
3324 convenience. */
3325 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3326 flags);
3327 if (htab->pltlocal == NULL
3328 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
3329 return FALSE;
3330
3331 if (!bfd_link_pic (info))
3332 return TRUE;
3333
3334 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3335 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3336 htab->relbrlt
3337 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3338 if (htab->relbrlt == NULL
3339 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3340 return FALSE;
3341
3342 htab->relpltlocal
3343 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3344 if (htab->relpltlocal == NULL
3345 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
3346 return FALSE;
3347
3348 return TRUE;
3349 }
3350
3351 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3352
3353 bfd_boolean
3354 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3355 struct ppc64_elf_params *params)
3356 {
3357 struct ppc_link_hash_table *htab;
3358
3359 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3360
3361 /* Always hook our dynamic sections into the first bfd, which is the
3362 linker created stub bfd. This ensures that the GOT header is at
3363 the start of the output TOC section. */
3364 htab = ppc_hash_table (info);
3365 htab->elf.dynobj = params->stub_bfd;
3366 htab->params = params;
3367
3368 return create_linkage_sections (htab->elf.dynobj, info);
3369 }
3370
3371 /* Build a name for an entry in the stub hash table. */
3372
3373 static char *
3374 ppc_stub_name (const asection *input_section,
3375 const asection *sym_sec,
3376 const struct ppc_link_hash_entry *h,
3377 const Elf_Internal_Rela *rel)
3378 {
3379 char *stub_name;
3380 ssize_t len;
3381
3382 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3383 offsets from a sym as a branch target? In fact, we could
3384 probably assume the addend is always zero. */
3385 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3386
3387 if (h)
3388 {
3389 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3390 stub_name = bfd_malloc (len);
3391 if (stub_name == NULL)
3392 return stub_name;
3393
3394 len = sprintf (stub_name, "%08x.%s+%x",
3395 input_section->id & 0xffffffff,
3396 h->elf.root.root.string,
3397 (int) rel->r_addend & 0xffffffff);
3398 }
3399 else
3400 {
3401 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3402 stub_name = bfd_malloc (len);
3403 if (stub_name == NULL)
3404 return stub_name;
3405
3406 len = sprintf (stub_name, "%08x.%x:%x+%x",
3407 input_section->id & 0xffffffff,
3408 sym_sec->id & 0xffffffff,
3409 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3410 (int) rel->r_addend & 0xffffffff);
3411 }
3412 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3413 stub_name[len - 2] = 0;
3414 return stub_name;
3415 }
3416
3417 /* Look up an entry in the stub hash. Stub entries are cached because
3418 creating the stub name takes a bit of time. */
3419
3420 static struct ppc_stub_hash_entry *
3421 ppc_get_stub_entry (const asection *input_section,
3422 const asection *sym_sec,
3423 struct ppc_link_hash_entry *h,
3424 const Elf_Internal_Rela *rel,
3425 struct ppc_link_hash_table *htab)
3426 {
3427 struct ppc_stub_hash_entry *stub_entry;
3428 struct map_stub *group;
3429
3430 /* If this input section is part of a group of sections sharing one
3431 stub section, then use the id of the first section in the group.
3432 Stub names need to include a section id, as there may well be
3433 more than one stub used to reach say, printf, and we need to
3434 distinguish between them. */
3435 group = htab->sec_info[input_section->id].u.group;
3436 if (group == NULL)
3437 return NULL;
3438
3439 if (h != NULL && h->u.stub_cache != NULL
3440 && h->u.stub_cache->h == h
3441 && h->u.stub_cache->group == group)
3442 {
3443 stub_entry = h->u.stub_cache;
3444 }
3445 else
3446 {
3447 char *stub_name;
3448
3449 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3450 if (stub_name == NULL)
3451 return NULL;
3452
3453 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3454 stub_name, FALSE, FALSE);
3455 if (h != NULL)
3456 h->u.stub_cache = stub_entry;
3457
3458 free (stub_name);
3459 }
3460
3461 return stub_entry;
3462 }
3463
3464 /* Add a new stub entry to the stub hash. Not all fields of the new
3465 stub entry are initialised. */
3466
3467 static struct ppc_stub_hash_entry *
3468 ppc_add_stub (const char *stub_name,
3469 asection *section,
3470 struct bfd_link_info *info)
3471 {
3472 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3473 struct map_stub *group;
3474 asection *link_sec;
3475 asection *stub_sec;
3476 struct ppc_stub_hash_entry *stub_entry;
3477
3478 group = htab->sec_info[section->id].u.group;
3479 link_sec = group->link_sec;
3480 stub_sec = group->stub_sec;
3481 if (stub_sec == NULL)
3482 {
3483 size_t namelen;
3484 bfd_size_type len;
3485 char *s_name;
3486
3487 namelen = strlen (link_sec->name);
3488 len = namelen + sizeof (STUB_SUFFIX);
3489 s_name = bfd_alloc (htab->params->stub_bfd, len);
3490 if (s_name == NULL)
3491 return NULL;
3492
3493 memcpy (s_name, link_sec->name, namelen);
3494 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3495 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3496 if (stub_sec == NULL)
3497 return NULL;
3498 group->stub_sec = stub_sec;
3499 }
3500
3501 /* Enter this entry into the linker stub hash table. */
3502 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3503 TRUE, FALSE);
3504 if (stub_entry == NULL)
3505 {
3506 /* xgettext:c-format */
3507 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3508 section->owner, stub_name);
3509 return NULL;
3510 }
3511
3512 stub_entry->group = group;
3513 stub_entry->stub_offset = 0;
3514 return stub_entry;
3515 }
3516
3517 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3518 not already done. */
3519
3520 static bfd_boolean
3521 create_got_section (bfd *abfd, struct bfd_link_info *info)
3522 {
3523 asection *got, *relgot;
3524 flagword flags;
3525 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3526
3527 if (!is_ppc64_elf (abfd))
3528 return FALSE;
3529 if (htab == NULL)
3530 return FALSE;
3531
3532 if (!htab->elf.sgot
3533 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3534 return FALSE;
3535
3536 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3537 | SEC_LINKER_CREATED);
3538
3539 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3540 if (!got
3541 || !bfd_set_section_alignment (abfd, got, 3))
3542 return FALSE;
3543
3544 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3545 flags | SEC_READONLY);
3546 if (!relgot
3547 || !bfd_set_section_alignment (abfd, relgot, 3))
3548 return FALSE;
3549
3550 ppc64_elf_tdata (abfd)->got = got;
3551 ppc64_elf_tdata (abfd)->relgot = relgot;
3552 return TRUE;
3553 }
3554
3555 /* Follow indirect and warning symbol links. */
3556
3557 static inline struct bfd_link_hash_entry *
3558 follow_link (struct bfd_link_hash_entry *h)
3559 {
3560 while (h->type == bfd_link_hash_indirect
3561 || h->type == bfd_link_hash_warning)
3562 h = h->u.i.link;
3563 return h;
3564 }
3565
3566 static inline struct elf_link_hash_entry *
3567 elf_follow_link (struct elf_link_hash_entry *h)
3568 {
3569 return (struct elf_link_hash_entry *) follow_link (&h->root);
3570 }
3571
3572 static inline struct ppc_link_hash_entry *
3573 ppc_follow_link (struct ppc_link_hash_entry *h)
3574 {
3575 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3576 }
3577
3578 /* Merge PLT info on FROM with that on TO. */
3579
3580 static void
3581 move_plt_plist (struct ppc_link_hash_entry *from,
3582 struct ppc_link_hash_entry *to)
3583 {
3584 if (from->elf.plt.plist != NULL)
3585 {
3586 if (to->elf.plt.plist != NULL)
3587 {
3588 struct plt_entry **entp;
3589 struct plt_entry *ent;
3590
3591 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3592 {
3593 struct plt_entry *dent;
3594
3595 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3596 if (dent->addend == ent->addend)
3597 {
3598 dent->plt.refcount += ent->plt.refcount;
3599 *entp = ent->next;
3600 break;
3601 }
3602 if (dent == NULL)
3603 entp = &ent->next;
3604 }
3605 *entp = to->elf.plt.plist;
3606 }
3607
3608 to->elf.plt.plist = from->elf.plt.plist;
3609 from->elf.plt.plist = NULL;
3610 }
3611 }
3612
3613 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3614
3615 static void
3616 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3617 struct elf_link_hash_entry *dir,
3618 struct elf_link_hash_entry *ind)
3619 {
3620 struct ppc_link_hash_entry *edir, *eind;
3621
3622 edir = (struct ppc_link_hash_entry *) dir;
3623 eind = (struct ppc_link_hash_entry *) ind;
3624
3625 edir->is_func |= eind->is_func;
3626 edir->is_func_descriptor |= eind->is_func_descriptor;
3627 edir->tls_mask |= eind->tls_mask;
3628 if (eind->oh != NULL)
3629 edir->oh = ppc_follow_link (eind->oh);
3630
3631 if (edir->elf.versioned != versioned_hidden)
3632 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3633 edir->elf.ref_regular |= eind->elf.ref_regular;
3634 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3635 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3636 edir->elf.needs_plt |= eind->elf.needs_plt;
3637 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3638
3639 /* If we were called to copy over info for a weak sym, don't copy
3640 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3641 in order to simplify readonly_dynrelocs and save a field in the
3642 symbol hash entry, but that means dyn_relocs can't be used in any
3643 tests about a specific symbol, or affect other symbol flags which
3644 are then tested. */
3645 if (eind->elf.root.type != bfd_link_hash_indirect)
3646 return;
3647
3648 /* Copy over any dynamic relocs we may have on the indirect sym. */
3649 if (eind->dyn_relocs != NULL)
3650 {
3651 if (edir->dyn_relocs != NULL)
3652 {
3653 struct elf_dyn_relocs **pp;
3654 struct elf_dyn_relocs *p;
3655
3656 /* Add reloc counts against the indirect sym to the direct sym
3657 list. Merge any entries against the same section. */
3658 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3659 {
3660 struct elf_dyn_relocs *q;
3661
3662 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3663 if (q->sec == p->sec)
3664 {
3665 q->pc_count += p->pc_count;
3666 q->count += p->count;
3667 *pp = p->next;
3668 break;
3669 }
3670 if (q == NULL)
3671 pp = &p->next;
3672 }
3673 *pp = edir->dyn_relocs;
3674 }
3675
3676 edir->dyn_relocs = eind->dyn_relocs;
3677 eind->dyn_relocs = NULL;
3678 }
3679
3680 /* Copy over got entries that we may have already seen to the
3681 symbol which just became indirect. */
3682 if (eind->elf.got.glist != NULL)
3683 {
3684 if (edir->elf.got.glist != NULL)
3685 {
3686 struct got_entry **entp;
3687 struct got_entry *ent;
3688
3689 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3690 {
3691 struct got_entry *dent;
3692
3693 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3694 if (dent->addend == ent->addend
3695 && dent->owner == ent->owner
3696 && dent->tls_type == ent->tls_type)
3697 {
3698 dent->got.refcount += ent->got.refcount;
3699 *entp = ent->next;
3700 break;
3701 }
3702 if (dent == NULL)
3703 entp = &ent->next;
3704 }
3705 *entp = edir->elf.got.glist;
3706 }
3707
3708 edir->elf.got.glist = eind->elf.got.glist;
3709 eind->elf.got.glist = NULL;
3710 }
3711
3712 /* And plt entries. */
3713 move_plt_plist (eind, edir);
3714
3715 if (eind->elf.dynindx != -1)
3716 {
3717 if (edir->elf.dynindx != -1)
3718 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3719 edir->elf.dynstr_index);
3720 edir->elf.dynindx = eind->elf.dynindx;
3721 edir->elf.dynstr_index = eind->elf.dynstr_index;
3722 eind->elf.dynindx = -1;
3723 eind->elf.dynstr_index = 0;
3724 }
3725 }
3726
3727 /* Find the function descriptor hash entry from the given function code
3728 hash entry FH. Link the entries via their OH fields. */
3729
3730 static struct ppc_link_hash_entry *
3731 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3732 {
3733 struct ppc_link_hash_entry *fdh = fh->oh;
3734
3735 if (fdh == NULL)
3736 {
3737 const char *fd_name = fh->elf.root.root.string + 1;
3738
3739 fdh = (struct ppc_link_hash_entry *)
3740 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3741 if (fdh == NULL)
3742 return fdh;
3743
3744 fdh->is_func_descriptor = 1;
3745 fdh->oh = fh;
3746 fh->is_func = 1;
3747 fh->oh = fdh;
3748 }
3749
3750 fdh = ppc_follow_link (fdh);
3751 fdh->is_func_descriptor = 1;
3752 fdh->oh = fh;
3753 return fdh;
3754 }
3755
3756 /* Make a fake function descriptor sym for the undefined code sym FH. */
3757
3758 static struct ppc_link_hash_entry *
3759 make_fdh (struct bfd_link_info *info,
3760 struct ppc_link_hash_entry *fh)
3761 {
3762 bfd *abfd = fh->elf.root.u.undef.abfd;
3763 struct bfd_link_hash_entry *bh = NULL;
3764 struct ppc_link_hash_entry *fdh;
3765 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3766 ? BSF_WEAK
3767 : BSF_GLOBAL);
3768
3769 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3770 fh->elf.root.root.string + 1,
3771 flags, bfd_und_section_ptr, 0,
3772 NULL, FALSE, FALSE, &bh))
3773 return NULL;
3774
3775 fdh = (struct ppc_link_hash_entry *) bh;
3776 fdh->elf.non_elf = 0;
3777 fdh->fake = 1;
3778 fdh->is_func_descriptor = 1;
3779 fdh->oh = fh;
3780 fh->is_func = 1;
3781 fh->oh = fdh;
3782 return fdh;
3783 }
3784
3785 /* Fix function descriptor symbols defined in .opd sections to be
3786 function type. */
3787
3788 static bfd_boolean
3789 ppc64_elf_add_symbol_hook (bfd *ibfd,
3790 struct bfd_link_info *info,
3791 Elf_Internal_Sym *isym,
3792 const char **name,
3793 flagword *flags ATTRIBUTE_UNUSED,
3794 asection **sec,
3795 bfd_vma *value)
3796 {
3797 if (*sec != NULL
3798 && strcmp ((*sec)->name, ".opd") == 0)
3799 {
3800 asection *code_sec;
3801
3802 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3803 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
3804 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
3805
3806 /* If the symbol is a function defined in .opd, and the function
3807 code is in a discarded group, let it appear to be undefined. */
3808 if (!bfd_link_relocatable (info)
3809 && (*sec)->reloc_count != 0
3810 && opd_entry_value (*sec, *value, &code_sec, NULL,
3811 FALSE) != (bfd_vma) -1
3812 && discarded_section (code_sec))
3813 {
3814 *sec = bfd_und_section_ptr;
3815 isym->st_shndx = SHN_UNDEF;
3816 }
3817 }
3818 else if (*sec != NULL
3819 && strcmp ((*sec)->name, ".toc") == 0
3820 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
3821 {
3822 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3823 if (htab != NULL)
3824 htab->params->object_in_toc = 1;
3825 }
3826
3827 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3828 {
3829 if (abiversion (ibfd) == 0)
3830 set_abiversion (ibfd, 2);
3831 else if (abiversion (ibfd) == 1)
3832 {
3833 _bfd_error_handler (_("symbol '%s' has invalid st_other"
3834 " for ABI version 1"), *name);
3835 bfd_set_error (bfd_error_bad_value);
3836 return FALSE;
3837 }
3838 }
3839
3840 return TRUE;
3841 }
3842
3843 /* Merge non-visibility st_other attributes: local entry point. */
3844
3845 static void
3846 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
3847 const Elf_Internal_Sym *isym,
3848 bfd_boolean definition,
3849 bfd_boolean dynamic)
3850 {
3851 if (definition && (!dynamic || !h->def_regular))
3852 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
3853 | ELF_ST_VISIBILITY (h->other));
3854 }
3855
3856 /* Hook called on merging a symbol. We use this to clear "fake" since
3857 we now have a real symbol. */
3858
3859 static bfd_boolean
3860 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
3861 const Elf_Internal_Sym *isym,
3862 asection **psec ATTRIBUTE_UNUSED,
3863 bfd_boolean newdef ATTRIBUTE_UNUSED,
3864 bfd_boolean olddef ATTRIBUTE_UNUSED,
3865 bfd *oldbfd ATTRIBUTE_UNUSED,
3866 const asection *oldsec ATTRIBUTE_UNUSED)
3867 {
3868 ((struct ppc_link_hash_entry *) h)->fake = 0;
3869 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3870 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
3871 return TRUE;
3872 }
3873
3874 /* This function makes an old ABI object reference to ".bar" cause the
3875 inclusion of a new ABI object archive that defines "bar".
3876 NAME is a symbol defined in an archive. Return a symbol in the hash
3877 table that might be satisfied by the archive symbols. */
3878
3879 static struct elf_link_hash_entry *
3880 ppc64_elf_archive_symbol_lookup (bfd *abfd,
3881 struct bfd_link_info *info,
3882 const char *name)
3883 {
3884 struct elf_link_hash_entry *h;
3885 char *dot_name;
3886 size_t len;
3887
3888 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
3889 if (h != NULL
3890 /* Don't return this sym if it is a fake function descriptor
3891 created by add_symbol_adjust. */
3892 && !((struct ppc_link_hash_entry *) h)->fake)
3893 return h;
3894
3895 if (name[0] == '.')
3896 return h;
3897
3898 len = strlen (name);
3899 dot_name = bfd_alloc (abfd, len + 2);
3900 if (dot_name == NULL)
3901 return (struct elf_link_hash_entry *) -1;
3902 dot_name[0] = '.';
3903 memcpy (dot_name + 1, name, len + 1);
3904 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
3905 bfd_release (abfd, dot_name);
3906 return h;
3907 }
3908
3909 /* This function satisfies all old ABI object references to ".bar" if a
3910 new ABI object defines "bar". Well, at least, undefined dot symbols
3911 are made weak. This stops later archive searches from including an
3912 object if we already have a function descriptor definition. It also
3913 prevents the linker complaining about undefined symbols.
3914 We also check and correct mismatched symbol visibility here. The
3915 most restrictive visibility of the function descriptor and the
3916 function entry symbol is used. */
3917
3918 static bfd_boolean
3919 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
3920 {
3921 struct ppc_link_hash_table *htab;
3922 struct ppc_link_hash_entry *fdh;
3923
3924 if (eh->elf.root.type == bfd_link_hash_warning)
3925 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
3926
3927 if (eh->elf.root.type == bfd_link_hash_indirect)
3928 return TRUE;
3929
3930 if (eh->elf.root.root.string[0] != '.')
3931 abort ();
3932
3933 htab = ppc_hash_table (info);
3934 if (htab == NULL)
3935 return FALSE;
3936
3937 fdh = lookup_fdh (eh, htab);
3938 if (fdh == NULL
3939 && !bfd_link_relocatable (info)
3940 && (eh->elf.root.type == bfd_link_hash_undefined
3941 || eh->elf.root.type == bfd_link_hash_undefweak)
3942 && eh->elf.ref_regular)
3943 {
3944 /* Make an undefined function descriptor sym, in order to
3945 pull in an --as-needed shared lib. Archives are handled
3946 elsewhere. */
3947 fdh = make_fdh (info, eh);
3948 if (fdh == NULL)
3949 return FALSE;
3950 }
3951
3952 if (fdh != NULL)
3953 {
3954 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
3955 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
3956
3957 /* Make both descriptor and entry symbol have the most
3958 constraining visibility of either symbol. */
3959 if (entry_vis < descr_vis)
3960 fdh->elf.other += entry_vis - descr_vis;
3961 else if (entry_vis > descr_vis)
3962 eh->elf.other += descr_vis - entry_vis;
3963
3964 /* Propagate reference flags from entry symbol to function
3965 descriptor symbol. */
3966 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
3967 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
3968 fdh->elf.ref_regular |= eh->elf.ref_regular;
3969 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
3970
3971 if (!fdh->elf.forced_local
3972 && fdh->elf.dynindx == -1
3973 && fdh->elf.versioned != versioned_hidden
3974 && (bfd_link_dll (info)
3975 || fdh->elf.def_dynamic
3976 || fdh->elf.ref_dynamic)
3977 && (eh->elf.ref_regular
3978 || eh->elf.def_regular))
3979 {
3980 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
3981 return FALSE;
3982 }
3983 }
3984
3985 return TRUE;
3986 }
3987
3988 /* Set up opd section info and abiversion for IBFD, and process list
3989 of dot-symbols we made in link_hash_newfunc. */
3990
3991 static bfd_boolean
3992 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
3993 {
3994 struct ppc_link_hash_table *htab;
3995 struct ppc_link_hash_entry **p, *eh;
3996 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
3997
3998 if (opd != NULL && opd->size != 0)
3999 {
4000 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4001 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4002
4003 if (abiversion (ibfd) == 0)
4004 set_abiversion (ibfd, 1);
4005 else if (abiversion (ibfd) >= 2)
4006 {
4007 /* xgettext:c-format */
4008 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4009 ibfd, abiversion (ibfd));
4010 bfd_set_error (bfd_error_bad_value);
4011 return FALSE;
4012 }
4013 }
4014
4015 if (is_ppc64_elf (info->output_bfd))
4016 {
4017 /* For input files without an explicit abiversion in e_flags
4018 we should have flagged any with symbol st_other bits set
4019 as ELFv1 and above flagged those with .opd as ELFv2.
4020 Set the output abiversion if not yet set, and for any input
4021 still ambiguous, take its abiversion from the output.
4022 Differences in ABI are reported later. */
4023 if (abiversion (info->output_bfd) == 0)
4024 set_abiversion (info->output_bfd, abiversion (ibfd));
4025 else if (abiversion (ibfd) == 0)
4026 set_abiversion (ibfd, abiversion (info->output_bfd));
4027 }
4028
4029 htab = ppc_hash_table (info);
4030 if (htab == NULL)
4031 return TRUE;
4032
4033 if (opd != NULL && opd->size != 0
4034 && (ibfd->flags & DYNAMIC) == 0
4035 && (opd->flags & SEC_RELOC) != 0
4036 && opd->reloc_count != 0
4037 && !bfd_is_abs_section (opd->output_section)
4038 && info->gc_sections)
4039 {
4040 /* Garbage collection needs some extra help with .opd sections.
4041 We don't want to necessarily keep everything referenced by
4042 relocs in .opd, as that would keep all functions. Instead,
4043 if we reference an .opd symbol (a function descriptor), we
4044 want to keep the function code symbol's section. This is
4045 easy for global symbols, but for local syms we need to keep
4046 information about the associated function section. */
4047 bfd_size_type amt;
4048 asection **opd_sym_map;
4049 Elf_Internal_Shdr *symtab_hdr;
4050 Elf_Internal_Rela *relocs, *rel_end, *rel;
4051
4052 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4053 opd_sym_map = bfd_zalloc (ibfd, amt);
4054 if (opd_sym_map == NULL)
4055 return FALSE;
4056 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4057 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4058 info->keep_memory);
4059 if (relocs == NULL)
4060 return FALSE;
4061 symtab_hdr = &elf_symtab_hdr (ibfd);
4062 rel_end = relocs + opd->reloc_count - 1;
4063 for (rel = relocs; rel < rel_end; rel++)
4064 {
4065 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4066 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4067
4068 if (r_type == R_PPC64_ADDR64
4069 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4070 && r_symndx < symtab_hdr->sh_info)
4071 {
4072 Elf_Internal_Sym *isym;
4073 asection *s;
4074
4075 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4076 if (isym == NULL)
4077 {
4078 if (elf_section_data (opd)->relocs != relocs)
4079 free (relocs);
4080 return FALSE;
4081 }
4082
4083 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4084 if (s != NULL && s != opd)
4085 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4086 }
4087 }
4088 if (elf_section_data (opd)->relocs != relocs)
4089 free (relocs);
4090 }
4091
4092 p = &htab->dot_syms;
4093 while ((eh = *p) != NULL)
4094 {
4095 *p = NULL;
4096 if (&eh->elf == htab->elf.hgot)
4097 ;
4098 else if (htab->elf.hgot == NULL
4099 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4100 htab->elf.hgot = &eh->elf;
4101 else if (abiversion (ibfd) <= 1)
4102 {
4103 htab->need_func_desc_adj = 1;
4104 if (!add_symbol_adjust (eh, info))
4105 return FALSE;
4106 }
4107 p = &eh->u.next_dot_sym;
4108 }
4109 return TRUE;
4110 }
4111
4112 /* Undo hash table changes when an --as-needed input file is determined
4113 not to be needed. */
4114
4115 static bfd_boolean
4116 ppc64_elf_notice_as_needed (bfd *ibfd,
4117 struct bfd_link_info *info,
4118 enum notice_asneeded_action act)
4119 {
4120 if (act == notice_not_needed)
4121 {
4122 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4123
4124 if (htab == NULL)
4125 return FALSE;
4126
4127 htab->dot_syms = NULL;
4128 }
4129 return _bfd_elf_notice_as_needed (ibfd, info, act);
4130 }
4131
4132 /* If --just-symbols against a final linked binary, then assume we need
4133 toc adjusting stubs when calling functions defined there. */
4134
4135 static void
4136 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4137 {
4138 if ((sec->flags & SEC_CODE) != 0
4139 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4140 && is_ppc64_elf (sec->owner))
4141 {
4142 if (abiversion (sec->owner) >= 2
4143 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4144 sec->has_toc_reloc = 1;
4145 }
4146 _bfd_elf_link_just_syms (sec, info);
4147 }
4148
4149 static struct plt_entry **
4150 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4151 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4152 {
4153 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4154 struct plt_entry **local_plt;
4155 unsigned char *local_got_tls_masks;
4156
4157 if (local_got_ents == NULL)
4158 {
4159 bfd_size_type size = symtab_hdr->sh_info;
4160
4161 size *= (sizeof (*local_got_ents)
4162 + sizeof (*local_plt)
4163 + sizeof (*local_got_tls_masks));
4164 local_got_ents = bfd_zalloc (abfd, size);
4165 if (local_got_ents == NULL)
4166 return NULL;
4167 elf_local_got_ents (abfd) = local_got_ents;
4168 }
4169
4170 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4171 {
4172 struct got_entry *ent;
4173
4174 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4175 if (ent->addend == r_addend
4176 && ent->owner == abfd
4177 && ent->tls_type == tls_type)
4178 break;
4179 if (ent == NULL)
4180 {
4181 bfd_size_type amt = sizeof (*ent);
4182 ent = bfd_alloc (abfd, amt);
4183 if (ent == NULL)
4184 return FALSE;
4185 ent->next = local_got_ents[r_symndx];
4186 ent->addend = r_addend;
4187 ent->owner = abfd;
4188 ent->tls_type = tls_type;
4189 ent->is_indirect = FALSE;
4190 ent->got.refcount = 0;
4191 local_got_ents[r_symndx] = ent;
4192 }
4193 ent->got.refcount += 1;
4194 }
4195
4196 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4197 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4198 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4199
4200 return local_plt + r_symndx;
4201 }
4202
4203 static bfd_boolean
4204 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4205 {
4206 struct plt_entry *ent;
4207
4208 for (ent = *plist; ent != NULL; ent = ent->next)
4209 if (ent->addend == addend)
4210 break;
4211 if (ent == NULL)
4212 {
4213 bfd_size_type amt = sizeof (*ent);
4214 ent = bfd_alloc (abfd, amt);
4215 if (ent == NULL)
4216 return FALSE;
4217 ent->next = *plist;
4218 ent->addend = addend;
4219 ent->plt.refcount = 0;
4220 *plist = ent;
4221 }
4222 ent->plt.refcount += 1;
4223 return TRUE;
4224 }
4225
4226 static bfd_boolean
4227 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4228 {
4229 return (r_type == R_PPC64_REL24
4230 || r_type == R_PPC64_REL24_NOTOC
4231 || r_type == R_PPC64_REL14
4232 || r_type == R_PPC64_REL14_BRTAKEN
4233 || r_type == R_PPC64_REL14_BRNTAKEN
4234 || r_type == R_PPC64_ADDR24
4235 || r_type == R_PPC64_ADDR14
4236 || r_type == R_PPC64_ADDR14_BRTAKEN
4237 || r_type == R_PPC64_ADDR14_BRNTAKEN
4238 || r_type == R_PPC64_PLTCALL);
4239 }
4240
4241 /* Relocs on inline plt call sequence insns prior to the call. */
4242
4243 static bfd_boolean
4244 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4245 {
4246 return (r_type == R_PPC64_PLT16_HA
4247 || r_type == R_PPC64_PLT16_HI
4248 || r_type == R_PPC64_PLT16_LO
4249 || r_type == R_PPC64_PLT16_LO_DS
4250 || r_type == R_PPC64_PLTSEQ);
4251 }
4252
4253 /* Look through the relocs for a section during the first phase, and
4254 calculate needed space in the global offset table, procedure
4255 linkage table, and dynamic reloc sections. */
4256
4257 static bfd_boolean
4258 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4259 asection *sec, const Elf_Internal_Rela *relocs)
4260 {
4261 struct ppc_link_hash_table *htab;
4262 Elf_Internal_Shdr *symtab_hdr;
4263 struct elf_link_hash_entry **sym_hashes;
4264 const Elf_Internal_Rela *rel;
4265 const Elf_Internal_Rela *rel_end;
4266 asection *sreloc;
4267 struct elf_link_hash_entry *tga, *dottga;
4268 bfd_boolean is_opd;
4269
4270 if (bfd_link_relocatable (info))
4271 return TRUE;
4272
4273 /* Don't do anything special with non-loaded, non-alloced sections.
4274 In particular, any relocs in such sections should not affect GOT
4275 and PLT reference counting (ie. we don't allow them to create GOT
4276 or PLT entries), there's no possibility or desire to optimize TLS
4277 relocs, and there's not much point in propagating relocs to shared
4278 libs that the dynamic linker won't relocate. */
4279 if ((sec->flags & SEC_ALLOC) == 0)
4280 return TRUE;
4281
4282 BFD_ASSERT (is_ppc64_elf (abfd));
4283
4284 htab = ppc_hash_table (info);
4285 if (htab == NULL)
4286 return FALSE;
4287
4288 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4289 FALSE, FALSE, TRUE);
4290 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4291 FALSE, FALSE, TRUE);
4292 symtab_hdr = &elf_symtab_hdr (abfd);
4293 sym_hashes = elf_sym_hashes (abfd);
4294 sreloc = NULL;
4295 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4296 rel_end = relocs + sec->reloc_count;
4297 for (rel = relocs; rel < rel_end; rel++)
4298 {
4299 unsigned long r_symndx;
4300 struct elf_link_hash_entry *h;
4301 enum elf_ppc64_reloc_type r_type;
4302 int tls_type;
4303 struct _ppc64_elf_section_data *ppc64_sec;
4304 struct plt_entry **ifunc, **plt_list;
4305
4306 r_symndx = ELF64_R_SYM (rel->r_info);
4307 if (r_symndx < symtab_hdr->sh_info)
4308 h = NULL;
4309 else
4310 {
4311 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4312 h = elf_follow_link (h);
4313
4314 if (h == htab->elf.hgot)
4315 sec->has_toc_reloc = 1;
4316 }
4317
4318 tls_type = 0;
4319 ifunc = NULL;
4320 if (h != NULL)
4321 {
4322 if (h->type == STT_GNU_IFUNC)
4323 {
4324 h->needs_plt = 1;
4325 ifunc = &h->plt.plist;
4326 }
4327 }
4328 else
4329 {
4330 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4331 abfd, r_symndx);
4332 if (isym == NULL)
4333 return FALSE;
4334
4335 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4336 {
4337 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4338 rel->r_addend,
4339 NON_GOT | PLT_IFUNC);
4340 if (ifunc == NULL)
4341 return FALSE;
4342 }
4343 }
4344
4345 r_type = ELF64_R_TYPE (rel->r_info);
4346 switch (r_type)
4347 {
4348 case R_PPC64_TLSGD:
4349 case R_PPC64_TLSLD:
4350 /* These special tls relocs tie a call to __tls_get_addr with
4351 its parameter symbol. */
4352 if (h != NULL)
4353 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4354 else
4355 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4356 rel->r_addend,
4357 NON_GOT | TLS_TLS | TLS_MARK))
4358 return FALSE;
4359 sec->has_tls_reloc = 1;
4360 break;
4361
4362 case R_PPC64_GOT_TLSLD16:
4363 case R_PPC64_GOT_TLSLD16_LO:
4364 case R_PPC64_GOT_TLSLD16_HI:
4365 case R_PPC64_GOT_TLSLD16_HA:
4366 tls_type = TLS_TLS | TLS_LD;
4367 goto dogottls;
4368
4369 case R_PPC64_GOT_TLSGD16:
4370 case R_PPC64_GOT_TLSGD16_LO:
4371 case R_PPC64_GOT_TLSGD16_HI:
4372 case R_PPC64_GOT_TLSGD16_HA:
4373 tls_type = TLS_TLS | TLS_GD;
4374 goto dogottls;
4375
4376 case R_PPC64_GOT_TPREL16_DS:
4377 case R_PPC64_GOT_TPREL16_LO_DS:
4378 case R_PPC64_GOT_TPREL16_HI:
4379 case R_PPC64_GOT_TPREL16_HA:
4380 if (bfd_link_dll (info))
4381 info->flags |= DF_STATIC_TLS;
4382 tls_type = TLS_TLS | TLS_TPREL;
4383 goto dogottls;
4384
4385 case R_PPC64_GOT_DTPREL16_DS:
4386 case R_PPC64_GOT_DTPREL16_LO_DS:
4387 case R_PPC64_GOT_DTPREL16_HI:
4388 case R_PPC64_GOT_DTPREL16_HA:
4389 tls_type = TLS_TLS | TLS_DTPREL;
4390 dogottls:
4391 sec->has_tls_reloc = 1;
4392 goto dogot;
4393
4394 case R_PPC64_GOT16_DS:
4395 case R_PPC64_GOT16_HA:
4396 case R_PPC64_GOT16_LO_DS:
4397 ppc64_elf_tdata (abfd)->has_gotrel = 1;
4398 ppc64_elf_section_data (sec)->has_gotrel = 1;
4399 /* Fall through. */
4400
4401 case R_PPC64_GOT16:
4402 case R_PPC64_GOT16_HI:
4403 case R_PPC64_GOT16_LO:
4404 dogot:
4405 /* This symbol requires a global offset table entry. */
4406 sec->has_toc_reloc = 1;
4407 if (r_type == R_PPC64_GOT_TLSLD16
4408 || r_type == R_PPC64_GOT_TLSGD16
4409 || r_type == R_PPC64_GOT_TPREL16_DS
4410 || r_type == R_PPC64_GOT_DTPREL16_DS
4411 || r_type == R_PPC64_GOT16
4412 || r_type == R_PPC64_GOT16_DS)
4413 {
4414 htab->do_multi_toc = 1;
4415 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4416 }
4417
4418 if (ppc64_elf_tdata (abfd)->got == NULL
4419 && !create_got_section (abfd, info))
4420 return FALSE;
4421
4422 if (h != NULL)
4423 {
4424 struct ppc_link_hash_entry *eh;
4425 struct got_entry *ent;
4426
4427 eh = (struct ppc_link_hash_entry *) h;
4428 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4429 if (ent->addend == rel->r_addend
4430 && ent->owner == abfd
4431 && ent->tls_type == tls_type)
4432 break;
4433 if (ent == NULL)
4434 {
4435 bfd_size_type amt = sizeof (*ent);
4436 ent = bfd_alloc (abfd, amt);
4437 if (ent == NULL)
4438 return FALSE;
4439 ent->next = eh->elf.got.glist;
4440 ent->addend = rel->r_addend;
4441 ent->owner = abfd;
4442 ent->tls_type = tls_type;
4443 ent->is_indirect = FALSE;
4444 ent->got.refcount = 0;
4445 eh->elf.got.glist = ent;
4446 }
4447 ent->got.refcount += 1;
4448 eh->tls_mask |= tls_type;
4449 }
4450 else
4451 /* This is a global offset table entry for a local symbol. */
4452 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4453 rel->r_addend, tls_type))
4454 return FALSE;
4455
4456 /* We may also need a plt entry if the symbol turns out to be
4457 an ifunc. */
4458 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
4459 {
4460 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4461 return FALSE;
4462 }
4463 break;
4464
4465 case R_PPC64_PLT16_HA:
4466 case R_PPC64_PLT16_HI:
4467 case R_PPC64_PLT16_LO:
4468 case R_PPC64_PLT16_LO_DS:
4469 case R_PPC64_PLT32:
4470 case R_PPC64_PLT64:
4471 /* This symbol requires a procedure linkage table entry. */
4472 plt_list = ifunc;
4473 if (h != NULL)
4474 {
4475 h->needs_plt = 1;
4476 if (h->root.root.string[0] == '.'
4477 && h->root.root.string[1] != '\0')
4478 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4479 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
4480 plt_list = &h->plt.plist;
4481 }
4482 if (plt_list == NULL)
4483 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4484 rel->r_addend,
4485 NON_GOT | PLT_KEEP);
4486 if (!update_plt_info (abfd, plt_list, rel->r_addend))
4487 return FALSE;
4488 break;
4489
4490 /* The following relocations don't need to propagate the
4491 relocation if linking a shared object since they are
4492 section relative. */
4493 case R_PPC64_SECTOFF:
4494 case R_PPC64_SECTOFF_LO:
4495 case R_PPC64_SECTOFF_HI:
4496 case R_PPC64_SECTOFF_HA:
4497 case R_PPC64_SECTOFF_DS:
4498 case R_PPC64_SECTOFF_LO_DS:
4499 case R_PPC64_DTPREL16:
4500 case R_PPC64_DTPREL16_LO:
4501 case R_PPC64_DTPREL16_HI:
4502 case R_PPC64_DTPREL16_HA:
4503 case R_PPC64_DTPREL16_DS:
4504 case R_PPC64_DTPREL16_LO_DS:
4505 case R_PPC64_DTPREL16_HIGH:
4506 case R_PPC64_DTPREL16_HIGHA:
4507 case R_PPC64_DTPREL16_HIGHER:
4508 case R_PPC64_DTPREL16_HIGHERA:
4509 case R_PPC64_DTPREL16_HIGHEST:
4510 case R_PPC64_DTPREL16_HIGHESTA:
4511 break;
4512
4513 /* Nor do these. */
4514 case R_PPC64_REL16:
4515 case R_PPC64_REL16_LO:
4516 case R_PPC64_REL16_HI:
4517 case R_PPC64_REL16_HA:
4518 case R_PPC64_REL16_HIGH:
4519 case R_PPC64_REL16_HIGHA:
4520 case R_PPC64_REL16_HIGHER:
4521 case R_PPC64_REL16_HIGHERA:
4522 case R_PPC64_REL16_HIGHEST:
4523 case R_PPC64_REL16_HIGHESTA:
4524 case R_PPC64_REL16DX_HA:
4525 break;
4526
4527 /* Not supported as a dynamic relocation. */
4528 case R_PPC64_ADDR64_LOCAL:
4529 if (bfd_link_pic (info))
4530 {
4531 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4532 ppc_howto_init ();
4533 /* xgettext:c-format */
4534 info->callbacks->einfo (_("%H: %s reloc unsupported "
4535 "in shared libraries and PIEs\n"),
4536 abfd, sec, rel->r_offset,
4537 ppc64_elf_howto_table[r_type]->name);
4538 bfd_set_error (bfd_error_bad_value);
4539 return FALSE;
4540 }
4541 break;
4542
4543 case R_PPC64_TOC16:
4544 case R_PPC64_TOC16_DS:
4545 htab->do_multi_toc = 1;
4546 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4547 /* Fall through. */
4548 case R_PPC64_TOC16_LO:
4549 case R_PPC64_TOC16_HI:
4550 case R_PPC64_TOC16_HA:
4551 case R_PPC64_TOC16_LO_DS:
4552 sec->has_toc_reloc = 1;
4553 break;
4554
4555 /* Marker reloc. */
4556 case R_PPC64_ENTRY:
4557 break;
4558
4559 /* This relocation describes the C++ object vtable hierarchy.
4560 Reconstruct it for later use during GC. */
4561 case R_PPC64_GNU_VTINHERIT:
4562 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4563 return FALSE;
4564 break;
4565
4566 /* This relocation describes which C++ vtable entries are actually
4567 used. Record for later use during GC. */
4568 case R_PPC64_GNU_VTENTRY:
4569 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4570 return FALSE;
4571 break;
4572
4573 case R_PPC64_REL14:
4574 case R_PPC64_REL14_BRTAKEN:
4575 case R_PPC64_REL14_BRNTAKEN:
4576 {
4577 asection *dest = NULL;
4578
4579 /* Heuristic: If jumping outside our section, chances are
4580 we are going to need a stub. */
4581 if (h != NULL)
4582 {
4583 /* If the sym is weak it may be overridden later, so
4584 don't assume we know where a weak sym lives. */
4585 if (h->root.type == bfd_link_hash_defined)
4586 dest = h->root.u.def.section;
4587 }
4588 else
4589 {
4590 Elf_Internal_Sym *isym;
4591
4592 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4593 abfd, r_symndx);
4594 if (isym == NULL)
4595 return FALSE;
4596
4597 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4598 }
4599
4600 if (dest != sec)
4601 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4602 }
4603 goto rel24;
4604
4605 case R_PPC64_PLTCALL:
4606 ppc64_elf_section_data (sec)->has_pltcall = 1;
4607 /* Fall through. */
4608
4609 case R_PPC64_REL24:
4610 case R_PPC64_REL24_NOTOC:
4611 rel24:
4612 plt_list = ifunc;
4613 if (h != NULL)
4614 {
4615 h->needs_plt = 1;
4616 if (h->root.root.string[0] == '.'
4617 && h->root.root.string[1] != '\0')
4618 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4619
4620 if (h == tga || h == dottga)
4621 {
4622 sec->has_tls_reloc = 1;
4623 if (rel != relocs
4624 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4625 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4626 /* We have a new-style __tls_get_addr call with
4627 a marker reloc. */
4628 ;
4629 else
4630 /* Mark this section as having an old-style call. */
4631 sec->has_tls_get_addr_call = 1;
4632 }
4633 plt_list = &h->plt.plist;
4634 }
4635
4636 /* We may need a .plt entry if the function this reloc
4637 refers to is in a shared lib. */
4638 if (plt_list
4639 && !update_plt_info (abfd, plt_list, rel->r_addend))
4640 return FALSE;
4641 break;
4642
4643 case R_PPC64_ADDR14:
4644 case R_PPC64_ADDR14_BRNTAKEN:
4645 case R_PPC64_ADDR14_BRTAKEN:
4646 case R_PPC64_ADDR24:
4647 goto dodyn;
4648
4649 case R_PPC64_TPREL64:
4650 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4651 if (bfd_link_dll (info))
4652 info->flags |= DF_STATIC_TLS;
4653 goto dotlstoc;
4654
4655 case R_PPC64_DTPMOD64:
4656 if (rel + 1 < rel_end
4657 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4658 && rel[1].r_offset == rel->r_offset + 8)
4659 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4660 else
4661 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4662 goto dotlstoc;
4663
4664 case R_PPC64_DTPREL64:
4665 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4666 if (rel != relocs
4667 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4668 && rel[-1].r_offset == rel->r_offset - 8)
4669 /* This is the second reloc of a dtpmod, dtprel pair.
4670 Don't mark with TLS_DTPREL. */
4671 goto dodyn;
4672
4673 dotlstoc:
4674 sec->has_tls_reloc = 1;
4675 if (h != NULL)
4676 {
4677 struct ppc_link_hash_entry *eh;
4678 eh = (struct ppc_link_hash_entry *) h;
4679 eh->tls_mask |= tls_type;
4680 }
4681 else
4682 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4683 rel->r_addend, tls_type))
4684 return FALSE;
4685
4686 ppc64_sec = ppc64_elf_section_data (sec);
4687 if (ppc64_sec->sec_type != sec_toc)
4688 {
4689 bfd_size_type amt;
4690
4691 /* One extra to simplify get_tls_mask. */
4692 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4693 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4694 if (ppc64_sec->u.toc.symndx == NULL)
4695 return FALSE;
4696 amt = sec->size * sizeof (bfd_vma) / 8;
4697 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4698 if (ppc64_sec->u.toc.add == NULL)
4699 return FALSE;
4700 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4701 ppc64_sec->sec_type = sec_toc;
4702 }
4703 BFD_ASSERT (rel->r_offset % 8 == 0);
4704 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
4705 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
4706
4707 /* Mark the second slot of a GD or LD entry.
4708 -1 to indicate GD and -2 to indicate LD. */
4709 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4710 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
4711 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4712 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
4713 goto dodyn;
4714
4715 case R_PPC64_TPREL16:
4716 case R_PPC64_TPREL16_LO:
4717 case R_PPC64_TPREL16_HI:
4718 case R_PPC64_TPREL16_HA:
4719 case R_PPC64_TPREL16_DS:
4720 case R_PPC64_TPREL16_LO_DS:
4721 case R_PPC64_TPREL16_HIGH:
4722 case R_PPC64_TPREL16_HIGHA:
4723 case R_PPC64_TPREL16_HIGHER:
4724 case R_PPC64_TPREL16_HIGHERA:
4725 case R_PPC64_TPREL16_HIGHEST:
4726 case R_PPC64_TPREL16_HIGHESTA:
4727 if (bfd_link_dll (info))
4728 info->flags |= DF_STATIC_TLS;
4729 goto dodyn;
4730
4731 case R_PPC64_ADDR64:
4732 if (is_opd
4733 && rel + 1 < rel_end
4734 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
4735 {
4736 if (h != NULL)
4737 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4738 }
4739 /* Fall through. */
4740
4741 case R_PPC64_ADDR16:
4742 case R_PPC64_ADDR16_DS:
4743 case R_PPC64_ADDR16_HA:
4744 case R_PPC64_ADDR16_HI:
4745 case R_PPC64_ADDR16_HIGH:
4746 case R_PPC64_ADDR16_HIGHA:
4747 case R_PPC64_ADDR16_HIGHER:
4748 case R_PPC64_ADDR16_HIGHERA:
4749 case R_PPC64_ADDR16_HIGHEST:
4750 case R_PPC64_ADDR16_HIGHESTA:
4751 case R_PPC64_ADDR16_LO:
4752 case R_PPC64_ADDR16_LO_DS:
4753 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
4754 && rel->r_addend == 0)
4755 {
4756 /* We may need a .plt entry if this reloc refers to a
4757 function in a shared lib. */
4758 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4759 return FALSE;
4760 h->pointer_equality_needed = 1;
4761 }
4762 /* Fall through. */
4763
4764 case R_PPC64_REL30:
4765 case R_PPC64_REL32:
4766 case R_PPC64_REL64:
4767 case R_PPC64_ADDR32:
4768 case R_PPC64_UADDR16:
4769 case R_PPC64_UADDR32:
4770 case R_PPC64_UADDR64:
4771 case R_PPC64_TOC:
4772 if (h != NULL && !bfd_link_pic (info))
4773 /* We may need a copy reloc. */
4774 h->non_got_ref = 1;
4775
4776 /* Don't propagate .opd relocs. */
4777 if (NO_OPD_RELOCS && is_opd)
4778 break;
4779
4780 /* If we are creating a shared library, and this is a reloc
4781 against a global symbol, or a non PC relative reloc
4782 against a local symbol, then we need to copy the reloc
4783 into the shared library. However, if we are linking with
4784 -Bsymbolic, we do not need to copy a reloc against a
4785 global symbol which is defined in an object we are
4786 including in the link (i.e., DEF_REGULAR is set). At
4787 this point we have not seen all the input files, so it is
4788 possible that DEF_REGULAR is not set now but will be set
4789 later (it is never cleared). In case of a weak definition,
4790 DEF_REGULAR may be cleared later by a strong definition in
4791 a shared library. We account for that possibility below by
4792 storing information in the dyn_relocs field of the hash
4793 table entry. A similar situation occurs when creating
4794 shared libraries and symbol visibility changes render the
4795 symbol local.
4796
4797 If on the other hand, we are creating an executable, we
4798 may need to keep relocations for symbols satisfied by a
4799 dynamic library if we manage to avoid copy relocs for the
4800 symbol. */
4801 dodyn:
4802 if ((bfd_link_pic (info)
4803 && (must_be_dyn_reloc (info, r_type)
4804 || (h != NULL
4805 && (!SYMBOLIC_BIND (info, h)
4806 || h->root.type == bfd_link_hash_defweak
4807 || !h->def_regular))))
4808 || (ELIMINATE_COPY_RELOCS
4809 && !bfd_link_pic (info)
4810 && h != NULL
4811 && (h->root.type == bfd_link_hash_defweak
4812 || !h->def_regular))
4813 || (!bfd_link_pic (info)
4814 && ifunc != NULL))
4815 {
4816 /* We must copy these reloc types into the output file.
4817 Create a reloc section in dynobj and make room for
4818 this reloc. */
4819 if (sreloc == NULL)
4820 {
4821 sreloc = _bfd_elf_make_dynamic_reloc_section
4822 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
4823
4824 if (sreloc == NULL)
4825 return FALSE;
4826 }
4827
4828 /* If this is a global symbol, we count the number of
4829 relocations we need for this symbol. */
4830 if (h != NULL)
4831 {
4832 struct elf_dyn_relocs *p;
4833 struct elf_dyn_relocs **head;
4834
4835 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
4836 p = *head;
4837 if (p == NULL || p->sec != sec)
4838 {
4839 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4840 if (p == NULL)
4841 return FALSE;
4842 p->next = *head;
4843 *head = p;
4844 p->sec = sec;
4845 p->count = 0;
4846 p->pc_count = 0;
4847 }
4848 p->count += 1;
4849 if (!must_be_dyn_reloc (info, r_type))
4850 p->pc_count += 1;
4851 }
4852 else
4853 {
4854 /* Track dynamic relocs needed for local syms too.
4855 We really need local syms available to do this
4856 easily. Oh well. */
4857 struct ppc_dyn_relocs *p;
4858 struct ppc_dyn_relocs **head;
4859 bfd_boolean is_ifunc;
4860 asection *s;
4861 void *vpp;
4862 Elf_Internal_Sym *isym;
4863
4864 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4865 abfd, r_symndx);
4866 if (isym == NULL)
4867 return FALSE;
4868
4869 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4870 if (s == NULL)
4871 s = sec;
4872
4873 vpp = &elf_section_data (s)->local_dynrel;
4874 head = (struct ppc_dyn_relocs **) vpp;
4875 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
4876 p = *head;
4877 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
4878 p = p->next;
4879 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
4880 {
4881 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4882 if (p == NULL)
4883 return FALSE;
4884 p->next = *head;
4885 *head = p;
4886 p->sec = sec;
4887 p->ifunc = is_ifunc;
4888 p->count = 0;
4889 }
4890 p->count += 1;
4891 }
4892 }
4893 break;
4894
4895 default:
4896 break;
4897 }
4898 }
4899
4900 return TRUE;
4901 }
4902
4903 /* Merge backend specific data from an object file to the output
4904 object file when linking. */
4905
4906 static bfd_boolean
4907 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4908 {
4909 bfd *obfd = info->output_bfd;
4910 unsigned long iflags, oflags;
4911
4912 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
4913 return TRUE;
4914
4915 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
4916 return TRUE;
4917
4918 if (!_bfd_generic_verify_endian_match (ibfd, info))
4919 return FALSE;
4920
4921 iflags = elf_elfheader (ibfd)->e_flags;
4922 oflags = elf_elfheader (obfd)->e_flags;
4923
4924 if (iflags & ~EF_PPC64_ABI)
4925 {
4926 _bfd_error_handler
4927 /* xgettext:c-format */
4928 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
4929 bfd_set_error (bfd_error_bad_value);
4930 return FALSE;
4931 }
4932 else if (iflags != oflags && iflags != 0)
4933 {
4934 _bfd_error_handler
4935 /* xgettext:c-format */
4936 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
4937 ibfd, iflags, oflags);
4938 bfd_set_error (bfd_error_bad_value);
4939 return FALSE;
4940 }
4941
4942 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
4943 return FALSE;
4944
4945 /* Merge Tag_compatibility attributes and any common GNU ones. */
4946 return _bfd_elf_merge_object_attributes (ibfd, info);
4947 }
4948
4949 static bfd_boolean
4950 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
4951 {
4952 /* Print normal ELF private data. */
4953 _bfd_elf_print_private_bfd_data (abfd, ptr);
4954
4955 if (elf_elfheader (abfd)->e_flags != 0)
4956 {
4957 FILE *file = ptr;
4958
4959 fprintf (file, _("private flags = 0x%lx:"),
4960 elf_elfheader (abfd)->e_flags);
4961
4962 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
4963 fprintf (file, _(" [abiv%ld]"),
4964 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
4965 fputc ('\n', file);
4966 }
4967
4968 return TRUE;
4969 }
4970
4971 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
4972 of the code entry point, and its section, which must be in the same
4973 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
4974
4975 static bfd_vma
4976 opd_entry_value (asection *opd_sec,
4977 bfd_vma offset,
4978 asection **code_sec,
4979 bfd_vma *code_off,
4980 bfd_boolean in_code_sec)
4981 {
4982 bfd *opd_bfd = opd_sec->owner;
4983 Elf_Internal_Rela *relocs;
4984 Elf_Internal_Rela *lo, *hi, *look;
4985 bfd_vma val;
4986
4987 /* No relocs implies we are linking a --just-symbols object, or looking
4988 at a final linked executable with addr2line or somesuch. */
4989 if (opd_sec->reloc_count == 0)
4990 {
4991 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
4992
4993 if (contents == NULL)
4994 {
4995 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
4996 return (bfd_vma) -1;
4997 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
4998 }
4999
5000 /* PR 17512: file: 64b9dfbb. */
5001 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5002 return (bfd_vma) -1;
5003
5004 val = bfd_get_64 (opd_bfd, contents + offset);
5005 if (code_sec != NULL)
5006 {
5007 asection *sec, *likely = NULL;
5008
5009 if (in_code_sec)
5010 {
5011 sec = *code_sec;
5012 if (sec->vma <= val
5013 && val < sec->vma + sec->size)
5014 likely = sec;
5015 else
5016 val = -1;
5017 }
5018 else
5019 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5020 if (sec->vma <= val
5021 && (sec->flags & SEC_LOAD) != 0
5022 && (sec->flags & SEC_ALLOC) != 0)
5023 likely = sec;
5024 if (likely != NULL)
5025 {
5026 *code_sec = likely;
5027 if (code_off != NULL)
5028 *code_off = val - likely->vma;
5029 }
5030 }
5031 return val;
5032 }
5033
5034 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5035
5036 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5037 if (relocs == NULL)
5038 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5039 /* PR 17512: file: df8e1fd6. */
5040 if (relocs == NULL)
5041 return (bfd_vma) -1;
5042
5043 /* Go find the opd reloc at the sym address. */
5044 lo = relocs;
5045 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5046 val = (bfd_vma) -1;
5047 while (lo < hi)
5048 {
5049 look = lo + (hi - lo) / 2;
5050 if (look->r_offset < offset)
5051 lo = look + 1;
5052 else if (look->r_offset > offset)
5053 hi = look;
5054 else
5055 {
5056 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5057
5058 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5059 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5060 {
5061 unsigned long symndx = ELF64_R_SYM (look->r_info);
5062 asection *sec = NULL;
5063
5064 if (symndx >= symtab_hdr->sh_info
5065 && elf_sym_hashes (opd_bfd) != NULL)
5066 {
5067 struct elf_link_hash_entry **sym_hashes;
5068 struct elf_link_hash_entry *rh;
5069
5070 sym_hashes = elf_sym_hashes (opd_bfd);
5071 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5072 if (rh != NULL)
5073 {
5074 rh = elf_follow_link (rh);
5075 if (rh->root.type != bfd_link_hash_defined
5076 && rh->root.type != bfd_link_hash_defweak)
5077 break;
5078 if (rh->root.u.def.section->owner == opd_bfd)
5079 {
5080 val = rh->root.u.def.value;
5081 sec = rh->root.u.def.section;
5082 }
5083 }
5084 }
5085
5086 if (sec == NULL)
5087 {
5088 Elf_Internal_Sym *sym;
5089
5090 if (symndx < symtab_hdr->sh_info)
5091 {
5092 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5093 if (sym == NULL)
5094 {
5095 size_t symcnt = symtab_hdr->sh_info;
5096 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5097 symcnt, 0,
5098 NULL, NULL, NULL);
5099 if (sym == NULL)
5100 break;
5101 symtab_hdr->contents = (bfd_byte *) sym;
5102 }
5103 sym += symndx;
5104 }
5105 else
5106 {
5107 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5108 1, symndx,
5109 NULL, NULL, NULL);
5110 if (sym == NULL)
5111 break;
5112 }
5113 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5114 if (sec == NULL)
5115 break;
5116 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5117 val = sym->st_value;
5118 }
5119
5120 val += look->r_addend;
5121 if (code_off != NULL)
5122 *code_off = val;
5123 if (code_sec != NULL)
5124 {
5125 if (in_code_sec && *code_sec != sec)
5126 return -1;
5127 else
5128 *code_sec = sec;
5129 }
5130 if (sec->output_section != NULL)
5131 val += sec->output_section->vma + sec->output_offset;
5132 }
5133 break;
5134 }
5135 }
5136
5137 return val;
5138 }
5139
5140 /* If the ELF symbol SYM might be a function in SEC, return the
5141 function size and set *CODE_OFF to the function's entry point,
5142 otherwise return zero. */
5143
5144 static bfd_size_type
5145 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5146 bfd_vma *code_off)
5147 {
5148 bfd_size_type size;
5149
5150 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5151 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5152 return 0;
5153
5154 size = 0;
5155 if (!(sym->flags & BSF_SYNTHETIC))
5156 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5157
5158 if (strcmp (sym->section->name, ".opd") == 0)
5159 {
5160 struct _opd_sec_data *opd = get_opd_info (sym->section);
5161 bfd_vma symval = sym->value;
5162
5163 if (opd != NULL
5164 && opd->adjust != NULL
5165 && elf_section_data (sym->section)->relocs != NULL)
5166 {
5167 /* opd_entry_value will use cached relocs that have been
5168 adjusted, but with raw symbols. That means both local
5169 and global symbols need adjusting. */
5170 long adjust = opd->adjust[OPD_NDX (symval)];
5171 if (adjust == -1)
5172 return 0;
5173 symval += adjust;
5174 }
5175
5176 if (opd_entry_value (sym->section, symval,
5177 &sec, code_off, TRUE) == (bfd_vma) -1)
5178 return 0;
5179 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5180 symbol. This size has nothing to do with the code size of the
5181 function, which is what we're supposed to return, but the
5182 code size isn't available without looking up the dot-sym.
5183 However, doing that would be a waste of time particularly
5184 since elf_find_function will look at the dot-sym anyway.
5185 Now, elf_find_function will keep the largest size of any
5186 function sym found at the code address of interest, so return
5187 1 here to avoid it incorrectly caching a larger function size
5188 for a small function. This does mean we return the wrong
5189 size for a new-ABI function of size 24, but all that does is
5190 disable caching for such functions. */
5191 if (size == 24)
5192 size = 1;
5193 }
5194 else
5195 {
5196 if (sym->section != sec)
5197 return 0;
5198 *code_off = sym->value;
5199 }
5200 if (size == 0)
5201 size = 1;
5202 return size;
5203 }
5204
5205 /* Return true if symbol is a strong function defined in an ELFv2
5206 object with st_other localentry bits of zero, ie. its local entry
5207 point coincides with its global entry point. */
5208
5209 static bfd_boolean
5210 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5211 {
5212 return (h != NULL
5213 && h->type == STT_FUNC
5214 && h->root.type == bfd_link_hash_defined
5215 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5216 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
5217 && is_ppc64_elf (h->root.u.def.section->owner)
5218 && abiversion (h->root.u.def.section->owner) >= 2);
5219 }
5220
5221 /* Return true if symbol is defined in a regular object file. */
5222
5223 static bfd_boolean
5224 is_static_defined (struct elf_link_hash_entry *h)
5225 {
5226 return ((h->root.type == bfd_link_hash_defined
5227 || h->root.type == bfd_link_hash_defweak)
5228 && h->root.u.def.section != NULL
5229 && h->root.u.def.section->output_section != NULL);
5230 }
5231
5232 /* If FDH is a function descriptor symbol, return the associated code
5233 entry symbol if it is defined. Return NULL otherwise. */
5234
5235 static struct ppc_link_hash_entry *
5236 defined_code_entry (struct ppc_link_hash_entry *fdh)
5237 {
5238 if (fdh->is_func_descriptor)
5239 {
5240 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5241 if (fh->elf.root.type == bfd_link_hash_defined
5242 || fh->elf.root.type == bfd_link_hash_defweak)
5243 return fh;
5244 }
5245 return NULL;
5246 }
5247
5248 /* If FH is a function code entry symbol, return the associated
5249 function descriptor symbol if it is defined. Return NULL otherwise. */
5250
5251 static struct ppc_link_hash_entry *
5252 defined_func_desc (struct ppc_link_hash_entry *fh)
5253 {
5254 if (fh->oh != NULL
5255 && fh->oh->is_func_descriptor)
5256 {
5257 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5258 if (fdh->elf.root.type == bfd_link_hash_defined
5259 || fdh->elf.root.type == bfd_link_hash_defweak)
5260 return fdh;
5261 }
5262 return NULL;
5263 }
5264
5265 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5266
5267 /* Garbage collect sections, after first dealing with dot-symbols. */
5268
5269 static bfd_boolean
5270 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5271 {
5272 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5273
5274 if (htab != NULL && htab->need_func_desc_adj)
5275 {
5276 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5277 htab->need_func_desc_adj = 0;
5278 }
5279 return bfd_elf_gc_sections (abfd, info);
5280 }
5281
5282 /* Mark all our entry sym sections, both opd and code section. */
5283
5284 static void
5285 ppc64_elf_gc_keep (struct bfd_link_info *info)
5286 {
5287 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5288 struct bfd_sym_chain *sym;
5289
5290 if (htab == NULL)
5291 return;
5292
5293 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5294 {
5295 struct ppc_link_hash_entry *eh, *fh;
5296 asection *sec;
5297
5298 eh = (struct ppc_link_hash_entry *)
5299 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5300 if (eh == NULL)
5301 continue;
5302 if (eh->elf.root.type != bfd_link_hash_defined
5303 && eh->elf.root.type != bfd_link_hash_defweak)
5304 continue;
5305
5306 fh = defined_code_entry (eh);
5307 if (fh != NULL)
5308 {
5309 sec = fh->elf.root.u.def.section;
5310 sec->flags |= SEC_KEEP;
5311 }
5312 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5313 && opd_entry_value (eh->elf.root.u.def.section,
5314 eh->elf.root.u.def.value,
5315 &sec, NULL, FALSE) != (bfd_vma) -1)
5316 sec->flags |= SEC_KEEP;
5317
5318 sec = eh->elf.root.u.def.section;
5319 sec->flags |= SEC_KEEP;
5320 }
5321 }
5322
5323 /* Mark sections containing dynamically referenced symbols. When
5324 building shared libraries, we must assume that any visible symbol is
5325 referenced. */
5326
5327 static bfd_boolean
5328 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5329 {
5330 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5331 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5332 struct ppc_link_hash_entry *fdh;
5333 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5334
5335 /* Dynamic linking info is on the func descriptor sym. */
5336 fdh = defined_func_desc (eh);
5337 if (fdh != NULL)
5338 eh = fdh;
5339
5340 if ((eh->elf.root.type == bfd_link_hash_defined
5341 || eh->elf.root.type == bfd_link_hash_defweak)
5342 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5343 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5344 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5345 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5346 && (!bfd_link_executable (info)
5347 || info->gc_keep_exported
5348 || info->export_dynamic
5349 || (eh->elf.dynamic
5350 && d != NULL
5351 && (*d->match) (&d->head, NULL,
5352 eh->elf.root.root.string)))
5353 && (eh->elf.versioned >= versioned
5354 || !bfd_hide_sym_by_version (info->version_info,
5355 eh->elf.root.root.string)))))
5356 {
5357 asection *code_sec;
5358 struct ppc_link_hash_entry *fh;
5359
5360 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5361
5362 /* Function descriptor syms cause the associated
5363 function code sym section to be marked. */
5364 fh = defined_code_entry (eh);
5365 if (fh != NULL)
5366 {
5367 code_sec = fh->elf.root.u.def.section;
5368 code_sec->flags |= SEC_KEEP;
5369 }
5370 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5371 && opd_entry_value (eh->elf.root.u.def.section,
5372 eh->elf.root.u.def.value,
5373 &code_sec, NULL, FALSE) != (bfd_vma) -1)
5374 code_sec->flags |= SEC_KEEP;
5375 }
5376
5377 return TRUE;
5378 }
5379
5380 /* Return the section that should be marked against GC for a given
5381 relocation. */
5382
5383 static asection *
5384 ppc64_elf_gc_mark_hook (asection *sec,
5385 struct bfd_link_info *info,
5386 Elf_Internal_Rela *rel,
5387 struct elf_link_hash_entry *h,
5388 Elf_Internal_Sym *sym)
5389 {
5390 asection *rsec;
5391
5392 /* Syms return NULL if we're marking .opd, so we avoid marking all
5393 function sections, as all functions are referenced in .opd. */
5394 rsec = NULL;
5395 if (get_opd_info (sec) != NULL)
5396 return rsec;
5397
5398 if (h != NULL)
5399 {
5400 enum elf_ppc64_reloc_type r_type;
5401 struct ppc_link_hash_entry *eh, *fh, *fdh;
5402
5403 r_type = ELF64_R_TYPE (rel->r_info);
5404 switch (r_type)
5405 {
5406 case R_PPC64_GNU_VTINHERIT:
5407 case R_PPC64_GNU_VTENTRY:
5408 break;
5409
5410 default:
5411 switch (h->root.type)
5412 {
5413 case bfd_link_hash_defined:
5414 case bfd_link_hash_defweak:
5415 eh = (struct ppc_link_hash_entry *) h;
5416 fdh = defined_func_desc (eh);
5417 if (fdh != NULL)
5418 {
5419 /* -mcall-aixdesc code references the dot-symbol on
5420 a call reloc. Mark the function descriptor too
5421 against garbage collection. */
5422 fdh->elf.mark = 1;
5423 if (fdh->elf.is_weakalias)
5424 weakdef (&fdh->elf)->mark = 1;
5425 eh = fdh;
5426 }
5427
5428 /* Function descriptor syms cause the associated
5429 function code sym section to be marked. */
5430 fh = defined_code_entry (eh);
5431 if (fh != NULL)
5432 {
5433 /* They also mark their opd section. */
5434 eh->elf.root.u.def.section->gc_mark = 1;
5435
5436 rsec = fh->elf.root.u.def.section;
5437 }
5438 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5439 && opd_entry_value (eh->elf.root.u.def.section,
5440 eh->elf.root.u.def.value,
5441 &rsec, NULL, FALSE) != (bfd_vma) -1)
5442 eh->elf.root.u.def.section->gc_mark = 1;
5443 else
5444 rsec = h->root.u.def.section;
5445 break;
5446
5447 case bfd_link_hash_common:
5448 rsec = h->root.u.c.p->section;
5449 break;
5450
5451 default:
5452 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5453 }
5454 }
5455 }
5456 else
5457 {
5458 struct _opd_sec_data *opd;
5459
5460 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5461 opd = get_opd_info (rsec);
5462 if (opd != NULL && opd->func_sec != NULL)
5463 {
5464 rsec->gc_mark = 1;
5465
5466 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
5467 }
5468 }
5469
5470 return rsec;
5471 }
5472
5473 /* The maximum size of .sfpr. */
5474 #define SFPR_MAX (218*4)
5475
5476 struct sfpr_def_parms
5477 {
5478 const char name[12];
5479 unsigned char lo, hi;
5480 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5481 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
5482 };
5483
5484 /* Auto-generate _save*, _rest* functions in .sfpr.
5485 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5486 instead. */
5487
5488 static bfd_boolean
5489 sfpr_define (struct bfd_link_info *info,
5490 const struct sfpr_def_parms *parm,
5491 asection *stub_sec)
5492 {
5493 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5494 unsigned int i;
5495 size_t len = strlen (parm->name);
5496 bfd_boolean writing = FALSE;
5497 char sym[16];
5498
5499 if (htab == NULL)
5500 return FALSE;
5501
5502 memcpy (sym, parm->name, len);
5503 sym[len + 2] = 0;
5504
5505 for (i = parm->lo; i <= parm->hi; i++)
5506 {
5507 struct ppc_link_hash_entry *h;
5508
5509 sym[len + 0] = i / 10 + '0';
5510 sym[len + 1] = i % 10 + '0';
5511 h = (struct ppc_link_hash_entry *)
5512 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
5513 if (stub_sec != NULL)
5514 {
5515 if (h != NULL
5516 && h->elf.root.type == bfd_link_hash_defined
5517 && h->elf.root.u.def.section == htab->sfpr)
5518 {
5519 struct elf_link_hash_entry *s;
5520 char buf[32];
5521 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5522 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5523 if (s == NULL)
5524 return FALSE;
5525 if (s->root.type == bfd_link_hash_new
5526 || (s->root.type = bfd_link_hash_defined
5527 && s->root.u.def.section == stub_sec))
5528 {
5529 s->root.type = bfd_link_hash_defined;
5530 s->root.u.def.section = stub_sec;
5531 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
5532 + h->elf.root.u.def.value);
5533 s->ref_regular = 1;
5534 s->def_regular = 1;
5535 s->ref_regular_nonweak = 1;
5536 s->forced_local = 1;
5537 s->non_elf = 0;
5538 s->root.linker_def = 1;
5539 }
5540 }
5541 continue;
5542 }
5543 if (h != NULL)
5544 {
5545 h->save_res = 1;
5546 if (!h->elf.def_regular)
5547 {
5548 h->elf.root.type = bfd_link_hash_defined;
5549 h->elf.root.u.def.section = htab->sfpr;
5550 h->elf.root.u.def.value = htab->sfpr->size;
5551 h->elf.type = STT_FUNC;
5552 h->elf.def_regular = 1;
5553 h->elf.non_elf = 0;
5554 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5555 writing = TRUE;
5556 if (htab->sfpr->contents == NULL)
5557 {
5558 htab->sfpr->contents
5559 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5560 if (htab->sfpr->contents == NULL)
5561 return FALSE;
5562 }
5563 }
5564 }
5565 if (writing)
5566 {
5567 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5568 if (i != parm->hi)
5569 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5570 else
5571 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5572 htab->sfpr->size = p - htab->sfpr->contents;
5573 }
5574 }
5575
5576 return TRUE;
5577 }
5578
5579 static bfd_byte *
5580 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5581 {
5582 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5583 return p + 4;
5584 }
5585
5586 static bfd_byte *
5587 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5588 {
5589 p = savegpr0 (abfd, p, r);
5590 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5591 p = p + 4;
5592 bfd_put_32 (abfd, BLR, p);
5593 return p + 4;
5594 }
5595
5596 static bfd_byte *
5597 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5598 {
5599 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5600 return p + 4;
5601 }
5602
5603 static bfd_byte *
5604 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5605 {
5606 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5607 p = p + 4;
5608 p = restgpr0 (abfd, p, r);
5609 bfd_put_32 (abfd, MTLR_R0, p);
5610 p = p + 4;
5611 if (r == 29)
5612 {
5613 p = restgpr0 (abfd, p, 30);
5614 p = restgpr0 (abfd, p, 31);
5615 }
5616 bfd_put_32 (abfd, BLR, p);
5617 return p + 4;
5618 }
5619
5620 static bfd_byte *
5621 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5622 {
5623 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5624 return p + 4;
5625 }
5626
5627 static bfd_byte *
5628 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5629 {
5630 p = savegpr1 (abfd, p, r);
5631 bfd_put_32 (abfd, BLR, p);
5632 return p + 4;
5633 }
5634
5635 static bfd_byte *
5636 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5637 {
5638 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5639 return p + 4;
5640 }
5641
5642 static bfd_byte *
5643 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5644 {
5645 p = restgpr1 (abfd, p, r);
5646 bfd_put_32 (abfd, BLR, p);
5647 return p + 4;
5648 }
5649
5650 static bfd_byte *
5651 savefpr (bfd *abfd, bfd_byte *p, int r)
5652 {
5653 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5654 return p + 4;
5655 }
5656
5657 static bfd_byte *
5658 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5659 {
5660 p = savefpr (abfd, p, r);
5661 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5662 p = p + 4;
5663 bfd_put_32 (abfd, BLR, p);
5664 return p + 4;
5665 }
5666
5667 static bfd_byte *
5668 restfpr (bfd *abfd, bfd_byte *p, int r)
5669 {
5670 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5671 return p + 4;
5672 }
5673
5674 static bfd_byte *
5675 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5676 {
5677 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5678 p = p + 4;
5679 p = restfpr (abfd, p, r);
5680 bfd_put_32 (abfd, MTLR_R0, p);
5681 p = p + 4;
5682 if (r == 29)
5683 {
5684 p = restfpr (abfd, p, 30);
5685 p = restfpr (abfd, p, 31);
5686 }
5687 bfd_put_32 (abfd, BLR, p);
5688 return p + 4;
5689 }
5690
5691 static bfd_byte *
5692 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5693 {
5694 p = savefpr (abfd, p, r);
5695 bfd_put_32 (abfd, BLR, p);
5696 return p + 4;
5697 }
5698
5699 static bfd_byte *
5700 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5701 {
5702 p = restfpr (abfd, p, r);
5703 bfd_put_32 (abfd, BLR, p);
5704 return p + 4;
5705 }
5706
5707 static bfd_byte *
5708 savevr (bfd *abfd, bfd_byte *p, int r)
5709 {
5710 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5711 p = p + 4;
5712 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5713 return p + 4;
5714 }
5715
5716 static bfd_byte *
5717 savevr_tail (bfd *abfd, bfd_byte *p, int r)
5718 {
5719 p = savevr (abfd, p, r);
5720 bfd_put_32 (abfd, BLR, p);
5721 return p + 4;
5722 }
5723
5724 static bfd_byte *
5725 restvr (bfd *abfd, bfd_byte *p, int r)
5726 {
5727 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5728 p = p + 4;
5729 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5730 return p + 4;
5731 }
5732
5733 static bfd_byte *
5734 restvr_tail (bfd *abfd, bfd_byte *p, int r)
5735 {
5736 p = restvr (abfd, p, r);
5737 bfd_put_32 (abfd, BLR, p);
5738 return p + 4;
5739 }
5740
5741 /* Called via elf_link_hash_traverse to transfer dynamic linking
5742 information on function code symbol entries to their corresponding
5743 function descriptor symbol entries. */
5744
5745 static bfd_boolean
5746 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5747 {
5748 struct bfd_link_info *info;
5749 struct ppc_link_hash_table *htab;
5750 struct ppc_link_hash_entry *fh;
5751 struct ppc_link_hash_entry *fdh;
5752 bfd_boolean force_local;
5753
5754 fh = (struct ppc_link_hash_entry *) h;
5755 if (fh->elf.root.type == bfd_link_hash_indirect)
5756 return TRUE;
5757
5758 if (!fh->is_func)
5759 return TRUE;
5760
5761 if (fh->elf.root.root.string[0] != '.'
5762 || fh->elf.root.root.string[1] == '\0')
5763 return TRUE;
5764
5765 info = inf;
5766 htab = ppc_hash_table (info);
5767 if (htab == NULL)
5768 return FALSE;
5769
5770 /* Find the corresponding function descriptor symbol. */
5771 fdh = lookup_fdh (fh, htab);
5772
5773 /* Resolve undefined references to dot-symbols as the value
5774 in the function descriptor, if we have one in a regular object.
5775 This is to satisfy cases like ".quad .foo". Calls to functions
5776 in dynamic objects are handled elsewhere. */
5777 if ((fh->elf.root.type == bfd_link_hash_undefined
5778 || fh->elf.root.type == bfd_link_hash_undefweak)
5779 && (fdh->elf.root.type == bfd_link_hash_defined
5780 || fdh->elf.root.type == bfd_link_hash_defweak)
5781 && get_opd_info (fdh->elf.root.u.def.section) != NULL
5782 && opd_entry_value (fdh->elf.root.u.def.section,
5783 fdh->elf.root.u.def.value,
5784 &fh->elf.root.u.def.section,
5785 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
5786 {
5787 fh->elf.root.type = fdh->elf.root.type;
5788 fh->elf.forced_local = 1;
5789 fh->elf.def_regular = fdh->elf.def_regular;
5790 fh->elf.def_dynamic = fdh->elf.def_dynamic;
5791 }
5792
5793 if (!fh->elf.dynamic)
5794 {
5795 struct plt_entry *ent;
5796
5797 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5798 if (ent->plt.refcount > 0)
5799 break;
5800 if (ent == NULL)
5801 return TRUE;
5802 }
5803
5804 /* Create a descriptor as undefined if necessary. */
5805 if (fdh == NULL
5806 && !bfd_link_executable (info)
5807 && (fh->elf.root.type == bfd_link_hash_undefined
5808 || fh->elf.root.type == bfd_link_hash_undefweak))
5809 {
5810 fdh = make_fdh (info, fh);
5811 if (fdh == NULL)
5812 return FALSE;
5813 }
5814
5815 /* We can't support overriding of symbols on a fake descriptor. */
5816 if (fdh != NULL
5817 && fdh->fake
5818 && (fh->elf.root.type == bfd_link_hash_defined
5819 || fh->elf.root.type == bfd_link_hash_defweak))
5820 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
5821
5822 /* Transfer dynamic linking information to the function descriptor. */
5823 if (fdh != NULL)
5824 {
5825 fdh->elf.ref_regular |= fh->elf.ref_regular;
5826 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5827 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5828 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
5829 fdh->elf.dynamic |= fh->elf.dynamic;
5830 fdh->elf.needs_plt |= (fh->elf.needs_plt
5831 || fh->elf.type == STT_FUNC
5832 || fh->elf.type == STT_GNU_IFUNC);
5833 move_plt_plist (fh, fdh);
5834
5835 if (!fdh->elf.forced_local
5836 && fh->elf.dynindx != -1)
5837 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5838 return FALSE;
5839 }
5840
5841 /* Now that the info is on the function descriptor, clear the
5842 function code sym info. Any function code syms for which we
5843 don't have a definition in a regular file, we force local.
5844 This prevents a shared library from exporting syms that have
5845 been imported from another library. Function code syms that
5846 are really in the library we must leave global to prevent the
5847 linker dragging in a definition from a static library. */
5848 force_local = (!fh->elf.def_regular
5849 || fdh == NULL
5850 || !fdh->elf.def_regular
5851 || fdh->elf.forced_local);
5852 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5853
5854 return TRUE;
5855 }
5856
5857 static const struct sfpr_def_parms save_res_funcs[] =
5858 {
5859 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5860 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5861 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5862 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5863 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5864 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5865 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5866 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5867 { "._savef", 14, 31, savefpr, savefpr1_tail },
5868 { "._restf", 14, 31, restfpr, restfpr1_tail },
5869 { "_savevr_", 20, 31, savevr, savevr_tail },
5870 { "_restvr_", 20, 31, restvr, restvr_tail }
5871 };
5872
5873 /* Called near the start of bfd_elf_size_dynamic_sections. We use
5874 this hook to a) provide some gcc support functions, and b) transfer
5875 dynamic linking information gathered so far on function code symbol
5876 entries, to their corresponding function descriptor symbol entries. */
5877
5878 static bfd_boolean
5879 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5880 struct bfd_link_info *info)
5881 {
5882 struct ppc_link_hash_table *htab;
5883
5884 htab = ppc_hash_table (info);
5885 if (htab == NULL)
5886 return FALSE;
5887
5888 /* Provide any missing _save* and _rest* functions. */
5889 if (htab->sfpr != NULL)
5890 {
5891 unsigned int i;
5892
5893 htab->sfpr->size = 0;
5894 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
5895 if (!sfpr_define (info, &save_res_funcs[i], NULL))
5896 return FALSE;
5897 if (htab->sfpr->size == 0)
5898 htab->sfpr->flags |= SEC_EXCLUDE;
5899 }
5900
5901 if (bfd_link_relocatable (info))
5902 return TRUE;
5903
5904 if (htab->elf.hgot != NULL)
5905 {
5906 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
5907 /* Make .TOC. defined so as to prevent it being made dynamic.
5908 The wrong value here is fixed later in ppc64_elf_set_toc. */
5909 if (!htab->elf.hgot->def_regular
5910 || htab->elf.hgot->root.type != bfd_link_hash_defined)
5911 {
5912 htab->elf.hgot->root.type = bfd_link_hash_defined;
5913 htab->elf.hgot->root.u.def.value = 0;
5914 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
5915 htab->elf.hgot->def_regular = 1;
5916 htab->elf.hgot->root.linker_def = 1;
5917 }
5918 htab->elf.hgot->type = STT_OBJECT;
5919 htab->elf.hgot->other
5920 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5921 }
5922
5923 if (htab->need_func_desc_adj)
5924 {
5925 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5926 htab->need_func_desc_adj = 0;
5927 }
5928
5929 return TRUE;
5930 }
5931
5932 /* Find dynamic relocs for H that apply to read-only sections. */
5933
5934 static asection *
5935 readonly_dynrelocs (struct elf_link_hash_entry *h)
5936 {
5937 struct ppc_link_hash_entry *eh;
5938 struct elf_dyn_relocs *p;
5939
5940 eh = (struct ppc_link_hash_entry *) h;
5941 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5942 {
5943 asection *s = p->sec->output_section;
5944
5945 if (s != NULL && (s->flags & SEC_READONLY) != 0)
5946 return p->sec;
5947 }
5948 return NULL;
5949 }
5950
5951 /* Return true if we have dynamic relocs against H or any of its weak
5952 aliases, that apply to read-only sections. Cannot be used after
5953 size_dynamic_sections. */
5954
5955 static bfd_boolean
5956 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
5957 {
5958 struct ppc_link_hash_entry *eh;
5959
5960 eh = (struct ppc_link_hash_entry *) h;
5961 do
5962 {
5963 if (readonly_dynrelocs (&eh->elf))
5964 return TRUE;
5965 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
5966 }
5967 while (eh != NULL && &eh->elf != h);
5968
5969 return FALSE;
5970 }
5971
5972 /* Return whether EH has pc-relative dynamic relocs. */
5973
5974 static bfd_boolean
5975 pc_dynrelocs (struct ppc_link_hash_entry *eh)
5976 {
5977 struct elf_dyn_relocs *p;
5978
5979 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5980 if (p->pc_count != 0)
5981 return TRUE;
5982 return FALSE;
5983 }
5984
5985 /* Return true if a global entry stub will be created for H. Valid
5986 for ELFv2 before plt entries have been allocated. */
5987
5988 static bfd_boolean
5989 global_entry_stub (struct elf_link_hash_entry *h)
5990 {
5991 struct plt_entry *pent;
5992
5993 if (!h->pointer_equality_needed
5994 || h->def_regular)
5995 return FALSE;
5996
5997 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
5998 if (pent->plt.refcount > 0
5999 && pent->addend == 0)
6000 return TRUE;
6001
6002 return FALSE;
6003 }
6004
6005 /* Adjust a symbol defined by a dynamic object and referenced by a
6006 regular object. The current definition is in some section of the
6007 dynamic object, but we're not including those sections. We have to
6008 change the definition to something the rest of the link can
6009 understand. */
6010
6011 static bfd_boolean
6012 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6013 struct elf_link_hash_entry *h)
6014 {
6015 struct ppc_link_hash_table *htab;
6016 asection *s, *srel;
6017
6018 htab = ppc_hash_table (info);
6019 if (htab == NULL)
6020 return FALSE;
6021
6022 /* Deal with function syms. */
6023 if (h->type == STT_FUNC
6024 || h->type == STT_GNU_IFUNC
6025 || h->needs_plt)
6026 {
6027 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6028 || SYMBOL_CALLS_LOCAL (info, h)
6029 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6030 /* Discard dyn_relocs when non-pic if we've decided that a
6031 function symbol is local and not an ifunc. We keep dynamic
6032 relocs for ifuncs when local rather than always emitting a
6033 plt call stub for them and defining the symbol on the call
6034 stub. We can't do that for ELFv1 anyway (a function symbol
6035 is defined on a descriptor, not code) and it can be faster at
6036 run-time due to not needing to bounce through a stub. The
6037 dyn_relocs for ifuncs will be applied even in a static
6038 executable. */
6039 if (!bfd_link_pic (info)
6040 && h->type != STT_GNU_IFUNC
6041 && local)
6042 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6043
6044 /* Clear procedure linkage table information for any symbol that
6045 won't need a .plt entry. */
6046 struct plt_entry *ent;
6047 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6048 if (ent->plt.refcount > 0)
6049 break;
6050 if (ent == NULL
6051 || (h->type != STT_GNU_IFUNC
6052 && local
6053 && (htab->can_convert_all_inline_plt
6054 || (((struct ppc_link_hash_entry *) h)->tls_mask
6055 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6056 {
6057 h->plt.plist = NULL;
6058 h->needs_plt = 0;
6059 h->pointer_equality_needed = 0;
6060 }
6061 else if (abiversion (info->output_bfd) >= 2)
6062 {
6063 /* Taking a function's address in a read/write section
6064 doesn't require us to define the function symbol in the
6065 executable on a global entry stub. A dynamic reloc can
6066 be used instead. The reason we prefer a few more dynamic
6067 relocs is that calling via a global entry stub costs a
6068 few more instructions, and pointer_equality_needed causes
6069 extra work in ld.so when resolving these symbols. */
6070 if (global_entry_stub (h))
6071 {
6072 if (!readonly_dynrelocs (h))
6073 {
6074 h->pointer_equality_needed = 0;
6075 /* If we haven't seen a branch reloc and the symbol
6076 isn't an ifunc then we don't need a plt entry. */
6077 if (!h->needs_plt)
6078 h->plt.plist = NULL;
6079 }
6080 else if (!bfd_link_pic (info))
6081 /* We are going to be defining the function symbol on the
6082 plt stub, so no dyn_relocs needed when non-pic. */
6083 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6084 }
6085
6086 /* ELFv2 function symbols can't have copy relocs. */
6087 return TRUE;
6088 }
6089 else if (!h->needs_plt
6090 && !readonly_dynrelocs (h))
6091 {
6092 /* If we haven't seen a branch reloc and the symbol isn't an
6093 ifunc then we don't need a plt entry. */
6094 h->plt.plist = NULL;
6095 h->pointer_equality_needed = 0;
6096 return TRUE;
6097 }
6098 }
6099 else
6100 h->plt.plist = NULL;
6101
6102 /* If this is a weak symbol, and there is a real definition, the
6103 processor independent code will have arranged for us to see the
6104 real definition first, and we can just use the same value. */
6105 if (h->is_weakalias)
6106 {
6107 struct elf_link_hash_entry *def = weakdef (h);
6108 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6109 h->root.u.def.section = def->root.u.def.section;
6110 h->root.u.def.value = def->root.u.def.value;
6111 if (def->root.u.def.section == htab->elf.sdynbss
6112 || def->root.u.def.section == htab->elf.sdynrelro)
6113 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6114 return TRUE;
6115 }
6116
6117 /* If we are creating a shared library, we must presume that the
6118 only references to the symbol are via the global offset table.
6119 For such cases we need not do anything here; the relocations will
6120 be handled correctly by relocate_section. */
6121 if (bfd_link_pic (info))
6122 return TRUE;
6123
6124 /* If there are no references to this symbol that do not use the
6125 GOT, we don't need to generate a copy reloc. */
6126 if (!h->non_got_ref)
6127 return TRUE;
6128
6129 /* Don't generate a copy reloc for symbols defined in the executable. */
6130 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6131
6132 /* If -z nocopyreloc was given, don't generate them either. */
6133 || info->nocopyreloc
6134
6135 /* If we don't find any dynamic relocs in read-only sections, then
6136 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6137 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
6138
6139 /* Protected variables do not work with .dynbss. The copy in
6140 .dynbss won't be used by the shared library with the protected
6141 definition for the variable. Text relocations are preferable
6142 to an incorrect program. */
6143 || h->protected_def)
6144 return TRUE;
6145
6146 if (h->plt.plist != NULL)
6147 {
6148 /* We should never get here, but unfortunately there are versions
6149 of gcc out there that improperly (for this ABI) put initialized
6150 function pointers, vtable refs and suchlike in read-only
6151 sections. Allow them to proceed, but warn that this might
6152 break at runtime. */
6153 info->callbacks->einfo
6154 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6155 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6156 h->root.root.string);
6157 }
6158
6159 /* This is a reference to a symbol defined by a dynamic object which
6160 is not a function. */
6161
6162 /* We must allocate the symbol in our .dynbss section, which will
6163 become part of the .bss section of the executable. There will be
6164 an entry for this symbol in the .dynsym section. The dynamic
6165 object will contain position independent code, so all references
6166 from the dynamic object to this symbol will go through the global
6167 offset table. The dynamic linker will use the .dynsym entry to
6168 determine the address it must put in the global offset table, so
6169 both the dynamic object and the regular object will refer to the
6170 same memory location for the variable. */
6171 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6172 {
6173 s = htab->elf.sdynrelro;
6174 srel = htab->elf.sreldynrelro;
6175 }
6176 else
6177 {
6178 s = htab->elf.sdynbss;
6179 srel = htab->elf.srelbss;
6180 }
6181 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6182 {
6183 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6184 linker to copy the initial value out of the dynamic object
6185 and into the runtime process image. */
6186 srel->size += sizeof (Elf64_External_Rela);
6187 h->needs_copy = 1;
6188 }
6189
6190 /* We no longer want dyn_relocs. */
6191 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6192 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6193 }
6194
6195 /* If given a function descriptor symbol, hide both the function code
6196 sym and the descriptor. */
6197 static void
6198 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6199 struct elf_link_hash_entry *h,
6200 bfd_boolean force_local)
6201 {
6202 struct ppc_link_hash_entry *eh;
6203 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6204
6205 if (ppc_hash_table (info) == NULL)
6206 return;
6207
6208 eh = (struct ppc_link_hash_entry *) h;
6209 if (eh->is_func_descriptor)
6210 {
6211 struct ppc_link_hash_entry *fh = eh->oh;
6212
6213 if (fh == NULL)
6214 {
6215 const char *p, *q;
6216 struct elf_link_hash_table *htab = elf_hash_table (info);
6217 char save;
6218
6219 /* We aren't supposed to use alloca in BFD because on
6220 systems which do not have alloca the version in libiberty
6221 calls xmalloc, which might cause the program to crash
6222 when it runs out of memory. This function doesn't have a
6223 return status, so there's no way to gracefully return an
6224 error. So cheat. We know that string[-1] can be safely
6225 accessed; It's either a string in an ELF string table,
6226 or allocated in an objalloc structure. */
6227
6228 p = eh->elf.root.root.string - 1;
6229 save = *p;
6230 *(char *) p = '.';
6231 fh = (struct ppc_link_hash_entry *)
6232 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6233 *(char *) p = save;
6234
6235 /* Unfortunately, if it so happens that the string we were
6236 looking for was allocated immediately before this string,
6237 then we overwrote the string terminator. That's the only
6238 reason the lookup should fail. */
6239 if (fh == NULL)
6240 {
6241 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6242 while (q >= eh->elf.root.root.string && *q == *p)
6243 --q, --p;
6244 if (q < eh->elf.root.root.string && *p == '.')
6245 fh = (struct ppc_link_hash_entry *)
6246 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6247 }
6248 if (fh != NULL)
6249 {
6250 eh->oh = fh;
6251 fh->oh = eh;
6252 }
6253 }
6254 if (fh != NULL)
6255 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6256 }
6257 }
6258
6259 static bfd_boolean
6260 get_sym_h (struct elf_link_hash_entry **hp,
6261 Elf_Internal_Sym **symp,
6262 asection **symsecp,
6263 unsigned char **tls_maskp,
6264 Elf_Internal_Sym **locsymsp,
6265 unsigned long r_symndx,
6266 bfd *ibfd)
6267 {
6268 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6269
6270 if (r_symndx >= symtab_hdr->sh_info)
6271 {
6272 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6273 struct elf_link_hash_entry *h;
6274
6275 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6276 h = elf_follow_link (h);
6277
6278 if (hp != NULL)
6279 *hp = h;
6280
6281 if (symp != NULL)
6282 *symp = NULL;
6283
6284 if (symsecp != NULL)
6285 {
6286 asection *symsec = NULL;
6287 if (h->root.type == bfd_link_hash_defined
6288 || h->root.type == bfd_link_hash_defweak)
6289 symsec = h->root.u.def.section;
6290 *symsecp = symsec;
6291 }
6292
6293 if (tls_maskp != NULL)
6294 {
6295 struct ppc_link_hash_entry *eh;
6296
6297 eh = (struct ppc_link_hash_entry *) h;
6298 *tls_maskp = &eh->tls_mask;
6299 }
6300 }
6301 else
6302 {
6303 Elf_Internal_Sym *sym;
6304 Elf_Internal_Sym *locsyms = *locsymsp;
6305
6306 if (locsyms == NULL)
6307 {
6308 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6309 if (locsyms == NULL)
6310 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6311 symtab_hdr->sh_info,
6312 0, NULL, NULL, NULL);
6313 if (locsyms == NULL)
6314 return FALSE;
6315 *locsymsp = locsyms;
6316 }
6317 sym = locsyms + r_symndx;
6318
6319 if (hp != NULL)
6320 *hp = NULL;
6321
6322 if (symp != NULL)
6323 *symp = sym;
6324
6325 if (symsecp != NULL)
6326 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6327
6328 if (tls_maskp != NULL)
6329 {
6330 struct got_entry **lgot_ents;
6331 unsigned char *tls_mask;
6332
6333 tls_mask = NULL;
6334 lgot_ents = elf_local_got_ents (ibfd);
6335 if (lgot_ents != NULL)
6336 {
6337 struct plt_entry **local_plt = (struct plt_entry **)
6338 (lgot_ents + symtab_hdr->sh_info);
6339 unsigned char *lgot_masks = (unsigned char *)
6340 (local_plt + symtab_hdr->sh_info);
6341 tls_mask = &lgot_masks[r_symndx];
6342 }
6343 *tls_maskp = tls_mask;
6344 }
6345 }
6346 return TRUE;
6347 }
6348
6349 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6350 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6351 type suitable for optimization, and 1 otherwise. */
6352
6353 static int
6354 get_tls_mask (unsigned char **tls_maskp,
6355 unsigned long *toc_symndx,
6356 bfd_vma *toc_addend,
6357 Elf_Internal_Sym **locsymsp,
6358 const Elf_Internal_Rela *rel,
6359 bfd *ibfd)
6360 {
6361 unsigned long r_symndx;
6362 int next_r;
6363 struct elf_link_hash_entry *h;
6364 Elf_Internal_Sym *sym;
6365 asection *sec;
6366 bfd_vma off;
6367
6368 r_symndx = ELF64_R_SYM (rel->r_info);
6369 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6370 return 0;
6371
6372 if ((*tls_maskp != NULL
6373 && (**tls_maskp & TLS_TLS) != 0
6374 && **tls_maskp != (TLS_TLS | TLS_MARK))
6375 || sec == NULL
6376 || ppc64_elf_section_data (sec) == NULL
6377 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6378 return 1;
6379
6380 /* Look inside a TOC section too. */
6381 if (h != NULL)
6382 {
6383 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6384 off = h->root.u.def.value;
6385 }
6386 else
6387 off = sym->st_value;
6388 off += rel->r_addend;
6389 BFD_ASSERT (off % 8 == 0);
6390 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6391 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6392 if (toc_symndx != NULL)
6393 *toc_symndx = r_symndx;
6394 if (toc_addend != NULL)
6395 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6396 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6397 return 0;
6398 if ((h == NULL || is_static_defined (h))
6399 && (next_r == -1 || next_r == -2))
6400 return 1 - next_r;
6401 return 1;
6402 }
6403
6404 /* Find (or create) an entry in the tocsave hash table. */
6405
6406 static struct tocsave_entry *
6407 tocsave_find (struct ppc_link_hash_table *htab,
6408 enum insert_option insert,
6409 Elf_Internal_Sym **local_syms,
6410 const Elf_Internal_Rela *irela,
6411 bfd *ibfd)
6412 {
6413 unsigned long r_indx;
6414 struct elf_link_hash_entry *h;
6415 Elf_Internal_Sym *sym;
6416 struct tocsave_entry ent, *p;
6417 hashval_t hash;
6418 struct tocsave_entry **slot;
6419
6420 r_indx = ELF64_R_SYM (irela->r_info);
6421 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6422 return NULL;
6423 if (ent.sec == NULL || ent.sec->output_section == NULL)
6424 {
6425 _bfd_error_handler
6426 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
6427 return NULL;
6428 }
6429
6430 if (h != NULL)
6431 ent.offset = h->root.u.def.value;
6432 else
6433 ent.offset = sym->st_value;
6434 ent.offset += irela->r_addend;
6435
6436 hash = tocsave_htab_hash (&ent);
6437 slot = ((struct tocsave_entry **)
6438 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6439 if (slot == NULL)
6440 return NULL;
6441
6442 if (*slot == NULL)
6443 {
6444 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6445 if (p == NULL)
6446 return NULL;
6447 *p = ent;
6448 *slot = p;
6449 }
6450 return *slot;
6451 }
6452
6453 /* Adjust all global syms defined in opd sections. In gcc generated
6454 code for the old ABI, these will already have been done. */
6455
6456 static bfd_boolean
6457 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6458 {
6459 struct ppc_link_hash_entry *eh;
6460 asection *sym_sec;
6461 struct _opd_sec_data *opd;
6462
6463 if (h->root.type == bfd_link_hash_indirect)
6464 return TRUE;
6465
6466 if (h->root.type != bfd_link_hash_defined
6467 && h->root.type != bfd_link_hash_defweak)
6468 return TRUE;
6469
6470 eh = (struct ppc_link_hash_entry *) h;
6471 if (eh->adjust_done)
6472 return TRUE;
6473
6474 sym_sec = eh->elf.root.u.def.section;
6475 opd = get_opd_info (sym_sec);
6476 if (opd != NULL && opd->adjust != NULL)
6477 {
6478 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
6479 if (adjust == -1)
6480 {
6481 /* This entry has been deleted. */
6482 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6483 if (dsec == NULL)
6484 {
6485 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6486 if (discarded_section (dsec))
6487 {
6488 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6489 break;
6490 }
6491 }
6492 eh->elf.root.u.def.value = 0;
6493 eh->elf.root.u.def.section = dsec;
6494 }
6495 else
6496 eh->elf.root.u.def.value += adjust;
6497 eh->adjust_done = 1;
6498 }
6499 return TRUE;
6500 }
6501
6502 /* Handles decrementing dynamic reloc counts for the reloc specified by
6503 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
6504 have already been determined. */
6505
6506 static bfd_boolean
6507 dec_dynrel_count (bfd_vma r_info,
6508 asection *sec,
6509 struct bfd_link_info *info,
6510 Elf_Internal_Sym **local_syms,
6511 struct elf_link_hash_entry *h,
6512 Elf_Internal_Sym *sym)
6513 {
6514 enum elf_ppc64_reloc_type r_type;
6515 asection *sym_sec = NULL;
6516
6517 /* Can this reloc be dynamic? This switch, and later tests here
6518 should be kept in sync with the code in check_relocs. */
6519 r_type = ELF64_R_TYPE (r_info);
6520 switch (r_type)
6521 {
6522 default:
6523 return TRUE;
6524
6525 case R_PPC64_TPREL16:
6526 case R_PPC64_TPREL16_LO:
6527 case R_PPC64_TPREL16_HI:
6528 case R_PPC64_TPREL16_HA:
6529 case R_PPC64_TPREL16_DS:
6530 case R_PPC64_TPREL16_LO_DS:
6531 case R_PPC64_TPREL16_HIGH:
6532 case R_PPC64_TPREL16_HIGHA:
6533 case R_PPC64_TPREL16_HIGHER:
6534 case R_PPC64_TPREL16_HIGHERA:
6535 case R_PPC64_TPREL16_HIGHEST:
6536 case R_PPC64_TPREL16_HIGHESTA:
6537 case R_PPC64_TPREL64:
6538 case R_PPC64_DTPMOD64:
6539 case R_PPC64_DTPREL64:
6540 case R_PPC64_ADDR64:
6541 case R_PPC64_REL30:
6542 case R_PPC64_REL32:
6543 case R_PPC64_REL64:
6544 case R_PPC64_ADDR14:
6545 case R_PPC64_ADDR14_BRNTAKEN:
6546 case R_PPC64_ADDR14_BRTAKEN:
6547 case R_PPC64_ADDR16:
6548 case R_PPC64_ADDR16_DS:
6549 case R_PPC64_ADDR16_HA:
6550 case R_PPC64_ADDR16_HI:
6551 case R_PPC64_ADDR16_HIGH:
6552 case R_PPC64_ADDR16_HIGHA:
6553 case R_PPC64_ADDR16_HIGHER:
6554 case R_PPC64_ADDR16_HIGHERA:
6555 case R_PPC64_ADDR16_HIGHEST:
6556 case R_PPC64_ADDR16_HIGHESTA:
6557 case R_PPC64_ADDR16_LO:
6558 case R_PPC64_ADDR16_LO_DS:
6559 case R_PPC64_ADDR24:
6560 case R_PPC64_ADDR32:
6561 case R_PPC64_UADDR16:
6562 case R_PPC64_UADDR32:
6563 case R_PPC64_UADDR64:
6564 case R_PPC64_TOC:
6565 break;
6566 }
6567
6568 if (local_syms != NULL)
6569 {
6570 unsigned long r_symndx;
6571 bfd *ibfd = sec->owner;
6572
6573 r_symndx = ELF64_R_SYM (r_info);
6574 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6575 return FALSE;
6576 }
6577
6578 if ((bfd_link_pic (info)
6579 && (must_be_dyn_reloc (info, r_type)
6580 || (h != NULL
6581 && (!SYMBOLIC_BIND (info, h)
6582 || h->root.type == bfd_link_hash_defweak
6583 || !h->def_regular))))
6584 || (ELIMINATE_COPY_RELOCS
6585 && !bfd_link_pic (info)
6586 && h != NULL
6587 && (h->root.type == bfd_link_hash_defweak
6588 || !h->def_regular)))
6589 ;
6590 else
6591 return TRUE;
6592
6593 if (h != NULL)
6594 {
6595 struct elf_dyn_relocs *p;
6596 struct elf_dyn_relocs **pp;
6597 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6598
6599 /* elf_gc_sweep may have already removed all dyn relocs associated
6600 with local syms for a given section. Also, symbol flags are
6601 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6602 report a dynreloc miscount. */
6603 if (*pp == NULL && info->gc_sections)
6604 return TRUE;
6605
6606 while ((p = *pp) != NULL)
6607 {
6608 if (p->sec == sec)
6609 {
6610 if (!must_be_dyn_reloc (info, r_type))
6611 p->pc_count -= 1;
6612 p->count -= 1;
6613 if (p->count == 0)
6614 *pp = p->next;
6615 return TRUE;
6616 }
6617 pp = &p->next;
6618 }
6619 }
6620 else
6621 {
6622 struct ppc_dyn_relocs *p;
6623 struct ppc_dyn_relocs **pp;
6624 void *vpp;
6625 bfd_boolean is_ifunc;
6626
6627 if (local_syms == NULL)
6628 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6629 if (sym_sec == NULL)
6630 sym_sec = sec;
6631
6632 vpp = &elf_section_data (sym_sec)->local_dynrel;
6633 pp = (struct ppc_dyn_relocs **) vpp;
6634
6635 if (*pp == NULL && info->gc_sections)
6636 return TRUE;
6637
6638 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6639 while ((p = *pp) != NULL)
6640 {
6641 if (p->sec == sec && p->ifunc == is_ifunc)
6642 {
6643 p->count -= 1;
6644 if (p->count == 0)
6645 *pp = p->next;
6646 return TRUE;
6647 }
6648 pp = &p->next;
6649 }
6650 }
6651
6652 /* xgettext:c-format */
6653 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6654 sec->owner, sec);
6655 bfd_set_error (bfd_error_bad_value);
6656 return FALSE;
6657 }
6658
6659 /* Remove unused Official Procedure Descriptor entries. Currently we
6660 only remove those associated with functions in discarded link-once
6661 sections, or weakly defined functions that have been overridden. It
6662 would be possible to remove many more entries for statically linked
6663 applications. */
6664
6665 bfd_boolean
6666 ppc64_elf_edit_opd (struct bfd_link_info *info)
6667 {
6668 bfd *ibfd;
6669 bfd_boolean some_edited = FALSE;
6670 asection *need_pad = NULL;
6671 struct ppc_link_hash_table *htab;
6672
6673 htab = ppc_hash_table (info);
6674 if (htab == NULL)
6675 return FALSE;
6676
6677 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6678 {
6679 asection *sec;
6680 Elf_Internal_Rela *relstart, *rel, *relend;
6681 Elf_Internal_Shdr *symtab_hdr;
6682 Elf_Internal_Sym *local_syms;
6683 struct _opd_sec_data *opd;
6684 bfd_boolean need_edit, add_aux_fields, broken;
6685 bfd_size_type cnt_16b = 0;
6686
6687 if (!is_ppc64_elf (ibfd))
6688 continue;
6689
6690 sec = bfd_get_section_by_name (ibfd, ".opd");
6691 if (sec == NULL || sec->size == 0)
6692 continue;
6693
6694 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6695 continue;
6696
6697 if (sec->output_section == bfd_abs_section_ptr)
6698 continue;
6699
6700 /* Look through the section relocs. */
6701 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6702 continue;
6703
6704 local_syms = NULL;
6705 symtab_hdr = &elf_symtab_hdr (ibfd);
6706
6707 /* Read the relocations. */
6708 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6709 info->keep_memory);
6710 if (relstart == NULL)
6711 return FALSE;
6712
6713 /* First run through the relocs to check they are sane, and to
6714 determine whether we need to edit this opd section. */
6715 need_edit = FALSE;
6716 broken = FALSE;
6717 need_pad = sec;
6718 relend = relstart + sec->reloc_count;
6719 for (rel = relstart; rel < relend; )
6720 {
6721 enum elf_ppc64_reloc_type r_type;
6722 unsigned long r_symndx;
6723 asection *sym_sec;
6724 struct elf_link_hash_entry *h;
6725 Elf_Internal_Sym *sym;
6726 bfd_vma offset;
6727
6728 /* .opd contains an array of 16 or 24 byte entries. We're
6729 only interested in the reloc pointing to a function entry
6730 point. */
6731 offset = rel->r_offset;
6732 if (rel + 1 == relend
6733 || rel[1].r_offset != offset + 8)
6734 {
6735 /* If someone messes with .opd alignment then after a
6736 "ld -r" we might have padding in the middle of .opd.
6737 Also, there's nothing to prevent someone putting
6738 something silly in .opd with the assembler. No .opd
6739 optimization for them! */
6740 broken_opd:
6741 _bfd_error_handler
6742 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
6743 broken = TRUE;
6744 break;
6745 }
6746
6747 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6748 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6749 {
6750 _bfd_error_handler
6751 /* xgettext:c-format */
6752 (_("%pB: unexpected reloc type %u in .opd section"),
6753 ibfd, r_type);
6754 broken = TRUE;
6755 break;
6756 }
6757
6758 r_symndx = ELF64_R_SYM (rel->r_info);
6759 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6760 r_symndx, ibfd))
6761 goto error_ret;
6762
6763 if (sym_sec == NULL || sym_sec->owner == NULL)
6764 {
6765 const char *sym_name;
6766 if (h != NULL)
6767 sym_name = h->root.root.string;
6768 else
6769 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6770 sym_sec);
6771
6772 _bfd_error_handler
6773 /* xgettext:c-format */
6774 (_("%pB: undefined sym `%s' in .opd section"),
6775 ibfd, sym_name);
6776 broken = TRUE;
6777 break;
6778 }
6779
6780 /* opd entries are always for functions defined in the
6781 current input bfd. If the symbol isn't defined in the
6782 input bfd, then we won't be using the function in this
6783 bfd; It must be defined in a linkonce section in another
6784 bfd, or is weak. It's also possible that we are
6785 discarding the function due to a linker script /DISCARD/,
6786 which we test for via the output_section. */
6787 if (sym_sec->owner != ibfd
6788 || sym_sec->output_section == bfd_abs_section_ptr)
6789 need_edit = TRUE;
6790
6791 rel += 2;
6792 if (rel + 1 == relend
6793 || (rel + 2 < relend
6794 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
6795 ++rel;
6796
6797 if (rel == relend)
6798 {
6799 if (sec->size == offset + 24)
6800 {
6801 need_pad = NULL;
6802 break;
6803 }
6804 if (sec->size == offset + 16)
6805 {
6806 cnt_16b++;
6807 break;
6808 }
6809 goto broken_opd;
6810 }
6811 else if (rel + 1 < relend
6812 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6813 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6814 {
6815 if (rel[0].r_offset == offset + 16)
6816 cnt_16b++;
6817 else if (rel[0].r_offset != offset + 24)
6818 goto broken_opd;
6819 }
6820 else
6821 goto broken_opd;
6822 }
6823
6824 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
6825
6826 if (!broken && (need_edit || add_aux_fields))
6827 {
6828 Elf_Internal_Rela *write_rel;
6829 Elf_Internal_Shdr *rel_hdr;
6830 bfd_byte *rptr, *wptr;
6831 bfd_byte *new_contents;
6832 bfd_size_type amt;
6833
6834 new_contents = NULL;
6835 amt = OPD_NDX (sec->size) * sizeof (long);
6836 opd = &ppc64_elf_section_data (sec)->u.opd;
6837 opd->adjust = bfd_zalloc (sec->owner, amt);
6838 if (opd->adjust == NULL)
6839 return FALSE;
6840
6841 /* This seems a waste of time as input .opd sections are all
6842 zeros as generated by gcc, but I suppose there's no reason
6843 this will always be so. We might start putting something in
6844 the third word of .opd entries. */
6845 if ((sec->flags & SEC_IN_MEMORY) == 0)
6846 {
6847 bfd_byte *loc;
6848 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6849 {
6850 if (loc != NULL)
6851 free (loc);
6852 error_ret:
6853 if (local_syms != NULL
6854 && symtab_hdr->contents != (unsigned char *) local_syms)
6855 free (local_syms);
6856 if (elf_section_data (sec)->relocs != relstart)
6857 free (relstart);
6858 return FALSE;
6859 }
6860 sec->contents = loc;
6861 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6862 }
6863
6864 elf_section_data (sec)->relocs = relstart;
6865
6866 new_contents = sec->contents;
6867 if (add_aux_fields)
6868 {
6869 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6870 if (new_contents == NULL)
6871 return FALSE;
6872 need_pad = NULL;
6873 }
6874 wptr = new_contents;
6875 rptr = sec->contents;
6876 write_rel = relstart;
6877 for (rel = relstart; rel < relend; )
6878 {
6879 unsigned long r_symndx;
6880 asection *sym_sec;
6881 struct elf_link_hash_entry *h;
6882 struct ppc_link_hash_entry *fdh = NULL;
6883 Elf_Internal_Sym *sym;
6884 long opd_ent_size;
6885 Elf_Internal_Rela *next_rel;
6886 bfd_boolean skip;
6887
6888 r_symndx = ELF64_R_SYM (rel->r_info);
6889 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6890 r_symndx, ibfd))
6891 goto error_ret;
6892
6893 next_rel = rel + 2;
6894 if (next_rel + 1 == relend
6895 || (next_rel + 2 < relend
6896 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
6897 ++next_rel;
6898
6899 /* See if the .opd entry is full 24 byte or
6900 16 byte (with fd_aux entry overlapped with next
6901 fd_func). */
6902 opd_ent_size = 24;
6903 if (next_rel == relend)
6904 {
6905 if (sec->size == rel->r_offset + 16)
6906 opd_ent_size = 16;
6907 }
6908 else if (next_rel->r_offset == rel->r_offset + 16)
6909 opd_ent_size = 16;
6910
6911 if (h != NULL
6912 && h->root.root.string[0] == '.')
6913 {
6914 fdh = ((struct ppc_link_hash_entry *) h)->oh;
6915 if (fdh != NULL)
6916 {
6917 fdh = ppc_follow_link (fdh);
6918 if (fdh->elf.root.type != bfd_link_hash_defined
6919 && fdh->elf.root.type != bfd_link_hash_defweak)
6920 fdh = NULL;
6921 }
6922 }
6923
6924 skip = (sym_sec->owner != ibfd
6925 || sym_sec->output_section == bfd_abs_section_ptr);
6926 if (skip)
6927 {
6928 if (fdh != NULL && sym_sec->owner == ibfd)
6929 {
6930 /* Arrange for the function descriptor sym
6931 to be dropped. */
6932 fdh->elf.root.u.def.value = 0;
6933 fdh->elf.root.u.def.section = sym_sec;
6934 }
6935 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
6936
6937 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
6938 rel = next_rel;
6939 else
6940 while (1)
6941 {
6942 if (!dec_dynrel_count (rel->r_info, sec, info,
6943 NULL, h, sym))
6944 goto error_ret;
6945
6946 if (++rel == next_rel)
6947 break;
6948
6949 r_symndx = ELF64_R_SYM (rel->r_info);
6950 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6951 r_symndx, ibfd))
6952 goto error_ret;
6953 }
6954 }
6955 else
6956 {
6957 /* We'll be keeping this opd entry. */
6958 long adjust;
6959
6960 if (fdh != NULL)
6961 {
6962 /* Redefine the function descriptor symbol to
6963 this location in the opd section. It is
6964 necessary to update the value here rather
6965 than using an array of adjustments as we do
6966 for local symbols, because various places
6967 in the generic ELF code use the value
6968 stored in u.def.value. */
6969 fdh->elf.root.u.def.value = wptr - new_contents;
6970 fdh->adjust_done = 1;
6971 }
6972
6973 /* Local syms are a bit tricky. We could
6974 tweak them as they can be cached, but
6975 we'd need to look through the local syms
6976 for the function descriptor sym which we
6977 don't have at the moment. So keep an
6978 array of adjustments. */
6979 adjust = (wptr - new_contents) - (rptr - sec->contents);
6980 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
6981
6982 if (wptr != rptr)
6983 memcpy (wptr, rptr, opd_ent_size);
6984 wptr += opd_ent_size;
6985 if (add_aux_fields && opd_ent_size == 16)
6986 {
6987 memset (wptr, '\0', 8);
6988 wptr += 8;
6989 }
6990
6991 /* We need to adjust any reloc offsets to point to the
6992 new opd entries. */
6993 for ( ; rel != next_rel; ++rel)
6994 {
6995 rel->r_offset += adjust;
6996 if (write_rel != rel)
6997 memcpy (write_rel, rel, sizeof (*rel));
6998 ++write_rel;
6999 }
7000 }
7001
7002 rptr += opd_ent_size;
7003 }
7004
7005 sec->size = wptr - new_contents;
7006 sec->reloc_count = write_rel - relstart;
7007 if (add_aux_fields)
7008 {
7009 free (sec->contents);
7010 sec->contents = new_contents;
7011 }
7012
7013 /* Fudge the header size too, as this is used later in
7014 elf_bfd_final_link if we are emitting relocs. */
7015 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7016 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7017 some_edited = TRUE;
7018 }
7019 else if (elf_section_data (sec)->relocs != relstart)
7020 free (relstart);
7021
7022 if (local_syms != NULL
7023 && symtab_hdr->contents != (unsigned char *) local_syms)
7024 {
7025 if (!info->keep_memory)
7026 free (local_syms);
7027 else
7028 symtab_hdr->contents = (unsigned char *) local_syms;
7029 }
7030 }
7031
7032 if (some_edited)
7033 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7034
7035 /* If we are doing a final link and the last .opd entry is just 16 byte
7036 long, add a 8 byte padding after it. */
7037 if (need_pad != NULL && !bfd_link_relocatable (info))
7038 {
7039 bfd_byte *p;
7040
7041 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7042 {
7043 BFD_ASSERT (need_pad->size > 0);
7044
7045 p = bfd_malloc (need_pad->size + 8);
7046 if (p == NULL)
7047 return FALSE;
7048
7049 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7050 p, 0, need_pad->size))
7051 return FALSE;
7052
7053 need_pad->contents = p;
7054 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7055 }
7056 else
7057 {
7058 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7059 if (p == NULL)
7060 return FALSE;
7061
7062 need_pad->contents = p;
7063 }
7064
7065 memset (need_pad->contents + need_pad->size, 0, 8);
7066 need_pad->size += 8;
7067 }
7068
7069 return TRUE;
7070 }
7071
7072 /* Analyze inline PLT call relocations to see whether calls to locally
7073 defined functions can be converted to direct calls. */
7074
7075 bfd_boolean
7076 ppc64_elf_inline_plt (struct bfd_link_info *info)
7077 {
7078 struct ppc_link_hash_table *htab;
7079 bfd *ibfd;
7080 asection *sec;
7081 bfd_vma low_vma, high_vma, limit;
7082
7083 htab = ppc_hash_table (info);
7084 if (htab == NULL)
7085 return FALSE;
7086
7087 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7088 reduced somewhat to cater for possible stubs that might be added
7089 between the call and its destination. */
7090 if (htab->params->group_size < 0)
7091 {
7092 limit = -htab->params->group_size;
7093 if (limit == 1)
7094 limit = 0x1e00000;
7095 }
7096 else
7097 {
7098 limit = htab->params->group_size;
7099 if (limit == 1)
7100 limit = 0x1c00000;
7101 }
7102
7103 low_vma = -1;
7104 high_vma = 0;
7105 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7106 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7107 {
7108 if (low_vma > sec->vma)
7109 low_vma = sec->vma;
7110 if (high_vma < sec->vma + sec->size)
7111 high_vma = sec->vma + sec->size;
7112 }
7113
7114 /* If a "bl" can reach anywhere in local code sections, then we can
7115 convert all inline PLT sequences to direct calls when the symbol
7116 is local. */
7117 if (high_vma - low_vma < limit)
7118 {
7119 htab->can_convert_all_inline_plt = 1;
7120 return TRUE;
7121 }
7122
7123 /* Otherwise, go looking through relocs for cases where a direct
7124 call won't reach. Mark the symbol on any such reloc to disable
7125 the optimization and keep the PLT entry as it seems likely that
7126 this will be better than creating trampolines. Note that this
7127 will disable the optimization for all inline PLT calls to a
7128 particular symbol, not just those that won't reach. The
7129 difficulty in doing a more precise optimization is that the
7130 linker needs to make a decision depending on whether a
7131 particular R_PPC64_PLTCALL insn can be turned into a direct
7132 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7133 the sequence, and there is nothing that ties those relocs
7134 together except their symbol. */
7135
7136 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7137 {
7138 Elf_Internal_Shdr *symtab_hdr;
7139 Elf_Internal_Sym *local_syms;
7140
7141 if (!is_ppc64_elf (ibfd))
7142 continue;
7143
7144 local_syms = NULL;
7145 symtab_hdr = &elf_symtab_hdr (ibfd);
7146
7147 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7148 if (ppc64_elf_section_data (sec)->has_pltcall
7149 && !bfd_is_abs_section (sec->output_section))
7150 {
7151 Elf_Internal_Rela *relstart, *rel, *relend;
7152
7153 /* Read the relocations. */
7154 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7155 info->keep_memory);
7156 if (relstart == NULL)
7157 return FALSE;
7158
7159 relend = relstart + sec->reloc_count;
7160 for (rel = relstart; rel < relend; )
7161 {
7162 enum elf_ppc64_reloc_type r_type;
7163 unsigned long r_symndx;
7164 asection *sym_sec;
7165 struct elf_link_hash_entry *h;
7166 Elf_Internal_Sym *sym;
7167 unsigned char *tls_maskp;
7168
7169 r_type = ELF64_R_TYPE (rel->r_info);
7170 if (r_type != R_PPC64_PLTCALL)
7171 continue;
7172
7173 r_symndx = ELF64_R_SYM (rel->r_info);
7174 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7175 r_symndx, ibfd))
7176 {
7177 if (elf_section_data (sec)->relocs != relstart)
7178 free (relstart);
7179 if (local_syms != NULL
7180 && symtab_hdr->contents != (bfd_byte *) local_syms)
7181 free (local_syms);
7182 return FALSE;
7183 }
7184
7185 if (sym_sec != NULL && sym_sec->output_section != NULL)
7186 {
7187 bfd_vma from, to;
7188 if (h != NULL)
7189 to = h->root.u.def.value;
7190 else
7191 to = sym->st_value;
7192 to += (rel->r_addend
7193 + sym_sec->output_offset
7194 + sym_sec->output_section->vma);
7195 from = (rel->r_offset
7196 + sec->output_offset
7197 + sec->output_section->vma);
7198 if (to - from + limit < 2 * limit)
7199 *tls_maskp &= ~PLT_KEEP;
7200 }
7201 }
7202 if (elf_section_data (sec)->relocs != relstart)
7203 free (relstart);
7204 }
7205
7206 if (local_syms != NULL
7207 && symtab_hdr->contents != (unsigned char *) local_syms)
7208 {
7209 if (!info->keep_memory)
7210 free (local_syms);
7211 else
7212 symtab_hdr->contents = (unsigned char *) local_syms;
7213 }
7214 }
7215
7216 return TRUE;
7217 }
7218
7219 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7220
7221 asection *
7222 ppc64_elf_tls_setup (struct bfd_link_info *info)
7223 {
7224 struct ppc_link_hash_table *htab;
7225
7226 htab = ppc_hash_table (info);
7227 if (htab == NULL)
7228 return NULL;
7229
7230 if (abiversion (info->output_bfd) == 1)
7231 htab->opd_abi = 1;
7232
7233 if (htab->params->no_multi_toc)
7234 htab->do_multi_toc = 0;
7235 else if (!htab->do_multi_toc)
7236 htab->params->no_multi_toc = 1;
7237
7238 /* Default to --no-plt-localentry, as this option can cause problems
7239 with symbol interposition. For example, glibc libpthread.so and
7240 libc.so duplicate many pthread symbols, with a fallback
7241 implementation in libc.so. In some cases the fallback does more
7242 work than the pthread implementation. __pthread_condattr_destroy
7243 is one such symbol: the libpthread.so implementation is
7244 localentry:0 while the libc.so implementation is localentry:8.
7245 An app that "cleverly" uses dlopen to only load necessary
7246 libraries at runtime may omit loading libpthread.so when not
7247 running multi-threaded, which then results in the libc.so
7248 fallback symbols being used and ld.so complaining. Now there
7249 are workarounds in ld (see non_zero_localentry) to detect the
7250 pthread situation, but that may not be the only case where
7251 --plt-localentry can cause trouble. */
7252 if (htab->params->plt_localentry0 < 0)
7253 htab->params->plt_localentry0 = 0;
7254 if (htab->params->plt_localentry0
7255 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7256 FALSE, FALSE, FALSE) == NULL)
7257 _bfd_error_handler
7258 (_("warning: --plt-localentry is especially dangerous without "
7259 "ld.so support to detect ABI violations"));
7260
7261 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7262 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7263 FALSE, FALSE, TRUE));
7264 /* Move dynamic linking info to the function descriptor sym. */
7265 if (htab->tls_get_addr != NULL)
7266 func_desc_adjust (&htab->tls_get_addr->elf, info);
7267 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7268 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7269 FALSE, FALSE, TRUE));
7270 if (htab->params->tls_get_addr_opt)
7271 {
7272 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7273
7274 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7275 FALSE, FALSE, TRUE);
7276 if (opt != NULL)
7277 func_desc_adjust (opt, info);
7278 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7279 FALSE, FALSE, TRUE);
7280 if (opt_fd != NULL
7281 && (opt_fd->root.type == bfd_link_hash_defined
7282 || opt_fd->root.type == bfd_link_hash_defweak))
7283 {
7284 /* If glibc supports an optimized __tls_get_addr call stub,
7285 signalled by the presence of __tls_get_addr_opt, and we'll
7286 be calling __tls_get_addr via a plt call stub, then
7287 make __tls_get_addr point to __tls_get_addr_opt. */
7288 tga_fd = &htab->tls_get_addr_fd->elf;
7289 if (htab->elf.dynamic_sections_created
7290 && tga_fd != NULL
7291 && (tga_fd->type == STT_FUNC
7292 || tga_fd->needs_plt)
7293 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7294 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
7295 {
7296 struct plt_entry *ent;
7297
7298 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7299 if (ent->plt.refcount > 0)
7300 break;
7301 if (ent != NULL)
7302 {
7303 tga_fd->root.type = bfd_link_hash_indirect;
7304 tga_fd->root.u.i.link = &opt_fd->root;
7305 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7306 opt_fd->mark = 1;
7307 if (opt_fd->dynindx != -1)
7308 {
7309 /* Use __tls_get_addr_opt in dynamic relocations. */
7310 opt_fd->dynindx = -1;
7311 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7312 opt_fd->dynstr_index);
7313 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7314 return NULL;
7315 }
7316 htab->tls_get_addr_fd
7317 = (struct ppc_link_hash_entry *) opt_fd;
7318 tga = &htab->tls_get_addr->elf;
7319 if (opt != NULL && tga != NULL)
7320 {
7321 tga->root.type = bfd_link_hash_indirect;
7322 tga->root.u.i.link = &opt->root;
7323 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7324 opt->mark = 1;
7325 _bfd_elf_link_hash_hide_symbol (info, opt,
7326 tga->forced_local);
7327 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7328 }
7329 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7330 htab->tls_get_addr_fd->is_func_descriptor = 1;
7331 if (htab->tls_get_addr != NULL)
7332 {
7333 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7334 htab->tls_get_addr->is_func = 1;
7335 }
7336 }
7337 }
7338 }
7339 else if (htab->params->tls_get_addr_opt < 0)
7340 htab->params->tls_get_addr_opt = 0;
7341 }
7342 return _bfd_elf_tls_setup (info->output_bfd, info);
7343 }
7344
7345 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7346 HASH1 or HASH2. */
7347
7348 static bfd_boolean
7349 branch_reloc_hash_match (const bfd *ibfd,
7350 const Elf_Internal_Rela *rel,
7351 const struct ppc_link_hash_entry *hash1,
7352 const struct ppc_link_hash_entry *hash2)
7353 {
7354 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7355 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7356 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7357
7358 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7359 {
7360 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7361 struct elf_link_hash_entry *h;
7362
7363 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7364 h = elf_follow_link (h);
7365 if (h == &hash1->elf || h == &hash2->elf)
7366 return TRUE;
7367 }
7368 return FALSE;
7369 }
7370
7371 /* Run through all the TLS relocs looking for optimization
7372 opportunities. The linker has been hacked (see ppc64elf.em) to do
7373 a preliminary section layout so that we know the TLS segment
7374 offsets. We can't optimize earlier because some optimizations need
7375 to know the tp offset, and we need to optimize before allocating
7376 dynamic relocations. */
7377
7378 bfd_boolean
7379 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7380 {
7381 bfd *ibfd;
7382 asection *sec;
7383 struct ppc_link_hash_table *htab;
7384 unsigned char *toc_ref;
7385 int pass;
7386
7387 if (!bfd_link_executable (info))
7388 return TRUE;
7389
7390 htab = ppc_hash_table (info);
7391 if (htab == NULL)
7392 return FALSE;
7393
7394 /* Make two passes over the relocs. On the first pass, mark toc
7395 entries involved with tls relocs, and check that tls relocs
7396 involved in setting up a tls_get_addr call are indeed followed by
7397 such a call. If they are not, we can't do any tls optimization.
7398 On the second pass twiddle tls_mask flags to notify
7399 relocate_section that optimization can be done, and adjust got
7400 and plt refcounts. */
7401 toc_ref = NULL;
7402 for (pass = 0; pass < 2; ++pass)
7403 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7404 {
7405 Elf_Internal_Sym *locsyms = NULL;
7406 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7407
7408 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7409 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7410 {
7411 Elf_Internal_Rela *relstart, *rel, *relend;
7412 bfd_boolean found_tls_get_addr_arg = 0;
7413
7414 /* Read the relocations. */
7415 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7416 info->keep_memory);
7417 if (relstart == NULL)
7418 {
7419 free (toc_ref);
7420 return FALSE;
7421 }
7422
7423 relend = relstart + sec->reloc_count;
7424 for (rel = relstart; rel < relend; rel++)
7425 {
7426 enum elf_ppc64_reloc_type r_type;
7427 unsigned long r_symndx;
7428 struct elf_link_hash_entry *h;
7429 Elf_Internal_Sym *sym;
7430 asection *sym_sec;
7431 unsigned char *tls_mask;
7432 unsigned char tls_set, tls_clear, tls_type = 0;
7433 bfd_vma value;
7434 bfd_boolean ok_tprel, is_local;
7435 long toc_ref_index = 0;
7436 int expecting_tls_get_addr = 0;
7437 bfd_boolean ret = FALSE;
7438
7439 r_symndx = ELF64_R_SYM (rel->r_info);
7440 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7441 r_symndx, ibfd))
7442 {
7443 err_free_rel:
7444 if (elf_section_data (sec)->relocs != relstart)
7445 free (relstart);
7446 if (toc_ref != NULL)
7447 free (toc_ref);
7448 if (locsyms != NULL
7449 && (elf_symtab_hdr (ibfd).contents
7450 != (unsigned char *) locsyms))
7451 free (locsyms);
7452 return ret;
7453 }
7454
7455 if (h != NULL)
7456 {
7457 if (h->root.type == bfd_link_hash_defined
7458 || h->root.type == bfd_link_hash_defweak)
7459 value = h->root.u.def.value;
7460 else if (h->root.type == bfd_link_hash_undefweak)
7461 value = 0;
7462 else
7463 {
7464 found_tls_get_addr_arg = 0;
7465 continue;
7466 }
7467 }
7468 else
7469 /* Symbols referenced by TLS relocs must be of type
7470 STT_TLS. So no need for .opd local sym adjust. */
7471 value = sym->st_value;
7472
7473 ok_tprel = FALSE;
7474 is_local = FALSE;
7475 if (h == NULL
7476 || !h->def_dynamic)
7477 {
7478 is_local = TRUE;
7479 if (h != NULL
7480 && h->root.type == bfd_link_hash_undefweak)
7481 ok_tprel = TRUE;
7482 else if (sym_sec != NULL
7483 && sym_sec->output_section != NULL)
7484 {
7485 value += sym_sec->output_offset;
7486 value += sym_sec->output_section->vma;
7487 value -= htab->elf.tls_sec->vma;
7488 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7489 < (bfd_vma) 1 << 32);
7490 }
7491 }
7492
7493 r_type = ELF64_R_TYPE (rel->r_info);
7494 /* If this section has old-style __tls_get_addr calls
7495 without marker relocs, then check that each
7496 __tls_get_addr call reloc is preceded by a reloc
7497 that conceivably belongs to the __tls_get_addr arg
7498 setup insn. If we don't find matching arg setup
7499 relocs, don't do any tls optimization. */
7500 if (pass == 0
7501 && sec->has_tls_get_addr_call
7502 && h != NULL
7503 && (h == &htab->tls_get_addr->elf
7504 || h == &htab->tls_get_addr_fd->elf)
7505 && !found_tls_get_addr_arg
7506 && is_branch_reloc (r_type))
7507 {
7508 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7509 "TLS optimization disabled\n"),
7510 ibfd, sec, rel->r_offset);
7511 ret = TRUE;
7512 goto err_free_rel;
7513 }
7514
7515 found_tls_get_addr_arg = 0;
7516 switch (r_type)
7517 {
7518 case R_PPC64_GOT_TLSLD16:
7519 case R_PPC64_GOT_TLSLD16_LO:
7520 expecting_tls_get_addr = 1;
7521 found_tls_get_addr_arg = 1;
7522 /* Fall through. */
7523
7524 case R_PPC64_GOT_TLSLD16_HI:
7525 case R_PPC64_GOT_TLSLD16_HA:
7526 /* These relocs should never be against a symbol
7527 defined in a shared lib. Leave them alone if
7528 that turns out to be the case. */
7529 if (!is_local)
7530 continue;
7531
7532 /* LD -> LE */
7533 tls_set = 0;
7534 tls_clear = TLS_LD;
7535 tls_type = TLS_TLS | TLS_LD;
7536 break;
7537
7538 case R_PPC64_GOT_TLSGD16:
7539 case R_PPC64_GOT_TLSGD16_LO:
7540 expecting_tls_get_addr = 1;
7541 found_tls_get_addr_arg = 1;
7542 /* Fall through. */
7543
7544 case R_PPC64_GOT_TLSGD16_HI:
7545 case R_PPC64_GOT_TLSGD16_HA:
7546 if (ok_tprel)
7547 /* GD -> LE */
7548 tls_set = 0;
7549 else
7550 /* GD -> IE */
7551 tls_set = TLS_TLS | TLS_TPRELGD;
7552 tls_clear = TLS_GD;
7553 tls_type = TLS_TLS | TLS_GD;
7554 break;
7555
7556 case R_PPC64_GOT_TPREL16_DS:
7557 case R_PPC64_GOT_TPREL16_LO_DS:
7558 case R_PPC64_GOT_TPREL16_HI:
7559 case R_PPC64_GOT_TPREL16_HA:
7560 if (ok_tprel)
7561 {
7562 /* IE -> LE */
7563 tls_set = 0;
7564 tls_clear = TLS_TPREL;
7565 tls_type = TLS_TLS | TLS_TPREL;
7566 break;
7567 }
7568 continue;
7569
7570 case R_PPC64_TLSGD:
7571 case R_PPC64_TLSLD:
7572 if (rel + 1 < relend
7573 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7574 {
7575 if (pass != 0
7576 && (ELF64_R_TYPE (rel[1].r_info)
7577 != R_PPC64_PLTSEQ))
7578 {
7579 r_symndx = ELF64_R_SYM (rel[1].r_info);
7580 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
7581 r_symndx, ibfd))
7582 goto err_free_rel;
7583 if (h != NULL)
7584 {
7585 struct plt_entry *ent = NULL;
7586
7587 for (ent = h->plt.plist;
7588 ent != NULL;
7589 ent = ent->next)
7590 if (ent->addend == rel[1].r_addend)
7591 break;
7592
7593 if (ent != NULL
7594 && ent->plt.refcount > 0)
7595 ent->plt.refcount -= 1;
7596 }
7597 }
7598 continue;
7599 }
7600 found_tls_get_addr_arg = 1;
7601 /* Fall through. */
7602
7603 case R_PPC64_TLS:
7604 case R_PPC64_TOC16:
7605 case R_PPC64_TOC16_LO:
7606 if (sym_sec == NULL || sym_sec != toc)
7607 continue;
7608
7609 /* Mark this toc entry as referenced by a TLS
7610 code sequence. We can do that now in the
7611 case of R_PPC64_TLS, and after checking for
7612 tls_get_addr for the TOC16 relocs. */
7613 if (toc_ref == NULL)
7614 toc_ref
7615 = bfd_zmalloc (toc->output_section->rawsize / 8);
7616 if (toc_ref == NULL)
7617 goto err_free_rel;
7618
7619 if (h != NULL)
7620 value = h->root.u.def.value;
7621 else
7622 value = sym->st_value;
7623 value += rel->r_addend;
7624 if (value % 8 != 0)
7625 continue;
7626 BFD_ASSERT (value < toc->size
7627 && toc->output_offset % 8 == 0);
7628 toc_ref_index = (value + toc->output_offset) / 8;
7629 if (r_type == R_PPC64_TLS
7630 || r_type == R_PPC64_TLSGD
7631 || r_type == R_PPC64_TLSLD)
7632 {
7633 toc_ref[toc_ref_index] = 1;
7634 continue;
7635 }
7636
7637 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7638 continue;
7639
7640 tls_set = 0;
7641 tls_clear = 0;
7642 expecting_tls_get_addr = 2;
7643 break;
7644
7645 case R_PPC64_TPREL64:
7646 if (pass == 0
7647 || sec != toc
7648 || toc_ref == NULL
7649 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7650 continue;
7651 if (ok_tprel)
7652 {
7653 /* IE -> LE */
7654 tls_set = TLS_EXPLICIT;
7655 tls_clear = TLS_TPREL;
7656 break;
7657 }
7658 continue;
7659
7660 case R_PPC64_DTPMOD64:
7661 if (pass == 0
7662 || sec != toc
7663 || toc_ref == NULL
7664 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7665 continue;
7666 if (rel + 1 < relend
7667 && (rel[1].r_info
7668 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7669 && rel[1].r_offset == rel->r_offset + 8)
7670 {
7671 if (ok_tprel)
7672 /* GD -> LE */
7673 tls_set = TLS_EXPLICIT | TLS_GD;
7674 else
7675 /* GD -> IE */
7676 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7677 tls_clear = TLS_GD;
7678 }
7679 else
7680 {
7681 if (!is_local)
7682 continue;
7683
7684 /* LD -> LE */
7685 tls_set = TLS_EXPLICIT;
7686 tls_clear = TLS_LD;
7687 }
7688 break;
7689
7690 default:
7691 continue;
7692 }
7693
7694 if (pass == 0)
7695 {
7696 if (!expecting_tls_get_addr
7697 || !sec->has_tls_get_addr_call)
7698 continue;
7699
7700 if (rel + 1 < relend
7701 && branch_reloc_hash_match (ibfd, rel + 1,
7702 htab->tls_get_addr,
7703 htab->tls_get_addr_fd))
7704 {
7705 if (expecting_tls_get_addr == 2)
7706 {
7707 /* Check for toc tls entries. */
7708 unsigned char *toc_tls;
7709 int retval;
7710
7711 retval = get_tls_mask (&toc_tls, NULL, NULL,
7712 &locsyms,
7713 rel, ibfd);
7714 if (retval == 0)
7715 goto err_free_rel;
7716 if (toc_tls != NULL)
7717 {
7718 if ((*toc_tls & TLS_TLS) != 0
7719 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
7720 found_tls_get_addr_arg = 1;
7721 if (retval > 1)
7722 toc_ref[toc_ref_index] = 1;
7723 }
7724 }
7725 continue;
7726 }
7727
7728 /* Uh oh, we didn't find the expected call. We
7729 could just mark this symbol to exclude it
7730 from tls optimization but it's safer to skip
7731 the entire optimization. */
7732 /* xgettext:c-format */
7733 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
7734 "TLS optimization disabled\n"),
7735 ibfd, sec, rel->r_offset);
7736 ret = TRUE;
7737 goto err_free_rel;
7738 }
7739
7740 /* If we don't have old-style __tls_get_addr calls
7741 without TLSGD/TLSLD marker relocs, and we haven't
7742 found a new-style __tls_get_addr call with a
7743 marker for this symbol, then we either have a
7744 broken object file or an -mlongcall style
7745 indirect call to __tls_get_addr without a marker.
7746 Disable optimization in this case. */
7747 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
7748 && (tls_set & TLS_EXPLICIT) == 0
7749 && !sec->has_tls_get_addr_call
7750 && ((*tls_mask & (TLS_TLS | TLS_MARK))
7751 != (TLS_TLS | TLS_MARK)))
7752 continue;
7753
7754 if (expecting_tls_get_addr)
7755 {
7756 struct plt_entry *ent = NULL;
7757
7758 if (htab->tls_get_addr != NULL)
7759 for (ent = htab->tls_get_addr->elf.plt.plist;
7760 ent != NULL;
7761 ent = ent->next)
7762 if (ent->addend == 0)
7763 break;
7764
7765 if (ent == NULL && htab->tls_get_addr_fd != NULL)
7766 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7767 ent != NULL;
7768 ent = ent->next)
7769 if (ent->addend == 0)
7770 break;
7771
7772 if (ent != NULL
7773 && ent->plt.refcount > 0)
7774 ent->plt.refcount -= 1;
7775 }
7776
7777 if (tls_clear == 0)
7778 continue;
7779
7780 if ((tls_set & TLS_EXPLICIT) == 0)
7781 {
7782 struct got_entry *ent;
7783
7784 /* Adjust got entry for this reloc. */
7785 if (h != NULL)
7786 ent = h->got.glist;
7787 else
7788 ent = elf_local_got_ents (ibfd)[r_symndx];
7789
7790 for (; ent != NULL; ent = ent->next)
7791 if (ent->addend == rel->r_addend
7792 && ent->owner == ibfd
7793 && ent->tls_type == tls_type)
7794 break;
7795 if (ent == NULL)
7796 abort ();
7797
7798 if (tls_set == 0)
7799 {
7800 /* We managed to get rid of a got entry. */
7801 if (ent->got.refcount > 0)
7802 ent->got.refcount -= 1;
7803 }
7804 }
7805 else
7806 {
7807 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7808 we'll lose one or two dyn relocs. */
7809 if (!dec_dynrel_count (rel->r_info, sec, info,
7810 NULL, h, sym))
7811 return FALSE;
7812
7813 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7814 {
7815 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7816 NULL, h, sym))
7817 return FALSE;
7818 }
7819 }
7820
7821 *tls_mask |= tls_set;
7822 *tls_mask &= ~tls_clear;
7823 }
7824
7825 if (elf_section_data (sec)->relocs != relstart)
7826 free (relstart);
7827 }
7828
7829 if (locsyms != NULL
7830 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7831 {
7832 if (!info->keep_memory)
7833 free (locsyms);
7834 else
7835 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7836 }
7837 }
7838
7839 if (toc_ref != NULL)
7840 free (toc_ref);
7841 htab->do_tls_opt = 1;
7842 return TRUE;
7843 }
7844
7845 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7846 the values of any global symbols in a toc section that has been
7847 edited. Globals in toc sections should be a rarity, so this function
7848 sets a flag if any are found in toc sections other than the one just
7849 edited, so that further hash table traversals can be avoided. */
7850
7851 struct adjust_toc_info
7852 {
7853 asection *toc;
7854 unsigned long *skip;
7855 bfd_boolean global_toc_syms;
7856 };
7857
7858 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7859
7860 static bfd_boolean
7861 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7862 {
7863 struct ppc_link_hash_entry *eh;
7864 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7865 unsigned long i;
7866
7867 if (h->root.type != bfd_link_hash_defined
7868 && h->root.type != bfd_link_hash_defweak)
7869 return TRUE;
7870
7871 eh = (struct ppc_link_hash_entry *) h;
7872 if (eh->adjust_done)
7873 return TRUE;
7874
7875 if (eh->elf.root.u.def.section == toc_inf->toc)
7876 {
7877 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7878 i = toc_inf->toc->rawsize >> 3;
7879 else
7880 i = eh->elf.root.u.def.value >> 3;
7881
7882 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
7883 {
7884 _bfd_error_handler
7885 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7886 do
7887 ++i;
7888 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
7889 eh->elf.root.u.def.value = (bfd_vma) i << 3;
7890 }
7891
7892 eh->elf.root.u.def.value -= toc_inf->skip[i];
7893 eh->adjust_done = 1;
7894 }
7895 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7896 toc_inf->global_toc_syms = TRUE;
7897
7898 return TRUE;
7899 }
7900
7901 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
7902 on a _LO variety toc/got reloc. */
7903
7904 static bfd_boolean
7905 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
7906 {
7907 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7908 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
7909 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7910 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7911 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7912 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7913 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7914 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7915 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7916 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7917 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7918 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7919 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7920 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7921 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7922 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7923 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7924 /* Exclude lfqu by testing reloc. If relocs are ever
7925 defined for the reduced D field in psq_lu then those
7926 will need testing too. */
7927 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7928 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7929 && (insn & 1) == 0)
7930 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7931 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7932 /* Exclude stfqu. psq_stu as above for psq_lu. */
7933 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7934 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7935 && (insn & 1) == 0));
7936 }
7937
7938 /* Examine all relocs referencing .toc sections in order to remove
7939 unused .toc entries. */
7940
7941 bfd_boolean
7942 ppc64_elf_edit_toc (struct bfd_link_info *info)
7943 {
7944 bfd *ibfd;
7945 struct adjust_toc_info toc_inf;
7946 struct ppc_link_hash_table *htab = ppc_hash_table (info);
7947
7948 htab->do_toc_opt = 1;
7949 toc_inf.global_toc_syms = TRUE;
7950 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7951 {
7952 asection *toc, *sec;
7953 Elf_Internal_Shdr *symtab_hdr;
7954 Elf_Internal_Sym *local_syms;
7955 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
7956 unsigned long *skip, *drop;
7957 unsigned char *used;
7958 unsigned char *keep, last, some_unused;
7959
7960 if (!is_ppc64_elf (ibfd))
7961 continue;
7962
7963 toc = bfd_get_section_by_name (ibfd, ".toc");
7964 if (toc == NULL
7965 || toc->size == 0
7966 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
7967 || discarded_section (toc))
7968 continue;
7969
7970 toc_relocs = NULL;
7971 local_syms = NULL;
7972 symtab_hdr = &elf_symtab_hdr (ibfd);
7973
7974 /* Look at sections dropped from the final link. */
7975 skip = NULL;
7976 relstart = NULL;
7977 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7978 {
7979 if (sec->reloc_count == 0
7980 || !discarded_section (sec)
7981 || get_opd_info (sec)
7982 || (sec->flags & SEC_ALLOC) == 0
7983 || (sec->flags & SEC_DEBUGGING) != 0)
7984 continue;
7985
7986 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7987 if (relstart == NULL)
7988 goto error_ret;
7989
7990 /* Run through the relocs to see which toc entries might be
7991 unused. */
7992 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7993 {
7994 enum elf_ppc64_reloc_type r_type;
7995 unsigned long r_symndx;
7996 asection *sym_sec;
7997 struct elf_link_hash_entry *h;
7998 Elf_Internal_Sym *sym;
7999 bfd_vma val;
8000
8001 r_type = ELF64_R_TYPE (rel->r_info);
8002 switch (r_type)
8003 {
8004 default:
8005 continue;
8006
8007 case R_PPC64_TOC16:
8008 case R_PPC64_TOC16_LO:
8009 case R_PPC64_TOC16_HI:
8010 case R_PPC64_TOC16_HA:
8011 case R_PPC64_TOC16_DS:
8012 case R_PPC64_TOC16_LO_DS:
8013 break;
8014 }
8015
8016 r_symndx = ELF64_R_SYM (rel->r_info);
8017 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8018 r_symndx, ibfd))
8019 goto error_ret;
8020
8021 if (sym_sec != toc)
8022 continue;
8023
8024 if (h != NULL)
8025 val = h->root.u.def.value;
8026 else
8027 val = sym->st_value;
8028 val += rel->r_addend;
8029
8030 if (val >= toc->size)
8031 continue;
8032
8033 /* Anything in the toc ought to be aligned to 8 bytes.
8034 If not, don't mark as unused. */
8035 if (val & 7)
8036 continue;
8037
8038 if (skip == NULL)
8039 {
8040 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8041 if (skip == NULL)
8042 goto error_ret;
8043 }
8044
8045 skip[val >> 3] = ref_from_discarded;
8046 }
8047
8048 if (elf_section_data (sec)->relocs != relstart)
8049 free (relstart);
8050 }
8051
8052 /* For largetoc loads of address constants, we can convert
8053 . addis rx,2,addr@got@ha
8054 . ld ry,addr@got@l(rx)
8055 to
8056 . addis rx,2,addr@toc@ha
8057 . addi ry,rx,addr@toc@l
8058 when addr is within 2G of the toc pointer. This then means
8059 that the word storing "addr" in the toc is no longer needed. */
8060
8061 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8062 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8063 && toc->reloc_count != 0)
8064 {
8065 /* Read toc relocs. */
8066 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8067 info->keep_memory);
8068 if (toc_relocs == NULL)
8069 goto error_ret;
8070
8071 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8072 {
8073 enum elf_ppc64_reloc_type r_type;
8074 unsigned long r_symndx;
8075 asection *sym_sec;
8076 struct elf_link_hash_entry *h;
8077 Elf_Internal_Sym *sym;
8078 bfd_vma val, addr;
8079
8080 r_type = ELF64_R_TYPE (rel->r_info);
8081 if (r_type != R_PPC64_ADDR64)
8082 continue;
8083
8084 r_symndx = ELF64_R_SYM (rel->r_info);
8085 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8086 r_symndx, ibfd))
8087 goto error_ret;
8088
8089 if (sym_sec == NULL
8090 || sym_sec->output_section == NULL
8091 || discarded_section (sym_sec))
8092 continue;
8093
8094 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8095 continue;
8096
8097 if (h != NULL)
8098 {
8099 if (h->type == STT_GNU_IFUNC)
8100 continue;
8101 val = h->root.u.def.value;
8102 }
8103 else
8104 {
8105 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8106 continue;
8107 val = sym->st_value;
8108 }
8109 val += rel->r_addend;
8110 val += sym_sec->output_section->vma + sym_sec->output_offset;
8111
8112 /* We don't yet know the exact toc pointer value, but we
8113 know it will be somewhere in the toc section. Don't
8114 optimize if the difference from any possible toc
8115 pointer is outside [ff..f80008000, 7fff7fff]. */
8116 addr = toc->output_section->vma + TOC_BASE_OFF;
8117 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8118 continue;
8119
8120 addr = toc->output_section->vma + toc->output_section->rawsize;
8121 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8122 continue;
8123
8124 if (skip == NULL)
8125 {
8126 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8127 if (skip == NULL)
8128 goto error_ret;
8129 }
8130
8131 skip[rel->r_offset >> 3]
8132 |= can_optimize | ((rel - toc_relocs) << 2);
8133 }
8134 }
8135
8136 if (skip == NULL)
8137 continue;
8138
8139 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8140 if (used == NULL)
8141 {
8142 error_ret:
8143 if (local_syms != NULL
8144 && symtab_hdr->contents != (unsigned char *) local_syms)
8145 free (local_syms);
8146 if (sec != NULL
8147 && relstart != NULL
8148 && elf_section_data (sec)->relocs != relstart)
8149 free (relstart);
8150 if (toc_relocs != NULL
8151 && elf_section_data (toc)->relocs != toc_relocs)
8152 free (toc_relocs);
8153 if (skip != NULL)
8154 free (skip);
8155 return FALSE;
8156 }
8157
8158 /* Now check all kept sections that might reference the toc.
8159 Check the toc itself last. */
8160 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8161 : ibfd->sections);
8162 sec != NULL;
8163 sec = (sec == toc ? NULL
8164 : sec->next == NULL ? toc
8165 : sec->next == toc && toc->next ? toc->next
8166 : sec->next))
8167 {
8168 int repeat;
8169
8170 if (sec->reloc_count == 0
8171 || discarded_section (sec)
8172 || get_opd_info (sec)
8173 || (sec->flags & SEC_ALLOC) == 0
8174 || (sec->flags & SEC_DEBUGGING) != 0)
8175 continue;
8176
8177 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8178 info->keep_memory);
8179 if (relstart == NULL)
8180 {
8181 free (used);
8182 goto error_ret;
8183 }
8184
8185 /* Mark toc entries referenced as used. */
8186 do
8187 {
8188 repeat = 0;
8189 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8190 {
8191 enum elf_ppc64_reloc_type r_type;
8192 unsigned long r_symndx;
8193 asection *sym_sec;
8194 struct elf_link_hash_entry *h;
8195 Elf_Internal_Sym *sym;
8196 bfd_vma val;
8197 enum {no_check, check_lo, check_ha} insn_check;
8198
8199 r_type = ELF64_R_TYPE (rel->r_info);
8200 switch (r_type)
8201 {
8202 default:
8203 insn_check = no_check;
8204 break;
8205
8206 case R_PPC64_GOT_TLSLD16_HA:
8207 case R_PPC64_GOT_TLSGD16_HA:
8208 case R_PPC64_GOT_TPREL16_HA:
8209 case R_PPC64_GOT_DTPREL16_HA:
8210 case R_PPC64_GOT16_HA:
8211 case R_PPC64_TOC16_HA:
8212 insn_check = check_ha;
8213 break;
8214
8215 case R_PPC64_GOT_TLSLD16_LO:
8216 case R_PPC64_GOT_TLSGD16_LO:
8217 case R_PPC64_GOT_TPREL16_LO_DS:
8218 case R_PPC64_GOT_DTPREL16_LO_DS:
8219 case R_PPC64_GOT16_LO:
8220 case R_PPC64_GOT16_LO_DS:
8221 case R_PPC64_TOC16_LO:
8222 case R_PPC64_TOC16_LO_DS:
8223 insn_check = check_lo;
8224 break;
8225 }
8226
8227 if (insn_check != no_check)
8228 {
8229 bfd_vma off = rel->r_offset & ~3;
8230 unsigned char buf[4];
8231 unsigned int insn;
8232
8233 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8234 {
8235 free (used);
8236 goto error_ret;
8237 }
8238 insn = bfd_get_32 (ibfd, buf);
8239 if (insn_check == check_lo
8240 ? !ok_lo_toc_insn (insn, r_type)
8241 : ((insn & ((0x3f << 26) | 0x1f << 16))
8242 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8243 {
8244 char str[12];
8245
8246 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8247 sprintf (str, "%#08x", insn);
8248 info->callbacks->einfo
8249 /* xgettext:c-format */
8250 (_("%H: toc optimization is not supported for"
8251 " %s instruction\n"),
8252 ibfd, sec, rel->r_offset & ~3, str);
8253 }
8254 }
8255
8256 switch (r_type)
8257 {
8258 case R_PPC64_TOC16:
8259 case R_PPC64_TOC16_LO:
8260 case R_PPC64_TOC16_HI:
8261 case R_PPC64_TOC16_HA:
8262 case R_PPC64_TOC16_DS:
8263 case R_PPC64_TOC16_LO_DS:
8264 /* In case we're taking addresses of toc entries. */
8265 case R_PPC64_ADDR64:
8266 break;
8267
8268 default:
8269 continue;
8270 }
8271
8272 r_symndx = ELF64_R_SYM (rel->r_info);
8273 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8274 r_symndx, ibfd))
8275 {
8276 free (used);
8277 goto error_ret;
8278 }
8279
8280 if (sym_sec != toc)
8281 continue;
8282
8283 if (h != NULL)
8284 val = h->root.u.def.value;
8285 else
8286 val = sym->st_value;
8287 val += rel->r_addend;
8288
8289 if (val >= toc->size)
8290 continue;
8291
8292 if ((skip[val >> 3] & can_optimize) != 0)
8293 {
8294 bfd_vma off;
8295 unsigned char opc;
8296
8297 switch (r_type)
8298 {
8299 case R_PPC64_TOC16_HA:
8300 break;
8301
8302 case R_PPC64_TOC16_LO_DS:
8303 off = rel->r_offset;
8304 off += (bfd_big_endian (ibfd) ? -2 : 3);
8305 if (!bfd_get_section_contents (ibfd, sec, &opc,
8306 off, 1))
8307 {
8308 free (used);
8309 goto error_ret;
8310 }
8311 if ((opc & (0x3f << 2)) == (58u << 2))
8312 break;
8313 /* Fall through. */
8314
8315 default:
8316 /* Wrong sort of reloc, or not a ld. We may
8317 as well clear ref_from_discarded too. */
8318 skip[val >> 3] = 0;
8319 }
8320 }
8321
8322 if (sec != toc)
8323 used[val >> 3] = 1;
8324 /* For the toc section, we only mark as used if this
8325 entry itself isn't unused. */
8326 else if ((used[rel->r_offset >> 3]
8327 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8328 && !used[val >> 3])
8329 {
8330 /* Do all the relocs again, to catch reference
8331 chains. */
8332 repeat = 1;
8333 used[val >> 3] = 1;
8334 }
8335 }
8336 }
8337 while (repeat);
8338
8339 if (elf_section_data (sec)->relocs != relstart)
8340 free (relstart);
8341 }
8342
8343 /* Merge the used and skip arrays. Assume that TOC
8344 doublewords not appearing as either used or unused belong
8345 to an entry more than one doubleword in size. */
8346 for (drop = skip, keep = used, last = 0, some_unused = 0;
8347 drop < skip + (toc->size + 7) / 8;
8348 ++drop, ++keep)
8349 {
8350 if (*keep)
8351 {
8352 *drop &= ~ref_from_discarded;
8353 if ((*drop & can_optimize) != 0)
8354 some_unused = 1;
8355 last = 0;
8356 }
8357 else if ((*drop & ref_from_discarded) != 0)
8358 {
8359 some_unused = 1;
8360 last = ref_from_discarded;
8361 }
8362 else
8363 *drop = last;
8364 }
8365
8366 free (used);
8367
8368 if (some_unused)
8369 {
8370 bfd_byte *contents, *src;
8371 unsigned long off;
8372 Elf_Internal_Sym *sym;
8373 bfd_boolean local_toc_syms = FALSE;
8374
8375 /* Shuffle the toc contents, and at the same time convert the
8376 skip array from booleans into offsets. */
8377 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8378 goto error_ret;
8379
8380 elf_section_data (toc)->this_hdr.contents = contents;
8381
8382 for (src = contents, off = 0, drop = skip;
8383 src < contents + toc->size;
8384 src += 8, ++drop)
8385 {
8386 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8387 off += 8;
8388 else if (off != 0)
8389 {
8390 *drop = off;
8391 memcpy (src - off, src, 8);
8392 }
8393 }
8394 *drop = off;
8395 toc->rawsize = toc->size;
8396 toc->size = src - contents - off;
8397
8398 /* Adjust addends for relocs against the toc section sym,
8399 and optimize any accesses we can. */
8400 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8401 {
8402 if (sec->reloc_count == 0
8403 || discarded_section (sec))
8404 continue;
8405
8406 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8407 info->keep_memory);
8408 if (relstart == NULL)
8409 goto error_ret;
8410
8411 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8412 {
8413 enum elf_ppc64_reloc_type r_type;
8414 unsigned long r_symndx;
8415 asection *sym_sec;
8416 struct elf_link_hash_entry *h;
8417 bfd_vma val;
8418
8419 r_type = ELF64_R_TYPE (rel->r_info);
8420 switch (r_type)
8421 {
8422 default:
8423 continue;
8424
8425 case R_PPC64_TOC16:
8426 case R_PPC64_TOC16_LO:
8427 case R_PPC64_TOC16_HI:
8428 case R_PPC64_TOC16_HA:
8429 case R_PPC64_TOC16_DS:
8430 case R_PPC64_TOC16_LO_DS:
8431 case R_PPC64_ADDR64:
8432 break;
8433 }
8434
8435 r_symndx = ELF64_R_SYM (rel->r_info);
8436 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8437 r_symndx, ibfd))
8438 goto error_ret;
8439
8440 if (sym_sec != toc)
8441 continue;
8442
8443 if (h != NULL)
8444 val = h->root.u.def.value;
8445 else
8446 {
8447 val = sym->st_value;
8448 if (val != 0)
8449 local_toc_syms = TRUE;
8450 }
8451
8452 val += rel->r_addend;
8453
8454 if (val > toc->rawsize)
8455 val = toc->rawsize;
8456 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8457 continue;
8458 else if ((skip[val >> 3] & can_optimize) != 0)
8459 {
8460 Elf_Internal_Rela *tocrel
8461 = toc_relocs + (skip[val >> 3] >> 2);
8462 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8463
8464 switch (r_type)
8465 {
8466 case R_PPC64_TOC16_HA:
8467 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8468 break;
8469
8470 case R_PPC64_TOC16_LO_DS:
8471 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8472 break;
8473
8474 default:
8475 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8476 ppc_howto_init ();
8477 info->callbacks->einfo
8478 /* xgettext:c-format */
8479 (_("%H: %s references "
8480 "optimized away TOC entry\n"),
8481 ibfd, sec, rel->r_offset,
8482 ppc64_elf_howto_table[r_type]->name);
8483 bfd_set_error (bfd_error_bad_value);
8484 goto error_ret;
8485 }
8486 rel->r_addend = tocrel->r_addend;
8487 elf_section_data (sec)->relocs = relstart;
8488 continue;
8489 }
8490
8491 if (h != NULL || sym->st_value != 0)
8492 continue;
8493
8494 rel->r_addend -= skip[val >> 3];
8495 elf_section_data (sec)->relocs = relstart;
8496 }
8497
8498 if (elf_section_data (sec)->relocs != relstart)
8499 free (relstart);
8500 }
8501
8502 /* We shouldn't have local or global symbols defined in the TOC,
8503 but handle them anyway. */
8504 if (local_syms != NULL)
8505 for (sym = local_syms;
8506 sym < local_syms + symtab_hdr->sh_info;
8507 ++sym)
8508 if (sym->st_value != 0
8509 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8510 {
8511 unsigned long i;
8512
8513 if (sym->st_value > toc->rawsize)
8514 i = toc->rawsize >> 3;
8515 else
8516 i = sym->st_value >> 3;
8517
8518 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8519 {
8520 if (local_toc_syms)
8521 _bfd_error_handler
8522 (_("%s defined on removed toc entry"),
8523 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8524 do
8525 ++i;
8526 while ((skip[i] & (ref_from_discarded | can_optimize)));
8527 sym->st_value = (bfd_vma) i << 3;
8528 }
8529
8530 sym->st_value -= skip[i];
8531 symtab_hdr->contents = (unsigned char *) local_syms;
8532 }
8533
8534 /* Adjust any global syms defined in this toc input section. */
8535 if (toc_inf.global_toc_syms)
8536 {
8537 toc_inf.toc = toc;
8538 toc_inf.skip = skip;
8539 toc_inf.global_toc_syms = FALSE;
8540 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8541 &toc_inf);
8542 }
8543
8544 if (toc->reloc_count != 0)
8545 {
8546 Elf_Internal_Shdr *rel_hdr;
8547 Elf_Internal_Rela *wrel;
8548 bfd_size_type sz;
8549
8550 /* Remove unused toc relocs, and adjust those we keep. */
8551 if (toc_relocs == NULL)
8552 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8553 info->keep_memory);
8554 if (toc_relocs == NULL)
8555 goto error_ret;
8556
8557 wrel = toc_relocs;
8558 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8559 if ((skip[rel->r_offset >> 3]
8560 & (ref_from_discarded | can_optimize)) == 0)
8561 {
8562 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8563 wrel->r_info = rel->r_info;
8564 wrel->r_addend = rel->r_addend;
8565 ++wrel;
8566 }
8567 else if (!dec_dynrel_count (rel->r_info, toc, info,
8568 &local_syms, NULL, NULL))
8569 goto error_ret;
8570
8571 elf_section_data (toc)->relocs = toc_relocs;
8572 toc->reloc_count = wrel - toc_relocs;
8573 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8574 sz = rel_hdr->sh_entsize;
8575 rel_hdr->sh_size = toc->reloc_count * sz;
8576 }
8577 }
8578 else if (toc_relocs != NULL
8579 && elf_section_data (toc)->relocs != toc_relocs)
8580 free (toc_relocs);
8581
8582 if (local_syms != NULL
8583 && symtab_hdr->contents != (unsigned char *) local_syms)
8584 {
8585 if (!info->keep_memory)
8586 free (local_syms);
8587 else
8588 symtab_hdr->contents = (unsigned char *) local_syms;
8589 }
8590 free (skip);
8591 }
8592
8593 /* Look for cases where we can change an indirect GOT access to
8594 a GOT relative access, possibly reducing the number of GOT
8595 entries. */
8596 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8597 {
8598 asection *sec;
8599 Elf_Internal_Shdr *symtab_hdr;
8600 Elf_Internal_Sym *local_syms;
8601 Elf_Internal_Rela *relstart, *rel;
8602 bfd_vma got;
8603
8604 if (!is_ppc64_elf (ibfd))
8605 continue;
8606
8607 if (!ppc64_elf_tdata (ibfd)->has_gotrel)
8608 continue;
8609
8610 sec = ppc64_elf_tdata (ibfd)->got;
8611 got = sec->output_section->vma + sec->output_offset + 0x8000;
8612
8613 local_syms = NULL;
8614 symtab_hdr = &elf_symtab_hdr (ibfd);
8615
8616 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8617 {
8618 if (sec->reloc_count == 0
8619 || !ppc64_elf_section_data (sec)->has_gotrel
8620 || discarded_section (sec))
8621 continue;
8622
8623 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8624 info->keep_memory);
8625 if (relstart == NULL)
8626 {
8627 got_error_ret:
8628 if (local_syms != NULL
8629 && symtab_hdr->contents != (unsigned char *) local_syms)
8630 free (local_syms);
8631 if (sec != NULL
8632 && relstart != NULL
8633 && elf_section_data (sec)->relocs != relstart)
8634 free (relstart);
8635 return FALSE;
8636 }
8637
8638 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8639 {
8640 enum elf_ppc64_reloc_type r_type;
8641 unsigned long r_symndx;
8642 Elf_Internal_Sym *sym;
8643 asection *sym_sec;
8644 struct elf_link_hash_entry *h;
8645 struct got_entry *ent;
8646 bfd_vma val;
8647 unsigned char buf[4];
8648 unsigned int insn;
8649
8650 r_type = ELF64_R_TYPE (rel->r_info);
8651 switch (r_type)
8652 {
8653 default:
8654 continue;
8655
8656 case R_PPC64_GOT16_DS:
8657 case R_PPC64_GOT16_HA:
8658 case R_PPC64_GOT16_LO_DS:
8659 break;
8660 }
8661
8662 r_symndx = ELF64_R_SYM (rel->r_info);
8663 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8664 r_symndx, ibfd))
8665 goto got_error_ret;
8666
8667 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8668 continue;
8669
8670 if (h != NULL)
8671 val = h->root.u.def.value;
8672 else
8673 val = sym->st_value;
8674 val += rel->r_addend;
8675 val += sym_sec->output_section->vma + sym_sec->output_offset;
8676
8677 switch (r_type)
8678 {
8679 default:
8680 continue;
8681
8682 case R_PPC64_GOT16_DS:
8683 if (val - got + 0x8000 >= 0x10000)
8684 continue;
8685 if (!bfd_get_section_contents (ibfd, sec, buf,
8686 rel->r_offset & ~3, 4))
8687 goto got_error_ret;
8688 insn = bfd_get_32 (ibfd, buf);
8689 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
8690 continue;
8691 break;
8692
8693 case R_PPC64_GOT16_HA:
8694 if (val - got + 0x80008000ULL >= 0x100000000ULL)
8695 continue;
8696
8697 if (!bfd_get_section_contents (ibfd, sec, buf,
8698 rel->r_offset & ~3, 4))
8699 goto got_error_ret;
8700 insn = bfd_get_32 (ibfd, buf);
8701 if (((insn & ((0x3f << 26) | 0x1f << 16))
8702 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8703 continue;
8704 break;
8705
8706 case R_PPC64_GOT16_LO_DS:
8707 if (val - got + 0x80008000ULL >= 0x100000000ULL)
8708 continue;
8709 if (!bfd_get_section_contents (ibfd, sec, buf,
8710 rel->r_offset & ~3, 4))
8711 goto got_error_ret;
8712 insn = bfd_get_32 (ibfd, buf);
8713 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
8714 continue;
8715 break;
8716 }
8717
8718 if (h != NULL)
8719 ent = h->got.glist;
8720 else
8721 {
8722 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
8723 ent = local_got_ents[r_symndx];
8724 }
8725 for (; ent != NULL; ent = ent->next)
8726 if (ent->addend == rel->r_addend
8727 && ent->owner == ibfd
8728 && ent->tls_type == 0)
8729 break;
8730 BFD_ASSERT (ent && ent->got.refcount > 0);
8731 ent->got.refcount -= 1;
8732 }
8733
8734 if (elf_section_data (sec)->relocs != relstart)
8735 free (relstart);
8736 }
8737
8738 if (local_syms != NULL
8739 && symtab_hdr->contents != (unsigned char *) local_syms)
8740 {
8741 if (!info->keep_memory)
8742 free (local_syms);
8743 else
8744 symtab_hdr->contents = (unsigned char *) local_syms;
8745 }
8746 }
8747
8748 return TRUE;
8749 }
8750
8751 /* Return true iff input section I references the TOC using
8752 instructions limited to +/-32k offsets. */
8753
8754 bfd_boolean
8755 ppc64_elf_has_small_toc_reloc (asection *i)
8756 {
8757 return (is_ppc64_elf (i->owner)
8758 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8759 }
8760
8761 /* Allocate space for one GOT entry. */
8762
8763 static void
8764 allocate_got (struct elf_link_hash_entry *h,
8765 struct bfd_link_info *info,
8766 struct got_entry *gent)
8767 {
8768 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8769 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8770 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8771 ? 16 : 8);
8772 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8773 ? 2 : 1) * sizeof (Elf64_External_Rela);
8774 asection *got = ppc64_elf_tdata (gent->owner)->got;
8775
8776 gent->got.offset = got->size;
8777 got->size += entsize;
8778
8779 if (h->type == STT_GNU_IFUNC)
8780 {
8781 htab->elf.irelplt->size += rentsize;
8782 htab->got_reli_size += rentsize;
8783 }
8784 else if (((bfd_link_pic (info)
8785 && !((gent->tls_type & TLS_TPREL) != 0
8786 && bfd_link_executable (info)
8787 && SYMBOL_REFERENCES_LOCAL (info, h)))
8788 || (htab->elf.dynamic_sections_created
8789 && h->dynindx != -1
8790 && !SYMBOL_REFERENCES_LOCAL (info, h)))
8791 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8792 {
8793 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8794 relgot->size += rentsize;
8795 }
8796 }
8797
8798 /* This function merges got entries in the same toc group. */
8799
8800 static void
8801 merge_got_entries (struct got_entry **pent)
8802 {
8803 struct got_entry *ent, *ent2;
8804
8805 for (ent = *pent; ent != NULL; ent = ent->next)
8806 if (!ent->is_indirect)
8807 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8808 if (!ent2->is_indirect
8809 && ent2->addend == ent->addend
8810 && ent2->tls_type == ent->tls_type
8811 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8812 {
8813 ent2->is_indirect = TRUE;
8814 ent2->got.ent = ent;
8815 }
8816 }
8817
8818 /* If H is undefined, make it dynamic if that makes sense. */
8819
8820 static bfd_boolean
8821 ensure_undef_dynamic (struct bfd_link_info *info,
8822 struct elf_link_hash_entry *h)
8823 {
8824 struct elf_link_hash_table *htab = elf_hash_table (info);
8825
8826 if (htab->dynamic_sections_created
8827 && ((info->dynamic_undefined_weak != 0
8828 && h->root.type == bfd_link_hash_undefweak)
8829 || h->root.type == bfd_link_hash_undefined)
8830 && h->dynindx == -1
8831 && !h->forced_local
8832 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
8833 return bfd_elf_link_record_dynamic_symbol (info, h);
8834 return TRUE;
8835 }
8836
8837 /* Allocate space in .plt, .got and associated reloc sections for
8838 dynamic relocs. */
8839
8840 static bfd_boolean
8841 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8842 {
8843 struct bfd_link_info *info;
8844 struct ppc_link_hash_table *htab;
8845 asection *s;
8846 struct ppc_link_hash_entry *eh;
8847 struct got_entry **pgent, *gent;
8848
8849 if (h->root.type == bfd_link_hash_indirect)
8850 return TRUE;
8851
8852 info = (struct bfd_link_info *) inf;
8853 htab = ppc_hash_table (info);
8854 if (htab == NULL)
8855 return FALSE;
8856
8857 eh = (struct ppc_link_hash_entry *) h;
8858 /* Run through the TLS GD got entries first if we're changing them
8859 to TPREL. */
8860 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
8861 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8862 if (gent->got.refcount > 0
8863 && (gent->tls_type & TLS_GD) != 0)
8864 {
8865 /* This was a GD entry that has been converted to TPREL. If
8866 there happens to be a TPREL entry we can use that one. */
8867 struct got_entry *ent;
8868 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8869 if (ent->got.refcount > 0
8870 && (ent->tls_type & TLS_TPREL) != 0
8871 && ent->addend == gent->addend
8872 && ent->owner == gent->owner)
8873 {
8874 gent->got.refcount = 0;
8875 break;
8876 }
8877
8878 /* If not, then we'll be using our own TPREL entry. */
8879 if (gent->got.refcount != 0)
8880 gent->tls_type = TLS_TLS | TLS_TPREL;
8881 }
8882
8883 /* Remove any list entry that won't generate a word in the GOT before
8884 we call merge_got_entries. Otherwise we risk merging to empty
8885 entries. */
8886 pgent = &h->got.glist;
8887 while ((gent = *pgent) != NULL)
8888 if (gent->got.refcount > 0)
8889 {
8890 if ((gent->tls_type & TLS_LD) != 0
8891 && !h->def_dynamic)
8892 {
8893 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8894 *pgent = gent->next;
8895 }
8896 else
8897 pgent = &gent->next;
8898 }
8899 else
8900 *pgent = gent->next;
8901
8902 if (!htab->do_multi_toc)
8903 merge_got_entries (&h->got.glist);
8904
8905 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8906 if (!gent->is_indirect)
8907 {
8908 /* Make sure this symbol is output as a dynamic symbol. */
8909 if (!ensure_undef_dynamic (info, h))
8910 return FALSE;
8911
8912 if (!is_ppc64_elf (gent->owner))
8913 abort ();
8914
8915 allocate_got (h, info, gent);
8916 }
8917
8918 /* If no dynamic sections we can't have dynamic relocs, except for
8919 IFUNCs which are handled even in static executables. */
8920 if (!htab->elf.dynamic_sections_created
8921 && h->type != STT_GNU_IFUNC)
8922 eh->dyn_relocs = NULL;
8923
8924 /* Discard relocs on undefined symbols that must be local. */
8925 else if (h->root.type == bfd_link_hash_undefined
8926 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8927 eh->dyn_relocs = NULL;
8928
8929 /* Also discard relocs on undefined weak syms with non-default
8930 visibility, or when dynamic_undefined_weak says so. */
8931 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8932 eh->dyn_relocs = NULL;
8933
8934 if (eh->dyn_relocs != NULL)
8935 {
8936 struct elf_dyn_relocs *p, **pp;
8937
8938 /* In the shared -Bsymbolic case, discard space allocated for
8939 dynamic pc-relative relocs against symbols which turn out to
8940 be defined in regular objects. For the normal shared case,
8941 discard space for relocs that have become local due to symbol
8942 visibility changes. */
8943
8944 if (bfd_link_pic (info))
8945 {
8946 /* Relocs that use pc_count are those that appear on a call
8947 insn, or certain REL relocs (see must_be_dyn_reloc) that
8948 can be generated via assembly. We want calls to
8949 protected symbols to resolve directly to the function
8950 rather than going via the plt. If people want function
8951 pointer comparisons to work as expected then they should
8952 avoid writing weird assembly. */
8953 if (SYMBOL_CALLS_LOCAL (info, h))
8954 {
8955 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8956 {
8957 p->count -= p->pc_count;
8958 p->pc_count = 0;
8959 if (p->count == 0)
8960 *pp = p->next;
8961 else
8962 pp = &p->next;
8963 }
8964 }
8965
8966 if (eh->dyn_relocs != NULL)
8967 {
8968 /* Make sure this symbol is output as a dynamic symbol. */
8969 if (!ensure_undef_dynamic (info, h))
8970 return FALSE;
8971 }
8972 }
8973 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
8974 {
8975 /* For the non-pic case, discard space for relocs against
8976 symbols which turn out to need copy relocs or are not
8977 dynamic. */
8978 if (h->dynamic_adjusted
8979 && !h->def_regular
8980 && !ELF_COMMON_DEF_P (h))
8981 {
8982 /* Make sure this symbol is output as a dynamic symbol. */
8983 if (!ensure_undef_dynamic (info, h))
8984 return FALSE;
8985
8986 if (h->dynindx == -1)
8987 eh->dyn_relocs = NULL;
8988 }
8989 else
8990 eh->dyn_relocs = NULL;
8991 }
8992
8993 /* Finally, allocate space. */
8994 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8995 {
8996 asection *sreloc = elf_section_data (p->sec)->sreloc;
8997 if (eh->elf.type == STT_GNU_IFUNC)
8998 sreloc = htab->elf.irelplt;
8999 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9000 }
9001 }
9002
9003 /* We might need a PLT entry when the symbol
9004 a) is dynamic, or
9005 b) is an ifunc, or
9006 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9007 d) has plt16 relocs and we are linking statically. */
9008 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9009 || h->type == STT_GNU_IFUNC
9010 || (h->needs_plt && h->dynamic_adjusted)
9011 || (h->needs_plt
9012 && h->def_regular
9013 && !htab->elf.dynamic_sections_created
9014 && !htab->can_convert_all_inline_plt
9015 && (((struct ppc_link_hash_entry *) h)->tls_mask
9016 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
9017 {
9018 struct plt_entry *pent;
9019 bfd_boolean doneone = FALSE;
9020 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9021 if (pent->plt.refcount > 0)
9022 {
9023 if (!htab->elf.dynamic_sections_created
9024 || h->dynindx == -1)
9025 {
9026 if (h->type == STT_GNU_IFUNC)
9027 {
9028 s = htab->elf.iplt;
9029 pent->plt.offset = s->size;
9030 s->size += PLT_ENTRY_SIZE (htab);
9031 s = htab->elf.irelplt;
9032 }
9033 else
9034 {
9035 s = htab->pltlocal;
9036 pent->plt.offset = s->size;
9037 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9038 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9039 }
9040 }
9041 else
9042 {
9043 /* If this is the first .plt entry, make room for the special
9044 first entry. */
9045 s = htab->elf.splt;
9046 if (s->size == 0)
9047 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9048
9049 pent->plt.offset = s->size;
9050
9051 /* Make room for this entry. */
9052 s->size += PLT_ENTRY_SIZE (htab);
9053
9054 /* Make room for the .glink code. */
9055 s = htab->glink;
9056 if (s->size == 0)
9057 s->size += GLINK_PLTRESOLVE_SIZE (htab);
9058 if (htab->opd_abi)
9059 {
9060 /* We need bigger stubs past index 32767. */
9061 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9062 s->size += 4;
9063 s->size += 2*4;
9064 }
9065 else
9066 s->size += 4;
9067
9068 /* We also need to make an entry in the .rela.plt section. */
9069 s = htab->elf.srelplt;
9070 }
9071 if (s != NULL)
9072 s->size += sizeof (Elf64_External_Rela);
9073 doneone = TRUE;
9074 }
9075 else
9076 pent->plt.offset = (bfd_vma) -1;
9077 if (!doneone)
9078 {
9079 h->plt.plist = NULL;
9080 h->needs_plt = 0;
9081 }
9082 }
9083 else
9084 {
9085 h->plt.plist = NULL;
9086 h->needs_plt = 0;
9087 }
9088
9089 return TRUE;
9090 }
9091
9092 #define PPC_LO(v) ((v) & 0xffff)
9093 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9094 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9095
9096 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9097 to set up space for global entry stubs. These are put in glink,
9098 after the branch table. */
9099
9100 static bfd_boolean
9101 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9102 {
9103 struct bfd_link_info *info;
9104 struct ppc_link_hash_table *htab;
9105 struct plt_entry *pent;
9106 asection *s, *plt;
9107
9108 if (h->root.type == bfd_link_hash_indirect)
9109 return TRUE;
9110
9111 if (!h->pointer_equality_needed)
9112 return TRUE;
9113
9114 if (h->def_regular)
9115 return TRUE;
9116
9117 info = inf;
9118 htab = ppc_hash_table (info);
9119 if (htab == NULL)
9120 return FALSE;
9121
9122 s = htab->global_entry;
9123 plt = htab->elf.splt;
9124 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9125 if (pent->plt.offset != (bfd_vma) -1
9126 && pent->addend == 0)
9127 {
9128 /* For ELFv2, if this symbol is not defined in a regular file
9129 and we are not generating a shared library or pie, then we
9130 need to define the symbol in the executable on a call stub.
9131 This is to avoid text relocations. */
9132 bfd_vma off, stub_align, stub_off, stub_size;
9133 unsigned int align_power;
9134
9135 stub_size = 16;
9136 stub_off = s->size;
9137 if (htab->params->plt_stub_align >= 0)
9138 align_power = htab->params->plt_stub_align;
9139 else
9140 align_power = -htab->params->plt_stub_align;
9141 /* Setting section alignment is delayed until we know it is
9142 non-empty. Otherwise the .text output section will be
9143 aligned at least to plt_stub_align even when no global
9144 entry stubs are needed. */
9145 if (s->alignment_power < align_power)
9146 s->alignment_power = align_power;
9147 stub_align = (bfd_vma) 1 << align_power;
9148 if (htab->params->plt_stub_align >= 0
9149 || ((((stub_off + stub_size - 1) & -stub_align)
9150 - (stub_off & -stub_align))
9151 > ((stub_size - 1) & -stub_align)))
9152 stub_off = (stub_off + stub_align - 1) & -stub_align;
9153 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9154 off -= stub_off + s->output_offset + s->output_section->vma;
9155 /* Note that for --plt-stub-align negative we have a possible
9156 dependency between stub offset and size. Break that
9157 dependency by assuming the max stub size when calculating
9158 the stub offset. */
9159 if (PPC_HA (off) == 0)
9160 stub_size -= 4;
9161 h->root.type = bfd_link_hash_defined;
9162 h->root.u.def.section = s;
9163 h->root.u.def.value = stub_off;
9164 s->size = stub_off + stub_size;
9165 break;
9166 }
9167 return TRUE;
9168 }
9169
9170 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9171 read-only sections. */
9172
9173 static bfd_boolean
9174 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
9175 {
9176 asection *sec;
9177
9178 if (h->root.type == bfd_link_hash_indirect)
9179 return TRUE;
9180
9181 sec = readonly_dynrelocs (h);
9182 if (sec != NULL)
9183 {
9184 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9185
9186 info->flags |= DF_TEXTREL;
9187 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9188 " in read-only section `%pA'\n"),
9189 sec->owner, h->root.root.string, sec);
9190
9191 /* Not an error, just cut short the traversal. */
9192 return FALSE;
9193 }
9194 return TRUE;
9195 }
9196
9197 /* Set the sizes of the dynamic sections. */
9198
9199 static bfd_boolean
9200 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9201 struct bfd_link_info *info)
9202 {
9203 struct ppc_link_hash_table *htab;
9204 bfd *dynobj;
9205 asection *s;
9206 bfd_boolean relocs;
9207 bfd *ibfd;
9208 struct got_entry *first_tlsld;
9209
9210 htab = ppc_hash_table (info);
9211 if (htab == NULL)
9212 return FALSE;
9213
9214 dynobj = htab->elf.dynobj;
9215 if (dynobj == NULL)
9216 abort ();
9217
9218 if (htab->elf.dynamic_sections_created)
9219 {
9220 /* Set the contents of the .interp section to the interpreter. */
9221 if (bfd_link_executable (info) && !info->nointerp)
9222 {
9223 s = bfd_get_linker_section (dynobj, ".interp");
9224 if (s == NULL)
9225 abort ();
9226 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9227 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9228 }
9229 }
9230
9231 /* Set up .got offsets for local syms, and space for local dynamic
9232 relocs. */
9233 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9234 {
9235 struct got_entry **lgot_ents;
9236 struct got_entry **end_lgot_ents;
9237 struct plt_entry **local_plt;
9238 struct plt_entry **end_local_plt;
9239 unsigned char *lgot_masks;
9240 bfd_size_type locsymcount;
9241 Elf_Internal_Shdr *symtab_hdr;
9242
9243 if (!is_ppc64_elf (ibfd))
9244 continue;
9245
9246 for (s = ibfd->sections; s != NULL; s = s->next)
9247 {
9248 struct ppc_dyn_relocs *p;
9249
9250 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9251 {
9252 if (!bfd_is_abs_section (p->sec)
9253 && bfd_is_abs_section (p->sec->output_section))
9254 {
9255 /* Input section has been discarded, either because
9256 it is a copy of a linkonce section or due to
9257 linker script /DISCARD/, so we'll be discarding
9258 the relocs too. */
9259 }
9260 else if (p->count != 0)
9261 {
9262 asection *srel = elf_section_data (p->sec)->sreloc;
9263 if (p->ifunc)
9264 srel = htab->elf.irelplt;
9265 srel->size += p->count * sizeof (Elf64_External_Rela);
9266 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9267 info->flags |= DF_TEXTREL;
9268 }
9269 }
9270 }
9271
9272 lgot_ents = elf_local_got_ents (ibfd);
9273 if (!lgot_ents)
9274 continue;
9275
9276 symtab_hdr = &elf_symtab_hdr (ibfd);
9277 locsymcount = symtab_hdr->sh_info;
9278 end_lgot_ents = lgot_ents + locsymcount;
9279 local_plt = (struct plt_entry **) end_lgot_ents;
9280 end_local_plt = local_plt + locsymcount;
9281 lgot_masks = (unsigned char *) end_local_plt;
9282 s = ppc64_elf_tdata (ibfd)->got;
9283 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9284 {
9285 struct got_entry **pent, *ent;
9286
9287 pent = lgot_ents;
9288 while ((ent = *pent) != NULL)
9289 if (ent->got.refcount > 0)
9290 {
9291 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9292 {
9293 ppc64_tlsld_got (ibfd)->got.refcount += 1;
9294 *pent = ent->next;
9295 }
9296 else
9297 {
9298 unsigned int ent_size = 8;
9299 unsigned int rel_size = sizeof (Elf64_External_Rela);
9300
9301 ent->got.offset = s->size;
9302 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9303 {
9304 ent_size *= 2;
9305 rel_size *= 2;
9306 }
9307 s->size += ent_size;
9308 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9309 {
9310 htab->elf.irelplt->size += rel_size;
9311 htab->got_reli_size += rel_size;
9312 }
9313 else if (bfd_link_pic (info)
9314 && !((ent->tls_type & TLS_TPREL) != 0
9315 && bfd_link_executable (info)))
9316 {
9317 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9318 srel->size += rel_size;
9319 }
9320 pent = &ent->next;
9321 }
9322 }
9323 else
9324 *pent = ent->next;
9325 }
9326
9327 /* Allocate space for plt calls to local syms. */
9328 lgot_masks = (unsigned char *) end_local_plt;
9329 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
9330 {
9331 struct plt_entry *ent;
9332
9333 for (ent = *local_plt; ent != NULL; ent = ent->next)
9334 if (ent->plt.refcount > 0)
9335 {
9336 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9337 {
9338 s = htab->elf.iplt;
9339 ent->plt.offset = s->size;
9340 s->size += PLT_ENTRY_SIZE (htab);
9341 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9342 }
9343 else if (htab->can_convert_all_inline_plt
9344 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
9345 ent->plt.offset = (bfd_vma) -1;
9346 else
9347 {
9348 s = htab->pltlocal;
9349 ent->plt.offset = s->size;
9350 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9351 if (bfd_link_pic (info))
9352 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9353 }
9354 }
9355 else
9356 ent->plt.offset = (bfd_vma) -1;
9357 }
9358 }
9359
9360 /* Allocate global sym .plt and .got entries, and space for global
9361 sym dynamic relocs. */
9362 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9363
9364 if (!htab->opd_abi && !bfd_link_pic (info))
9365 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9366
9367 first_tlsld = NULL;
9368 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9369 {
9370 struct got_entry *ent;
9371
9372 if (!is_ppc64_elf (ibfd))
9373 continue;
9374
9375 ent = ppc64_tlsld_got (ibfd);
9376 if (ent->got.refcount > 0)
9377 {
9378 if (!htab->do_multi_toc && first_tlsld != NULL)
9379 {
9380 ent->is_indirect = TRUE;
9381 ent->got.ent = first_tlsld;
9382 }
9383 else
9384 {
9385 if (first_tlsld == NULL)
9386 first_tlsld = ent;
9387 s = ppc64_elf_tdata (ibfd)->got;
9388 ent->got.offset = s->size;
9389 ent->owner = ibfd;
9390 s->size += 16;
9391 if (bfd_link_pic (info))
9392 {
9393 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9394 srel->size += sizeof (Elf64_External_Rela);
9395 }
9396 }
9397 }
9398 else
9399 ent->got.offset = (bfd_vma) -1;
9400 }
9401
9402 /* We now have determined the sizes of the various dynamic sections.
9403 Allocate memory for them. */
9404 relocs = FALSE;
9405 for (s = dynobj->sections; s != NULL; s = s->next)
9406 {
9407 if ((s->flags & SEC_LINKER_CREATED) == 0)
9408 continue;
9409
9410 if (s == htab->brlt || s == htab->relbrlt)
9411 /* These haven't been allocated yet; don't strip. */
9412 continue;
9413 else if (s == htab->elf.sgot
9414 || s == htab->elf.splt
9415 || s == htab->elf.iplt
9416 || s == htab->pltlocal
9417 || s == htab->glink
9418 || s == htab->global_entry
9419 || s == htab->elf.sdynbss
9420 || s == htab->elf.sdynrelro)
9421 {
9422 /* Strip this section if we don't need it; see the
9423 comment below. */
9424 }
9425 else if (s == htab->glink_eh_frame)
9426 {
9427 if (!bfd_is_abs_section (s->output_section))
9428 /* Not sized yet. */
9429 continue;
9430 }
9431 else if (CONST_STRNEQ (s->name, ".rela"))
9432 {
9433 if (s->size != 0)
9434 {
9435 if (s != htab->elf.srelplt)
9436 relocs = TRUE;
9437
9438 /* We use the reloc_count field as a counter if we need
9439 to copy relocs into the output file. */
9440 s->reloc_count = 0;
9441 }
9442 }
9443 else
9444 {
9445 /* It's not one of our sections, so don't allocate space. */
9446 continue;
9447 }
9448
9449 if (s->size == 0)
9450 {
9451 /* If we don't need this section, strip it from the
9452 output file. This is mostly to handle .rela.bss and
9453 .rela.plt. We must create both sections in
9454 create_dynamic_sections, because they must be created
9455 before the linker maps input sections to output
9456 sections. The linker does that before
9457 adjust_dynamic_symbol is called, and it is that
9458 function which decides whether anything needs to go
9459 into these sections. */
9460 s->flags |= SEC_EXCLUDE;
9461 continue;
9462 }
9463
9464 if (bfd_is_abs_section (s->output_section))
9465 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9466 s->name);
9467
9468 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9469 continue;
9470
9471 /* Allocate memory for the section contents. We use bfd_zalloc
9472 here in case unused entries are not reclaimed before the
9473 section's contents are written out. This should not happen,
9474 but this way if it does we get a R_PPC64_NONE reloc in .rela
9475 sections instead of garbage.
9476 We also rely on the section contents being zero when writing
9477 the GOT and .dynrelro. */
9478 s->contents = bfd_zalloc (dynobj, s->size);
9479 if (s->contents == NULL)
9480 return FALSE;
9481 }
9482
9483 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9484 {
9485 if (!is_ppc64_elf (ibfd))
9486 continue;
9487
9488 s = ppc64_elf_tdata (ibfd)->got;
9489 if (s != NULL && s != htab->elf.sgot)
9490 {
9491 if (s->size == 0)
9492 s->flags |= SEC_EXCLUDE;
9493 else
9494 {
9495 s->contents = bfd_zalloc (ibfd, s->size);
9496 if (s->contents == NULL)
9497 return FALSE;
9498 }
9499 }
9500 s = ppc64_elf_tdata (ibfd)->relgot;
9501 if (s != NULL)
9502 {
9503 if (s->size == 0)
9504 s->flags |= SEC_EXCLUDE;
9505 else
9506 {
9507 s->contents = bfd_zalloc (ibfd, s->size);
9508 if (s->contents == NULL)
9509 return FALSE;
9510 relocs = TRUE;
9511 s->reloc_count = 0;
9512 }
9513 }
9514 }
9515
9516 if (htab->elf.dynamic_sections_created)
9517 {
9518 bfd_boolean tls_opt;
9519
9520 /* Add some entries to the .dynamic section. We fill in the
9521 values later, in ppc64_elf_finish_dynamic_sections, but we
9522 must add the entries now so that we get the correct size for
9523 the .dynamic section. The DT_DEBUG entry is filled in by the
9524 dynamic linker and used by the debugger. */
9525 #define add_dynamic_entry(TAG, VAL) \
9526 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9527
9528 if (bfd_link_executable (info))
9529 {
9530 if (!add_dynamic_entry (DT_DEBUG, 0))
9531 return FALSE;
9532 }
9533
9534 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
9535 {
9536 if (!add_dynamic_entry (DT_PLTGOT, 0)
9537 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9538 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9539 || !add_dynamic_entry (DT_JMPREL, 0)
9540 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9541 return FALSE;
9542 }
9543
9544 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
9545 {
9546 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9547 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9548 return FALSE;
9549 }
9550
9551 tls_opt = (htab->params->tls_get_addr_opt
9552 && htab->tls_get_addr_fd != NULL
9553 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9554 if (tls_opt || !htab->opd_abi)
9555 {
9556 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9557 return FALSE;
9558 }
9559
9560 if (relocs)
9561 {
9562 if (!add_dynamic_entry (DT_RELA, 0)
9563 || !add_dynamic_entry (DT_RELASZ, 0)
9564 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9565 return FALSE;
9566
9567 /* If any dynamic relocs apply to a read-only section,
9568 then we need a DT_TEXTREL entry. */
9569 if ((info->flags & DF_TEXTREL) == 0)
9570 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
9571
9572 if ((info->flags & DF_TEXTREL) != 0)
9573 {
9574 if (!add_dynamic_entry (DT_TEXTREL, 0))
9575 return FALSE;
9576 }
9577 }
9578 }
9579 #undef add_dynamic_entry
9580
9581 return TRUE;
9582 }
9583
9584 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
9585
9586 static bfd_boolean
9587 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9588 {
9589 if (h->plt.plist != NULL
9590 && !h->def_regular
9591 && !h->pointer_equality_needed)
9592 return FALSE;
9593
9594 return _bfd_elf_hash_symbol (h);
9595 }
9596
9597 /* Determine the type of stub needed, if any, for a call. */
9598
9599 static inline enum ppc_stub_type
9600 ppc_type_of_stub (asection *input_sec,
9601 const Elf_Internal_Rela *rel,
9602 struct ppc_link_hash_entry **hash,
9603 struct plt_entry **plt_ent,
9604 bfd_vma destination,
9605 unsigned long local_off)
9606 {
9607 struct ppc_link_hash_entry *h = *hash;
9608 bfd_vma location;
9609 bfd_vma branch_offset;
9610 bfd_vma max_branch_offset;
9611 enum elf_ppc64_reloc_type r_type;
9612
9613 if (h != NULL)
9614 {
9615 struct plt_entry *ent;
9616 struct ppc_link_hash_entry *fdh = h;
9617 if (h->oh != NULL
9618 && h->oh->is_func_descriptor)
9619 {
9620 fdh = ppc_follow_link (h->oh);
9621 *hash = fdh;
9622 }
9623
9624 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9625 if (ent->addend == rel->r_addend
9626 && ent->plt.offset != (bfd_vma) -1)
9627 {
9628 *plt_ent = ent;
9629 return ppc_stub_plt_call;
9630 }
9631
9632 /* Here, we know we don't have a plt entry. If we don't have a
9633 either a defined function descriptor or a defined entry symbol
9634 in a regular object file, then it is pointless trying to make
9635 any other type of stub. */
9636 if (!is_static_defined (&fdh->elf)
9637 && !is_static_defined (&h->elf))
9638 return ppc_stub_none;
9639 }
9640 else if (elf_local_got_ents (input_sec->owner) != NULL)
9641 {
9642 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9643 struct plt_entry **local_plt = (struct plt_entry **)
9644 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9645 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9646
9647 if (local_plt[r_symndx] != NULL)
9648 {
9649 struct plt_entry *ent;
9650
9651 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9652 if (ent->addend == rel->r_addend
9653 && ent->plt.offset != (bfd_vma) -1)
9654 {
9655 *plt_ent = ent;
9656 return ppc_stub_plt_call;
9657 }
9658 }
9659 }
9660
9661 /* Determine where the call point is. */
9662 location = (input_sec->output_offset
9663 + input_sec->output_section->vma
9664 + rel->r_offset);
9665
9666 branch_offset = destination - location;
9667 r_type = ELF64_R_TYPE (rel->r_info);
9668
9669 /* Determine if a long branch stub is needed. */
9670 max_branch_offset = 1 << 25;
9671 if (r_type == R_PPC64_REL14
9672 || r_type == R_PPC64_REL14_BRTAKEN
9673 || r_type == R_PPC64_REL14_BRNTAKEN)
9674 max_branch_offset = 1 << 15;
9675
9676 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
9677 /* We need a stub. Figure out whether a long_branch or plt_branch
9678 is needed later. */
9679 return ppc_stub_long_branch;
9680
9681 return ppc_stub_none;
9682 }
9683
9684 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
9685 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
9686 . mflr %r12
9687 . bcl 20,31,1f
9688 .1: mflr %r11
9689 . mtlr %r12
9690 . lis %r12,xxx-1b@highest
9691 . ori %r12,%r12,xxx-1b@higher
9692 . sldi %r12,%r12,32
9693 . oris %r12,%r12,xxx-1b@high
9694 . ori %r12,%r12,xxx-1b@l
9695 . add/ldx %r12,%r11,%r12 */
9696
9697 static bfd_byte *
9698 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
9699 {
9700 bfd_put_32 (abfd, MFLR_R12, p);
9701 p += 4;
9702 bfd_put_32 (abfd, BCL_20_31, p);
9703 p += 4;
9704 bfd_put_32 (abfd, MFLR_R11, p);
9705 p += 4;
9706 bfd_put_32 (abfd, MTLR_R12, p);
9707 p += 4;
9708 if (off + 0x8000 < 0x10000)
9709 {
9710 if (load)
9711 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
9712 else
9713 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
9714 p += 4;
9715 }
9716 else if (off + 0x80008000ULL < 0x100000000ULL)
9717 {
9718 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
9719 p += 4;
9720 if (load)
9721 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
9722 else
9723 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
9724 p += 4;
9725 }
9726 else
9727 {
9728 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9729 {
9730 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
9731 p += 4;
9732 }
9733 else
9734 {
9735 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
9736 p += 4;
9737 if (((off >> 32) & 0xffff) != 0)
9738 {
9739 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
9740 p += 4;
9741 }
9742 }
9743 if (((off >> 32) & 0xffffffffULL) != 0)
9744 {
9745 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
9746 p += 4;
9747 }
9748 if (PPC_HI (off) != 0)
9749 {
9750 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
9751 p += 4;
9752 }
9753 if (PPC_LO (off) != 0)
9754 {
9755 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
9756 p += 4;
9757 }
9758 if (load)
9759 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
9760 else
9761 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
9762 p += 4;
9763 }
9764 return p;
9765 }
9766
9767 static unsigned int
9768 size_offset (bfd_vma off)
9769 {
9770 unsigned int size;
9771 if (off + 0x8000 < 0x10000)
9772 size = 4;
9773 else if (off + 0x80008000ULL < 0x100000000ULL)
9774 size = 8;
9775 else
9776 {
9777 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9778 size = 4;
9779 else
9780 {
9781 size = 4;
9782 if (((off >> 32) & 0xffff) != 0)
9783 size += 4;
9784 }
9785 if (((off >> 32) & 0xffffffffULL) != 0)
9786 size += 4;
9787 if (PPC_HI (off) != 0)
9788 size += 4;
9789 if (PPC_LO (off) != 0)
9790 size += 4;
9791 size += 4;
9792 }
9793 return size + 16;
9794 }
9795
9796 static unsigned int
9797 num_relocs_for_offset (bfd_vma off)
9798 {
9799 unsigned int num_rel;
9800 if (off + 0x8000 < 0x10000)
9801 num_rel = 1;
9802 else if (off + 0x80008000ULL < 0x100000000ULL)
9803 num_rel = 2;
9804 else
9805 {
9806 num_rel = 1;
9807 if (off + 0x800000000000ULL >= 0x1000000000000ULL
9808 && ((off >> 32) & 0xffff) != 0)
9809 num_rel += 1;
9810 if (PPC_HI (off) != 0)
9811 num_rel += 1;
9812 if (PPC_LO (off) != 0)
9813 num_rel += 1;
9814 }
9815 return num_rel;
9816 }
9817
9818 static Elf_Internal_Rela *
9819 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
9820 bfd_vma roff, bfd_vma targ, bfd_vma off)
9821 {
9822 bfd_vma relative_targ = targ - (roff - 8);
9823 if (bfd_big_endian (info->output_bfd))
9824 roff += 2;
9825 r->r_offset = roff;
9826 r->r_addend = relative_targ + roff;
9827 if (off + 0x8000 < 0x10000)
9828 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
9829 else if (off + 0x80008000ULL < 0x100000000ULL)
9830 {
9831 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
9832 ++r;
9833 roff += 4;
9834 r->r_offset = roff;
9835 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
9836 r->r_addend = relative_targ + roff;
9837 }
9838 else
9839 {
9840 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9841 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
9842 else
9843 {
9844 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
9845 if (((off >> 32) & 0xffff) != 0)
9846 {
9847 ++r;
9848 roff += 4;
9849 r->r_offset = roff;
9850 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
9851 r->r_addend = relative_targ + roff;
9852 }
9853 }
9854 if (((off >> 32) & 0xffffffffULL) != 0)
9855 roff += 4;
9856 if (PPC_HI (off) != 0)
9857 {
9858 ++r;
9859 roff += 4;
9860 r->r_offset = roff;
9861 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
9862 r->r_addend = relative_targ + roff;
9863 }
9864 if (PPC_LO (off) != 0)
9865 {
9866 ++r;
9867 roff += 4;
9868 r->r_offset = roff;
9869 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
9870 r->r_addend = relative_targ + roff;
9871 }
9872 }
9873 return r;
9874 }
9875
9876 /* Emit .eh_frame opcode to advance pc by DELTA. */
9877
9878 static bfd_byte *
9879 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
9880 {
9881 delta /= 4;
9882 if (delta < 64)
9883 *eh++ = DW_CFA_advance_loc + delta;
9884 else if (delta < 256)
9885 {
9886 *eh++ = DW_CFA_advance_loc1;
9887 *eh++ = delta;
9888 }
9889 else if (delta < 65536)
9890 {
9891 *eh++ = DW_CFA_advance_loc2;
9892 bfd_put_16 (abfd, delta, eh);
9893 eh += 2;
9894 }
9895 else
9896 {
9897 *eh++ = DW_CFA_advance_loc4;
9898 bfd_put_32 (abfd, delta, eh);
9899 eh += 4;
9900 }
9901 return eh;
9902 }
9903
9904 /* Size of required .eh_frame opcode to advance pc by DELTA. */
9905
9906 static unsigned int
9907 eh_advance_size (unsigned int delta)
9908 {
9909 if (delta < 64 * 4)
9910 /* DW_CFA_advance_loc+[1..63]. */
9911 return 1;
9912 if (delta < 256 * 4)
9913 /* DW_CFA_advance_loc1, byte. */
9914 return 2;
9915 if (delta < 65536 * 4)
9916 /* DW_CFA_advance_loc2, 2 bytes. */
9917 return 3;
9918 /* DW_CFA_advance_loc4, 4 bytes. */
9919 return 5;
9920 }
9921
9922 /* With power7 weakly ordered memory model, it is possible for ld.so
9923 to update a plt entry in one thread and have another thread see a
9924 stale zero toc entry. To avoid this we need some sort of acquire
9925 barrier in the call stub. One solution is to make the load of the
9926 toc word seem to appear to depend on the load of the function entry
9927 word. Another solution is to test for r2 being zero, and branch to
9928 the appropriate glink entry if so.
9929
9930 . fake dep barrier compare
9931 . ld 12,xxx(2) ld 12,xxx(2)
9932 . mtctr 12 mtctr 12
9933 . xor 11,12,12 ld 2,xxx+8(2)
9934 . add 2,2,11 cmpldi 2,0
9935 . ld 2,xxx+8(2) bnectr+
9936 . bctr b <glink_entry>
9937
9938 The solution involving the compare turns out to be faster, so
9939 that's what we use unless the branch won't reach. */
9940
9941 #define ALWAYS_USE_FAKE_DEP 0
9942 #define ALWAYS_EMIT_R2SAVE 0
9943
9944 static inline unsigned int
9945 plt_stub_size (struct ppc_link_hash_table *htab,
9946 struct ppc_stub_hash_entry *stub_entry,
9947 bfd_vma off)
9948 {
9949 unsigned size;
9950
9951 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
9952 {
9953 size = 8 + size_offset (off - 8);
9954 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
9955 size += 4;
9956 return size;
9957 }
9958
9959 size = 12;
9960 if (ALWAYS_EMIT_R2SAVE
9961 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9962 size += 4;
9963 if (PPC_HA (off) != 0)
9964 size += 4;
9965 if (htab->opd_abi)
9966 {
9967 size += 4;
9968 if (htab->params->plt_static_chain)
9969 size += 4;
9970 if (htab->params->plt_thread_safe
9971 && htab->elf.dynamic_sections_created
9972 && stub_entry->h != NULL
9973 && stub_entry->h->elf.dynindx != -1)
9974 size += 8;
9975 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
9976 size += 4;
9977 }
9978 if (stub_entry->h != NULL
9979 && (stub_entry->h == htab->tls_get_addr_fd
9980 || stub_entry->h == htab->tls_get_addr)
9981 && htab->params->tls_get_addr_opt)
9982 {
9983 size += 7 * 4;
9984 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
9985 size += 6 * 4;
9986 }
9987 return size;
9988 }
9989
9990 /* Depending on the sign of plt_stub_align:
9991 If positive, return the padding to align to a 2**plt_stub_align
9992 boundary.
9993 If negative, if this stub would cross fewer 2**plt_stub_align
9994 boundaries if we align, then return the padding needed to do so. */
9995
9996 static inline unsigned int
9997 plt_stub_pad (struct ppc_link_hash_table *htab,
9998 struct ppc_stub_hash_entry *stub_entry,
9999 bfd_vma plt_off)
10000 {
10001 int stub_align;
10002 unsigned stub_size;
10003 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10004
10005 if (htab->params->plt_stub_align >= 0)
10006 {
10007 stub_align = 1 << htab->params->plt_stub_align;
10008 if ((stub_off & (stub_align - 1)) != 0)
10009 return stub_align - (stub_off & (stub_align - 1));
10010 return 0;
10011 }
10012
10013 stub_align = 1 << -htab->params->plt_stub_align;
10014 stub_size = plt_stub_size (htab, stub_entry, plt_off);
10015 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10016 > ((stub_size - 1) & -stub_align))
10017 return stub_align - (stub_off & (stub_align - 1));
10018 return 0;
10019 }
10020
10021 /* Build a .plt call stub. */
10022
10023 static inline bfd_byte *
10024 build_plt_stub (struct ppc_link_hash_table *htab,
10025 struct ppc_stub_hash_entry *stub_entry,
10026 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10027 {
10028 bfd *obfd = htab->params->stub_bfd;
10029 bfd_boolean plt_load_toc = htab->opd_abi;
10030 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10031 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10032 && htab->elf.dynamic_sections_created
10033 && stub_entry->h != NULL
10034 && stub_entry->h->elf.dynindx != -1);
10035 bfd_boolean use_fake_dep = plt_thread_safe;
10036 bfd_vma cmp_branch_off = 0;
10037
10038 if (!ALWAYS_USE_FAKE_DEP
10039 && plt_load_toc
10040 && plt_thread_safe
10041 && !((stub_entry->h == htab->tls_get_addr_fd
10042 || stub_entry->h == htab->tls_get_addr)
10043 && htab->params->tls_get_addr_opt))
10044 {
10045 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10046 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10047 / PLT_ENTRY_SIZE (htab));
10048 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10049 bfd_vma to, from;
10050
10051 if (pltindex > 32768)
10052 glinkoff += (pltindex - 32768) * 4;
10053 to = (glinkoff
10054 + htab->glink->output_offset
10055 + htab->glink->output_section->vma);
10056 from = (p - stub_entry->group->stub_sec->contents
10057 + 4 * (ALWAYS_EMIT_R2SAVE
10058 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10059 + 4 * (PPC_HA (offset) != 0)
10060 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10061 != PPC_HA (offset))
10062 + 4 * (plt_static_chain != 0)
10063 + 20
10064 + stub_entry->group->stub_sec->output_offset
10065 + stub_entry->group->stub_sec->output_section->vma);
10066 cmp_branch_off = to - from;
10067 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10068 }
10069
10070 if (PPC_HA (offset) != 0)
10071 {
10072 if (r != NULL)
10073 {
10074 if (ALWAYS_EMIT_R2SAVE
10075 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10076 r[0].r_offset += 4;
10077 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10078 r[1].r_offset = r[0].r_offset + 4;
10079 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10080 r[1].r_addend = r[0].r_addend;
10081 if (plt_load_toc)
10082 {
10083 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10084 {
10085 r[2].r_offset = r[1].r_offset + 4;
10086 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10087 r[2].r_addend = r[0].r_addend;
10088 }
10089 else
10090 {
10091 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10092 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10093 r[2].r_addend = r[0].r_addend + 8;
10094 if (plt_static_chain)
10095 {
10096 r[3].r_offset = r[2].r_offset + 4;
10097 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10098 r[3].r_addend = r[0].r_addend + 16;
10099 }
10100 }
10101 }
10102 }
10103 if (ALWAYS_EMIT_R2SAVE
10104 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10105 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10106 if (plt_load_toc)
10107 {
10108 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10109 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10110 }
10111 else
10112 {
10113 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10114 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10115 }
10116 if (plt_load_toc
10117 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10118 {
10119 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10120 offset = 0;
10121 }
10122 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10123 if (plt_load_toc)
10124 {
10125 if (use_fake_dep)
10126 {
10127 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10128 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10129 }
10130 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10131 if (plt_static_chain)
10132 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10133 }
10134 }
10135 else
10136 {
10137 if (r != NULL)
10138 {
10139 if (ALWAYS_EMIT_R2SAVE
10140 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10141 r[0].r_offset += 4;
10142 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10143 if (plt_load_toc)
10144 {
10145 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10146 {
10147 r[1].r_offset = r[0].r_offset + 4;
10148 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10149 r[1].r_addend = r[0].r_addend;
10150 }
10151 else
10152 {
10153 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10154 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10155 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10156 if (plt_static_chain)
10157 {
10158 r[2].r_offset = r[1].r_offset + 4;
10159 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10160 r[2].r_addend = r[0].r_addend + 8;
10161 }
10162 }
10163 }
10164 }
10165 if (ALWAYS_EMIT_R2SAVE
10166 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10167 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10168 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10169 if (plt_load_toc
10170 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10171 {
10172 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10173 offset = 0;
10174 }
10175 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10176 if (plt_load_toc)
10177 {
10178 if (use_fake_dep)
10179 {
10180 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10181 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10182 }
10183 if (plt_static_chain)
10184 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10185 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10186 }
10187 }
10188 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10189 {
10190 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10191 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10192 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10193 }
10194 else
10195 bfd_put_32 (obfd, BCTR, p), p += 4;
10196 return p;
10197 }
10198
10199 /* Build a special .plt call stub for __tls_get_addr. */
10200
10201 #define LD_R11_0R3 0xe9630000
10202 #define LD_R12_0R3 0xe9830000
10203 #define MR_R0_R3 0x7c601b78
10204 #define CMPDI_R11_0 0x2c2b0000
10205 #define ADD_R3_R12_R13 0x7c6c6a14
10206 #define BEQLR 0x4d820020
10207 #define MR_R3_R0 0x7c030378
10208 #define STD_R11_0R1 0xf9610000
10209 #define BCTRL 0x4e800421
10210 #define LD_R11_0R1 0xe9610000
10211 #define MTLR_R11 0x7d6803a6
10212
10213 static inline bfd_byte *
10214 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10215 struct ppc_stub_hash_entry *stub_entry,
10216 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10217 {
10218 bfd *obfd = htab->params->stub_bfd;
10219 bfd_byte *loc = p;
10220
10221 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10222 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10223 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10224 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10225 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10226 bfd_put_32 (obfd, BEQLR, p), p += 4;
10227 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10228 if (r != NULL)
10229 r[0].r_offset += 7 * 4;
10230 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
10231 return build_plt_stub (htab, stub_entry, p, offset, r);
10232
10233 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10234 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10235
10236 if (r != NULL)
10237 r[0].r_offset += 2 * 4;
10238 p = build_plt_stub (htab, stub_entry, p, offset, r);
10239 bfd_put_32 (obfd, BCTRL, p - 4);
10240
10241 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10242 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10243 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10244 bfd_put_32 (obfd, BLR, p), p += 4;
10245
10246 if (htab->glink_eh_frame != NULL
10247 && htab->glink_eh_frame->size != 0)
10248 {
10249 bfd_byte *base, *eh;
10250 unsigned int lr_used, delta;
10251
10252 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10253 eh = base + stub_entry->group->eh_size;
10254 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10255 delta = lr_used - stub_entry->group->lr_restore;
10256 stub_entry->group->lr_restore = lr_used + 16;
10257 eh = eh_advance (htab->elf.dynobj, eh, delta);
10258 *eh++ = DW_CFA_offset_extended_sf;
10259 *eh++ = 65;
10260 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10261 *eh++ = DW_CFA_advance_loc + 4;
10262 *eh++ = DW_CFA_restore_extended;
10263 *eh++ = 65;
10264 stub_entry->group->eh_size = eh - base;
10265 }
10266 return p;
10267 }
10268
10269 static Elf_Internal_Rela *
10270 get_relocs (asection *sec, int count)
10271 {
10272 Elf_Internal_Rela *relocs;
10273 struct bfd_elf_section_data *elfsec_data;
10274
10275 elfsec_data = elf_section_data (sec);
10276 relocs = elfsec_data->relocs;
10277 if (relocs == NULL)
10278 {
10279 bfd_size_type relsize;
10280 relsize = sec->reloc_count * sizeof (*relocs);
10281 relocs = bfd_alloc (sec->owner, relsize);
10282 if (relocs == NULL)
10283 return NULL;
10284 elfsec_data->relocs = relocs;
10285 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10286 sizeof (Elf_Internal_Shdr));
10287 if (elfsec_data->rela.hdr == NULL)
10288 return NULL;
10289 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10290 * sizeof (Elf64_External_Rela));
10291 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10292 sec->reloc_count = 0;
10293 }
10294 relocs += sec->reloc_count;
10295 sec->reloc_count += count;
10296 return relocs;
10297 }
10298
10299 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10300 forms, to the equivalent relocs against the global symbol given by
10301 STUB_ENTRY->H. */
10302
10303 static bfd_boolean
10304 use_global_in_relocs (struct ppc_link_hash_table *htab,
10305 struct ppc_stub_hash_entry *stub_entry,
10306 Elf_Internal_Rela *r, unsigned int num_rel)
10307 {
10308 struct elf_link_hash_entry **hashes;
10309 unsigned long symndx;
10310 struct ppc_link_hash_entry *h;
10311 bfd_vma symval;
10312
10313 /* Relocs are always against symbols in their own object file. Fake
10314 up global sym hashes for the stub bfd (which has no symbols). */
10315 hashes = elf_sym_hashes (htab->params->stub_bfd);
10316 if (hashes == NULL)
10317 {
10318 bfd_size_type hsize;
10319
10320 /* When called the first time, stub_globals will contain the
10321 total number of symbols seen during stub sizing. After
10322 allocating, stub_globals is used as an index to fill the
10323 hashes array. */
10324 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10325 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10326 if (hashes == NULL)
10327 return FALSE;
10328 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10329 htab->stub_globals = 1;
10330 }
10331 symndx = htab->stub_globals++;
10332 h = stub_entry->h;
10333 hashes[symndx] = &h->elf;
10334 if (h->oh != NULL && h->oh->is_func)
10335 h = ppc_follow_link (h->oh);
10336 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10337 || h->elf.root.type == bfd_link_hash_defweak);
10338 symval = (h->elf.root.u.def.value
10339 + h->elf.root.u.def.section->output_offset
10340 + h->elf.root.u.def.section->output_section->vma);
10341 while (num_rel-- != 0)
10342 {
10343 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10344 if (h->elf.root.u.def.section != stub_entry->target_section)
10345 {
10346 /* H is an opd symbol. The addend must be zero, and the
10347 branch reloc is the only one we can convert. */
10348 r->r_addend = 0;
10349 break;
10350 }
10351 else
10352 r->r_addend -= symval;
10353 --r;
10354 }
10355 return TRUE;
10356 }
10357
10358 static bfd_vma
10359 get_r2off (struct bfd_link_info *info,
10360 struct ppc_stub_hash_entry *stub_entry)
10361 {
10362 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10363 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10364
10365 if (r2off == 0)
10366 {
10367 /* Support linking -R objects. Get the toc pointer from the
10368 opd entry. */
10369 char buf[8];
10370 if (!htab->opd_abi)
10371 return r2off;
10372 asection *opd = stub_entry->h->elf.root.u.def.section;
10373 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10374
10375 if (strcmp (opd->name, ".opd") != 0
10376 || opd->reloc_count != 0)
10377 {
10378 info->callbacks->einfo
10379 (_("%P: cannot find opd entry toc for `%pT'\n"),
10380 stub_entry->h->elf.root.root.string);
10381 bfd_set_error (bfd_error_bad_value);
10382 return (bfd_vma) -1;
10383 }
10384 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10385 return (bfd_vma) -1;
10386 r2off = bfd_get_64 (opd->owner, buf);
10387 r2off -= elf_gp (info->output_bfd);
10388 }
10389 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10390 return r2off;
10391 }
10392
10393 static bfd_boolean
10394 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10395 {
10396 struct ppc_stub_hash_entry *stub_entry;
10397 struct ppc_branch_hash_entry *br_entry;
10398 struct bfd_link_info *info;
10399 struct ppc_link_hash_table *htab;
10400 bfd_byte *loc;
10401 bfd_byte *p, *relp;
10402 bfd_vma targ, off;
10403 Elf_Internal_Rela *r;
10404 asection *plt;
10405 int num_rel;
10406
10407 /* Massage our args to the form they really have. */
10408 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10409 info = in_arg;
10410
10411 htab = ppc_hash_table (info);
10412 if (htab == NULL)
10413 return FALSE;
10414
10415 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
10416 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10417
10418 htab->stub_count[stub_entry->stub_type - 1] += 1;
10419 switch (stub_entry->stub_type)
10420 {
10421 case ppc_stub_long_branch:
10422 case ppc_stub_long_branch_r2off:
10423 /* Branches are relative. This is where we are going to. */
10424 targ = (stub_entry->target_value
10425 + stub_entry->target_section->output_offset
10426 + stub_entry->target_section->output_section->vma);
10427 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10428
10429 /* And this is where we are coming from. */
10430 off = (stub_entry->stub_offset
10431 + stub_entry->group->stub_sec->output_offset
10432 + stub_entry->group->stub_sec->output_section->vma);
10433 off = targ - off;
10434
10435 p = loc;
10436 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10437 {
10438 bfd_vma r2off = get_r2off (info, stub_entry);
10439
10440 if (r2off == (bfd_vma) -1)
10441 {
10442 htab->stub_error = TRUE;
10443 return FALSE;
10444 }
10445 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10446 p += 4;
10447 if (PPC_HA (r2off) != 0)
10448 {
10449 bfd_put_32 (htab->params->stub_bfd,
10450 ADDIS_R2_R2 | PPC_HA (r2off), p);
10451 p += 4;
10452 }
10453 if (PPC_LO (r2off) != 0)
10454 {
10455 bfd_put_32 (htab->params->stub_bfd,
10456 ADDI_R2_R2 | PPC_LO (r2off), p);
10457 p += 4;
10458 }
10459 off -= p - loc;
10460 }
10461 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10462 p += 4;
10463
10464 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10465 {
10466 _bfd_error_handler
10467 (_("long branch stub `%s' offset overflow"),
10468 stub_entry->root.string);
10469 htab->stub_error = TRUE;
10470 return FALSE;
10471 }
10472
10473 if (info->emitrelocations)
10474 {
10475 r = get_relocs (stub_entry->group->stub_sec, 1);
10476 if (r == NULL)
10477 return FALSE;
10478 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
10479 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10480 r->r_addend = targ;
10481 if (stub_entry->h != NULL
10482 && !use_global_in_relocs (htab, stub_entry, r, 1))
10483 return FALSE;
10484 }
10485 break;
10486
10487 case ppc_stub_plt_branch:
10488 case ppc_stub_plt_branch_r2off:
10489 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10490 stub_entry->root.string + 9,
10491 FALSE, FALSE);
10492 if (br_entry == NULL)
10493 {
10494 _bfd_error_handler (_("can't find branch stub `%s'"),
10495 stub_entry->root.string);
10496 htab->stub_error = TRUE;
10497 return FALSE;
10498 }
10499
10500 targ = (stub_entry->target_value
10501 + stub_entry->target_section->output_offset
10502 + stub_entry->target_section->output_section->vma);
10503 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10504 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10505
10506 bfd_put_64 (htab->brlt->owner, targ,
10507 htab->brlt->contents + br_entry->offset);
10508
10509 if (br_entry->iter == htab->stub_iteration)
10510 {
10511 br_entry->iter = 0;
10512
10513 if (htab->relbrlt != NULL)
10514 {
10515 /* Create a reloc for the branch lookup table entry. */
10516 Elf_Internal_Rela rela;
10517 bfd_byte *rl;
10518
10519 rela.r_offset = (br_entry->offset
10520 + htab->brlt->output_offset
10521 + htab->brlt->output_section->vma);
10522 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10523 rela.r_addend = targ;
10524
10525 rl = htab->relbrlt->contents;
10526 rl += (htab->relbrlt->reloc_count++
10527 * sizeof (Elf64_External_Rela));
10528 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10529 }
10530 else if (info->emitrelocations)
10531 {
10532 r = get_relocs (htab->brlt, 1);
10533 if (r == NULL)
10534 return FALSE;
10535 /* brlt, being SEC_LINKER_CREATED does not go through the
10536 normal reloc processing. Symbols and offsets are not
10537 translated from input file to output file form, so
10538 set up the offset per the output file. */
10539 r->r_offset = (br_entry->offset
10540 + htab->brlt->output_offset
10541 + htab->brlt->output_section->vma);
10542 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10543 r->r_addend = targ;
10544 }
10545 }
10546
10547 targ = (br_entry->offset
10548 + htab->brlt->output_offset
10549 + htab->brlt->output_section->vma);
10550
10551 off = (elf_gp (info->output_bfd)
10552 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10553 off = targ - off;
10554
10555 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10556 {
10557 info->callbacks->einfo
10558 (_("%P: linkage table error against `%pT'\n"),
10559 stub_entry->root.string);
10560 bfd_set_error (bfd_error_bad_value);
10561 htab->stub_error = TRUE;
10562 return FALSE;
10563 }
10564
10565 if (info->emitrelocations)
10566 {
10567 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
10568 if (r == NULL)
10569 return FALSE;
10570 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10571 if (bfd_big_endian (info->output_bfd))
10572 r[0].r_offset += 2;
10573 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10574 r[0].r_offset += 4;
10575 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10576 r[0].r_addend = targ;
10577 if (PPC_HA (off) != 0)
10578 {
10579 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10580 r[1].r_offset = r[0].r_offset + 4;
10581 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10582 r[1].r_addend = r[0].r_addend;
10583 }
10584 }
10585
10586 p = loc;
10587 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10588 {
10589 if (PPC_HA (off) != 0)
10590 {
10591 bfd_put_32 (htab->params->stub_bfd,
10592 ADDIS_R12_R2 | PPC_HA (off), p);
10593 p += 4;
10594 bfd_put_32 (htab->params->stub_bfd,
10595 LD_R12_0R12 | PPC_LO (off), p);
10596 }
10597 else
10598 bfd_put_32 (htab->params->stub_bfd,
10599 LD_R12_0R2 | PPC_LO (off), p);
10600 }
10601 else
10602 {
10603 bfd_vma r2off = get_r2off (info, stub_entry);
10604
10605 if (r2off == (bfd_vma) -1)
10606 {
10607 htab->stub_error = TRUE;
10608 return FALSE;
10609 }
10610
10611 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10612 p += 4;
10613 if (PPC_HA (off) != 0)
10614 {
10615 bfd_put_32 (htab->params->stub_bfd,
10616 ADDIS_R12_R2 | PPC_HA (off), p);
10617 p += 4;
10618 bfd_put_32 (htab->params->stub_bfd,
10619 LD_R12_0R12 | PPC_LO (off), p);
10620 }
10621 else
10622 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
10623
10624 if (PPC_HA (r2off) != 0)
10625 {
10626 p += 4;
10627 bfd_put_32 (htab->params->stub_bfd,
10628 ADDIS_R2_R2 | PPC_HA (r2off), p);
10629 }
10630 if (PPC_LO (r2off) != 0)
10631 {
10632 p += 4;
10633 bfd_put_32 (htab->params->stub_bfd,
10634 ADDI_R2_R2 | PPC_LO (r2off), p);
10635 }
10636 }
10637 p += 4;
10638 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10639 p += 4;
10640 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10641 p += 4;
10642 break;
10643
10644 case ppc_stub_long_branch_notoc:
10645 case ppc_stub_long_branch_both:
10646 case ppc_stub_plt_branch_notoc:
10647 case ppc_stub_plt_branch_both:
10648 case ppc_stub_plt_call_notoc:
10649 case ppc_stub_plt_call_both:
10650 p = loc;
10651 off = (stub_entry->stub_offset
10652 + stub_entry->group->stub_sec->output_offset
10653 + stub_entry->group->stub_sec->output_section->vma);
10654 if (stub_entry->stub_type == ppc_stub_long_branch_both
10655 || stub_entry->stub_type == ppc_stub_plt_branch_both
10656 || stub_entry->stub_type == ppc_stub_plt_call_both)
10657 {
10658 off += 4;
10659 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10660 p += 4;
10661 }
10662 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10663 {
10664 targ = stub_entry->plt_ent->plt.offset & ~1;
10665 if (targ >= (bfd_vma) -2)
10666 abort ();
10667
10668 plt = htab->elf.splt;
10669 if (!htab->elf.dynamic_sections_created
10670 || stub_entry->h == NULL
10671 || stub_entry->h->elf.dynindx == -1)
10672 {
10673 if (stub_entry->symtype == STT_GNU_IFUNC)
10674 plt = htab->elf.iplt;
10675 else
10676 plt = htab->pltlocal;
10677 }
10678 targ += plt->output_offset + plt->output_section->vma;
10679 }
10680 else
10681 targ = (stub_entry->target_value
10682 + stub_entry->target_section->output_offset
10683 + stub_entry->target_section->output_section->vma);
10684 off = targ - off;
10685
10686 relp = p;
10687 num_rel = 0;
10688 /* The notoc stubs calculate their target (either a PLT entry or
10689 the global entry point of a function) relative to the PC
10690 returned by the "bcl" two instructions past the start of the
10691 sequence emitted by build_offset. The offset is therefore 8
10692 less than calculated from the start of the sequence. */
10693 off -= 8;
10694 p = build_offset (htab->params->stub_bfd, p, off,
10695 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
10696 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
10697 {
10698 bfd_vma from;
10699 num_rel = 1;
10700 from = (stub_entry->stub_offset
10701 + stub_entry->group->stub_sec->output_offset
10702 + stub_entry->group->stub_sec->output_section->vma
10703 + (p - loc));
10704 bfd_put_32 (htab->params->stub_bfd,
10705 B_DOT | ((targ - from) & 0x3fffffc), p);
10706 }
10707 else
10708 {
10709 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10710 p += 4;
10711 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10712 }
10713 p += 4;
10714
10715 if (info->emitrelocations)
10716 {
10717 bfd_vma roff;
10718 num_rel += num_relocs_for_offset (off);
10719 r = get_relocs (stub_entry->group->stub_sec, num_rel);
10720 if (r == NULL)
10721 return FALSE;
10722 roff = relp + 16 - stub_entry->group->stub_sec->contents;
10723 r = emit_relocs_for_offset (info, r, roff, targ, off);
10724 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
10725 || stub_entry->stub_type == ppc_stub_long_branch_both)
10726 {
10727 ++r;
10728 roff = p - 4 - stub_entry->group->stub_sec->contents;
10729 r->r_offset = roff;
10730 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10731 r->r_addend = targ;
10732 if (stub_entry->h != NULL
10733 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
10734 return FALSE;
10735 }
10736 }
10737
10738 if (htab->glink_eh_frame != NULL
10739 && htab->glink_eh_frame->size != 0)
10740 {
10741 bfd_byte *base, *eh;
10742 unsigned int lr_used, delta;
10743
10744 base = (htab->glink_eh_frame->contents
10745 + stub_entry->group->eh_base + 17);
10746 eh = base + stub_entry->group->eh_size;
10747 lr_used = stub_entry->stub_offset + 8;
10748 if (stub_entry->stub_type == ppc_stub_long_branch_both
10749 || stub_entry->stub_type == ppc_stub_plt_branch_both
10750 || stub_entry->stub_type == ppc_stub_plt_call_both)
10751 lr_used += 4;
10752 delta = lr_used - stub_entry->group->lr_restore;
10753 stub_entry->group->lr_restore = lr_used + 8;
10754 eh = eh_advance (htab->elf.dynobj, eh, delta);
10755 *eh++ = DW_CFA_register;
10756 *eh++ = 65;
10757 *eh++ = 12;
10758 *eh++ = DW_CFA_advance_loc + 2;
10759 *eh++ = DW_CFA_restore_extended;
10760 *eh++ = 65;
10761 stub_entry->group->eh_size = eh - base;
10762 }
10763 break;
10764
10765 case ppc_stub_plt_call:
10766 case ppc_stub_plt_call_r2save:
10767 if (stub_entry->h != NULL
10768 && stub_entry->h->is_func_descriptor
10769 && stub_entry->h->oh != NULL)
10770 {
10771 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10772
10773 /* If the old-ABI "dot-symbol" is undefined make it weak so
10774 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
10775 if (fh->elf.root.type == bfd_link_hash_undefined
10776 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
10777 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
10778 fh->elf.root.type = bfd_link_hash_undefweak;
10779 }
10780
10781 /* Now build the stub. */
10782 targ = stub_entry->plt_ent->plt.offset & ~1;
10783 if (targ >= (bfd_vma) -2)
10784 abort ();
10785
10786 plt = htab->elf.splt;
10787 if (!htab->elf.dynamic_sections_created
10788 || stub_entry->h == NULL
10789 || stub_entry->h->elf.dynindx == -1)
10790 {
10791 if (stub_entry->symtype == STT_GNU_IFUNC)
10792 plt = htab->elf.iplt;
10793 else
10794 plt = htab->pltlocal;
10795 }
10796 targ += plt->output_offset + plt->output_section->vma;
10797
10798 off = (elf_gp (info->output_bfd)
10799 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10800 off = targ - off;
10801
10802 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10803 {
10804 info->callbacks->einfo
10805 /* xgettext:c-format */
10806 (_("%P: linkage table error against `%pT'\n"),
10807 stub_entry->h != NULL
10808 ? stub_entry->h->elf.root.root.string
10809 : "<local sym>");
10810 bfd_set_error (bfd_error_bad_value);
10811 htab->stub_error = TRUE;
10812 return FALSE;
10813 }
10814
10815 r = NULL;
10816 if (info->emitrelocations)
10817 {
10818 r = get_relocs (stub_entry->group->stub_sec,
10819 ((PPC_HA (off) != 0)
10820 + (htab->opd_abi
10821 ? 2 + (htab->params->plt_static_chain
10822 && PPC_HA (off + 16) == PPC_HA (off))
10823 : 1)));
10824 if (r == NULL)
10825 return FALSE;
10826 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10827 if (bfd_big_endian (info->output_bfd))
10828 r[0].r_offset += 2;
10829 r[0].r_addend = targ;
10830 }
10831 if (stub_entry->h != NULL
10832 && (stub_entry->h == htab->tls_get_addr_fd
10833 || stub_entry->h == htab->tls_get_addr)
10834 && htab->params->tls_get_addr_opt)
10835 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10836 else
10837 p = build_plt_stub (htab, stub_entry, loc, off, r);
10838 break;
10839
10840 case ppc_stub_save_res:
10841 return TRUE;
10842
10843 default:
10844 BFD_FAIL ();
10845 return FALSE;
10846 }
10847
10848 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
10849
10850 if (htab->params->emit_stub_syms)
10851 {
10852 struct elf_link_hash_entry *h;
10853 size_t len1, len2;
10854 char *name;
10855 const char *const stub_str[] = { "long_branch",
10856 "long_branch",
10857 "long_branch",
10858 "long_branch",
10859 "plt_branch",
10860 "plt_branch",
10861 "plt_branch",
10862 "plt_branch",
10863 "plt_call",
10864 "plt_call",
10865 "plt_call",
10866 "plt_call" };
10867
10868 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10869 len2 = strlen (stub_entry->root.string);
10870 name = bfd_malloc (len1 + len2 + 2);
10871 if (name == NULL)
10872 return FALSE;
10873 memcpy (name, stub_entry->root.string, 9);
10874 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10875 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10876 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10877 if (h == NULL)
10878 return FALSE;
10879 if (h->root.type == bfd_link_hash_new)
10880 {
10881 h->root.type = bfd_link_hash_defined;
10882 h->root.u.def.section = stub_entry->group->stub_sec;
10883 h->root.u.def.value = stub_entry->stub_offset;
10884 h->ref_regular = 1;
10885 h->def_regular = 1;
10886 h->ref_regular_nonweak = 1;
10887 h->forced_local = 1;
10888 h->non_elf = 0;
10889 h->root.linker_def = 1;
10890 }
10891 }
10892
10893 return TRUE;
10894 }
10895
10896 /* As above, but don't actually build the stub. Just bump offset so
10897 we know stub section sizes, and select plt_branch stubs where
10898 long_branch stubs won't do. */
10899
10900 static bfd_boolean
10901 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10902 {
10903 struct ppc_stub_hash_entry *stub_entry;
10904 struct bfd_link_info *info;
10905 struct ppc_link_hash_table *htab;
10906 asection *plt;
10907 bfd_vma targ, off, r2off;
10908 unsigned int size, extra, lr_used, delta;
10909
10910 /* Massage our args to the form they really have. */
10911 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10912 info = in_arg;
10913
10914 htab = ppc_hash_table (info);
10915 if (htab == NULL)
10916 return FALSE;
10917
10918 /* Make a note of the offset within the stubs for this entry. */
10919 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10920
10921 if (stub_entry->h != NULL
10922 && stub_entry->h->save_res
10923 && stub_entry->h->elf.root.type == bfd_link_hash_defined
10924 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
10925 {
10926 /* Don't make stubs to out-of-line register save/restore
10927 functions. Instead, emit copies of the functions. */
10928 stub_entry->group->needs_save_res = 1;
10929 stub_entry->stub_type = ppc_stub_save_res;
10930 return TRUE;
10931 }
10932
10933 switch (stub_entry->stub_type)
10934 {
10935 case ppc_stub_plt_branch:
10936 case ppc_stub_plt_branch_r2off:
10937 /* Reset the stub type from the plt branch variant in case we now
10938 can reach with a shorter stub. */
10939 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10940 /* Fall through. */
10941 case ppc_stub_long_branch:
10942 case ppc_stub_long_branch_r2off:
10943 targ = (stub_entry->target_value
10944 + stub_entry->target_section->output_offset
10945 + stub_entry->target_section->output_section->vma);
10946 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10947 off = (stub_entry->stub_offset
10948 + stub_entry->group->stub_sec->output_offset
10949 + stub_entry->group->stub_sec->output_section->vma);
10950
10951 size = 4;
10952 r2off = 0;
10953 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10954 {
10955 r2off = get_r2off (info, stub_entry);
10956 if (r2off == (bfd_vma) -1)
10957 {
10958 htab->stub_error = TRUE;
10959 return FALSE;
10960 }
10961 size = 8;
10962 if (PPC_HA (r2off) != 0)
10963 size += 4;
10964 if (PPC_LO (r2off) != 0)
10965 size += 4;
10966 off += size - 4;
10967 }
10968 off = targ - off;
10969
10970 /* If the branch offset is too big, use a ppc_stub_plt_branch.
10971 Do the same for -R objects without function descriptors. */
10972 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
10973 && r2off == 0
10974 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
10975 || off + (1 << 25) >= (bfd_vma) (1 << 26))
10976 {
10977 struct ppc_branch_hash_entry *br_entry;
10978
10979 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10980 stub_entry->root.string + 9,
10981 TRUE, FALSE);
10982 if (br_entry == NULL)
10983 {
10984 _bfd_error_handler (_("can't build branch stub `%s'"),
10985 stub_entry->root.string);
10986 htab->stub_error = TRUE;
10987 return FALSE;
10988 }
10989
10990 if (br_entry->iter != htab->stub_iteration)
10991 {
10992 br_entry->iter = htab->stub_iteration;
10993 br_entry->offset = htab->brlt->size;
10994 htab->brlt->size += 8;
10995
10996 if (htab->relbrlt != NULL)
10997 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10998 else if (info->emitrelocations)
10999 {
11000 htab->brlt->reloc_count += 1;
11001 htab->brlt->flags |= SEC_RELOC;
11002 }
11003 }
11004
11005 targ = (br_entry->offset
11006 + htab->brlt->output_offset
11007 + htab->brlt->output_section->vma);
11008 off = (elf_gp (info->output_bfd)
11009 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11010 off = targ - off;
11011
11012 if (info->emitrelocations)
11013 {
11014 stub_entry->group->stub_sec->reloc_count
11015 += 1 + (PPC_HA (off) != 0);
11016 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11017 }
11018
11019 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11020 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11021 {
11022 size = 12;
11023 if (PPC_HA (off) != 0)
11024 size = 16;
11025 }
11026 else
11027 {
11028 size = 16;
11029 if (PPC_HA (off) != 0)
11030 size += 4;
11031
11032 if (PPC_HA (r2off) != 0)
11033 size += 4;
11034 if (PPC_LO (r2off) != 0)
11035 size += 4;
11036 }
11037 }
11038 else if (info->emitrelocations)
11039 {
11040 stub_entry->group->stub_sec->reloc_count += 1;
11041 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11042 }
11043 break;
11044
11045 case ppc_stub_plt_branch_notoc:
11046 case ppc_stub_plt_branch_both:
11047 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11048 /* Fall through. */
11049 case ppc_stub_long_branch_notoc:
11050 case ppc_stub_long_branch_both:
11051 off = (stub_entry->stub_offset
11052 + stub_entry->group->stub_sec->output_offset
11053 + stub_entry->group->stub_sec->output_section->vma);
11054 size = 0;
11055 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11056 size = 4;
11057 off += size;
11058 targ = (stub_entry->target_value
11059 + stub_entry->target_section->output_offset
11060 + stub_entry->target_section->output_section->vma);
11061 off = targ - off;
11062
11063 if (info->emitrelocations)
11064 {
11065 stub_entry->group->stub_sec->reloc_count
11066 += num_relocs_for_offset (off);
11067 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11068 }
11069
11070 extra = size_offset (off - 8);
11071 /* Include branch insn plus those in the offset sequence. */
11072 size += 4 + extra;
11073 /* The branch insn is at the end, or "extra" bytes along. So
11074 its offset will be "extra" bytes less that that already
11075 calculated. */
11076 off -= extra;
11077
11078 /* After the bcl, lr has been modified so we need to emit
11079 .eh_frame info saying the return address is in r12. */
11080 lr_used = stub_entry->stub_offset + 8;
11081 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11082 lr_used += 4;
11083 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11084 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11085 DW_CFA_restore_extended 65. */
11086 delta = lr_used - stub_entry->group->lr_restore;
11087 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11088 stub_entry->group->lr_restore = lr_used + 8;
11089
11090 /* If the branch can't reach, use a plt_branch. */
11091 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11092 {
11093 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11094 - ppc_stub_long_branch_notoc);
11095 size += 4;
11096 }
11097 else if (info->emitrelocations)
11098 stub_entry->group->stub_sec->reloc_count +=1;
11099 break;
11100
11101 case ppc_stub_plt_call_notoc:
11102 case ppc_stub_plt_call_both:
11103 off = (stub_entry->stub_offset
11104 + stub_entry->group->stub_sec->output_offset
11105 + stub_entry->group->stub_sec->output_section->vma);
11106 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11107 off += 4;
11108 targ = stub_entry->plt_ent->plt.offset & ~1;
11109 if (targ >= (bfd_vma) -2)
11110 abort ();
11111
11112 plt = htab->elf.splt;
11113 if (!htab->elf.dynamic_sections_created
11114 || stub_entry->h == NULL
11115 || stub_entry->h->elf.dynindx == -1)
11116 {
11117 if (stub_entry->symtype == STT_GNU_IFUNC)
11118 plt = htab->elf.iplt;
11119 else
11120 plt = htab->pltlocal;
11121 }
11122 targ += plt->output_offset + plt->output_section->vma;
11123 off = targ - off;
11124
11125 if (htab->params->plt_stub_align != 0)
11126 {
11127 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11128
11129 stub_entry->group->stub_sec->size += pad;
11130 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11131 off -= pad;
11132 }
11133
11134 if (info->emitrelocations)
11135 {
11136 stub_entry->group->stub_sec->reloc_count
11137 += num_relocs_for_offset (off - 8);
11138 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11139 }
11140
11141 size = plt_stub_size (htab, stub_entry, off);
11142
11143 /* After the bcl, lr has been modified so we need to emit
11144 .eh_frame info saying the return address is in r12. */
11145 lr_used = stub_entry->stub_offset + 8;
11146 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11147 lr_used += 4;
11148 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11149 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11150 DW_CFA_restore_extended 65. */
11151 delta = lr_used - stub_entry->group->lr_restore;
11152 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11153 stub_entry->group->lr_restore = lr_used + 8;
11154 break;
11155
11156 case ppc_stub_plt_call:
11157 case ppc_stub_plt_call_r2save:
11158 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11159 if (targ >= (bfd_vma) -2)
11160 abort ();
11161 plt = htab->elf.splt;
11162 if (!htab->elf.dynamic_sections_created
11163 || stub_entry->h == NULL
11164 || stub_entry->h->elf.dynindx == -1)
11165 {
11166 if (stub_entry->symtype == STT_GNU_IFUNC)
11167 plt = htab->elf.iplt;
11168 else
11169 plt = htab->pltlocal;
11170 }
11171 targ += plt->output_offset + plt->output_section->vma;
11172
11173 off = (elf_gp (info->output_bfd)
11174 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11175 off = targ - off;
11176
11177 if (htab->params->plt_stub_align != 0)
11178 {
11179 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11180
11181 stub_entry->group->stub_sec->size += pad;
11182 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11183 }
11184
11185 if (info->emitrelocations)
11186 {
11187 stub_entry->group->stub_sec->reloc_count
11188 += ((PPC_HA (off) != 0)
11189 + (htab->opd_abi
11190 ? 2 + (htab->params->plt_static_chain
11191 && PPC_HA (off + 16) == PPC_HA (off))
11192 : 1));
11193 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11194 }
11195
11196 size = plt_stub_size (htab, stub_entry, off);
11197
11198 if (stub_entry->h != NULL
11199 && (stub_entry->h == htab->tls_get_addr_fd
11200 || stub_entry->h == htab->tls_get_addr)
11201 && htab->params->tls_get_addr_opt
11202 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11203 {
11204 /* After the bctrl, lr has been modified so we need to
11205 emit .eh_frame info saying the return address is
11206 on the stack. In fact we put the EH info specifying
11207 that the return address is on the stack *at* the
11208 call rather than after it, because the EH info for a
11209 call needs to be specified by that point.
11210 See libgcc/unwind-dw2.c execute_cfa_program. */
11211 lr_used = stub_entry->stub_offset + size - 20;
11212 /* The eh_frame info will consist of a DW_CFA_advance_loc
11213 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11214 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11215 delta = lr_used - stub_entry->group->lr_restore;
11216 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11217 stub_entry->group->lr_restore = size - 4;
11218 }
11219 break;
11220
11221 default:
11222 BFD_FAIL ();
11223 return FALSE;
11224 }
11225
11226 stub_entry->group->stub_sec->size += size;
11227 return TRUE;
11228 }
11229
11230 /* Set up various things so that we can make a list of input sections
11231 for each output section included in the link. Returns -1 on error,
11232 0 when no stubs will be needed, and 1 on success. */
11233
11234 int
11235 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11236 {
11237 unsigned int id;
11238 bfd_size_type amt;
11239 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11240
11241 if (htab == NULL)
11242 return -1;
11243
11244 htab->sec_info_arr_size = _bfd_section_id;
11245 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11246 htab->sec_info = bfd_zmalloc (amt);
11247 if (htab->sec_info == NULL)
11248 return -1;
11249
11250 /* Set toc_off for com, und, abs and ind sections. */
11251 for (id = 0; id < 3; id++)
11252 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11253
11254 return 1;
11255 }
11256
11257 /* Set up for first pass at multitoc partitioning. */
11258
11259 void
11260 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11261 {
11262 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11263
11264 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11265 htab->toc_bfd = NULL;
11266 htab->toc_first_sec = NULL;
11267 }
11268
11269 /* The linker repeatedly calls this function for each TOC input section
11270 and linker generated GOT section. Group input bfds such that the toc
11271 within a group is less than 64k in size. */
11272
11273 bfd_boolean
11274 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11275 {
11276 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11277 bfd_vma addr, off, limit;
11278
11279 if (htab == NULL)
11280 return FALSE;
11281
11282 if (!htab->second_toc_pass)
11283 {
11284 /* Keep track of the first .toc or .got section for this input bfd. */
11285 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11286
11287 if (new_bfd)
11288 {
11289 htab->toc_bfd = isec->owner;
11290 htab->toc_first_sec = isec;
11291 }
11292
11293 addr = isec->output_offset + isec->output_section->vma;
11294 off = addr - htab->toc_curr;
11295 limit = 0x80008000;
11296 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11297 limit = 0x10000;
11298 if (off + isec->size > limit)
11299 {
11300 addr = (htab->toc_first_sec->output_offset
11301 + htab->toc_first_sec->output_section->vma);
11302 htab->toc_curr = addr;
11303 htab->toc_curr &= -TOC_BASE_ALIGN;
11304 }
11305
11306 /* toc_curr is the base address of this toc group. Set elf_gp
11307 for the input section to be the offset relative to the
11308 output toc base plus 0x8000. Making the input elf_gp an
11309 offset allows us to move the toc as a whole without
11310 recalculating input elf_gp. */
11311 off = htab->toc_curr - elf_gp (info->output_bfd);
11312 off += TOC_BASE_OFF;
11313
11314 /* Die if someone uses a linker script that doesn't keep input
11315 file .toc and .got together. */
11316 if (new_bfd
11317 && elf_gp (isec->owner) != 0
11318 && elf_gp (isec->owner) != off)
11319 return FALSE;
11320
11321 elf_gp (isec->owner) = off;
11322 return TRUE;
11323 }
11324
11325 /* During the second pass toc_first_sec points to the start of
11326 a toc group, and toc_curr is used to track the old elf_gp.
11327 We use toc_bfd to ensure we only look at each bfd once. */
11328 if (htab->toc_bfd == isec->owner)
11329 return TRUE;
11330 htab->toc_bfd = isec->owner;
11331
11332 if (htab->toc_first_sec == NULL
11333 || htab->toc_curr != elf_gp (isec->owner))
11334 {
11335 htab->toc_curr = elf_gp (isec->owner);
11336 htab->toc_first_sec = isec;
11337 }
11338 addr = (htab->toc_first_sec->output_offset
11339 + htab->toc_first_sec->output_section->vma);
11340 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11341 elf_gp (isec->owner) = off;
11342
11343 return TRUE;
11344 }
11345
11346 /* Called via elf_link_hash_traverse to merge GOT entries for global
11347 symbol H. */
11348
11349 static bfd_boolean
11350 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11351 {
11352 if (h->root.type == bfd_link_hash_indirect)
11353 return TRUE;
11354
11355 merge_got_entries (&h->got.glist);
11356
11357 return TRUE;
11358 }
11359
11360 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11361 symbol H. */
11362
11363 static bfd_boolean
11364 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11365 {
11366 struct got_entry *gent;
11367
11368 if (h->root.type == bfd_link_hash_indirect)
11369 return TRUE;
11370
11371 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11372 if (!gent->is_indirect)
11373 allocate_got (h, (struct bfd_link_info *) inf, gent);
11374 return TRUE;
11375 }
11376
11377 /* Called on the first multitoc pass after the last call to
11378 ppc64_elf_next_toc_section. This function removes duplicate GOT
11379 entries. */
11380
11381 bfd_boolean
11382 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11383 {
11384 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11385 struct bfd *ibfd, *ibfd2;
11386 bfd_boolean done_something;
11387
11388 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11389
11390 if (!htab->do_multi_toc)
11391 return FALSE;
11392
11393 /* Merge global sym got entries within a toc group. */
11394 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11395
11396 /* And tlsld_got. */
11397 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11398 {
11399 struct got_entry *ent, *ent2;
11400
11401 if (!is_ppc64_elf (ibfd))
11402 continue;
11403
11404 ent = ppc64_tlsld_got (ibfd);
11405 if (!ent->is_indirect
11406 && ent->got.offset != (bfd_vma) -1)
11407 {
11408 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11409 {
11410 if (!is_ppc64_elf (ibfd2))
11411 continue;
11412
11413 ent2 = ppc64_tlsld_got (ibfd2);
11414 if (!ent2->is_indirect
11415 && ent2->got.offset != (bfd_vma) -1
11416 && elf_gp (ibfd2) == elf_gp (ibfd))
11417 {
11418 ent2->is_indirect = TRUE;
11419 ent2->got.ent = ent;
11420 }
11421 }
11422 }
11423 }
11424
11425 /* Zap sizes of got sections. */
11426 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11427 htab->elf.irelplt->size -= htab->got_reli_size;
11428 htab->got_reli_size = 0;
11429
11430 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11431 {
11432 asection *got, *relgot;
11433
11434 if (!is_ppc64_elf (ibfd))
11435 continue;
11436
11437 got = ppc64_elf_tdata (ibfd)->got;
11438 if (got != NULL)
11439 {
11440 got->rawsize = got->size;
11441 got->size = 0;
11442 relgot = ppc64_elf_tdata (ibfd)->relgot;
11443 relgot->rawsize = relgot->size;
11444 relgot->size = 0;
11445 }
11446 }
11447
11448 /* Now reallocate the got, local syms first. We don't need to
11449 allocate section contents again since we never increase size. */
11450 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11451 {
11452 struct got_entry **lgot_ents;
11453 struct got_entry **end_lgot_ents;
11454 struct plt_entry **local_plt;
11455 struct plt_entry **end_local_plt;
11456 unsigned char *lgot_masks;
11457 bfd_size_type locsymcount;
11458 Elf_Internal_Shdr *symtab_hdr;
11459 asection *s;
11460
11461 if (!is_ppc64_elf (ibfd))
11462 continue;
11463
11464 lgot_ents = elf_local_got_ents (ibfd);
11465 if (!lgot_ents)
11466 continue;
11467
11468 symtab_hdr = &elf_symtab_hdr (ibfd);
11469 locsymcount = symtab_hdr->sh_info;
11470 end_lgot_ents = lgot_ents + locsymcount;
11471 local_plt = (struct plt_entry **) end_lgot_ents;
11472 end_local_plt = local_plt + locsymcount;
11473 lgot_masks = (unsigned char *) end_local_plt;
11474 s = ppc64_elf_tdata (ibfd)->got;
11475 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11476 {
11477 struct got_entry *ent;
11478
11479 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11480 {
11481 unsigned int ent_size = 8;
11482 unsigned int rel_size = sizeof (Elf64_External_Rela);
11483
11484 ent->got.offset = s->size;
11485 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11486 {
11487 ent_size *= 2;
11488 rel_size *= 2;
11489 }
11490 s->size += ent_size;
11491 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
11492 {
11493 htab->elf.irelplt->size += rel_size;
11494 htab->got_reli_size += rel_size;
11495 }
11496 else if (bfd_link_pic (info)
11497 && !((ent->tls_type & TLS_TPREL) != 0
11498 && bfd_link_executable (info)))
11499 {
11500 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11501 srel->size += rel_size;
11502 }
11503 }
11504 }
11505 }
11506
11507 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11508
11509 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11510 {
11511 struct got_entry *ent;
11512
11513 if (!is_ppc64_elf (ibfd))
11514 continue;
11515
11516 ent = ppc64_tlsld_got (ibfd);
11517 if (!ent->is_indirect
11518 && ent->got.offset != (bfd_vma) -1)
11519 {
11520 asection *s = ppc64_elf_tdata (ibfd)->got;
11521 ent->got.offset = s->size;
11522 s->size += 16;
11523 if (bfd_link_pic (info))
11524 {
11525 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11526 srel->size += sizeof (Elf64_External_Rela);
11527 }
11528 }
11529 }
11530
11531 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11532 if (!done_something)
11533 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11534 {
11535 asection *got;
11536
11537 if (!is_ppc64_elf (ibfd))
11538 continue;
11539
11540 got = ppc64_elf_tdata (ibfd)->got;
11541 if (got != NULL)
11542 {
11543 done_something = got->rawsize != got->size;
11544 if (done_something)
11545 break;
11546 }
11547 }
11548
11549 if (done_something)
11550 (*htab->params->layout_sections_again) ();
11551
11552 /* Set up for second pass over toc sections to recalculate elf_gp
11553 on input sections. */
11554 htab->toc_bfd = NULL;
11555 htab->toc_first_sec = NULL;
11556 htab->second_toc_pass = TRUE;
11557 return done_something;
11558 }
11559
11560 /* Called after second pass of multitoc partitioning. */
11561
11562 void
11563 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11564 {
11565 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11566
11567 /* After the second pass, toc_curr tracks the TOC offset used
11568 for code sections below in ppc64_elf_next_input_section. */
11569 htab->toc_curr = TOC_BASE_OFF;
11570 }
11571
11572 /* No toc references were found in ISEC. If the code in ISEC makes no
11573 calls, then there's no need to use toc adjusting stubs when branching
11574 into ISEC. Actually, indirect calls from ISEC are OK as they will
11575 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11576 needed, and 2 if a cyclical call-graph was found but no other reason
11577 for a stub was detected. If called from the top level, a return of
11578 2 means the same as a return of 0. */
11579
11580 static int
11581 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11582 {
11583 int ret;
11584
11585 /* Mark this section as checked. */
11586 isec->call_check_done = 1;
11587
11588 /* We know none of our code bearing sections will need toc stubs. */
11589 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11590 return 0;
11591
11592 if (isec->size == 0)
11593 return 0;
11594
11595 if (isec->output_section == NULL)
11596 return 0;
11597
11598 ret = 0;
11599 if (isec->reloc_count != 0)
11600 {
11601 Elf_Internal_Rela *relstart, *rel;
11602 Elf_Internal_Sym *local_syms;
11603 struct ppc_link_hash_table *htab;
11604
11605 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11606 info->keep_memory);
11607 if (relstart == NULL)
11608 return -1;
11609
11610 /* Look for branches to outside of this section. */
11611 local_syms = NULL;
11612 htab = ppc_hash_table (info);
11613 if (htab == NULL)
11614 return -1;
11615
11616 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11617 {
11618 enum elf_ppc64_reloc_type r_type;
11619 unsigned long r_symndx;
11620 struct elf_link_hash_entry *h;
11621 struct ppc_link_hash_entry *eh;
11622 Elf_Internal_Sym *sym;
11623 asection *sym_sec;
11624 struct _opd_sec_data *opd;
11625 bfd_vma sym_value;
11626 bfd_vma dest;
11627
11628 r_type = ELF64_R_TYPE (rel->r_info);
11629 if (r_type != R_PPC64_REL24
11630 && r_type != R_PPC64_REL24_NOTOC
11631 && r_type != R_PPC64_REL14
11632 && r_type != R_PPC64_REL14_BRTAKEN
11633 && r_type != R_PPC64_REL14_BRNTAKEN
11634 && r_type != R_PPC64_PLTCALL)
11635 continue;
11636
11637 r_symndx = ELF64_R_SYM (rel->r_info);
11638 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11639 isec->owner))
11640 {
11641 ret = -1;
11642 break;
11643 }
11644
11645 /* Calls to dynamic lib functions go through a plt call stub
11646 that uses r2. */
11647 eh = (struct ppc_link_hash_entry *) h;
11648 if (eh != NULL
11649 && (eh->elf.plt.plist != NULL
11650 || (eh->oh != NULL
11651 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11652 {
11653 ret = 1;
11654 break;
11655 }
11656
11657 if (sym_sec == NULL)
11658 /* Ignore other undefined symbols. */
11659 continue;
11660
11661 /* Assume branches to other sections not included in the
11662 link need stubs too, to cover -R and absolute syms. */
11663 if (sym_sec->output_section == NULL)
11664 {
11665 ret = 1;
11666 break;
11667 }
11668
11669 if (h == NULL)
11670 sym_value = sym->st_value;
11671 else
11672 {
11673 if (h->root.type != bfd_link_hash_defined
11674 && h->root.type != bfd_link_hash_defweak)
11675 abort ();
11676 sym_value = h->root.u.def.value;
11677 }
11678 sym_value += rel->r_addend;
11679
11680 /* If this branch reloc uses an opd sym, find the code section. */
11681 opd = get_opd_info (sym_sec);
11682 if (opd != NULL)
11683 {
11684 if (h == NULL && opd->adjust != NULL)
11685 {
11686 long adjust;
11687
11688 adjust = opd->adjust[OPD_NDX (sym_value)];
11689 if (adjust == -1)
11690 /* Assume deleted functions won't ever be called. */
11691 continue;
11692 sym_value += adjust;
11693 }
11694
11695 dest = opd_entry_value (sym_sec, sym_value,
11696 &sym_sec, NULL, FALSE);
11697 if (dest == (bfd_vma) -1)
11698 continue;
11699 }
11700 else
11701 dest = (sym_value
11702 + sym_sec->output_offset
11703 + sym_sec->output_section->vma);
11704
11705 /* Ignore branch to self. */
11706 if (sym_sec == isec)
11707 continue;
11708
11709 /* If the called function uses the toc, we need a stub. */
11710 if (sym_sec->has_toc_reloc
11711 || sym_sec->makes_toc_func_call)
11712 {
11713 ret = 1;
11714 break;
11715 }
11716
11717 /* Assume any branch that needs a long branch stub might in fact
11718 need a plt_branch stub. A plt_branch stub uses r2. */
11719 else if (dest - (isec->output_offset
11720 + isec->output_section->vma
11721 + rel->r_offset) + (1 << 25)
11722 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11723 ? h->other
11724 : sym->st_other))
11725 {
11726 ret = 1;
11727 break;
11728 }
11729
11730 /* If calling back to a section in the process of being
11731 tested, we can't say for sure that no toc adjusting stubs
11732 are needed, so don't return zero. */
11733 else if (sym_sec->call_check_in_progress)
11734 ret = 2;
11735
11736 /* Branches to another section that itself doesn't have any TOC
11737 references are OK. Recursively call ourselves to check. */
11738 else if (!sym_sec->call_check_done)
11739 {
11740 int recur;
11741
11742 /* Mark current section as indeterminate, so that other
11743 sections that call back to current won't be marked as
11744 known. */
11745 isec->call_check_in_progress = 1;
11746 recur = toc_adjusting_stub_needed (info, sym_sec);
11747 isec->call_check_in_progress = 0;
11748
11749 if (recur != 0)
11750 {
11751 ret = recur;
11752 if (recur != 2)
11753 break;
11754 }
11755 }
11756 }
11757
11758 if (local_syms != NULL
11759 && (elf_symtab_hdr (isec->owner).contents
11760 != (unsigned char *) local_syms))
11761 free (local_syms);
11762 if (elf_section_data (isec)->relocs != relstart)
11763 free (relstart);
11764 }
11765
11766 if ((ret & 1) == 0
11767 && isec->map_head.s != NULL
11768 && (strcmp (isec->output_section->name, ".init") == 0
11769 || strcmp (isec->output_section->name, ".fini") == 0))
11770 {
11771 if (isec->map_head.s->has_toc_reloc
11772 || isec->map_head.s->makes_toc_func_call)
11773 ret = 1;
11774 else if (!isec->map_head.s->call_check_done)
11775 {
11776 int recur;
11777 isec->call_check_in_progress = 1;
11778 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11779 isec->call_check_in_progress = 0;
11780 if (recur != 0)
11781 ret = recur;
11782 }
11783 }
11784
11785 if (ret == 1)
11786 isec->makes_toc_func_call = 1;
11787
11788 return ret;
11789 }
11790
11791 /* The linker repeatedly calls this function for each input section,
11792 in the order that input sections are linked into output sections.
11793 Build lists of input sections to determine groupings between which
11794 we may insert linker stubs. */
11795
11796 bfd_boolean
11797 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11798 {
11799 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11800
11801 if (htab == NULL)
11802 return FALSE;
11803
11804 if ((isec->output_section->flags & SEC_CODE) != 0
11805 && isec->output_section->id < htab->sec_info_arr_size)
11806 {
11807 /* This happens to make the list in reverse order,
11808 which is what we want. */
11809 htab->sec_info[isec->id].u.list
11810 = htab->sec_info[isec->output_section->id].u.list;
11811 htab->sec_info[isec->output_section->id].u.list = isec;
11812 }
11813
11814 if (htab->multi_toc_needed)
11815 {
11816 /* Analyse sections that aren't already flagged as needing a
11817 valid toc pointer. Exclude .fixup for the linux kernel.
11818 .fixup contains branches, but only back to the function that
11819 hit an exception. */
11820 if (!(isec->has_toc_reloc
11821 || (isec->flags & SEC_CODE) == 0
11822 || strcmp (isec->name, ".fixup") == 0
11823 || isec->call_check_done))
11824 {
11825 if (toc_adjusting_stub_needed (info, isec) < 0)
11826 return FALSE;
11827 }
11828 /* Make all sections use the TOC assigned for this object file.
11829 This will be wrong for pasted sections; We fix that in
11830 check_pasted_section(). */
11831 if (elf_gp (isec->owner) != 0)
11832 htab->toc_curr = elf_gp (isec->owner);
11833 }
11834
11835 htab->sec_info[isec->id].toc_off = htab->toc_curr;
11836 return TRUE;
11837 }
11838
11839 /* Check that all .init and .fini sections use the same toc, if they
11840 have toc relocs. */
11841
11842 static bfd_boolean
11843 check_pasted_section (struct bfd_link_info *info, const char *name)
11844 {
11845 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11846
11847 if (o != NULL)
11848 {
11849 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11850 bfd_vma toc_off = 0;
11851 asection *i;
11852
11853 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11854 if (i->has_toc_reloc)
11855 {
11856 if (toc_off == 0)
11857 toc_off = htab->sec_info[i->id].toc_off;
11858 else if (toc_off != htab->sec_info[i->id].toc_off)
11859 return FALSE;
11860 }
11861
11862 if (toc_off == 0)
11863 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11864 if (i->makes_toc_func_call)
11865 {
11866 toc_off = htab->sec_info[i->id].toc_off;
11867 break;
11868 }
11869
11870 /* Make sure the whole pasted function uses the same toc offset. */
11871 if (toc_off != 0)
11872 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11873 htab->sec_info[i->id].toc_off = toc_off;
11874 }
11875 return TRUE;
11876 }
11877
11878 bfd_boolean
11879 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11880 {
11881 return (check_pasted_section (info, ".init")
11882 & check_pasted_section (info, ".fini"));
11883 }
11884
11885 /* See whether we can group stub sections together. Grouping stub
11886 sections may result in fewer stubs. More importantly, we need to
11887 put all .init* and .fini* stubs at the beginning of the .init or
11888 .fini output sections respectively, because glibc splits the
11889 _init and _fini functions into multiple parts. Putting a stub in
11890 the middle of a function is not a good idea. */
11891
11892 static bfd_boolean
11893 group_sections (struct bfd_link_info *info,
11894 bfd_size_type stub_group_size,
11895 bfd_boolean stubs_always_before_branch)
11896 {
11897 struct ppc_link_hash_table *htab;
11898 asection *osec;
11899 bfd_boolean suppress_size_errors;
11900
11901 htab = ppc_hash_table (info);
11902 if (htab == NULL)
11903 return FALSE;
11904
11905 suppress_size_errors = FALSE;
11906 if (stub_group_size == 1)
11907 {
11908 /* Default values. */
11909 if (stubs_always_before_branch)
11910 stub_group_size = 0x1e00000;
11911 else
11912 stub_group_size = 0x1c00000;
11913 suppress_size_errors = TRUE;
11914 }
11915
11916 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
11917 {
11918 asection *tail;
11919
11920 if (osec->id >= htab->sec_info_arr_size)
11921 continue;
11922
11923 tail = htab->sec_info[osec->id].u.list;
11924 while (tail != NULL)
11925 {
11926 asection *curr;
11927 asection *prev;
11928 bfd_size_type total;
11929 bfd_boolean big_sec;
11930 bfd_vma curr_toc;
11931 struct map_stub *group;
11932 bfd_size_type group_size;
11933
11934 curr = tail;
11935 total = tail->size;
11936 group_size = (ppc64_elf_section_data (tail) != NULL
11937 && ppc64_elf_section_data (tail)->has_14bit_branch
11938 ? stub_group_size >> 10 : stub_group_size);
11939
11940 big_sec = total > group_size;
11941 if (big_sec && !suppress_size_errors)
11942 /* xgettext:c-format */
11943 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
11944 tail->owner, tail);
11945 curr_toc = htab->sec_info[tail->id].toc_off;
11946
11947 while ((prev = htab->sec_info[curr->id].u.list) != NULL
11948 && ((total += curr->output_offset - prev->output_offset)
11949 < (ppc64_elf_section_data (prev) != NULL
11950 && ppc64_elf_section_data (prev)->has_14bit_branch
11951 ? (group_size = stub_group_size >> 10) : group_size))
11952 && htab->sec_info[prev->id].toc_off == curr_toc)
11953 curr = prev;
11954
11955 /* OK, the size from the start of CURR to the end is less
11956 than group_size and thus can be handled by one stub
11957 section. (or the tail section is itself larger than
11958 group_size, in which case we may be toast.) We should
11959 really be keeping track of the total size of stubs added
11960 here, as stubs contribute to the final output section
11961 size. That's a little tricky, and this way will only
11962 break if stubs added make the total size more than 2^25,
11963 ie. for the default stub_group_size, if stubs total more
11964 than 2097152 bytes, or nearly 75000 plt call stubs. */
11965 group = bfd_alloc (curr->owner, sizeof (*group));
11966 if (group == NULL)
11967 return FALSE;
11968 group->link_sec = curr;
11969 group->stub_sec = NULL;
11970 group->needs_save_res = 0;
11971 group->lr_restore = 0;
11972 group->eh_size = 0;
11973 group->eh_base = 0;
11974 group->next = htab->group;
11975 htab->group = group;
11976 do
11977 {
11978 prev = htab->sec_info[tail->id].u.list;
11979 /* Set up this stub group. */
11980 htab->sec_info[tail->id].u.group = group;
11981 }
11982 while (tail != curr && (tail = prev) != NULL);
11983
11984 /* But wait, there's more! Input sections up to group_size
11985 bytes before the stub section can be handled by it too.
11986 Don't do this if we have a really large section after the
11987 stubs, as adding more stubs increases the chance that
11988 branches may not reach into the stub section. */
11989 if (!stubs_always_before_branch && !big_sec)
11990 {
11991 total = 0;
11992 while (prev != NULL
11993 && ((total += tail->output_offset - prev->output_offset)
11994 < (ppc64_elf_section_data (prev) != NULL
11995 && ppc64_elf_section_data (prev)->has_14bit_branch
11996 ? (group_size = stub_group_size >> 10)
11997 : group_size))
11998 && htab->sec_info[prev->id].toc_off == curr_toc)
11999 {
12000 tail = prev;
12001 prev = htab->sec_info[tail->id].u.list;
12002 htab->sec_info[tail->id].u.group = group;
12003 }
12004 }
12005 tail = prev;
12006 }
12007 }
12008 return TRUE;
12009 }
12010
12011 static const unsigned char glink_eh_frame_cie[] =
12012 {
12013 0, 0, 0, 16, /* length. */
12014 0, 0, 0, 0, /* id. */
12015 1, /* CIE version. */
12016 'z', 'R', 0, /* Augmentation string. */
12017 4, /* Code alignment. */
12018 0x78, /* Data alignment. */
12019 65, /* RA reg. */
12020 1, /* Augmentation size. */
12021 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12022 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12023 };
12024
12025 /* Stripping output sections is normally done before dynamic section
12026 symbols have been allocated. This function is called later, and
12027 handles cases like htab->brlt which is mapped to its own output
12028 section. */
12029
12030 static void
12031 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12032 {
12033 if (isec->size == 0
12034 && isec->output_section->size == 0
12035 && !(isec->output_section->flags & SEC_KEEP)
12036 && !bfd_section_removed_from_list (info->output_bfd,
12037 isec->output_section)
12038 && elf_section_data (isec->output_section)->dynindx == 0)
12039 {
12040 isec->output_section->flags |= SEC_EXCLUDE;
12041 bfd_section_list_remove (info->output_bfd, isec->output_section);
12042 info->output_bfd->section_count--;
12043 }
12044 }
12045
12046 /* Determine and set the size of the stub section for a final link.
12047
12048 The basic idea here is to examine all the relocations looking for
12049 PC-relative calls to a target that is unreachable with a "bl"
12050 instruction. */
12051
12052 bfd_boolean
12053 ppc64_elf_size_stubs (struct bfd_link_info *info)
12054 {
12055 bfd_size_type stub_group_size;
12056 bfd_boolean stubs_always_before_branch;
12057 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12058
12059 if (htab == NULL)
12060 return FALSE;
12061
12062 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12063 htab->params->plt_thread_safe = 1;
12064 if (!htab->opd_abi)
12065 htab->params->plt_thread_safe = 0;
12066 else if (htab->params->plt_thread_safe == -1)
12067 {
12068 static const char *const thread_starter[] =
12069 {
12070 "pthread_create",
12071 /* libstdc++ */
12072 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12073 /* librt */
12074 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12075 "mq_notify", "create_timer",
12076 /* libanl */
12077 "getaddrinfo_a",
12078 /* libgomp */
12079 "GOMP_parallel",
12080 "GOMP_parallel_start",
12081 "GOMP_parallel_loop_static",
12082 "GOMP_parallel_loop_static_start",
12083 "GOMP_parallel_loop_dynamic",
12084 "GOMP_parallel_loop_dynamic_start",
12085 "GOMP_parallel_loop_guided",
12086 "GOMP_parallel_loop_guided_start",
12087 "GOMP_parallel_loop_runtime",
12088 "GOMP_parallel_loop_runtime_start",
12089 "GOMP_parallel_sections",
12090 "GOMP_parallel_sections_start",
12091 /* libgo */
12092 "__go_go",
12093 };
12094 unsigned i;
12095
12096 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12097 {
12098 struct elf_link_hash_entry *h;
12099 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12100 FALSE, FALSE, TRUE);
12101 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12102 if (htab->params->plt_thread_safe)
12103 break;
12104 }
12105 }
12106 stubs_always_before_branch = htab->params->group_size < 0;
12107 if (htab->params->group_size < 0)
12108 stub_group_size = -htab->params->group_size;
12109 else
12110 stub_group_size = htab->params->group_size;
12111
12112 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12113 return FALSE;
12114
12115 #define STUB_SHRINK_ITER 20
12116 /* Loop until no stubs added. After iteration 20 of this loop we may
12117 exit on a stub section shrinking. This is to break out of a
12118 pathological case where adding stubs on one iteration decreases
12119 section gaps (perhaps due to alignment), which then requires
12120 fewer or smaller stubs on the next iteration. */
12121
12122 while (1)
12123 {
12124 bfd *input_bfd;
12125 unsigned int bfd_indx;
12126 struct map_stub *group;
12127
12128 htab->stub_iteration += 1;
12129
12130 for (input_bfd = info->input_bfds, bfd_indx = 0;
12131 input_bfd != NULL;
12132 input_bfd = input_bfd->link.next, bfd_indx++)
12133 {
12134 Elf_Internal_Shdr *symtab_hdr;
12135 asection *section;
12136 Elf_Internal_Sym *local_syms = NULL;
12137
12138 if (!is_ppc64_elf (input_bfd))
12139 continue;
12140
12141 /* We'll need the symbol table in a second. */
12142 symtab_hdr = &elf_symtab_hdr (input_bfd);
12143 if (symtab_hdr->sh_info == 0)
12144 continue;
12145
12146 /* Walk over each section attached to the input bfd. */
12147 for (section = input_bfd->sections;
12148 section != NULL;
12149 section = section->next)
12150 {
12151 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12152
12153 /* If there aren't any relocs, then there's nothing more
12154 to do. */
12155 if ((section->flags & SEC_RELOC) == 0
12156 || (section->flags & SEC_ALLOC) == 0
12157 || (section->flags & SEC_LOAD) == 0
12158 || (section->flags & SEC_CODE) == 0
12159 || section->reloc_count == 0)
12160 continue;
12161
12162 /* If this section is a link-once section that will be
12163 discarded, then don't create any stubs. */
12164 if (section->output_section == NULL
12165 || section->output_section->owner != info->output_bfd)
12166 continue;
12167
12168 /* Get the relocs. */
12169 internal_relocs
12170 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12171 info->keep_memory);
12172 if (internal_relocs == NULL)
12173 goto error_ret_free_local;
12174
12175 /* Now examine each relocation. */
12176 irela = internal_relocs;
12177 irelaend = irela + section->reloc_count;
12178 for (; irela < irelaend; irela++)
12179 {
12180 enum elf_ppc64_reloc_type r_type;
12181 unsigned int r_indx;
12182 enum ppc_stub_type stub_type;
12183 struct ppc_stub_hash_entry *stub_entry;
12184 asection *sym_sec, *code_sec;
12185 bfd_vma sym_value, code_value;
12186 bfd_vma destination;
12187 unsigned long local_off;
12188 bfd_boolean ok_dest;
12189 struct ppc_link_hash_entry *hash;
12190 struct ppc_link_hash_entry *fdh;
12191 struct elf_link_hash_entry *h;
12192 Elf_Internal_Sym *sym;
12193 char *stub_name;
12194 const asection *id_sec;
12195 struct _opd_sec_data *opd;
12196 struct plt_entry *plt_ent;
12197
12198 r_type = ELF64_R_TYPE (irela->r_info);
12199 r_indx = ELF64_R_SYM (irela->r_info);
12200
12201 if (r_type >= R_PPC64_max)
12202 {
12203 bfd_set_error (bfd_error_bad_value);
12204 goto error_ret_free_internal;
12205 }
12206
12207 /* Only look for stubs on branch instructions. */
12208 if (r_type != R_PPC64_REL24
12209 && r_type != R_PPC64_REL24_NOTOC
12210 && r_type != R_PPC64_REL14
12211 && r_type != R_PPC64_REL14_BRTAKEN
12212 && r_type != R_PPC64_REL14_BRNTAKEN)
12213 continue;
12214
12215 /* Now determine the call target, its name, value,
12216 section. */
12217 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12218 r_indx, input_bfd))
12219 goto error_ret_free_internal;
12220 hash = (struct ppc_link_hash_entry *) h;
12221
12222 ok_dest = FALSE;
12223 fdh = NULL;
12224 sym_value = 0;
12225 if (hash == NULL)
12226 {
12227 sym_value = sym->st_value;
12228 if (sym_sec != NULL
12229 && sym_sec->output_section != NULL)
12230 ok_dest = TRUE;
12231 }
12232 else if (hash->elf.root.type == bfd_link_hash_defined
12233 || hash->elf.root.type == bfd_link_hash_defweak)
12234 {
12235 sym_value = hash->elf.root.u.def.value;
12236 if (sym_sec->output_section != NULL)
12237 ok_dest = TRUE;
12238 }
12239 else if (hash->elf.root.type == bfd_link_hash_undefweak
12240 || hash->elf.root.type == bfd_link_hash_undefined)
12241 {
12242 /* Recognise an old ABI func code entry sym, and
12243 use the func descriptor sym instead if it is
12244 defined. */
12245 if (hash->elf.root.root.string[0] == '.'
12246 && hash->oh != NULL)
12247 {
12248 fdh = ppc_follow_link (hash->oh);
12249 if (fdh->elf.root.type == bfd_link_hash_defined
12250 || fdh->elf.root.type == bfd_link_hash_defweak)
12251 {
12252 sym_sec = fdh->elf.root.u.def.section;
12253 sym_value = fdh->elf.root.u.def.value;
12254 if (sym_sec->output_section != NULL)
12255 ok_dest = TRUE;
12256 }
12257 else
12258 fdh = NULL;
12259 }
12260 }
12261 else
12262 {
12263 bfd_set_error (bfd_error_bad_value);
12264 goto error_ret_free_internal;
12265 }
12266
12267 destination = 0;
12268 local_off = 0;
12269 if (ok_dest)
12270 {
12271 sym_value += irela->r_addend;
12272 destination = (sym_value
12273 + sym_sec->output_offset
12274 + sym_sec->output_section->vma);
12275 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12276 ? hash->elf.other
12277 : sym->st_other);
12278 }
12279
12280 code_sec = sym_sec;
12281 code_value = sym_value;
12282 opd = get_opd_info (sym_sec);
12283 if (opd != NULL)
12284 {
12285 bfd_vma dest;
12286
12287 if (hash == NULL && opd->adjust != NULL)
12288 {
12289 long adjust = opd->adjust[OPD_NDX (sym_value)];
12290 if (adjust == -1)
12291 continue;
12292 code_value += adjust;
12293 sym_value += adjust;
12294 }
12295 dest = opd_entry_value (sym_sec, sym_value,
12296 &code_sec, &code_value, FALSE);
12297 if (dest != (bfd_vma) -1)
12298 {
12299 destination = dest;
12300 if (fdh != NULL)
12301 {
12302 /* Fixup old ABI sym to point at code
12303 entry. */
12304 hash->elf.root.type = bfd_link_hash_defweak;
12305 hash->elf.root.u.def.section = code_sec;
12306 hash->elf.root.u.def.value = code_value;
12307 }
12308 }
12309 }
12310
12311 /* Determine what (if any) linker stub is needed. */
12312 plt_ent = NULL;
12313 stub_type = ppc_type_of_stub (section, irela, &hash,
12314 &plt_ent, destination,
12315 local_off);
12316
12317 if (r_type == R_PPC64_REL24_NOTOC)
12318 {
12319 if (stub_type == ppc_stub_plt_call)
12320 stub_type = ppc_stub_plt_call_notoc;
12321 else if (stub_type == ppc_stub_long_branch
12322 || (code_sec != NULL
12323 && code_sec->output_section != NULL
12324 && (((hash ? hash->elf.other : sym->st_other)
12325 & STO_PPC64_LOCAL_MASK)
12326 != 1 << STO_PPC64_LOCAL_BIT)))
12327 stub_type = ppc_stub_long_branch_notoc;
12328 }
12329 else if (stub_type != ppc_stub_plt_call)
12330 {
12331 /* Check whether we need a TOC adjusting stub.
12332 Since the linker pastes together pieces from
12333 different object files when creating the
12334 _init and _fini functions, it may be that a
12335 call to what looks like a local sym is in
12336 fact a call needing a TOC adjustment. */
12337 if ((code_sec != NULL
12338 && code_sec->output_section != NULL
12339 && (htab->sec_info[code_sec->id].toc_off
12340 != htab->sec_info[section->id].toc_off)
12341 && (code_sec->has_toc_reloc
12342 || code_sec->makes_toc_func_call))
12343 || (((hash ? hash->elf.other : sym->st_other)
12344 & STO_PPC64_LOCAL_MASK)
12345 == 1 << STO_PPC64_LOCAL_BIT))
12346 stub_type = ppc_stub_long_branch_r2off;
12347 }
12348
12349 if (stub_type == ppc_stub_none)
12350 continue;
12351
12352 /* __tls_get_addr calls might be eliminated. */
12353 if (stub_type != ppc_stub_plt_call
12354 && stub_type != ppc_stub_plt_call_notoc
12355 && hash != NULL
12356 && (hash == htab->tls_get_addr
12357 || hash == htab->tls_get_addr_fd)
12358 && section->has_tls_reloc
12359 && irela != internal_relocs)
12360 {
12361 /* Get tls info. */
12362 unsigned char *tls_mask;
12363
12364 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12365 irela - 1, input_bfd))
12366 goto error_ret_free_internal;
12367 if ((*tls_mask & TLS_TLS) != 0)
12368 continue;
12369 }
12370
12371 if (stub_type == ppc_stub_plt_call)
12372 {
12373 if (!htab->opd_abi
12374 && htab->params->plt_localentry0 != 0
12375 && is_elfv2_localentry0 (&hash->elf))
12376 htab->has_plt_localentry0 = 1;
12377 else if (irela + 1 < irelaend
12378 && irela[1].r_offset == irela->r_offset + 4
12379 && (ELF64_R_TYPE (irela[1].r_info)
12380 == R_PPC64_TOCSAVE))
12381 {
12382 if (!tocsave_find (htab, INSERT,
12383 &local_syms, irela + 1, input_bfd))
12384 goto error_ret_free_internal;
12385 }
12386 else
12387 stub_type = ppc_stub_plt_call_r2save;
12388 }
12389
12390 /* Support for grouping stub sections. */
12391 id_sec = htab->sec_info[section->id].u.group->link_sec;
12392
12393 /* Get the name of this stub. */
12394 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12395 if (!stub_name)
12396 goto error_ret_free_internal;
12397
12398 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12399 stub_name, FALSE, FALSE);
12400 if (stub_entry != NULL)
12401 {
12402 enum ppc_stub_type old_type;
12403 /* A stub has already been created, but it may
12404 not be the required type. We shouldn't be
12405 transitioning from plt_call to long_branch
12406 stubs or vice versa, but we might be
12407 upgrading from plt_call to plt_call_r2save or
12408 from long_branch to long_branch_r2off. */
12409 free (stub_name);
12410 old_type = stub_entry->stub_type;
12411 switch (old_type)
12412 {
12413 default:
12414 abort ();
12415
12416 case ppc_stub_save_res:
12417 continue;
12418
12419 case ppc_stub_plt_call:
12420 case ppc_stub_plt_call_r2save:
12421 case ppc_stub_plt_call_notoc:
12422 case ppc_stub_plt_call_both:
12423 if (stub_type == ppc_stub_plt_call)
12424 continue;
12425 else if (stub_type == ppc_stub_plt_call_r2save)
12426 {
12427 if (old_type == ppc_stub_plt_call_notoc)
12428 stub_type = ppc_stub_plt_call_both;
12429 }
12430 else if (stub_type == ppc_stub_plt_call_notoc)
12431 {
12432 if (old_type == ppc_stub_plt_call_r2save)
12433 stub_type = ppc_stub_plt_call_both;
12434 }
12435 else
12436 abort ();
12437 break;
12438
12439 case ppc_stub_plt_branch:
12440 case ppc_stub_plt_branch_r2off:
12441 case ppc_stub_plt_branch_notoc:
12442 case ppc_stub_plt_branch_both:
12443 old_type += (ppc_stub_long_branch
12444 - ppc_stub_plt_branch);
12445 /* Fall through. */
12446 case ppc_stub_long_branch:
12447 case ppc_stub_long_branch_r2off:
12448 case ppc_stub_long_branch_notoc:
12449 case ppc_stub_long_branch_both:
12450 if (stub_type == ppc_stub_long_branch)
12451 continue;
12452 else if (stub_type == ppc_stub_long_branch_r2off)
12453 {
12454 if (old_type == ppc_stub_long_branch_notoc)
12455 stub_type = ppc_stub_long_branch_both;
12456 }
12457 else if (stub_type == ppc_stub_long_branch_notoc)
12458 {
12459 if (old_type == ppc_stub_long_branch_r2off)
12460 stub_type = ppc_stub_long_branch_both;
12461 }
12462 else
12463 abort ();
12464 break;
12465 }
12466 if (old_type < stub_type)
12467 stub_entry->stub_type = stub_type;
12468 continue;
12469 }
12470
12471 stub_entry = ppc_add_stub (stub_name, section, info);
12472 if (stub_entry == NULL)
12473 {
12474 free (stub_name);
12475 error_ret_free_internal:
12476 if (elf_section_data (section)->relocs == NULL)
12477 free (internal_relocs);
12478 error_ret_free_local:
12479 if (local_syms != NULL
12480 && (symtab_hdr->contents
12481 != (unsigned char *) local_syms))
12482 free (local_syms);
12483 return FALSE;
12484 }
12485
12486 stub_entry->stub_type = stub_type;
12487 if (stub_type >= ppc_stub_plt_call
12488 && stub_type <= ppc_stub_plt_call_both)
12489 {
12490 stub_entry->target_value = sym_value;
12491 stub_entry->target_section = sym_sec;
12492 }
12493 else
12494 {
12495 stub_entry->target_value = code_value;
12496 stub_entry->target_section = code_sec;
12497 }
12498 stub_entry->h = hash;
12499 stub_entry->plt_ent = plt_ent;
12500 stub_entry->symtype
12501 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
12502 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12503
12504 if (hash != NULL
12505 && (hash->elf.root.type == bfd_link_hash_defined
12506 || hash->elf.root.type == bfd_link_hash_defweak))
12507 htab->stub_globals += 1;
12508 }
12509
12510 /* We're done with the internal relocs, free them. */
12511 if (elf_section_data (section)->relocs != internal_relocs)
12512 free (internal_relocs);
12513 }
12514
12515 if (local_syms != NULL
12516 && symtab_hdr->contents != (unsigned char *) local_syms)
12517 {
12518 if (!info->keep_memory)
12519 free (local_syms);
12520 else
12521 symtab_hdr->contents = (unsigned char *) local_syms;
12522 }
12523 }
12524
12525 /* We may have added some stubs. Find out the new size of the
12526 stub sections. */
12527 for (group = htab->group; group != NULL; group = group->next)
12528 {
12529 group->lr_restore = 0;
12530 group->eh_size = 0;
12531 if (group->stub_sec != NULL)
12532 {
12533 asection *stub_sec = group->stub_sec;
12534
12535 if (htab->stub_iteration <= STUB_SHRINK_ITER
12536 || stub_sec->rawsize < stub_sec->size)
12537 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12538 stub_sec->rawsize = stub_sec->size;
12539 stub_sec->size = 0;
12540 stub_sec->reloc_count = 0;
12541 stub_sec->flags &= ~SEC_RELOC;
12542 }
12543 }
12544
12545 if (htab->stub_iteration <= STUB_SHRINK_ITER
12546 || htab->brlt->rawsize < htab->brlt->size)
12547 htab->brlt->rawsize = htab->brlt->size;
12548 htab->brlt->size = 0;
12549 htab->brlt->reloc_count = 0;
12550 htab->brlt->flags &= ~SEC_RELOC;
12551 if (htab->relbrlt != NULL)
12552 htab->relbrlt->size = 0;
12553
12554 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12555
12556 for (group = htab->group; group != NULL; group = group->next)
12557 if (group->needs_save_res)
12558 group->stub_sec->size += htab->sfpr->size;
12559
12560 if (info->emitrelocations
12561 && htab->glink != NULL && htab->glink->size != 0)
12562 {
12563 htab->glink->reloc_count = 1;
12564 htab->glink->flags |= SEC_RELOC;
12565 }
12566
12567 if (htab->glink_eh_frame != NULL
12568 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12569 && htab->glink_eh_frame->output_section->size > 8)
12570 {
12571 size_t size = 0, align = 4;
12572
12573 for (group = htab->group; group != NULL; group = group->next)
12574 if (group->eh_size != 0)
12575 size += (group->eh_size + 17 + align - 1) & -align;
12576 if (htab->glink != NULL && htab->glink->size != 0)
12577 size += (24 + align - 1) & -align;
12578 if (size != 0)
12579 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12580 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12581 size = (size + align - 1) & -align;
12582 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12583 htab->glink_eh_frame->size = size;
12584 }
12585
12586 if (htab->params->plt_stub_align != 0)
12587 for (group = htab->group; group != NULL; group = group->next)
12588 if (group->stub_sec != NULL)
12589 {
12590 int align = abs (htab->params->plt_stub_align);
12591 group->stub_sec->size
12592 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12593 }
12594
12595 for (group = htab->group; group != NULL; group = group->next)
12596 if (group->stub_sec != NULL
12597 && group->stub_sec->rawsize != group->stub_sec->size
12598 && (htab->stub_iteration <= STUB_SHRINK_ITER
12599 || group->stub_sec->rawsize < group->stub_sec->size))
12600 break;
12601
12602 if (group == NULL
12603 && (htab->brlt->rawsize == htab->brlt->size
12604 || (htab->stub_iteration > STUB_SHRINK_ITER
12605 && htab->brlt->rawsize > htab->brlt->size))
12606 && (htab->glink_eh_frame == NULL
12607 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12608 break;
12609
12610 /* Ask the linker to do its stuff. */
12611 (*htab->params->layout_sections_again) ();
12612 }
12613
12614 if (htab->glink_eh_frame != NULL
12615 && htab->glink_eh_frame->size != 0)
12616 {
12617 bfd_vma val;
12618 bfd_byte *p, *last_fde;
12619 size_t last_fde_len, size, align, pad;
12620 struct map_stub *group;
12621
12622 /* It is necessary to at least have a rough outline of the
12623 linker generated CIEs and FDEs written before
12624 bfd_elf_discard_info is run, in order for these FDEs to be
12625 indexed in .eh_frame_hdr. */
12626 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12627 if (p == NULL)
12628 return FALSE;
12629 htab->glink_eh_frame->contents = p;
12630 last_fde = p;
12631 align = 4;
12632
12633 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12634 /* CIE length (rewrite in case little-endian). */
12635 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12636 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12637 p += last_fde_len + 4;
12638
12639 for (group = htab->group; group != NULL; group = group->next)
12640 if (group->eh_size != 0)
12641 {
12642 group->eh_base = p - htab->glink_eh_frame->contents;
12643 last_fde = p;
12644 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
12645 /* FDE length. */
12646 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12647 p += 4;
12648 /* CIE pointer. */
12649 val = p - htab->glink_eh_frame->contents;
12650 bfd_put_32 (htab->elf.dynobj, val, p);
12651 p += 4;
12652 /* Offset to stub section, written later. */
12653 p += 4;
12654 /* stub section size. */
12655 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
12656 p += 4;
12657 /* Augmentation. */
12658 p += 1;
12659 /* Make sure we don't have all nops. This is enough for
12660 elf-eh-frame.c to detect the last non-nop opcode. */
12661 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
12662 p = last_fde + last_fde_len + 4;
12663 }
12664 if (htab->glink != NULL && htab->glink->size != 0)
12665 {
12666 last_fde = p;
12667 last_fde_len = ((24 + align - 1) & -align) - 4;
12668 /* FDE length. */
12669 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12670 p += 4;
12671 /* CIE pointer. */
12672 val = p - htab->glink_eh_frame->contents;
12673 bfd_put_32 (htab->elf.dynobj, val, p);
12674 p += 4;
12675 /* Offset to .glink, written later. */
12676 p += 4;
12677 /* .glink size. */
12678 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12679 p += 4;
12680 /* Augmentation. */
12681 p += 1;
12682
12683 *p++ = DW_CFA_advance_loc + 1;
12684 *p++ = DW_CFA_register;
12685 *p++ = 65;
12686 *p++ = htab->opd_abi ? 12 : 0;
12687 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
12688 *p++ = DW_CFA_restore_extended;
12689 *p++ = 65;
12690 p += ((24 + align - 1) & -align) - 24;
12691 }
12692 /* Subsume any padding into the last FDE if user .eh_frame
12693 sections are aligned more than glink_eh_frame. Otherwise any
12694 zero padding will be seen as a terminator. */
12695 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12696 size = p - htab->glink_eh_frame->contents;
12697 pad = ((size + align - 1) & -align) - size;
12698 htab->glink_eh_frame->size = size + pad;
12699 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12700 }
12701
12702 maybe_strip_output (info, htab->brlt);
12703 if (htab->glink_eh_frame != NULL)
12704 maybe_strip_output (info, htab->glink_eh_frame);
12705
12706 return TRUE;
12707 }
12708
12709 /* Called after we have determined section placement. If sections
12710 move, we'll be called again. Provide a value for TOCstart. */
12711
12712 bfd_vma
12713 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12714 {
12715 asection *s;
12716 bfd_vma TOCstart, adjust;
12717
12718 if (info != NULL)
12719 {
12720 struct elf_link_hash_entry *h;
12721 struct elf_link_hash_table *htab = elf_hash_table (info);
12722
12723 if (is_elf_hash_table (htab)
12724 && htab->hgot != NULL)
12725 h = htab->hgot;
12726 else
12727 {
12728 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12729 if (is_elf_hash_table (htab))
12730 htab->hgot = h;
12731 }
12732 if (h != NULL
12733 && h->root.type == bfd_link_hash_defined
12734 && !h->root.linker_def
12735 && (!is_elf_hash_table (htab)
12736 || h->def_regular))
12737 {
12738 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12739 + h->root.u.def.section->output_offset
12740 + h->root.u.def.section->output_section->vma);
12741 _bfd_set_gp_value (obfd, TOCstart);
12742 return TOCstart;
12743 }
12744 }
12745
12746 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12747 order. The TOC starts where the first of these sections starts. */
12748 s = bfd_get_section_by_name (obfd, ".got");
12749 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12750 s = bfd_get_section_by_name (obfd, ".toc");
12751 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12752 s = bfd_get_section_by_name (obfd, ".tocbss");
12753 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12754 s = bfd_get_section_by_name (obfd, ".plt");
12755 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12756 {
12757 /* This may happen for
12758 o references to TOC base (SYM@toc / TOC[tc0]) without a
12759 .toc directive
12760 o bad linker script
12761 o --gc-sections and empty TOC sections
12762
12763 FIXME: Warn user? */
12764
12765 /* Look for a likely section. We probably won't even be
12766 using TOCstart. */
12767 for (s = obfd->sections; s != NULL; s = s->next)
12768 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12769 | SEC_EXCLUDE))
12770 == (SEC_ALLOC | SEC_SMALL_DATA))
12771 break;
12772 if (s == NULL)
12773 for (s = obfd->sections; s != NULL; s = s->next)
12774 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12775 == (SEC_ALLOC | SEC_SMALL_DATA))
12776 break;
12777 if (s == NULL)
12778 for (s = obfd->sections; s != NULL; s = s->next)
12779 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12780 == SEC_ALLOC)
12781 break;
12782 if (s == NULL)
12783 for (s = obfd->sections; s != NULL; s = s->next)
12784 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12785 break;
12786 }
12787
12788 TOCstart = 0;
12789 if (s != NULL)
12790 TOCstart = s->output_section->vma + s->output_offset;
12791
12792 /* Force alignment. */
12793 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12794 TOCstart -= adjust;
12795 _bfd_set_gp_value (obfd, TOCstart);
12796
12797 if (info != NULL && s != NULL)
12798 {
12799 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12800
12801 if (htab != NULL)
12802 {
12803 if (htab->elf.hgot != NULL)
12804 {
12805 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12806 htab->elf.hgot->root.u.def.section = s;
12807 }
12808 }
12809 else
12810 {
12811 struct bfd_link_hash_entry *bh = NULL;
12812 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12813 s, TOC_BASE_OFF - adjust,
12814 NULL, FALSE, FALSE, &bh);
12815 }
12816 }
12817 return TOCstart;
12818 }
12819
12820 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12821 write out any global entry stubs, and PLT relocations. */
12822
12823 static bfd_boolean
12824 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
12825 {
12826 struct bfd_link_info *info;
12827 struct ppc_link_hash_table *htab;
12828 struct plt_entry *ent;
12829 asection *s;
12830
12831 if (h->root.type == bfd_link_hash_indirect)
12832 return TRUE;
12833
12834 info = inf;
12835 htab = ppc_hash_table (info);
12836 if (htab == NULL)
12837 return FALSE;
12838
12839 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12840 if (ent->plt.offset != (bfd_vma) -1)
12841 {
12842 /* This symbol has an entry in the procedure linkage
12843 table. Set it up. */
12844 Elf_Internal_Rela rela;
12845 asection *plt, *relplt;
12846 bfd_byte *loc;
12847
12848 if (!htab->elf.dynamic_sections_created
12849 || h->dynindx == -1)
12850 {
12851 if (!(h->def_regular
12852 && (h->root.type == bfd_link_hash_defined
12853 || h->root.type == bfd_link_hash_defweak)))
12854 continue;
12855 if (h->type == STT_GNU_IFUNC)
12856 {
12857 plt = htab->elf.iplt;
12858 relplt = htab->elf.irelplt;
12859 htab->local_ifunc_resolver = 1;
12860 if (htab->opd_abi)
12861 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
12862 else
12863 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12864 }
12865 else
12866 {
12867 plt = htab->pltlocal;
12868 if (bfd_link_pic (info))
12869 {
12870 relplt = htab->relpltlocal;
12871 if (htab->opd_abi)
12872 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
12873 else
12874 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12875 }
12876 else
12877 relplt = NULL;
12878 }
12879 rela.r_addend = (h->root.u.def.value
12880 + h->root.u.def.section->output_offset
12881 + h->root.u.def.section->output_section->vma
12882 + ent->addend);
12883
12884 if (relplt == NULL)
12885 {
12886 loc = plt->contents + ent->plt.offset;
12887 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
12888 if (htab->opd_abi)
12889 {
12890 bfd_vma toc = elf_gp (info->output_bfd);
12891 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
12892 bfd_put_64 (info->output_bfd, toc, loc + 8);
12893 }
12894 }
12895 else
12896 {
12897 rela.r_offset = (plt->output_section->vma
12898 + plt->output_offset
12899 + ent->plt.offset);
12900 loc = relplt->contents + (relplt->reloc_count++
12901 * sizeof (Elf64_External_Rela));
12902 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12903 }
12904 }
12905 else
12906 {
12907 rela.r_offset = (htab->elf.splt->output_section->vma
12908 + htab->elf.splt->output_offset
12909 + ent->plt.offset);
12910 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
12911 rela.r_addend = ent->addend;
12912 loc = (htab->elf.srelplt->contents
12913 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
12914 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
12915 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
12916 htab->maybe_local_ifunc_resolver = 1;
12917 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12918 }
12919 }
12920
12921 if (!h->pointer_equality_needed)
12922 return TRUE;
12923
12924 if (h->def_regular)
12925 return TRUE;
12926
12927 s = htab->global_entry;
12928 if (s == NULL || s->size == 0)
12929 return TRUE;
12930
12931 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12932 if (ent->plt.offset != (bfd_vma) -1
12933 && ent->addend == 0)
12934 {
12935 bfd_byte *p;
12936 asection *plt;
12937 bfd_vma off;
12938
12939 p = s->contents + h->root.u.def.value;
12940 plt = htab->elf.splt;
12941 if (!htab->elf.dynamic_sections_created
12942 || h->dynindx == -1)
12943 {
12944 if (h->type == STT_GNU_IFUNC)
12945 plt = htab->elf.iplt;
12946 else
12947 plt = htab->pltlocal;
12948 }
12949 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
12950 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12951
12952 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12953 {
12954 info->callbacks->einfo
12955 (_("%P: linkage table error against `%pT'\n"),
12956 h->root.root.string);
12957 bfd_set_error (bfd_error_bad_value);
12958 htab->stub_error = TRUE;
12959 }
12960
12961 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12962 if (htab->params->emit_stub_syms)
12963 {
12964 size_t len = strlen (h->root.root.string);
12965 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12966
12967 if (name == NULL)
12968 return FALSE;
12969
12970 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12971 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12972 if (h == NULL)
12973 return FALSE;
12974 if (h->root.type == bfd_link_hash_new)
12975 {
12976 h->root.type = bfd_link_hash_defined;
12977 h->root.u.def.section = s;
12978 h->root.u.def.value = p - s->contents;
12979 h->ref_regular = 1;
12980 h->def_regular = 1;
12981 h->ref_regular_nonweak = 1;
12982 h->forced_local = 1;
12983 h->non_elf = 0;
12984 h->root.linker_def = 1;
12985 }
12986 }
12987
12988 if (PPC_HA (off) != 0)
12989 {
12990 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12991 p += 4;
12992 }
12993 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12994 p += 4;
12995 bfd_put_32 (s->owner, MTCTR_R12, p);
12996 p += 4;
12997 bfd_put_32 (s->owner, BCTR, p);
12998 break;
12999 }
13000 return TRUE;
13001 }
13002
13003 /* Write PLT relocs for locals. */
13004
13005 static bfd_boolean
13006 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13007 {
13008 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13009 bfd *ibfd;
13010
13011 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13012 {
13013 struct got_entry **lgot_ents, **end_lgot_ents;
13014 struct plt_entry **local_plt, **lplt, **end_local_plt;
13015 Elf_Internal_Shdr *symtab_hdr;
13016 bfd_size_type locsymcount;
13017 Elf_Internal_Sym *local_syms = NULL;
13018 struct plt_entry *ent;
13019
13020 if (!is_ppc64_elf (ibfd))
13021 continue;
13022
13023 lgot_ents = elf_local_got_ents (ibfd);
13024 if (!lgot_ents)
13025 continue;
13026
13027 symtab_hdr = &elf_symtab_hdr (ibfd);
13028 locsymcount = symtab_hdr->sh_info;
13029 end_lgot_ents = lgot_ents + locsymcount;
13030 local_plt = (struct plt_entry **) end_lgot_ents;
13031 end_local_plt = local_plt + locsymcount;
13032 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13033 for (ent = *lplt; ent != NULL; ent = ent->next)
13034 if (ent->plt.offset != (bfd_vma) -1)
13035 {
13036 Elf_Internal_Sym *sym;
13037 asection *sym_sec;
13038 asection *plt, *relplt;
13039 bfd_byte *loc;
13040 bfd_vma val;
13041
13042 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13043 lplt - local_plt, ibfd))
13044 {
13045 if (local_syms != NULL
13046 && symtab_hdr->contents != (unsigned char *) local_syms)
13047 free (local_syms);
13048 return FALSE;
13049 }
13050
13051 val = sym->st_value + ent->addend;
13052 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13053 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13054 if (sym_sec != NULL && sym_sec->output_section != NULL)
13055 val += sym_sec->output_offset + sym_sec->output_section->vma;
13056
13057 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13058 {
13059 htab->local_ifunc_resolver = 1;
13060 plt = htab->elf.iplt;
13061 relplt = htab->elf.irelplt;
13062 }
13063 else
13064 {
13065 plt = htab->pltlocal;
13066 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13067 }
13068
13069 if (relplt == NULL)
13070 {
13071 loc = plt->contents + ent->plt.offset;
13072 bfd_put_64 (info->output_bfd, val, loc);
13073 if (htab->opd_abi)
13074 {
13075 bfd_vma toc = elf_gp (ibfd);
13076 bfd_put_64 (info->output_bfd, toc, loc + 8);
13077 }
13078 }
13079 else
13080 {
13081 Elf_Internal_Rela rela;
13082 rela.r_offset = (ent->plt.offset
13083 + plt->output_offset
13084 + plt->output_section->vma);
13085 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13086 {
13087 if (htab->opd_abi)
13088 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13089 else
13090 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13091 }
13092 else
13093 {
13094 if (htab->opd_abi)
13095 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13096 else
13097 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13098 }
13099 rela.r_addend = val;
13100 loc = relplt->contents + (relplt->reloc_count++
13101 * sizeof (Elf64_External_Rela));
13102 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13103 }
13104 }
13105
13106 if (local_syms != NULL
13107 && symtab_hdr->contents != (unsigned char *) local_syms)
13108 {
13109 if (!info->keep_memory)
13110 free (local_syms);
13111 else
13112 symtab_hdr->contents = (unsigned char *) local_syms;
13113 }
13114 }
13115 return TRUE;
13116 }
13117
13118 /* Build all the stubs associated with the current output file.
13119 The stubs are kept in a hash table attached to the main linker
13120 hash table. This function is called via gldelf64ppc_finish. */
13121
13122 bfd_boolean
13123 ppc64_elf_build_stubs (struct bfd_link_info *info,
13124 char **stats)
13125 {
13126 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13127 struct map_stub *group;
13128 asection *stub_sec;
13129 bfd_byte *p;
13130 int stub_sec_count = 0;
13131
13132 if (htab == NULL)
13133 return FALSE;
13134
13135 /* Allocate memory to hold the linker stubs. */
13136 for (group = htab->group; group != NULL; group = group->next)
13137 {
13138 group->eh_size = 0;
13139 group->lr_restore = 0;
13140 if ((stub_sec = group->stub_sec) != NULL
13141 && stub_sec->size != 0)
13142 {
13143 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13144 stub_sec->size);
13145 if (stub_sec->contents == NULL)
13146 return FALSE;
13147 stub_sec->size = 0;
13148 }
13149 }
13150
13151 if (htab->glink != NULL && htab->glink->size != 0)
13152 {
13153 unsigned int indx;
13154 bfd_vma plt0;
13155
13156 /* Build the .glink plt call stub. */
13157 if (htab->params->emit_stub_syms)
13158 {
13159 struct elf_link_hash_entry *h;
13160 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13161 TRUE, FALSE, FALSE);
13162 if (h == NULL)
13163 return FALSE;
13164 if (h->root.type == bfd_link_hash_new)
13165 {
13166 h->root.type = bfd_link_hash_defined;
13167 h->root.u.def.section = htab->glink;
13168 h->root.u.def.value = 8;
13169 h->ref_regular = 1;
13170 h->def_regular = 1;
13171 h->ref_regular_nonweak = 1;
13172 h->forced_local = 1;
13173 h->non_elf = 0;
13174 h->root.linker_def = 1;
13175 }
13176 }
13177 plt0 = (htab->elf.splt->output_section->vma
13178 + htab->elf.splt->output_offset
13179 - 16);
13180 if (info->emitrelocations)
13181 {
13182 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13183 if (r == NULL)
13184 return FALSE;
13185 r->r_offset = (htab->glink->output_offset
13186 + htab->glink->output_section->vma);
13187 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13188 r->r_addend = plt0;
13189 }
13190 p = htab->glink->contents;
13191 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13192 bfd_put_64 (htab->glink->owner, plt0, p);
13193 p += 8;
13194 if (htab->opd_abi)
13195 {
13196 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13197 p += 4;
13198 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13199 p += 4;
13200 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13201 p += 4;
13202 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13203 p += 4;
13204 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13205 p += 4;
13206 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13207 p += 4;
13208 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13209 p += 4;
13210 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13211 p += 4;
13212 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13213 p += 4;
13214 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13215 p += 4;
13216 }
13217 else
13218 {
13219 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13220 p += 4;
13221 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13222 p += 4;
13223 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13224 p += 4;
13225 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13226 p += 4;
13227 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13228 p += 4;
13229 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13230 p += 4;
13231 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13232 p += 4;
13233 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13234 p += 4;
13235 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13236 p += 4;
13237 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13238 p += 4;
13239 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13240 p += 4;
13241 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13242 p += 4;
13243 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13244 p += 4;
13245 }
13246 bfd_put_32 (htab->glink->owner, BCTR, p);
13247 p += 4;
13248 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13249
13250 /* Build the .glink lazy link call stubs. */
13251 indx = 0;
13252 while (p < htab->glink->contents + htab->glink->size)
13253 {
13254 if (htab->opd_abi)
13255 {
13256 if (indx < 0x8000)
13257 {
13258 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13259 p += 4;
13260 }
13261 else
13262 {
13263 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13264 p += 4;
13265 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13266 p);
13267 p += 4;
13268 }
13269 }
13270 bfd_put_32 (htab->glink->owner,
13271 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13272 indx++;
13273 p += 4;
13274 }
13275 }
13276
13277 /* Build .glink global entry stubs, and PLT relocs for globals. */
13278 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13279
13280 if (!write_plt_relocs_for_local_syms (info))
13281 return FALSE;
13282
13283 if (htab->brlt != NULL && htab->brlt->size != 0)
13284 {
13285 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13286 htab->brlt->size);
13287 if (htab->brlt->contents == NULL)
13288 return FALSE;
13289 }
13290 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13291 {
13292 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13293 htab->relbrlt->size);
13294 if (htab->relbrlt->contents == NULL)
13295 return FALSE;
13296 }
13297
13298 /* Build the stubs as directed by the stub hash table. */
13299 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13300
13301 for (group = htab->group; group != NULL; group = group->next)
13302 if (group->needs_save_res)
13303 group->stub_sec->size += htab->sfpr->size;
13304
13305 if (htab->relbrlt != NULL)
13306 htab->relbrlt->reloc_count = 0;
13307
13308 if (htab->params->plt_stub_align != 0)
13309 for (group = htab->group; group != NULL; group = group->next)
13310 if ((stub_sec = group->stub_sec) != NULL)
13311 {
13312 int align = abs (htab->params->plt_stub_align);
13313 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13314 }
13315
13316 for (group = htab->group; group != NULL; group = group->next)
13317 if (group->needs_save_res)
13318 {
13319 stub_sec = group->stub_sec;
13320 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13321 htab->sfpr->contents, htab->sfpr->size);
13322 if (htab->params->emit_stub_syms)
13323 {
13324 unsigned int i;
13325
13326 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13327 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13328 return FALSE;
13329 }
13330 }
13331
13332 if (htab->glink_eh_frame != NULL
13333 && htab->glink_eh_frame->size != 0)
13334 {
13335 bfd_vma val;
13336 size_t align = 4;
13337
13338 p = htab->glink_eh_frame->contents;
13339 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13340
13341 for (group = htab->group; group != NULL; group = group->next)
13342 if (group->eh_size != 0)
13343 {
13344 /* Offset to stub section. */
13345 val = (group->stub_sec->output_section->vma
13346 + group->stub_sec->output_offset);
13347 val -= (htab->glink_eh_frame->output_section->vma
13348 + htab->glink_eh_frame->output_offset
13349 + (p + 8 - htab->glink_eh_frame->contents));
13350 if (val + 0x80000000 > 0xffffffff)
13351 {
13352 _bfd_error_handler
13353 (_("%s offset too large for .eh_frame sdata4 encoding"),
13354 group->stub_sec->name);
13355 return FALSE;
13356 }
13357 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13358 p += (group->eh_size + 17 + 3) & -4;
13359 }
13360 if (htab->glink != NULL && htab->glink->size != 0)
13361 {
13362 /* Offset to .glink. */
13363 val = (htab->glink->output_section->vma
13364 + htab->glink->output_offset
13365 + 8);
13366 val -= (htab->glink_eh_frame->output_section->vma
13367 + htab->glink_eh_frame->output_offset
13368 + (p + 8 - htab->glink_eh_frame->contents));
13369 if (val + 0x80000000 > 0xffffffff)
13370 {
13371 _bfd_error_handler
13372 (_("%s offset too large for .eh_frame sdata4 encoding"),
13373 htab->glink->name);
13374 return FALSE;
13375 }
13376 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13377 p += (24 + align - 1) & -align;
13378 }
13379 }
13380
13381 for (group = htab->group; group != NULL; group = group->next)
13382 if ((stub_sec = group->stub_sec) != NULL)
13383 {
13384 stub_sec_count += 1;
13385 if (stub_sec->rawsize != stub_sec->size
13386 && (htab->stub_iteration <= STUB_SHRINK_ITER
13387 || stub_sec->rawsize < stub_sec->size))
13388 break;
13389 }
13390
13391 if (group != NULL)
13392 {
13393 htab->stub_error = TRUE;
13394 _bfd_error_handler (_("stubs don't match calculated size"));
13395 }
13396
13397 if (htab->stub_error)
13398 return FALSE;
13399
13400 if (stats != NULL)
13401 {
13402 size_t len;
13403 *stats = bfd_malloc (500);
13404 if (*stats == NULL)
13405 return FALSE;
13406
13407 len = sprintf (*stats,
13408 ngettext ("linker stubs in %u group\n",
13409 "linker stubs in %u groups\n",
13410 stub_sec_count),
13411 stub_sec_count);
13412 sprintf (*stats + len, _(" branch %lu\n"
13413 " branch toc adj %lu\n"
13414 " branch notoc %lu\n"
13415 " branch both %lu\n"
13416 " long branch %lu\n"
13417 " long toc adj %lu\n"
13418 " long notoc %lu\n"
13419 " long both %lu\n"
13420 " plt call %lu\n"
13421 " plt call save %lu\n"
13422 " plt call notoc %lu\n"
13423 " plt call both %lu\n"
13424 " global entry %lu"),
13425 htab->stub_count[ppc_stub_long_branch - 1],
13426 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13427 htab->stub_count[ppc_stub_long_branch_notoc - 1],
13428 htab->stub_count[ppc_stub_long_branch_both - 1],
13429 htab->stub_count[ppc_stub_plt_branch - 1],
13430 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13431 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
13432 htab->stub_count[ppc_stub_plt_branch_both - 1],
13433 htab->stub_count[ppc_stub_plt_call - 1],
13434 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13435 htab->stub_count[ppc_stub_plt_call_notoc - 1],
13436 htab->stub_count[ppc_stub_plt_call_both - 1],
13437 htab->stub_count[ppc_stub_global_entry - 1]);
13438 }
13439 return TRUE;
13440 }
13441
13442 /* What to do when ld finds relocations against symbols defined in
13443 discarded sections. */
13444
13445 static unsigned int
13446 ppc64_elf_action_discarded (asection *sec)
13447 {
13448 if (strcmp (".opd", sec->name) == 0)
13449 return 0;
13450
13451 if (strcmp (".toc", sec->name) == 0)
13452 return 0;
13453
13454 if (strcmp (".toc1", sec->name) == 0)
13455 return 0;
13456
13457 return _bfd_elf_default_action_discarded (sec);
13458 }
13459
13460 /* The RELOCATE_SECTION function is called by the ELF backend linker
13461 to handle the relocations for a section.
13462
13463 The relocs are always passed as Rela structures; if the section
13464 actually uses Rel structures, the r_addend field will always be
13465 zero.
13466
13467 This function is responsible for adjust the section contents as
13468 necessary, and (if using Rela relocs and generating a
13469 relocatable output file) adjusting the reloc addend as
13470 necessary.
13471
13472 This function does not have to worry about setting the reloc
13473 address or the reloc symbol index.
13474
13475 LOCAL_SYMS is a pointer to the swapped in local symbols.
13476
13477 LOCAL_SECTIONS is an array giving the section in the input file
13478 corresponding to the st_shndx field of each local symbol.
13479
13480 The global hash table entry for the global symbols can be found
13481 via elf_sym_hashes (input_bfd).
13482
13483 When generating relocatable output, this function must handle
13484 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13485 going to be the section symbol corresponding to the output
13486 section, which means that the addend must be adjusted
13487 accordingly. */
13488
13489 static bfd_boolean
13490 ppc64_elf_relocate_section (bfd *output_bfd,
13491 struct bfd_link_info *info,
13492 bfd *input_bfd,
13493 asection *input_section,
13494 bfd_byte *contents,
13495 Elf_Internal_Rela *relocs,
13496 Elf_Internal_Sym *local_syms,
13497 asection **local_sections)
13498 {
13499 struct ppc_link_hash_table *htab;
13500 Elf_Internal_Shdr *symtab_hdr;
13501 struct elf_link_hash_entry **sym_hashes;
13502 Elf_Internal_Rela *rel;
13503 Elf_Internal_Rela *wrel;
13504 Elf_Internal_Rela *relend;
13505 Elf_Internal_Rela outrel;
13506 bfd_byte *loc;
13507 struct got_entry **local_got_ents;
13508 bfd_vma TOCstart;
13509 bfd_boolean ret = TRUE;
13510 bfd_boolean is_opd;
13511 /* Assume 'at' branch hints. */
13512 bfd_boolean is_isa_v2 = TRUE;
13513 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13514
13515 /* Initialize howto table if needed. */
13516 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13517 ppc_howto_init ();
13518
13519 htab = ppc_hash_table (info);
13520 if (htab == NULL)
13521 return FALSE;
13522
13523 /* Don't relocate stub sections. */
13524 if (input_section->owner == htab->params->stub_bfd)
13525 return TRUE;
13526
13527 if (!is_ppc64_elf (input_bfd))
13528 {
13529 bfd_set_error (bfd_error_wrong_format);
13530 return FALSE;
13531 }
13532
13533 local_got_ents = elf_local_got_ents (input_bfd);
13534 TOCstart = elf_gp (output_bfd);
13535 symtab_hdr = &elf_symtab_hdr (input_bfd);
13536 sym_hashes = elf_sym_hashes (input_bfd);
13537 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13538
13539 rel = wrel = relocs;
13540 relend = relocs + input_section->reloc_count;
13541 for (; rel < relend; wrel++, rel++)
13542 {
13543 enum elf_ppc64_reloc_type r_type;
13544 bfd_vma addend;
13545 bfd_reloc_status_type r;
13546 Elf_Internal_Sym *sym;
13547 asection *sec;
13548 struct elf_link_hash_entry *h_elf;
13549 struct ppc_link_hash_entry *h;
13550 struct ppc_link_hash_entry *fdh;
13551 const char *sym_name;
13552 unsigned long r_symndx, toc_symndx;
13553 bfd_vma toc_addend;
13554 unsigned char tls_mask, tls_gd, tls_type;
13555 unsigned char sym_type;
13556 bfd_vma relocation;
13557 bfd_boolean unresolved_reloc, save_unresolved_reloc;
13558 bfd_boolean warned;
13559 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13560 unsigned int insn;
13561 unsigned int mask;
13562 struct ppc_stub_hash_entry *stub_entry;
13563 bfd_vma max_br_offset;
13564 bfd_vma from;
13565 Elf_Internal_Rela orig_rel;
13566 reloc_howto_type *howto;
13567 struct reloc_howto_struct alt_howto;
13568
13569 again:
13570 orig_rel = *rel;
13571
13572 r_type = ELF64_R_TYPE (rel->r_info);
13573 r_symndx = ELF64_R_SYM (rel->r_info);
13574
13575 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13576 symbol of the previous ADDR64 reloc. The symbol gives us the
13577 proper TOC base to use. */
13578 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13579 && wrel != relocs
13580 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13581 && is_opd)
13582 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13583
13584 sym = NULL;
13585 sec = NULL;
13586 h_elf = NULL;
13587 sym_name = NULL;
13588 unresolved_reloc = FALSE;
13589 warned = FALSE;
13590
13591 if (r_symndx < symtab_hdr->sh_info)
13592 {
13593 /* It's a local symbol. */
13594 struct _opd_sec_data *opd;
13595
13596 sym = local_syms + r_symndx;
13597 sec = local_sections[r_symndx];
13598 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13599 sym_type = ELF64_ST_TYPE (sym->st_info);
13600 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13601 opd = get_opd_info (sec);
13602 if (opd != NULL && opd->adjust != NULL)
13603 {
13604 long adjust = opd->adjust[OPD_NDX (sym->st_value
13605 + rel->r_addend)];
13606 if (adjust == -1)
13607 relocation = 0;
13608 else
13609 {
13610 /* If this is a relocation against the opd section sym
13611 and we have edited .opd, adjust the reloc addend so
13612 that ld -r and ld --emit-relocs output is correct.
13613 If it is a reloc against some other .opd symbol,
13614 then the symbol value will be adjusted later. */
13615 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13616 rel->r_addend += adjust;
13617 else
13618 relocation += adjust;
13619 }
13620 }
13621 }
13622 else
13623 {
13624 bfd_boolean ignored;
13625
13626 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13627 r_symndx, symtab_hdr, sym_hashes,
13628 h_elf, sec, relocation,
13629 unresolved_reloc, warned, ignored);
13630 sym_name = h_elf->root.root.string;
13631 sym_type = h_elf->type;
13632 if (sec != NULL
13633 && sec->owner == output_bfd
13634 && strcmp (sec->name, ".opd") == 0)
13635 {
13636 /* This is a symbol defined in a linker script. All
13637 such are defined in output sections, even those
13638 defined by simple assignment from a symbol defined in
13639 an input section. Transfer the symbol to an
13640 appropriate input .opd section, so that a branch to
13641 this symbol will be mapped to the location specified
13642 by the opd entry. */
13643 struct bfd_link_order *lo;
13644 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13645 if (lo->type == bfd_indirect_link_order)
13646 {
13647 asection *isec = lo->u.indirect.section;
13648 if (h_elf->root.u.def.value >= isec->output_offset
13649 && h_elf->root.u.def.value < (isec->output_offset
13650 + isec->size))
13651 {
13652 h_elf->root.u.def.value -= isec->output_offset;
13653 h_elf->root.u.def.section = isec;
13654 sec = isec;
13655 break;
13656 }
13657 }
13658 }
13659 }
13660 h = (struct ppc_link_hash_entry *) h_elf;
13661
13662 if (sec != NULL && discarded_section (sec))
13663 {
13664 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13665 input_bfd, input_section,
13666 contents, rel->r_offset);
13667 wrel->r_offset = rel->r_offset;
13668 wrel->r_info = 0;
13669 wrel->r_addend = 0;
13670
13671 /* For ld -r, remove relocations in debug sections against
13672 symbols defined in discarded sections. Not done for
13673 non-debug to preserve relocs in .eh_frame which the
13674 eh_frame editing code expects to be present. */
13675 if (bfd_link_relocatable (info)
13676 && (input_section->flags & SEC_DEBUGGING))
13677 wrel--;
13678
13679 continue;
13680 }
13681
13682 if (bfd_link_relocatable (info))
13683 goto copy_reloc;
13684
13685 if (h != NULL && &h->elf == htab->elf.hgot)
13686 {
13687 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13688 sec = bfd_abs_section_ptr;
13689 unresolved_reloc = FALSE;
13690 }
13691
13692 /* TLS optimizations. Replace instruction sequences and relocs
13693 based on information we collected in tls_optimize. We edit
13694 RELOCS so that --emit-relocs will output something sensible
13695 for the final instruction stream. */
13696 tls_mask = 0;
13697 tls_gd = 0;
13698 toc_symndx = 0;
13699 if (h != NULL)
13700 tls_mask = h->tls_mask;
13701 else if (local_got_ents != NULL)
13702 {
13703 struct plt_entry **local_plt = (struct plt_entry **)
13704 (local_got_ents + symtab_hdr->sh_info);
13705 unsigned char *lgot_masks = (unsigned char *)
13706 (local_plt + symtab_hdr->sh_info);
13707 tls_mask = lgot_masks[r_symndx];
13708 }
13709 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
13710 && (r_type == R_PPC64_TLS
13711 || r_type == R_PPC64_TLSGD
13712 || r_type == R_PPC64_TLSLD))
13713 {
13714 /* Check for toc tls entries. */
13715 unsigned char *toc_tls;
13716
13717 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13718 &local_syms, rel, input_bfd))
13719 return FALSE;
13720
13721 if (toc_tls)
13722 tls_mask = *toc_tls;
13723 }
13724
13725 /* Check that tls relocs are used with tls syms, and non-tls
13726 relocs are used with non-tls syms. */
13727 if (r_symndx != STN_UNDEF
13728 && r_type != R_PPC64_NONE
13729 && (h == NULL
13730 || h->elf.root.type == bfd_link_hash_defined
13731 || h->elf.root.type == bfd_link_hash_defweak)
13732 && (IS_PPC64_TLS_RELOC (r_type)
13733 != (sym_type == STT_TLS
13734 || (sym_type == STT_SECTION
13735 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13736 {
13737 if ((tls_mask & TLS_TLS) != 0
13738 && (r_type == R_PPC64_TLS
13739 || r_type == R_PPC64_TLSGD
13740 || r_type == R_PPC64_TLSLD))
13741 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13742 ;
13743 else
13744 info->callbacks->einfo
13745 (!IS_PPC64_TLS_RELOC (r_type)
13746 /* xgettext:c-format */
13747 ? _("%H: %s used with TLS symbol `%pT'\n")
13748 /* xgettext:c-format */
13749 : _("%H: %s used with non-TLS symbol `%pT'\n"),
13750 input_bfd, input_section, rel->r_offset,
13751 ppc64_elf_howto_table[r_type]->name,
13752 sym_name);
13753 }
13754
13755 /* Ensure reloc mapping code below stays sane. */
13756 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13757 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13758 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13759 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13760 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13761 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13762 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13763 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13764 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13765 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13766 abort ();
13767
13768 switch (r_type)
13769 {
13770 default:
13771 break;
13772
13773 case R_PPC64_LO_DS_OPT:
13774 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13775 if ((insn & (0x3f << 26)) != 58u << 26)
13776 abort ();
13777 insn += (14u << 26) - (58u << 26);
13778 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13779 r_type = R_PPC64_TOC16_LO;
13780 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13781 break;
13782
13783 case R_PPC64_TOC16:
13784 case R_PPC64_TOC16_LO:
13785 case R_PPC64_TOC16_DS:
13786 case R_PPC64_TOC16_LO_DS:
13787 {
13788 /* Check for toc tls entries. */
13789 unsigned char *toc_tls;
13790 int retval;
13791
13792 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13793 &local_syms, rel, input_bfd);
13794 if (retval == 0)
13795 return FALSE;
13796
13797 if (toc_tls)
13798 {
13799 tls_mask = *toc_tls;
13800 if (r_type == R_PPC64_TOC16_DS
13801 || r_type == R_PPC64_TOC16_LO_DS)
13802 {
13803 if ((tls_mask & TLS_TLS) != 0
13804 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13805 goto toctprel;
13806 }
13807 else
13808 {
13809 /* If we found a GD reloc pair, then we might be
13810 doing a GD->IE transition. */
13811 if (retval == 2)
13812 {
13813 tls_gd = TLS_TPRELGD;
13814 if ((tls_mask & TLS_TLS) != 0
13815 && (tls_mask & TLS_GD) == 0)
13816 goto tls_ldgd_opt;
13817 }
13818 else if (retval == 3)
13819 {
13820 if ((tls_mask & TLS_TLS) != 0
13821 && (tls_mask & TLS_LD) == 0)
13822 goto tls_ldgd_opt;
13823 }
13824 }
13825 }
13826 }
13827 break;
13828
13829 case R_PPC64_GOT_TPREL16_HI:
13830 case R_PPC64_GOT_TPREL16_HA:
13831 if ((tls_mask & TLS_TLS) != 0
13832 && (tls_mask & TLS_TPREL) == 0)
13833 {
13834 rel->r_offset -= d_offset;
13835 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13836 r_type = R_PPC64_NONE;
13837 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13838 }
13839 break;
13840
13841 case R_PPC64_GOT_TPREL16_DS:
13842 case R_PPC64_GOT_TPREL16_LO_DS:
13843 if ((tls_mask & TLS_TLS) != 0
13844 && (tls_mask & TLS_TPREL) == 0)
13845 {
13846 toctprel:
13847 insn = bfd_get_32 (input_bfd,
13848 contents + rel->r_offset - d_offset);
13849 insn &= 31 << 21;
13850 insn |= 0x3c0d0000; /* addis 0,13,0 */
13851 bfd_put_32 (input_bfd, insn,
13852 contents + rel->r_offset - d_offset);
13853 r_type = R_PPC64_TPREL16_HA;
13854 if (toc_symndx != 0)
13855 {
13856 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13857 rel->r_addend = toc_addend;
13858 /* We changed the symbol. Start over in order to
13859 get h, sym, sec etc. right. */
13860 goto again;
13861 }
13862 else
13863 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13864 }
13865 break;
13866
13867 case R_PPC64_TLS:
13868 if ((tls_mask & TLS_TLS) != 0
13869 && (tls_mask & TLS_TPREL) == 0)
13870 {
13871 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13872 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13873 if (insn == 0)
13874 abort ();
13875 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
13876 /* Was PPC64_TLS which sits on insn boundary, now
13877 PPC64_TPREL16_LO which is at low-order half-word. */
13878 rel->r_offset += d_offset;
13879 r_type = R_PPC64_TPREL16_LO;
13880 if (toc_symndx != 0)
13881 {
13882 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13883 rel->r_addend = toc_addend;
13884 /* We changed the symbol. Start over in order to
13885 get h, sym, sec etc. right. */
13886 goto again;
13887 }
13888 else
13889 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13890 }
13891 break;
13892
13893 case R_PPC64_GOT_TLSGD16_HI:
13894 case R_PPC64_GOT_TLSGD16_HA:
13895 tls_gd = TLS_TPRELGD;
13896 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
13897 goto tls_gdld_hi;
13898 break;
13899
13900 case R_PPC64_GOT_TLSLD16_HI:
13901 case R_PPC64_GOT_TLSLD16_HA:
13902 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
13903 {
13904 tls_gdld_hi:
13905 if ((tls_mask & tls_gd) != 0)
13906 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13907 + R_PPC64_GOT_TPREL16_DS);
13908 else
13909 {
13910 rel->r_offset -= d_offset;
13911 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13912 r_type = R_PPC64_NONE;
13913 }
13914 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13915 }
13916 break;
13917
13918 case R_PPC64_GOT_TLSGD16:
13919 case R_PPC64_GOT_TLSGD16_LO:
13920 tls_gd = TLS_TPRELGD;
13921 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
13922 goto tls_ldgd_opt;
13923 break;
13924
13925 case R_PPC64_GOT_TLSLD16:
13926 case R_PPC64_GOT_TLSLD16_LO:
13927 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
13928 {
13929 unsigned int insn1, insn2;
13930 bfd_vma offset;
13931
13932 tls_ldgd_opt:
13933 offset = (bfd_vma) -1;
13934 /* If not using the newer R_PPC64_TLSGD/LD to mark
13935 __tls_get_addr calls, we must trust that the call
13936 stays with its arg setup insns, ie. that the next
13937 reloc is the __tls_get_addr call associated with
13938 the current reloc. Edit both insns. */
13939 if (input_section->has_tls_get_addr_call
13940 && rel + 1 < relend
13941 && branch_reloc_hash_match (input_bfd, rel + 1,
13942 htab->tls_get_addr,
13943 htab->tls_get_addr_fd))
13944 offset = rel[1].r_offset;
13945 /* We read the low GOT_TLS (or TOC16) insn because we
13946 need to keep the destination reg. It may be
13947 something other than the usual r3, and moved to r3
13948 before the call by intervening code. */
13949 insn1 = bfd_get_32 (input_bfd,
13950 contents + rel->r_offset - d_offset);
13951 if ((tls_mask & tls_gd) != 0)
13952 {
13953 /* IE */
13954 insn1 &= (0x1f << 21) | (0x1f << 16);
13955 insn1 |= 58 << 26; /* ld */
13956 insn2 = 0x7c636a14; /* add 3,3,13 */
13957 if (offset != (bfd_vma) -1)
13958 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13959 if ((tls_mask & TLS_EXPLICIT) == 0)
13960 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13961 + R_PPC64_GOT_TPREL16_DS);
13962 else
13963 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13964 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13965 }
13966 else
13967 {
13968 /* LE */
13969 insn1 &= 0x1f << 21;
13970 insn1 |= 0x3c0d0000; /* addis r,13,0 */
13971 insn2 = 0x38630000; /* addi 3,3,0 */
13972 if (tls_gd == 0)
13973 {
13974 /* Was an LD reloc. */
13975 if (toc_symndx)
13976 sec = local_sections[toc_symndx];
13977 for (r_symndx = 0;
13978 r_symndx < symtab_hdr->sh_info;
13979 r_symndx++)
13980 if (local_sections[r_symndx] == sec)
13981 break;
13982 if (r_symndx >= symtab_hdr->sh_info)
13983 r_symndx = STN_UNDEF;
13984 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13985 if (r_symndx != STN_UNDEF)
13986 rel->r_addend -= (local_syms[r_symndx].st_value
13987 + sec->output_offset
13988 + sec->output_section->vma);
13989 }
13990 else if (toc_symndx != 0)
13991 {
13992 r_symndx = toc_symndx;
13993 rel->r_addend = toc_addend;
13994 }
13995 r_type = R_PPC64_TPREL16_HA;
13996 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13997 if (offset != (bfd_vma) -1)
13998 {
13999 rel[1].r_info = ELF64_R_INFO (r_symndx,
14000 R_PPC64_TPREL16_LO);
14001 rel[1].r_offset = offset + d_offset;
14002 rel[1].r_addend = rel->r_addend;
14003 }
14004 }
14005 bfd_put_32 (input_bfd, insn1,
14006 contents + rel->r_offset - d_offset);
14007 if (offset != (bfd_vma) -1)
14008 {
14009 bfd_put_32 (input_bfd, insn2, contents + offset);
14010 if (offset + 8 <= input_section->size)
14011 {
14012 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14013 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14014 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14015 }
14016 }
14017 if ((tls_mask & tls_gd) == 0
14018 && (tls_gd == 0 || toc_symndx != 0))
14019 {
14020 /* We changed the symbol. Start over in order
14021 to get h, sym, sec etc. right. */
14022 goto again;
14023 }
14024 }
14025 break;
14026
14027 case R_PPC64_TLSGD:
14028 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14029 && rel + 1 < relend)
14030 {
14031 unsigned int insn2;
14032 bfd_vma offset = rel->r_offset;
14033
14034 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14035 {
14036 bfd_put_32 (output_bfd, NOP, contents + offset);
14037 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14038 break;
14039 }
14040
14041 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14042 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14043
14044 if ((tls_mask & TLS_TPRELGD) != 0)
14045 {
14046 /* IE */
14047 r_type = R_PPC64_NONE;
14048 insn2 = 0x7c636a14; /* add 3,3,13 */
14049 }
14050 else
14051 {
14052 /* LE */
14053 if (toc_symndx != 0)
14054 {
14055 r_symndx = toc_symndx;
14056 rel->r_addend = toc_addend;
14057 }
14058 r_type = R_PPC64_TPREL16_LO;
14059 rel->r_offset = offset + d_offset;
14060 insn2 = 0x38630000; /* addi 3,3,0 */
14061 }
14062 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14063 /* Zap the reloc on the _tls_get_addr call too. */
14064 BFD_ASSERT (offset == rel[1].r_offset);
14065 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14066 bfd_put_32 (input_bfd, insn2, contents + offset);
14067 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
14068 goto again;
14069 }
14070 break;
14071
14072 case R_PPC64_TLSLD:
14073 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14074 && rel + 1 < relend)
14075 {
14076 unsigned int insn2;
14077 bfd_vma offset = rel->r_offset;
14078
14079 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14080 {
14081 bfd_put_32 (output_bfd, NOP, contents + offset);
14082 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14083 break;
14084 }
14085
14086 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14087 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14088
14089 if (toc_symndx)
14090 sec = local_sections[toc_symndx];
14091 for (r_symndx = 0;
14092 r_symndx < symtab_hdr->sh_info;
14093 r_symndx++)
14094 if (local_sections[r_symndx] == sec)
14095 break;
14096 if (r_symndx >= symtab_hdr->sh_info)
14097 r_symndx = STN_UNDEF;
14098 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14099 if (r_symndx != STN_UNDEF)
14100 rel->r_addend -= (local_syms[r_symndx].st_value
14101 + sec->output_offset
14102 + sec->output_section->vma);
14103
14104 r_type = R_PPC64_TPREL16_LO;
14105 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14106 rel->r_offset = offset + d_offset;
14107 /* Zap the reloc on the _tls_get_addr call too. */
14108 BFD_ASSERT (offset == rel[1].r_offset);
14109 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14110 insn2 = 0x38630000; /* addi 3,3,0 */
14111 bfd_put_32 (input_bfd, insn2, contents + offset);
14112 goto again;
14113 }
14114 break;
14115
14116 case R_PPC64_DTPMOD64:
14117 if (rel + 1 < relend
14118 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14119 && rel[1].r_offset == rel->r_offset + 8)
14120 {
14121 if ((tls_mask & TLS_GD) == 0)
14122 {
14123 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14124 if ((tls_mask & TLS_TPRELGD) != 0)
14125 r_type = R_PPC64_TPREL64;
14126 else
14127 {
14128 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14129 r_type = R_PPC64_NONE;
14130 }
14131 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14132 }
14133 }
14134 else
14135 {
14136 if ((tls_mask & TLS_LD) == 0)
14137 {
14138 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14139 r_type = R_PPC64_NONE;
14140 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14141 }
14142 }
14143 break;
14144
14145 case R_PPC64_TPREL64:
14146 if ((tls_mask & TLS_TPREL) == 0)
14147 {
14148 r_type = R_PPC64_NONE;
14149 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14150 }
14151 break;
14152
14153 case R_PPC64_ENTRY:
14154 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14155 if (!bfd_link_pic (info)
14156 && !info->traditional_format
14157 && relocation + 0x80008000 <= 0xffffffff)
14158 {
14159 unsigned int insn1, insn2;
14160
14161 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14162 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14163 if ((insn1 & ~0xfffc) == LD_R2_0R12
14164 && insn2 == ADD_R2_R2_R12)
14165 {
14166 bfd_put_32 (input_bfd,
14167 LIS_R2 + PPC_HA (relocation),
14168 contents + rel->r_offset);
14169 bfd_put_32 (input_bfd,
14170 ADDI_R2_R2 + PPC_LO (relocation),
14171 contents + rel->r_offset + 4);
14172 }
14173 }
14174 else
14175 {
14176 relocation -= (rel->r_offset
14177 + input_section->output_offset
14178 + input_section->output_section->vma);
14179 if (relocation + 0x80008000 <= 0xffffffff)
14180 {
14181 unsigned int insn1, insn2;
14182
14183 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14184 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14185 if ((insn1 & ~0xfffc) == LD_R2_0R12
14186 && insn2 == ADD_R2_R2_R12)
14187 {
14188 bfd_put_32 (input_bfd,
14189 ADDIS_R2_R12 + PPC_HA (relocation),
14190 contents + rel->r_offset);
14191 bfd_put_32 (input_bfd,
14192 ADDI_R2_R2 + PPC_LO (relocation),
14193 contents + rel->r_offset + 4);
14194 }
14195 }
14196 }
14197 break;
14198
14199 case R_PPC64_REL16_HA:
14200 /* If we are generating a non-PIC executable, edit
14201 . 0: addis 2,12,.TOC.-0b@ha
14202 . addi 2,2,.TOC.-0b@l
14203 used by ELFv2 global entry points to set up r2, to
14204 . lis 2,.TOC.@ha
14205 . addi 2,2,.TOC.@l
14206 if .TOC. is in range. */
14207 if (!bfd_link_pic (info)
14208 && !info->traditional_format
14209 && !htab->opd_abi
14210 && rel->r_addend == d_offset
14211 && h != NULL && &h->elf == htab->elf.hgot
14212 && rel + 1 < relend
14213 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14214 && rel[1].r_offset == rel->r_offset + 4
14215 && rel[1].r_addend == rel->r_addend + 4
14216 && relocation + 0x80008000 <= 0xffffffff)
14217 {
14218 unsigned int insn1, insn2;
14219 bfd_vma offset = rel->r_offset - d_offset;
14220 insn1 = bfd_get_32 (input_bfd, contents + offset);
14221 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14222 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14223 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14224 {
14225 r_type = R_PPC64_ADDR16_HA;
14226 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14227 rel->r_addend -= d_offset;
14228 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14229 rel[1].r_addend -= d_offset + 4;
14230 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14231 }
14232 }
14233 break;
14234 }
14235
14236 /* Handle other relocations that tweak non-addend part of insn. */
14237 insn = 0;
14238 max_br_offset = 1 << 25;
14239 addend = rel->r_addend;
14240 reloc_dest = DEST_NORMAL;
14241 switch (r_type)
14242 {
14243 default:
14244 break;
14245
14246 case R_PPC64_TOCSAVE:
14247 if (relocation + addend == (rel->r_offset
14248 + input_section->output_offset
14249 + input_section->output_section->vma)
14250 && tocsave_find (htab, NO_INSERT,
14251 &local_syms, rel, input_bfd))
14252 {
14253 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14254 if (insn == NOP
14255 || insn == CROR_151515 || insn == CROR_313131)
14256 bfd_put_32 (input_bfd,
14257 STD_R2_0R1 + STK_TOC (htab),
14258 contents + rel->r_offset);
14259 }
14260 break;
14261
14262 /* Branch taken prediction relocations. */
14263 case R_PPC64_ADDR14_BRTAKEN:
14264 case R_PPC64_REL14_BRTAKEN:
14265 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14266 /* Fall through. */
14267
14268 /* Branch not taken prediction relocations. */
14269 case R_PPC64_ADDR14_BRNTAKEN:
14270 case R_PPC64_REL14_BRNTAKEN:
14271 insn |= bfd_get_32 (input_bfd,
14272 contents + rel->r_offset) & ~(0x01 << 21);
14273 /* Fall through. */
14274
14275 case R_PPC64_REL14:
14276 max_br_offset = 1 << 15;
14277 /* Fall through. */
14278
14279 case R_PPC64_REL24:
14280 case R_PPC64_REL24_NOTOC:
14281 case R_PPC64_PLTCALL:
14282 /* Calls to functions with a different TOC, such as calls to
14283 shared objects, need to alter the TOC pointer. This is
14284 done using a linkage stub. A REL24 branching to these
14285 linkage stubs needs to be followed by a nop, as the nop
14286 will be replaced with an instruction to restore the TOC
14287 base pointer. */
14288 fdh = h;
14289 if (h != NULL
14290 && h->oh != NULL
14291 && h->oh->is_func_descriptor)
14292 fdh = ppc_follow_link (h->oh);
14293 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14294 htab);
14295 if (r_type == R_PPC64_PLTCALL
14296 && stub_entry != NULL
14297 && stub_entry->stub_type >= ppc_stub_plt_call
14298 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14299 stub_entry = NULL;
14300
14301 if (stub_entry != NULL
14302 && ((stub_entry->stub_type >= ppc_stub_plt_call
14303 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14304 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14305 || stub_entry->stub_type == ppc_stub_plt_branch_both
14306 || stub_entry->stub_type == ppc_stub_long_branch_r2off
14307 || stub_entry->stub_type == ppc_stub_long_branch_both))
14308 {
14309 bfd_boolean can_plt_call = FALSE;
14310
14311 if (stub_entry->stub_type == ppc_stub_plt_call
14312 && !htab->opd_abi
14313 && htab->params->plt_localentry0 != 0
14314 && is_elfv2_localentry0 (&h->elf))
14315 {
14316 /* The function doesn't use or change r2. */
14317 can_plt_call = TRUE;
14318 }
14319 else if (r_type == R_PPC64_REL24_NOTOC)
14320 {
14321 /* NOTOC calls don't need to restore r2. */
14322 can_plt_call = TRUE;
14323 }
14324
14325 /* All of these stubs may modify r2, so there must be a
14326 branch and link followed by a nop. The nop is
14327 replaced by an insn to restore r2. */
14328 else if (rel->r_offset + 8 <= input_section->size)
14329 {
14330 unsigned long br;
14331
14332 br = bfd_get_32 (input_bfd,
14333 contents + rel->r_offset);
14334 if ((br & 1) != 0)
14335 {
14336 unsigned long nop;
14337
14338 nop = bfd_get_32 (input_bfd,
14339 contents + rel->r_offset + 4);
14340 if (nop == LD_R2_0R1 + STK_TOC (htab))
14341 can_plt_call = TRUE;
14342 else if (nop == NOP
14343 || nop == CROR_151515
14344 || nop == CROR_313131)
14345 {
14346 if (h != NULL
14347 && (h == htab->tls_get_addr_fd
14348 || h == htab->tls_get_addr)
14349 && htab->params->tls_get_addr_opt)
14350 {
14351 /* Special stub used, leave nop alone. */
14352 }
14353 else
14354 bfd_put_32 (input_bfd,
14355 LD_R2_0R1 + STK_TOC (htab),
14356 contents + rel->r_offset + 4);
14357 can_plt_call = TRUE;
14358 }
14359 }
14360 }
14361
14362 if (!can_plt_call && h != NULL)
14363 {
14364 const char *name = h->elf.root.root.string;
14365
14366 if (*name == '.')
14367 ++name;
14368
14369 if (strncmp (name, "__libc_start_main", 17) == 0
14370 && (name[17] == 0 || name[17] == '@'))
14371 {
14372 /* Allow crt1 branch to go via a toc adjusting
14373 stub. Other calls that never return could do
14374 the same, if we could detect such. */
14375 can_plt_call = TRUE;
14376 }
14377 }
14378
14379 if (!can_plt_call)
14380 {
14381 /* g++ as of 20130507 emits self-calls without a
14382 following nop. This is arguably wrong since we
14383 have conflicting information. On the one hand a
14384 global symbol and on the other a local call
14385 sequence, but don't error for this special case.
14386 It isn't possible to cheaply verify we have
14387 exactly such a call. Allow all calls to the same
14388 section. */
14389 asection *code_sec = sec;
14390
14391 if (get_opd_info (sec) != NULL)
14392 {
14393 bfd_vma off = (relocation + addend
14394 - sec->output_section->vma
14395 - sec->output_offset);
14396
14397 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14398 }
14399 if (code_sec == input_section)
14400 can_plt_call = TRUE;
14401 }
14402
14403 if (!can_plt_call)
14404 {
14405 if (stub_entry->stub_type >= ppc_stub_plt_call
14406 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14407 info->callbacks->einfo
14408 /* xgettext:c-format */
14409 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14410 "(plt call stub)\n"),
14411 input_bfd, input_section, rel->r_offset, sym_name);
14412 else
14413 info->callbacks->einfo
14414 /* xgettext:c-format */
14415 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14416 "(toc save/adjust stub)\n"),
14417 input_bfd, input_section, rel->r_offset, sym_name);
14418
14419 bfd_set_error (bfd_error_bad_value);
14420 ret = FALSE;
14421 }
14422
14423 if (can_plt_call
14424 && stub_entry->stub_type >= ppc_stub_plt_call
14425 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14426 unresolved_reloc = FALSE;
14427 }
14428
14429 if ((stub_entry == NULL
14430 || stub_entry->stub_type == ppc_stub_long_branch
14431 || stub_entry->stub_type == ppc_stub_plt_branch)
14432 && get_opd_info (sec) != NULL)
14433 {
14434 /* The branch destination is the value of the opd entry. */
14435 bfd_vma off = (relocation + addend
14436 - sec->output_section->vma
14437 - sec->output_offset);
14438 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14439 if (dest != (bfd_vma) -1)
14440 {
14441 relocation = dest;
14442 addend = 0;
14443 reloc_dest = DEST_OPD;
14444 }
14445 }
14446
14447 /* If the branch is out of reach we ought to have a long
14448 branch stub. */
14449 from = (rel->r_offset
14450 + input_section->output_offset
14451 + input_section->output_section->vma);
14452
14453 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14454 ? fdh->elf.other
14455 : sym->st_other);
14456
14457 if (stub_entry != NULL
14458 && (stub_entry->stub_type == ppc_stub_long_branch
14459 || stub_entry->stub_type == ppc_stub_plt_branch)
14460 && (r_type == R_PPC64_ADDR14_BRTAKEN
14461 || r_type == R_PPC64_ADDR14_BRNTAKEN
14462 || (relocation + addend - from + max_br_offset
14463 < 2 * max_br_offset)))
14464 /* Don't use the stub if this branch is in range. */
14465 stub_entry = NULL;
14466
14467 if (stub_entry != NULL
14468 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
14469 || stub_entry->stub_type == ppc_stub_long_branch_both
14470 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
14471 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14472 && (r_type != R_PPC64_REL24_NOTOC
14473 || ((fdh ? fdh->elf.other : sym->st_other)
14474 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
14475 && (relocation + addend - from + max_br_offset
14476 < 2 * max_br_offset))
14477 stub_entry = NULL;
14478
14479 if (stub_entry != NULL
14480 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
14481 || stub_entry->stub_type == ppc_stub_long_branch_both
14482 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14483 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14484 && r_type == R_PPC64_REL24_NOTOC
14485 && (relocation + addend - from + max_br_offset
14486 < 2 * max_br_offset))
14487 stub_entry = NULL;
14488
14489 if (stub_entry != NULL)
14490 {
14491 /* Munge up the value and addend so that we call the stub
14492 rather than the procedure directly. */
14493 asection *stub_sec = stub_entry->group->stub_sec;
14494
14495 if (stub_entry->stub_type == ppc_stub_save_res)
14496 relocation += (stub_sec->output_offset
14497 + stub_sec->output_section->vma
14498 + stub_sec->size - htab->sfpr->size
14499 - htab->sfpr->output_offset
14500 - htab->sfpr->output_section->vma);
14501 else
14502 relocation = (stub_entry->stub_offset
14503 + stub_sec->output_offset
14504 + stub_sec->output_section->vma);
14505 addend = 0;
14506 reloc_dest = DEST_STUB;
14507
14508 if (((stub_entry->stub_type == ppc_stub_plt_call
14509 && ALWAYS_EMIT_R2SAVE)
14510 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14511 || stub_entry->stub_type == ppc_stub_plt_call_both)
14512 && !(h != NULL
14513 && (h == htab->tls_get_addr_fd
14514 || h == htab->tls_get_addr)
14515 && htab->params->tls_get_addr_opt)
14516 && rel + 1 < relend
14517 && rel[1].r_offset == rel->r_offset + 4
14518 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14519 relocation += 4;
14520 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
14521 || stub_entry->stub_type == ppc_stub_plt_branch_both
14522 || stub_entry->stub_type == ppc_stub_plt_call_both)
14523 && r_type == R_PPC64_REL24_NOTOC)
14524 relocation += 4;
14525 }
14526
14527 if (insn != 0)
14528 {
14529 if (is_isa_v2)
14530 {
14531 /* Set 'a' bit. This is 0b00010 in BO field for branch
14532 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14533 for branch on CTR insns (BO == 1a00t or 1a01t). */
14534 if ((insn & (0x14 << 21)) == (0x04 << 21))
14535 insn |= 0x02 << 21;
14536 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14537 insn |= 0x08 << 21;
14538 else
14539 break;
14540 }
14541 else
14542 {
14543 /* Invert 'y' bit if not the default. */
14544 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14545 insn ^= 0x01 << 21;
14546 }
14547
14548 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14549 }
14550
14551 /* NOP out calls to undefined weak functions.
14552 We can thus call a weak function without first
14553 checking whether the function is defined. */
14554 else if (h != NULL
14555 && h->elf.root.type == bfd_link_hash_undefweak
14556 && h->elf.dynindx == -1
14557 && (r_type == R_PPC64_REL24
14558 || r_type == R_PPC64_REL24_NOTOC)
14559 && relocation == 0
14560 && addend == 0)
14561 {
14562 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14563 goto copy_reloc;
14564 }
14565 break;
14566
14567 case R_PPC64_GOT16_DS:
14568 from = TOCstart + htab->sec_info[input_section->id].toc_off;
14569 if (relocation + addend - from + 0x8000 < 0x10000
14570 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14571 {
14572 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14573 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
14574 {
14575 insn += (14u << 26) - (58u << 26);
14576 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
14577 r_type = R_PPC64_TOC16;
14578 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14579 }
14580 }
14581 break;
14582
14583 case R_PPC64_GOT16_LO_DS:
14584 case R_PPC64_GOT16_HA:
14585 from = TOCstart + htab->sec_info[input_section->id].toc_off;
14586 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
14587 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14588 {
14589 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14590 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
14591 {
14592 insn += (14u << 26) - (58u << 26);
14593 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
14594 r_type = R_PPC64_TOC16_LO;
14595 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14596 }
14597 else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
14598 {
14599 r_type = R_PPC64_TOC16_HA;
14600 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14601 }
14602 }
14603 break;
14604 }
14605
14606 /* Set `addend'. */
14607 tls_type = 0;
14608 save_unresolved_reloc = unresolved_reloc;
14609 switch (r_type)
14610 {
14611 default:
14612 /* xgettext:c-format */
14613 _bfd_error_handler (_("%pB: %s unsupported"),
14614 input_bfd, ppc64_elf_howto_table[r_type]->name);
14615
14616 bfd_set_error (bfd_error_bad_value);
14617 ret = FALSE;
14618 goto copy_reloc;
14619
14620 case R_PPC64_NONE:
14621 case R_PPC64_TLS:
14622 case R_PPC64_TLSGD:
14623 case R_PPC64_TLSLD:
14624 case R_PPC64_TOCSAVE:
14625 case R_PPC64_GNU_VTINHERIT:
14626 case R_PPC64_GNU_VTENTRY:
14627 case R_PPC64_ENTRY:
14628 goto copy_reloc;
14629
14630 /* GOT16 relocations. Like an ADDR16 using the symbol's
14631 address in the GOT as relocation value instead of the
14632 symbol's value itself. Also, create a GOT entry for the
14633 symbol and put the symbol value there. */
14634 case R_PPC64_GOT_TLSGD16:
14635 case R_PPC64_GOT_TLSGD16_LO:
14636 case R_PPC64_GOT_TLSGD16_HI:
14637 case R_PPC64_GOT_TLSGD16_HA:
14638 tls_type = TLS_TLS | TLS_GD;
14639 goto dogot;
14640
14641 case R_PPC64_GOT_TLSLD16:
14642 case R_PPC64_GOT_TLSLD16_LO:
14643 case R_PPC64_GOT_TLSLD16_HI:
14644 case R_PPC64_GOT_TLSLD16_HA:
14645 tls_type = TLS_TLS | TLS_LD;
14646 goto dogot;
14647
14648 case R_PPC64_GOT_TPREL16_DS:
14649 case R_PPC64_GOT_TPREL16_LO_DS:
14650 case R_PPC64_GOT_TPREL16_HI:
14651 case R_PPC64_GOT_TPREL16_HA:
14652 tls_type = TLS_TLS | TLS_TPREL;
14653 goto dogot;
14654
14655 case R_PPC64_GOT_DTPREL16_DS:
14656 case R_PPC64_GOT_DTPREL16_LO_DS:
14657 case R_PPC64_GOT_DTPREL16_HI:
14658 case R_PPC64_GOT_DTPREL16_HA:
14659 tls_type = TLS_TLS | TLS_DTPREL;
14660 goto dogot;
14661
14662 case R_PPC64_GOT16:
14663 case R_PPC64_GOT16_LO:
14664 case R_PPC64_GOT16_HI:
14665 case R_PPC64_GOT16_HA:
14666 case R_PPC64_GOT16_DS:
14667 case R_PPC64_GOT16_LO_DS:
14668 dogot:
14669 {
14670 /* Relocation is to the entry for this symbol in the global
14671 offset table. */
14672 asection *got;
14673 bfd_vma *offp;
14674 bfd_vma off;
14675 unsigned long indx = 0;
14676 struct got_entry *ent;
14677
14678 if (tls_type == (TLS_TLS | TLS_LD)
14679 && (h == NULL
14680 || !h->elf.def_dynamic))
14681 ent = ppc64_tlsld_got (input_bfd);
14682 else
14683 {
14684 if (h != NULL)
14685 {
14686 if (!htab->elf.dynamic_sections_created
14687 || h->elf.dynindx == -1
14688 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14689 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
14690 /* This is actually a static link, or it is a
14691 -Bsymbolic link and the symbol is defined
14692 locally, or the symbol was forced to be local
14693 because of a version file. */
14694 ;
14695 else
14696 {
14697 indx = h->elf.dynindx;
14698 unresolved_reloc = FALSE;
14699 }
14700 ent = h->elf.got.glist;
14701 }
14702 else
14703 {
14704 if (local_got_ents == NULL)
14705 abort ();
14706 ent = local_got_ents[r_symndx];
14707 }
14708
14709 for (; ent != NULL; ent = ent->next)
14710 if (ent->addend == orig_rel.r_addend
14711 && ent->owner == input_bfd
14712 && ent->tls_type == tls_type)
14713 break;
14714 }
14715
14716 if (ent == NULL)
14717 abort ();
14718 if (ent->is_indirect)
14719 ent = ent->got.ent;
14720 offp = &ent->got.offset;
14721 got = ppc64_elf_tdata (ent->owner)->got;
14722 if (got == NULL)
14723 abort ();
14724
14725 /* The offset must always be a multiple of 8. We use the
14726 least significant bit to record whether we have already
14727 processed this entry. */
14728 off = *offp;
14729 if ((off & 1) != 0)
14730 off &= ~1;
14731 else
14732 {
14733 /* Generate relocs for the dynamic linker, except in
14734 the case of TLSLD where we'll use one entry per
14735 module. */
14736 asection *relgot;
14737 bfd_boolean ifunc;
14738
14739 *offp = off | 1;
14740 relgot = NULL;
14741 ifunc = (h != NULL
14742 ? h->elf.type == STT_GNU_IFUNC
14743 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14744 if (ifunc)
14745 {
14746 relgot = htab->elf.irelplt;
14747 if (indx == 0)
14748 htab->local_ifunc_resolver = 1;
14749 else if (is_static_defined (&h->elf))
14750 htab->maybe_local_ifunc_resolver = 1;
14751 }
14752 else if (indx != 0
14753 || (bfd_link_pic (info)
14754 && (h == NULL
14755 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
14756 || (tls_type == (TLS_TLS | TLS_LD)
14757 && !h->elf.def_dynamic))
14758 && !(tls_type == (TLS_TLS | TLS_TPREL)
14759 && bfd_link_executable (info)
14760 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
14761 relgot = ppc64_elf_tdata (ent->owner)->relgot;
14762 if (relgot != NULL)
14763 {
14764 outrel.r_offset = (got->output_section->vma
14765 + got->output_offset
14766 + off);
14767 outrel.r_addend = addend;
14768 if (tls_type & (TLS_LD | TLS_GD))
14769 {
14770 outrel.r_addend = 0;
14771 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14772 if (tls_type == (TLS_TLS | TLS_GD))
14773 {
14774 loc = relgot->contents;
14775 loc += (relgot->reloc_count++
14776 * sizeof (Elf64_External_Rela));
14777 bfd_elf64_swap_reloca_out (output_bfd,
14778 &outrel, loc);
14779 outrel.r_offset += 8;
14780 outrel.r_addend = addend;
14781 outrel.r_info
14782 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14783 }
14784 }
14785 else if (tls_type == (TLS_TLS | TLS_DTPREL))
14786 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14787 else if (tls_type == (TLS_TLS | TLS_TPREL))
14788 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14789 else if (indx != 0)
14790 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14791 else
14792 {
14793 if (ifunc)
14794 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14795 else
14796 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14797
14798 /* Write the .got section contents for the sake
14799 of prelink. */
14800 loc = got->contents + off;
14801 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14802 loc);
14803 }
14804
14805 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14806 {
14807 outrel.r_addend += relocation;
14808 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14809 {
14810 if (htab->elf.tls_sec == NULL)
14811 outrel.r_addend = 0;
14812 else
14813 outrel.r_addend -= htab->elf.tls_sec->vma;
14814 }
14815 }
14816 loc = relgot->contents;
14817 loc += (relgot->reloc_count++
14818 * sizeof (Elf64_External_Rela));
14819 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14820 }
14821
14822 /* Init the .got section contents here if we're not
14823 emitting a reloc. */
14824 else
14825 {
14826 relocation += addend;
14827 if (tls_type != 0)
14828 {
14829 if (htab->elf.tls_sec == NULL)
14830 relocation = 0;
14831 else
14832 {
14833 if (tls_type & TLS_LD)
14834 relocation = 0;
14835 else
14836 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14837 if (tls_type & TLS_TPREL)
14838 relocation += DTP_OFFSET - TP_OFFSET;
14839 }
14840
14841 if (tls_type & (TLS_GD | TLS_LD))
14842 {
14843 bfd_put_64 (output_bfd, relocation,
14844 got->contents + off + 8);
14845 relocation = 1;
14846 }
14847 }
14848 bfd_put_64 (output_bfd, relocation,
14849 got->contents + off);
14850 }
14851 }
14852
14853 if (off >= (bfd_vma) -2)
14854 abort ();
14855
14856 relocation = got->output_section->vma + got->output_offset + off;
14857 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14858 }
14859 break;
14860
14861 case R_PPC64_PLT16_HA:
14862 case R_PPC64_PLT16_HI:
14863 case R_PPC64_PLT16_LO:
14864 case R_PPC64_PLT16_LO_DS:
14865 case R_PPC64_PLT32:
14866 case R_PPC64_PLT64:
14867 case R_PPC64_PLTSEQ:
14868 case R_PPC64_PLTCALL:
14869 /* Relocation is to the entry for this symbol in the
14870 procedure linkage table. */
14871 unresolved_reloc = TRUE;
14872 {
14873 struct plt_entry **plt_list = NULL;
14874 if (h != NULL)
14875 plt_list = &h->elf.plt.plist;
14876 else if (local_got_ents != NULL)
14877 {
14878 struct plt_entry **local_plt = (struct plt_entry **)
14879 (local_got_ents + symtab_hdr->sh_info);
14880 plt_list = local_plt + r_symndx;
14881 }
14882 if (plt_list)
14883 {
14884 struct plt_entry *ent;
14885
14886 for (ent = *plt_list; ent != NULL; ent = ent->next)
14887 if (ent->plt.offset != (bfd_vma) -1
14888 && ent->addend == orig_rel.r_addend)
14889 {
14890 asection *plt;
14891 bfd_vma got;
14892
14893 plt = htab->elf.splt;
14894 if (!htab->elf.dynamic_sections_created
14895 || h == NULL
14896 || h->elf.dynindx == -1)
14897 {
14898 if (h != NULL
14899 ? h->elf.type == STT_GNU_IFUNC
14900 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14901 plt = htab->elf.iplt;
14902 else
14903 plt = htab->pltlocal;
14904 }
14905 relocation = (plt->output_section->vma
14906 + plt->output_offset
14907 + ent->plt.offset);
14908 if (r_type == R_PPC64_PLT16_HA
14909 || r_type == R_PPC64_PLT16_HI
14910 || r_type == R_PPC64_PLT16_LO
14911 || r_type == R_PPC64_PLT16_LO_DS)
14912 {
14913 got = (elf_gp (output_bfd)
14914 + htab->sec_info[input_section->id].toc_off);
14915 relocation -= got;
14916 }
14917 addend = 0;
14918 unresolved_reloc = FALSE;
14919 break;
14920 }
14921 }
14922 }
14923 break;
14924
14925 case R_PPC64_TOC:
14926 /* Relocation value is TOC base. */
14927 relocation = TOCstart;
14928 if (r_symndx == STN_UNDEF)
14929 relocation += htab->sec_info[input_section->id].toc_off;
14930 else if (unresolved_reloc)
14931 ;
14932 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14933 relocation += htab->sec_info[sec->id].toc_off;
14934 else
14935 unresolved_reloc = TRUE;
14936 goto dodyn;
14937
14938 /* TOC16 relocs. We want the offset relative to the TOC base,
14939 which is the address of the start of the TOC plus 0x8000.
14940 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14941 in this order. */
14942 case R_PPC64_TOC16:
14943 case R_PPC64_TOC16_LO:
14944 case R_PPC64_TOC16_HI:
14945 case R_PPC64_TOC16_DS:
14946 case R_PPC64_TOC16_LO_DS:
14947 case R_PPC64_TOC16_HA:
14948 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14949 break;
14950
14951 /* Relocate against the beginning of the section. */
14952 case R_PPC64_SECTOFF:
14953 case R_PPC64_SECTOFF_LO:
14954 case R_PPC64_SECTOFF_HI:
14955 case R_PPC64_SECTOFF_DS:
14956 case R_PPC64_SECTOFF_LO_DS:
14957 case R_PPC64_SECTOFF_HA:
14958 if (sec != NULL)
14959 addend -= sec->output_section->vma;
14960 break;
14961
14962 case R_PPC64_REL16:
14963 case R_PPC64_REL16_LO:
14964 case R_PPC64_REL16_HI:
14965 case R_PPC64_REL16_HA:
14966 case R_PPC64_REL16_HIGH:
14967 case R_PPC64_REL16_HIGHA:
14968 case R_PPC64_REL16_HIGHER:
14969 case R_PPC64_REL16_HIGHERA:
14970 case R_PPC64_REL16_HIGHEST:
14971 case R_PPC64_REL16_HIGHESTA:
14972 case R_PPC64_REL16DX_HA:
14973 break;
14974
14975 case R_PPC64_REL14:
14976 case R_PPC64_REL14_BRNTAKEN:
14977 case R_PPC64_REL14_BRTAKEN:
14978 case R_PPC64_REL24:
14979 case R_PPC64_REL24_NOTOC:
14980 break;
14981
14982 case R_PPC64_TPREL16:
14983 case R_PPC64_TPREL16_LO:
14984 case R_PPC64_TPREL16_HI:
14985 case R_PPC64_TPREL16_HA:
14986 case R_PPC64_TPREL16_DS:
14987 case R_PPC64_TPREL16_LO_DS:
14988 case R_PPC64_TPREL16_HIGH:
14989 case R_PPC64_TPREL16_HIGHA:
14990 case R_PPC64_TPREL16_HIGHER:
14991 case R_PPC64_TPREL16_HIGHERA:
14992 case R_PPC64_TPREL16_HIGHEST:
14993 case R_PPC64_TPREL16_HIGHESTA:
14994 if (h != NULL
14995 && h->elf.root.type == bfd_link_hash_undefweak
14996 && h->elf.dynindx == -1)
14997 {
14998 /* Make this relocation against an undefined weak symbol
14999 resolve to zero. This is really just a tweak, since
15000 code using weak externs ought to check that they are
15001 defined before using them. */
15002 bfd_byte *p = contents + rel->r_offset - d_offset;
15003
15004 insn = bfd_get_32 (input_bfd, p);
15005 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15006 if (insn != 0)
15007 bfd_put_32 (input_bfd, insn, p);
15008 break;
15009 }
15010 if (htab->elf.tls_sec != NULL)
15011 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15012 /* The TPREL16 relocs shouldn't really be used in shared
15013 libs or with non-local symbols as that will result in
15014 DT_TEXTREL being set, but support them anyway. */
15015 goto dodyn;
15016
15017 case R_PPC64_DTPREL16:
15018 case R_PPC64_DTPREL16_LO:
15019 case R_PPC64_DTPREL16_HI:
15020 case R_PPC64_DTPREL16_HA:
15021 case R_PPC64_DTPREL16_DS:
15022 case R_PPC64_DTPREL16_LO_DS:
15023 case R_PPC64_DTPREL16_HIGH:
15024 case R_PPC64_DTPREL16_HIGHA:
15025 case R_PPC64_DTPREL16_HIGHER:
15026 case R_PPC64_DTPREL16_HIGHERA:
15027 case R_PPC64_DTPREL16_HIGHEST:
15028 case R_PPC64_DTPREL16_HIGHESTA:
15029 if (htab->elf.tls_sec != NULL)
15030 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15031 break;
15032
15033 case R_PPC64_ADDR64_LOCAL:
15034 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15035 ? h->elf.other
15036 : sym->st_other);
15037 break;
15038
15039 case R_PPC64_DTPMOD64:
15040 relocation = 1;
15041 addend = 0;
15042 goto dodyn;
15043
15044 case R_PPC64_TPREL64:
15045 if (htab->elf.tls_sec != NULL)
15046 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15047 goto dodyn;
15048
15049 case R_PPC64_DTPREL64:
15050 if (htab->elf.tls_sec != NULL)
15051 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15052 /* Fall through. */
15053
15054 /* Relocations that may need to be propagated if this is a
15055 dynamic object. */
15056 case R_PPC64_REL30:
15057 case R_PPC64_REL32:
15058 case R_PPC64_REL64:
15059 case R_PPC64_ADDR14:
15060 case R_PPC64_ADDR14_BRNTAKEN:
15061 case R_PPC64_ADDR14_BRTAKEN:
15062 case R_PPC64_ADDR16:
15063 case R_PPC64_ADDR16_DS:
15064 case R_PPC64_ADDR16_HA:
15065 case R_PPC64_ADDR16_HI:
15066 case R_PPC64_ADDR16_HIGH:
15067 case R_PPC64_ADDR16_HIGHA:
15068 case R_PPC64_ADDR16_HIGHER:
15069 case R_PPC64_ADDR16_HIGHERA:
15070 case R_PPC64_ADDR16_HIGHEST:
15071 case R_PPC64_ADDR16_HIGHESTA:
15072 case R_PPC64_ADDR16_LO:
15073 case R_PPC64_ADDR16_LO_DS:
15074 case R_PPC64_ADDR24:
15075 case R_PPC64_ADDR32:
15076 case R_PPC64_ADDR64:
15077 case R_PPC64_UADDR16:
15078 case R_PPC64_UADDR32:
15079 case R_PPC64_UADDR64:
15080 dodyn:
15081 if ((input_section->flags & SEC_ALLOC) == 0)
15082 break;
15083
15084 if (NO_OPD_RELOCS && is_opd)
15085 break;
15086
15087 if (bfd_link_pic (info)
15088 ? ((h == NULL
15089 || h->dyn_relocs != NULL)
15090 && ((h != NULL && pc_dynrelocs (h))
15091 || must_be_dyn_reloc (info, r_type)))
15092 : (h != NULL
15093 ? h->dyn_relocs != NULL
15094 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15095 {
15096 bfd_boolean skip, relocate;
15097 asection *sreloc;
15098 bfd_vma out_off;
15099 long indx = 0;
15100
15101 /* When generating a dynamic object, these relocations
15102 are copied into the output file to be resolved at run
15103 time. */
15104
15105 skip = FALSE;
15106 relocate = FALSE;
15107
15108 out_off = _bfd_elf_section_offset (output_bfd, info,
15109 input_section, rel->r_offset);
15110 if (out_off == (bfd_vma) -1)
15111 skip = TRUE;
15112 else if (out_off == (bfd_vma) -2)
15113 skip = TRUE, relocate = TRUE;
15114 out_off += (input_section->output_section->vma
15115 + input_section->output_offset);
15116 outrel.r_offset = out_off;
15117 outrel.r_addend = rel->r_addend;
15118
15119 /* Optimize unaligned reloc use. */
15120 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15121 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15122 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15123 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15124 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15125 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15126 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15127 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15128 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15129
15130 if (skip)
15131 memset (&outrel, 0, sizeof outrel);
15132 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15133 && !is_opd
15134 && r_type != R_PPC64_TOC)
15135 {
15136 indx = h->elf.dynindx;
15137 BFD_ASSERT (indx != -1);
15138 outrel.r_info = ELF64_R_INFO (indx, r_type);
15139 }
15140 else
15141 {
15142 /* This symbol is local, or marked to become local,
15143 or this is an opd section reloc which must point
15144 at a local function. */
15145 outrel.r_addend += relocation;
15146 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
15147 {
15148 if (is_opd && h != NULL)
15149 {
15150 /* Lie about opd entries. This case occurs
15151 when building shared libraries and we
15152 reference a function in another shared
15153 lib. The same thing happens for a weak
15154 definition in an application that's
15155 overridden by a strong definition in a
15156 shared lib. (I believe this is a generic
15157 bug in binutils handling of weak syms.)
15158 In these cases we won't use the opd
15159 entry in this lib. */
15160 unresolved_reloc = FALSE;
15161 }
15162 if (!is_opd
15163 && r_type == R_PPC64_ADDR64
15164 && (h != NULL
15165 ? h->elf.type == STT_GNU_IFUNC
15166 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15167 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15168 else
15169 {
15170 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15171
15172 /* We need to relocate .opd contents for ld.so.
15173 Prelink also wants simple and consistent rules
15174 for relocs. This make all RELATIVE relocs have
15175 *r_offset equal to r_addend. */
15176 relocate = TRUE;
15177 }
15178 }
15179 else
15180 {
15181 if (h != NULL
15182 ? h->elf.type == STT_GNU_IFUNC
15183 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15184 {
15185 info->callbacks->einfo
15186 /* xgettext:c-format */
15187 (_("%H: %s for indirect "
15188 "function `%pT' unsupported\n"),
15189 input_bfd, input_section, rel->r_offset,
15190 ppc64_elf_howto_table[r_type]->name,
15191 sym_name);
15192 ret = FALSE;
15193 }
15194 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
15195 ;
15196 else if (sec == NULL || sec->owner == NULL)
15197 {
15198 bfd_set_error (bfd_error_bad_value);
15199 return FALSE;
15200 }
15201 else
15202 {
15203 asection *osec;
15204
15205 osec = sec->output_section;
15206 indx = elf_section_data (osec)->dynindx;
15207
15208 if (indx == 0)
15209 {
15210 if ((osec->flags & SEC_READONLY) == 0
15211 && htab->elf.data_index_section != NULL)
15212 osec = htab->elf.data_index_section;
15213 else
15214 osec = htab->elf.text_index_section;
15215 indx = elf_section_data (osec)->dynindx;
15216 }
15217 BFD_ASSERT (indx != 0);
15218
15219 /* We are turning this relocation into one
15220 against a section symbol, so subtract out
15221 the output section's address but not the
15222 offset of the input section in the output
15223 section. */
15224 outrel.r_addend -= osec->vma;
15225 }
15226
15227 outrel.r_info = ELF64_R_INFO (indx, r_type);
15228 }
15229 }
15230
15231 sreloc = elf_section_data (input_section)->sreloc;
15232 if (h != NULL
15233 ? h->elf.type == STT_GNU_IFUNC
15234 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15235 {
15236 sreloc = htab->elf.irelplt;
15237 if (indx == 0)
15238 htab->local_ifunc_resolver = 1;
15239 else if (is_static_defined (&h->elf))
15240 htab->maybe_local_ifunc_resolver = 1;
15241 }
15242 if (sreloc == NULL)
15243 abort ();
15244
15245 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15246 >= sreloc->size)
15247 abort ();
15248 loc = sreloc->contents;
15249 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15250 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15251
15252 /* If this reloc is against an external symbol, it will
15253 be computed at runtime, so there's no need to do
15254 anything now. However, for the sake of prelink ensure
15255 that the section contents are a known value. */
15256 if (!relocate)
15257 {
15258 unresolved_reloc = FALSE;
15259 /* The value chosen here is quite arbitrary as ld.so
15260 ignores section contents except for the special
15261 case of .opd where the contents might be accessed
15262 before relocation. Choose zero, as that won't
15263 cause reloc overflow. */
15264 relocation = 0;
15265 addend = 0;
15266 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15267 to improve backward compatibility with older
15268 versions of ld. */
15269 if (r_type == R_PPC64_ADDR64)
15270 addend = outrel.r_addend;
15271 /* Adjust pc_relative relocs to have zero in *r_offset. */
15272 else if (ppc64_elf_howto_table[r_type]->pc_relative)
15273 addend = outrel.r_offset;
15274 }
15275 }
15276 break;
15277
15278 case R_PPC64_COPY:
15279 case R_PPC64_GLOB_DAT:
15280 case R_PPC64_JMP_SLOT:
15281 case R_PPC64_JMP_IREL:
15282 case R_PPC64_RELATIVE:
15283 /* We shouldn't ever see these dynamic relocs in relocatable
15284 files. */
15285 /* Fall through. */
15286
15287 case R_PPC64_PLTGOT16:
15288 case R_PPC64_PLTGOT16_DS:
15289 case R_PPC64_PLTGOT16_HA:
15290 case R_PPC64_PLTGOT16_HI:
15291 case R_PPC64_PLTGOT16_LO:
15292 case R_PPC64_PLTGOT16_LO_DS:
15293 case R_PPC64_PLTREL32:
15294 case R_PPC64_PLTREL64:
15295 /* These ones haven't been implemented yet. */
15296
15297 info->callbacks->einfo
15298 /* xgettext:c-format */
15299 (_("%P: %pB: %s is not supported for `%pT'\n"),
15300 input_bfd,
15301 ppc64_elf_howto_table[r_type]->name, sym_name);
15302
15303 bfd_set_error (bfd_error_invalid_operation);
15304 ret = FALSE;
15305 goto copy_reloc;
15306 }
15307
15308 /* Multi-instruction sequences that access the TOC can be
15309 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15310 to nop; addi rb,r2,x; */
15311 switch (r_type)
15312 {
15313 default:
15314 break;
15315
15316 case R_PPC64_GOT_TLSLD16_HI:
15317 case R_PPC64_GOT_TLSGD16_HI:
15318 case R_PPC64_GOT_TPREL16_HI:
15319 case R_PPC64_GOT_DTPREL16_HI:
15320 case R_PPC64_GOT16_HI:
15321 case R_PPC64_TOC16_HI:
15322 /* These relocs would only be useful if building up an
15323 offset to later add to r2, perhaps in an indexed
15324 addressing mode instruction. Don't try to optimize.
15325 Unfortunately, the possibility of someone building up an
15326 offset like this or even with the HA relocs, means that
15327 we need to check the high insn when optimizing the low
15328 insn. */
15329 break;
15330
15331 case R_PPC64_PLTCALL:
15332 if (unresolved_reloc)
15333 {
15334 /* No plt entry. Make this into a direct call. */
15335 bfd_byte *p = contents + rel->r_offset;
15336 insn = bfd_get_32 (input_bfd, p);
15337 insn &= 1;
15338 bfd_put_32 (input_bfd, B_DOT | insn, p);
15339 bfd_put_32 (input_bfd, NOP, p + 4);
15340 unresolved_reloc = save_unresolved_reloc;
15341 r_type = R_PPC64_REL24;
15342 }
15343 break;
15344
15345 case R_PPC64_PLTSEQ:
15346 if (unresolved_reloc)
15347 {
15348 unresolved_reloc = FALSE;
15349 goto nop_it;
15350 }
15351 break;
15352
15353 case R_PPC64_PLT16_HA:
15354 if (unresolved_reloc)
15355 {
15356 unresolved_reloc = FALSE;
15357 goto nop_it;
15358 }
15359 /* Fall through. */
15360 case R_PPC64_GOT_TLSLD16_HA:
15361 case R_PPC64_GOT_TLSGD16_HA:
15362 case R_PPC64_GOT_TPREL16_HA:
15363 case R_PPC64_GOT_DTPREL16_HA:
15364 case R_PPC64_GOT16_HA:
15365 case R_PPC64_TOC16_HA:
15366 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15367 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15368 {
15369 bfd_byte *p;
15370 nop_it:
15371 p = contents + (rel->r_offset & ~3);
15372 bfd_put_32 (input_bfd, NOP, p);
15373 goto copy_reloc;
15374 }
15375 break;
15376
15377 case R_PPC64_PLT16_LO:
15378 case R_PPC64_PLT16_LO_DS:
15379 if (unresolved_reloc)
15380 {
15381 unresolved_reloc = FALSE;
15382 goto nop_it;
15383 }
15384 /* Fall through. */
15385 case R_PPC64_GOT_TLSLD16_LO:
15386 case R_PPC64_GOT_TLSGD16_LO:
15387 case R_PPC64_GOT_TPREL16_LO_DS:
15388 case R_PPC64_GOT_DTPREL16_LO_DS:
15389 case R_PPC64_GOT16_LO:
15390 case R_PPC64_GOT16_LO_DS:
15391 case R_PPC64_TOC16_LO:
15392 case R_PPC64_TOC16_LO_DS:
15393 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15394 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15395 {
15396 bfd_byte *p = contents + (rel->r_offset & ~3);
15397 insn = bfd_get_32 (input_bfd, p);
15398 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15399 {
15400 /* Transform addic to addi when we change reg. */
15401 insn &= ~((0x3f << 26) | (0x1f << 16));
15402 insn |= (14u << 26) | (2 << 16);
15403 }
15404 else
15405 {
15406 insn &= ~(0x1f << 16);
15407 insn |= 2 << 16;
15408 }
15409 bfd_put_32 (input_bfd, insn, p);
15410 }
15411 break;
15412
15413 case R_PPC64_TPREL16_HA:
15414 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15415 {
15416 bfd_byte *p = contents + (rel->r_offset & ~3);
15417 insn = bfd_get_32 (input_bfd, p);
15418 if ((insn & ((0x3f << 26) | 0x1f << 16))
15419 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15420 /* xgettext:c-format */
15421 info->callbacks->minfo
15422 (_("%H: warning: %s unexpected insn %#x.\n"),
15423 input_bfd, input_section, rel->r_offset,
15424 ppc64_elf_howto_table[r_type]->name, insn);
15425 else
15426 {
15427 bfd_put_32 (input_bfd, NOP, p);
15428 goto copy_reloc;
15429 }
15430 }
15431 break;
15432
15433 case R_PPC64_TPREL16_LO:
15434 case R_PPC64_TPREL16_LO_DS:
15435 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15436 {
15437 bfd_byte *p = contents + (rel->r_offset & ~3);
15438 insn = bfd_get_32 (input_bfd, p);
15439 insn &= ~(0x1f << 16);
15440 insn |= 13 << 16;
15441 bfd_put_32 (input_bfd, insn, p);
15442 }
15443 break;
15444 }
15445
15446 /* Do any further special processing. */
15447 switch (r_type)
15448 {
15449 default:
15450 break;
15451
15452 case R_PPC64_REL16_HA:
15453 case R_PPC64_REL16_HIGHA:
15454 case R_PPC64_REL16_HIGHERA:
15455 case R_PPC64_REL16_HIGHESTA:
15456 case R_PPC64_REL16DX_HA:
15457 case R_PPC64_ADDR16_HA:
15458 case R_PPC64_ADDR16_HIGHA:
15459 case R_PPC64_ADDR16_HIGHERA:
15460 case R_PPC64_ADDR16_HIGHESTA:
15461 case R_PPC64_TOC16_HA:
15462 case R_PPC64_SECTOFF_HA:
15463 case R_PPC64_TPREL16_HA:
15464 case R_PPC64_TPREL16_HIGHA:
15465 case R_PPC64_TPREL16_HIGHERA:
15466 case R_PPC64_TPREL16_HIGHESTA:
15467 case R_PPC64_DTPREL16_HA:
15468 case R_PPC64_DTPREL16_HIGHA:
15469 case R_PPC64_DTPREL16_HIGHERA:
15470 case R_PPC64_DTPREL16_HIGHESTA:
15471 /* It's just possible that this symbol is a weak symbol
15472 that's not actually defined anywhere. In that case,
15473 'sec' would be NULL, and we should leave the symbol
15474 alone (it will be set to zero elsewhere in the link). */
15475 if (sec == NULL)
15476 break;
15477 /* Fall through. */
15478
15479 case R_PPC64_GOT16_HA:
15480 case R_PPC64_PLTGOT16_HA:
15481 case R_PPC64_PLT16_HA:
15482 case R_PPC64_GOT_TLSGD16_HA:
15483 case R_PPC64_GOT_TLSLD16_HA:
15484 case R_PPC64_GOT_TPREL16_HA:
15485 case R_PPC64_GOT_DTPREL16_HA:
15486 /* Add 0x10000 if sign bit in 0:15 is set.
15487 Bits 0:15 are not used. */
15488 addend += 0x8000;
15489 break;
15490
15491 case R_PPC64_ADDR16_DS:
15492 case R_PPC64_ADDR16_LO_DS:
15493 case R_PPC64_GOT16_DS:
15494 case R_PPC64_GOT16_LO_DS:
15495 case R_PPC64_PLT16_LO_DS:
15496 case R_PPC64_SECTOFF_DS:
15497 case R_PPC64_SECTOFF_LO_DS:
15498 case R_PPC64_TOC16_DS:
15499 case R_PPC64_TOC16_LO_DS:
15500 case R_PPC64_PLTGOT16_DS:
15501 case R_PPC64_PLTGOT16_LO_DS:
15502 case R_PPC64_GOT_TPREL16_DS:
15503 case R_PPC64_GOT_TPREL16_LO_DS:
15504 case R_PPC64_GOT_DTPREL16_DS:
15505 case R_PPC64_GOT_DTPREL16_LO_DS:
15506 case R_PPC64_TPREL16_DS:
15507 case R_PPC64_TPREL16_LO_DS:
15508 case R_PPC64_DTPREL16_DS:
15509 case R_PPC64_DTPREL16_LO_DS:
15510 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15511 mask = 3;
15512 /* If this reloc is against an lq, lxv, or stxv insn, then
15513 the value must be a multiple of 16. This is somewhat of
15514 a hack, but the "correct" way to do this by defining _DQ
15515 forms of all the _DS relocs bloats all reloc switches in
15516 this file. It doesn't make much sense to use these
15517 relocs in data, so testing the insn should be safe. */
15518 if ((insn & (0x3f << 26)) == (56u << 26)
15519 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15520 mask = 15;
15521 relocation += addend;
15522 addend = insn & (mask ^ 3);
15523 if ((relocation & mask) != 0)
15524 {
15525 relocation ^= relocation & mask;
15526 info->callbacks->einfo
15527 /* xgettext:c-format */
15528 (_("%H: error: %s not a multiple of %u\n"),
15529 input_bfd, input_section, rel->r_offset,
15530 ppc64_elf_howto_table[r_type]->name,
15531 mask + 1);
15532 bfd_set_error (bfd_error_bad_value);
15533 ret = FALSE;
15534 goto copy_reloc;
15535 }
15536 break;
15537 }
15538
15539 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15540 because such sections are not SEC_ALLOC and thus ld.so will
15541 not process them. */
15542 howto = ppc64_elf_howto_table[(int) r_type];
15543 if (unresolved_reloc
15544 && !((input_section->flags & SEC_DEBUGGING) != 0
15545 && h->elf.def_dynamic)
15546 && _bfd_elf_section_offset (output_bfd, info, input_section,
15547 rel->r_offset) != (bfd_vma) -1)
15548 {
15549 info->callbacks->einfo
15550 /* xgettext:c-format */
15551 (_("%H: unresolvable %s against `%pT'\n"),
15552 input_bfd, input_section, rel->r_offset,
15553 howto->name,
15554 h->elf.root.root.string);
15555 ret = FALSE;
15556 }
15557
15558 /* 16-bit fields in insns mostly have signed values, but a
15559 few insns have 16-bit unsigned values. Really, we should
15560 have different reloc types. */
15561 if (howto->complain_on_overflow != complain_overflow_dont
15562 && howto->dst_mask == 0xffff
15563 && (input_section->flags & SEC_CODE) != 0)
15564 {
15565 enum complain_overflow complain = complain_overflow_signed;
15566
15567 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15568 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15569 complain = complain_overflow_bitfield;
15570 else if (howto->rightshift == 0
15571 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15572 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15573 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15574 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15575 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15576 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15577 complain = complain_overflow_unsigned;
15578 if (howto->complain_on_overflow != complain)
15579 {
15580 alt_howto = *howto;
15581 alt_howto.complain_on_overflow = complain;
15582 howto = &alt_howto;
15583 }
15584 }
15585
15586 if (r_type == R_PPC64_REL16DX_HA)
15587 {
15588 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15589 if (rel->r_offset + 4 > input_section->size)
15590 r = bfd_reloc_outofrange;
15591 else
15592 {
15593 relocation += addend;
15594 relocation -= (rel->r_offset
15595 + input_section->output_offset
15596 + input_section->output_section->vma);
15597 relocation = (bfd_signed_vma) relocation >> 16;
15598 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15599 insn &= ~0x1fffc1;
15600 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15601 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15602 r = bfd_reloc_ok;
15603 if (relocation + 0x8000 > 0xffff)
15604 r = bfd_reloc_overflow;
15605 }
15606 }
15607 else
15608 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15609 rel->r_offset, relocation, addend);
15610
15611 if (r != bfd_reloc_ok)
15612 {
15613 char *more_info = NULL;
15614 const char *reloc_name = howto->name;
15615
15616 if (reloc_dest != DEST_NORMAL)
15617 {
15618 more_info = bfd_malloc (strlen (reloc_name) + 8);
15619 if (more_info != NULL)
15620 {
15621 strcpy (more_info, reloc_name);
15622 strcat (more_info, (reloc_dest == DEST_OPD
15623 ? " (OPD)" : " (stub)"));
15624 reloc_name = more_info;
15625 }
15626 }
15627
15628 if (r == bfd_reloc_overflow)
15629 {
15630 /* On code like "if (foo) foo();" don't report overflow
15631 on a branch to zero when foo is undefined. */
15632 if (!warned
15633 && (reloc_dest == DEST_STUB
15634 || !(h != NULL
15635 && (h->elf.root.type == bfd_link_hash_undefweak
15636 || h->elf.root.type == bfd_link_hash_undefined)
15637 && is_branch_reloc (r_type))))
15638 info->callbacks->reloc_overflow (info, &h->elf.root,
15639 sym_name, reloc_name,
15640 orig_rel.r_addend,
15641 input_bfd, input_section,
15642 rel->r_offset);
15643 }
15644 else
15645 {
15646 info->callbacks->einfo
15647 /* xgettext:c-format */
15648 (_("%H: %s against `%pT': error %d\n"),
15649 input_bfd, input_section, rel->r_offset,
15650 reloc_name, sym_name, (int) r);
15651 ret = FALSE;
15652 }
15653 if (more_info != NULL)
15654 free (more_info);
15655 }
15656 copy_reloc:
15657 if (wrel != rel)
15658 *wrel = *rel;
15659 }
15660
15661 if (wrel != rel)
15662 {
15663 Elf_Internal_Shdr *rel_hdr;
15664 size_t deleted = rel - wrel;
15665
15666 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15667 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15668 if (rel_hdr->sh_size == 0)
15669 {
15670 /* It is too late to remove an empty reloc section. Leave
15671 one NONE reloc.
15672 ??? What is wrong with an empty section??? */
15673 rel_hdr->sh_size = rel_hdr->sh_entsize;
15674 deleted -= 1;
15675 }
15676 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15677 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15678 input_section->reloc_count -= deleted;
15679 }
15680
15681 /* If we're emitting relocations, then shortly after this function
15682 returns, reloc offsets and addends for this section will be
15683 adjusted. Worse, reloc symbol indices will be for the output
15684 file rather than the input. Save a copy of the relocs for
15685 opd_entry_value. */
15686 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15687 {
15688 bfd_size_type amt;
15689 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15690 rel = bfd_alloc (input_bfd, amt);
15691 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15692 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15693 if (rel == NULL)
15694 return FALSE;
15695 memcpy (rel, relocs, amt);
15696 }
15697 return ret;
15698 }
15699
15700 /* Adjust the value of any local symbols in opd sections. */
15701
15702 static int
15703 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15704 const char *name ATTRIBUTE_UNUSED,
15705 Elf_Internal_Sym *elfsym,
15706 asection *input_sec,
15707 struct elf_link_hash_entry *h)
15708 {
15709 struct _opd_sec_data *opd;
15710 long adjust;
15711 bfd_vma value;
15712
15713 if (h != NULL)
15714 return 1;
15715
15716 opd = get_opd_info (input_sec);
15717 if (opd == NULL || opd->adjust == NULL)
15718 return 1;
15719
15720 value = elfsym->st_value - input_sec->output_offset;
15721 if (!bfd_link_relocatable (info))
15722 value -= input_sec->output_section->vma;
15723
15724 adjust = opd->adjust[OPD_NDX (value)];
15725 if (adjust == -1)
15726 return 2;
15727
15728 elfsym->st_value += adjust;
15729 return 1;
15730 }
15731
15732 /* Finish up dynamic symbol handling. We set the contents of various
15733 dynamic sections here. */
15734
15735 static bfd_boolean
15736 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15737 struct bfd_link_info *info,
15738 struct elf_link_hash_entry *h,
15739 Elf_Internal_Sym *sym)
15740 {
15741 struct ppc_link_hash_table *htab;
15742 struct plt_entry *ent;
15743
15744 htab = ppc_hash_table (info);
15745 if (htab == NULL)
15746 return FALSE;
15747
15748 if (!htab->opd_abi && !h->def_regular)
15749 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15750 if (ent->plt.offset != (bfd_vma) -1)
15751 {
15752 /* Mark the symbol as undefined, rather than as
15753 defined in glink. Leave the value if there were
15754 any relocations where pointer equality matters
15755 (this is a clue for the dynamic linker, to make
15756 function pointer comparisons work between an
15757 application and shared library), otherwise set it
15758 to zero. */
15759 sym->st_shndx = SHN_UNDEF;
15760 if (!h->pointer_equality_needed)
15761 sym->st_value = 0;
15762 else if (!h->ref_regular_nonweak)
15763 {
15764 /* This breaks function pointer comparisons, but
15765 that is better than breaking tests for a NULL
15766 function pointer. */
15767 sym->st_value = 0;
15768 }
15769 break;
15770 }
15771
15772 if (h->needs_copy)
15773 {
15774 /* This symbol needs a copy reloc. Set it up. */
15775 Elf_Internal_Rela rela;
15776 asection *srel;
15777 bfd_byte *loc;
15778
15779 if (h->dynindx == -1
15780 || (h->root.type != bfd_link_hash_defined
15781 && h->root.type != bfd_link_hash_defweak)
15782 || htab->elf.srelbss == NULL
15783 || htab->elf.sreldynrelro == NULL)
15784 abort ();
15785
15786 rela.r_offset = (h->root.u.def.value
15787 + h->root.u.def.section->output_section->vma
15788 + h->root.u.def.section->output_offset);
15789 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15790 rela.r_addend = 0;
15791 if (h->root.u.def.section == htab->elf.sdynrelro)
15792 srel = htab->elf.sreldynrelro;
15793 else
15794 srel = htab->elf.srelbss;
15795 loc = srel->contents;
15796 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
15797 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15798 }
15799
15800 return TRUE;
15801 }
15802
15803 /* Used to decide how to sort relocs in an optimal manner for the
15804 dynamic linker, before writing them out. */
15805
15806 static enum elf_reloc_type_class
15807 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15808 const asection *rel_sec,
15809 const Elf_Internal_Rela *rela)
15810 {
15811 enum elf_ppc64_reloc_type r_type;
15812 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15813
15814 if (rel_sec == htab->elf.irelplt)
15815 return reloc_class_ifunc;
15816
15817 r_type = ELF64_R_TYPE (rela->r_info);
15818 switch (r_type)
15819 {
15820 case R_PPC64_RELATIVE:
15821 return reloc_class_relative;
15822 case R_PPC64_JMP_SLOT:
15823 return reloc_class_plt;
15824 case R_PPC64_COPY:
15825 return reloc_class_copy;
15826 default:
15827 return reloc_class_normal;
15828 }
15829 }
15830
15831 /* Finish up the dynamic sections. */
15832
15833 static bfd_boolean
15834 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15835 struct bfd_link_info *info)
15836 {
15837 struct ppc_link_hash_table *htab;
15838 bfd *dynobj;
15839 asection *sdyn;
15840
15841 htab = ppc_hash_table (info);
15842 if (htab == NULL)
15843 return FALSE;
15844
15845 dynobj = htab->elf.dynobj;
15846 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15847
15848 if (htab->elf.dynamic_sections_created)
15849 {
15850 Elf64_External_Dyn *dyncon, *dynconend;
15851
15852 if (sdyn == NULL || htab->elf.sgot == NULL)
15853 abort ();
15854
15855 dyncon = (Elf64_External_Dyn *) sdyn->contents;
15856 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15857 for (; dyncon < dynconend; dyncon++)
15858 {
15859 Elf_Internal_Dyn dyn;
15860 asection *s;
15861
15862 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15863
15864 switch (dyn.d_tag)
15865 {
15866 default:
15867 continue;
15868
15869 case DT_PPC64_GLINK:
15870 s = htab->glink;
15871 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15872 /* We stupidly defined DT_PPC64_GLINK to be the start
15873 of glink rather than the first entry point, which is
15874 what ld.so needs, and now have a bigger stub to
15875 support automatic multiple TOCs. */
15876 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
15877 break;
15878
15879 case DT_PPC64_OPD:
15880 s = bfd_get_section_by_name (output_bfd, ".opd");
15881 if (s == NULL)
15882 continue;
15883 dyn.d_un.d_ptr = s->vma;
15884 break;
15885
15886 case DT_PPC64_OPT:
15887 if (htab->do_multi_toc && htab->multi_toc_needed)
15888 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15889 if (htab->has_plt_localentry0)
15890 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
15891 break;
15892
15893 case DT_PPC64_OPDSZ:
15894 s = bfd_get_section_by_name (output_bfd, ".opd");
15895 if (s == NULL)
15896 continue;
15897 dyn.d_un.d_val = s->size;
15898 break;
15899
15900 case DT_PLTGOT:
15901 s = htab->elf.splt;
15902 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15903 break;
15904
15905 case DT_JMPREL:
15906 s = htab->elf.srelplt;
15907 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15908 break;
15909
15910 case DT_PLTRELSZ:
15911 dyn.d_un.d_val = htab->elf.srelplt->size;
15912 break;
15913
15914 case DT_TEXTREL:
15915 if (htab->local_ifunc_resolver)
15916 info->callbacks->einfo
15917 (_("%X%P: text relocations and GNU indirect "
15918 "functions will result in a segfault at runtime\n"));
15919 else if (htab->maybe_local_ifunc_resolver)
15920 info->callbacks->einfo
15921 (_("%P: warning: text relocations and GNU indirect "
15922 "functions may result in a segfault at runtime\n"));
15923 continue;
15924 }
15925
15926 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15927 }
15928 }
15929
15930 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15931 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
15932 {
15933 /* Fill in the first entry in the global offset table.
15934 We use it to hold the link-time TOCbase. */
15935 bfd_put_64 (output_bfd,
15936 elf_gp (output_bfd) + TOC_BASE_OFF,
15937 htab->elf.sgot->contents);
15938
15939 /* Set .got entry size. */
15940 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
15941 = 8;
15942 }
15943
15944 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15945 && htab->elf.splt->output_section != bfd_abs_section_ptr)
15946 {
15947 /* Set .plt entry size. */
15948 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15949 = PLT_ENTRY_SIZE (htab);
15950 }
15951
15952 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15953 brlt ourselves if emitrelocations. */
15954 if (htab->brlt != NULL
15955 && htab->brlt->reloc_count != 0
15956 && !_bfd_elf_link_output_relocs (output_bfd,
15957 htab->brlt,
15958 elf_section_data (htab->brlt)->rela.hdr,
15959 elf_section_data (htab->brlt)->relocs,
15960 NULL))
15961 return FALSE;
15962
15963 if (htab->glink != NULL
15964 && htab->glink->reloc_count != 0
15965 && !_bfd_elf_link_output_relocs (output_bfd,
15966 htab->glink,
15967 elf_section_data (htab->glink)->rela.hdr,
15968 elf_section_data (htab->glink)->relocs,
15969 NULL))
15970 return FALSE;
15971
15972
15973 if (htab->glink_eh_frame != NULL
15974 && htab->glink_eh_frame->size != 0
15975 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15976 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15977 htab->glink_eh_frame,
15978 htab->glink_eh_frame->contents))
15979 return FALSE;
15980
15981 /* We need to handle writing out multiple GOT sections ourselves,
15982 since we didn't add them to DYNOBJ. We know dynobj is the first
15983 bfd. */
15984 while ((dynobj = dynobj->link.next) != NULL)
15985 {
15986 asection *s;
15987
15988 if (!is_ppc64_elf (dynobj))
15989 continue;
15990
15991 s = ppc64_elf_tdata (dynobj)->got;
15992 if (s != NULL
15993 && s->size != 0
15994 && s->output_section != bfd_abs_section_ptr
15995 && !bfd_set_section_contents (output_bfd, s->output_section,
15996 s->contents, s->output_offset,
15997 s->size))
15998 return FALSE;
15999 s = ppc64_elf_tdata (dynobj)->relgot;
16000 if (s != NULL
16001 && s->size != 0
16002 && s->output_section != bfd_abs_section_ptr
16003 && !bfd_set_section_contents (output_bfd, s->output_section,
16004 s->contents, s->output_offset,
16005 s->size))
16006 return FALSE;
16007 }
16008
16009 return TRUE;
16010 }
16011
16012 #include "elf64-target.h"
16013
16014 /* FreeBSD support */
16015
16016 #undef TARGET_LITTLE_SYM
16017 #undef TARGET_LITTLE_NAME
16018
16019 #undef TARGET_BIG_SYM
16020 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
16021 #undef TARGET_BIG_NAME
16022 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16023
16024 #undef ELF_OSABI
16025 #define ELF_OSABI ELFOSABI_FREEBSD
16026
16027 #undef elf64_bed
16028 #define elf64_bed elf64_powerpc_fbsd_bed
16029
16030 #include "elf64-target.h"
This page took 0.388267 seconds and 5 git commands to generate.