Fix "unresolved reloc" error for NOTOC relocs
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2018 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 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
824 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
825 ppc64_elf_ha_reloc),
826
827 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
828 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
829 ppc64_elf_ha_reloc),
830
831 /* Like R_PPC64_ADDR16_HI, but no overflow. */
832 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
833 bfd_elf_generic_reloc),
834
835 /* Like R_PPC64_ADDR16_HA, but no overflow. */
836 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
837 ppc64_elf_ha_reloc),
838
839 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
840 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
841 ppc64_elf_unhandled_reloc),
842
843 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
844 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
845 ppc64_elf_unhandled_reloc),
846
847 /* Like R_PPC64_TPREL16_HI, but no overflow. */
848 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
849 ppc64_elf_unhandled_reloc),
850
851 /* Like R_PPC64_TPREL16_HA, but no overflow. */
852 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
853 ppc64_elf_unhandled_reloc),
854
855 /* Marker reloc on ELFv2 large-model function entry. */
856 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
857 bfd_elf_generic_reloc),
858
859 /* Like ADDR64, but use local entry point of function. */
860 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
861 bfd_elf_generic_reloc),
862
863 /* GNU extension to record C++ vtable hierarchy. */
864 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
865 NULL),
866
867 /* GNU extension to record C++ vtable member usage. */
868 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
869 NULL),
870 };
871
872 \f
873 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
874 be done. */
875
876 static void
877 ppc_howto_init (void)
878 {
879 unsigned int i, type;
880
881 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
882 {
883 type = ppc64_elf_howto_raw[i].type;
884 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
885 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
886 }
887 }
888
889 static reloc_howto_type *
890 ppc64_elf_reloc_type_lookup (bfd *abfd,
891 bfd_reloc_code_real_type code)
892 {
893 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
894
895 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
896 /* Initialize howto table if needed. */
897 ppc_howto_init ();
898
899 switch (code)
900 {
901 default:
902 /* xgettext:c-format */
903 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
904 (int) code);
905 bfd_set_error (bfd_error_bad_value);
906 return NULL;
907
908 case BFD_RELOC_NONE: r = R_PPC64_NONE;
909 break;
910 case BFD_RELOC_32: r = R_PPC64_ADDR32;
911 break;
912 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
913 break;
914 case BFD_RELOC_16: r = R_PPC64_ADDR16;
915 break;
916 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
917 break;
918 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
919 break;
920 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
921 break;
922 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
923 break;
924 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
925 break;
926 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
927 break;
928 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
929 break;
930 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
931 break;
932 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
933 break;
934 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
935 break;
936 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
937 break;
938 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
939 break;
940 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
941 break;
942 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
943 break;
944 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
945 break;
946 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
947 break;
948 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
949 break;
950 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
951 break;
952 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
953 break;
954 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
955 break;
956 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
957 break;
958 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
959 break;
960 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
961 break;
962 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
963 break;
964 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
965 break;
966 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
967 break;
968 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
969 break;
970 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
971 break;
972 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
973 break;
974 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
975 break;
976 case BFD_RELOC_64: r = R_PPC64_ADDR64;
977 break;
978 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
979 break;
980 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
981 break;
982 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
983 break;
984 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
985 break;
986 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
987 break;
988 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
989 break;
990 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
991 break;
992 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
993 break;
994 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
995 break;
996 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
997 break;
998 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
999 break;
1000 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1001 break;
1002 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1003 break;
1004 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1005 break;
1006 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1007 break;
1008 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1009 break;
1010 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1011 break;
1012 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1013 break;
1014 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1015 break;
1016 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1017 break;
1018 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1019 break;
1020 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1021 break;
1022 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1023 break;
1024 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1025 break;
1026 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1027 break;
1028 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1029 break;
1030 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1031 break;
1032 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1033 break;
1034 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1035 break;
1036 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1037 break;
1038 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1039 break;
1040 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1041 break;
1042 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1043 break;
1044 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1045 break;
1046 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1047 break;
1048 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1049 break;
1050 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1051 break;
1052 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1053 break;
1054 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1055 break;
1056 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1057 break;
1058 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1059 break;
1060 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1061 break;
1062 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1063 break;
1064 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1065 break;
1066 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1067 break;
1068 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1069 break;
1070 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1071 break;
1072 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1073 break;
1074 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1075 break;
1076 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1077 break;
1078 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1079 break;
1080 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1081 break;
1082 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1083 break;
1084 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1085 break;
1086 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1087 break;
1088 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1089 break;
1090 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1091 break;
1092 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1093 break;
1094 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1095 break;
1096 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1097 break;
1098 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1099 break;
1100 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1101 break;
1102 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1103 break;
1104 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1105 break;
1106 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1107 break;
1108 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1109 break;
1110 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1111 break;
1112 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1113 break;
1114 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1115 break;
1116 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1117 break;
1118 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1119 break;
1120 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1121 break;
1122 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1123 break;
1124 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1125 break;
1126 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1127 break;
1128 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1129 break;
1130 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1131 break;
1132 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1133 break;
1134 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1135 break;
1136 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1137 break;
1138 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1139 break;
1140 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1141 break;
1142 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1143 break;
1144 }
1145
1146 return ppc64_elf_howto_table[r];
1147 };
1148
1149 static reloc_howto_type *
1150 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1151 const char *r_name)
1152 {
1153 unsigned int i;
1154
1155 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1156 if (ppc64_elf_howto_raw[i].name != NULL
1157 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1158 return &ppc64_elf_howto_raw[i];
1159
1160 return NULL;
1161 }
1162
1163 /* Set the howto pointer for a PowerPC ELF reloc. */
1164
1165 static bfd_boolean
1166 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1167 Elf_Internal_Rela *dst)
1168 {
1169 unsigned int type;
1170
1171 /* Initialize howto table if needed. */
1172 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1173 ppc_howto_init ();
1174
1175 type = ELF64_R_TYPE (dst->r_info);
1176 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1177 {
1178 /* xgettext:c-format */
1179 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1180 abfd, type);
1181 bfd_set_error (bfd_error_bad_value);
1182 return FALSE;
1183 }
1184 cache_ptr->howto = ppc64_elf_howto_table[type];
1185 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1186 {
1187 /* xgettext:c-format */
1188 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1189 abfd, type);
1190 bfd_set_error (bfd_error_bad_value);
1191 return FALSE;
1192 }
1193
1194 return TRUE;
1195 }
1196
1197 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1198
1199 static bfd_reloc_status_type
1200 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1201 void *data, asection *input_section,
1202 bfd *output_bfd, char **error_message)
1203 {
1204 enum elf_ppc64_reloc_type r_type;
1205 long insn;
1206 bfd_size_type octets;
1207 bfd_vma value;
1208
1209 /* If this is a relocatable link (output_bfd test tells us), just
1210 call the generic function. Any adjustment will be done at final
1211 link time. */
1212 if (output_bfd != NULL)
1213 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1214 input_section, output_bfd, error_message);
1215
1216 /* Adjust the addend for sign extension of the low 16 bits.
1217 We won't actually be using the low 16 bits, so trashing them
1218 doesn't matter. */
1219 reloc_entry->addend += 0x8000;
1220 r_type = reloc_entry->howto->type;
1221 if (r_type != R_PPC64_REL16DX_HA)
1222 return bfd_reloc_continue;
1223
1224 value = 0;
1225 if (!bfd_is_com_section (symbol->section))
1226 value = symbol->value;
1227 value += (reloc_entry->addend
1228 + symbol->section->output_offset
1229 + symbol->section->output_section->vma);
1230 value -= (reloc_entry->address
1231 + input_section->output_offset
1232 + input_section->output_section->vma);
1233 value = (bfd_signed_vma) value >> 16;
1234
1235 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1236 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1237 insn &= ~0x1fffc1;
1238 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1239 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1240 if (value + 0x8000 > 0xffff)
1241 return bfd_reloc_overflow;
1242 return bfd_reloc_ok;
1243 }
1244
1245 static bfd_reloc_status_type
1246 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1247 void *data, asection *input_section,
1248 bfd *output_bfd, char **error_message)
1249 {
1250 if (output_bfd != NULL)
1251 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1252 input_section, output_bfd, error_message);
1253
1254 if (strcmp (symbol->section->name, ".opd") == 0
1255 && (symbol->section->owner->flags & DYNAMIC) == 0)
1256 {
1257 bfd_vma dest = opd_entry_value (symbol->section,
1258 symbol->value + reloc_entry->addend,
1259 NULL, NULL, FALSE);
1260 if (dest != (bfd_vma) -1)
1261 reloc_entry->addend = dest - (symbol->value
1262 + symbol->section->output_section->vma
1263 + symbol->section->output_offset);
1264 }
1265 else
1266 {
1267 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1268
1269 if (symbol->section->owner != abfd
1270 && symbol->section->owner != NULL
1271 && abiversion (symbol->section->owner) >= 2)
1272 {
1273 unsigned int i;
1274
1275 for (i = 0; i < symbol->section->owner->symcount; ++i)
1276 {
1277 asymbol *symdef = symbol->section->owner->outsymbols[i];
1278
1279 if (strcmp (symdef->name, symbol->name) == 0)
1280 {
1281 elfsym = (elf_symbol_type *) symdef;
1282 break;
1283 }
1284 }
1285 }
1286 reloc_entry->addend
1287 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1288 }
1289 return bfd_reloc_continue;
1290 }
1291
1292 static bfd_reloc_status_type
1293 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1294 void *data, asection *input_section,
1295 bfd *output_bfd, char **error_message)
1296 {
1297 long insn;
1298 enum elf_ppc64_reloc_type r_type;
1299 bfd_size_type octets;
1300 /* Assume 'at' branch hints. */
1301 bfd_boolean is_isa_v2 = TRUE;
1302
1303 /* If this is a relocatable link (output_bfd test tells us), just
1304 call the generic function. Any adjustment will be done at final
1305 link time. */
1306 if (output_bfd != NULL)
1307 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1308 input_section, output_bfd, error_message);
1309
1310 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1311 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1312 insn &= ~(0x01 << 21);
1313 r_type = reloc_entry->howto->type;
1314 if (r_type == R_PPC64_ADDR14_BRTAKEN
1315 || r_type == R_PPC64_REL14_BRTAKEN)
1316 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1317
1318 if (is_isa_v2)
1319 {
1320 /* Set 'a' bit. This is 0b00010 in BO field for branch
1321 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1322 for branch on CTR insns (BO == 1a00t or 1a01t). */
1323 if ((insn & (0x14 << 21)) == (0x04 << 21))
1324 insn |= 0x02 << 21;
1325 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1326 insn |= 0x08 << 21;
1327 else
1328 goto out;
1329 }
1330 else
1331 {
1332 bfd_vma target = 0;
1333 bfd_vma from;
1334
1335 if (!bfd_is_com_section (symbol->section))
1336 target = symbol->value;
1337 target += symbol->section->output_section->vma;
1338 target += symbol->section->output_offset;
1339 target += reloc_entry->addend;
1340
1341 from = (reloc_entry->address
1342 + input_section->output_offset
1343 + input_section->output_section->vma);
1344
1345 /* Invert 'y' bit if not the default. */
1346 if ((bfd_signed_vma) (target - from) < 0)
1347 insn ^= 0x01 << 21;
1348 }
1349 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1350 out:
1351 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1352 input_section, output_bfd, error_message);
1353 }
1354
1355 static bfd_reloc_status_type
1356 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1357 void *data, asection *input_section,
1358 bfd *output_bfd, char **error_message)
1359 {
1360 /* If this is a relocatable link (output_bfd test tells us), just
1361 call the generic function. Any adjustment will be done at final
1362 link time. */
1363 if (output_bfd != NULL)
1364 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1365 input_section, output_bfd, error_message);
1366
1367 /* Subtract the symbol section base address. */
1368 reloc_entry->addend -= symbol->section->output_section->vma;
1369 return bfd_reloc_continue;
1370 }
1371
1372 static bfd_reloc_status_type
1373 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1374 void *data, asection *input_section,
1375 bfd *output_bfd, char **error_message)
1376 {
1377 /* If this is a relocatable link (output_bfd test tells us), just
1378 call the generic function. Any adjustment will be done at final
1379 link time. */
1380 if (output_bfd != NULL)
1381 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1382 input_section, output_bfd, error_message);
1383
1384 /* Subtract the symbol section base address. */
1385 reloc_entry->addend -= symbol->section->output_section->vma;
1386
1387 /* Adjust the addend for sign extension of the low 16 bits. */
1388 reloc_entry->addend += 0x8000;
1389 return bfd_reloc_continue;
1390 }
1391
1392 static bfd_reloc_status_type
1393 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1394 void *data, asection *input_section,
1395 bfd *output_bfd, char **error_message)
1396 {
1397 bfd_vma TOCstart;
1398
1399 /* If this is a relocatable link (output_bfd test tells us), just
1400 call the generic function. Any adjustment will be done at final
1401 link time. */
1402 if (output_bfd != NULL)
1403 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1404 input_section, output_bfd, error_message);
1405
1406 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1407 if (TOCstart == 0)
1408 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1409
1410 /* Subtract the TOC base address. */
1411 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1412 return bfd_reloc_continue;
1413 }
1414
1415 static bfd_reloc_status_type
1416 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1417 void *data, asection *input_section,
1418 bfd *output_bfd, char **error_message)
1419 {
1420 bfd_vma TOCstart;
1421
1422 /* If this is a relocatable link (output_bfd test tells us), just
1423 call the generic function. Any adjustment will be done at final
1424 link time. */
1425 if (output_bfd != NULL)
1426 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1427 input_section, output_bfd, error_message);
1428
1429 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1430 if (TOCstart == 0)
1431 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1432
1433 /* Subtract the TOC base address. */
1434 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1435
1436 /* Adjust the addend for sign extension of the low 16 bits. */
1437 reloc_entry->addend += 0x8000;
1438 return bfd_reloc_continue;
1439 }
1440
1441 static bfd_reloc_status_type
1442 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1443 void *data, asection *input_section,
1444 bfd *output_bfd, char **error_message)
1445 {
1446 bfd_vma TOCstart;
1447 bfd_size_type octets;
1448
1449 /* If this is a relocatable link (output_bfd test tells us), just
1450 call the generic function. Any adjustment will be done at final
1451 link time. */
1452 if (output_bfd != NULL)
1453 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1454 input_section, output_bfd, error_message);
1455
1456 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1457 if (TOCstart == 0)
1458 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1459
1460 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1461 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1462 return bfd_reloc_ok;
1463 }
1464
1465 static bfd_reloc_status_type
1466 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1467 void *data, asection *input_section,
1468 bfd *output_bfd, char **error_message)
1469 {
1470 /* If this is a relocatable link (output_bfd test tells us), just
1471 call the generic function. Any adjustment will be done at final
1472 link time. */
1473 if (output_bfd != NULL)
1474 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1475 input_section, output_bfd, error_message);
1476
1477 if (error_message != NULL)
1478 {
1479 static char buf[60];
1480 sprintf (buf, "generic linker can't handle %s",
1481 reloc_entry->howto->name);
1482 *error_message = buf;
1483 }
1484 return bfd_reloc_dangerous;
1485 }
1486
1487 /* Track GOT entries needed for a given symbol. We might need more
1488 than one got entry per symbol. */
1489 struct got_entry
1490 {
1491 struct got_entry *next;
1492
1493 /* The symbol addend that we'll be placing in the GOT. */
1494 bfd_vma addend;
1495
1496 /* Unlike other ELF targets, we use separate GOT entries for the same
1497 symbol referenced from different input files. This is to support
1498 automatic multiple TOC/GOT sections, where the TOC base can vary
1499 from one input file to another. After partitioning into TOC groups
1500 we merge entries within the group.
1501
1502 Point to the BFD owning this GOT entry. */
1503 bfd *owner;
1504
1505 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1506 TLS_TPREL or TLS_DTPREL for tls entries. */
1507 unsigned char tls_type;
1508
1509 /* Non-zero if got.ent points to real entry. */
1510 unsigned char is_indirect;
1511
1512 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1513 union
1514 {
1515 bfd_signed_vma refcount;
1516 bfd_vma offset;
1517 struct got_entry *ent;
1518 } got;
1519 };
1520
1521 /* The same for PLT. */
1522 struct plt_entry
1523 {
1524 struct plt_entry *next;
1525
1526 bfd_vma addend;
1527
1528 union
1529 {
1530 bfd_signed_vma refcount;
1531 bfd_vma offset;
1532 } plt;
1533 };
1534
1535 struct ppc64_elf_obj_tdata
1536 {
1537 struct elf_obj_tdata elf;
1538
1539 /* Shortcuts to dynamic linker sections. */
1540 asection *got;
1541 asection *relgot;
1542
1543 /* Used during garbage collection. We attach global symbols defined
1544 on removed .opd entries to this section so that the sym is removed. */
1545 asection *deleted_section;
1546
1547 /* TLS local dynamic got entry handling. Support for multiple GOT
1548 sections means we potentially need one of these for each input bfd. */
1549 struct got_entry tlsld_got;
1550
1551 union
1552 {
1553 /* A copy of relocs before they are modified for --emit-relocs. */
1554 Elf_Internal_Rela *relocs;
1555
1556 /* Section contents. */
1557 bfd_byte *contents;
1558 } opd;
1559
1560 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1561 the reloc to be in the range -32768 to 32767. */
1562 unsigned int has_small_toc_reloc : 1;
1563
1564 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1565 instruction not one we handle. */
1566 unsigned int unexpected_toc_insn : 1;
1567 };
1568
1569 #define ppc64_elf_tdata(bfd) \
1570 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1571
1572 #define ppc64_tlsld_got(bfd) \
1573 (&ppc64_elf_tdata (bfd)->tlsld_got)
1574
1575 #define is_ppc64_elf(bfd) \
1576 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1577 && elf_object_id (bfd) == PPC64_ELF_DATA)
1578
1579 /* Override the generic function because we store some extras. */
1580
1581 static bfd_boolean
1582 ppc64_elf_mkobject (bfd *abfd)
1583 {
1584 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1585 PPC64_ELF_DATA);
1586 }
1587
1588 /* Fix bad default arch selected for a 64 bit input bfd when the
1589 default is 32 bit. Also select arch based on apuinfo. */
1590
1591 static bfd_boolean
1592 ppc64_elf_object_p (bfd *abfd)
1593 {
1594 if (!abfd->arch_info->the_default)
1595 return TRUE;
1596
1597 if (abfd->arch_info->bits_per_word == 32)
1598 {
1599 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1600
1601 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1602 {
1603 /* Relies on arch after 32 bit default being 64 bit default. */
1604 abfd->arch_info = abfd->arch_info->next;
1605 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1606 }
1607 }
1608 return _bfd_elf_ppc_set_arch (abfd);
1609 }
1610
1611 /* Support for core dump NOTE sections. */
1612
1613 static bfd_boolean
1614 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1615 {
1616 size_t offset, size;
1617
1618 if (note->descsz != 504)
1619 return FALSE;
1620
1621 /* pr_cursig */
1622 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1623
1624 /* pr_pid */
1625 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1626
1627 /* pr_reg */
1628 offset = 112;
1629 size = 384;
1630
1631 /* Make a ".reg/999" section. */
1632 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1633 size, note->descpos + offset);
1634 }
1635
1636 static bfd_boolean
1637 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1638 {
1639 if (note->descsz != 136)
1640 return FALSE;
1641
1642 elf_tdata (abfd)->core->pid
1643 = bfd_get_32 (abfd, note->descdata + 24);
1644 elf_tdata (abfd)->core->program
1645 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1646 elf_tdata (abfd)->core->command
1647 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1648
1649 return TRUE;
1650 }
1651
1652 static char *
1653 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1654 ...)
1655 {
1656 switch (note_type)
1657 {
1658 default:
1659 return NULL;
1660
1661 case NT_PRPSINFO:
1662 {
1663 char data[136] ATTRIBUTE_NONSTRING;
1664 va_list ap;
1665
1666 va_start (ap, note_type);
1667 memset (data, 0, sizeof (data));
1668 strncpy (data + 40, va_arg (ap, const char *), 16);
1669 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1670 DIAGNOSTIC_PUSH;
1671 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1672 -Wstringop-truncation:
1673 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1674 */
1675 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1676 #endif
1677 strncpy (data + 56, va_arg (ap, const char *), 80);
1678 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1679 DIAGNOSTIC_POP;
1680 #endif
1681 va_end (ap);
1682 return elfcore_write_note (abfd, buf, bufsiz,
1683 "CORE", note_type, data, sizeof (data));
1684 }
1685
1686 case NT_PRSTATUS:
1687 {
1688 char data[504];
1689 va_list ap;
1690 long pid;
1691 int cursig;
1692 const void *greg;
1693
1694 va_start (ap, note_type);
1695 memset (data, 0, 112);
1696 pid = va_arg (ap, long);
1697 bfd_put_32 (abfd, pid, data + 32);
1698 cursig = va_arg (ap, int);
1699 bfd_put_16 (abfd, cursig, data + 12);
1700 greg = va_arg (ap, const void *);
1701 memcpy (data + 112, greg, 384);
1702 memset (data + 496, 0, 8);
1703 va_end (ap);
1704 return elfcore_write_note (abfd, buf, bufsiz,
1705 "CORE", note_type, data, sizeof (data));
1706 }
1707 }
1708 }
1709
1710 /* Add extra PPC sections. */
1711
1712 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1713 {
1714 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1715 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1716 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1717 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1718 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1719 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1720 { NULL, 0, 0, 0, 0 }
1721 };
1722
1723 enum _ppc64_sec_type {
1724 sec_normal = 0,
1725 sec_opd = 1,
1726 sec_toc = 2
1727 };
1728
1729 struct _ppc64_elf_section_data
1730 {
1731 struct bfd_elf_section_data elf;
1732
1733 union
1734 {
1735 /* An array with one entry for each opd function descriptor,
1736 and some spares since opd entries may be either 16 or 24 bytes. */
1737 #define OPD_NDX(OFF) ((OFF) >> 4)
1738 struct _opd_sec_data
1739 {
1740 /* Points to the function code section for local opd entries. */
1741 asection **func_sec;
1742
1743 /* After editing .opd, adjust references to opd local syms. */
1744 long *adjust;
1745 } opd;
1746
1747 /* An array for toc sections, indexed by offset/8. */
1748 struct _toc_sec_data
1749 {
1750 /* Specifies the relocation symbol index used at a given toc offset. */
1751 unsigned *symndx;
1752
1753 /* And the relocation addend. */
1754 bfd_vma *add;
1755 } toc;
1756 } u;
1757
1758 enum _ppc64_sec_type sec_type:2;
1759
1760 /* Flag set when small branches are detected. Used to
1761 select suitable defaults for the stub group size. */
1762 unsigned int has_14bit_branch:1;
1763
1764 /* Flag set when PLTCALL relocs are detected. */
1765 unsigned int has_pltcall:1;
1766 };
1767
1768 #define ppc64_elf_section_data(sec) \
1769 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
1770
1771 static bfd_boolean
1772 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
1773 {
1774 if (!sec->used_by_bfd)
1775 {
1776 struct _ppc64_elf_section_data *sdata;
1777 bfd_size_type amt = sizeof (*sdata);
1778
1779 sdata = bfd_zalloc (abfd, amt);
1780 if (sdata == NULL)
1781 return FALSE;
1782 sec->used_by_bfd = sdata;
1783 }
1784
1785 return _bfd_elf_new_section_hook (abfd, sec);
1786 }
1787
1788 static struct _opd_sec_data *
1789 get_opd_info (asection * sec)
1790 {
1791 if (sec != NULL
1792 && ppc64_elf_section_data (sec) != NULL
1793 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
1794 return &ppc64_elf_section_data (sec)->u.opd;
1795 return NULL;
1796 }
1797 \f
1798 /* Parameters for the qsort hook. */
1799 static bfd_boolean synthetic_relocatable;
1800 static asection *synthetic_opd;
1801
1802 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
1803
1804 static int
1805 compare_symbols (const void *ap, const void *bp)
1806 {
1807 const asymbol *a = *(const asymbol **) ap;
1808 const asymbol *b = *(const asymbol **) bp;
1809
1810 /* Section symbols first. */
1811 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
1812 return -1;
1813 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
1814 return 1;
1815
1816 /* then .opd symbols. */
1817 if (synthetic_opd != NULL)
1818 {
1819 if (strcmp (a->section->name, ".opd") == 0
1820 && strcmp (b->section->name, ".opd") != 0)
1821 return -1;
1822 if (strcmp (a->section->name, ".opd") != 0
1823 && strcmp (b->section->name, ".opd") == 0)
1824 return 1;
1825 }
1826
1827 /* then other code symbols. */
1828 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1829 == (SEC_CODE | SEC_ALLOC))
1830 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1831 != (SEC_CODE | SEC_ALLOC)))
1832 return -1;
1833
1834 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1835 != (SEC_CODE | SEC_ALLOC))
1836 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1837 == (SEC_CODE | SEC_ALLOC)))
1838 return 1;
1839
1840 if (synthetic_relocatable)
1841 {
1842 if (a->section->id < b->section->id)
1843 return -1;
1844
1845 if (a->section->id > b->section->id)
1846 return 1;
1847 }
1848
1849 if (a->value + a->section->vma < b->value + b->section->vma)
1850 return -1;
1851
1852 if (a->value + a->section->vma > b->value + b->section->vma)
1853 return 1;
1854
1855 /* For syms with the same value, prefer strong dynamic global function
1856 syms over other syms. */
1857 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
1858 return -1;
1859
1860 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
1861 return 1;
1862
1863 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
1864 return -1;
1865
1866 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
1867 return 1;
1868
1869 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
1870 return -1;
1871
1872 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
1873 return 1;
1874
1875 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
1876 return -1;
1877
1878 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
1879 return 1;
1880
1881 return a > b;
1882 }
1883
1884 /* Search SYMS for a symbol of the given VALUE. */
1885
1886 static asymbol *
1887 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
1888 {
1889 long mid;
1890
1891 if (id == (unsigned) -1)
1892 {
1893 while (lo < hi)
1894 {
1895 mid = (lo + hi) >> 1;
1896 if (syms[mid]->value + syms[mid]->section->vma < value)
1897 lo = mid + 1;
1898 else if (syms[mid]->value + syms[mid]->section->vma > value)
1899 hi = mid;
1900 else
1901 return syms[mid];
1902 }
1903 }
1904 else
1905 {
1906 while (lo < hi)
1907 {
1908 mid = (lo + hi) >> 1;
1909 if (syms[mid]->section->id < id)
1910 lo = mid + 1;
1911 else if (syms[mid]->section->id > id)
1912 hi = mid;
1913 else if (syms[mid]->value < value)
1914 lo = mid + 1;
1915 else if (syms[mid]->value > value)
1916 hi = mid;
1917 else
1918 return syms[mid];
1919 }
1920 }
1921 return NULL;
1922 }
1923
1924 static bfd_boolean
1925 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
1926 {
1927 bfd_vma vma = *(bfd_vma *) ptr;
1928 return ((section->flags & SEC_ALLOC) != 0
1929 && section->vma <= vma
1930 && vma < section->vma + section->size);
1931 }
1932
1933 /* Create synthetic symbols, effectively restoring "dot-symbol" function
1934 entry syms. Also generate @plt symbols for the glink branch table.
1935 Returns count of synthetic symbols in RET or -1 on error. */
1936
1937 static long
1938 ppc64_elf_get_synthetic_symtab (bfd *abfd,
1939 long static_count, asymbol **static_syms,
1940 long dyn_count, asymbol **dyn_syms,
1941 asymbol **ret)
1942 {
1943 asymbol *s;
1944 size_t i, j, count;
1945 char *names;
1946 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
1947 asection *opd = NULL;
1948 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
1949 asymbol **syms;
1950 int abi = abiversion (abfd);
1951
1952 *ret = NULL;
1953
1954 if (abi < 2)
1955 {
1956 opd = bfd_get_section_by_name (abfd, ".opd");
1957 if (opd == NULL && abi == 1)
1958 return 0;
1959 }
1960
1961 syms = NULL;
1962 codesecsym = 0;
1963 codesecsymend = 0;
1964 secsymend = 0;
1965 opdsymend = 0;
1966 symcount = 0;
1967 if (opd != NULL)
1968 {
1969 symcount = static_count;
1970 if (!relocatable)
1971 symcount += dyn_count;
1972 if (symcount == 0)
1973 return 0;
1974
1975 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
1976 if (syms == NULL)
1977 return -1;
1978
1979 if (!relocatable && static_count != 0 && dyn_count != 0)
1980 {
1981 /* Use both symbol tables. */
1982 memcpy (syms, static_syms, static_count * sizeof (*syms));
1983 memcpy (syms + static_count, dyn_syms,
1984 (dyn_count + 1) * sizeof (*syms));
1985 }
1986 else if (!relocatable && static_count == 0)
1987 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
1988 else
1989 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
1990
1991 /* Trim uninteresting symbols. Interesting symbols are section,
1992 function, and notype symbols. */
1993 for (i = 0, j = 0; i < symcount; ++i)
1994 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
1995 | BSF_RELC | BSF_SRELC)) == 0)
1996 syms[j++] = syms[i];
1997 symcount = j;
1998
1999 synthetic_relocatable = relocatable;
2000 synthetic_opd = opd;
2001 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2002
2003 if (!relocatable && symcount > 1)
2004 {
2005 /* Trim duplicate syms, since we may have merged the normal
2006 and dynamic symbols. Actually, we only care about syms
2007 that have different values, so trim any with the same
2008 value. Don't consider ifunc and ifunc resolver symbols
2009 duplicates however, because GDB wants to know whether a
2010 text symbol is an ifunc resolver. */
2011 for (i = 1, j = 1; i < symcount; ++i)
2012 {
2013 const asymbol *s0 = syms[i - 1];
2014 const asymbol *s1 = syms[i];
2015
2016 if ((s0->value + s0->section->vma
2017 != s1->value + s1->section->vma)
2018 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2019 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2020 syms[j++] = syms[i];
2021 }
2022 symcount = j;
2023 }
2024
2025 i = 0;
2026 /* Note that here and in compare_symbols we can't compare opd and
2027 sym->section directly. With separate debug info files, the
2028 symbols will be extracted from the debug file while abfd passed
2029 to this function is the real binary. */
2030 if (strcmp (syms[i]->section->name, ".opd") == 0)
2031 ++i;
2032 codesecsym = i;
2033
2034 for (; i < symcount; ++i)
2035 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2036 | SEC_THREAD_LOCAL))
2037 != (SEC_CODE | SEC_ALLOC))
2038 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2039 break;
2040 codesecsymend = i;
2041
2042 for (; i < symcount; ++i)
2043 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2044 break;
2045 secsymend = i;
2046
2047 for (; i < symcount; ++i)
2048 if (strcmp (syms[i]->section->name, ".opd") != 0)
2049 break;
2050 opdsymend = i;
2051
2052 for (; i < symcount; ++i)
2053 if (((syms[i]->section->flags
2054 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2055 != (SEC_CODE | SEC_ALLOC))
2056 break;
2057 symcount = i;
2058 }
2059 count = 0;
2060
2061 if (relocatable)
2062 {
2063 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2064 arelent *r;
2065 size_t size;
2066 size_t relcount;
2067
2068 if (opdsymend == secsymend)
2069 goto done;
2070
2071 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2072 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2073 if (relcount == 0)
2074 goto done;
2075
2076 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2077 {
2078 count = -1;
2079 goto done;
2080 }
2081
2082 size = 0;
2083 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2084 {
2085 asymbol *sym;
2086
2087 while (r < opd->relocation + relcount
2088 && r->address < syms[i]->value + opd->vma)
2089 ++r;
2090
2091 if (r == opd->relocation + relcount)
2092 break;
2093
2094 if (r->address != syms[i]->value + opd->vma)
2095 continue;
2096
2097 if (r->howto->type != R_PPC64_ADDR64)
2098 continue;
2099
2100 sym = *r->sym_ptr_ptr;
2101 if (!sym_exists_at (syms, opdsymend, symcount,
2102 sym->section->id, sym->value + r->addend))
2103 {
2104 ++count;
2105 size += sizeof (asymbol);
2106 size += strlen (syms[i]->name) + 2;
2107 }
2108 }
2109
2110 if (size == 0)
2111 goto done;
2112 s = *ret = bfd_malloc (size);
2113 if (s == NULL)
2114 {
2115 count = -1;
2116 goto done;
2117 }
2118
2119 names = (char *) (s + count);
2120
2121 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2122 {
2123 asymbol *sym;
2124
2125 while (r < opd->relocation + relcount
2126 && r->address < syms[i]->value + opd->vma)
2127 ++r;
2128
2129 if (r == opd->relocation + relcount)
2130 break;
2131
2132 if (r->address != syms[i]->value + opd->vma)
2133 continue;
2134
2135 if (r->howto->type != R_PPC64_ADDR64)
2136 continue;
2137
2138 sym = *r->sym_ptr_ptr;
2139 if (!sym_exists_at (syms, opdsymend, symcount,
2140 sym->section->id, sym->value + r->addend))
2141 {
2142 size_t len;
2143
2144 *s = *syms[i];
2145 s->flags |= BSF_SYNTHETIC;
2146 s->section = sym->section;
2147 s->value = sym->value + r->addend;
2148 s->name = names;
2149 *names++ = '.';
2150 len = strlen (syms[i]->name);
2151 memcpy (names, syms[i]->name, len + 1);
2152 names += len + 1;
2153 /* Have udata.p point back to the original symbol this
2154 synthetic symbol was derived from. */
2155 s->udata.p = syms[i];
2156 s++;
2157 }
2158 }
2159 }
2160 else
2161 {
2162 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2163 bfd_byte *contents = NULL;
2164 size_t size;
2165 size_t plt_count = 0;
2166 bfd_vma glink_vma = 0, resolv_vma = 0;
2167 asection *dynamic, *glink = NULL, *relplt = NULL;
2168 arelent *p;
2169
2170 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2171 {
2172 free_contents_and_exit_err:
2173 count = -1;
2174 free_contents_and_exit:
2175 if (contents)
2176 free (contents);
2177 goto done;
2178 }
2179
2180 size = 0;
2181 for (i = secsymend; i < opdsymend; ++i)
2182 {
2183 bfd_vma ent;
2184
2185 /* Ignore bogus symbols. */
2186 if (syms[i]->value > opd->size - 8)
2187 continue;
2188
2189 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2190 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2191 {
2192 ++count;
2193 size += sizeof (asymbol);
2194 size += strlen (syms[i]->name) + 2;
2195 }
2196 }
2197
2198 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2199 if (dyn_count != 0
2200 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2201 {
2202 bfd_byte *dynbuf, *extdyn, *extdynend;
2203 size_t extdynsize;
2204 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2205
2206 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2207 goto free_contents_and_exit_err;
2208
2209 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2210 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2211
2212 extdyn = dynbuf;
2213 extdynend = extdyn + dynamic->size;
2214 for (; extdyn < extdynend; extdyn += extdynsize)
2215 {
2216 Elf_Internal_Dyn dyn;
2217 (*swap_dyn_in) (abfd, extdyn, &dyn);
2218
2219 if (dyn.d_tag == DT_NULL)
2220 break;
2221
2222 if (dyn.d_tag == DT_PPC64_GLINK)
2223 {
2224 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2225 See comment in ppc64_elf_finish_dynamic_sections. */
2226 glink_vma = dyn.d_un.d_val + 8 * 4;
2227 /* The .glink section usually does not survive the final
2228 link; search for the section (usually .text) where the
2229 glink stubs now reside. */
2230 glink = bfd_sections_find_if (abfd, section_covers_vma,
2231 &glink_vma);
2232 break;
2233 }
2234 }
2235
2236 free (dynbuf);
2237 }
2238
2239 if (glink != NULL)
2240 {
2241 /* Determine __glink trampoline by reading the relative branch
2242 from the first glink stub. */
2243 bfd_byte buf[4];
2244 unsigned int off = 0;
2245
2246 while (bfd_get_section_contents (abfd, glink, buf,
2247 glink_vma + off - glink->vma, 4))
2248 {
2249 unsigned int insn = bfd_get_32 (abfd, buf);
2250 insn ^= B_DOT;
2251 if ((insn & ~0x3fffffc) == 0)
2252 {
2253 resolv_vma
2254 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2255 break;
2256 }
2257 off += 4;
2258 if (off > 4)
2259 break;
2260 }
2261
2262 if (resolv_vma)
2263 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2264
2265 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2266 if (relplt != NULL)
2267 {
2268 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2269 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
2270 goto free_contents_and_exit_err;
2271
2272 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2273 size += plt_count * sizeof (asymbol);
2274
2275 p = relplt->relocation;
2276 for (i = 0; i < plt_count; i++, p++)
2277 {
2278 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2279 if (p->addend != 0)
2280 size += sizeof ("+0x") - 1 + 16;
2281 }
2282 }
2283 }
2284
2285 if (size == 0)
2286 goto free_contents_and_exit;
2287 s = *ret = bfd_malloc (size);
2288 if (s == NULL)
2289 goto free_contents_and_exit_err;
2290
2291 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2292
2293 for (i = secsymend; i < opdsymend; ++i)
2294 {
2295 bfd_vma ent;
2296
2297 if (syms[i]->value > opd->size - 8)
2298 continue;
2299
2300 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2301 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2302 {
2303 size_t lo, hi;
2304 size_t len;
2305 asection *sec = abfd->sections;
2306
2307 *s = *syms[i];
2308 lo = codesecsym;
2309 hi = codesecsymend;
2310 while (lo < hi)
2311 {
2312 size_t mid = (lo + hi) >> 1;
2313 if (syms[mid]->section->vma < ent)
2314 lo = mid + 1;
2315 else if (syms[mid]->section->vma > ent)
2316 hi = mid;
2317 else
2318 {
2319 sec = syms[mid]->section;
2320 break;
2321 }
2322 }
2323
2324 if (lo >= hi && lo > codesecsym)
2325 sec = syms[lo - 1]->section;
2326
2327 for (; sec != NULL; sec = sec->next)
2328 {
2329 if (sec->vma > ent)
2330 break;
2331 /* SEC_LOAD may not be set if SEC is from a separate debug
2332 info file. */
2333 if ((sec->flags & SEC_ALLOC) == 0)
2334 break;
2335 if ((sec->flags & SEC_CODE) != 0)
2336 s->section = sec;
2337 }
2338 s->flags |= BSF_SYNTHETIC;
2339 s->value = ent - s->section->vma;
2340 s->name = names;
2341 *names++ = '.';
2342 len = strlen (syms[i]->name);
2343 memcpy (names, syms[i]->name, len + 1);
2344 names += len + 1;
2345 /* Have udata.p point back to the original symbol this
2346 synthetic symbol was derived from. */
2347 s->udata.p = syms[i];
2348 s++;
2349 }
2350 }
2351 free (contents);
2352
2353 if (glink != NULL && relplt != NULL)
2354 {
2355 if (resolv_vma)
2356 {
2357 /* Add a symbol for the main glink trampoline. */
2358 memset (s, 0, sizeof *s);
2359 s->the_bfd = abfd;
2360 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2361 s->section = glink;
2362 s->value = resolv_vma - glink->vma;
2363 s->name = names;
2364 memcpy (names, "__glink_PLTresolve",
2365 sizeof ("__glink_PLTresolve"));
2366 names += sizeof ("__glink_PLTresolve");
2367 s++;
2368 count++;
2369 }
2370
2371 /* FIXME: It would be very much nicer to put sym@plt on the
2372 stub rather than on the glink branch table entry. The
2373 objdump disassembler would then use a sensible symbol
2374 name on plt calls. The difficulty in doing so is
2375 a) finding the stubs, and,
2376 b) matching stubs against plt entries, and,
2377 c) there can be multiple stubs for a given plt entry.
2378
2379 Solving (a) could be done by code scanning, but older
2380 ppc64 binaries used different stubs to current code.
2381 (b) is the tricky one since you need to known the toc
2382 pointer for at least one function that uses a pic stub to
2383 be able to calculate the plt address referenced.
2384 (c) means gdb would need to set multiple breakpoints (or
2385 find the glink branch itself) when setting breakpoints
2386 for pending shared library loads. */
2387 p = relplt->relocation;
2388 for (i = 0; i < plt_count; i++, p++)
2389 {
2390 size_t len;
2391
2392 *s = **p->sym_ptr_ptr;
2393 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2394 we are defining a symbol, ensure one of them is set. */
2395 if ((s->flags & BSF_LOCAL) == 0)
2396 s->flags |= BSF_GLOBAL;
2397 s->flags |= BSF_SYNTHETIC;
2398 s->section = glink;
2399 s->value = glink_vma - glink->vma;
2400 s->name = names;
2401 s->udata.p = NULL;
2402 len = strlen ((*p->sym_ptr_ptr)->name);
2403 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2404 names += len;
2405 if (p->addend != 0)
2406 {
2407 memcpy (names, "+0x", sizeof ("+0x") - 1);
2408 names += sizeof ("+0x") - 1;
2409 bfd_sprintf_vma (abfd, names, p->addend);
2410 names += strlen (names);
2411 }
2412 memcpy (names, "@plt", sizeof ("@plt"));
2413 names += sizeof ("@plt");
2414 s++;
2415 if (abi < 2)
2416 {
2417 glink_vma += 8;
2418 if (i >= 0x8000)
2419 glink_vma += 4;
2420 }
2421 else
2422 glink_vma += 4;
2423 }
2424 count += plt_count;
2425 }
2426 }
2427
2428 done:
2429 free (syms);
2430 return count;
2431 }
2432 \f
2433 /* The following functions are specific to the ELF linker, while
2434 functions above are used generally. Those named ppc64_elf_* are
2435 called by the main ELF linker code. They appear in this file more
2436 or less in the order in which they are called. eg.
2437 ppc64_elf_check_relocs is called early in the link process,
2438 ppc64_elf_finish_dynamic_sections is one of the last functions
2439 called.
2440
2441 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2442 functions have both a function code symbol and a function descriptor
2443 symbol. A call to foo in a relocatable object file looks like:
2444
2445 . .text
2446 . x:
2447 . bl .foo
2448 . nop
2449
2450 The function definition in another object file might be:
2451
2452 . .section .opd
2453 . foo: .quad .foo
2454 . .quad .TOC.@tocbase
2455 . .quad 0
2456 .
2457 . .text
2458 . .foo: blr
2459
2460 When the linker resolves the call during a static link, the branch
2461 unsurprisingly just goes to .foo and the .opd information is unused.
2462 If the function definition is in a shared library, things are a little
2463 different: The call goes via a plt call stub, the opd information gets
2464 copied to the plt, and the linker patches the nop.
2465
2466 . x:
2467 . bl .foo_stub
2468 . ld 2,40(1)
2469 .
2470 .
2471 . .foo_stub:
2472 . std 2,40(1) # in practice, the call stub
2473 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2474 . addi 11,11,Lfoo@toc@l # this is the general idea
2475 . ld 12,0(11)
2476 . ld 2,8(11)
2477 . mtctr 12
2478 . ld 11,16(11)
2479 . bctr
2480 .
2481 . .section .plt
2482 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2483
2484 The "reloc ()" notation is supposed to indicate that the linker emits
2485 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2486 copying.
2487
2488 What are the difficulties here? Well, firstly, the relocations
2489 examined by the linker in check_relocs are against the function code
2490 sym .foo, while the dynamic relocation in the plt is emitted against
2491 the function descriptor symbol, foo. Somewhere along the line, we need
2492 to carefully copy dynamic link information from one symbol to the other.
2493 Secondly, the generic part of the elf linker will make .foo a dynamic
2494 symbol as is normal for most other backends. We need foo dynamic
2495 instead, at least for an application final link. However, when
2496 creating a shared library containing foo, we need to have both symbols
2497 dynamic so that references to .foo are satisfied during the early
2498 stages of linking. Otherwise the linker might decide to pull in a
2499 definition from some other object, eg. a static library.
2500
2501 Update: As of August 2004, we support a new convention. Function
2502 calls may use the function descriptor symbol, ie. "bl foo". This
2503 behaves exactly as "bl .foo". */
2504
2505 /* Of those relocs that might be copied as dynamic relocs, this
2506 function selects those that must be copied when linking a shared
2507 library or PIE, even when the symbol is local. */
2508
2509 static int
2510 must_be_dyn_reloc (struct bfd_link_info *info,
2511 enum elf_ppc64_reloc_type r_type)
2512 {
2513 switch (r_type)
2514 {
2515 default:
2516 /* Only relative relocs can be resolved when the object load
2517 address isn't fixed. DTPREL64 is excluded because the
2518 dynamic linker needs to differentiate global dynamic from
2519 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2520 return 1;
2521
2522 case R_PPC64_REL32:
2523 case R_PPC64_REL64:
2524 case R_PPC64_REL30:
2525 return 0;
2526
2527 case R_PPC64_TPREL16:
2528 case R_PPC64_TPREL16_LO:
2529 case R_PPC64_TPREL16_HI:
2530 case R_PPC64_TPREL16_HA:
2531 case R_PPC64_TPREL16_DS:
2532 case R_PPC64_TPREL16_LO_DS:
2533 case R_PPC64_TPREL16_HIGH:
2534 case R_PPC64_TPREL16_HIGHA:
2535 case R_PPC64_TPREL16_HIGHER:
2536 case R_PPC64_TPREL16_HIGHERA:
2537 case R_PPC64_TPREL16_HIGHEST:
2538 case R_PPC64_TPREL16_HIGHESTA:
2539 case R_PPC64_TPREL64:
2540 /* These relocations are relative but in a shared library the
2541 linker doesn't know the thread pointer base. */
2542 return bfd_link_dll (info);
2543 }
2544 }
2545
2546 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2547 copying dynamic variables from a shared lib into an app's dynbss
2548 section, and instead use a dynamic relocation to point into the
2549 shared lib. With code that gcc generates, it's vital that this be
2550 enabled; In the PowerPC64 ABI, the address of a function is actually
2551 the address of a function descriptor, which resides in the .opd
2552 section. gcc uses the descriptor directly rather than going via the
2553 GOT as some other ABI's do, which means that initialized function
2554 pointers must reference the descriptor. Thus, a function pointer
2555 initialized to the address of a function in a shared library will
2556 either require a copy reloc, or a dynamic reloc. Using a copy reloc
2557 redefines the function descriptor symbol to point to the copy. This
2558 presents a problem as a plt entry for that function is also
2559 initialized from the function descriptor symbol and the copy reloc
2560 may not be initialized first. */
2561 #define ELIMINATE_COPY_RELOCS 1
2562
2563 /* Section name for stubs is the associated section name plus this
2564 string. */
2565 #define STUB_SUFFIX ".stub"
2566
2567 /* Linker stubs.
2568 ppc_stub_long_branch:
2569 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2570 destination, but a 24 bit branch in a stub section will reach.
2571 . b dest
2572
2573 ppc_stub_plt_branch:
2574 Similar to the above, but a 24 bit branch in the stub section won't
2575 reach its destination.
2576 . addis %r11,%r2,xxx@toc@ha
2577 . ld %r12,xxx@toc@l(%r11)
2578 . mtctr %r12
2579 . bctr
2580
2581 ppc_stub_plt_call:
2582 Used to call a function in a shared library. If it so happens that
2583 the plt entry referenced crosses a 64k boundary, then an extra
2584 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2585 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
2586 . addis %r11,%r2,xxx@toc@ha
2587 . ld %r12,xxx+0@toc@l(%r11)
2588 . mtctr %r12
2589 . ld %r2,xxx+8@toc@l(%r11)
2590 . ld %r11,xxx+16@toc@l(%r11)
2591 . bctr
2592
2593 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2594 code to adjust the value and save r2 to support multiple toc sections.
2595 A ppc_stub_long_branch with an r2 offset looks like:
2596 . std %r2,40(%r1)
2597 . addis %r2,%r2,off@ha
2598 . addi %r2,%r2,off@l
2599 . b dest
2600
2601 A ppc_stub_plt_branch with an r2 offset looks like:
2602 . std %r2,40(%r1)
2603 . addis %r11,%r2,xxx@toc@ha
2604 . ld %r12,xxx@toc@l(%r11)
2605 . addis %r2,%r2,off@ha
2606 . addi %r2,%r2,off@l
2607 . mtctr %r12
2608 . bctr
2609
2610 All of the above stubs are shown as their ELFv1 variants. ELFv2
2611 variants exist too, simpler for plt calls since a new toc pointer
2612 and static chain are not loaded by the stub. In addition, ELFv2
2613 has some more complex stubs to handle calls marked with NOTOC
2614 relocs from functions where r2 is not a valid toc pointer. These
2615 come in two flavours, the ones shown below, and _both variants that
2616 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2617 one call is from a function where r2 is used as the toc pointer but
2618 needs a toc adjusting stub for small-model multi-toc, and another
2619 call is from a function where r2 is not valid.
2620 ppc_stub_long_branch_notoc:
2621 . mflr %r12
2622 . bcl 20,31,1f
2623 . 1:
2624 . mflr %r11
2625 . mtlr %r12
2626 . addis %r12,%r11,dest-1b@ha
2627 . addi %r12,%r12,dest-1b@l
2628 . b dest
2629
2630 ppc_stub_plt_branch_notoc:
2631 . mflr %r12
2632 . bcl 20,31,1f
2633 . 1:
2634 . mflr %r11
2635 . mtlr %r12
2636 . lis %r12,xxx-1b@highest
2637 . ori %r12,xxx-1b@higher
2638 . sldi %r12,%r12,32
2639 . oris %r12,%r12,xxx-1b@hi
2640 . ori %r12,%r12,xxx-1b@l
2641 . add %r12,%r11,%r12
2642 . mtctr %r12
2643 . bctr
2644
2645 ppc_stub_plt_call_notoc:
2646 . mflr %r12
2647 . bcl 20,31,1f
2648 . 1:
2649 . mflr %r11
2650 . mtlr %r12
2651 . lis %r12,xxx-1b@highest
2652 . ori %r12,xxx-1b@higher
2653 . sldi %r12,%r12,32
2654 . oris %r12,%r12,xxx-1b@hi
2655 . ori %r12,%r12,xxx-1b@l
2656 . ldx %r12,%r11,%r12
2657 . mtctr %r12
2658 . bctr
2659
2660 In cases where the high instructions would add zero, they are
2661 omitted and following instructions modified in some cases.
2662
2663 For a given stub group (a set of sections all using the same toc
2664 pointer value) there will be just one stub type used for any
2665 particular function symbol. For example, if printf is called from
2666 code with the tocsave optimization (ie. r2 saved in function
2667 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2668 and from other code without the tocsave optimization requiring a
2669 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2670 type will be created. Calls with the tocsave optimization will
2671 enter this stub after the instruction saving r2. A similar
2672 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2673 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2674 to call an external function like printf. If other calls to printf
2675 require a ppc_stub_plt_call linkage stub then a single
2676 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2677 call. If other calls to printf require a ppc_stub_plt_call_r2save
2678 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2679 be created and calls not requiring r2 to be saved will enter the
2680 stub after the r2 save instruction. There is an analogous
2681 hierarchy of long branch and plt branch stubs for local call
2682 linkage. */
2683
2684 enum ppc_stub_type
2685 {
2686 ppc_stub_none,
2687 ppc_stub_long_branch,
2688 ppc_stub_long_branch_r2off,
2689 ppc_stub_long_branch_notoc,
2690 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
2691 ppc_stub_plt_branch,
2692 ppc_stub_plt_branch_r2off,
2693 ppc_stub_plt_branch_notoc,
2694 ppc_stub_plt_branch_both,
2695 ppc_stub_plt_call,
2696 ppc_stub_plt_call_r2save,
2697 ppc_stub_plt_call_notoc,
2698 ppc_stub_plt_call_both,
2699 ppc_stub_global_entry,
2700 ppc_stub_save_res
2701 };
2702
2703 /* Information on stub grouping. */
2704 struct map_stub
2705 {
2706 /* The stub section. */
2707 asection *stub_sec;
2708 /* This is the section to which stubs in the group will be attached. */
2709 asection *link_sec;
2710 /* Next group. */
2711 struct map_stub *next;
2712 /* Whether to emit a copy of register save/restore functions in this
2713 group. */
2714 int needs_save_res;
2715 /* Current offset within stubs after the insn restoring lr in a
2716 _notoc or _both stub using bcl for pc-relative addressing, or
2717 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2718 unsigned int lr_restore;
2719 /* Accumulated size of EH info emitted to describe return address
2720 if stubs modify lr. Does not include 17 byte FDE header. */
2721 unsigned int eh_size;
2722 /* Offset in glink_eh_frame to the start of EH info for this group. */
2723 unsigned int eh_base;
2724 };
2725
2726 struct ppc_stub_hash_entry
2727 {
2728 /* Base hash table entry structure. */
2729 struct bfd_hash_entry root;
2730
2731 enum ppc_stub_type stub_type;
2732
2733 /* Group information. */
2734 struct map_stub *group;
2735
2736 /* Offset within stub_sec of the beginning of this stub. */
2737 bfd_vma stub_offset;
2738
2739 /* Given the symbol's value and its section we can determine its final
2740 value when building the stubs (so the stub knows where to jump. */
2741 bfd_vma target_value;
2742 asection *target_section;
2743
2744 /* The symbol table entry, if any, that this was derived from. */
2745 struct ppc_link_hash_entry *h;
2746 struct plt_entry *plt_ent;
2747
2748 /* Symbol type. */
2749 unsigned char symtype;
2750
2751 /* Symbol st_other. */
2752 unsigned char other;
2753 };
2754
2755 struct ppc_branch_hash_entry
2756 {
2757 /* Base hash table entry structure. */
2758 struct bfd_hash_entry root;
2759
2760 /* Offset within branch lookup table. */
2761 unsigned int offset;
2762
2763 /* Generation marker. */
2764 unsigned int iter;
2765 };
2766
2767 /* Used to track dynamic relocations for local symbols. */
2768 struct ppc_dyn_relocs
2769 {
2770 struct ppc_dyn_relocs *next;
2771
2772 /* The input section of the reloc. */
2773 asection *sec;
2774
2775 /* Total number of relocs copied for the input section. */
2776 unsigned int count : 31;
2777
2778 /* Whether this entry is for STT_GNU_IFUNC symbols. */
2779 unsigned int ifunc : 1;
2780 };
2781
2782 struct ppc_link_hash_entry
2783 {
2784 struct elf_link_hash_entry elf;
2785
2786 union
2787 {
2788 /* A pointer to the most recently used stub hash entry against this
2789 symbol. */
2790 struct ppc_stub_hash_entry *stub_cache;
2791
2792 /* A pointer to the next symbol starting with a '.' */
2793 struct ppc_link_hash_entry *next_dot_sym;
2794 } u;
2795
2796 /* Track dynamic relocs copied for this symbol. */
2797 struct elf_dyn_relocs *dyn_relocs;
2798
2799 /* Link between function code and descriptor symbols. */
2800 struct ppc_link_hash_entry *oh;
2801
2802 /* Flag function code and descriptor symbols. */
2803 unsigned int is_func:1;
2804 unsigned int is_func_descriptor:1;
2805 unsigned int fake:1;
2806
2807 /* Whether global opd/toc sym has been adjusted or not.
2808 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
2809 should be set for all globals defined in any opd/toc section. */
2810 unsigned int adjust_done:1;
2811
2812 /* Set if this is an out-of-line register save/restore function,
2813 with non-standard calling convention. */
2814 unsigned int save_res:1;
2815
2816 /* Set if a duplicate symbol with non-zero localentry is detected,
2817 even when the duplicate symbol does not provide a definition. */
2818 unsigned int non_zero_localentry:1;
2819
2820 /* Contexts in which symbol is used in the GOT (or TOC).
2821 Bits are or'd into the mask as the corresponding relocs are
2822 encountered during check_relocs, with TLS_TLS being set when any
2823 of the other TLS bits are set. tls_optimize clears bits when
2824 optimizing to indicate the corresponding GOT entry type is not
2825 needed. If set, TLS_TLS is never cleared. tls_optimize may also
2826 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
2827 separate flag rather than setting TPREL just for convenience in
2828 distinguishing the two cases.
2829 These flags are also kept for local symbols. */
2830 #define TLS_TLS 1 /* Any TLS reloc. */
2831 #define TLS_GD 2 /* GD reloc. */
2832 #define TLS_LD 4 /* LD reloc. */
2833 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
2834 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
2835 #define TLS_MARK 32 /* __tls_get_addr call marked. */
2836 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
2837 #define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
2838 unsigned char tls_mask;
2839
2840 /* The above field is also used to mark function symbols. In which
2841 case TLS_TLS will be 0. */
2842 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2843 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
2844 #define NON_GOT 256 /* local symbol plt, not stored. */
2845 };
2846
2847 /* ppc64 ELF linker hash table. */
2848
2849 struct ppc_link_hash_table
2850 {
2851 struct elf_link_hash_table elf;
2852
2853 /* The stub hash table. */
2854 struct bfd_hash_table stub_hash_table;
2855
2856 /* Another hash table for plt_branch stubs. */
2857 struct bfd_hash_table branch_hash_table;
2858
2859 /* Hash table for function prologue tocsave. */
2860 htab_t tocsave_htab;
2861
2862 /* Various options and other info passed from the linker. */
2863 struct ppc64_elf_params *params;
2864
2865 /* The size of sec_info below. */
2866 unsigned int sec_info_arr_size;
2867
2868 /* Per-section array of extra section info. Done this way rather
2869 than as part of ppc64_elf_section_data so we have the info for
2870 non-ppc64 sections. */
2871 struct
2872 {
2873 /* Along with elf_gp, specifies the TOC pointer used by this section. */
2874 bfd_vma toc_off;
2875
2876 union
2877 {
2878 /* The section group that this section belongs to. */
2879 struct map_stub *group;
2880 /* A temp section list pointer. */
2881 asection *list;
2882 } u;
2883 } *sec_info;
2884
2885 /* Linked list of groups. */
2886 struct map_stub *group;
2887
2888 /* Temp used when calculating TOC pointers. */
2889 bfd_vma toc_curr;
2890 bfd *toc_bfd;
2891 asection *toc_first_sec;
2892
2893 /* Used when adding symbols. */
2894 struct ppc_link_hash_entry *dot_syms;
2895
2896 /* Shortcuts to get to dynamic linker sections. */
2897 asection *glink;
2898 asection *global_entry;
2899 asection *sfpr;
2900 asection *pltlocal;
2901 asection *relpltlocal;
2902 asection *brlt;
2903 asection *relbrlt;
2904 asection *glink_eh_frame;
2905
2906 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
2907 struct ppc_link_hash_entry *tls_get_addr;
2908 struct ppc_link_hash_entry *tls_get_addr_fd;
2909
2910 /* The size of reliplt used by got entry relocs. */
2911 bfd_size_type got_reli_size;
2912
2913 /* Statistics. */
2914 unsigned long stub_count[ppc_stub_global_entry];
2915
2916 /* Number of stubs against global syms. */
2917 unsigned long stub_globals;
2918
2919 /* Set if we're linking code with function descriptors. */
2920 unsigned int opd_abi:1;
2921
2922 /* Support for multiple toc sections. */
2923 unsigned int do_multi_toc:1;
2924 unsigned int multi_toc_needed:1;
2925 unsigned int second_toc_pass:1;
2926 unsigned int do_toc_opt:1;
2927
2928 /* Set if tls optimization is enabled. */
2929 unsigned int do_tls_opt:1;
2930
2931 /* Set if inline plt calls should be converted to direct calls. */
2932 unsigned int can_convert_all_inline_plt:1;
2933
2934 /* Set on error. */
2935 unsigned int stub_error:1;
2936
2937 /* Whether func_desc_adjust needs to be run over symbols. */
2938 unsigned int need_func_desc_adj:1;
2939
2940 /* Whether there exist local gnu indirect function resolvers,
2941 referenced by dynamic relocations. */
2942 unsigned int local_ifunc_resolver:1;
2943 unsigned int maybe_local_ifunc_resolver:1;
2944
2945 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
2946 unsigned int has_plt_localentry0:1;
2947
2948 /* Incremented every time we size stubs. */
2949 unsigned int stub_iteration;
2950
2951 /* Small local sym cache. */
2952 struct sym_cache sym_cache;
2953 };
2954
2955 /* Rename some of the generic section flags to better document how they
2956 are used here. */
2957
2958 /* Nonzero if this section has TLS related relocations. */
2959 #define has_tls_reloc sec_flg0
2960
2961 /* Nonzero if this section has an old-style call to __tls_get_addr. */
2962 #define has_tls_get_addr_call sec_flg1
2963
2964 /* Nonzero if this section has any toc or got relocs. */
2965 #define has_toc_reloc sec_flg2
2966
2967 /* Nonzero if this section has a call to another section that uses
2968 the toc or got. */
2969 #define makes_toc_func_call sec_flg3
2970
2971 /* Recursion protection when determining above flag. */
2972 #define call_check_in_progress sec_flg4
2973 #define call_check_done sec_flg5
2974
2975 /* Get the ppc64 ELF linker hash table from a link_info structure. */
2976
2977 #define ppc_hash_table(p) \
2978 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2979 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
2980
2981 #define ppc_stub_hash_lookup(table, string, create, copy) \
2982 ((struct ppc_stub_hash_entry *) \
2983 bfd_hash_lookup ((table), (string), (create), (copy)))
2984
2985 #define ppc_branch_hash_lookup(table, string, create, copy) \
2986 ((struct ppc_branch_hash_entry *) \
2987 bfd_hash_lookup ((table), (string), (create), (copy)))
2988
2989 /* Create an entry in the stub hash table. */
2990
2991 static struct bfd_hash_entry *
2992 stub_hash_newfunc (struct bfd_hash_entry *entry,
2993 struct bfd_hash_table *table,
2994 const char *string)
2995 {
2996 /* Allocate the structure if it has not already been allocated by a
2997 subclass. */
2998 if (entry == NULL)
2999 {
3000 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3001 if (entry == NULL)
3002 return entry;
3003 }
3004
3005 /* Call the allocation method of the superclass. */
3006 entry = bfd_hash_newfunc (entry, table, string);
3007 if (entry != NULL)
3008 {
3009 struct ppc_stub_hash_entry *eh;
3010
3011 /* Initialize the local fields. */
3012 eh = (struct ppc_stub_hash_entry *) entry;
3013 eh->stub_type = ppc_stub_none;
3014 eh->group = NULL;
3015 eh->stub_offset = 0;
3016 eh->target_value = 0;
3017 eh->target_section = NULL;
3018 eh->h = NULL;
3019 eh->plt_ent = NULL;
3020 eh->other = 0;
3021 }
3022
3023 return entry;
3024 }
3025
3026 /* Create an entry in the branch hash table. */
3027
3028 static struct bfd_hash_entry *
3029 branch_hash_newfunc (struct bfd_hash_entry *entry,
3030 struct bfd_hash_table *table,
3031 const char *string)
3032 {
3033 /* Allocate the structure if it has not already been allocated by a
3034 subclass. */
3035 if (entry == NULL)
3036 {
3037 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3038 if (entry == NULL)
3039 return entry;
3040 }
3041
3042 /* Call the allocation method of the superclass. */
3043 entry = bfd_hash_newfunc (entry, table, string);
3044 if (entry != NULL)
3045 {
3046 struct ppc_branch_hash_entry *eh;
3047
3048 /* Initialize the local fields. */
3049 eh = (struct ppc_branch_hash_entry *) entry;
3050 eh->offset = 0;
3051 eh->iter = 0;
3052 }
3053
3054 return entry;
3055 }
3056
3057 /* Create an entry in a ppc64 ELF linker hash table. */
3058
3059 static struct bfd_hash_entry *
3060 link_hash_newfunc (struct bfd_hash_entry *entry,
3061 struct bfd_hash_table *table,
3062 const char *string)
3063 {
3064 /* Allocate the structure if it has not already been allocated by a
3065 subclass. */
3066 if (entry == NULL)
3067 {
3068 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3069 if (entry == NULL)
3070 return entry;
3071 }
3072
3073 /* Call the allocation method of the superclass. */
3074 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3075 if (entry != NULL)
3076 {
3077 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3078
3079 memset (&eh->u.stub_cache, 0,
3080 (sizeof (struct ppc_link_hash_entry)
3081 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3082
3083 /* When making function calls, old ABI code references function entry
3084 points (dot symbols), while new ABI code references the function
3085 descriptor symbol. We need to make any combination of reference and
3086 definition work together, without breaking archive linking.
3087
3088 For a defined function "foo" and an undefined call to "bar":
3089 An old object defines "foo" and ".foo", references ".bar" (possibly
3090 "bar" too).
3091 A new object defines "foo" and references "bar".
3092
3093 A new object thus has no problem with its undefined symbols being
3094 satisfied by definitions in an old object. On the other hand, the
3095 old object won't have ".bar" satisfied by a new object.
3096
3097 Keep a list of newly added dot-symbols. */
3098
3099 if (string[0] == '.')
3100 {
3101 struct ppc_link_hash_table *htab;
3102
3103 htab = (struct ppc_link_hash_table *) table;
3104 eh->u.next_dot_sym = htab->dot_syms;
3105 htab->dot_syms = eh;
3106 }
3107 }
3108
3109 return entry;
3110 }
3111
3112 struct tocsave_entry
3113 {
3114 asection *sec;
3115 bfd_vma offset;
3116 };
3117
3118 static hashval_t
3119 tocsave_htab_hash (const void *p)
3120 {
3121 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3122 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3123 }
3124
3125 static int
3126 tocsave_htab_eq (const void *p1, const void *p2)
3127 {
3128 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3129 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3130 return e1->sec == e2->sec && e1->offset == e2->offset;
3131 }
3132
3133 /* Destroy a ppc64 ELF linker hash table. */
3134
3135 static void
3136 ppc64_elf_link_hash_table_free (bfd *obfd)
3137 {
3138 struct ppc_link_hash_table *htab;
3139
3140 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3141 if (htab->tocsave_htab)
3142 htab_delete (htab->tocsave_htab);
3143 bfd_hash_table_free (&htab->branch_hash_table);
3144 bfd_hash_table_free (&htab->stub_hash_table);
3145 _bfd_elf_link_hash_table_free (obfd);
3146 }
3147
3148 /* Create a ppc64 ELF linker hash table. */
3149
3150 static struct bfd_link_hash_table *
3151 ppc64_elf_link_hash_table_create (bfd *abfd)
3152 {
3153 struct ppc_link_hash_table *htab;
3154 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3155
3156 htab = bfd_zmalloc (amt);
3157 if (htab == NULL)
3158 return NULL;
3159
3160 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3161 sizeof (struct ppc_link_hash_entry),
3162 PPC64_ELF_DATA))
3163 {
3164 free (htab);
3165 return NULL;
3166 }
3167
3168 /* Init the stub hash table too. */
3169 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3170 sizeof (struct ppc_stub_hash_entry)))
3171 {
3172 _bfd_elf_link_hash_table_free (abfd);
3173 return NULL;
3174 }
3175
3176 /* And the branch hash table. */
3177 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3178 sizeof (struct ppc_branch_hash_entry)))
3179 {
3180 bfd_hash_table_free (&htab->stub_hash_table);
3181 _bfd_elf_link_hash_table_free (abfd);
3182 return NULL;
3183 }
3184
3185 htab->tocsave_htab = htab_try_create (1024,
3186 tocsave_htab_hash,
3187 tocsave_htab_eq,
3188 NULL);
3189 if (htab->tocsave_htab == NULL)
3190 {
3191 ppc64_elf_link_hash_table_free (abfd);
3192 return NULL;
3193 }
3194 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3195
3196 /* Initializing two fields of the union is just cosmetic. We really
3197 only care about glist, but when compiled on a 32-bit host the
3198 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3199 debugger inspection of these fields look nicer. */
3200 htab->elf.init_got_refcount.refcount = 0;
3201 htab->elf.init_got_refcount.glist = NULL;
3202 htab->elf.init_plt_refcount.refcount = 0;
3203 htab->elf.init_plt_refcount.glist = NULL;
3204 htab->elf.init_got_offset.offset = 0;
3205 htab->elf.init_got_offset.glist = NULL;
3206 htab->elf.init_plt_offset.offset = 0;
3207 htab->elf.init_plt_offset.glist = NULL;
3208
3209 return &htab->elf.root;
3210 }
3211
3212 /* Create sections for linker generated code. */
3213
3214 static bfd_boolean
3215 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3216 {
3217 struct ppc_link_hash_table *htab;
3218 flagword flags;
3219
3220 htab = ppc_hash_table (info);
3221
3222 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3223 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3224 if (htab->params->save_restore_funcs)
3225 {
3226 /* Create .sfpr for code to save and restore fp regs. */
3227 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3228 flags);
3229 if (htab->sfpr == NULL
3230 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3231 return FALSE;
3232 }
3233
3234 if (bfd_link_relocatable (info))
3235 return TRUE;
3236
3237 /* Create .glink for lazy dynamic linking support. */
3238 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3239 flags);
3240 if (htab->glink == NULL
3241 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
3242 return FALSE;
3243
3244 /* The part of .glink used by global entry stubs, separate so that
3245 it can be aligned appropriately without affecting htab->glink. */
3246 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3247 flags);
3248 if (htab->global_entry == NULL
3249 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
3250 return FALSE;
3251
3252 if (!info->no_ld_generated_unwind_info)
3253 {
3254 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3255 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3256 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3257 ".eh_frame",
3258 flags);
3259 if (htab->glink_eh_frame == NULL
3260 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3261 return FALSE;
3262 }
3263
3264 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3265 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3266 if (htab->elf.iplt == NULL
3267 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
3268 return FALSE;
3269
3270 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3271 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3272 htab->elf.irelplt
3273 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3274 if (htab->elf.irelplt == NULL
3275 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
3276 return FALSE;
3277
3278 /* Create branch lookup table for plt_branch stubs. */
3279 flags = (SEC_ALLOC | SEC_LOAD
3280 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3281 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3282 flags);
3283 if (htab->brlt == NULL
3284 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
3285 return FALSE;
3286
3287 /* Local plt entries, put in .branch_lt but a separate section for
3288 convenience. */
3289 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3290 flags);
3291 if (htab->pltlocal == NULL
3292 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
3293 return FALSE;
3294
3295 if (!bfd_link_pic (info))
3296 return TRUE;
3297
3298 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3299 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3300 htab->relbrlt
3301 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3302 if (htab->relbrlt == NULL
3303 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3304 return FALSE;
3305
3306 htab->relpltlocal
3307 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3308 if (htab->relpltlocal == NULL
3309 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
3310 return FALSE;
3311
3312 return TRUE;
3313 }
3314
3315 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3316
3317 bfd_boolean
3318 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3319 struct ppc64_elf_params *params)
3320 {
3321 struct ppc_link_hash_table *htab;
3322
3323 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3324
3325 /* Always hook our dynamic sections into the first bfd, which is the
3326 linker created stub bfd. This ensures that the GOT header is at
3327 the start of the output TOC section. */
3328 htab = ppc_hash_table (info);
3329 htab->elf.dynobj = params->stub_bfd;
3330 htab->params = params;
3331
3332 return create_linkage_sections (htab->elf.dynobj, info);
3333 }
3334
3335 /* Build a name for an entry in the stub hash table. */
3336
3337 static char *
3338 ppc_stub_name (const asection *input_section,
3339 const asection *sym_sec,
3340 const struct ppc_link_hash_entry *h,
3341 const Elf_Internal_Rela *rel)
3342 {
3343 char *stub_name;
3344 ssize_t len;
3345
3346 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3347 offsets from a sym as a branch target? In fact, we could
3348 probably assume the addend is always zero. */
3349 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3350
3351 if (h)
3352 {
3353 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3354 stub_name = bfd_malloc (len);
3355 if (stub_name == NULL)
3356 return stub_name;
3357
3358 len = sprintf (stub_name, "%08x.%s+%x",
3359 input_section->id & 0xffffffff,
3360 h->elf.root.root.string,
3361 (int) rel->r_addend & 0xffffffff);
3362 }
3363 else
3364 {
3365 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3366 stub_name = bfd_malloc (len);
3367 if (stub_name == NULL)
3368 return stub_name;
3369
3370 len = sprintf (stub_name, "%08x.%x:%x+%x",
3371 input_section->id & 0xffffffff,
3372 sym_sec->id & 0xffffffff,
3373 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3374 (int) rel->r_addend & 0xffffffff);
3375 }
3376 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3377 stub_name[len - 2] = 0;
3378 return stub_name;
3379 }
3380
3381 /* Look up an entry in the stub hash. Stub entries are cached because
3382 creating the stub name takes a bit of time. */
3383
3384 static struct ppc_stub_hash_entry *
3385 ppc_get_stub_entry (const asection *input_section,
3386 const asection *sym_sec,
3387 struct ppc_link_hash_entry *h,
3388 const Elf_Internal_Rela *rel,
3389 struct ppc_link_hash_table *htab)
3390 {
3391 struct ppc_stub_hash_entry *stub_entry;
3392 struct map_stub *group;
3393
3394 /* If this input section is part of a group of sections sharing one
3395 stub section, then use the id of the first section in the group.
3396 Stub names need to include a section id, as there may well be
3397 more than one stub used to reach say, printf, and we need to
3398 distinguish between them. */
3399 group = htab->sec_info[input_section->id].u.group;
3400 if (group == NULL)
3401 return NULL;
3402
3403 if (h != NULL && h->u.stub_cache != NULL
3404 && h->u.stub_cache->h == h
3405 && h->u.stub_cache->group == group)
3406 {
3407 stub_entry = h->u.stub_cache;
3408 }
3409 else
3410 {
3411 char *stub_name;
3412
3413 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3414 if (stub_name == NULL)
3415 return NULL;
3416
3417 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3418 stub_name, FALSE, FALSE);
3419 if (h != NULL)
3420 h->u.stub_cache = stub_entry;
3421
3422 free (stub_name);
3423 }
3424
3425 return stub_entry;
3426 }
3427
3428 /* Add a new stub entry to the stub hash. Not all fields of the new
3429 stub entry are initialised. */
3430
3431 static struct ppc_stub_hash_entry *
3432 ppc_add_stub (const char *stub_name,
3433 asection *section,
3434 struct bfd_link_info *info)
3435 {
3436 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3437 struct map_stub *group;
3438 asection *link_sec;
3439 asection *stub_sec;
3440 struct ppc_stub_hash_entry *stub_entry;
3441
3442 group = htab->sec_info[section->id].u.group;
3443 link_sec = group->link_sec;
3444 stub_sec = group->stub_sec;
3445 if (stub_sec == NULL)
3446 {
3447 size_t namelen;
3448 bfd_size_type len;
3449 char *s_name;
3450
3451 namelen = strlen (link_sec->name);
3452 len = namelen + sizeof (STUB_SUFFIX);
3453 s_name = bfd_alloc (htab->params->stub_bfd, len);
3454 if (s_name == NULL)
3455 return NULL;
3456
3457 memcpy (s_name, link_sec->name, namelen);
3458 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3459 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3460 if (stub_sec == NULL)
3461 return NULL;
3462 group->stub_sec = stub_sec;
3463 }
3464
3465 /* Enter this entry into the linker stub hash table. */
3466 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3467 TRUE, FALSE);
3468 if (stub_entry == NULL)
3469 {
3470 /* xgettext:c-format */
3471 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3472 section->owner, stub_name);
3473 return NULL;
3474 }
3475
3476 stub_entry->group = group;
3477 stub_entry->stub_offset = 0;
3478 return stub_entry;
3479 }
3480
3481 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3482 not already done. */
3483
3484 static bfd_boolean
3485 create_got_section (bfd *abfd, struct bfd_link_info *info)
3486 {
3487 asection *got, *relgot;
3488 flagword flags;
3489 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3490
3491 if (!is_ppc64_elf (abfd))
3492 return FALSE;
3493 if (htab == NULL)
3494 return FALSE;
3495
3496 if (!htab->elf.sgot
3497 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3498 return FALSE;
3499
3500 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3501 | SEC_LINKER_CREATED);
3502
3503 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3504 if (!got
3505 || !bfd_set_section_alignment (abfd, got, 3))
3506 return FALSE;
3507
3508 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3509 flags | SEC_READONLY);
3510 if (!relgot
3511 || !bfd_set_section_alignment (abfd, relgot, 3))
3512 return FALSE;
3513
3514 ppc64_elf_tdata (abfd)->got = got;
3515 ppc64_elf_tdata (abfd)->relgot = relgot;
3516 return TRUE;
3517 }
3518
3519 /* Follow indirect and warning symbol links. */
3520
3521 static inline struct bfd_link_hash_entry *
3522 follow_link (struct bfd_link_hash_entry *h)
3523 {
3524 while (h->type == bfd_link_hash_indirect
3525 || h->type == bfd_link_hash_warning)
3526 h = h->u.i.link;
3527 return h;
3528 }
3529
3530 static inline struct elf_link_hash_entry *
3531 elf_follow_link (struct elf_link_hash_entry *h)
3532 {
3533 return (struct elf_link_hash_entry *) follow_link (&h->root);
3534 }
3535
3536 static inline struct ppc_link_hash_entry *
3537 ppc_follow_link (struct ppc_link_hash_entry *h)
3538 {
3539 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3540 }
3541
3542 /* Merge PLT info on FROM with that on TO. */
3543
3544 static void
3545 move_plt_plist (struct ppc_link_hash_entry *from,
3546 struct ppc_link_hash_entry *to)
3547 {
3548 if (from->elf.plt.plist != NULL)
3549 {
3550 if (to->elf.plt.plist != NULL)
3551 {
3552 struct plt_entry **entp;
3553 struct plt_entry *ent;
3554
3555 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3556 {
3557 struct plt_entry *dent;
3558
3559 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3560 if (dent->addend == ent->addend)
3561 {
3562 dent->plt.refcount += ent->plt.refcount;
3563 *entp = ent->next;
3564 break;
3565 }
3566 if (dent == NULL)
3567 entp = &ent->next;
3568 }
3569 *entp = to->elf.plt.plist;
3570 }
3571
3572 to->elf.plt.plist = from->elf.plt.plist;
3573 from->elf.plt.plist = NULL;
3574 }
3575 }
3576
3577 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3578
3579 static void
3580 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3581 struct elf_link_hash_entry *dir,
3582 struct elf_link_hash_entry *ind)
3583 {
3584 struct ppc_link_hash_entry *edir, *eind;
3585
3586 edir = (struct ppc_link_hash_entry *) dir;
3587 eind = (struct ppc_link_hash_entry *) ind;
3588
3589 edir->is_func |= eind->is_func;
3590 edir->is_func_descriptor |= eind->is_func_descriptor;
3591 edir->tls_mask |= eind->tls_mask;
3592 if (eind->oh != NULL)
3593 edir->oh = ppc_follow_link (eind->oh);
3594
3595 if (edir->elf.versioned != versioned_hidden)
3596 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3597 edir->elf.ref_regular |= eind->elf.ref_regular;
3598 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3599 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3600 edir->elf.needs_plt |= eind->elf.needs_plt;
3601 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3602
3603 /* If we were called to copy over info for a weak sym, don't copy
3604 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3605 in order to simplify readonly_dynrelocs and save a field in the
3606 symbol hash entry, but that means dyn_relocs can't be used in any
3607 tests about a specific symbol, or affect other symbol flags which
3608 are then tested. */
3609 if (eind->elf.root.type != bfd_link_hash_indirect)
3610 return;
3611
3612 /* Copy over any dynamic relocs we may have on the indirect sym. */
3613 if (eind->dyn_relocs != NULL)
3614 {
3615 if (edir->dyn_relocs != NULL)
3616 {
3617 struct elf_dyn_relocs **pp;
3618 struct elf_dyn_relocs *p;
3619
3620 /* Add reloc counts against the indirect sym to the direct sym
3621 list. Merge any entries against the same section. */
3622 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3623 {
3624 struct elf_dyn_relocs *q;
3625
3626 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3627 if (q->sec == p->sec)
3628 {
3629 q->pc_count += p->pc_count;
3630 q->count += p->count;
3631 *pp = p->next;
3632 break;
3633 }
3634 if (q == NULL)
3635 pp = &p->next;
3636 }
3637 *pp = edir->dyn_relocs;
3638 }
3639
3640 edir->dyn_relocs = eind->dyn_relocs;
3641 eind->dyn_relocs = NULL;
3642 }
3643
3644 /* Copy over got entries that we may have already seen to the
3645 symbol which just became indirect. */
3646 if (eind->elf.got.glist != NULL)
3647 {
3648 if (edir->elf.got.glist != NULL)
3649 {
3650 struct got_entry **entp;
3651 struct got_entry *ent;
3652
3653 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3654 {
3655 struct got_entry *dent;
3656
3657 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3658 if (dent->addend == ent->addend
3659 && dent->owner == ent->owner
3660 && dent->tls_type == ent->tls_type)
3661 {
3662 dent->got.refcount += ent->got.refcount;
3663 *entp = ent->next;
3664 break;
3665 }
3666 if (dent == NULL)
3667 entp = &ent->next;
3668 }
3669 *entp = edir->elf.got.glist;
3670 }
3671
3672 edir->elf.got.glist = eind->elf.got.glist;
3673 eind->elf.got.glist = NULL;
3674 }
3675
3676 /* And plt entries. */
3677 move_plt_plist (eind, edir);
3678
3679 if (eind->elf.dynindx != -1)
3680 {
3681 if (edir->elf.dynindx != -1)
3682 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3683 edir->elf.dynstr_index);
3684 edir->elf.dynindx = eind->elf.dynindx;
3685 edir->elf.dynstr_index = eind->elf.dynstr_index;
3686 eind->elf.dynindx = -1;
3687 eind->elf.dynstr_index = 0;
3688 }
3689 }
3690
3691 /* Find the function descriptor hash entry from the given function code
3692 hash entry FH. Link the entries via their OH fields. */
3693
3694 static struct ppc_link_hash_entry *
3695 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3696 {
3697 struct ppc_link_hash_entry *fdh = fh->oh;
3698
3699 if (fdh == NULL)
3700 {
3701 const char *fd_name = fh->elf.root.root.string + 1;
3702
3703 fdh = (struct ppc_link_hash_entry *)
3704 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3705 if (fdh == NULL)
3706 return fdh;
3707
3708 fdh->is_func_descriptor = 1;
3709 fdh->oh = fh;
3710 fh->is_func = 1;
3711 fh->oh = fdh;
3712 }
3713
3714 fdh = ppc_follow_link (fdh);
3715 fdh->is_func_descriptor = 1;
3716 fdh->oh = fh;
3717 return fdh;
3718 }
3719
3720 /* Make a fake function descriptor sym for the undefined code sym FH. */
3721
3722 static struct ppc_link_hash_entry *
3723 make_fdh (struct bfd_link_info *info,
3724 struct ppc_link_hash_entry *fh)
3725 {
3726 bfd *abfd = fh->elf.root.u.undef.abfd;
3727 struct bfd_link_hash_entry *bh = NULL;
3728 struct ppc_link_hash_entry *fdh;
3729 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3730 ? BSF_WEAK
3731 : BSF_GLOBAL);
3732
3733 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3734 fh->elf.root.root.string + 1,
3735 flags, bfd_und_section_ptr, 0,
3736 NULL, FALSE, FALSE, &bh))
3737 return NULL;
3738
3739 fdh = (struct ppc_link_hash_entry *) bh;
3740 fdh->elf.non_elf = 0;
3741 fdh->fake = 1;
3742 fdh->is_func_descriptor = 1;
3743 fdh->oh = fh;
3744 fh->is_func = 1;
3745 fh->oh = fdh;
3746 return fdh;
3747 }
3748
3749 /* Fix function descriptor symbols defined in .opd sections to be
3750 function type. */
3751
3752 static bfd_boolean
3753 ppc64_elf_add_symbol_hook (bfd *ibfd,
3754 struct bfd_link_info *info,
3755 Elf_Internal_Sym *isym,
3756 const char **name,
3757 flagword *flags ATTRIBUTE_UNUSED,
3758 asection **sec,
3759 bfd_vma *value)
3760 {
3761 if (*sec != NULL
3762 && strcmp ((*sec)->name, ".opd") == 0)
3763 {
3764 asection *code_sec;
3765
3766 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3767 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
3768 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
3769
3770 /* If the symbol is a function defined in .opd, and the function
3771 code is in a discarded group, let it appear to be undefined. */
3772 if (!bfd_link_relocatable (info)
3773 && (*sec)->reloc_count != 0
3774 && opd_entry_value (*sec, *value, &code_sec, NULL,
3775 FALSE) != (bfd_vma) -1
3776 && discarded_section (code_sec))
3777 {
3778 *sec = bfd_und_section_ptr;
3779 isym->st_shndx = SHN_UNDEF;
3780 }
3781 }
3782 else if (*sec != NULL
3783 && strcmp ((*sec)->name, ".toc") == 0
3784 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
3785 {
3786 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3787 if (htab != NULL)
3788 htab->params->object_in_toc = 1;
3789 }
3790
3791 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3792 {
3793 if (abiversion (ibfd) == 0)
3794 set_abiversion (ibfd, 2);
3795 else if (abiversion (ibfd) == 1)
3796 {
3797 _bfd_error_handler (_("symbol '%s' has invalid st_other"
3798 " for ABI version 1"), *name);
3799 bfd_set_error (bfd_error_bad_value);
3800 return FALSE;
3801 }
3802 }
3803
3804 return TRUE;
3805 }
3806
3807 /* Merge non-visibility st_other attributes: local entry point. */
3808
3809 static void
3810 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
3811 const Elf_Internal_Sym *isym,
3812 bfd_boolean definition,
3813 bfd_boolean dynamic)
3814 {
3815 if (definition && (!dynamic || !h->def_regular))
3816 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
3817 | ELF_ST_VISIBILITY (h->other));
3818 }
3819
3820 /* Hook called on merging a symbol. We use this to clear "fake" since
3821 we now have a real symbol. */
3822
3823 static bfd_boolean
3824 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
3825 const Elf_Internal_Sym *isym,
3826 asection **psec ATTRIBUTE_UNUSED,
3827 bfd_boolean newdef ATTRIBUTE_UNUSED,
3828 bfd_boolean olddef ATTRIBUTE_UNUSED,
3829 bfd *oldbfd ATTRIBUTE_UNUSED,
3830 const asection *oldsec ATTRIBUTE_UNUSED)
3831 {
3832 ((struct ppc_link_hash_entry *) h)->fake = 0;
3833 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3834 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
3835 return TRUE;
3836 }
3837
3838 /* This function makes an old ABI object reference to ".bar" cause the
3839 inclusion of a new ABI object archive that defines "bar".
3840 NAME is a symbol defined in an archive. Return a symbol in the hash
3841 table that might be satisfied by the archive symbols. */
3842
3843 static struct elf_link_hash_entry *
3844 ppc64_elf_archive_symbol_lookup (bfd *abfd,
3845 struct bfd_link_info *info,
3846 const char *name)
3847 {
3848 struct elf_link_hash_entry *h;
3849 char *dot_name;
3850 size_t len;
3851
3852 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
3853 if (h != NULL
3854 /* Don't return this sym if it is a fake function descriptor
3855 created by add_symbol_adjust. */
3856 && !((struct ppc_link_hash_entry *) h)->fake)
3857 return h;
3858
3859 if (name[0] == '.')
3860 return h;
3861
3862 len = strlen (name);
3863 dot_name = bfd_alloc (abfd, len + 2);
3864 if (dot_name == NULL)
3865 return (struct elf_link_hash_entry *) -1;
3866 dot_name[0] = '.';
3867 memcpy (dot_name + 1, name, len + 1);
3868 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
3869 bfd_release (abfd, dot_name);
3870 return h;
3871 }
3872
3873 /* This function satisfies all old ABI object references to ".bar" if a
3874 new ABI object defines "bar". Well, at least, undefined dot symbols
3875 are made weak. This stops later archive searches from including an
3876 object if we already have a function descriptor definition. It also
3877 prevents the linker complaining about undefined symbols.
3878 We also check and correct mismatched symbol visibility here. The
3879 most restrictive visibility of the function descriptor and the
3880 function entry symbol is used. */
3881
3882 static bfd_boolean
3883 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
3884 {
3885 struct ppc_link_hash_table *htab;
3886 struct ppc_link_hash_entry *fdh;
3887
3888 if (eh->elf.root.type == bfd_link_hash_warning)
3889 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
3890
3891 if (eh->elf.root.type == bfd_link_hash_indirect)
3892 return TRUE;
3893
3894 if (eh->elf.root.root.string[0] != '.')
3895 abort ();
3896
3897 htab = ppc_hash_table (info);
3898 if (htab == NULL)
3899 return FALSE;
3900
3901 fdh = lookup_fdh (eh, htab);
3902 if (fdh == NULL
3903 && !bfd_link_relocatable (info)
3904 && (eh->elf.root.type == bfd_link_hash_undefined
3905 || eh->elf.root.type == bfd_link_hash_undefweak)
3906 && eh->elf.ref_regular)
3907 {
3908 /* Make an undefined function descriptor sym, in order to
3909 pull in an --as-needed shared lib. Archives are handled
3910 elsewhere. */
3911 fdh = make_fdh (info, eh);
3912 if (fdh == NULL)
3913 return FALSE;
3914 }
3915
3916 if (fdh != NULL)
3917 {
3918 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
3919 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
3920
3921 /* Make both descriptor and entry symbol have the most
3922 constraining visibility of either symbol. */
3923 if (entry_vis < descr_vis)
3924 fdh->elf.other += entry_vis - descr_vis;
3925 else if (entry_vis > descr_vis)
3926 eh->elf.other += descr_vis - entry_vis;
3927
3928 /* Propagate reference flags from entry symbol to function
3929 descriptor symbol. */
3930 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
3931 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
3932 fdh->elf.ref_regular |= eh->elf.ref_regular;
3933 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
3934
3935 if (!fdh->elf.forced_local
3936 && fdh->elf.dynindx == -1
3937 && fdh->elf.versioned != versioned_hidden
3938 && (bfd_link_dll (info)
3939 || fdh->elf.def_dynamic
3940 || fdh->elf.ref_dynamic)
3941 && (eh->elf.ref_regular
3942 || eh->elf.def_regular))
3943 {
3944 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
3945 return FALSE;
3946 }
3947 }
3948
3949 return TRUE;
3950 }
3951
3952 /* Set up opd section info and abiversion for IBFD, and process list
3953 of dot-symbols we made in link_hash_newfunc. */
3954
3955 static bfd_boolean
3956 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
3957 {
3958 struct ppc_link_hash_table *htab;
3959 struct ppc_link_hash_entry **p, *eh;
3960 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
3961
3962 if (opd != NULL && opd->size != 0)
3963 {
3964 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
3965 ppc64_elf_section_data (opd)->sec_type = sec_opd;
3966
3967 if (abiversion (ibfd) == 0)
3968 set_abiversion (ibfd, 1);
3969 else if (abiversion (ibfd) >= 2)
3970 {
3971 /* xgettext:c-format */
3972 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
3973 ibfd, abiversion (ibfd));
3974 bfd_set_error (bfd_error_bad_value);
3975 return FALSE;
3976 }
3977 }
3978
3979 if (is_ppc64_elf (info->output_bfd))
3980 {
3981 /* For input files without an explicit abiversion in e_flags
3982 we should have flagged any with symbol st_other bits set
3983 as ELFv1 and above flagged those with .opd as ELFv2.
3984 Set the output abiversion if not yet set, and for any input
3985 still ambiguous, take its abiversion from the output.
3986 Differences in ABI are reported later. */
3987 if (abiversion (info->output_bfd) == 0)
3988 set_abiversion (info->output_bfd, abiversion (ibfd));
3989 else if (abiversion (ibfd) == 0)
3990 set_abiversion (ibfd, abiversion (info->output_bfd));
3991 }
3992
3993 htab = ppc_hash_table (info);
3994 if (htab == NULL)
3995 return TRUE;
3996
3997 if (opd != NULL && opd->size != 0
3998 && (ibfd->flags & DYNAMIC) == 0
3999 && (opd->flags & SEC_RELOC) != 0
4000 && opd->reloc_count != 0
4001 && !bfd_is_abs_section (opd->output_section)
4002 && info->gc_sections)
4003 {
4004 /* Garbage collection needs some extra help with .opd sections.
4005 We don't want to necessarily keep everything referenced by
4006 relocs in .opd, as that would keep all functions. Instead,
4007 if we reference an .opd symbol (a function descriptor), we
4008 want to keep the function code symbol's section. This is
4009 easy for global symbols, but for local syms we need to keep
4010 information about the associated function section. */
4011 bfd_size_type amt;
4012 asection **opd_sym_map;
4013 Elf_Internal_Shdr *symtab_hdr;
4014 Elf_Internal_Rela *relocs, *rel_end, *rel;
4015
4016 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4017 opd_sym_map = bfd_zalloc (ibfd, amt);
4018 if (opd_sym_map == NULL)
4019 return FALSE;
4020 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4021 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4022 info->keep_memory);
4023 if (relocs == NULL)
4024 return FALSE;
4025 symtab_hdr = &elf_symtab_hdr (ibfd);
4026 rel_end = relocs + opd->reloc_count - 1;
4027 for (rel = relocs; rel < rel_end; rel++)
4028 {
4029 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4030 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4031
4032 if (r_type == R_PPC64_ADDR64
4033 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4034 && r_symndx < symtab_hdr->sh_info)
4035 {
4036 Elf_Internal_Sym *isym;
4037 asection *s;
4038
4039 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4040 if (isym == NULL)
4041 {
4042 if (elf_section_data (opd)->relocs != relocs)
4043 free (relocs);
4044 return FALSE;
4045 }
4046
4047 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4048 if (s != NULL && s != opd)
4049 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4050 }
4051 }
4052 if (elf_section_data (opd)->relocs != relocs)
4053 free (relocs);
4054 }
4055
4056 p = &htab->dot_syms;
4057 while ((eh = *p) != NULL)
4058 {
4059 *p = NULL;
4060 if (&eh->elf == htab->elf.hgot)
4061 ;
4062 else if (htab->elf.hgot == NULL
4063 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4064 htab->elf.hgot = &eh->elf;
4065 else if (abiversion (ibfd) <= 1)
4066 {
4067 htab->need_func_desc_adj = 1;
4068 if (!add_symbol_adjust (eh, info))
4069 return FALSE;
4070 }
4071 p = &eh->u.next_dot_sym;
4072 }
4073 return TRUE;
4074 }
4075
4076 /* Undo hash table changes when an --as-needed input file is determined
4077 not to be needed. */
4078
4079 static bfd_boolean
4080 ppc64_elf_notice_as_needed (bfd *ibfd,
4081 struct bfd_link_info *info,
4082 enum notice_asneeded_action act)
4083 {
4084 if (act == notice_not_needed)
4085 {
4086 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4087
4088 if (htab == NULL)
4089 return FALSE;
4090
4091 htab->dot_syms = NULL;
4092 }
4093 return _bfd_elf_notice_as_needed (ibfd, info, act);
4094 }
4095
4096 /* If --just-symbols against a final linked binary, then assume we need
4097 toc adjusting stubs when calling functions defined there. */
4098
4099 static void
4100 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4101 {
4102 if ((sec->flags & SEC_CODE) != 0
4103 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4104 && is_ppc64_elf (sec->owner))
4105 {
4106 if (abiversion (sec->owner) >= 2
4107 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4108 sec->has_toc_reloc = 1;
4109 }
4110 _bfd_elf_link_just_syms (sec, info);
4111 }
4112
4113 static struct plt_entry **
4114 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4115 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4116 {
4117 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4118 struct plt_entry **local_plt;
4119 unsigned char *local_got_tls_masks;
4120
4121 if (local_got_ents == NULL)
4122 {
4123 bfd_size_type size = symtab_hdr->sh_info;
4124
4125 size *= (sizeof (*local_got_ents)
4126 + sizeof (*local_plt)
4127 + sizeof (*local_got_tls_masks));
4128 local_got_ents = bfd_zalloc (abfd, size);
4129 if (local_got_ents == NULL)
4130 return NULL;
4131 elf_local_got_ents (abfd) = local_got_ents;
4132 }
4133
4134 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4135 {
4136 struct got_entry *ent;
4137
4138 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4139 if (ent->addend == r_addend
4140 && ent->owner == abfd
4141 && ent->tls_type == tls_type)
4142 break;
4143 if (ent == NULL)
4144 {
4145 bfd_size_type amt = sizeof (*ent);
4146 ent = bfd_alloc (abfd, amt);
4147 if (ent == NULL)
4148 return FALSE;
4149 ent->next = local_got_ents[r_symndx];
4150 ent->addend = r_addend;
4151 ent->owner = abfd;
4152 ent->tls_type = tls_type;
4153 ent->is_indirect = FALSE;
4154 ent->got.refcount = 0;
4155 local_got_ents[r_symndx] = ent;
4156 }
4157 ent->got.refcount += 1;
4158 }
4159
4160 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4161 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4162 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4163
4164 return local_plt + r_symndx;
4165 }
4166
4167 static bfd_boolean
4168 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4169 {
4170 struct plt_entry *ent;
4171
4172 for (ent = *plist; ent != NULL; ent = ent->next)
4173 if (ent->addend == addend)
4174 break;
4175 if (ent == NULL)
4176 {
4177 bfd_size_type amt = sizeof (*ent);
4178 ent = bfd_alloc (abfd, amt);
4179 if (ent == NULL)
4180 return FALSE;
4181 ent->next = *plist;
4182 ent->addend = addend;
4183 ent->plt.refcount = 0;
4184 *plist = ent;
4185 }
4186 ent->plt.refcount += 1;
4187 return TRUE;
4188 }
4189
4190 static bfd_boolean
4191 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4192 {
4193 return (r_type == R_PPC64_REL24
4194 || r_type == R_PPC64_REL24_NOTOC
4195 || r_type == R_PPC64_REL14
4196 || r_type == R_PPC64_REL14_BRTAKEN
4197 || r_type == R_PPC64_REL14_BRNTAKEN
4198 || r_type == R_PPC64_ADDR24
4199 || r_type == R_PPC64_ADDR14
4200 || r_type == R_PPC64_ADDR14_BRTAKEN
4201 || r_type == R_PPC64_ADDR14_BRNTAKEN
4202 || r_type == R_PPC64_PLTCALL);
4203 }
4204
4205 /* Relocs on inline plt call sequence insns prior to the call. */
4206
4207 static bfd_boolean
4208 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4209 {
4210 return (r_type == R_PPC64_PLT16_HA
4211 || r_type == R_PPC64_PLT16_HI
4212 || r_type == R_PPC64_PLT16_LO
4213 || r_type == R_PPC64_PLT16_LO_DS
4214 || r_type == R_PPC64_PLTSEQ);
4215 }
4216
4217 /* Look through the relocs for a section during the first phase, and
4218 calculate needed space in the global offset table, procedure
4219 linkage table, and dynamic reloc sections. */
4220
4221 static bfd_boolean
4222 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4223 asection *sec, const Elf_Internal_Rela *relocs)
4224 {
4225 struct ppc_link_hash_table *htab;
4226 Elf_Internal_Shdr *symtab_hdr;
4227 struct elf_link_hash_entry **sym_hashes;
4228 const Elf_Internal_Rela *rel;
4229 const Elf_Internal_Rela *rel_end;
4230 asection *sreloc;
4231 struct elf_link_hash_entry *tga, *dottga;
4232 bfd_boolean is_opd;
4233
4234 if (bfd_link_relocatable (info))
4235 return TRUE;
4236
4237 /* Don't do anything special with non-loaded, non-alloced sections.
4238 In particular, any relocs in such sections should not affect GOT
4239 and PLT reference counting (ie. we don't allow them to create GOT
4240 or PLT entries), there's no possibility or desire to optimize TLS
4241 relocs, and there's not much point in propagating relocs to shared
4242 libs that the dynamic linker won't relocate. */
4243 if ((sec->flags & SEC_ALLOC) == 0)
4244 return TRUE;
4245
4246 BFD_ASSERT (is_ppc64_elf (abfd));
4247
4248 htab = ppc_hash_table (info);
4249 if (htab == NULL)
4250 return FALSE;
4251
4252 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4253 FALSE, FALSE, TRUE);
4254 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4255 FALSE, FALSE, TRUE);
4256 symtab_hdr = &elf_symtab_hdr (abfd);
4257 sym_hashes = elf_sym_hashes (abfd);
4258 sreloc = NULL;
4259 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4260 rel_end = relocs + sec->reloc_count;
4261 for (rel = relocs; rel < rel_end; rel++)
4262 {
4263 unsigned long r_symndx;
4264 struct elf_link_hash_entry *h;
4265 enum elf_ppc64_reloc_type r_type;
4266 int tls_type;
4267 struct _ppc64_elf_section_data *ppc64_sec;
4268 struct plt_entry **ifunc, **plt_list;
4269
4270 r_symndx = ELF64_R_SYM (rel->r_info);
4271 if (r_symndx < symtab_hdr->sh_info)
4272 h = NULL;
4273 else
4274 {
4275 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4276 h = elf_follow_link (h);
4277
4278 if (h == htab->elf.hgot)
4279 sec->has_toc_reloc = 1;
4280 }
4281
4282 tls_type = 0;
4283 ifunc = NULL;
4284 if (h != NULL)
4285 {
4286 if (h->type == STT_GNU_IFUNC)
4287 {
4288 h->needs_plt = 1;
4289 ifunc = &h->plt.plist;
4290 }
4291 }
4292 else
4293 {
4294 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4295 abfd, r_symndx);
4296 if (isym == NULL)
4297 return FALSE;
4298
4299 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4300 {
4301 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4302 rel->r_addend,
4303 NON_GOT | PLT_IFUNC);
4304 if (ifunc == NULL)
4305 return FALSE;
4306 }
4307 }
4308
4309 r_type = ELF64_R_TYPE (rel->r_info);
4310 switch (r_type)
4311 {
4312 case R_PPC64_TLSGD:
4313 case R_PPC64_TLSLD:
4314 /* These special tls relocs tie a call to __tls_get_addr with
4315 its parameter symbol. */
4316 if (h != NULL)
4317 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4318 else
4319 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4320 rel->r_addend,
4321 NON_GOT | TLS_TLS | TLS_MARK))
4322 return FALSE;
4323 sec->has_tls_reloc = 1;
4324 break;
4325
4326 case R_PPC64_GOT_TLSLD16:
4327 case R_PPC64_GOT_TLSLD16_LO:
4328 case R_PPC64_GOT_TLSLD16_HI:
4329 case R_PPC64_GOT_TLSLD16_HA:
4330 tls_type = TLS_TLS | TLS_LD;
4331 goto dogottls;
4332
4333 case R_PPC64_GOT_TLSGD16:
4334 case R_PPC64_GOT_TLSGD16_LO:
4335 case R_PPC64_GOT_TLSGD16_HI:
4336 case R_PPC64_GOT_TLSGD16_HA:
4337 tls_type = TLS_TLS | TLS_GD;
4338 goto dogottls;
4339
4340 case R_PPC64_GOT_TPREL16_DS:
4341 case R_PPC64_GOT_TPREL16_LO_DS:
4342 case R_PPC64_GOT_TPREL16_HI:
4343 case R_PPC64_GOT_TPREL16_HA:
4344 if (bfd_link_dll (info))
4345 info->flags |= DF_STATIC_TLS;
4346 tls_type = TLS_TLS | TLS_TPREL;
4347 goto dogottls;
4348
4349 case R_PPC64_GOT_DTPREL16_DS:
4350 case R_PPC64_GOT_DTPREL16_LO_DS:
4351 case R_PPC64_GOT_DTPREL16_HI:
4352 case R_PPC64_GOT_DTPREL16_HA:
4353 tls_type = TLS_TLS | TLS_DTPREL;
4354 dogottls:
4355 sec->has_tls_reloc = 1;
4356 /* Fall through */
4357
4358 case R_PPC64_GOT16:
4359 case R_PPC64_GOT16_DS:
4360 case R_PPC64_GOT16_HA:
4361 case R_PPC64_GOT16_HI:
4362 case R_PPC64_GOT16_LO:
4363 case R_PPC64_GOT16_LO_DS:
4364 /* This symbol requires a global offset table entry. */
4365 sec->has_toc_reloc = 1;
4366 if (r_type == R_PPC64_GOT_TLSLD16
4367 || r_type == R_PPC64_GOT_TLSGD16
4368 || r_type == R_PPC64_GOT_TPREL16_DS
4369 || r_type == R_PPC64_GOT_DTPREL16_DS
4370 || r_type == R_PPC64_GOT16
4371 || r_type == R_PPC64_GOT16_DS)
4372 {
4373 htab->do_multi_toc = 1;
4374 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4375 }
4376
4377 if (ppc64_elf_tdata (abfd)->got == NULL
4378 && !create_got_section (abfd, info))
4379 return FALSE;
4380
4381 if (h != NULL)
4382 {
4383 struct ppc_link_hash_entry *eh;
4384 struct got_entry *ent;
4385
4386 eh = (struct ppc_link_hash_entry *) h;
4387 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4388 if (ent->addend == rel->r_addend
4389 && ent->owner == abfd
4390 && ent->tls_type == tls_type)
4391 break;
4392 if (ent == NULL)
4393 {
4394 bfd_size_type amt = sizeof (*ent);
4395 ent = bfd_alloc (abfd, amt);
4396 if (ent == NULL)
4397 return FALSE;
4398 ent->next = eh->elf.got.glist;
4399 ent->addend = rel->r_addend;
4400 ent->owner = abfd;
4401 ent->tls_type = tls_type;
4402 ent->is_indirect = FALSE;
4403 ent->got.refcount = 0;
4404 eh->elf.got.glist = ent;
4405 }
4406 ent->got.refcount += 1;
4407 eh->tls_mask |= tls_type;
4408 }
4409 else
4410 /* This is a global offset table entry for a local symbol. */
4411 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4412 rel->r_addend, tls_type))
4413 return FALSE;
4414
4415 /* We may also need a plt entry if the symbol turns out to be
4416 an ifunc. */
4417 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
4418 {
4419 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4420 return FALSE;
4421 }
4422 break;
4423
4424 case R_PPC64_PLT16_HA:
4425 case R_PPC64_PLT16_HI:
4426 case R_PPC64_PLT16_LO:
4427 case R_PPC64_PLT16_LO_DS:
4428 case R_PPC64_PLT32:
4429 case R_PPC64_PLT64:
4430 /* This symbol requires a procedure linkage table entry. */
4431 plt_list = ifunc;
4432 if (h != NULL)
4433 {
4434 h->needs_plt = 1;
4435 if (h->root.root.string[0] == '.'
4436 && h->root.root.string[1] != '\0')
4437 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4438 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
4439 plt_list = &h->plt.plist;
4440 }
4441 if (plt_list == NULL)
4442 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4443 rel->r_addend,
4444 NON_GOT | PLT_KEEP);
4445 if (!update_plt_info (abfd, plt_list, rel->r_addend))
4446 return FALSE;
4447 break;
4448
4449 /* The following relocations don't need to propagate the
4450 relocation if linking a shared object since they are
4451 section relative. */
4452 case R_PPC64_SECTOFF:
4453 case R_PPC64_SECTOFF_LO:
4454 case R_PPC64_SECTOFF_HI:
4455 case R_PPC64_SECTOFF_HA:
4456 case R_PPC64_SECTOFF_DS:
4457 case R_PPC64_SECTOFF_LO_DS:
4458 case R_PPC64_DTPREL16:
4459 case R_PPC64_DTPREL16_LO:
4460 case R_PPC64_DTPREL16_HI:
4461 case R_PPC64_DTPREL16_HA:
4462 case R_PPC64_DTPREL16_DS:
4463 case R_PPC64_DTPREL16_LO_DS:
4464 case R_PPC64_DTPREL16_HIGH:
4465 case R_PPC64_DTPREL16_HIGHA:
4466 case R_PPC64_DTPREL16_HIGHER:
4467 case R_PPC64_DTPREL16_HIGHERA:
4468 case R_PPC64_DTPREL16_HIGHEST:
4469 case R_PPC64_DTPREL16_HIGHESTA:
4470 break;
4471
4472 /* Nor do these. */
4473 case R_PPC64_REL16:
4474 case R_PPC64_REL16_LO:
4475 case R_PPC64_REL16_HI:
4476 case R_PPC64_REL16_HA:
4477 case R_PPC64_REL16DX_HA:
4478 break;
4479
4480 /* Not supported as a dynamic relocation. */
4481 case R_PPC64_ADDR64_LOCAL:
4482 if (bfd_link_pic (info))
4483 {
4484 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4485 ppc_howto_init ();
4486 /* xgettext:c-format */
4487 info->callbacks->einfo (_("%H: %s reloc unsupported "
4488 "in shared libraries and PIEs\n"),
4489 abfd, sec, rel->r_offset,
4490 ppc64_elf_howto_table[r_type]->name);
4491 bfd_set_error (bfd_error_bad_value);
4492 return FALSE;
4493 }
4494 break;
4495
4496 case R_PPC64_TOC16:
4497 case R_PPC64_TOC16_DS:
4498 htab->do_multi_toc = 1;
4499 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4500 /* Fall through. */
4501 case R_PPC64_TOC16_LO:
4502 case R_PPC64_TOC16_HI:
4503 case R_PPC64_TOC16_HA:
4504 case R_PPC64_TOC16_LO_DS:
4505 sec->has_toc_reloc = 1;
4506 break;
4507
4508 /* Marker reloc. */
4509 case R_PPC64_ENTRY:
4510 break;
4511
4512 /* This relocation describes the C++ object vtable hierarchy.
4513 Reconstruct it for later use during GC. */
4514 case R_PPC64_GNU_VTINHERIT:
4515 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4516 return FALSE;
4517 break;
4518
4519 /* This relocation describes which C++ vtable entries are actually
4520 used. Record for later use during GC. */
4521 case R_PPC64_GNU_VTENTRY:
4522 BFD_ASSERT (h != NULL);
4523 if (h != NULL
4524 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4525 return FALSE;
4526 break;
4527
4528 case R_PPC64_REL14:
4529 case R_PPC64_REL14_BRTAKEN:
4530 case R_PPC64_REL14_BRNTAKEN:
4531 {
4532 asection *dest = NULL;
4533
4534 /* Heuristic: If jumping outside our section, chances are
4535 we are going to need a stub. */
4536 if (h != NULL)
4537 {
4538 /* If the sym is weak it may be overridden later, so
4539 don't assume we know where a weak sym lives. */
4540 if (h->root.type == bfd_link_hash_defined)
4541 dest = h->root.u.def.section;
4542 }
4543 else
4544 {
4545 Elf_Internal_Sym *isym;
4546
4547 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4548 abfd, r_symndx);
4549 if (isym == NULL)
4550 return FALSE;
4551
4552 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4553 }
4554
4555 if (dest != sec)
4556 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4557 }
4558 goto rel24;
4559
4560 case R_PPC64_PLTCALL:
4561 ppc64_elf_section_data (sec)->has_pltcall = 1;
4562 /* Fall through. */
4563
4564 case R_PPC64_REL24:
4565 case R_PPC64_REL24_NOTOC:
4566 rel24:
4567 plt_list = ifunc;
4568 if (h != NULL)
4569 {
4570 h->needs_plt = 1;
4571 if (h->root.root.string[0] == '.'
4572 && h->root.root.string[1] != '\0')
4573 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4574
4575 if (h == tga || h == dottga)
4576 {
4577 sec->has_tls_reloc = 1;
4578 if (rel != relocs
4579 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4580 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4581 /* We have a new-style __tls_get_addr call with
4582 a marker reloc. */
4583 ;
4584 else
4585 /* Mark this section as having an old-style call. */
4586 sec->has_tls_get_addr_call = 1;
4587 }
4588 plt_list = &h->plt.plist;
4589 }
4590
4591 /* We may need a .plt entry if the function this reloc
4592 refers to is in a shared lib. */
4593 if (plt_list
4594 && !update_plt_info (abfd, plt_list, rel->r_addend))
4595 return FALSE;
4596 break;
4597
4598 case R_PPC64_ADDR14:
4599 case R_PPC64_ADDR14_BRNTAKEN:
4600 case R_PPC64_ADDR14_BRTAKEN:
4601 case R_PPC64_ADDR24:
4602 goto dodyn;
4603
4604 case R_PPC64_TPREL64:
4605 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4606 if (bfd_link_dll (info))
4607 info->flags |= DF_STATIC_TLS;
4608 goto dotlstoc;
4609
4610 case R_PPC64_DTPMOD64:
4611 if (rel + 1 < rel_end
4612 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4613 && rel[1].r_offset == rel->r_offset + 8)
4614 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4615 else
4616 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4617 goto dotlstoc;
4618
4619 case R_PPC64_DTPREL64:
4620 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4621 if (rel != relocs
4622 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4623 && rel[-1].r_offset == rel->r_offset - 8)
4624 /* This is the second reloc of a dtpmod, dtprel pair.
4625 Don't mark with TLS_DTPREL. */
4626 goto dodyn;
4627
4628 dotlstoc:
4629 sec->has_tls_reloc = 1;
4630 if (h != NULL)
4631 {
4632 struct ppc_link_hash_entry *eh;
4633 eh = (struct ppc_link_hash_entry *) h;
4634 eh->tls_mask |= tls_type;
4635 }
4636 else
4637 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4638 rel->r_addend, tls_type))
4639 return FALSE;
4640
4641 ppc64_sec = ppc64_elf_section_data (sec);
4642 if (ppc64_sec->sec_type != sec_toc)
4643 {
4644 bfd_size_type amt;
4645
4646 /* One extra to simplify get_tls_mask. */
4647 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4648 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4649 if (ppc64_sec->u.toc.symndx == NULL)
4650 return FALSE;
4651 amt = sec->size * sizeof (bfd_vma) / 8;
4652 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4653 if (ppc64_sec->u.toc.add == NULL)
4654 return FALSE;
4655 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4656 ppc64_sec->sec_type = sec_toc;
4657 }
4658 BFD_ASSERT (rel->r_offset % 8 == 0);
4659 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
4660 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
4661
4662 /* Mark the second slot of a GD or LD entry.
4663 -1 to indicate GD and -2 to indicate LD. */
4664 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4665 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
4666 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4667 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
4668 goto dodyn;
4669
4670 case R_PPC64_TPREL16:
4671 case R_PPC64_TPREL16_LO:
4672 case R_PPC64_TPREL16_HI:
4673 case R_PPC64_TPREL16_HA:
4674 case R_PPC64_TPREL16_DS:
4675 case R_PPC64_TPREL16_LO_DS:
4676 case R_PPC64_TPREL16_HIGH:
4677 case R_PPC64_TPREL16_HIGHA:
4678 case R_PPC64_TPREL16_HIGHER:
4679 case R_PPC64_TPREL16_HIGHERA:
4680 case R_PPC64_TPREL16_HIGHEST:
4681 case R_PPC64_TPREL16_HIGHESTA:
4682 if (bfd_link_dll (info))
4683 info->flags |= DF_STATIC_TLS;
4684 goto dodyn;
4685
4686 case R_PPC64_ADDR64:
4687 if (is_opd
4688 && rel + 1 < rel_end
4689 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
4690 {
4691 if (h != NULL)
4692 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4693 }
4694 /* Fall through. */
4695
4696 case R_PPC64_ADDR16:
4697 case R_PPC64_ADDR16_DS:
4698 case R_PPC64_ADDR16_HA:
4699 case R_PPC64_ADDR16_HI:
4700 case R_PPC64_ADDR16_HIGH:
4701 case R_PPC64_ADDR16_HIGHA:
4702 case R_PPC64_ADDR16_HIGHER:
4703 case R_PPC64_ADDR16_HIGHERA:
4704 case R_PPC64_ADDR16_HIGHEST:
4705 case R_PPC64_ADDR16_HIGHESTA:
4706 case R_PPC64_ADDR16_LO:
4707 case R_PPC64_ADDR16_LO_DS:
4708 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
4709 && rel->r_addend == 0)
4710 {
4711 /* We may need a .plt entry if this reloc refers to a
4712 function in a shared lib. */
4713 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4714 return FALSE;
4715 h->pointer_equality_needed = 1;
4716 }
4717 /* Fall through. */
4718
4719 case R_PPC64_REL30:
4720 case R_PPC64_REL32:
4721 case R_PPC64_REL64:
4722 case R_PPC64_ADDR32:
4723 case R_PPC64_UADDR16:
4724 case R_PPC64_UADDR32:
4725 case R_PPC64_UADDR64:
4726 case R_PPC64_TOC:
4727 if (h != NULL && !bfd_link_pic (info))
4728 /* We may need a copy reloc. */
4729 h->non_got_ref = 1;
4730
4731 /* Don't propagate .opd relocs. */
4732 if (NO_OPD_RELOCS && is_opd)
4733 break;
4734
4735 /* If we are creating a shared library, and this is a reloc
4736 against a global symbol, or a non PC relative reloc
4737 against a local symbol, then we need to copy the reloc
4738 into the shared library. However, if we are linking with
4739 -Bsymbolic, we do not need to copy a reloc against a
4740 global symbol which is defined in an object we are
4741 including in the link (i.e., DEF_REGULAR is set). At
4742 this point we have not seen all the input files, so it is
4743 possible that DEF_REGULAR is not set now but will be set
4744 later (it is never cleared). In case of a weak definition,
4745 DEF_REGULAR may be cleared later by a strong definition in
4746 a shared library. We account for that possibility below by
4747 storing information in the dyn_relocs field of the hash
4748 table entry. A similar situation occurs when creating
4749 shared libraries and symbol visibility changes render the
4750 symbol local.
4751
4752 If on the other hand, we are creating an executable, we
4753 may need to keep relocations for symbols satisfied by a
4754 dynamic library if we manage to avoid copy relocs for the
4755 symbol. */
4756 dodyn:
4757 if ((bfd_link_pic (info)
4758 && (must_be_dyn_reloc (info, r_type)
4759 || (h != NULL
4760 && (!SYMBOLIC_BIND (info, h)
4761 || h->root.type == bfd_link_hash_defweak
4762 || !h->def_regular))))
4763 || (ELIMINATE_COPY_RELOCS
4764 && !bfd_link_pic (info)
4765 && h != NULL
4766 && (h->root.type == bfd_link_hash_defweak
4767 || !h->def_regular))
4768 || (!bfd_link_pic (info)
4769 && ifunc != NULL))
4770 {
4771 /* We must copy these reloc types into the output file.
4772 Create a reloc section in dynobj and make room for
4773 this reloc. */
4774 if (sreloc == NULL)
4775 {
4776 sreloc = _bfd_elf_make_dynamic_reloc_section
4777 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
4778
4779 if (sreloc == NULL)
4780 return FALSE;
4781 }
4782
4783 /* If this is a global symbol, we count the number of
4784 relocations we need for this symbol. */
4785 if (h != NULL)
4786 {
4787 struct elf_dyn_relocs *p;
4788 struct elf_dyn_relocs **head;
4789
4790 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
4791 p = *head;
4792 if (p == NULL || p->sec != sec)
4793 {
4794 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4795 if (p == NULL)
4796 return FALSE;
4797 p->next = *head;
4798 *head = p;
4799 p->sec = sec;
4800 p->count = 0;
4801 p->pc_count = 0;
4802 }
4803 p->count += 1;
4804 if (!must_be_dyn_reloc (info, r_type))
4805 p->pc_count += 1;
4806 }
4807 else
4808 {
4809 /* Track dynamic relocs needed for local syms too.
4810 We really need local syms available to do this
4811 easily. Oh well. */
4812 struct ppc_dyn_relocs *p;
4813 struct ppc_dyn_relocs **head;
4814 bfd_boolean is_ifunc;
4815 asection *s;
4816 void *vpp;
4817 Elf_Internal_Sym *isym;
4818
4819 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4820 abfd, r_symndx);
4821 if (isym == NULL)
4822 return FALSE;
4823
4824 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4825 if (s == NULL)
4826 s = sec;
4827
4828 vpp = &elf_section_data (s)->local_dynrel;
4829 head = (struct ppc_dyn_relocs **) vpp;
4830 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
4831 p = *head;
4832 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
4833 p = p->next;
4834 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
4835 {
4836 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4837 if (p == NULL)
4838 return FALSE;
4839 p->next = *head;
4840 *head = p;
4841 p->sec = sec;
4842 p->ifunc = is_ifunc;
4843 p->count = 0;
4844 }
4845 p->count += 1;
4846 }
4847 }
4848 break;
4849
4850 default:
4851 break;
4852 }
4853 }
4854
4855 return TRUE;
4856 }
4857
4858 /* Merge backend specific data from an object file to the output
4859 object file when linking. */
4860
4861 static bfd_boolean
4862 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4863 {
4864 bfd *obfd = info->output_bfd;
4865 unsigned long iflags, oflags;
4866
4867 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
4868 return TRUE;
4869
4870 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
4871 return TRUE;
4872
4873 if (!_bfd_generic_verify_endian_match (ibfd, info))
4874 return FALSE;
4875
4876 iflags = elf_elfheader (ibfd)->e_flags;
4877 oflags = elf_elfheader (obfd)->e_flags;
4878
4879 if (iflags & ~EF_PPC64_ABI)
4880 {
4881 _bfd_error_handler
4882 /* xgettext:c-format */
4883 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
4884 bfd_set_error (bfd_error_bad_value);
4885 return FALSE;
4886 }
4887 else if (iflags != oflags && iflags != 0)
4888 {
4889 _bfd_error_handler
4890 /* xgettext:c-format */
4891 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
4892 ibfd, iflags, oflags);
4893 bfd_set_error (bfd_error_bad_value);
4894 return FALSE;
4895 }
4896
4897 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
4898 return FALSE;
4899
4900 /* Merge Tag_compatibility attributes and any common GNU ones. */
4901 return _bfd_elf_merge_object_attributes (ibfd, info);
4902 }
4903
4904 static bfd_boolean
4905 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
4906 {
4907 /* Print normal ELF private data. */
4908 _bfd_elf_print_private_bfd_data (abfd, ptr);
4909
4910 if (elf_elfheader (abfd)->e_flags != 0)
4911 {
4912 FILE *file = ptr;
4913
4914 fprintf (file, _("private flags = 0x%lx:"),
4915 elf_elfheader (abfd)->e_flags);
4916
4917 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
4918 fprintf (file, _(" [abiv%ld]"),
4919 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
4920 fputc ('\n', file);
4921 }
4922
4923 return TRUE;
4924 }
4925
4926 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
4927 of the code entry point, and its section, which must be in the same
4928 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
4929
4930 static bfd_vma
4931 opd_entry_value (asection *opd_sec,
4932 bfd_vma offset,
4933 asection **code_sec,
4934 bfd_vma *code_off,
4935 bfd_boolean in_code_sec)
4936 {
4937 bfd *opd_bfd = opd_sec->owner;
4938 Elf_Internal_Rela *relocs;
4939 Elf_Internal_Rela *lo, *hi, *look;
4940 bfd_vma val;
4941
4942 /* No relocs implies we are linking a --just-symbols object, or looking
4943 at a final linked executable with addr2line or somesuch. */
4944 if (opd_sec->reloc_count == 0)
4945 {
4946 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
4947
4948 if (contents == NULL)
4949 {
4950 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
4951 return (bfd_vma) -1;
4952 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
4953 }
4954
4955 /* PR 17512: file: 64b9dfbb. */
4956 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
4957 return (bfd_vma) -1;
4958
4959 val = bfd_get_64 (opd_bfd, contents + offset);
4960 if (code_sec != NULL)
4961 {
4962 asection *sec, *likely = NULL;
4963
4964 if (in_code_sec)
4965 {
4966 sec = *code_sec;
4967 if (sec->vma <= val
4968 && val < sec->vma + sec->size)
4969 likely = sec;
4970 else
4971 val = -1;
4972 }
4973 else
4974 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
4975 if (sec->vma <= val
4976 && (sec->flags & SEC_LOAD) != 0
4977 && (sec->flags & SEC_ALLOC) != 0)
4978 likely = sec;
4979 if (likely != NULL)
4980 {
4981 *code_sec = likely;
4982 if (code_off != NULL)
4983 *code_off = val - likely->vma;
4984 }
4985 }
4986 return val;
4987 }
4988
4989 BFD_ASSERT (is_ppc64_elf (opd_bfd));
4990
4991 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
4992 if (relocs == NULL)
4993 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
4994 /* PR 17512: file: df8e1fd6. */
4995 if (relocs == NULL)
4996 return (bfd_vma) -1;
4997
4998 /* Go find the opd reloc at the sym address. */
4999 lo = relocs;
5000 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5001 val = (bfd_vma) -1;
5002 while (lo < hi)
5003 {
5004 look = lo + (hi - lo) / 2;
5005 if (look->r_offset < offset)
5006 lo = look + 1;
5007 else if (look->r_offset > offset)
5008 hi = look;
5009 else
5010 {
5011 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5012
5013 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5014 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5015 {
5016 unsigned long symndx = ELF64_R_SYM (look->r_info);
5017 asection *sec = NULL;
5018
5019 if (symndx >= symtab_hdr->sh_info
5020 && elf_sym_hashes (opd_bfd) != NULL)
5021 {
5022 struct elf_link_hash_entry **sym_hashes;
5023 struct elf_link_hash_entry *rh;
5024
5025 sym_hashes = elf_sym_hashes (opd_bfd);
5026 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5027 if (rh != NULL)
5028 {
5029 rh = elf_follow_link (rh);
5030 if (rh->root.type != bfd_link_hash_defined
5031 && rh->root.type != bfd_link_hash_defweak)
5032 break;
5033 if (rh->root.u.def.section->owner == opd_bfd)
5034 {
5035 val = rh->root.u.def.value;
5036 sec = rh->root.u.def.section;
5037 }
5038 }
5039 }
5040
5041 if (sec == NULL)
5042 {
5043 Elf_Internal_Sym *sym;
5044
5045 if (symndx < symtab_hdr->sh_info)
5046 {
5047 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5048 if (sym == NULL)
5049 {
5050 size_t symcnt = symtab_hdr->sh_info;
5051 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5052 symcnt, 0,
5053 NULL, NULL, NULL);
5054 if (sym == NULL)
5055 break;
5056 symtab_hdr->contents = (bfd_byte *) sym;
5057 }
5058 sym += symndx;
5059 }
5060 else
5061 {
5062 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5063 1, symndx,
5064 NULL, NULL, NULL);
5065 if (sym == NULL)
5066 break;
5067 }
5068 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5069 if (sec == NULL)
5070 break;
5071 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5072 val = sym->st_value;
5073 }
5074
5075 val += look->r_addend;
5076 if (code_off != NULL)
5077 *code_off = val;
5078 if (code_sec != NULL)
5079 {
5080 if (in_code_sec && *code_sec != sec)
5081 return -1;
5082 else
5083 *code_sec = sec;
5084 }
5085 if (sec->output_section != NULL)
5086 val += sec->output_section->vma + sec->output_offset;
5087 }
5088 break;
5089 }
5090 }
5091
5092 return val;
5093 }
5094
5095 /* If the ELF symbol SYM might be a function in SEC, return the
5096 function size and set *CODE_OFF to the function's entry point,
5097 otherwise return zero. */
5098
5099 static bfd_size_type
5100 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5101 bfd_vma *code_off)
5102 {
5103 bfd_size_type size;
5104
5105 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5106 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5107 return 0;
5108
5109 size = 0;
5110 if (!(sym->flags & BSF_SYNTHETIC))
5111 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5112
5113 if (strcmp (sym->section->name, ".opd") == 0)
5114 {
5115 struct _opd_sec_data *opd = get_opd_info (sym->section);
5116 bfd_vma symval = sym->value;
5117
5118 if (opd != NULL
5119 && opd->adjust != NULL
5120 && elf_section_data (sym->section)->relocs != NULL)
5121 {
5122 /* opd_entry_value will use cached relocs that have been
5123 adjusted, but with raw symbols. That means both local
5124 and global symbols need adjusting. */
5125 long adjust = opd->adjust[OPD_NDX (symval)];
5126 if (adjust == -1)
5127 return 0;
5128 symval += adjust;
5129 }
5130
5131 if (opd_entry_value (sym->section, symval,
5132 &sec, code_off, TRUE) == (bfd_vma) -1)
5133 return 0;
5134 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5135 symbol. This size has nothing to do with the code size of the
5136 function, which is what we're supposed to return, but the
5137 code size isn't available without looking up the dot-sym.
5138 However, doing that would be a waste of time particularly
5139 since elf_find_function will look at the dot-sym anyway.
5140 Now, elf_find_function will keep the largest size of any
5141 function sym found at the code address of interest, so return
5142 1 here to avoid it incorrectly caching a larger function size
5143 for a small function. This does mean we return the wrong
5144 size for a new-ABI function of size 24, but all that does is
5145 disable caching for such functions. */
5146 if (size == 24)
5147 size = 1;
5148 }
5149 else
5150 {
5151 if (sym->section != sec)
5152 return 0;
5153 *code_off = sym->value;
5154 }
5155 if (size == 0)
5156 size = 1;
5157 return size;
5158 }
5159
5160 /* Return true if symbol is a strong function defined in an ELFv2
5161 object with st_other localentry bits of zero, ie. its local entry
5162 point coincides with its global entry point. */
5163
5164 static bfd_boolean
5165 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5166 {
5167 return (h != NULL
5168 && h->type == STT_FUNC
5169 && h->root.type == bfd_link_hash_defined
5170 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5171 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
5172 && is_ppc64_elf (h->root.u.def.section->owner)
5173 && abiversion (h->root.u.def.section->owner) >= 2);
5174 }
5175
5176 /* Return true if symbol is defined in a regular object file. */
5177
5178 static bfd_boolean
5179 is_static_defined (struct elf_link_hash_entry *h)
5180 {
5181 return ((h->root.type == bfd_link_hash_defined
5182 || h->root.type == bfd_link_hash_defweak)
5183 && h->root.u.def.section != NULL
5184 && h->root.u.def.section->output_section != NULL);
5185 }
5186
5187 /* If FDH is a function descriptor symbol, return the associated code
5188 entry symbol if it is defined. Return NULL otherwise. */
5189
5190 static struct ppc_link_hash_entry *
5191 defined_code_entry (struct ppc_link_hash_entry *fdh)
5192 {
5193 if (fdh->is_func_descriptor)
5194 {
5195 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5196 if (fh->elf.root.type == bfd_link_hash_defined
5197 || fh->elf.root.type == bfd_link_hash_defweak)
5198 return fh;
5199 }
5200 return NULL;
5201 }
5202
5203 /* If FH is a function code entry symbol, return the associated
5204 function descriptor symbol if it is defined. Return NULL otherwise. */
5205
5206 static struct ppc_link_hash_entry *
5207 defined_func_desc (struct ppc_link_hash_entry *fh)
5208 {
5209 if (fh->oh != NULL
5210 && fh->oh->is_func_descriptor)
5211 {
5212 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5213 if (fdh->elf.root.type == bfd_link_hash_defined
5214 || fdh->elf.root.type == bfd_link_hash_defweak)
5215 return fdh;
5216 }
5217 return NULL;
5218 }
5219
5220 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5221
5222 /* Garbage collect sections, after first dealing with dot-symbols. */
5223
5224 static bfd_boolean
5225 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5226 {
5227 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5228
5229 if (htab != NULL && htab->need_func_desc_adj)
5230 {
5231 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5232 htab->need_func_desc_adj = 0;
5233 }
5234 return bfd_elf_gc_sections (abfd, info);
5235 }
5236
5237 /* Mark all our entry sym sections, both opd and code section. */
5238
5239 static void
5240 ppc64_elf_gc_keep (struct bfd_link_info *info)
5241 {
5242 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5243 struct bfd_sym_chain *sym;
5244
5245 if (htab == NULL)
5246 return;
5247
5248 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5249 {
5250 struct ppc_link_hash_entry *eh, *fh;
5251 asection *sec;
5252
5253 eh = (struct ppc_link_hash_entry *)
5254 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5255 if (eh == NULL)
5256 continue;
5257 if (eh->elf.root.type != bfd_link_hash_defined
5258 && eh->elf.root.type != bfd_link_hash_defweak)
5259 continue;
5260
5261 fh = defined_code_entry (eh);
5262 if (fh != NULL)
5263 {
5264 sec = fh->elf.root.u.def.section;
5265 sec->flags |= SEC_KEEP;
5266 }
5267 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5268 && opd_entry_value (eh->elf.root.u.def.section,
5269 eh->elf.root.u.def.value,
5270 &sec, NULL, FALSE) != (bfd_vma) -1)
5271 sec->flags |= SEC_KEEP;
5272
5273 sec = eh->elf.root.u.def.section;
5274 sec->flags |= SEC_KEEP;
5275 }
5276 }
5277
5278 /* Mark sections containing dynamically referenced symbols. When
5279 building shared libraries, we must assume that any visible symbol is
5280 referenced. */
5281
5282 static bfd_boolean
5283 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5284 {
5285 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5286 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5287 struct ppc_link_hash_entry *fdh;
5288 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5289
5290 /* Dynamic linking info is on the func descriptor sym. */
5291 fdh = defined_func_desc (eh);
5292 if (fdh != NULL)
5293 eh = fdh;
5294
5295 if ((eh->elf.root.type == bfd_link_hash_defined
5296 || eh->elf.root.type == bfd_link_hash_defweak)
5297 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5298 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5299 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5300 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5301 && (!bfd_link_executable (info)
5302 || info->gc_keep_exported
5303 || info->export_dynamic
5304 || (eh->elf.dynamic
5305 && d != NULL
5306 && (*d->match) (&d->head, NULL,
5307 eh->elf.root.root.string)))
5308 && (eh->elf.versioned >= versioned
5309 || !bfd_hide_sym_by_version (info->version_info,
5310 eh->elf.root.root.string)))))
5311 {
5312 asection *code_sec;
5313 struct ppc_link_hash_entry *fh;
5314
5315 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5316
5317 /* Function descriptor syms cause the associated
5318 function code sym section to be marked. */
5319 fh = defined_code_entry (eh);
5320 if (fh != NULL)
5321 {
5322 code_sec = fh->elf.root.u.def.section;
5323 code_sec->flags |= SEC_KEEP;
5324 }
5325 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5326 && opd_entry_value (eh->elf.root.u.def.section,
5327 eh->elf.root.u.def.value,
5328 &code_sec, NULL, FALSE) != (bfd_vma) -1)
5329 code_sec->flags |= SEC_KEEP;
5330 }
5331
5332 return TRUE;
5333 }
5334
5335 /* Return the section that should be marked against GC for a given
5336 relocation. */
5337
5338 static asection *
5339 ppc64_elf_gc_mark_hook (asection *sec,
5340 struct bfd_link_info *info,
5341 Elf_Internal_Rela *rel,
5342 struct elf_link_hash_entry *h,
5343 Elf_Internal_Sym *sym)
5344 {
5345 asection *rsec;
5346
5347 /* Syms return NULL if we're marking .opd, so we avoid marking all
5348 function sections, as all functions are referenced in .opd. */
5349 rsec = NULL;
5350 if (get_opd_info (sec) != NULL)
5351 return rsec;
5352
5353 if (h != NULL)
5354 {
5355 enum elf_ppc64_reloc_type r_type;
5356 struct ppc_link_hash_entry *eh, *fh, *fdh;
5357
5358 r_type = ELF64_R_TYPE (rel->r_info);
5359 switch (r_type)
5360 {
5361 case R_PPC64_GNU_VTINHERIT:
5362 case R_PPC64_GNU_VTENTRY:
5363 break;
5364
5365 default:
5366 switch (h->root.type)
5367 {
5368 case bfd_link_hash_defined:
5369 case bfd_link_hash_defweak:
5370 eh = (struct ppc_link_hash_entry *) h;
5371 fdh = defined_func_desc (eh);
5372 if (fdh != NULL)
5373 {
5374 /* -mcall-aixdesc code references the dot-symbol on
5375 a call reloc. Mark the function descriptor too
5376 against garbage collection. */
5377 fdh->elf.mark = 1;
5378 if (fdh->elf.is_weakalias)
5379 weakdef (&fdh->elf)->mark = 1;
5380 eh = fdh;
5381 }
5382
5383 /* Function descriptor syms cause the associated
5384 function code sym section to be marked. */
5385 fh = defined_code_entry (eh);
5386 if (fh != NULL)
5387 {
5388 /* They also mark their opd section. */
5389 eh->elf.root.u.def.section->gc_mark = 1;
5390
5391 rsec = fh->elf.root.u.def.section;
5392 }
5393 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5394 && opd_entry_value (eh->elf.root.u.def.section,
5395 eh->elf.root.u.def.value,
5396 &rsec, NULL, FALSE) != (bfd_vma) -1)
5397 eh->elf.root.u.def.section->gc_mark = 1;
5398 else
5399 rsec = h->root.u.def.section;
5400 break;
5401
5402 case bfd_link_hash_common:
5403 rsec = h->root.u.c.p->section;
5404 break;
5405
5406 default:
5407 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5408 }
5409 }
5410 }
5411 else
5412 {
5413 struct _opd_sec_data *opd;
5414
5415 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5416 opd = get_opd_info (rsec);
5417 if (opd != NULL && opd->func_sec != NULL)
5418 {
5419 rsec->gc_mark = 1;
5420
5421 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
5422 }
5423 }
5424
5425 return rsec;
5426 }
5427
5428 /* The maximum size of .sfpr. */
5429 #define SFPR_MAX (218*4)
5430
5431 struct sfpr_def_parms
5432 {
5433 const char name[12];
5434 unsigned char lo, hi;
5435 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5436 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
5437 };
5438
5439 /* Auto-generate _save*, _rest* functions in .sfpr.
5440 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5441 instead. */
5442
5443 static bfd_boolean
5444 sfpr_define (struct bfd_link_info *info,
5445 const struct sfpr_def_parms *parm,
5446 asection *stub_sec)
5447 {
5448 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5449 unsigned int i;
5450 size_t len = strlen (parm->name);
5451 bfd_boolean writing = FALSE;
5452 char sym[16];
5453
5454 if (htab == NULL)
5455 return FALSE;
5456
5457 memcpy (sym, parm->name, len);
5458 sym[len + 2] = 0;
5459
5460 for (i = parm->lo; i <= parm->hi; i++)
5461 {
5462 struct ppc_link_hash_entry *h;
5463
5464 sym[len + 0] = i / 10 + '0';
5465 sym[len + 1] = i % 10 + '0';
5466 h = (struct ppc_link_hash_entry *)
5467 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
5468 if (stub_sec != NULL)
5469 {
5470 if (h != NULL
5471 && h->elf.root.type == bfd_link_hash_defined
5472 && h->elf.root.u.def.section == htab->sfpr)
5473 {
5474 struct elf_link_hash_entry *s;
5475 char buf[32];
5476 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5477 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5478 if (s == NULL)
5479 return FALSE;
5480 if (s->root.type == bfd_link_hash_new
5481 || (s->root.type = bfd_link_hash_defined
5482 && s->root.u.def.section == stub_sec))
5483 {
5484 s->root.type = bfd_link_hash_defined;
5485 s->root.u.def.section = stub_sec;
5486 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
5487 + h->elf.root.u.def.value);
5488 s->ref_regular = 1;
5489 s->def_regular = 1;
5490 s->ref_regular_nonweak = 1;
5491 s->forced_local = 1;
5492 s->non_elf = 0;
5493 s->root.linker_def = 1;
5494 }
5495 }
5496 continue;
5497 }
5498 if (h != NULL)
5499 {
5500 h->save_res = 1;
5501 if (!h->elf.def_regular)
5502 {
5503 h->elf.root.type = bfd_link_hash_defined;
5504 h->elf.root.u.def.section = htab->sfpr;
5505 h->elf.root.u.def.value = htab->sfpr->size;
5506 h->elf.type = STT_FUNC;
5507 h->elf.def_regular = 1;
5508 h->elf.non_elf = 0;
5509 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5510 writing = TRUE;
5511 if (htab->sfpr->contents == NULL)
5512 {
5513 htab->sfpr->contents
5514 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5515 if (htab->sfpr->contents == NULL)
5516 return FALSE;
5517 }
5518 }
5519 }
5520 if (writing)
5521 {
5522 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5523 if (i != parm->hi)
5524 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5525 else
5526 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5527 htab->sfpr->size = p - htab->sfpr->contents;
5528 }
5529 }
5530
5531 return TRUE;
5532 }
5533
5534 static bfd_byte *
5535 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5536 {
5537 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5538 return p + 4;
5539 }
5540
5541 static bfd_byte *
5542 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5543 {
5544 p = savegpr0 (abfd, p, r);
5545 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5546 p = p + 4;
5547 bfd_put_32 (abfd, BLR, p);
5548 return p + 4;
5549 }
5550
5551 static bfd_byte *
5552 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5553 {
5554 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5555 return p + 4;
5556 }
5557
5558 static bfd_byte *
5559 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5560 {
5561 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5562 p = p + 4;
5563 p = restgpr0 (abfd, p, r);
5564 bfd_put_32 (abfd, MTLR_R0, p);
5565 p = p + 4;
5566 if (r == 29)
5567 {
5568 p = restgpr0 (abfd, p, 30);
5569 p = restgpr0 (abfd, p, 31);
5570 }
5571 bfd_put_32 (abfd, BLR, p);
5572 return p + 4;
5573 }
5574
5575 static bfd_byte *
5576 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5577 {
5578 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5579 return p + 4;
5580 }
5581
5582 static bfd_byte *
5583 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5584 {
5585 p = savegpr1 (abfd, p, r);
5586 bfd_put_32 (abfd, BLR, p);
5587 return p + 4;
5588 }
5589
5590 static bfd_byte *
5591 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5592 {
5593 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5594 return p + 4;
5595 }
5596
5597 static bfd_byte *
5598 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5599 {
5600 p = restgpr1 (abfd, p, r);
5601 bfd_put_32 (abfd, BLR, p);
5602 return p + 4;
5603 }
5604
5605 static bfd_byte *
5606 savefpr (bfd *abfd, bfd_byte *p, int r)
5607 {
5608 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5609 return p + 4;
5610 }
5611
5612 static bfd_byte *
5613 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5614 {
5615 p = savefpr (abfd, p, r);
5616 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5617 p = p + 4;
5618 bfd_put_32 (abfd, BLR, p);
5619 return p + 4;
5620 }
5621
5622 static bfd_byte *
5623 restfpr (bfd *abfd, bfd_byte *p, int r)
5624 {
5625 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5626 return p + 4;
5627 }
5628
5629 static bfd_byte *
5630 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5631 {
5632 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5633 p = p + 4;
5634 p = restfpr (abfd, p, r);
5635 bfd_put_32 (abfd, MTLR_R0, p);
5636 p = p + 4;
5637 if (r == 29)
5638 {
5639 p = restfpr (abfd, p, 30);
5640 p = restfpr (abfd, p, 31);
5641 }
5642 bfd_put_32 (abfd, BLR, p);
5643 return p + 4;
5644 }
5645
5646 static bfd_byte *
5647 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5648 {
5649 p = savefpr (abfd, p, r);
5650 bfd_put_32 (abfd, BLR, p);
5651 return p + 4;
5652 }
5653
5654 static bfd_byte *
5655 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5656 {
5657 p = restfpr (abfd, p, r);
5658 bfd_put_32 (abfd, BLR, p);
5659 return p + 4;
5660 }
5661
5662 static bfd_byte *
5663 savevr (bfd *abfd, bfd_byte *p, int r)
5664 {
5665 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5666 p = p + 4;
5667 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5668 return p + 4;
5669 }
5670
5671 static bfd_byte *
5672 savevr_tail (bfd *abfd, bfd_byte *p, int r)
5673 {
5674 p = savevr (abfd, p, r);
5675 bfd_put_32 (abfd, BLR, p);
5676 return p + 4;
5677 }
5678
5679 static bfd_byte *
5680 restvr (bfd *abfd, bfd_byte *p, int r)
5681 {
5682 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5683 p = p + 4;
5684 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5685 return p + 4;
5686 }
5687
5688 static bfd_byte *
5689 restvr_tail (bfd *abfd, bfd_byte *p, int r)
5690 {
5691 p = restvr (abfd, p, r);
5692 bfd_put_32 (abfd, BLR, p);
5693 return p + 4;
5694 }
5695
5696 /* Called via elf_link_hash_traverse to transfer dynamic linking
5697 information on function code symbol entries to their corresponding
5698 function descriptor symbol entries. */
5699
5700 static bfd_boolean
5701 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5702 {
5703 struct bfd_link_info *info;
5704 struct ppc_link_hash_table *htab;
5705 struct ppc_link_hash_entry *fh;
5706 struct ppc_link_hash_entry *fdh;
5707 bfd_boolean force_local;
5708
5709 fh = (struct ppc_link_hash_entry *) h;
5710 if (fh->elf.root.type == bfd_link_hash_indirect)
5711 return TRUE;
5712
5713 if (!fh->is_func)
5714 return TRUE;
5715
5716 if (fh->elf.root.root.string[0] != '.'
5717 || fh->elf.root.root.string[1] == '\0')
5718 return TRUE;
5719
5720 info = inf;
5721 htab = ppc_hash_table (info);
5722 if (htab == NULL)
5723 return FALSE;
5724
5725 /* Find the corresponding function descriptor symbol. */
5726 fdh = lookup_fdh (fh, htab);
5727
5728 /* Resolve undefined references to dot-symbols as the value
5729 in the function descriptor, if we have one in a regular object.
5730 This is to satisfy cases like ".quad .foo". Calls to functions
5731 in dynamic objects are handled elsewhere. */
5732 if ((fh->elf.root.type == bfd_link_hash_undefined
5733 || fh->elf.root.type == bfd_link_hash_undefweak)
5734 && (fdh->elf.root.type == bfd_link_hash_defined
5735 || fdh->elf.root.type == bfd_link_hash_defweak)
5736 && get_opd_info (fdh->elf.root.u.def.section) != NULL
5737 && opd_entry_value (fdh->elf.root.u.def.section,
5738 fdh->elf.root.u.def.value,
5739 &fh->elf.root.u.def.section,
5740 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
5741 {
5742 fh->elf.root.type = fdh->elf.root.type;
5743 fh->elf.forced_local = 1;
5744 fh->elf.def_regular = fdh->elf.def_regular;
5745 fh->elf.def_dynamic = fdh->elf.def_dynamic;
5746 }
5747
5748 if (!fh->elf.dynamic)
5749 {
5750 struct plt_entry *ent;
5751
5752 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5753 if (ent->plt.refcount > 0)
5754 break;
5755 if (ent == NULL)
5756 return TRUE;
5757 }
5758
5759 /* Create a descriptor as undefined if necessary. */
5760 if (fdh == NULL
5761 && !bfd_link_executable (info)
5762 && (fh->elf.root.type == bfd_link_hash_undefined
5763 || fh->elf.root.type == bfd_link_hash_undefweak))
5764 {
5765 fdh = make_fdh (info, fh);
5766 if (fdh == NULL)
5767 return FALSE;
5768 }
5769
5770 /* We can't support overriding of symbols on a fake descriptor. */
5771 if (fdh != NULL
5772 && fdh->fake
5773 && (fh->elf.root.type == bfd_link_hash_defined
5774 || fh->elf.root.type == bfd_link_hash_defweak))
5775 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
5776
5777 /* Transfer dynamic linking information to the function descriptor. */
5778 if (fdh != NULL)
5779 {
5780 fdh->elf.ref_regular |= fh->elf.ref_regular;
5781 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5782 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5783 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
5784 fdh->elf.dynamic |= fh->elf.dynamic;
5785 fdh->elf.needs_plt |= (fh->elf.needs_plt
5786 || fh->elf.type == STT_FUNC
5787 || fh->elf.type == STT_GNU_IFUNC);
5788 move_plt_plist (fh, fdh);
5789
5790 if (!fdh->elf.forced_local
5791 && fh->elf.dynindx != -1)
5792 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5793 return FALSE;
5794 }
5795
5796 /* Now that the info is on the function descriptor, clear the
5797 function code sym info. Any function code syms for which we
5798 don't have a definition in a regular file, we force local.
5799 This prevents a shared library from exporting syms that have
5800 been imported from another library. Function code syms that
5801 are really in the library we must leave global to prevent the
5802 linker dragging in a definition from a static library. */
5803 force_local = (!fh->elf.def_regular
5804 || fdh == NULL
5805 || !fdh->elf.def_regular
5806 || fdh->elf.forced_local);
5807 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5808
5809 return TRUE;
5810 }
5811
5812 static const struct sfpr_def_parms save_res_funcs[] =
5813 {
5814 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5815 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5816 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5817 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5818 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5819 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5820 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5821 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5822 { "._savef", 14, 31, savefpr, savefpr1_tail },
5823 { "._restf", 14, 31, restfpr, restfpr1_tail },
5824 { "_savevr_", 20, 31, savevr, savevr_tail },
5825 { "_restvr_", 20, 31, restvr, restvr_tail }
5826 };
5827
5828 /* Called near the start of bfd_elf_size_dynamic_sections. We use
5829 this hook to a) provide some gcc support functions, and b) transfer
5830 dynamic linking information gathered so far on function code symbol
5831 entries, to their corresponding function descriptor symbol entries. */
5832
5833 static bfd_boolean
5834 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5835 struct bfd_link_info *info)
5836 {
5837 struct ppc_link_hash_table *htab;
5838
5839 htab = ppc_hash_table (info);
5840 if (htab == NULL)
5841 return FALSE;
5842
5843 /* Provide any missing _save* and _rest* functions. */
5844 if (htab->sfpr != NULL)
5845 {
5846 unsigned int i;
5847
5848 htab->sfpr->size = 0;
5849 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
5850 if (!sfpr_define (info, &save_res_funcs[i], NULL))
5851 return FALSE;
5852 if (htab->sfpr->size == 0)
5853 htab->sfpr->flags |= SEC_EXCLUDE;
5854 }
5855
5856 if (bfd_link_relocatable (info))
5857 return TRUE;
5858
5859 if (htab->elf.hgot != NULL)
5860 {
5861 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
5862 /* Make .TOC. defined so as to prevent it being made dynamic.
5863 The wrong value here is fixed later in ppc64_elf_set_toc. */
5864 if (!htab->elf.hgot->def_regular
5865 || htab->elf.hgot->root.type != bfd_link_hash_defined)
5866 {
5867 htab->elf.hgot->root.type = bfd_link_hash_defined;
5868 htab->elf.hgot->root.u.def.value = 0;
5869 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
5870 htab->elf.hgot->def_regular = 1;
5871 htab->elf.hgot->root.linker_def = 1;
5872 }
5873 htab->elf.hgot->type = STT_OBJECT;
5874 htab->elf.hgot->other
5875 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5876 }
5877
5878 if (htab->need_func_desc_adj)
5879 {
5880 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5881 htab->need_func_desc_adj = 0;
5882 }
5883
5884 return TRUE;
5885 }
5886
5887 /* Find dynamic relocs for H that apply to read-only sections. */
5888
5889 static asection *
5890 readonly_dynrelocs (struct elf_link_hash_entry *h)
5891 {
5892 struct ppc_link_hash_entry *eh;
5893 struct elf_dyn_relocs *p;
5894
5895 eh = (struct ppc_link_hash_entry *) h;
5896 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5897 {
5898 asection *s = p->sec->output_section;
5899
5900 if (s != NULL && (s->flags & SEC_READONLY) != 0)
5901 return p->sec;
5902 }
5903 return NULL;
5904 }
5905
5906 /* Return true if we have dynamic relocs against H or any of its weak
5907 aliases, that apply to read-only sections. Cannot be used after
5908 size_dynamic_sections. */
5909
5910 static bfd_boolean
5911 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
5912 {
5913 struct ppc_link_hash_entry *eh;
5914
5915 eh = (struct ppc_link_hash_entry *) h;
5916 do
5917 {
5918 if (readonly_dynrelocs (&eh->elf))
5919 return TRUE;
5920 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
5921 }
5922 while (eh != NULL && &eh->elf != h);
5923
5924 return FALSE;
5925 }
5926
5927 /* Return whether EH has pc-relative dynamic relocs. */
5928
5929 static bfd_boolean
5930 pc_dynrelocs (struct ppc_link_hash_entry *eh)
5931 {
5932 struct elf_dyn_relocs *p;
5933
5934 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5935 if (p->pc_count != 0)
5936 return TRUE;
5937 return FALSE;
5938 }
5939
5940 /* Return true if a global entry stub will be created for H. Valid
5941 for ELFv2 before plt entries have been allocated. */
5942
5943 static bfd_boolean
5944 global_entry_stub (struct elf_link_hash_entry *h)
5945 {
5946 struct plt_entry *pent;
5947
5948 if (!h->pointer_equality_needed
5949 || h->def_regular)
5950 return FALSE;
5951
5952 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
5953 if (pent->plt.refcount > 0
5954 && pent->addend == 0)
5955 return TRUE;
5956
5957 return FALSE;
5958 }
5959
5960 /* Adjust a symbol defined by a dynamic object and referenced by a
5961 regular object. The current definition is in some section of the
5962 dynamic object, but we're not including those sections. We have to
5963 change the definition to something the rest of the link can
5964 understand. */
5965
5966 static bfd_boolean
5967 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
5968 struct elf_link_hash_entry *h)
5969 {
5970 struct ppc_link_hash_table *htab;
5971 asection *s, *srel;
5972
5973 htab = ppc_hash_table (info);
5974 if (htab == NULL)
5975 return FALSE;
5976
5977 /* Deal with function syms. */
5978 if (h->type == STT_FUNC
5979 || h->type == STT_GNU_IFUNC
5980 || h->needs_plt)
5981 {
5982 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
5983 || SYMBOL_CALLS_LOCAL (info, h)
5984 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
5985 /* Discard dyn_relocs when non-pic if we've decided that a
5986 function symbol is local and not an ifunc. We keep dynamic
5987 relocs for ifuncs when local rather than always emitting a
5988 plt call stub for them and defining the symbol on the call
5989 stub. We can't do that for ELFv1 anyway (a function symbol
5990 is defined on a descriptor, not code) and it can be faster at
5991 run-time due to not needing to bounce through a stub. The
5992 dyn_relocs for ifuncs will be applied even in a static
5993 executable. */
5994 if (!bfd_link_pic (info)
5995 && h->type != STT_GNU_IFUNC
5996 && local)
5997 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
5998
5999 /* Clear procedure linkage table information for any symbol that
6000 won't need a .plt entry. */
6001 struct plt_entry *ent;
6002 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6003 if (ent->plt.refcount > 0)
6004 break;
6005 if (ent == NULL
6006 || (h->type != STT_GNU_IFUNC
6007 && local
6008 && (htab->can_convert_all_inline_plt
6009 || (((struct ppc_link_hash_entry *) h)->tls_mask
6010 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6011 {
6012 h->plt.plist = NULL;
6013 h->needs_plt = 0;
6014 h->pointer_equality_needed = 0;
6015 }
6016 else if (abiversion (info->output_bfd) >= 2)
6017 {
6018 /* Taking a function's address in a read/write section
6019 doesn't require us to define the function symbol in the
6020 executable on a global entry stub. A dynamic reloc can
6021 be used instead. The reason we prefer a few more dynamic
6022 relocs is that calling via a global entry stub costs a
6023 few more instructions, and pointer_equality_needed causes
6024 extra work in ld.so when resolving these symbols. */
6025 if (global_entry_stub (h))
6026 {
6027 if (!readonly_dynrelocs (h))
6028 {
6029 h->pointer_equality_needed = 0;
6030 /* If we haven't seen a branch reloc and the symbol
6031 isn't an ifunc then we don't need a plt entry. */
6032 if (!h->needs_plt)
6033 h->plt.plist = NULL;
6034 }
6035 else if (!bfd_link_pic (info))
6036 /* We are going to be defining the function symbol on the
6037 plt stub, so no dyn_relocs needed when non-pic. */
6038 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6039 }
6040
6041 /* ELFv2 function symbols can't have copy relocs. */
6042 return TRUE;
6043 }
6044 else if (!h->needs_plt
6045 && !readonly_dynrelocs (h))
6046 {
6047 /* If we haven't seen a branch reloc and the symbol isn't an
6048 ifunc then we don't need a plt entry. */
6049 h->plt.plist = NULL;
6050 h->pointer_equality_needed = 0;
6051 return TRUE;
6052 }
6053 }
6054 else
6055 h->plt.plist = NULL;
6056
6057 /* If this is a weak symbol, and there is a real definition, the
6058 processor independent code will have arranged for us to see the
6059 real definition first, and we can just use the same value. */
6060 if (h->is_weakalias)
6061 {
6062 struct elf_link_hash_entry *def = weakdef (h);
6063 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6064 h->root.u.def.section = def->root.u.def.section;
6065 h->root.u.def.value = def->root.u.def.value;
6066 if (def->root.u.def.section == htab->elf.sdynbss
6067 || def->root.u.def.section == htab->elf.sdynrelro)
6068 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6069 return TRUE;
6070 }
6071
6072 /* If we are creating a shared library, we must presume that the
6073 only references to the symbol are via the global offset table.
6074 For such cases we need not do anything here; the relocations will
6075 be handled correctly by relocate_section. */
6076 if (bfd_link_pic (info))
6077 return TRUE;
6078
6079 /* If there are no references to this symbol that do not use the
6080 GOT, we don't need to generate a copy reloc. */
6081 if (!h->non_got_ref)
6082 return TRUE;
6083
6084 /* Don't generate a copy reloc for symbols defined in the executable. */
6085 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6086
6087 /* If -z nocopyreloc was given, don't generate them either. */
6088 || info->nocopyreloc
6089
6090 /* If we don't find any dynamic relocs in read-only sections, then
6091 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6092 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
6093
6094 /* Protected variables do not work with .dynbss. The copy in
6095 .dynbss won't be used by the shared library with the protected
6096 definition for the variable. Text relocations are preferable
6097 to an incorrect program. */
6098 || h->protected_def)
6099 return TRUE;
6100
6101 if (h->plt.plist != NULL)
6102 {
6103 /* We should never get here, but unfortunately there are versions
6104 of gcc out there that improperly (for this ABI) put initialized
6105 function pointers, vtable refs and suchlike in read-only
6106 sections. Allow them to proceed, but warn that this might
6107 break at runtime. */
6108 info->callbacks->einfo
6109 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6110 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6111 h->root.root.string);
6112 }
6113
6114 /* This is a reference to a symbol defined by a dynamic object which
6115 is not a function. */
6116
6117 /* We must allocate the symbol in our .dynbss section, which will
6118 become part of the .bss section of the executable. There will be
6119 an entry for this symbol in the .dynsym section. The dynamic
6120 object will contain position independent code, so all references
6121 from the dynamic object to this symbol will go through the global
6122 offset table. The dynamic linker will use the .dynsym entry to
6123 determine the address it must put in the global offset table, so
6124 both the dynamic object and the regular object will refer to the
6125 same memory location for the variable. */
6126 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6127 {
6128 s = htab->elf.sdynrelro;
6129 srel = htab->elf.sreldynrelro;
6130 }
6131 else
6132 {
6133 s = htab->elf.sdynbss;
6134 srel = htab->elf.srelbss;
6135 }
6136 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6137 {
6138 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6139 linker to copy the initial value out of the dynamic object
6140 and into the runtime process image. */
6141 srel->size += sizeof (Elf64_External_Rela);
6142 h->needs_copy = 1;
6143 }
6144
6145 /* We no longer want dyn_relocs. */
6146 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6147 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6148 }
6149
6150 /* If given a function descriptor symbol, hide both the function code
6151 sym and the descriptor. */
6152 static void
6153 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6154 struct elf_link_hash_entry *h,
6155 bfd_boolean force_local)
6156 {
6157 struct ppc_link_hash_entry *eh;
6158 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6159
6160 eh = (struct ppc_link_hash_entry *) h;
6161 if (eh->is_func_descriptor)
6162 {
6163 struct ppc_link_hash_entry *fh = eh->oh;
6164
6165 if (fh == NULL)
6166 {
6167 const char *p, *q;
6168 struct elf_link_hash_table *htab = elf_hash_table (info);
6169 char save;
6170
6171 /* We aren't supposed to use alloca in BFD because on
6172 systems which do not have alloca the version in libiberty
6173 calls xmalloc, which might cause the program to crash
6174 when it runs out of memory. This function doesn't have a
6175 return status, so there's no way to gracefully return an
6176 error. So cheat. We know that string[-1] can be safely
6177 accessed; It's either a string in an ELF string table,
6178 or allocated in an objalloc structure. */
6179
6180 p = eh->elf.root.root.string - 1;
6181 save = *p;
6182 *(char *) p = '.';
6183 fh = (struct ppc_link_hash_entry *)
6184 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6185 *(char *) p = save;
6186
6187 /* Unfortunately, if it so happens that the string we were
6188 looking for was allocated immediately before this string,
6189 then we overwrote the string terminator. That's the only
6190 reason the lookup should fail. */
6191 if (fh == NULL)
6192 {
6193 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6194 while (q >= eh->elf.root.root.string && *q == *p)
6195 --q, --p;
6196 if (q < eh->elf.root.root.string && *p == '.')
6197 fh = (struct ppc_link_hash_entry *)
6198 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6199 }
6200 if (fh != NULL)
6201 {
6202 eh->oh = fh;
6203 fh->oh = eh;
6204 }
6205 }
6206 if (fh != NULL)
6207 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6208 }
6209 }
6210
6211 static bfd_boolean
6212 get_sym_h (struct elf_link_hash_entry **hp,
6213 Elf_Internal_Sym **symp,
6214 asection **symsecp,
6215 unsigned char **tls_maskp,
6216 Elf_Internal_Sym **locsymsp,
6217 unsigned long r_symndx,
6218 bfd *ibfd)
6219 {
6220 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6221
6222 if (r_symndx >= symtab_hdr->sh_info)
6223 {
6224 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6225 struct elf_link_hash_entry *h;
6226
6227 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6228 h = elf_follow_link (h);
6229
6230 if (hp != NULL)
6231 *hp = h;
6232
6233 if (symp != NULL)
6234 *symp = NULL;
6235
6236 if (symsecp != NULL)
6237 {
6238 asection *symsec = NULL;
6239 if (h->root.type == bfd_link_hash_defined
6240 || h->root.type == bfd_link_hash_defweak)
6241 symsec = h->root.u.def.section;
6242 *symsecp = symsec;
6243 }
6244
6245 if (tls_maskp != NULL)
6246 {
6247 struct ppc_link_hash_entry *eh;
6248
6249 eh = (struct ppc_link_hash_entry *) h;
6250 *tls_maskp = &eh->tls_mask;
6251 }
6252 }
6253 else
6254 {
6255 Elf_Internal_Sym *sym;
6256 Elf_Internal_Sym *locsyms = *locsymsp;
6257
6258 if (locsyms == NULL)
6259 {
6260 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6261 if (locsyms == NULL)
6262 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6263 symtab_hdr->sh_info,
6264 0, NULL, NULL, NULL);
6265 if (locsyms == NULL)
6266 return FALSE;
6267 *locsymsp = locsyms;
6268 }
6269 sym = locsyms + r_symndx;
6270
6271 if (hp != NULL)
6272 *hp = NULL;
6273
6274 if (symp != NULL)
6275 *symp = sym;
6276
6277 if (symsecp != NULL)
6278 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6279
6280 if (tls_maskp != NULL)
6281 {
6282 struct got_entry **lgot_ents;
6283 unsigned char *tls_mask;
6284
6285 tls_mask = NULL;
6286 lgot_ents = elf_local_got_ents (ibfd);
6287 if (lgot_ents != NULL)
6288 {
6289 struct plt_entry **local_plt = (struct plt_entry **)
6290 (lgot_ents + symtab_hdr->sh_info);
6291 unsigned char *lgot_masks = (unsigned char *)
6292 (local_plt + symtab_hdr->sh_info);
6293 tls_mask = &lgot_masks[r_symndx];
6294 }
6295 *tls_maskp = tls_mask;
6296 }
6297 }
6298 return TRUE;
6299 }
6300
6301 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6302 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6303 type suitable for optimization, and 1 otherwise. */
6304
6305 static int
6306 get_tls_mask (unsigned char **tls_maskp,
6307 unsigned long *toc_symndx,
6308 bfd_vma *toc_addend,
6309 Elf_Internal_Sym **locsymsp,
6310 const Elf_Internal_Rela *rel,
6311 bfd *ibfd)
6312 {
6313 unsigned long r_symndx;
6314 int next_r;
6315 struct elf_link_hash_entry *h;
6316 Elf_Internal_Sym *sym;
6317 asection *sec;
6318 bfd_vma off;
6319
6320 r_symndx = ELF64_R_SYM (rel->r_info);
6321 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6322 return 0;
6323
6324 if ((*tls_maskp != NULL
6325 && (**tls_maskp & TLS_TLS) != 0
6326 && **tls_maskp != (TLS_TLS | TLS_MARK))
6327 || sec == NULL
6328 || ppc64_elf_section_data (sec) == NULL
6329 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6330 return 1;
6331
6332 /* Look inside a TOC section too. */
6333 if (h != NULL)
6334 {
6335 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6336 off = h->root.u.def.value;
6337 }
6338 else
6339 off = sym->st_value;
6340 off += rel->r_addend;
6341 BFD_ASSERT (off % 8 == 0);
6342 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6343 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6344 if (toc_symndx != NULL)
6345 *toc_symndx = r_symndx;
6346 if (toc_addend != NULL)
6347 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6348 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6349 return 0;
6350 if ((h == NULL || is_static_defined (h))
6351 && (next_r == -1 || next_r == -2))
6352 return 1 - next_r;
6353 return 1;
6354 }
6355
6356 /* Find (or create) an entry in the tocsave hash table. */
6357
6358 static struct tocsave_entry *
6359 tocsave_find (struct ppc_link_hash_table *htab,
6360 enum insert_option insert,
6361 Elf_Internal_Sym **local_syms,
6362 const Elf_Internal_Rela *irela,
6363 bfd *ibfd)
6364 {
6365 unsigned long r_indx;
6366 struct elf_link_hash_entry *h;
6367 Elf_Internal_Sym *sym;
6368 struct tocsave_entry ent, *p;
6369 hashval_t hash;
6370 struct tocsave_entry **slot;
6371
6372 r_indx = ELF64_R_SYM (irela->r_info);
6373 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6374 return NULL;
6375 if (ent.sec == NULL || ent.sec->output_section == NULL)
6376 {
6377 _bfd_error_handler
6378 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
6379 return NULL;
6380 }
6381
6382 if (h != NULL)
6383 ent.offset = h->root.u.def.value;
6384 else
6385 ent.offset = sym->st_value;
6386 ent.offset += irela->r_addend;
6387
6388 hash = tocsave_htab_hash (&ent);
6389 slot = ((struct tocsave_entry **)
6390 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6391 if (slot == NULL)
6392 return NULL;
6393
6394 if (*slot == NULL)
6395 {
6396 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6397 if (p == NULL)
6398 return NULL;
6399 *p = ent;
6400 *slot = p;
6401 }
6402 return *slot;
6403 }
6404
6405 /* Adjust all global syms defined in opd sections. In gcc generated
6406 code for the old ABI, these will already have been done. */
6407
6408 static bfd_boolean
6409 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6410 {
6411 struct ppc_link_hash_entry *eh;
6412 asection *sym_sec;
6413 struct _opd_sec_data *opd;
6414
6415 if (h->root.type == bfd_link_hash_indirect)
6416 return TRUE;
6417
6418 if (h->root.type != bfd_link_hash_defined
6419 && h->root.type != bfd_link_hash_defweak)
6420 return TRUE;
6421
6422 eh = (struct ppc_link_hash_entry *) h;
6423 if (eh->adjust_done)
6424 return TRUE;
6425
6426 sym_sec = eh->elf.root.u.def.section;
6427 opd = get_opd_info (sym_sec);
6428 if (opd != NULL && opd->adjust != NULL)
6429 {
6430 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
6431 if (adjust == -1)
6432 {
6433 /* This entry has been deleted. */
6434 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6435 if (dsec == NULL)
6436 {
6437 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6438 if (discarded_section (dsec))
6439 {
6440 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6441 break;
6442 }
6443 }
6444 eh->elf.root.u.def.value = 0;
6445 eh->elf.root.u.def.section = dsec;
6446 }
6447 else
6448 eh->elf.root.u.def.value += adjust;
6449 eh->adjust_done = 1;
6450 }
6451 return TRUE;
6452 }
6453
6454 /* Handles decrementing dynamic reloc counts for the reloc specified by
6455 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
6456 have already been determined. */
6457
6458 static bfd_boolean
6459 dec_dynrel_count (bfd_vma r_info,
6460 asection *sec,
6461 struct bfd_link_info *info,
6462 Elf_Internal_Sym **local_syms,
6463 struct elf_link_hash_entry *h,
6464 Elf_Internal_Sym *sym)
6465 {
6466 enum elf_ppc64_reloc_type r_type;
6467 asection *sym_sec = NULL;
6468
6469 /* Can this reloc be dynamic? This switch, and later tests here
6470 should be kept in sync with the code in check_relocs. */
6471 r_type = ELF64_R_TYPE (r_info);
6472 switch (r_type)
6473 {
6474 default:
6475 return TRUE;
6476
6477 case R_PPC64_TPREL16:
6478 case R_PPC64_TPREL16_LO:
6479 case R_PPC64_TPREL16_HI:
6480 case R_PPC64_TPREL16_HA:
6481 case R_PPC64_TPREL16_DS:
6482 case R_PPC64_TPREL16_LO_DS:
6483 case R_PPC64_TPREL16_HIGH:
6484 case R_PPC64_TPREL16_HIGHA:
6485 case R_PPC64_TPREL16_HIGHER:
6486 case R_PPC64_TPREL16_HIGHERA:
6487 case R_PPC64_TPREL16_HIGHEST:
6488 case R_PPC64_TPREL16_HIGHESTA:
6489 case R_PPC64_TPREL64:
6490 case R_PPC64_DTPMOD64:
6491 case R_PPC64_DTPREL64:
6492 case R_PPC64_ADDR64:
6493 case R_PPC64_REL30:
6494 case R_PPC64_REL32:
6495 case R_PPC64_REL64:
6496 case R_PPC64_ADDR14:
6497 case R_PPC64_ADDR14_BRNTAKEN:
6498 case R_PPC64_ADDR14_BRTAKEN:
6499 case R_PPC64_ADDR16:
6500 case R_PPC64_ADDR16_DS:
6501 case R_PPC64_ADDR16_HA:
6502 case R_PPC64_ADDR16_HI:
6503 case R_PPC64_ADDR16_HIGH:
6504 case R_PPC64_ADDR16_HIGHA:
6505 case R_PPC64_ADDR16_HIGHER:
6506 case R_PPC64_ADDR16_HIGHERA:
6507 case R_PPC64_ADDR16_HIGHEST:
6508 case R_PPC64_ADDR16_HIGHESTA:
6509 case R_PPC64_ADDR16_LO:
6510 case R_PPC64_ADDR16_LO_DS:
6511 case R_PPC64_ADDR24:
6512 case R_PPC64_ADDR32:
6513 case R_PPC64_UADDR16:
6514 case R_PPC64_UADDR32:
6515 case R_PPC64_UADDR64:
6516 case R_PPC64_TOC:
6517 break;
6518 }
6519
6520 if (local_syms != NULL)
6521 {
6522 unsigned long r_symndx;
6523 bfd *ibfd = sec->owner;
6524
6525 r_symndx = ELF64_R_SYM (r_info);
6526 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6527 return FALSE;
6528 }
6529
6530 if ((bfd_link_pic (info)
6531 && (must_be_dyn_reloc (info, r_type)
6532 || (h != NULL
6533 && (!SYMBOLIC_BIND (info, h)
6534 || h->root.type == bfd_link_hash_defweak
6535 || !h->def_regular))))
6536 || (ELIMINATE_COPY_RELOCS
6537 && !bfd_link_pic (info)
6538 && h != NULL
6539 && (h->root.type == bfd_link_hash_defweak
6540 || !h->def_regular)))
6541 ;
6542 else
6543 return TRUE;
6544
6545 if (h != NULL)
6546 {
6547 struct elf_dyn_relocs *p;
6548 struct elf_dyn_relocs **pp;
6549 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6550
6551 /* elf_gc_sweep may have already removed all dyn relocs associated
6552 with local syms for a given section. Also, symbol flags are
6553 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6554 report a dynreloc miscount. */
6555 if (*pp == NULL && info->gc_sections)
6556 return TRUE;
6557
6558 while ((p = *pp) != NULL)
6559 {
6560 if (p->sec == sec)
6561 {
6562 if (!must_be_dyn_reloc (info, r_type))
6563 p->pc_count -= 1;
6564 p->count -= 1;
6565 if (p->count == 0)
6566 *pp = p->next;
6567 return TRUE;
6568 }
6569 pp = &p->next;
6570 }
6571 }
6572 else
6573 {
6574 struct ppc_dyn_relocs *p;
6575 struct ppc_dyn_relocs **pp;
6576 void *vpp;
6577 bfd_boolean is_ifunc;
6578
6579 if (local_syms == NULL)
6580 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6581 if (sym_sec == NULL)
6582 sym_sec = sec;
6583
6584 vpp = &elf_section_data (sym_sec)->local_dynrel;
6585 pp = (struct ppc_dyn_relocs **) vpp;
6586
6587 if (*pp == NULL && info->gc_sections)
6588 return TRUE;
6589
6590 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6591 while ((p = *pp) != NULL)
6592 {
6593 if (p->sec == sec && p->ifunc == is_ifunc)
6594 {
6595 p->count -= 1;
6596 if (p->count == 0)
6597 *pp = p->next;
6598 return TRUE;
6599 }
6600 pp = &p->next;
6601 }
6602 }
6603
6604 /* xgettext:c-format */
6605 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6606 sec->owner, sec);
6607 bfd_set_error (bfd_error_bad_value);
6608 return FALSE;
6609 }
6610
6611 /* Remove unused Official Procedure Descriptor entries. Currently we
6612 only remove those associated with functions in discarded link-once
6613 sections, or weakly defined functions that have been overridden. It
6614 would be possible to remove many more entries for statically linked
6615 applications. */
6616
6617 bfd_boolean
6618 ppc64_elf_edit_opd (struct bfd_link_info *info)
6619 {
6620 bfd *ibfd;
6621 bfd_boolean some_edited = FALSE;
6622 asection *need_pad = NULL;
6623 struct ppc_link_hash_table *htab;
6624
6625 htab = ppc_hash_table (info);
6626 if (htab == NULL)
6627 return FALSE;
6628
6629 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6630 {
6631 asection *sec;
6632 Elf_Internal_Rela *relstart, *rel, *relend;
6633 Elf_Internal_Shdr *symtab_hdr;
6634 Elf_Internal_Sym *local_syms;
6635 struct _opd_sec_data *opd;
6636 bfd_boolean need_edit, add_aux_fields, broken;
6637 bfd_size_type cnt_16b = 0;
6638
6639 if (!is_ppc64_elf (ibfd))
6640 continue;
6641
6642 sec = bfd_get_section_by_name (ibfd, ".opd");
6643 if (sec == NULL || sec->size == 0)
6644 continue;
6645
6646 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6647 continue;
6648
6649 if (sec->output_section == bfd_abs_section_ptr)
6650 continue;
6651
6652 /* Look through the section relocs. */
6653 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6654 continue;
6655
6656 local_syms = NULL;
6657 symtab_hdr = &elf_symtab_hdr (ibfd);
6658
6659 /* Read the relocations. */
6660 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6661 info->keep_memory);
6662 if (relstart == NULL)
6663 return FALSE;
6664
6665 /* First run through the relocs to check they are sane, and to
6666 determine whether we need to edit this opd section. */
6667 need_edit = FALSE;
6668 broken = FALSE;
6669 need_pad = sec;
6670 relend = relstart + sec->reloc_count;
6671 for (rel = relstart; rel < relend; )
6672 {
6673 enum elf_ppc64_reloc_type r_type;
6674 unsigned long r_symndx;
6675 asection *sym_sec;
6676 struct elf_link_hash_entry *h;
6677 Elf_Internal_Sym *sym;
6678 bfd_vma offset;
6679
6680 /* .opd contains an array of 16 or 24 byte entries. We're
6681 only interested in the reloc pointing to a function entry
6682 point. */
6683 offset = rel->r_offset;
6684 if (rel + 1 == relend
6685 || rel[1].r_offset != offset + 8)
6686 {
6687 /* If someone messes with .opd alignment then after a
6688 "ld -r" we might have padding in the middle of .opd.
6689 Also, there's nothing to prevent someone putting
6690 something silly in .opd with the assembler. No .opd
6691 optimization for them! */
6692 broken_opd:
6693 _bfd_error_handler
6694 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
6695 broken = TRUE;
6696 break;
6697 }
6698
6699 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6700 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6701 {
6702 _bfd_error_handler
6703 /* xgettext:c-format */
6704 (_("%pB: unexpected reloc type %u in .opd section"),
6705 ibfd, r_type);
6706 broken = TRUE;
6707 break;
6708 }
6709
6710 r_symndx = ELF64_R_SYM (rel->r_info);
6711 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6712 r_symndx, ibfd))
6713 goto error_ret;
6714
6715 if (sym_sec == NULL || sym_sec->owner == NULL)
6716 {
6717 const char *sym_name;
6718 if (h != NULL)
6719 sym_name = h->root.root.string;
6720 else
6721 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6722 sym_sec);
6723
6724 _bfd_error_handler
6725 /* xgettext:c-format */
6726 (_("%pB: undefined sym `%s' in .opd section"),
6727 ibfd, sym_name);
6728 broken = TRUE;
6729 break;
6730 }
6731
6732 /* opd entries are always for functions defined in the
6733 current input bfd. If the symbol isn't defined in the
6734 input bfd, then we won't be using the function in this
6735 bfd; It must be defined in a linkonce section in another
6736 bfd, or is weak. It's also possible that we are
6737 discarding the function due to a linker script /DISCARD/,
6738 which we test for via the output_section. */
6739 if (sym_sec->owner != ibfd
6740 || sym_sec->output_section == bfd_abs_section_ptr)
6741 need_edit = TRUE;
6742
6743 rel += 2;
6744 if (rel + 1 == relend
6745 || (rel + 2 < relend
6746 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
6747 ++rel;
6748
6749 if (rel == relend)
6750 {
6751 if (sec->size == offset + 24)
6752 {
6753 need_pad = NULL;
6754 break;
6755 }
6756 if (sec->size == offset + 16)
6757 {
6758 cnt_16b++;
6759 break;
6760 }
6761 goto broken_opd;
6762 }
6763 else if (rel + 1 < relend
6764 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6765 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6766 {
6767 if (rel[0].r_offset == offset + 16)
6768 cnt_16b++;
6769 else if (rel[0].r_offset != offset + 24)
6770 goto broken_opd;
6771 }
6772 else
6773 goto broken_opd;
6774 }
6775
6776 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
6777
6778 if (!broken && (need_edit || add_aux_fields))
6779 {
6780 Elf_Internal_Rela *write_rel;
6781 Elf_Internal_Shdr *rel_hdr;
6782 bfd_byte *rptr, *wptr;
6783 bfd_byte *new_contents;
6784 bfd_size_type amt;
6785
6786 new_contents = NULL;
6787 amt = OPD_NDX (sec->size) * sizeof (long);
6788 opd = &ppc64_elf_section_data (sec)->u.opd;
6789 opd->adjust = bfd_zalloc (sec->owner, amt);
6790 if (opd->adjust == NULL)
6791 return FALSE;
6792
6793 /* This seems a waste of time as input .opd sections are all
6794 zeros as generated by gcc, but I suppose there's no reason
6795 this will always be so. We might start putting something in
6796 the third word of .opd entries. */
6797 if ((sec->flags & SEC_IN_MEMORY) == 0)
6798 {
6799 bfd_byte *loc;
6800 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6801 {
6802 if (loc != NULL)
6803 free (loc);
6804 error_ret:
6805 if (local_syms != NULL
6806 && symtab_hdr->contents != (unsigned char *) local_syms)
6807 free (local_syms);
6808 if (elf_section_data (sec)->relocs != relstart)
6809 free (relstart);
6810 return FALSE;
6811 }
6812 sec->contents = loc;
6813 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6814 }
6815
6816 elf_section_data (sec)->relocs = relstart;
6817
6818 new_contents = sec->contents;
6819 if (add_aux_fields)
6820 {
6821 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6822 if (new_contents == NULL)
6823 return FALSE;
6824 need_pad = NULL;
6825 }
6826 wptr = new_contents;
6827 rptr = sec->contents;
6828 write_rel = relstart;
6829 for (rel = relstart; rel < relend; )
6830 {
6831 unsigned long r_symndx;
6832 asection *sym_sec;
6833 struct elf_link_hash_entry *h;
6834 struct ppc_link_hash_entry *fdh = NULL;
6835 Elf_Internal_Sym *sym;
6836 long opd_ent_size;
6837 Elf_Internal_Rela *next_rel;
6838 bfd_boolean skip;
6839
6840 r_symndx = ELF64_R_SYM (rel->r_info);
6841 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6842 r_symndx, ibfd))
6843 goto error_ret;
6844
6845 next_rel = rel + 2;
6846 if (next_rel + 1 == relend
6847 || (next_rel + 2 < relend
6848 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
6849 ++next_rel;
6850
6851 /* See if the .opd entry is full 24 byte or
6852 16 byte (with fd_aux entry overlapped with next
6853 fd_func). */
6854 opd_ent_size = 24;
6855 if (next_rel == relend)
6856 {
6857 if (sec->size == rel->r_offset + 16)
6858 opd_ent_size = 16;
6859 }
6860 else if (next_rel->r_offset == rel->r_offset + 16)
6861 opd_ent_size = 16;
6862
6863 if (h != NULL
6864 && h->root.root.string[0] == '.')
6865 {
6866 fdh = ((struct ppc_link_hash_entry *) h)->oh;
6867 if (fdh != NULL)
6868 {
6869 fdh = ppc_follow_link (fdh);
6870 if (fdh->elf.root.type != bfd_link_hash_defined
6871 && fdh->elf.root.type != bfd_link_hash_defweak)
6872 fdh = NULL;
6873 }
6874 }
6875
6876 skip = (sym_sec->owner != ibfd
6877 || sym_sec->output_section == bfd_abs_section_ptr);
6878 if (skip)
6879 {
6880 if (fdh != NULL && sym_sec->owner == ibfd)
6881 {
6882 /* Arrange for the function descriptor sym
6883 to be dropped. */
6884 fdh->elf.root.u.def.value = 0;
6885 fdh->elf.root.u.def.section = sym_sec;
6886 }
6887 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
6888
6889 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
6890 rel = next_rel;
6891 else
6892 while (1)
6893 {
6894 if (!dec_dynrel_count (rel->r_info, sec, info,
6895 NULL, h, sym))
6896 goto error_ret;
6897
6898 if (++rel == next_rel)
6899 break;
6900
6901 r_symndx = ELF64_R_SYM (rel->r_info);
6902 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6903 r_symndx, ibfd))
6904 goto error_ret;
6905 }
6906 }
6907 else
6908 {
6909 /* We'll be keeping this opd entry. */
6910 long adjust;
6911
6912 if (fdh != NULL)
6913 {
6914 /* Redefine the function descriptor symbol to
6915 this location in the opd section. It is
6916 necessary to update the value here rather
6917 than using an array of adjustments as we do
6918 for local symbols, because various places
6919 in the generic ELF code use the value
6920 stored in u.def.value. */
6921 fdh->elf.root.u.def.value = wptr - new_contents;
6922 fdh->adjust_done = 1;
6923 }
6924
6925 /* Local syms are a bit tricky. We could
6926 tweak them as they can be cached, but
6927 we'd need to look through the local syms
6928 for the function descriptor sym which we
6929 don't have at the moment. So keep an
6930 array of adjustments. */
6931 adjust = (wptr - new_contents) - (rptr - sec->contents);
6932 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
6933
6934 if (wptr != rptr)
6935 memcpy (wptr, rptr, opd_ent_size);
6936 wptr += opd_ent_size;
6937 if (add_aux_fields && opd_ent_size == 16)
6938 {
6939 memset (wptr, '\0', 8);
6940 wptr += 8;
6941 }
6942
6943 /* We need to adjust any reloc offsets to point to the
6944 new opd entries. */
6945 for ( ; rel != next_rel; ++rel)
6946 {
6947 rel->r_offset += adjust;
6948 if (write_rel != rel)
6949 memcpy (write_rel, rel, sizeof (*rel));
6950 ++write_rel;
6951 }
6952 }
6953
6954 rptr += opd_ent_size;
6955 }
6956
6957 sec->size = wptr - new_contents;
6958 sec->reloc_count = write_rel - relstart;
6959 if (add_aux_fields)
6960 {
6961 free (sec->contents);
6962 sec->contents = new_contents;
6963 }
6964
6965 /* Fudge the header size too, as this is used later in
6966 elf_bfd_final_link if we are emitting relocs. */
6967 rel_hdr = _bfd_elf_single_rel_hdr (sec);
6968 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
6969 some_edited = TRUE;
6970 }
6971 else if (elf_section_data (sec)->relocs != relstart)
6972 free (relstart);
6973
6974 if (local_syms != NULL
6975 && symtab_hdr->contents != (unsigned char *) local_syms)
6976 {
6977 if (!info->keep_memory)
6978 free (local_syms);
6979 else
6980 symtab_hdr->contents = (unsigned char *) local_syms;
6981 }
6982 }
6983
6984 if (some_edited)
6985 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
6986
6987 /* If we are doing a final link and the last .opd entry is just 16 byte
6988 long, add a 8 byte padding after it. */
6989 if (need_pad != NULL && !bfd_link_relocatable (info))
6990 {
6991 bfd_byte *p;
6992
6993 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
6994 {
6995 BFD_ASSERT (need_pad->size > 0);
6996
6997 p = bfd_malloc (need_pad->size + 8);
6998 if (p == NULL)
6999 return FALSE;
7000
7001 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7002 p, 0, need_pad->size))
7003 return FALSE;
7004
7005 need_pad->contents = p;
7006 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7007 }
7008 else
7009 {
7010 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7011 if (p == NULL)
7012 return FALSE;
7013
7014 need_pad->contents = p;
7015 }
7016
7017 memset (need_pad->contents + need_pad->size, 0, 8);
7018 need_pad->size += 8;
7019 }
7020
7021 return TRUE;
7022 }
7023
7024 /* Analyze inline PLT call relocations to see whether calls to locally
7025 defined functions can be converted to direct calls. */
7026
7027 bfd_boolean
7028 ppc64_elf_inline_plt (struct bfd_link_info *info)
7029 {
7030 struct ppc_link_hash_table *htab;
7031 bfd *ibfd;
7032 asection *sec;
7033 bfd_vma low_vma, high_vma, limit;
7034
7035 htab = ppc_hash_table (info);
7036 if (htab == NULL)
7037 return FALSE;
7038
7039 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7040 reduced somewhat to cater for possible stubs that might be added
7041 between the call and its destination. */
7042 if (htab->params->group_size < 0)
7043 {
7044 limit = -htab->params->group_size;
7045 if (limit == 1)
7046 limit = 0x1e00000;
7047 }
7048 else
7049 {
7050 limit = htab->params->group_size;
7051 if (limit == 1)
7052 limit = 0x1c00000;
7053 }
7054
7055 low_vma = -1;
7056 high_vma = 0;
7057 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7058 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7059 {
7060 if (low_vma > sec->vma)
7061 low_vma = sec->vma;
7062 if (high_vma < sec->vma + sec->size)
7063 high_vma = sec->vma + sec->size;
7064 }
7065
7066 /* If a "bl" can reach anywhere in local code sections, then we can
7067 convert all inline PLT sequences to direct calls when the symbol
7068 is local. */
7069 if (high_vma - low_vma < limit)
7070 {
7071 htab->can_convert_all_inline_plt = 1;
7072 return TRUE;
7073 }
7074
7075 /* Otherwise, go looking through relocs for cases where a direct
7076 call won't reach. Mark the symbol on any such reloc to disable
7077 the optimization and keep the PLT entry as it seems likely that
7078 this will be better than creating trampolines. Note that this
7079 will disable the optimization for all inline PLT calls to a
7080 particular symbol, not just those that won't reach. The
7081 difficulty in doing a more precise optimization is that the
7082 linker needs to make a decision depending on whether a
7083 particular R_PPC64_PLTCALL insn can be turned into a direct
7084 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7085 the sequence, and there is nothing that ties those relocs
7086 together except their symbol. */
7087
7088 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7089 {
7090 Elf_Internal_Shdr *symtab_hdr;
7091 Elf_Internal_Sym *local_syms;
7092
7093 if (!is_ppc64_elf (ibfd))
7094 continue;
7095
7096 local_syms = NULL;
7097 symtab_hdr = &elf_symtab_hdr (ibfd);
7098
7099 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7100 if (ppc64_elf_section_data (sec)->has_pltcall
7101 && !bfd_is_abs_section (sec->output_section))
7102 {
7103 Elf_Internal_Rela *relstart, *rel, *relend;
7104
7105 /* Read the relocations. */
7106 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7107 info->keep_memory);
7108 if (relstart == NULL)
7109 return FALSE;
7110
7111 relend = relstart + sec->reloc_count;
7112 for (rel = relstart; rel < relend; )
7113 {
7114 enum elf_ppc64_reloc_type r_type;
7115 unsigned long r_symndx;
7116 asection *sym_sec;
7117 struct elf_link_hash_entry *h;
7118 Elf_Internal_Sym *sym;
7119 unsigned char *tls_maskp;
7120
7121 r_type = ELF64_R_TYPE (rel->r_info);
7122 if (r_type != R_PPC64_PLTCALL)
7123 continue;
7124
7125 r_symndx = ELF64_R_SYM (rel->r_info);
7126 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7127 r_symndx, ibfd))
7128 {
7129 if (elf_section_data (sec)->relocs != relstart)
7130 free (relstart);
7131 if (local_syms != NULL
7132 && symtab_hdr->contents != (bfd_byte *) local_syms)
7133 free (local_syms);
7134 return FALSE;
7135 }
7136
7137 if (sym_sec != NULL && sym_sec->output_section != NULL)
7138 {
7139 bfd_vma from, to;
7140 if (h != NULL)
7141 to = h->root.u.def.value;
7142 else
7143 to = sym->st_value;
7144 to += (rel->r_addend
7145 + sym_sec->output_offset
7146 + sym_sec->output_section->vma);
7147 from = (rel->r_offset
7148 + sec->output_offset
7149 + sec->output_section->vma);
7150 if (to - from + limit < 2 * limit)
7151 *tls_maskp &= ~PLT_KEEP;
7152 }
7153 }
7154 if (elf_section_data (sec)->relocs != relstart)
7155 free (relstart);
7156 }
7157
7158 if (local_syms != NULL
7159 && symtab_hdr->contents != (unsigned char *) local_syms)
7160 {
7161 if (!info->keep_memory)
7162 free (local_syms);
7163 else
7164 symtab_hdr->contents = (unsigned char *) local_syms;
7165 }
7166 }
7167
7168 return TRUE;
7169 }
7170
7171 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7172
7173 asection *
7174 ppc64_elf_tls_setup (struct bfd_link_info *info)
7175 {
7176 struct ppc_link_hash_table *htab;
7177
7178 htab = ppc_hash_table (info);
7179 if (htab == NULL)
7180 return NULL;
7181
7182 if (abiversion (info->output_bfd) == 1)
7183 htab->opd_abi = 1;
7184
7185 if (htab->params->no_multi_toc)
7186 htab->do_multi_toc = 0;
7187 else if (!htab->do_multi_toc)
7188 htab->params->no_multi_toc = 1;
7189
7190 /* Default to --no-plt-localentry, as this option can cause problems
7191 with symbol interposition. For example, glibc libpthread.so and
7192 libc.so duplicate many pthread symbols, with a fallback
7193 implementation in libc.so. In some cases the fallback does more
7194 work than the pthread implementation. __pthread_condattr_destroy
7195 is one such symbol: the libpthread.so implementation is
7196 localentry:0 while the libc.so implementation is localentry:8.
7197 An app that "cleverly" uses dlopen to only load necessary
7198 libraries at runtime may omit loading libpthread.so when not
7199 running multi-threaded, which then results in the libc.so
7200 fallback symbols being used and ld.so complaining. Now there
7201 are workarounds in ld (see non_zero_localentry) to detect the
7202 pthread situation, but that may not be the only case where
7203 --plt-localentry can cause trouble. */
7204 if (htab->params->plt_localentry0 < 0)
7205 htab->params->plt_localentry0 = 0;
7206 if (htab->params->plt_localentry0
7207 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7208 FALSE, FALSE, FALSE) == NULL)
7209 _bfd_error_handler
7210 (_("warning: --plt-localentry is especially dangerous without "
7211 "ld.so support to detect ABI violations"));
7212
7213 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7214 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7215 FALSE, FALSE, TRUE));
7216 /* Move dynamic linking info to the function descriptor sym. */
7217 if (htab->tls_get_addr != NULL)
7218 func_desc_adjust (&htab->tls_get_addr->elf, info);
7219 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7220 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7221 FALSE, FALSE, TRUE));
7222 if (htab->params->tls_get_addr_opt)
7223 {
7224 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7225
7226 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7227 FALSE, FALSE, TRUE);
7228 if (opt != NULL)
7229 func_desc_adjust (opt, info);
7230 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7231 FALSE, FALSE, TRUE);
7232 if (opt_fd != NULL
7233 && (opt_fd->root.type == bfd_link_hash_defined
7234 || opt_fd->root.type == bfd_link_hash_defweak))
7235 {
7236 /* If glibc supports an optimized __tls_get_addr call stub,
7237 signalled by the presence of __tls_get_addr_opt, and we'll
7238 be calling __tls_get_addr via a plt call stub, then
7239 make __tls_get_addr point to __tls_get_addr_opt. */
7240 tga_fd = &htab->tls_get_addr_fd->elf;
7241 if (htab->elf.dynamic_sections_created
7242 && tga_fd != NULL
7243 && (tga_fd->type == STT_FUNC
7244 || tga_fd->needs_plt)
7245 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7246 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
7247 {
7248 struct plt_entry *ent;
7249
7250 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7251 if (ent->plt.refcount > 0)
7252 break;
7253 if (ent != NULL)
7254 {
7255 tga_fd->root.type = bfd_link_hash_indirect;
7256 tga_fd->root.u.i.link = &opt_fd->root;
7257 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7258 opt_fd->mark = 1;
7259 if (opt_fd->dynindx != -1)
7260 {
7261 /* Use __tls_get_addr_opt in dynamic relocations. */
7262 opt_fd->dynindx = -1;
7263 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7264 opt_fd->dynstr_index);
7265 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7266 return NULL;
7267 }
7268 htab->tls_get_addr_fd
7269 = (struct ppc_link_hash_entry *) opt_fd;
7270 tga = &htab->tls_get_addr->elf;
7271 if (opt != NULL && tga != NULL)
7272 {
7273 tga->root.type = bfd_link_hash_indirect;
7274 tga->root.u.i.link = &opt->root;
7275 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7276 opt->mark = 1;
7277 _bfd_elf_link_hash_hide_symbol (info, opt,
7278 tga->forced_local);
7279 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7280 }
7281 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7282 htab->tls_get_addr_fd->is_func_descriptor = 1;
7283 if (htab->tls_get_addr != NULL)
7284 {
7285 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7286 htab->tls_get_addr->is_func = 1;
7287 }
7288 }
7289 }
7290 }
7291 else if (htab->params->tls_get_addr_opt < 0)
7292 htab->params->tls_get_addr_opt = 0;
7293 }
7294 return _bfd_elf_tls_setup (info->output_bfd, info);
7295 }
7296
7297 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7298 HASH1 or HASH2. */
7299
7300 static bfd_boolean
7301 branch_reloc_hash_match (const bfd *ibfd,
7302 const Elf_Internal_Rela *rel,
7303 const struct ppc_link_hash_entry *hash1,
7304 const struct ppc_link_hash_entry *hash2)
7305 {
7306 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7307 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7308 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7309
7310 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7311 {
7312 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7313 struct elf_link_hash_entry *h;
7314
7315 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7316 h = elf_follow_link (h);
7317 if (h == &hash1->elf || h == &hash2->elf)
7318 return TRUE;
7319 }
7320 return FALSE;
7321 }
7322
7323 /* Run through all the TLS relocs looking for optimization
7324 opportunities. The linker has been hacked (see ppc64elf.em) to do
7325 a preliminary section layout so that we know the TLS segment
7326 offsets. We can't optimize earlier because some optimizations need
7327 to know the tp offset, and we need to optimize before allocating
7328 dynamic relocations. */
7329
7330 bfd_boolean
7331 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7332 {
7333 bfd *ibfd;
7334 asection *sec;
7335 struct ppc_link_hash_table *htab;
7336 unsigned char *toc_ref;
7337 int pass;
7338
7339 if (!bfd_link_executable (info))
7340 return TRUE;
7341
7342 htab = ppc_hash_table (info);
7343 if (htab == NULL)
7344 return FALSE;
7345
7346 /* Make two passes over the relocs. On the first pass, mark toc
7347 entries involved with tls relocs, and check that tls relocs
7348 involved in setting up a tls_get_addr call are indeed followed by
7349 such a call. If they are not, we can't do any tls optimization.
7350 On the second pass twiddle tls_mask flags to notify
7351 relocate_section that optimization can be done, and adjust got
7352 and plt refcounts. */
7353 toc_ref = NULL;
7354 for (pass = 0; pass < 2; ++pass)
7355 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7356 {
7357 Elf_Internal_Sym *locsyms = NULL;
7358 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7359
7360 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7361 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7362 {
7363 Elf_Internal_Rela *relstart, *rel, *relend;
7364 bfd_boolean found_tls_get_addr_arg = 0;
7365
7366 /* Read the relocations. */
7367 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7368 info->keep_memory);
7369 if (relstart == NULL)
7370 {
7371 free (toc_ref);
7372 return FALSE;
7373 }
7374
7375 relend = relstart + sec->reloc_count;
7376 for (rel = relstart; rel < relend; rel++)
7377 {
7378 enum elf_ppc64_reloc_type r_type;
7379 unsigned long r_symndx;
7380 struct elf_link_hash_entry *h;
7381 Elf_Internal_Sym *sym;
7382 asection *sym_sec;
7383 unsigned char *tls_mask;
7384 unsigned char tls_set, tls_clear, tls_type = 0;
7385 bfd_vma value;
7386 bfd_boolean ok_tprel, is_local;
7387 long toc_ref_index = 0;
7388 int expecting_tls_get_addr = 0;
7389 bfd_boolean ret = FALSE;
7390
7391 r_symndx = ELF64_R_SYM (rel->r_info);
7392 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7393 r_symndx, ibfd))
7394 {
7395 err_free_rel:
7396 if (elf_section_data (sec)->relocs != relstart)
7397 free (relstart);
7398 if (toc_ref != NULL)
7399 free (toc_ref);
7400 if (locsyms != NULL
7401 && (elf_symtab_hdr (ibfd).contents
7402 != (unsigned char *) locsyms))
7403 free (locsyms);
7404 return ret;
7405 }
7406
7407 if (h != NULL)
7408 {
7409 if (h->root.type == bfd_link_hash_defined
7410 || h->root.type == bfd_link_hash_defweak)
7411 value = h->root.u.def.value;
7412 else if (h->root.type == bfd_link_hash_undefweak)
7413 value = 0;
7414 else
7415 {
7416 found_tls_get_addr_arg = 0;
7417 continue;
7418 }
7419 }
7420 else
7421 /* Symbols referenced by TLS relocs must be of type
7422 STT_TLS. So no need for .opd local sym adjust. */
7423 value = sym->st_value;
7424
7425 ok_tprel = FALSE;
7426 is_local = FALSE;
7427 if (h == NULL
7428 || !h->def_dynamic)
7429 {
7430 is_local = TRUE;
7431 if (h != NULL
7432 && h->root.type == bfd_link_hash_undefweak)
7433 ok_tprel = TRUE;
7434 else if (sym_sec != NULL
7435 && sym_sec->output_section != NULL)
7436 {
7437 value += sym_sec->output_offset;
7438 value += sym_sec->output_section->vma;
7439 value -= htab->elf.tls_sec->vma;
7440 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7441 < (bfd_vma) 1 << 32);
7442 }
7443 }
7444
7445 r_type = ELF64_R_TYPE (rel->r_info);
7446 /* If this section has old-style __tls_get_addr calls
7447 without marker relocs, then check that each
7448 __tls_get_addr call reloc is preceded by a reloc
7449 that conceivably belongs to the __tls_get_addr arg
7450 setup insn. If we don't find matching arg setup
7451 relocs, don't do any tls optimization. */
7452 if (pass == 0
7453 && sec->has_tls_get_addr_call
7454 && h != NULL
7455 && (h == &htab->tls_get_addr->elf
7456 || h == &htab->tls_get_addr_fd->elf)
7457 && !found_tls_get_addr_arg
7458 && is_branch_reloc (r_type))
7459 {
7460 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7461 "TLS optimization disabled\n"),
7462 ibfd, sec, rel->r_offset);
7463 ret = TRUE;
7464 goto err_free_rel;
7465 }
7466
7467 found_tls_get_addr_arg = 0;
7468 switch (r_type)
7469 {
7470 case R_PPC64_GOT_TLSLD16:
7471 case R_PPC64_GOT_TLSLD16_LO:
7472 expecting_tls_get_addr = 1;
7473 found_tls_get_addr_arg = 1;
7474 /* Fall through. */
7475
7476 case R_PPC64_GOT_TLSLD16_HI:
7477 case R_PPC64_GOT_TLSLD16_HA:
7478 /* These relocs should never be against a symbol
7479 defined in a shared lib. Leave them alone if
7480 that turns out to be the case. */
7481 if (!is_local)
7482 continue;
7483
7484 /* LD -> LE */
7485 tls_set = 0;
7486 tls_clear = TLS_LD;
7487 tls_type = TLS_TLS | TLS_LD;
7488 break;
7489
7490 case R_PPC64_GOT_TLSGD16:
7491 case R_PPC64_GOT_TLSGD16_LO:
7492 expecting_tls_get_addr = 1;
7493 found_tls_get_addr_arg = 1;
7494 /* Fall through. */
7495
7496 case R_PPC64_GOT_TLSGD16_HI:
7497 case R_PPC64_GOT_TLSGD16_HA:
7498 if (ok_tprel)
7499 /* GD -> LE */
7500 tls_set = 0;
7501 else
7502 /* GD -> IE */
7503 tls_set = TLS_TLS | TLS_TPRELGD;
7504 tls_clear = TLS_GD;
7505 tls_type = TLS_TLS | TLS_GD;
7506 break;
7507
7508 case R_PPC64_GOT_TPREL16_DS:
7509 case R_PPC64_GOT_TPREL16_LO_DS:
7510 case R_PPC64_GOT_TPREL16_HI:
7511 case R_PPC64_GOT_TPREL16_HA:
7512 if (ok_tprel)
7513 {
7514 /* IE -> LE */
7515 tls_set = 0;
7516 tls_clear = TLS_TPREL;
7517 tls_type = TLS_TLS | TLS_TPREL;
7518 break;
7519 }
7520 continue;
7521
7522 case R_PPC64_TLSGD:
7523 case R_PPC64_TLSLD:
7524 if (rel + 1 < relend
7525 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7526 {
7527 if (pass != 0
7528 && (ELF64_R_TYPE (rel[1].r_info)
7529 != R_PPC64_PLTSEQ))
7530 {
7531 r_symndx = ELF64_R_SYM (rel[1].r_info);
7532 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
7533 r_symndx, ibfd))
7534 goto err_free_rel;
7535 if (h != NULL)
7536 {
7537 struct plt_entry *ent = NULL;
7538
7539 for (ent = h->plt.plist;
7540 ent != NULL;
7541 ent = ent->next)
7542 if (ent->addend == rel[1].r_addend)
7543 break;
7544
7545 if (ent != NULL
7546 && ent->plt.refcount > 0)
7547 ent->plt.refcount -= 1;
7548 }
7549 }
7550 continue;
7551 }
7552 found_tls_get_addr_arg = 1;
7553 /* Fall through. */
7554
7555 case R_PPC64_TLS:
7556 case R_PPC64_TOC16:
7557 case R_PPC64_TOC16_LO:
7558 if (sym_sec == NULL || sym_sec != toc)
7559 continue;
7560
7561 /* Mark this toc entry as referenced by a TLS
7562 code sequence. We can do that now in the
7563 case of R_PPC64_TLS, and after checking for
7564 tls_get_addr for the TOC16 relocs. */
7565 if (toc_ref == NULL)
7566 toc_ref
7567 = bfd_zmalloc (toc->output_section->rawsize / 8);
7568 if (toc_ref == NULL)
7569 goto err_free_rel;
7570
7571 if (h != NULL)
7572 value = h->root.u.def.value;
7573 else
7574 value = sym->st_value;
7575 value += rel->r_addend;
7576 if (value % 8 != 0)
7577 continue;
7578 BFD_ASSERT (value < toc->size
7579 && toc->output_offset % 8 == 0);
7580 toc_ref_index = (value + toc->output_offset) / 8;
7581 if (r_type == R_PPC64_TLS
7582 || r_type == R_PPC64_TLSGD
7583 || r_type == R_PPC64_TLSLD)
7584 {
7585 toc_ref[toc_ref_index] = 1;
7586 continue;
7587 }
7588
7589 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7590 continue;
7591
7592 tls_set = 0;
7593 tls_clear = 0;
7594 expecting_tls_get_addr = 2;
7595 break;
7596
7597 case R_PPC64_TPREL64:
7598 if (pass == 0
7599 || sec != toc
7600 || toc_ref == NULL
7601 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7602 continue;
7603 if (ok_tprel)
7604 {
7605 /* IE -> LE */
7606 tls_set = TLS_EXPLICIT;
7607 tls_clear = TLS_TPREL;
7608 break;
7609 }
7610 continue;
7611
7612 case R_PPC64_DTPMOD64:
7613 if (pass == 0
7614 || sec != toc
7615 || toc_ref == NULL
7616 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7617 continue;
7618 if (rel + 1 < relend
7619 && (rel[1].r_info
7620 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7621 && rel[1].r_offset == rel->r_offset + 8)
7622 {
7623 if (ok_tprel)
7624 /* GD -> LE */
7625 tls_set = TLS_EXPLICIT | TLS_GD;
7626 else
7627 /* GD -> IE */
7628 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7629 tls_clear = TLS_GD;
7630 }
7631 else
7632 {
7633 if (!is_local)
7634 continue;
7635
7636 /* LD -> LE */
7637 tls_set = TLS_EXPLICIT;
7638 tls_clear = TLS_LD;
7639 }
7640 break;
7641
7642 default:
7643 continue;
7644 }
7645
7646 if (pass == 0)
7647 {
7648 if (!expecting_tls_get_addr
7649 || !sec->has_tls_get_addr_call)
7650 continue;
7651
7652 if (rel + 1 < relend
7653 && branch_reloc_hash_match (ibfd, rel + 1,
7654 htab->tls_get_addr,
7655 htab->tls_get_addr_fd))
7656 {
7657 if (expecting_tls_get_addr == 2)
7658 {
7659 /* Check for toc tls entries. */
7660 unsigned char *toc_tls;
7661 int retval;
7662
7663 retval = get_tls_mask (&toc_tls, NULL, NULL,
7664 &locsyms,
7665 rel, ibfd);
7666 if (retval == 0)
7667 goto err_free_rel;
7668 if (toc_tls != NULL)
7669 {
7670 if ((*toc_tls & TLS_TLS) != 0
7671 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
7672 found_tls_get_addr_arg = 1;
7673 if (retval > 1)
7674 toc_ref[toc_ref_index] = 1;
7675 }
7676 }
7677 continue;
7678 }
7679
7680 /* Uh oh, we didn't find the expected call. We
7681 could just mark this symbol to exclude it
7682 from tls optimization but it's safer to skip
7683 the entire optimization. */
7684 /* xgettext:c-format */
7685 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
7686 "TLS optimization disabled\n"),
7687 ibfd, sec, rel->r_offset);
7688 ret = TRUE;
7689 goto err_free_rel;
7690 }
7691
7692 /* If we don't have old-style __tls_get_addr calls
7693 without TLSGD/TLSLD marker relocs, and we haven't
7694 found a new-style __tls_get_addr call with a
7695 marker for this symbol, then we either have a
7696 broken object file or an -mlongcall style
7697 indirect call to __tls_get_addr without a marker.
7698 Disable optimization in this case. */
7699 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
7700 && (tls_set & TLS_EXPLICIT) == 0
7701 && !sec->has_tls_get_addr_call
7702 && ((*tls_mask & (TLS_TLS | TLS_MARK))
7703 != (TLS_TLS | TLS_MARK)))
7704 continue;
7705
7706 if (expecting_tls_get_addr)
7707 {
7708 struct plt_entry *ent = NULL;
7709
7710 if (htab->tls_get_addr != NULL)
7711 for (ent = htab->tls_get_addr->elf.plt.plist;
7712 ent != NULL;
7713 ent = ent->next)
7714 if (ent->addend == 0)
7715 break;
7716
7717 if (ent == NULL && htab->tls_get_addr_fd != NULL)
7718 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7719 ent != NULL;
7720 ent = ent->next)
7721 if (ent->addend == 0)
7722 break;
7723
7724 if (ent != NULL
7725 && ent->plt.refcount > 0)
7726 ent->plt.refcount -= 1;
7727 }
7728
7729 if (tls_clear == 0)
7730 continue;
7731
7732 if ((tls_set & TLS_EXPLICIT) == 0)
7733 {
7734 struct got_entry *ent;
7735
7736 /* Adjust got entry for this reloc. */
7737 if (h != NULL)
7738 ent = h->got.glist;
7739 else
7740 ent = elf_local_got_ents (ibfd)[r_symndx];
7741
7742 for (; ent != NULL; ent = ent->next)
7743 if (ent->addend == rel->r_addend
7744 && ent->owner == ibfd
7745 && ent->tls_type == tls_type)
7746 break;
7747 if (ent == NULL)
7748 abort ();
7749
7750 if (tls_set == 0)
7751 {
7752 /* We managed to get rid of a got entry. */
7753 if (ent->got.refcount > 0)
7754 ent->got.refcount -= 1;
7755 }
7756 }
7757 else
7758 {
7759 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7760 we'll lose one or two dyn relocs. */
7761 if (!dec_dynrel_count (rel->r_info, sec, info,
7762 NULL, h, sym))
7763 return FALSE;
7764
7765 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7766 {
7767 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7768 NULL, h, sym))
7769 return FALSE;
7770 }
7771 }
7772
7773 *tls_mask |= tls_set;
7774 *tls_mask &= ~tls_clear;
7775 }
7776
7777 if (elf_section_data (sec)->relocs != relstart)
7778 free (relstart);
7779 }
7780
7781 if (locsyms != NULL
7782 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7783 {
7784 if (!info->keep_memory)
7785 free (locsyms);
7786 else
7787 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7788 }
7789 }
7790
7791 if (toc_ref != NULL)
7792 free (toc_ref);
7793 htab->do_tls_opt = 1;
7794 return TRUE;
7795 }
7796
7797 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7798 the values of any global symbols in a toc section that has been
7799 edited. Globals in toc sections should be a rarity, so this function
7800 sets a flag if any are found in toc sections other than the one just
7801 edited, so that further hash table traversals can be avoided. */
7802
7803 struct adjust_toc_info
7804 {
7805 asection *toc;
7806 unsigned long *skip;
7807 bfd_boolean global_toc_syms;
7808 };
7809
7810 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7811
7812 static bfd_boolean
7813 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7814 {
7815 struct ppc_link_hash_entry *eh;
7816 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7817 unsigned long i;
7818
7819 if (h->root.type != bfd_link_hash_defined
7820 && h->root.type != bfd_link_hash_defweak)
7821 return TRUE;
7822
7823 eh = (struct ppc_link_hash_entry *) h;
7824 if (eh->adjust_done)
7825 return TRUE;
7826
7827 if (eh->elf.root.u.def.section == toc_inf->toc)
7828 {
7829 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7830 i = toc_inf->toc->rawsize >> 3;
7831 else
7832 i = eh->elf.root.u.def.value >> 3;
7833
7834 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
7835 {
7836 _bfd_error_handler
7837 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7838 do
7839 ++i;
7840 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
7841 eh->elf.root.u.def.value = (bfd_vma) i << 3;
7842 }
7843
7844 eh->elf.root.u.def.value -= toc_inf->skip[i];
7845 eh->adjust_done = 1;
7846 }
7847 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7848 toc_inf->global_toc_syms = TRUE;
7849
7850 return TRUE;
7851 }
7852
7853 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
7854 on a _LO variety toc/got reloc. */
7855
7856 static bfd_boolean
7857 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
7858 {
7859 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7860 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
7861 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7862 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7863 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7864 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7865 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7866 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7867 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7868 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7869 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7870 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7871 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7872 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7873 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7874 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7875 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7876 /* Exclude lfqu by testing reloc. If relocs are ever
7877 defined for the reduced D field in psq_lu then those
7878 will need testing too. */
7879 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7880 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7881 && (insn & 1) == 0)
7882 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7883 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7884 /* Exclude stfqu. psq_stu as above for psq_lu. */
7885 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7886 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7887 && (insn & 1) == 0));
7888 }
7889
7890 /* Examine all relocs referencing .toc sections in order to remove
7891 unused .toc entries. */
7892
7893 bfd_boolean
7894 ppc64_elf_edit_toc (struct bfd_link_info *info)
7895 {
7896 bfd *ibfd;
7897 struct adjust_toc_info toc_inf;
7898 struct ppc_link_hash_table *htab = ppc_hash_table (info);
7899
7900 htab->do_toc_opt = 1;
7901 toc_inf.global_toc_syms = TRUE;
7902 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7903 {
7904 asection *toc, *sec;
7905 Elf_Internal_Shdr *symtab_hdr;
7906 Elf_Internal_Sym *local_syms;
7907 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
7908 unsigned long *skip, *drop;
7909 unsigned char *used;
7910 unsigned char *keep, last, some_unused;
7911
7912 if (!is_ppc64_elf (ibfd))
7913 continue;
7914
7915 toc = bfd_get_section_by_name (ibfd, ".toc");
7916 if (toc == NULL
7917 || toc->size == 0
7918 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
7919 || discarded_section (toc))
7920 continue;
7921
7922 toc_relocs = NULL;
7923 local_syms = NULL;
7924 symtab_hdr = &elf_symtab_hdr (ibfd);
7925
7926 /* Look at sections dropped from the final link. */
7927 skip = NULL;
7928 relstart = NULL;
7929 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7930 {
7931 if (sec->reloc_count == 0
7932 || !discarded_section (sec)
7933 || get_opd_info (sec)
7934 || (sec->flags & SEC_ALLOC) == 0
7935 || (sec->flags & SEC_DEBUGGING) != 0)
7936 continue;
7937
7938 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7939 if (relstart == NULL)
7940 goto error_ret;
7941
7942 /* Run through the relocs to see which toc entries might be
7943 unused. */
7944 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7945 {
7946 enum elf_ppc64_reloc_type r_type;
7947 unsigned long r_symndx;
7948 asection *sym_sec;
7949 struct elf_link_hash_entry *h;
7950 Elf_Internal_Sym *sym;
7951 bfd_vma val;
7952
7953 r_type = ELF64_R_TYPE (rel->r_info);
7954 switch (r_type)
7955 {
7956 default:
7957 continue;
7958
7959 case R_PPC64_TOC16:
7960 case R_PPC64_TOC16_LO:
7961 case R_PPC64_TOC16_HI:
7962 case R_PPC64_TOC16_HA:
7963 case R_PPC64_TOC16_DS:
7964 case R_PPC64_TOC16_LO_DS:
7965 break;
7966 }
7967
7968 r_symndx = ELF64_R_SYM (rel->r_info);
7969 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7970 r_symndx, ibfd))
7971 goto error_ret;
7972
7973 if (sym_sec != toc)
7974 continue;
7975
7976 if (h != NULL)
7977 val = h->root.u.def.value;
7978 else
7979 val = sym->st_value;
7980 val += rel->r_addend;
7981
7982 if (val >= toc->size)
7983 continue;
7984
7985 /* Anything in the toc ought to be aligned to 8 bytes.
7986 If not, don't mark as unused. */
7987 if (val & 7)
7988 continue;
7989
7990 if (skip == NULL)
7991 {
7992 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
7993 if (skip == NULL)
7994 goto error_ret;
7995 }
7996
7997 skip[val >> 3] = ref_from_discarded;
7998 }
7999
8000 if (elf_section_data (sec)->relocs != relstart)
8001 free (relstart);
8002 }
8003
8004 /* For largetoc loads of address constants, we can convert
8005 . addis rx,2,addr@got@ha
8006 . ld ry,addr@got@l(rx)
8007 to
8008 . addis rx,2,addr@toc@ha
8009 . addi ry,rx,addr@toc@l
8010 when addr is within 2G of the toc pointer. This then means
8011 that the word storing "addr" in the toc is no longer needed. */
8012
8013 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8014 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8015 && toc->reloc_count != 0)
8016 {
8017 /* Read toc relocs. */
8018 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8019 info->keep_memory);
8020 if (toc_relocs == NULL)
8021 goto error_ret;
8022
8023 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8024 {
8025 enum elf_ppc64_reloc_type r_type;
8026 unsigned long r_symndx;
8027 asection *sym_sec;
8028 struct elf_link_hash_entry *h;
8029 Elf_Internal_Sym *sym;
8030 bfd_vma val, addr;
8031
8032 r_type = ELF64_R_TYPE (rel->r_info);
8033 if (r_type != R_PPC64_ADDR64)
8034 continue;
8035
8036 r_symndx = ELF64_R_SYM (rel->r_info);
8037 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8038 r_symndx, ibfd))
8039 goto error_ret;
8040
8041 if (sym_sec == NULL
8042 || sym_sec->output_section == NULL
8043 || discarded_section (sym_sec))
8044 continue;
8045
8046 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8047 continue;
8048
8049 if (h != NULL)
8050 {
8051 if (h->type == STT_GNU_IFUNC)
8052 continue;
8053 val = h->root.u.def.value;
8054 }
8055 else
8056 {
8057 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8058 continue;
8059 val = sym->st_value;
8060 }
8061 val += rel->r_addend;
8062 val += sym_sec->output_section->vma + sym_sec->output_offset;
8063
8064 /* We don't yet know the exact toc pointer value, but we
8065 know it will be somewhere in the toc section. Don't
8066 optimize if the difference from any possible toc
8067 pointer is outside [ff..f80008000, 7fff7fff]. */
8068 addr = toc->output_section->vma + TOC_BASE_OFF;
8069 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8070 continue;
8071
8072 addr = toc->output_section->vma + toc->output_section->rawsize;
8073 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8074 continue;
8075
8076 if (skip == NULL)
8077 {
8078 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8079 if (skip == NULL)
8080 goto error_ret;
8081 }
8082
8083 skip[rel->r_offset >> 3]
8084 |= can_optimize | ((rel - toc_relocs) << 2);
8085 }
8086 }
8087
8088 if (skip == NULL)
8089 continue;
8090
8091 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8092 if (used == NULL)
8093 {
8094 error_ret:
8095 if (local_syms != NULL
8096 && symtab_hdr->contents != (unsigned char *) local_syms)
8097 free (local_syms);
8098 if (sec != NULL
8099 && relstart != NULL
8100 && elf_section_data (sec)->relocs != relstart)
8101 free (relstart);
8102 if (toc_relocs != NULL
8103 && elf_section_data (toc)->relocs != toc_relocs)
8104 free (toc_relocs);
8105 if (skip != NULL)
8106 free (skip);
8107 return FALSE;
8108 }
8109
8110 /* Now check all kept sections that might reference the toc.
8111 Check the toc itself last. */
8112 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8113 : ibfd->sections);
8114 sec != NULL;
8115 sec = (sec == toc ? NULL
8116 : sec->next == NULL ? toc
8117 : sec->next == toc && toc->next ? toc->next
8118 : sec->next))
8119 {
8120 int repeat;
8121
8122 if (sec->reloc_count == 0
8123 || discarded_section (sec)
8124 || get_opd_info (sec)
8125 || (sec->flags & SEC_ALLOC) == 0
8126 || (sec->flags & SEC_DEBUGGING) != 0)
8127 continue;
8128
8129 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8130 info->keep_memory);
8131 if (relstart == NULL)
8132 {
8133 free (used);
8134 goto error_ret;
8135 }
8136
8137 /* Mark toc entries referenced as used. */
8138 do
8139 {
8140 repeat = 0;
8141 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8142 {
8143 enum elf_ppc64_reloc_type r_type;
8144 unsigned long r_symndx;
8145 asection *sym_sec;
8146 struct elf_link_hash_entry *h;
8147 Elf_Internal_Sym *sym;
8148 bfd_vma val;
8149 enum {no_check, check_lo, check_ha} insn_check;
8150
8151 r_type = ELF64_R_TYPE (rel->r_info);
8152 switch (r_type)
8153 {
8154 default:
8155 insn_check = no_check;
8156 break;
8157
8158 case R_PPC64_GOT_TLSLD16_HA:
8159 case R_PPC64_GOT_TLSGD16_HA:
8160 case R_PPC64_GOT_TPREL16_HA:
8161 case R_PPC64_GOT_DTPREL16_HA:
8162 case R_PPC64_GOT16_HA:
8163 case R_PPC64_TOC16_HA:
8164 insn_check = check_ha;
8165 break;
8166
8167 case R_PPC64_GOT_TLSLD16_LO:
8168 case R_PPC64_GOT_TLSGD16_LO:
8169 case R_PPC64_GOT_TPREL16_LO_DS:
8170 case R_PPC64_GOT_DTPREL16_LO_DS:
8171 case R_PPC64_GOT16_LO:
8172 case R_PPC64_GOT16_LO_DS:
8173 case R_PPC64_TOC16_LO:
8174 case R_PPC64_TOC16_LO_DS:
8175 insn_check = check_lo;
8176 break;
8177 }
8178
8179 if (insn_check != no_check)
8180 {
8181 bfd_vma off = rel->r_offset & ~3;
8182 unsigned char buf[4];
8183 unsigned int insn;
8184
8185 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8186 {
8187 free (used);
8188 goto error_ret;
8189 }
8190 insn = bfd_get_32 (ibfd, buf);
8191 if (insn_check == check_lo
8192 ? !ok_lo_toc_insn (insn, r_type)
8193 : ((insn & ((0x3f << 26) | 0x1f << 16))
8194 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8195 {
8196 char str[12];
8197
8198 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8199 sprintf (str, "%#08x", insn);
8200 info->callbacks->einfo
8201 /* xgettext:c-format */
8202 (_("%H: toc optimization is not supported for"
8203 " %s instruction\n"),
8204 ibfd, sec, rel->r_offset & ~3, str);
8205 }
8206 }
8207
8208 switch (r_type)
8209 {
8210 case R_PPC64_TOC16:
8211 case R_PPC64_TOC16_LO:
8212 case R_PPC64_TOC16_HI:
8213 case R_PPC64_TOC16_HA:
8214 case R_PPC64_TOC16_DS:
8215 case R_PPC64_TOC16_LO_DS:
8216 /* In case we're taking addresses of toc entries. */
8217 case R_PPC64_ADDR64:
8218 break;
8219
8220 default:
8221 continue;
8222 }
8223
8224 r_symndx = ELF64_R_SYM (rel->r_info);
8225 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8226 r_symndx, ibfd))
8227 {
8228 free (used);
8229 goto error_ret;
8230 }
8231
8232 if (sym_sec != toc)
8233 continue;
8234
8235 if (h != NULL)
8236 val = h->root.u.def.value;
8237 else
8238 val = sym->st_value;
8239 val += rel->r_addend;
8240
8241 if (val >= toc->size)
8242 continue;
8243
8244 if ((skip[val >> 3] & can_optimize) != 0)
8245 {
8246 bfd_vma off;
8247 unsigned char opc;
8248
8249 switch (r_type)
8250 {
8251 case R_PPC64_TOC16_HA:
8252 break;
8253
8254 case R_PPC64_TOC16_LO_DS:
8255 off = rel->r_offset;
8256 off += (bfd_big_endian (ibfd) ? -2 : 3);
8257 if (!bfd_get_section_contents (ibfd, sec, &opc,
8258 off, 1))
8259 {
8260 free (used);
8261 goto error_ret;
8262 }
8263 if ((opc & (0x3f << 2)) == (58u << 2))
8264 break;
8265 /* Fall through. */
8266
8267 default:
8268 /* Wrong sort of reloc, or not a ld. We may
8269 as well clear ref_from_discarded too. */
8270 skip[val >> 3] = 0;
8271 }
8272 }
8273
8274 if (sec != toc)
8275 used[val >> 3] = 1;
8276 /* For the toc section, we only mark as used if this
8277 entry itself isn't unused. */
8278 else if ((used[rel->r_offset >> 3]
8279 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8280 && !used[val >> 3])
8281 {
8282 /* Do all the relocs again, to catch reference
8283 chains. */
8284 repeat = 1;
8285 used[val >> 3] = 1;
8286 }
8287 }
8288 }
8289 while (repeat);
8290
8291 if (elf_section_data (sec)->relocs != relstart)
8292 free (relstart);
8293 }
8294
8295 /* Merge the used and skip arrays. Assume that TOC
8296 doublewords not appearing as either used or unused belong
8297 to an entry more than one doubleword in size. */
8298 for (drop = skip, keep = used, last = 0, some_unused = 0;
8299 drop < skip + (toc->size + 7) / 8;
8300 ++drop, ++keep)
8301 {
8302 if (*keep)
8303 {
8304 *drop &= ~ref_from_discarded;
8305 if ((*drop & can_optimize) != 0)
8306 some_unused = 1;
8307 last = 0;
8308 }
8309 else if ((*drop & ref_from_discarded) != 0)
8310 {
8311 some_unused = 1;
8312 last = ref_from_discarded;
8313 }
8314 else
8315 *drop = last;
8316 }
8317
8318 free (used);
8319
8320 if (some_unused)
8321 {
8322 bfd_byte *contents, *src;
8323 unsigned long off;
8324 Elf_Internal_Sym *sym;
8325 bfd_boolean local_toc_syms = FALSE;
8326
8327 /* Shuffle the toc contents, and at the same time convert the
8328 skip array from booleans into offsets. */
8329 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8330 goto error_ret;
8331
8332 elf_section_data (toc)->this_hdr.contents = contents;
8333
8334 for (src = contents, off = 0, drop = skip;
8335 src < contents + toc->size;
8336 src += 8, ++drop)
8337 {
8338 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8339 off += 8;
8340 else if (off != 0)
8341 {
8342 *drop = off;
8343 memcpy (src - off, src, 8);
8344 }
8345 }
8346 *drop = off;
8347 toc->rawsize = toc->size;
8348 toc->size = src - contents - off;
8349
8350 /* Adjust addends for relocs against the toc section sym,
8351 and optimize any accesses we can. */
8352 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8353 {
8354 if (sec->reloc_count == 0
8355 || discarded_section (sec))
8356 continue;
8357
8358 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8359 info->keep_memory);
8360 if (relstart == NULL)
8361 goto error_ret;
8362
8363 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8364 {
8365 enum elf_ppc64_reloc_type r_type;
8366 unsigned long r_symndx;
8367 asection *sym_sec;
8368 struct elf_link_hash_entry *h;
8369 bfd_vma val;
8370
8371 r_type = ELF64_R_TYPE (rel->r_info);
8372 switch (r_type)
8373 {
8374 default:
8375 continue;
8376
8377 case R_PPC64_TOC16:
8378 case R_PPC64_TOC16_LO:
8379 case R_PPC64_TOC16_HI:
8380 case R_PPC64_TOC16_HA:
8381 case R_PPC64_TOC16_DS:
8382 case R_PPC64_TOC16_LO_DS:
8383 case R_PPC64_ADDR64:
8384 break;
8385 }
8386
8387 r_symndx = ELF64_R_SYM (rel->r_info);
8388 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8389 r_symndx, ibfd))
8390 goto error_ret;
8391
8392 if (sym_sec != toc)
8393 continue;
8394
8395 if (h != NULL)
8396 val = h->root.u.def.value;
8397 else
8398 {
8399 val = sym->st_value;
8400 if (val != 0)
8401 local_toc_syms = TRUE;
8402 }
8403
8404 val += rel->r_addend;
8405
8406 if (val > toc->rawsize)
8407 val = toc->rawsize;
8408 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8409 continue;
8410 else if ((skip[val >> 3] & can_optimize) != 0)
8411 {
8412 Elf_Internal_Rela *tocrel
8413 = toc_relocs + (skip[val >> 3] >> 2);
8414 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8415
8416 switch (r_type)
8417 {
8418 case R_PPC64_TOC16_HA:
8419 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8420 break;
8421
8422 case R_PPC64_TOC16_LO_DS:
8423 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8424 break;
8425
8426 default:
8427 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8428 ppc_howto_init ();
8429 info->callbacks->einfo
8430 /* xgettext:c-format */
8431 (_("%H: %s references "
8432 "optimized away TOC entry\n"),
8433 ibfd, sec, rel->r_offset,
8434 ppc64_elf_howto_table[r_type]->name);
8435 bfd_set_error (bfd_error_bad_value);
8436 goto error_ret;
8437 }
8438 rel->r_addend = tocrel->r_addend;
8439 elf_section_data (sec)->relocs = relstart;
8440 continue;
8441 }
8442
8443 if (h != NULL || sym->st_value != 0)
8444 continue;
8445
8446 rel->r_addend -= skip[val >> 3];
8447 elf_section_data (sec)->relocs = relstart;
8448 }
8449
8450 if (elf_section_data (sec)->relocs != relstart)
8451 free (relstart);
8452 }
8453
8454 /* We shouldn't have local or global symbols defined in the TOC,
8455 but handle them anyway. */
8456 if (local_syms != NULL)
8457 for (sym = local_syms;
8458 sym < local_syms + symtab_hdr->sh_info;
8459 ++sym)
8460 if (sym->st_value != 0
8461 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8462 {
8463 unsigned long i;
8464
8465 if (sym->st_value > toc->rawsize)
8466 i = toc->rawsize >> 3;
8467 else
8468 i = sym->st_value >> 3;
8469
8470 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8471 {
8472 if (local_toc_syms)
8473 _bfd_error_handler
8474 (_("%s defined on removed toc entry"),
8475 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8476 do
8477 ++i;
8478 while ((skip[i] & (ref_from_discarded | can_optimize)));
8479 sym->st_value = (bfd_vma) i << 3;
8480 }
8481
8482 sym->st_value -= skip[i];
8483 symtab_hdr->contents = (unsigned char *) local_syms;
8484 }
8485
8486 /* Adjust any global syms defined in this toc input section. */
8487 if (toc_inf.global_toc_syms)
8488 {
8489 toc_inf.toc = toc;
8490 toc_inf.skip = skip;
8491 toc_inf.global_toc_syms = FALSE;
8492 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8493 &toc_inf);
8494 }
8495
8496 if (toc->reloc_count != 0)
8497 {
8498 Elf_Internal_Shdr *rel_hdr;
8499 Elf_Internal_Rela *wrel;
8500 bfd_size_type sz;
8501
8502 /* Remove unused toc relocs, and adjust those we keep. */
8503 if (toc_relocs == NULL)
8504 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8505 info->keep_memory);
8506 if (toc_relocs == NULL)
8507 goto error_ret;
8508
8509 wrel = toc_relocs;
8510 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8511 if ((skip[rel->r_offset >> 3]
8512 & (ref_from_discarded | can_optimize)) == 0)
8513 {
8514 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8515 wrel->r_info = rel->r_info;
8516 wrel->r_addend = rel->r_addend;
8517 ++wrel;
8518 }
8519 else if (!dec_dynrel_count (rel->r_info, toc, info,
8520 &local_syms, NULL, NULL))
8521 goto error_ret;
8522
8523 elf_section_data (toc)->relocs = toc_relocs;
8524 toc->reloc_count = wrel - toc_relocs;
8525 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8526 sz = rel_hdr->sh_entsize;
8527 rel_hdr->sh_size = toc->reloc_count * sz;
8528 }
8529 }
8530 else if (toc_relocs != NULL
8531 && elf_section_data (toc)->relocs != toc_relocs)
8532 free (toc_relocs);
8533
8534 if (local_syms != NULL
8535 && symtab_hdr->contents != (unsigned char *) local_syms)
8536 {
8537 if (!info->keep_memory)
8538 free (local_syms);
8539 else
8540 symtab_hdr->contents = (unsigned char *) local_syms;
8541 }
8542 free (skip);
8543 }
8544
8545 return TRUE;
8546 }
8547
8548 /* Return true iff input section I references the TOC using
8549 instructions limited to +/-32k offsets. */
8550
8551 bfd_boolean
8552 ppc64_elf_has_small_toc_reloc (asection *i)
8553 {
8554 return (is_ppc64_elf (i->owner)
8555 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8556 }
8557
8558 /* Allocate space for one GOT entry. */
8559
8560 static void
8561 allocate_got (struct elf_link_hash_entry *h,
8562 struct bfd_link_info *info,
8563 struct got_entry *gent)
8564 {
8565 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8566 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8567 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8568 ? 16 : 8);
8569 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8570 ? 2 : 1) * sizeof (Elf64_External_Rela);
8571 asection *got = ppc64_elf_tdata (gent->owner)->got;
8572
8573 gent->got.offset = got->size;
8574 got->size += entsize;
8575
8576 if (h->type == STT_GNU_IFUNC)
8577 {
8578 htab->elf.irelplt->size += rentsize;
8579 htab->got_reli_size += rentsize;
8580 }
8581 else if (((bfd_link_pic (info)
8582 && !((gent->tls_type & TLS_TPREL) != 0
8583 && bfd_link_executable (info)
8584 && SYMBOL_REFERENCES_LOCAL (info, h)))
8585 || (htab->elf.dynamic_sections_created
8586 && h->dynindx != -1
8587 && !SYMBOL_REFERENCES_LOCAL (info, h)))
8588 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8589 {
8590 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8591 relgot->size += rentsize;
8592 }
8593 }
8594
8595 /* This function merges got entries in the same toc group. */
8596
8597 static void
8598 merge_got_entries (struct got_entry **pent)
8599 {
8600 struct got_entry *ent, *ent2;
8601
8602 for (ent = *pent; ent != NULL; ent = ent->next)
8603 if (!ent->is_indirect)
8604 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8605 if (!ent2->is_indirect
8606 && ent2->addend == ent->addend
8607 && ent2->tls_type == ent->tls_type
8608 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8609 {
8610 ent2->is_indirect = TRUE;
8611 ent2->got.ent = ent;
8612 }
8613 }
8614
8615 /* If H is undefined, make it dynamic if that makes sense. */
8616
8617 static bfd_boolean
8618 ensure_undef_dynamic (struct bfd_link_info *info,
8619 struct elf_link_hash_entry *h)
8620 {
8621 struct elf_link_hash_table *htab = elf_hash_table (info);
8622
8623 if (htab->dynamic_sections_created
8624 && ((info->dynamic_undefined_weak != 0
8625 && h->root.type == bfd_link_hash_undefweak)
8626 || h->root.type == bfd_link_hash_undefined)
8627 && h->dynindx == -1
8628 && !h->forced_local
8629 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
8630 return bfd_elf_link_record_dynamic_symbol (info, h);
8631 return TRUE;
8632 }
8633
8634 /* Allocate space in .plt, .got and associated reloc sections for
8635 dynamic relocs. */
8636
8637 static bfd_boolean
8638 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8639 {
8640 struct bfd_link_info *info;
8641 struct ppc_link_hash_table *htab;
8642 asection *s;
8643 struct ppc_link_hash_entry *eh;
8644 struct got_entry **pgent, *gent;
8645
8646 if (h->root.type == bfd_link_hash_indirect)
8647 return TRUE;
8648
8649 info = (struct bfd_link_info *) inf;
8650 htab = ppc_hash_table (info);
8651 if (htab == NULL)
8652 return FALSE;
8653
8654 eh = (struct ppc_link_hash_entry *) h;
8655 /* Run through the TLS GD got entries first if we're changing them
8656 to TPREL. */
8657 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
8658 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8659 if (gent->got.refcount > 0
8660 && (gent->tls_type & TLS_GD) != 0)
8661 {
8662 /* This was a GD entry that has been converted to TPREL. If
8663 there happens to be a TPREL entry we can use that one. */
8664 struct got_entry *ent;
8665 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8666 if (ent->got.refcount > 0
8667 && (ent->tls_type & TLS_TPREL) != 0
8668 && ent->addend == gent->addend
8669 && ent->owner == gent->owner)
8670 {
8671 gent->got.refcount = 0;
8672 break;
8673 }
8674
8675 /* If not, then we'll be using our own TPREL entry. */
8676 if (gent->got.refcount != 0)
8677 gent->tls_type = TLS_TLS | TLS_TPREL;
8678 }
8679
8680 /* Remove any list entry that won't generate a word in the GOT before
8681 we call merge_got_entries. Otherwise we risk merging to empty
8682 entries. */
8683 pgent = &h->got.glist;
8684 while ((gent = *pgent) != NULL)
8685 if (gent->got.refcount > 0)
8686 {
8687 if ((gent->tls_type & TLS_LD) != 0
8688 && !h->def_dynamic)
8689 {
8690 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8691 *pgent = gent->next;
8692 }
8693 else
8694 pgent = &gent->next;
8695 }
8696 else
8697 *pgent = gent->next;
8698
8699 if (!htab->do_multi_toc)
8700 merge_got_entries (&h->got.glist);
8701
8702 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8703 if (!gent->is_indirect)
8704 {
8705 /* Make sure this symbol is output as a dynamic symbol. */
8706 if (!ensure_undef_dynamic (info, h))
8707 return FALSE;
8708
8709 if (!is_ppc64_elf (gent->owner))
8710 abort ();
8711
8712 allocate_got (h, info, gent);
8713 }
8714
8715 /* If no dynamic sections we can't have dynamic relocs, except for
8716 IFUNCs which are handled even in static executables. */
8717 if (!htab->elf.dynamic_sections_created
8718 && h->type != STT_GNU_IFUNC)
8719 eh->dyn_relocs = NULL;
8720
8721 /* Discard relocs on undefined symbols that must be local. */
8722 else if (h->root.type == bfd_link_hash_undefined
8723 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8724 eh->dyn_relocs = NULL;
8725
8726 /* Also discard relocs on undefined weak syms with non-default
8727 visibility, or when dynamic_undefined_weak says so. */
8728 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8729 eh->dyn_relocs = NULL;
8730
8731 if (eh->dyn_relocs != NULL)
8732 {
8733 struct elf_dyn_relocs *p, **pp;
8734
8735 /* In the shared -Bsymbolic case, discard space allocated for
8736 dynamic pc-relative relocs against symbols which turn out to
8737 be defined in regular objects. For the normal shared case,
8738 discard space for relocs that have become local due to symbol
8739 visibility changes. */
8740
8741 if (bfd_link_pic (info))
8742 {
8743 /* Relocs that use pc_count are those that appear on a call
8744 insn, or certain REL relocs (see must_be_dyn_reloc) that
8745 can be generated via assembly. We want calls to
8746 protected symbols to resolve directly to the function
8747 rather than going via the plt. If people want function
8748 pointer comparisons to work as expected then they should
8749 avoid writing weird assembly. */
8750 if (SYMBOL_CALLS_LOCAL (info, h))
8751 {
8752 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8753 {
8754 p->count -= p->pc_count;
8755 p->pc_count = 0;
8756 if (p->count == 0)
8757 *pp = p->next;
8758 else
8759 pp = &p->next;
8760 }
8761 }
8762
8763 if (eh->dyn_relocs != NULL)
8764 {
8765 /* Make sure this symbol is output as a dynamic symbol. */
8766 if (!ensure_undef_dynamic (info, h))
8767 return FALSE;
8768 }
8769 }
8770 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
8771 {
8772 /* For the non-pic case, discard space for relocs against
8773 symbols which turn out to need copy relocs or are not
8774 dynamic. */
8775 if (h->dynamic_adjusted
8776 && !h->def_regular
8777 && !ELF_COMMON_DEF_P (h))
8778 {
8779 /* Make sure this symbol is output as a dynamic symbol. */
8780 if (!ensure_undef_dynamic (info, h))
8781 return FALSE;
8782
8783 if (h->dynindx == -1)
8784 eh->dyn_relocs = NULL;
8785 }
8786 else
8787 eh->dyn_relocs = NULL;
8788 }
8789
8790 /* Finally, allocate space. */
8791 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8792 {
8793 asection *sreloc = elf_section_data (p->sec)->sreloc;
8794 if (eh->elf.type == STT_GNU_IFUNC)
8795 sreloc = htab->elf.irelplt;
8796 sreloc->size += p->count * sizeof (Elf64_External_Rela);
8797 }
8798 }
8799
8800 /* We might need a PLT entry when the symbol
8801 a) is dynamic, or
8802 b) is an ifunc, or
8803 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
8804 d) has plt16 relocs and we are linking statically. */
8805 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
8806 || h->type == STT_GNU_IFUNC
8807 || (h->needs_plt && h->dynamic_adjusted)
8808 || (h->needs_plt
8809 && h->def_regular
8810 && !htab->elf.dynamic_sections_created
8811 && !htab->can_convert_all_inline_plt
8812 && (((struct ppc_link_hash_entry *) h)->tls_mask
8813 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
8814 {
8815 struct plt_entry *pent;
8816 bfd_boolean doneone = FALSE;
8817 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8818 if (pent->plt.refcount > 0)
8819 {
8820 if (!htab->elf.dynamic_sections_created
8821 || h->dynindx == -1)
8822 {
8823 if (h->type == STT_GNU_IFUNC)
8824 {
8825 s = htab->elf.iplt;
8826 pent->plt.offset = s->size;
8827 s->size += PLT_ENTRY_SIZE (htab);
8828 s = htab->elf.irelplt;
8829 }
8830 else
8831 {
8832 s = htab->pltlocal;
8833 pent->plt.offset = s->size;
8834 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
8835 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
8836 }
8837 }
8838 else
8839 {
8840 /* If this is the first .plt entry, make room for the special
8841 first entry. */
8842 s = htab->elf.splt;
8843 if (s->size == 0)
8844 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
8845
8846 pent->plt.offset = s->size;
8847
8848 /* Make room for this entry. */
8849 s->size += PLT_ENTRY_SIZE (htab);
8850
8851 /* Make room for the .glink code. */
8852 s = htab->glink;
8853 if (s->size == 0)
8854 s->size += GLINK_PLTRESOLVE_SIZE (htab);
8855 if (htab->opd_abi)
8856 {
8857 /* We need bigger stubs past index 32767. */
8858 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
8859 s->size += 4;
8860 s->size += 2*4;
8861 }
8862 else
8863 s->size += 4;
8864
8865 /* We also need to make an entry in the .rela.plt section. */
8866 s = htab->elf.srelplt;
8867 }
8868 if (s != NULL)
8869 s->size += sizeof (Elf64_External_Rela);
8870 doneone = TRUE;
8871 }
8872 else
8873 pent->plt.offset = (bfd_vma) -1;
8874 if (!doneone)
8875 {
8876 h->plt.plist = NULL;
8877 h->needs_plt = 0;
8878 }
8879 }
8880 else
8881 {
8882 h->plt.plist = NULL;
8883 h->needs_plt = 0;
8884 }
8885
8886 return TRUE;
8887 }
8888
8889 #define PPC_LO(v) ((v) & 0xffff)
8890 #define PPC_HI(v) (((v) >> 16) & 0xffff)
8891 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
8892
8893 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
8894 to set up space for global entry stubs. These are put in glink,
8895 after the branch table. */
8896
8897 static bfd_boolean
8898 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
8899 {
8900 struct bfd_link_info *info;
8901 struct ppc_link_hash_table *htab;
8902 struct plt_entry *pent;
8903 asection *s, *plt;
8904
8905 if (h->root.type == bfd_link_hash_indirect)
8906 return TRUE;
8907
8908 if (!h->pointer_equality_needed)
8909 return TRUE;
8910
8911 if (h->def_regular)
8912 return TRUE;
8913
8914 info = inf;
8915 htab = ppc_hash_table (info);
8916 if (htab == NULL)
8917 return FALSE;
8918
8919 s = htab->global_entry;
8920 plt = htab->elf.splt;
8921 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8922 if (pent->plt.offset != (bfd_vma) -1
8923 && pent->addend == 0)
8924 {
8925 /* For ELFv2, if this symbol is not defined in a regular file
8926 and we are not generating a shared library or pie, then we
8927 need to define the symbol in the executable on a call stub.
8928 This is to avoid text relocations. */
8929 bfd_vma off, stub_align, stub_off, stub_size;
8930 unsigned int align_power;
8931
8932 stub_size = 16;
8933 stub_off = s->size;
8934 if (htab->params->plt_stub_align >= 0)
8935 align_power = htab->params->plt_stub_align;
8936 else
8937 align_power = -htab->params->plt_stub_align;
8938 /* Setting section alignment is delayed until we know it is
8939 non-empty. Otherwise the .text output section will be
8940 aligned at least to plt_stub_align even when no global
8941 entry stubs are needed. */
8942 if (s->alignment_power < align_power)
8943 s->alignment_power = align_power;
8944 stub_align = (bfd_vma) 1 << align_power;
8945 if (htab->params->plt_stub_align >= 0
8946 || ((((stub_off + stub_size - 1) & -stub_align)
8947 - (stub_off & -stub_align))
8948 > ((stub_size - 1) & -stub_align)))
8949 stub_off = (stub_off + stub_align - 1) & -stub_align;
8950 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
8951 off -= stub_off + s->output_offset + s->output_section->vma;
8952 /* Note that for --plt-stub-align negative we have a possible
8953 dependency between stub offset and size. Break that
8954 dependency by assuming the max stub size when calculating
8955 the stub offset. */
8956 if (PPC_HA (off) == 0)
8957 stub_size -= 4;
8958 h->root.type = bfd_link_hash_defined;
8959 h->root.u.def.section = s;
8960 h->root.u.def.value = stub_off;
8961 s->size = stub_off + stub_size;
8962 break;
8963 }
8964 return TRUE;
8965 }
8966
8967 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
8968 read-only sections. */
8969
8970 static bfd_boolean
8971 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
8972 {
8973 asection *sec;
8974
8975 if (h->root.type == bfd_link_hash_indirect)
8976 return TRUE;
8977
8978 sec = readonly_dynrelocs (h);
8979 if (sec != NULL)
8980 {
8981 struct bfd_link_info *info = (struct bfd_link_info *) inf;
8982
8983 info->flags |= DF_TEXTREL;
8984 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
8985 " in read-only section `%pA'\n"),
8986 sec->owner, h->root.root.string, sec);
8987
8988 /* Not an error, just cut short the traversal. */
8989 return FALSE;
8990 }
8991 return TRUE;
8992 }
8993
8994 /* Set the sizes of the dynamic sections. */
8995
8996 static bfd_boolean
8997 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
8998 struct bfd_link_info *info)
8999 {
9000 struct ppc_link_hash_table *htab;
9001 bfd *dynobj;
9002 asection *s;
9003 bfd_boolean relocs;
9004 bfd *ibfd;
9005 struct got_entry *first_tlsld;
9006
9007 htab = ppc_hash_table (info);
9008 if (htab == NULL)
9009 return FALSE;
9010
9011 dynobj = htab->elf.dynobj;
9012 if (dynobj == NULL)
9013 abort ();
9014
9015 if (htab->elf.dynamic_sections_created)
9016 {
9017 /* Set the contents of the .interp section to the interpreter. */
9018 if (bfd_link_executable (info) && !info->nointerp)
9019 {
9020 s = bfd_get_linker_section (dynobj, ".interp");
9021 if (s == NULL)
9022 abort ();
9023 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9024 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9025 }
9026 }
9027
9028 /* Set up .got offsets for local syms, and space for local dynamic
9029 relocs. */
9030 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9031 {
9032 struct got_entry **lgot_ents;
9033 struct got_entry **end_lgot_ents;
9034 struct plt_entry **local_plt;
9035 struct plt_entry **end_local_plt;
9036 unsigned char *lgot_masks;
9037 bfd_size_type locsymcount;
9038 Elf_Internal_Shdr *symtab_hdr;
9039
9040 if (!is_ppc64_elf (ibfd))
9041 continue;
9042
9043 for (s = ibfd->sections; s != NULL; s = s->next)
9044 {
9045 struct ppc_dyn_relocs *p;
9046
9047 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9048 {
9049 if (!bfd_is_abs_section (p->sec)
9050 && bfd_is_abs_section (p->sec->output_section))
9051 {
9052 /* Input section has been discarded, either because
9053 it is a copy of a linkonce section or due to
9054 linker script /DISCARD/, so we'll be discarding
9055 the relocs too. */
9056 }
9057 else if (p->count != 0)
9058 {
9059 asection *srel = elf_section_data (p->sec)->sreloc;
9060 if (p->ifunc)
9061 srel = htab->elf.irelplt;
9062 srel->size += p->count * sizeof (Elf64_External_Rela);
9063 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9064 info->flags |= DF_TEXTREL;
9065 }
9066 }
9067 }
9068
9069 lgot_ents = elf_local_got_ents (ibfd);
9070 if (!lgot_ents)
9071 continue;
9072
9073 symtab_hdr = &elf_symtab_hdr (ibfd);
9074 locsymcount = symtab_hdr->sh_info;
9075 end_lgot_ents = lgot_ents + locsymcount;
9076 local_plt = (struct plt_entry **) end_lgot_ents;
9077 end_local_plt = local_plt + locsymcount;
9078 lgot_masks = (unsigned char *) end_local_plt;
9079 s = ppc64_elf_tdata (ibfd)->got;
9080 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9081 {
9082 struct got_entry **pent, *ent;
9083
9084 pent = lgot_ents;
9085 while ((ent = *pent) != NULL)
9086 if (ent->got.refcount > 0)
9087 {
9088 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9089 {
9090 ppc64_tlsld_got (ibfd)->got.refcount += 1;
9091 *pent = ent->next;
9092 }
9093 else
9094 {
9095 unsigned int ent_size = 8;
9096 unsigned int rel_size = sizeof (Elf64_External_Rela);
9097
9098 ent->got.offset = s->size;
9099 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9100 {
9101 ent_size *= 2;
9102 rel_size *= 2;
9103 }
9104 s->size += ent_size;
9105 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9106 {
9107 htab->elf.irelplt->size += rel_size;
9108 htab->got_reli_size += rel_size;
9109 }
9110 else if (bfd_link_pic (info)
9111 && !((ent->tls_type & TLS_TPREL) != 0
9112 && bfd_link_executable (info)))
9113 {
9114 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9115 srel->size += rel_size;
9116 }
9117 pent = &ent->next;
9118 }
9119 }
9120 else
9121 *pent = ent->next;
9122 }
9123
9124 /* Allocate space for plt calls to local syms. */
9125 lgot_masks = (unsigned char *) end_local_plt;
9126 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
9127 {
9128 struct plt_entry *ent;
9129
9130 for (ent = *local_plt; ent != NULL; ent = ent->next)
9131 if (ent->plt.refcount > 0)
9132 {
9133 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9134 {
9135 s = htab->elf.iplt;
9136 ent->plt.offset = s->size;
9137 s->size += PLT_ENTRY_SIZE (htab);
9138 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9139 }
9140 else if (htab->can_convert_all_inline_plt
9141 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
9142 ent->plt.offset = (bfd_vma) -1;
9143 else
9144 {
9145 s = htab->pltlocal;
9146 ent->plt.offset = s->size;
9147 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9148 if (bfd_link_pic (info))
9149 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9150 }
9151 }
9152 else
9153 ent->plt.offset = (bfd_vma) -1;
9154 }
9155 }
9156
9157 /* Allocate global sym .plt and .got entries, and space for global
9158 sym dynamic relocs. */
9159 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9160
9161 if (!htab->opd_abi && !bfd_link_pic (info))
9162 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9163
9164 first_tlsld = NULL;
9165 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9166 {
9167 struct got_entry *ent;
9168
9169 if (!is_ppc64_elf (ibfd))
9170 continue;
9171
9172 ent = ppc64_tlsld_got (ibfd);
9173 if (ent->got.refcount > 0)
9174 {
9175 if (!htab->do_multi_toc && first_tlsld != NULL)
9176 {
9177 ent->is_indirect = TRUE;
9178 ent->got.ent = first_tlsld;
9179 }
9180 else
9181 {
9182 if (first_tlsld == NULL)
9183 first_tlsld = ent;
9184 s = ppc64_elf_tdata (ibfd)->got;
9185 ent->got.offset = s->size;
9186 ent->owner = ibfd;
9187 s->size += 16;
9188 if (bfd_link_pic (info))
9189 {
9190 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9191 srel->size += sizeof (Elf64_External_Rela);
9192 }
9193 }
9194 }
9195 else
9196 ent->got.offset = (bfd_vma) -1;
9197 }
9198
9199 /* We now have determined the sizes of the various dynamic sections.
9200 Allocate memory for them. */
9201 relocs = FALSE;
9202 for (s = dynobj->sections; s != NULL; s = s->next)
9203 {
9204 if ((s->flags & SEC_LINKER_CREATED) == 0)
9205 continue;
9206
9207 if (s == htab->brlt || s == htab->relbrlt)
9208 /* These haven't been allocated yet; don't strip. */
9209 continue;
9210 else if (s == htab->elf.sgot
9211 || s == htab->elf.splt
9212 || s == htab->elf.iplt
9213 || s == htab->pltlocal
9214 || s == htab->glink
9215 || s == htab->global_entry
9216 || s == htab->elf.sdynbss
9217 || s == htab->elf.sdynrelro)
9218 {
9219 /* Strip this section if we don't need it; see the
9220 comment below. */
9221 }
9222 else if (s == htab->glink_eh_frame)
9223 {
9224 if (!bfd_is_abs_section (s->output_section))
9225 /* Not sized yet. */
9226 continue;
9227 }
9228 else if (CONST_STRNEQ (s->name, ".rela"))
9229 {
9230 if (s->size != 0)
9231 {
9232 if (s != htab->elf.srelplt)
9233 relocs = TRUE;
9234
9235 /* We use the reloc_count field as a counter if we need
9236 to copy relocs into the output file. */
9237 s->reloc_count = 0;
9238 }
9239 }
9240 else
9241 {
9242 /* It's not one of our sections, so don't allocate space. */
9243 continue;
9244 }
9245
9246 if (s->size == 0)
9247 {
9248 /* If we don't need this section, strip it from the
9249 output file. This is mostly to handle .rela.bss and
9250 .rela.plt. We must create both sections in
9251 create_dynamic_sections, because they must be created
9252 before the linker maps input sections to output
9253 sections. The linker does that before
9254 adjust_dynamic_symbol is called, and it is that
9255 function which decides whether anything needs to go
9256 into these sections. */
9257 s->flags |= SEC_EXCLUDE;
9258 continue;
9259 }
9260
9261 if (bfd_is_abs_section (s->output_section))
9262 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9263 s->name);
9264
9265 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9266 continue;
9267
9268 /* Allocate memory for the section contents. We use bfd_zalloc
9269 here in case unused entries are not reclaimed before the
9270 section's contents are written out. This should not happen,
9271 but this way if it does we get a R_PPC64_NONE reloc in .rela
9272 sections instead of garbage.
9273 We also rely on the section contents being zero when writing
9274 the GOT and .dynrelro. */
9275 s->contents = bfd_zalloc (dynobj, s->size);
9276 if (s->contents == NULL)
9277 return FALSE;
9278 }
9279
9280 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9281 {
9282 if (!is_ppc64_elf (ibfd))
9283 continue;
9284
9285 s = ppc64_elf_tdata (ibfd)->got;
9286 if (s != NULL && s != htab->elf.sgot)
9287 {
9288 if (s->size == 0)
9289 s->flags |= SEC_EXCLUDE;
9290 else
9291 {
9292 s->contents = bfd_zalloc (ibfd, s->size);
9293 if (s->contents == NULL)
9294 return FALSE;
9295 }
9296 }
9297 s = ppc64_elf_tdata (ibfd)->relgot;
9298 if (s != NULL)
9299 {
9300 if (s->size == 0)
9301 s->flags |= SEC_EXCLUDE;
9302 else
9303 {
9304 s->contents = bfd_zalloc (ibfd, s->size);
9305 if (s->contents == NULL)
9306 return FALSE;
9307 relocs = TRUE;
9308 s->reloc_count = 0;
9309 }
9310 }
9311 }
9312
9313 if (htab->elf.dynamic_sections_created)
9314 {
9315 bfd_boolean tls_opt;
9316
9317 /* Add some entries to the .dynamic section. We fill in the
9318 values later, in ppc64_elf_finish_dynamic_sections, but we
9319 must add the entries now so that we get the correct size for
9320 the .dynamic section. The DT_DEBUG entry is filled in by the
9321 dynamic linker and used by the debugger. */
9322 #define add_dynamic_entry(TAG, VAL) \
9323 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9324
9325 if (bfd_link_executable (info))
9326 {
9327 if (!add_dynamic_entry (DT_DEBUG, 0))
9328 return FALSE;
9329 }
9330
9331 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
9332 {
9333 if (!add_dynamic_entry (DT_PLTGOT, 0)
9334 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9335 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9336 || !add_dynamic_entry (DT_JMPREL, 0)
9337 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9338 return FALSE;
9339 }
9340
9341 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
9342 {
9343 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9344 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9345 return FALSE;
9346 }
9347
9348 tls_opt = (htab->params->tls_get_addr_opt
9349 && htab->tls_get_addr_fd != NULL
9350 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9351 if (tls_opt || !htab->opd_abi)
9352 {
9353 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9354 return FALSE;
9355 }
9356
9357 if (relocs)
9358 {
9359 if (!add_dynamic_entry (DT_RELA, 0)
9360 || !add_dynamic_entry (DT_RELASZ, 0)
9361 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9362 return FALSE;
9363
9364 /* If any dynamic relocs apply to a read-only section,
9365 then we need a DT_TEXTREL entry. */
9366 if ((info->flags & DF_TEXTREL) == 0)
9367 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
9368
9369 if ((info->flags & DF_TEXTREL) != 0)
9370 {
9371 if (!add_dynamic_entry (DT_TEXTREL, 0))
9372 return FALSE;
9373 }
9374 }
9375 }
9376 #undef add_dynamic_entry
9377
9378 return TRUE;
9379 }
9380
9381 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
9382
9383 static bfd_boolean
9384 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9385 {
9386 if (h->plt.plist != NULL
9387 && !h->def_regular
9388 && !h->pointer_equality_needed)
9389 return FALSE;
9390
9391 return _bfd_elf_hash_symbol (h);
9392 }
9393
9394 /* Determine the type of stub needed, if any, for a call. */
9395
9396 static inline enum ppc_stub_type
9397 ppc_type_of_stub (asection *input_sec,
9398 const Elf_Internal_Rela *rel,
9399 struct ppc_link_hash_entry **hash,
9400 struct plt_entry **plt_ent,
9401 bfd_vma destination,
9402 unsigned long local_off)
9403 {
9404 struct ppc_link_hash_entry *h = *hash;
9405 bfd_vma location;
9406 bfd_vma branch_offset;
9407 bfd_vma max_branch_offset;
9408 enum elf_ppc64_reloc_type r_type;
9409
9410 if (h != NULL)
9411 {
9412 struct plt_entry *ent;
9413 struct ppc_link_hash_entry *fdh = h;
9414 if (h->oh != NULL
9415 && h->oh->is_func_descriptor)
9416 {
9417 fdh = ppc_follow_link (h->oh);
9418 *hash = fdh;
9419 }
9420
9421 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9422 if (ent->addend == rel->r_addend
9423 && ent->plt.offset != (bfd_vma) -1)
9424 {
9425 *plt_ent = ent;
9426 return ppc_stub_plt_call;
9427 }
9428
9429 /* Here, we know we don't have a plt entry. If we don't have a
9430 either a defined function descriptor or a defined entry symbol
9431 in a regular object file, then it is pointless trying to make
9432 any other type of stub. */
9433 if (!is_static_defined (&fdh->elf)
9434 && !is_static_defined (&h->elf))
9435 return ppc_stub_none;
9436 }
9437 else if (elf_local_got_ents (input_sec->owner) != NULL)
9438 {
9439 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9440 struct plt_entry **local_plt = (struct plt_entry **)
9441 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9442 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9443
9444 if (local_plt[r_symndx] != NULL)
9445 {
9446 struct plt_entry *ent;
9447
9448 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9449 if (ent->addend == rel->r_addend
9450 && ent->plt.offset != (bfd_vma) -1)
9451 {
9452 *plt_ent = ent;
9453 return ppc_stub_plt_call;
9454 }
9455 }
9456 }
9457
9458 /* Determine where the call point is. */
9459 location = (input_sec->output_offset
9460 + input_sec->output_section->vma
9461 + rel->r_offset);
9462
9463 branch_offset = destination - location;
9464 r_type = ELF64_R_TYPE (rel->r_info);
9465
9466 /* Determine if a long branch stub is needed. */
9467 max_branch_offset = 1 << 25;
9468 if (r_type == R_PPC64_REL14
9469 || r_type == R_PPC64_REL14_BRTAKEN
9470 || r_type == R_PPC64_REL14_BRNTAKEN)
9471 max_branch_offset = 1 << 15;
9472
9473 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
9474 /* We need a stub. Figure out whether a long_branch or plt_branch
9475 is needed later. */
9476 return ppc_stub_long_branch;
9477
9478 return ppc_stub_none;
9479 }
9480
9481 /* Builds a 64-bit offset in r12 then adds it to r11 (LOAD false) or
9482 loads r12 from r11+r12 (LOAD true).
9483 . lis %r12,xxx-1b@highest
9484 . ori %r12,xxx-1b@higher
9485 . sldi %r12,%r12,32
9486 . oris %r12,%r12,xxx-1b@hi
9487 . ori %r12,%r12,xxx-1b@l
9488 . add %r12,%r11,%r12 */
9489
9490 static bfd_byte *
9491 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
9492 {
9493 if (off + 0x8000 < 0x10000)
9494 {
9495 if (load)
9496 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
9497 else
9498 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
9499 p += 4;
9500 }
9501 else if (off + 0x80008000ULL < 0x100000000ULL)
9502 {
9503 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
9504 p += 4;
9505 if (load)
9506 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
9507 else
9508 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
9509 p += 4;
9510 }
9511 else
9512 {
9513 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9514 {
9515 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
9516 p += 4;
9517 }
9518 else
9519 {
9520 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
9521 p += 4;
9522 if (((off >> 32) & 0xffff) != 0)
9523 {
9524 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
9525 p += 4;
9526 }
9527 }
9528 if (((off >> 32) & 0xffffffffULL) != 0)
9529 {
9530 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
9531 p += 4;
9532 }
9533 if (PPC_HI (off) != 0)
9534 {
9535 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
9536 p += 4;
9537 }
9538 if (PPC_LO (off) != 0)
9539 {
9540 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
9541 p += 4;
9542 }
9543 if (load)
9544 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
9545 else
9546 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
9547 p += 4;
9548 }
9549 return p;
9550 }
9551
9552 static unsigned int
9553 size_offset (bfd_vma off)
9554 {
9555 unsigned int size;
9556 if (off + 0x8000 < 0x10000)
9557 size = 4;
9558 else if (off + 0x80008000ULL < 0x100000000ULL)
9559 size = 8;
9560 else
9561 {
9562 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9563 size = 4;
9564 else
9565 {
9566 size = 4;
9567 if (((off >> 32) & 0xffff) != 0)
9568 size += 4;
9569 }
9570 if (((off >> 32) & 0xffffffffULL) != 0)
9571 size += 4;
9572 if (PPC_HI (off) != 0)
9573 size += 4;
9574 if (PPC_LO (off) != 0)
9575 size += 4;
9576 size += 4;
9577 }
9578 return size;
9579 }
9580
9581 /* Emit .eh_frame opcode to advance pc by DELTA. */
9582
9583 static bfd_byte *
9584 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
9585 {
9586 delta /= 4;
9587 if (delta < 64)
9588 *eh++ = DW_CFA_advance_loc + delta;
9589 else if (delta < 256)
9590 {
9591 *eh++ = DW_CFA_advance_loc1;
9592 *eh++ = delta;
9593 }
9594 else if (delta < 65536)
9595 {
9596 *eh++ = DW_CFA_advance_loc2;
9597 bfd_put_16 (abfd, delta, eh);
9598 eh += 2;
9599 }
9600 else
9601 {
9602 *eh++ = DW_CFA_advance_loc4;
9603 bfd_put_32 (abfd, delta, eh);
9604 eh += 4;
9605 }
9606 return eh;
9607 }
9608
9609 /* Size of required .eh_frame opcode to advance pc by DELTA. */
9610
9611 static unsigned int
9612 eh_advance_size (unsigned int delta)
9613 {
9614 if (delta < 64 * 4)
9615 /* DW_CFA_advance_loc+[1..63]. */
9616 return 1;
9617 if (delta < 256 * 4)
9618 /* DW_CFA_advance_loc1, byte. */
9619 return 2;
9620 if (delta < 65536 * 4)
9621 /* DW_CFA_advance_loc2, 2 bytes. */
9622 return 3;
9623 /* DW_CFA_advance_loc4, 4 bytes. */
9624 return 5;
9625 }
9626
9627 /* With power7 weakly ordered memory model, it is possible for ld.so
9628 to update a plt entry in one thread and have another thread see a
9629 stale zero toc entry. To avoid this we need some sort of acquire
9630 barrier in the call stub. One solution is to make the load of the
9631 toc word seem to appear to depend on the load of the function entry
9632 word. Another solution is to test for r2 being zero, and branch to
9633 the appropriate glink entry if so.
9634
9635 . fake dep barrier compare
9636 . ld 12,xxx(2) ld 12,xxx(2)
9637 . mtctr 12 mtctr 12
9638 . xor 11,12,12 ld 2,xxx+8(2)
9639 . add 2,2,11 cmpldi 2,0
9640 . ld 2,xxx+8(2) bnectr+
9641 . bctr b <glink_entry>
9642
9643 The solution involving the compare turns out to be faster, so
9644 that's what we use unless the branch won't reach. */
9645
9646 #define ALWAYS_USE_FAKE_DEP 0
9647 #define ALWAYS_EMIT_R2SAVE 0
9648
9649 static inline unsigned int
9650 plt_stub_size (struct ppc_link_hash_table *htab,
9651 struct ppc_stub_hash_entry *stub_entry,
9652 bfd_vma off)
9653 {
9654 unsigned size;
9655
9656 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
9657 {
9658 size = 24 + size_offset (off);
9659 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
9660 size += 4;
9661 return size;
9662 }
9663
9664 size = 12;
9665 if (ALWAYS_EMIT_R2SAVE
9666 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9667 size += 4;
9668 if (PPC_HA (off) != 0)
9669 size += 4;
9670 if (htab->opd_abi)
9671 {
9672 size += 4;
9673 if (htab->params->plt_static_chain)
9674 size += 4;
9675 if (htab->params->plt_thread_safe
9676 && htab->elf.dynamic_sections_created
9677 && stub_entry->h != NULL
9678 && stub_entry->h->elf.dynindx != -1)
9679 size += 8;
9680 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
9681 size += 4;
9682 }
9683 if (stub_entry->h != NULL
9684 && (stub_entry->h == htab->tls_get_addr_fd
9685 || stub_entry->h == htab->tls_get_addr)
9686 && htab->params->tls_get_addr_opt)
9687 {
9688 size += 7 * 4;
9689 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
9690 size += 6 * 4;
9691 }
9692 return size;
9693 }
9694
9695 /* Depending on the sign of plt_stub_align:
9696 If positive, return the padding to align to a 2**plt_stub_align
9697 boundary.
9698 If negative, if this stub would cross fewer 2**plt_stub_align
9699 boundaries if we align, then return the padding needed to do so. */
9700
9701 static inline unsigned int
9702 plt_stub_pad (struct ppc_link_hash_table *htab,
9703 struct ppc_stub_hash_entry *stub_entry,
9704 bfd_vma plt_off)
9705 {
9706 int stub_align;
9707 unsigned stub_size;
9708 bfd_vma stub_off = stub_entry->group->stub_sec->size;
9709
9710 if (htab->params->plt_stub_align >= 0)
9711 {
9712 stub_align = 1 << htab->params->plt_stub_align;
9713 if ((stub_off & (stub_align - 1)) != 0)
9714 return stub_align - (stub_off & (stub_align - 1));
9715 return 0;
9716 }
9717
9718 stub_align = 1 << -htab->params->plt_stub_align;
9719 stub_size = plt_stub_size (htab, stub_entry, plt_off);
9720 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
9721 > ((stub_size - 1) & -stub_align))
9722 return stub_align - (stub_off & (stub_align - 1));
9723 return 0;
9724 }
9725
9726 /* Build a .plt call stub. */
9727
9728 static inline bfd_byte *
9729 build_plt_stub (struct ppc_link_hash_table *htab,
9730 struct ppc_stub_hash_entry *stub_entry,
9731 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9732 {
9733 bfd *obfd = htab->params->stub_bfd;
9734 bfd_boolean plt_load_toc = htab->opd_abi;
9735 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
9736 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
9737 && htab->elf.dynamic_sections_created
9738 && stub_entry->h != NULL
9739 && stub_entry->h->elf.dynindx != -1);
9740 bfd_boolean use_fake_dep = plt_thread_safe;
9741 bfd_vma cmp_branch_off = 0;
9742
9743 if (!ALWAYS_USE_FAKE_DEP
9744 && plt_load_toc
9745 && plt_thread_safe
9746 && !((stub_entry->h == htab->tls_get_addr_fd
9747 || stub_entry->h == htab->tls_get_addr)
9748 && htab->params->tls_get_addr_opt))
9749 {
9750 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
9751 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
9752 / PLT_ENTRY_SIZE (htab));
9753 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
9754 bfd_vma to, from;
9755
9756 if (pltindex > 32768)
9757 glinkoff += (pltindex - 32768) * 4;
9758 to = (glinkoff
9759 + htab->glink->output_offset
9760 + htab->glink->output_section->vma);
9761 from = (p - stub_entry->group->stub_sec->contents
9762 + 4 * (ALWAYS_EMIT_R2SAVE
9763 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9764 + 4 * (PPC_HA (offset) != 0)
9765 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
9766 != PPC_HA (offset))
9767 + 4 * (plt_static_chain != 0)
9768 + 20
9769 + stub_entry->group->stub_sec->output_offset
9770 + stub_entry->group->stub_sec->output_section->vma);
9771 cmp_branch_off = to - from;
9772 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
9773 }
9774
9775 if (PPC_HA (offset) != 0)
9776 {
9777 if (r != NULL)
9778 {
9779 if (ALWAYS_EMIT_R2SAVE
9780 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9781 r[0].r_offset += 4;
9782 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9783 r[1].r_offset = r[0].r_offset + 4;
9784 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9785 r[1].r_addend = r[0].r_addend;
9786 if (plt_load_toc)
9787 {
9788 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9789 {
9790 r[2].r_offset = r[1].r_offset + 4;
9791 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9792 r[2].r_addend = r[0].r_addend;
9793 }
9794 else
9795 {
9796 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
9797 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9798 r[2].r_addend = r[0].r_addend + 8;
9799 if (plt_static_chain)
9800 {
9801 r[3].r_offset = r[2].r_offset + 4;
9802 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9803 r[3].r_addend = r[0].r_addend + 16;
9804 }
9805 }
9806 }
9807 }
9808 if (ALWAYS_EMIT_R2SAVE
9809 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9810 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
9811 if (plt_load_toc)
9812 {
9813 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
9814 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
9815 }
9816 else
9817 {
9818 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9819 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
9820 }
9821 if (plt_load_toc
9822 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9823 {
9824 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
9825 offset = 0;
9826 }
9827 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
9828 if (plt_load_toc)
9829 {
9830 if (use_fake_dep)
9831 {
9832 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
9833 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
9834 }
9835 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
9836 if (plt_static_chain)
9837 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
9838 }
9839 }
9840 else
9841 {
9842 if (r != NULL)
9843 {
9844 if (ALWAYS_EMIT_R2SAVE
9845 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9846 r[0].r_offset += 4;
9847 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9848 if (plt_load_toc)
9849 {
9850 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9851 {
9852 r[1].r_offset = r[0].r_offset + 4;
9853 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9854 r[1].r_addend = r[0].r_addend;
9855 }
9856 else
9857 {
9858 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
9859 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9860 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
9861 if (plt_static_chain)
9862 {
9863 r[2].r_offset = r[1].r_offset + 4;
9864 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9865 r[2].r_addend = r[0].r_addend + 8;
9866 }
9867 }
9868 }
9869 }
9870 if (ALWAYS_EMIT_R2SAVE
9871 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9872 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
9873 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
9874 if (plt_load_toc
9875 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9876 {
9877 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9878 offset = 0;
9879 }
9880 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
9881 if (plt_load_toc)
9882 {
9883 if (use_fake_dep)
9884 {
9885 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
9886 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
9887 }
9888 if (plt_static_chain)
9889 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9890 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9891 }
9892 }
9893 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
9894 {
9895 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
9896 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
9897 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
9898 }
9899 else
9900 bfd_put_32 (obfd, BCTR, p), p += 4;
9901 return p;
9902 }
9903
9904 /* Build a special .plt call stub for __tls_get_addr. */
9905
9906 #define LD_R11_0R3 0xe9630000
9907 #define LD_R12_0R3 0xe9830000
9908 #define MR_R0_R3 0x7c601b78
9909 #define CMPDI_R11_0 0x2c2b0000
9910 #define ADD_R3_R12_R13 0x7c6c6a14
9911 #define BEQLR 0x4d820020
9912 #define MR_R3_R0 0x7c030378
9913 #define STD_R11_0R1 0xf9610000
9914 #define BCTRL 0x4e800421
9915 #define LD_R11_0R1 0xe9610000
9916 #define MTLR_R11 0x7d6803a6
9917
9918 static inline bfd_byte *
9919 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
9920 struct ppc_stub_hash_entry *stub_entry,
9921 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9922 {
9923 bfd *obfd = htab->params->stub_bfd;
9924 bfd_byte *loc = p;
9925
9926 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9927 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9928 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9929 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9930 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9931 bfd_put_32 (obfd, BEQLR, p), p += 4;
9932 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9933 if (r != NULL)
9934 r[0].r_offset += 7 * 4;
9935 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
9936 return build_plt_stub (htab, stub_entry, p, offset, r);
9937
9938 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9939 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
9940
9941 if (r != NULL)
9942 r[0].r_offset += 2 * 4;
9943 p = build_plt_stub (htab, stub_entry, p, offset, r);
9944 bfd_put_32 (obfd, BCTRL, p - 4);
9945
9946 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
9947 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
9948 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9949 bfd_put_32 (obfd, BLR, p), p += 4;
9950
9951 if (htab->glink_eh_frame != NULL
9952 && htab->glink_eh_frame->size != 0)
9953 {
9954 bfd_byte *base, *eh;
9955 unsigned int lr_used, delta;
9956
9957 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
9958 eh = base + stub_entry->group->eh_size;
9959 lr_used = stub_entry->stub_offset + (p - 20 - loc);
9960 delta = lr_used - stub_entry->group->lr_restore;
9961 stub_entry->group->lr_restore = lr_used + 16;
9962 eh = eh_advance (htab->elf.dynobj, eh, delta);
9963 *eh++ = DW_CFA_offset_extended_sf;
9964 *eh++ = 65;
9965 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
9966 *eh++ = DW_CFA_advance_loc + 4;
9967 *eh++ = DW_CFA_restore_extended;
9968 *eh++ = 65;
9969 stub_entry->group->eh_size = eh - base;
9970 }
9971 return p;
9972 }
9973
9974 static Elf_Internal_Rela *
9975 get_relocs (asection *sec, int count)
9976 {
9977 Elf_Internal_Rela *relocs;
9978 struct bfd_elf_section_data *elfsec_data;
9979
9980 elfsec_data = elf_section_data (sec);
9981 relocs = elfsec_data->relocs;
9982 if (relocs == NULL)
9983 {
9984 bfd_size_type relsize;
9985 relsize = sec->reloc_count * sizeof (*relocs);
9986 relocs = bfd_alloc (sec->owner, relsize);
9987 if (relocs == NULL)
9988 return NULL;
9989 elfsec_data->relocs = relocs;
9990 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9991 sizeof (Elf_Internal_Shdr));
9992 if (elfsec_data->rela.hdr == NULL)
9993 return NULL;
9994 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9995 * sizeof (Elf64_External_Rela));
9996 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
9997 sec->reloc_count = 0;
9998 }
9999 relocs += sec->reloc_count;
10000 sec->reloc_count += count;
10001 return relocs;
10002 }
10003
10004 static bfd_vma
10005 get_r2off (struct bfd_link_info *info,
10006 struct ppc_stub_hash_entry *stub_entry)
10007 {
10008 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10009 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10010
10011 if (r2off == 0)
10012 {
10013 /* Support linking -R objects. Get the toc pointer from the
10014 opd entry. */
10015 char buf[8];
10016 if (!htab->opd_abi)
10017 return r2off;
10018 asection *opd = stub_entry->h->elf.root.u.def.section;
10019 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10020
10021 if (strcmp (opd->name, ".opd") != 0
10022 || opd->reloc_count != 0)
10023 {
10024 info->callbacks->einfo
10025 (_("%P: cannot find opd entry toc for `%pT'\n"),
10026 stub_entry->h->elf.root.root.string);
10027 bfd_set_error (bfd_error_bad_value);
10028 return (bfd_vma) -1;
10029 }
10030 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10031 return (bfd_vma) -1;
10032 r2off = bfd_get_64 (opd->owner, buf);
10033 r2off -= elf_gp (info->output_bfd);
10034 }
10035 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10036 return r2off;
10037 }
10038
10039 static bfd_boolean
10040 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10041 {
10042 struct ppc_stub_hash_entry *stub_entry;
10043 struct ppc_branch_hash_entry *br_entry;
10044 struct bfd_link_info *info;
10045 struct ppc_link_hash_table *htab;
10046 bfd_byte *loc;
10047 bfd_byte *p;
10048 bfd_vma targ, off;
10049 Elf_Internal_Rela *r;
10050 asection *plt;
10051
10052 /* Massage our args to the form they really have. */
10053 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10054 info = in_arg;
10055
10056 htab = ppc_hash_table (info);
10057 if (htab == NULL)
10058 return FALSE;
10059
10060 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
10061 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10062
10063 htab->stub_count[stub_entry->stub_type - 1] += 1;
10064 switch (stub_entry->stub_type)
10065 {
10066 case ppc_stub_long_branch:
10067 case ppc_stub_long_branch_r2off:
10068 /* Branches are relative. This is where we are going to. */
10069 targ = (stub_entry->target_value
10070 + stub_entry->target_section->output_offset
10071 + stub_entry->target_section->output_section->vma);
10072 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10073
10074 /* And this is where we are coming from. */
10075 off = (stub_entry->stub_offset
10076 + stub_entry->group->stub_sec->output_offset
10077 + stub_entry->group->stub_sec->output_section->vma);
10078 off = targ - off;
10079
10080 p = loc;
10081 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10082 {
10083 bfd_vma r2off = get_r2off (info, stub_entry);
10084
10085 if (r2off == (bfd_vma) -1)
10086 {
10087 htab->stub_error = TRUE;
10088 return FALSE;
10089 }
10090 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10091 p += 4;
10092 if (PPC_HA (r2off) != 0)
10093 {
10094 bfd_put_32 (htab->params->stub_bfd,
10095 ADDIS_R2_R2 | PPC_HA (r2off), p);
10096 p += 4;
10097 }
10098 if (PPC_LO (r2off) != 0)
10099 {
10100 bfd_put_32 (htab->params->stub_bfd,
10101 ADDI_R2_R2 | PPC_LO (r2off), p);
10102 p += 4;
10103 }
10104 off -= p - loc;
10105 }
10106 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10107 p += 4;
10108
10109 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10110 {
10111 _bfd_error_handler
10112 (_("long branch stub `%s' offset overflow"),
10113 stub_entry->root.string);
10114 htab->stub_error = TRUE;
10115 return FALSE;
10116 }
10117
10118 if (info->emitrelocations)
10119 {
10120 r = get_relocs (stub_entry->group->stub_sec, 1);
10121 if (r == NULL)
10122 return FALSE;
10123 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
10124 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10125 r->r_addend = targ;
10126 if (stub_entry->h != NULL)
10127 {
10128 struct elf_link_hash_entry **hashes;
10129 unsigned long symndx;
10130 struct ppc_link_hash_entry *h;
10131
10132 hashes = elf_sym_hashes (htab->params->stub_bfd);
10133 if (hashes == NULL)
10134 {
10135 bfd_size_type hsize;
10136
10137 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10138 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10139 if (hashes == NULL)
10140 return FALSE;
10141 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10142 htab->stub_globals = 1;
10143 }
10144 symndx = htab->stub_globals++;
10145 h = stub_entry->h;
10146 hashes[symndx] = &h->elf;
10147 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10148 if (h->oh != NULL && h->oh->is_func)
10149 h = ppc_follow_link (h->oh);
10150 if (h->elf.root.u.def.section != stub_entry->target_section)
10151 /* H is an opd symbol. The addend must be zero. */
10152 r->r_addend = 0;
10153 else
10154 {
10155 off = (h->elf.root.u.def.value
10156 + h->elf.root.u.def.section->output_offset
10157 + h->elf.root.u.def.section->output_section->vma);
10158 r->r_addend -= off;
10159 }
10160 }
10161 }
10162 break;
10163
10164 case ppc_stub_plt_branch:
10165 case ppc_stub_plt_branch_r2off:
10166 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10167 stub_entry->root.string + 9,
10168 FALSE, FALSE);
10169 if (br_entry == NULL)
10170 {
10171 _bfd_error_handler (_("can't find branch stub `%s'"),
10172 stub_entry->root.string);
10173 htab->stub_error = TRUE;
10174 return FALSE;
10175 }
10176
10177 targ = (stub_entry->target_value
10178 + stub_entry->target_section->output_offset
10179 + stub_entry->target_section->output_section->vma);
10180 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10181 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10182
10183 bfd_put_64 (htab->brlt->owner, targ,
10184 htab->brlt->contents + br_entry->offset);
10185
10186 if (br_entry->iter == htab->stub_iteration)
10187 {
10188 br_entry->iter = 0;
10189
10190 if (htab->relbrlt != NULL)
10191 {
10192 /* Create a reloc for the branch lookup table entry. */
10193 Elf_Internal_Rela rela;
10194 bfd_byte *rl;
10195
10196 rela.r_offset = (br_entry->offset
10197 + htab->brlt->output_offset
10198 + htab->brlt->output_section->vma);
10199 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10200 rela.r_addend = targ;
10201
10202 rl = htab->relbrlt->contents;
10203 rl += (htab->relbrlt->reloc_count++
10204 * sizeof (Elf64_External_Rela));
10205 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10206 }
10207 else if (info->emitrelocations)
10208 {
10209 r = get_relocs (htab->brlt, 1);
10210 if (r == NULL)
10211 return FALSE;
10212 /* brlt, being SEC_LINKER_CREATED does not go through the
10213 normal reloc processing. Symbols and offsets are not
10214 translated from input file to output file form, so
10215 set up the offset per the output file. */
10216 r->r_offset = (br_entry->offset
10217 + htab->brlt->output_offset
10218 + htab->brlt->output_section->vma);
10219 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10220 r->r_addend = targ;
10221 }
10222 }
10223
10224 targ = (br_entry->offset
10225 + htab->brlt->output_offset
10226 + htab->brlt->output_section->vma);
10227
10228 off = (elf_gp (info->output_bfd)
10229 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10230 off = targ - off;
10231
10232 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10233 {
10234 info->callbacks->einfo
10235 (_("%P: linkage table error against `%pT'\n"),
10236 stub_entry->root.string);
10237 bfd_set_error (bfd_error_bad_value);
10238 htab->stub_error = TRUE;
10239 return FALSE;
10240 }
10241
10242 if (info->emitrelocations)
10243 {
10244 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
10245 if (r == NULL)
10246 return FALSE;
10247 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10248 if (bfd_big_endian (info->output_bfd))
10249 r[0].r_offset += 2;
10250 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10251 r[0].r_offset += 4;
10252 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10253 r[0].r_addend = targ;
10254 if (PPC_HA (off) != 0)
10255 {
10256 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10257 r[1].r_offset = r[0].r_offset + 4;
10258 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10259 r[1].r_addend = r[0].r_addend;
10260 }
10261 }
10262
10263 p = loc;
10264 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10265 {
10266 if (PPC_HA (off) != 0)
10267 {
10268 bfd_put_32 (htab->params->stub_bfd,
10269 ADDIS_R12_R2 | PPC_HA (off), p);
10270 p += 4;
10271 bfd_put_32 (htab->params->stub_bfd,
10272 LD_R12_0R12 | PPC_LO (off), p);
10273 }
10274 else
10275 bfd_put_32 (htab->params->stub_bfd,
10276 LD_R12_0R2 | PPC_LO (off), p);
10277 }
10278 else
10279 {
10280 bfd_vma r2off = get_r2off (info, stub_entry);
10281
10282 if (r2off == (bfd_vma) -1)
10283 {
10284 htab->stub_error = TRUE;
10285 return FALSE;
10286 }
10287
10288 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10289 p += 4;
10290 if (PPC_HA (off) != 0)
10291 {
10292 bfd_put_32 (htab->params->stub_bfd,
10293 ADDIS_R12_R2 | PPC_HA (off), p);
10294 p += 4;
10295 bfd_put_32 (htab->params->stub_bfd,
10296 LD_R12_0R12 | PPC_LO (off), p);
10297 }
10298 else
10299 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
10300
10301 if (PPC_HA (r2off) != 0)
10302 {
10303 p += 4;
10304 bfd_put_32 (htab->params->stub_bfd,
10305 ADDIS_R2_R2 | PPC_HA (r2off), p);
10306 }
10307 if (PPC_LO (r2off) != 0)
10308 {
10309 p += 4;
10310 bfd_put_32 (htab->params->stub_bfd,
10311 ADDI_R2_R2 | PPC_LO (r2off), p);
10312 }
10313 }
10314 p += 4;
10315 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10316 p += 4;
10317 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10318 p += 4;
10319 break;
10320
10321 case ppc_stub_long_branch_notoc:
10322 case ppc_stub_long_branch_both:
10323 case ppc_stub_plt_branch_notoc:
10324 case ppc_stub_plt_branch_both:
10325 case ppc_stub_plt_call_notoc:
10326 case ppc_stub_plt_call_both:
10327 p = loc;
10328 off = (8 + stub_entry->stub_offset
10329 + stub_entry->group->stub_sec->output_offset
10330 + stub_entry->group->stub_sec->output_section->vma);
10331 if (stub_entry->stub_type == ppc_stub_long_branch_both
10332 || stub_entry->stub_type == ppc_stub_plt_branch_both
10333 || stub_entry->stub_type == ppc_stub_plt_call_both)
10334 {
10335 off += 4;
10336 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10337 p += 4;
10338 }
10339 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10340 {
10341 targ = stub_entry->plt_ent->plt.offset & ~1;
10342 if (targ >= (bfd_vma) -2)
10343 abort ();
10344
10345 plt = htab->elf.splt;
10346 if (!htab->elf.dynamic_sections_created
10347 || stub_entry->h == NULL
10348 || stub_entry->h->elf.dynindx == -1)
10349 {
10350 if (stub_entry->symtype == STT_GNU_IFUNC)
10351 plt = htab->elf.iplt;
10352 else
10353 plt = htab->pltlocal;
10354 }
10355 targ += plt->output_offset + plt->output_section->vma;
10356 }
10357 else
10358 targ = (stub_entry->target_value
10359 + stub_entry->target_section->output_offset
10360 + stub_entry->target_section->output_section->vma);
10361 off = targ - off;
10362 bfd_put_32 (htab->params->stub_bfd, MFLR_R12, p);
10363 p += 4;
10364 bfd_put_32 (htab->params->stub_bfd, BCL_20_31, p);
10365 p += 4;
10366 bfd_put_32 (htab->params->stub_bfd, MFLR_R11, p);
10367 p += 4;
10368 bfd_put_32 (htab->params->stub_bfd, MTLR_R12, p);
10369 p += 4;
10370 p = build_offset (htab->params->stub_bfd, p, off,
10371 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
10372 if (stub_entry->stub_type == ppc_stub_long_branch_notoc)
10373 {
10374 off += 8;
10375 bfd_put_32 (htab->params->stub_bfd,
10376 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
10377 }
10378 else if (stub_entry->stub_type == ppc_stub_long_branch_both)
10379 {
10380 off += 12;
10381 bfd_put_32 (htab->params->stub_bfd,
10382 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
10383 }
10384 else
10385 {
10386 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10387 p += 4;
10388 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10389 }
10390 p += 4;
10391
10392 if (htab->glink_eh_frame != NULL
10393 && htab->glink_eh_frame->size != 0)
10394 {
10395 bfd_byte *base, *eh;
10396 unsigned int lr_used, delta;
10397
10398 base = (htab->glink_eh_frame->contents
10399 + stub_entry->group->eh_base + 17);
10400 eh = base + stub_entry->group->eh_size;
10401 lr_used = stub_entry->stub_offset + 8;
10402 if (stub_entry->stub_type == ppc_stub_long_branch_both
10403 || stub_entry->stub_type == ppc_stub_plt_branch_both
10404 || stub_entry->stub_type == ppc_stub_plt_call_both)
10405 lr_used += 4;
10406 delta = lr_used - stub_entry->group->lr_restore;
10407 stub_entry->group->lr_restore = lr_used + 8;
10408 eh = eh_advance (htab->elf.dynobj, eh, delta);
10409 *eh++ = DW_CFA_register;
10410 *eh++ = 65;
10411 *eh++ = 12;
10412 *eh++ = DW_CFA_advance_loc + 2;
10413 *eh++ = DW_CFA_restore_extended;
10414 *eh++ = 65;
10415 stub_entry->group->eh_size = eh - base;
10416 }
10417 break;
10418
10419 case ppc_stub_plt_call:
10420 case ppc_stub_plt_call_r2save:
10421 if (stub_entry->h != NULL
10422 && stub_entry->h->is_func_descriptor
10423 && stub_entry->h->oh != NULL)
10424 {
10425 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10426
10427 /* If the old-ABI "dot-symbol" is undefined make it weak so
10428 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
10429 if (fh->elf.root.type == bfd_link_hash_undefined
10430 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
10431 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
10432 fh->elf.root.type = bfd_link_hash_undefweak;
10433 }
10434
10435 /* Now build the stub. */
10436 targ = stub_entry->plt_ent->plt.offset & ~1;
10437 if (targ >= (bfd_vma) -2)
10438 abort ();
10439
10440 plt = htab->elf.splt;
10441 if (!htab->elf.dynamic_sections_created
10442 || stub_entry->h == NULL
10443 || stub_entry->h->elf.dynindx == -1)
10444 {
10445 if (stub_entry->symtype == STT_GNU_IFUNC)
10446 plt = htab->elf.iplt;
10447 else
10448 plt = htab->pltlocal;
10449 }
10450 targ += plt->output_offset + plt->output_section->vma;
10451
10452 off = (elf_gp (info->output_bfd)
10453 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10454 off = targ - off;
10455
10456 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10457 {
10458 info->callbacks->einfo
10459 /* xgettext:c-format */
10460 (_("%P: linkage table error against `%pT'\n"),
10461 stub_entry->h != NULL
10462 ? stub_entry->h->elf.root.root.string
10463 : "<local sym>");
10464 bfd_set_error (bfd_error_bad_value);
10465 htab->stub_error = TRUE;
10466 return FALSE;
10467 }
10468
10469 r = NULL;
10470 if (info->emitrelocations)
10471 {
10472 r = get_relocs (stub_entry->group->stub_sec,
10473 ((PPC_HA (off) != 0)
10474 + (htab->opd_abi
10475 ? 2 + (htab->params->plt_static_chain
10476 && PPC_HA (off + 16) == PPC_HA (off))
10477 : 1)));
10478 if (r == NULL)
10479 return FALSE;
10480 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10481 if (bfd_big_endian (info->output_bfd))
10482 r[0].r_offset += 2;
10483 r[0].r_addend = targ;
10484 }
10485 if (stub_entry->h != NULL
10486 && (stub_entry->h == htab->tls_get_addr_fd
10487 || stub_entry->h == htab->tls_get_addr)
10488 && htab->params->tls_get_addr_opt)
10489 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10490 else
10491 p = build_plt_stub (htab, stub_entry, loc, off, r);
10492 break;
10493
10494 case ppc_stub_save_res:
10495 return TRUE;
10496
10497 default:
10498 BFD_FAIL ();
10499 return FALSE;
10500 }
10501
10502 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
10503
10504 if (htab->params->emit_stub_syms)
10505 {
10506 struct elf_link_hash_entry *h;
10507 size_t len1, len2;
10508 char *name;
10509 const char *const stub_str[] = { "long_branch",
10510 "long_branch",
10511 "long_branch",
10512 "long_branch",
10513 "plt_branch",
10514 "plt_branch",
10515 "plt_branch",
10516 "plt_branch",
10517 "plt_call",
10518 "plt_call",
10519 "plt_call",
10520 "plt_call" };
10521
10522 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10523 len2 = strlen (stub_entry->root.string);
10524 name = bfd_malloc (len1 + len2 + 2);
10525 if (name == NULL)
10526 return FALSE;
10527 memcpy (name, stub_entry->root.string, 9);
10528 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10529 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10530 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10531 if (h == NULL)
10532 return FALSE;
10533 if (h->root.type == bfd_link_hash_new)
10534 {
10535 h->root.type = bfd_link_hash_defined;
10536 h->root.u.def.section = stub_entry->group->stub_sec;
10537 h->root.u.def.value = stub_entry->stub_offset;
10538 h->ref_regular = 1;
10539 h->def_regular = 1;
10540 h->ref_regular_nonweak = 1;
10541 h->forced_local = 1;
10542 h->non_elf = 0;
10543 h->root.linker_def = 1;
10544 }
10545 }
10546
10547 return TRUE;
10548 }
10549
10550 /* As above, but don't actually build the stub. Just bump offset so
10551 we know stub section sizes, and select plt_branch stubs where
10552 long_branch stubs won't do. */
10553
10554 static bfd_boolean
10555 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10556 {
10557 struct ppc_stub_hash_entry *stub_entry;
10558 struct bfd_link_info *info;
10559 struct ppc_link_hash_table *htab;
10560 bfd_vma targ, off;
10561 int size;
10562
10563 /* Massage our args to the form they really have. */
10564 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10565 info = in_arg;
10566
10567 htab = ppc_hash_table (info);
10568 if (htab == NULL)
10569 return FALSE;
10570
10571 /* Make a note of the offset within the stubs for this entry. */
10572 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10573
10574 if (stub_entry->h != NULL
10575 && stub_entry->h->save_res
10576 && stub_entry->h->elf.root.type == bfd_link_hash_defined
10577 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
10578 {
10579 /* Don't make stubs to out-of-line register save/restore
10580 functions. Instead, emit copies of the functions. */
10581 stub_entry->group->needs_save_res = 1;
10582 stub_entry->stub_type = ppc_stub_save_res;
10583 return TRUE;
10584 }
10585
10586 if (stub_entry->stub_type >= ppc_stub_plt_call
10587 && stub_entry->stub_type <= ppc_stub_plt_call_both)
10588 {
10589 asection *plt;
10590 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
10591 if (targ >= (bfd_vma) -2)
10592 abort ();
10593 plt = htab->elf.splt;
10594 if (!htab->elf.dynamic_sections_created
10595 || stub_entry->h == NULL
10596 || stub_entry->h->elf.dynindx == -1)
10597 {
10598 if (stub_entry->symtype == STT_GNU_IFUNC)
10599 plt = htab->elf.iplt;
10600 else
10601 plt = htab->pltlocal;
10602 }
10603 targ += plt->output_offset + plt->output_section->vma;
10604
10605 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10606 {
10607 off = (8 + stub_entry->stub_offset
10608 + stub_entry->group->stub_sec->output_offset
10609 + stub_entry->group->stub_sec->output_section->vma);
10610 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10611 off += 4;
10612 }
10613 else
10614 off = (elf_gp (info->output_bfd)
10615 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10616
10617 if (htab->params->plt_stub_align != 0)
10618 {
10619 unsigned pad = plt_stub_pad (htab, stub_entry, targ - off);
10620
10621 stub_entry->group->stub_sec->size += pad;
10622 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10623 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10624 off += pad;
10625 }
10626
10627 off = targ - off;
10628 size = plt_stub_size (htab, stub_entry, off);
10629
10630 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10631 {
10632 /* After the bcl, lr has been modified so we need to emit
10633 .eh_frame info saying the return address is in r12. */
10634 unsigned int lr_used = stub_entry->stub_offset + 8;
10635 unsigned int delta;
10636 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10637 lr_used += 4;
10638 /* The eh_frame info will consist of a DW_CFA_advance_loc or
10639 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
10640 DW_CFA_restore_extended 65. */
10641 delta = lr_used - stub_entry->group->lr_restore;
10642 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10643 stub_entry->group->lr_restore = lr_used + 8;
10644 }
10645 else
10646 {
10647 if (stub_entry->h != NULL
10648 && (stub_entry->h == htab->tls_get_addr_fd
10649 || stub_entry->h == htab->tls_get_addr)
10650 && htab->params->tls_get_addr_opt
10651 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
10652 {
10653 /* After the bctrl, lr has been modified so we need to
10654 emit .eh_frame info saying the return address is
10655 on the stack. In fact we put the EH info specifying
10656 that the return address is on the stack *at* the
10657 call rather than after it, because the EH info for a
10658 call needs to be specified by that point.
10659 See libgcc/unwind-dw2.c execute_cfa_program. */
10660 unsigned int lr_used = stub_entry->stub_offset + size - 20;
10661 unsigned int delta;
10662 /* The eh_frame info will consist of a DW_CFA_advance_loc
10663 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
10664 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
10665 delta = lr_used - stub_entry->group->lr_restore;
10666 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10667 stub_entry->group->lr_restore = size - 4;
10668 }
10669
10670 if (info->emitrelocations)
10671 {
10672 stub_entry->group->stub_sec->reloc_count
10673 += ((PPC_HA (off) != 0)
10674 + (htab->opd_abi
10675 ? 2 + (htab->params->plt_static_chain
10676 && PPC_HA (off + 16) == PPC_HA (off))
10677 : 1));
10678 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10679 }
10680 }
10681 }
10682 else
10683 {
10684 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10685 variants. */
10686 bfd_vma r2off = 0;
10687 bfd_vma local_off = 0;
10688
10689 targ = (stub_entry->target_value
10690 + stub_entry->target_section->output_offset
10691 + stub_entry->target_section->output_section->vma);
10692 off = (stub_entry->stub_offset
10693 + stub_entry->group->stub_sec->output_offset
10694 + stub_entry->group->stub_sec->output_section->vma);
10695
10696 /* Reset the stub type from the plt variant in case we now
10697 can reach with a shorter stub. */
10698 if (stub_entry->stub_type >= ppc_stub_plt_branch)
10699 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10700
10701 size = 4;
10702 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10703 {
10704 r2off = get_r2off (info, stub_entry);
10705 if (r2off == (bfd_vma) -1)
10706 {
10707 htab->stub_error = TRUE;
10708 return FALSE;
10709 }
10710 size = 8;
10711 if (PPC_HA (r2off) != 0)
10712 size += 4;
10713 if (PPC_LO (r2off) != 0)
10714 size += 4;
10715 off += size - 4;
10716 }
10717 else if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
10718 {
10719 size = 20 + size_offset (targ - (off + 8));
10720 if (stub_entry->stub_type > ppc_stub_long_branch_notoc)
10721 size += 4;
10722 off += size - 4;
10723 }
10724 off = targ - off;
10725
10726 if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
10727 {
10728 /* After the bcl, lr has been modified so we need to emit
10729 .eh_frame info saying the return address is in r12. */
10730 unsigned int lr_used = stub_entry->stub_offset + 8;
10731 unsigned int delta;
10732 if (stub_entry->stub_type > ppc_stub_long_branch_notoc)
10733 lr_used += 4;
10734 /* The eh_frame info will consist of a DW_CFA_advance_loc or
10735 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
10736 DW_CFA_restore_extended 65. */
10737 delta = lr_used - stub_entry->group->lr_restore;
10738 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10739 stub_entry->group->lr_restore = lr_used + 8;
10740
10741 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10742 {
10743 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
10744 - ppc_stub_long_branch_notoc);
10745 size += 4;
10746 }
10747 }
10748 else
10749 {
10750 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10751
10752 /* If the branch offset is too big, use a ppc_stub_plt_branch.
10753 Do the same for -R objects without function descriptors. */
10754 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
10755 && r2off == 0
10756 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
10757 || off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off)
10758 {
10759 struct ppc_branch_hash_entry *br_entry;
10760
10761 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10762 stub_entry->root.string + 9,
10763 TRUE, FALSE);
10764 if (br_entry == NULL)
10765 {
10766 _bfd_error_handler (_("can't build branch stub `%s'"),
10767 stub_entry->root.string);
10768 htab->stub_error = TRUE;
10769 return FALSE;
10770 }
10771
10772 if (br_entry->iter != htab->stub_iteration)
10773 {
10774 br_entry->iter = htab->stub_iteration;
10775 br_entry->offset = htab->brlt->size;
10776 htab->brlt->size += 8;
10777
10778 if (htab->relbrlt != NULL)
10779 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10780 else if (info->emitrelocations)
10781 {
10782 htab->brlt->reloc_count += 1;
10783 htab->brlt->flags |= SEC_RELOC;
10784 }
10785 }
10786
10787 targ = (br_entry->offset
10788 + htab->brlt->output_offset
10789 + htab->brlt->output_section->vma);
10790 off = (elf_gp (info->output_bfd)
10791 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10792 off = targ - off;
10793
10794 if (info->emitrelocations)
10795 {
10796 stub_entry->group->stub_sec->reloc_count
10797 += 1 + (PPC_HA (off) != 0);
10798 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10799 }
10800
10801 stub_entry->stub_type
10802 += ppc_stub_plt_branch - ppc_stub_long_branch;
10803 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10804 {
10805 size = 12;
10806 if (PPC_HA (off) != 0)
10807 size = 16;
10808 }
10809 else
10810 {
10811 size = 16;
10812 if (PPC_HA (off) != 0)
10813 size += 4;
10814
10815 if (PPC_HA (r2off) != 0)
10816 size += 4;
10817 if (PPC_LO (r2off) != 0)
10818 size += 4;
10819 }
10820 }
10821 else if (info->emitrelocations)
10822 {
10823 stub_entry->group->stub_sec->reloc_count += 1;
10824 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10825 }
10826 }
10827 }
10828
10829 stub_entry->group->stub_sec->size += size;
10830 return TRUE;
10831 }
10832
10833 /* Set up various things so that we can make a list of input sections
10834 for each output section included in the link. Returns -1 on error,
10835 0 when no stubs will be needed, and 1 on success. */
10836
10837 int
10838 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
10839 {
10840 unsigned int id;
10841 bfd_size_type amt;
10842 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10843
10844 if (htab == NULL)
10845 return -1;
10846
10847 htab->sec_info_arr_size = _bfd_section_id;
10848 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
10849 htab->sec_info = bfd_zmalloc (amt);
10850 if (htab->sec_info == NULL)
10851 return -1;
10852
10853 /* Set toc_off for com, und, abs and ind sections. */
10854 for (id = 0; id < 3; id++)
10855 htab->sec_info[id].toc_off = TOC_BASE_OFF;
10856
10857 return 1;
10858 }
10859
10860 /* Set up for first pass at multitoc partitioning. */
10861
10862 void
10863 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10864 {
10865 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10866
10867 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
10868 htab->toc_bfd = NULL;
10869 htab->toc_first_sec = NULL;
10870 }
10871
10872 /* The linker repeatedly calls this function for each TOC input section
10873 and linker generated GOT section. Group input bfds such that the toc
10874 within a group is less than 64k in size. */
10875
10876 bfd_boolean
10877 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10878 {
10879 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10880 bfd_vma addr, off, limit;
10881
10882 if (htab == NULL)
10883 return FALSE;
10884
10885 if (!htab->second_toc_pass)
10886 {
10887 /* Keep track of the first .toc or .got section for this input bfd. */
10888 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
10889
10890 if (new_bfd)
10891 {
10892 htab->toc_bfd = isec->owner;
10893 htab->toc_first_sec = isec;
10894 }
10895
10896 addr = isec->output_offset + isec->output_section->vma;
10897 off = addr - htab->toc_curr;
10898 limit = 0x80008000;
10899 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10900 limit = 0x10000;
10901 if (off + isec->size > limit)
10902 {
10903 addr = (htab->toc_first_sec->output_offset
10904 + htab->toc_first_sec->output_section->vma);
10905 htab->toc_curr = addr;
10906 htab->toc_curr &= -TOC_BASE_ALIGN;
10907 }
10908
10909 /* toc_curr is the base address of this toc group. Set elf_gp
10910 for the input section to be the offset relative to the
10911 output toc base plus 0x8000. Making the input elf_gp an
10912 offset allows us to move the toc as a whole without
10913 recalculating input elf_gp. */
10914 off = htab->toc_curr - elf_gp (info->output_bfd);
10915 off += TOC_BASE_OFF;
10916
10917 /* Die if someone uses a linker script that doesn't keep input
10918 file .toc and .got together. */
10919 if (new_bfd
10920 && elf_gp (isec->owner) != 0
10921 && elf_gp (isec->owner) != off)
10922 return FALSE;
10923
10924 elf_gp (isec->owner) = off;
10925 return TRUE;
10926 }
10927
10928 /* During the second pass toc_first_sec points to the start of
10929 a toc group, and toc_curr is used to track the old elf_gp.
10930 We use toc_bfd to ensure we only look at each bfd once. */
10931 if (htab->toc_bfd == isec->owner)
10932 return TRUE;
10933 htab->toc_bfd = isec->owner;
10934
10935 if (htab->toc_first_sec == NULL
10936 || htab->toc_curr != elf_gp (isec->owner))
10937 {
10938 htab->toc_curr = elf_gp (isec->owner);
10939 htab->toc_first_sec = isec;
10940 }
10941 addr = (htab->toc_first_sec->output_offset
10942 + htab->toc_first_sec->output_section->vma);
10943 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
10944 elf_gp (isec->owner) = off;
10945
10946 return TRUE;
10947 }
10948
10949 /* Called via elf_link_hash_traverse to merge GOT entries for global
10950 symbol H. */
10951
10952 static bfd_boolean
10953 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10954 {
10955 if (h->root.type == bfd_link_hash_indirect)
10956 return TRUE;
10957
10958 merge_got_entries (&h->got.glist);
10959
10960 return TRUE;
10961 }
10962
10963 /* Called via elf_link_hash_traverse to allocate GOT entries for global
10964 symbol H. */
10965
10966 static bfd_boolean
10967 reallocate_got (struct elf_link_hash_entry *h, void *inf)
10968 {
10969 struct got_entry *gent;
10970
10971 if (h->root.type == bfd_link_hash_indirect)
10972 return TRUE;
10973
10974 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10975 if (!gent->is_indirect)
10976 allocate_got (h, (struct bfd_link_info *) inf, gent);
10977 return TRUE;
10978 }
10979
10980 /* Called on the first multitoc pass after the last call to
10981 ppc64_elf_next_toc_section. This function removes duplicate GOT
10982 entries. */
10983
10984 bfd_boolean
10985 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
10986 {
10987 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10988 struct bfd *ibfd, *ibfd2;
10989 bfd_boolean done_something;
10990
10991 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
10992
10993 if (!htab->do_multi_toc)
10994 return FALSE;
10995
10996 /* Merge global sym got entries within a toc group. */
10997 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10998
10999 /* And tlsld_got. */
11000 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11001 {
11002 struct got_entry *ent, *ent2;
11003
11004 if (!is_ppc64_elf (ibfd))
11005 continue;
11006
11007 ent = ppc64_tlsld_got (ibfd);
11008 if (!ent->is_indirect
11009 && ent->got.offset != (bfd_vma) -1)
11010 {
11011 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11012 {
11013 if (!is_ppc64_elf (ibfd2))
11014 continue;
11015
11016 ent2 = ppc64_tlsld_got (ibfd2);
11017 if (!ent2->is_indirect
11018 && ent2->got.offset != (bfd_vma) -1
11019 && elf_gp (ibfd2) == elf_gp (ibfd))
11020 {
11021 ent2->is_indirect = TRUE;
11022 ent2->got.ent = ent;
11023 }
11024 }
11025 }
11026 }
11027
11028 /* Zap sizes of got sections. */
11029 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11030 htab->elf.irelplt->size -= htab->got_reli_size;
11031 htab->got_reli_size = 0;
11032
11033 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11034 {
11035 asection *got, *relgot;
11036
11037 if (!is_ppc64_elf (ibfd))
11038 continue;
11039
11040 got = ppc64_elf_tdata (ibfd)->got;
11041 if (got != NULL)
11042 {
11043 got->rawsize = got->size;
11044 got->size = 0;
11045 relgot = ppc64_elf_tdata (ibfd)->relgot;
11046 relgot->rawsize = relgot->size;
11047 relgot->size = 0;
11048 }
11049 }
11050
11051 /* Now reallocate the got, local syms first. We don't need to
11052 allocate section contents again since we never increase size. */
11053 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11054 {
11055 struct got_entry **lgot_ents;
11056 struct got_entry **end_lgot_ents;
11057 struct plt_entry **local_plt;
11058 struct plt_entry **end_local_plt;
11059 unsigned char *lgot_masks;
11060 bfd_size_type locsymcount;
11061 Elf_Internal_Shdr *symtab_hdr;
11062 asection *s;
11063
11064 if (!is_ppc64_elf (ibfd))
11065 continue;
11066
11067 lgot_ents = elf_local_got_ents (ibfd);
11068 if (!lgot_ents)
11069 continue;
11070
11071 symtab_hdr = &elf_symtab_hdr (ibfd);
11072 locsymcount = symtab_hdr->sh_info;
11073 end_lgot_ents = lgot_ents + locsymcount;
11074 local_plt = (struct plt_entry **) end_lgot_ents;
11075 end_local_plt = local_plt + locsymcount;
11076 lgot_masks = (unsigned char *) end_local_plt;
11077 s = ppc64_elf_tdata (ibfd)->got;
11078 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11079 {
11080 struct got_entry *ent;
11081
11082 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11083 {
11084 unsigned int ent_size = 8;
11085 unsigned int rel_size = sizeof (Elf64_External_Rela);
11086
11087 ent->got.offset = s->size;
11088 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11089 {
11090 ent_size *= 2;
11091 rel_size *= 2;
11092 }
11093 s->size += ent_size;
11094 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
11095 {
11096 htab->elf.irelplt->size += rel_size;
11097 htab->got_reli_size += rel_size;
11098 }
11099 else if (bfd_link_pic (info)
11100 && !((ent->tls_type & TLS_TPREL) != 0
11101 && bfd_link_executable (info)))
11102 {
11103 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11104 srel->size += rel_size;
11105 }
11106 }
11107 }
11108 }
11109
11110 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11111
11112 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11113 {
11114 struct got_entry *ent;
11115
11116 if (!is_ppc64_elf (ibfd))
11117 continue;
11118
11119 ent = ppc64_tlsld_got (ibfd);
11120 if (!ent->is_indirect
11121 && ent->got.offset != (bfd_vma) -1)
11122 {
11123 asection *s = ppc64_elf_tdata (ibfd)->got;
11124 ent->got.offset = s->size;
11125 s->size += 16;
11126 if (bfd_link_pic (info))
11127 {
11128 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11129 srel->size += sizeof (Elf64_External_Rela);
11130 }
11131 }
11132 }
11133
11134 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11135 if (!done_something)
11136 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11137 {
11138 asection *got;
11139
11140 if (!is_ppc64_elf (ibfd))
11141 continue;
11142
11143 got = ppc64_elf_tdata (ibfd)->got;
11144 if (got != NULL)
11145 {
11146 done_something = got->rawsize != got->size;
11147 if (done_something)
11148 break;
11149 }
11150 }
11151
11152 if (done_something)
11153 (*htab->params->layout_sections_again) ();
11154
11155 /* Set up for second pass over toc sections to recalculate elf_gp
11156 on input sections. */
11157 htab->toc_bfd = NULL;
11158 htab->toc_first_sec = NULL;
11159 htab->second_toc_pass = TRUE;
11160 return done_something;
11161 }
11162
11163 /* Called after second pass of multitoc partitioning. */
11164
11165 void
11166 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11167 {
11168 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11169
11170 /* After the second pass, toc_curr tracks the TOC offset used
11171 for code sections below in ppc64_elf_next_input_section. */
11172 htab->toc_curr = TOC_BASE_OFF;
11173 }
11174
11175 /* No toc references were found in ISEC. If the code in ISEC makes no
11176 calls, then there's no need to use toc adjusting stubs when branching
11177 into ISEC. Actually, indirect calls from ISEC are OK as they will
11178 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11179 needed, and 2 if a cyclical call-graph was found but no other reason
11180 for a stub was detected. If called from the top level, a return of
11181 2 means the same as a return of 0. */
11182
11183 static int
11184 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11185 {
11186 int ret;
11187
11188 /* Mark this section as checked. */
11189 isec->call_check_done = 1;
11190
11191 /* We know none of our code bearing sections will need toc stubs. */
11192 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11193 return 0;
11194
11195 if (isec->size == 0)
11196 return 0;
11197
11198 if (isec->output_section == NULL)
11199 return 0;
11200
11201 ret = 0;
11202 if (isec->reloc_count != 0)
11203 {
11204 Elf_Internal_Rela *relstart, *rel;
11205 Elf_Internal_Sym *local_syms;
11206 struct ppc_link_hash_table *htab;
11207
11208 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11209 info->keep_memory);
11210 if (relstart == NULL)
11211 return -1;
11212
11213 /* Look for branches to outside of this section. */
11214 local_syms = NULL;
11215 htab = ppc_hash_table (info);
11216 if (htab == NULL)
11217 return -1;
11218
11219 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11220 {
11221 enum elf_ppc64_reloc_type r_type;
11222 unsigned long r_symndx;
11223 struct elf_link_hash_entry *h;
11224 struct ppc_link_hash_entry *eh;
11225 Elf_Internal_Sym *sym;
11226 asection *sym_sec;
11227 struct _opd_sec_data *opd;
11228 bfd_vma sym_value;
11229 bfd_vma dest;
11230
11231 r_type = ELF64_R_TYPE (rel->r_info);
11232 if (r_type != R_PPC64_REL24
11233 && r_type != R_PPC64_REL24_NOTOC
11234 && r_type != R_PPC64_REL14
11235 && r_type != R_PPC64_REL14_BRTAKEN
11236 && r_type != R_PPC64_REL14_BRNTAKEN
11237 && r_type != R_PPC64_PLTCALL)
11238 continue;
11239
11240 r_symndx = ELF64_R_SYM (rel->r_info);
11241 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11242 isec->owner))
11243 {
11244 ret = -1;
11245 break;
11246 }
11247
11248 /* Calls to dynamic lib functions go through a plt call stub
11249 that uses r2. */
11250 eh = (struct ppc_link_hash_entry *) h;
11251 if (eh != NULL
11252 && (eh->elf.plt.plist != NULL
11253 || (eh->oh != NULL
11254 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11255 {
11256 ret = 1;
11257 break;
11258 }
11259
11260 if (sym_sec == NULL)
11261 /* Ignore other undefined symbols. */
11262 continue;
11263
11264 /* Assume branches to other sections not included in the
11265 link need stubs too, to cover -R and absolute syms. */
11266 if (sym_sec->output_section == NULL)
11267 {
11268 ret = 1;
11269 break;
11270 }
11271
11272 if (h == NULL)
11273 sym_value = sym->st_value;
11274 else
11275 {
11276 if (h->root.type != bfd_link_hash_defined
11277 && h->root.type != bfd_link_hash_defweak)
11278 abort ();
11279 sym_value = h->root.u.def.value;
11280 }
11281 sym_value += rel->r_addend;
11282
11283 /* If this branch reloc uses an opd sym, find the code section. */
11284 opd = get_opd_info (sym_sec);
11285 if (opd != NULL)
11286 {
11287 if (h == NULL && opd->adjust != NULL)
11288 {
11289 long adjust;
11290
11291 adjust = opd->adjust[OPD_NDX (sym_value)];
11292 if (adjust == -1)
11293 /* Assume deleted functions won't ever be called. */
11294 continue;
11295 sym_value += adjust;
11296 }
11297
11298 dest = opd_entry_value (sym_sec, sym_value,
11299 &sym_sec, NULL, FALSE);
11300 if (dest == (bfd_vma) -1)
11301 continue;
11302 }
11303 else
11304 dest = (sym_value
11305 + sym_sec->output_offset
11306 + sym_sec->output_section->vma);
11307
11308 /* Ignore branch to self. */
11309 if (sym_sec == isec)
11310 continue;
11311
11312 /* If the called function uses the toc, we need a stub. */
11313 if (sym_sec->has_toc_reloc
11314 || sym_sec->makes_toc_func_call)
11315 {
11316 ret = 1;
11317 break;
11318 }
11319
11320 /* Assume any branch that needs a long branch stub might in fact
11321 need a plt_branch stub. A plt_branch stub uses r2. */
11322 else if (dest - (isec->output_offset
11323 + isec->output_section->vma
11324 + rel->r_offset) + (1 << 25)
11325 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11326 ? h->other
11327 : sym->st_other))
11328 {
11329 ret = 1;
11330 break;
11331 }
11332
11333 /* If calling back to a section in the process of being
11334 tested, we can't say for sure that no toc adjusting stubs
11335 are needed, so don't return zero. */
11336 else if (sym_sec->call_check_in_progress)
11337 ret = 2;
11338
11339 /* Branches to another section that itself doesn't have any TOC
11340 references are OK. Recursively call ourselves to check. */
11341 else if (!sym_sec->call_check_done)
11342 {
11343 int recur;
11344
11345 /* Mark current section as indeterminate, so that other
11346 sections that call back to current won't be marked as
11347 known. */
11348 isec->call_check_in_progress = 1;
11349 recur = toc_adjusting_stub_needed (info, sym_sec);
11350 isec->call_check_in_progress = 0;
11351
11352 if (recur != 0)
11353 {
11354 ret = recur;
11355 if (recur != 2)
11356 break;
11357 }
11358 }
11359 }
11360
11361 if (local_syms != NULL
11362 && (elf_symtab_hdr (isec->owner).contents
11363 != (unsigned char *) local_syms))
11364 free (local_syms);
11365 if (elf_section_data (isec)->relocs != relstart)
11366 free (relstart);
11367 }
11368
11369 if ((ret & 1) == 0
11370 && isec->map_head.s != NULL
11371 && (strcmp (isec->output_section->name, ".init") == 0
11372 || strcmp (isec->output_section->name, ".fini") == 0))
11373 {
11374 if (isec->map_head.s->has_toc_reloc
11375 || isec->map_head.s->makes_toc_func_call)
11376 ret = 1;
11377 else if (!isec->map_head.s->call_check_done)
11378 {
11379 int recur;
11380 isec->call_check_in_progress = 1;
11381 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11382 isec->call_check_in_progress = 0;
11383 if (recur != 0)
11384 ret = recur;
11385 }
11386 }
11387
11388 if (ret == 1)
11389 isec->makes_toc_func_call = 1;
11390
11391 return ret;
11392 }
11393
11394 /* The linker repeatedly calls this function for each input section,
11395 in the order that input sections are linked into output sections.
11396 Build lists of input sections to determine groupings between which
11397 we may insert linker stubs. */
11398
11399 bfd_boolean
11400 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11401 {
11402 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11403
11404 if (htab == NULL)
11405 return FALSE;
11406
11407 if ((isec->output_section->flags & SEC_CODE) != 0
11408 && isec->output_section->id < htab->sec_info_arr_size)
11409 {
11410 /* This happens to make the list in reverse order,
11411 which is what we want. */
11412 htab->sec_info[isec->id].u.list
11413 = htab->sec_info[isec->output_section->id].u.list;
11414 htab->sec_info[isec->output_section->id].u.list = isec;
11415 }
11416
11417 if (htab->multi_toc_needed)
11418 {
11419 /* Analyse sections that aren't already flagged as needing a
11420 valid toc pointer. Exclude .fixup for the linux kernel.
11421 .fixup contains branches, but only back to the function that
11422 hit an exception. */
11423 if (!(isec->has_toc_reloc
11424 || (isec->flags & SEC_CODE) == 0
11425 || strcmp (isec->name, ".fixup") == 0
11426 || isec->call_check_done))
11427 {
11428 if (toc_adjusting_stub_needed (info, isec) < 0)
11429 return FALSE;
11430 }
11431 /* Make all sections use the TOC assigned for this object file.
11432 This will be wrong for pasted sections; We fix that in
11433 check_pasted_section(). */
11434 if (elf_gp (isec->owner) != 0)
11435 htab->toc_curr = elf_gp (isec->owner);
11436 }
11437
11438 htab->sec_info[isec->id].toc_off = htab->toc_curr;
11439 return TRUE;
11440 }
11441
11442 /* Check that all .init and .fini sections use the same toc, if they
11443 have toc relocs. */
11444
11445 static bfd_boolean
11446 check_pasted_section (struct bfd_link_info *info, const char *name)
11447 {
11448 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11449
11450 if (o != NULL)
11451 {
11452 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11453 bfd_vma toc_off = 0;
11454 asection *i;
11455
11456 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11457 if (i->has_toc_reloc)
11458 {
11459 if (toc_off == 0)
11460 toc_off = htab->sec_info[i->id].toc_off;
11461 else if (toc_off != htab->sec_info[i->id].toc_off)
11462 return FALSE;
11463 }
11464
11465 if (toc_off == 0)
11466 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11467 if (i->makes_toc_func_call)
11468 {
11469 toc_off = htab->sec_info[i->id].toc_off;
11470 break;
11471 }
11472
11473 /* Make sure the whole pasted function uses the same toc offset. */
11474 if (toc_off != 0)
11475 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11476 htab->sec_info[i->id].toc_off = toc_off;
11477 }
11478 return TRUE;
11479 }
11480
11481 bfd_boolean
11482 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11483 {
11484 return (check_pasted_section (info, ".init")
11485 & check_pasted_section (info, ".fini"));
11486 }
11487
11488 /* See whether we can group stub sections together. Grouping stub
11489 sections may result in fewer stubs. More importantly, we need to
11490 put all .init* and .fini* stubs at the beginning of the .init or
11491 .fini output sections respectively, because glibc splits the
11492 _init and _fini functions into multiple parts. Putting a stub in
11493 the middle of a function is not a good idea. */
11494
11495 static bfd_boolean
11496 group_sections (struct bfd_link_info *info,
11497 bfd_size_type stub_group_size,
11498 bfd_boolean stubs_always_before_branch)
11499 {
11500 struct ppc_link_hash_table *htab;
11501 asection *osec;
11502 bfd_boolean suppress_size_errors;
11503
11504 htab = ppc_hash_table (info);
11505 if (htab == NULL)
11506 return FALSE;
11507
11508 suppress_size_errors = FALSE;
11509 if (stub_group_size == 1)
11510 {
11511 /* Default values. */
11512 if (stubs_always_before_branch)
11513 stub_group_size = 0x1e00000;
11514 else
11515 stub_group_size = 0x1c00000;
11516 suppress_size_errors = TRUE;
11517 }
11518
11519 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
11520 {
11521 asection *tail;
11522
11523 if (osec->id >= htab->sec_info_arr_size)
11524 continue;
11525
11526 tail = htab->sec_info[osec->id].u.list;
11527 while (tail != NULL)
11528 {
11529 asection *curr;
11530 asection *prev;
11531 bfd_size_type total;
11532 bfd_boolean big_sec;
11533 bfd_vma curr_toc;
11534 struct map_stub *group;
11535 bfd_size_type group_size;
11536
11537 curr = tail;
11538 total = tail->size;
11539 group_size = (ppc64_elf_section_data (tail) != NULL
11540 && ppc64_elf_section_data (tail)->has_14bit_branch
11541 ? stub_group_size >> 10 : stub_group_size);
11542
11543 big_sec = total > group_size;
11544 if (big_sec && !suppress_size_errors)
11545 /* xgettext:c-format */
11546 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
11547 tail->owner, tail);
11548 curr_toc = htab->sec_info[tail->id].toc_off;
11549
11550 while ((prev = htab->sec_info[curr->id].u.list) != NULL
11551 && ((total += curr->output_offset - prev->output_offset)
11552 < (ppc64_elf_section_data (prev) != NULL
11553 && ppc64_elf_section_data (prev)->has_14bit_branch
11554 ? (group_size = stub_group_size >> 10) : group_size))
11555 && htab->sec_info[prev->id].toc_off == curr_toc)
11556 curr = prev;
11557
11558 /* OK, the size from the start of CURR to the end is less
11559 than group_size and thus can be handled by one stub
11560 section. (or the tail section is itself larger than
11561 group_size, in which case we may be toast.) We should
11562 really be keeping track of the total size of stubs added
11563 here, as stubs contribute to the final output section
11564 size. That's a little tricky, and this way will only
11565 break if stubs added make the total size more than 2^25,
11566 ie. for the default stub_group_size, if stubs total more
11567 than 2097152 bytes, or nearly 75000 plt call stubs. */
11568 group = bfd_alloc (curr->owner, sizeof (*group));
11569 if (group == NULL)
11570 return FALSE;
11571 group->link_sec = curr;
11572 group->stub_sec = NULL;
11573 group->needs_save_res = 0;
11574 group->lr_restore = 0;
11575 group->eh_size = 0;
11576 group->eh_base = 0;
11577 group->next = htab->group;
11578 htab->group = group;
11579 do
11580 {
11581 prev = htab->sec_info[tail->id].u.list;
11582 /* Set up this stub group. */
11583 htab->sec_info[tail->id].u.group = group;
11584 }
11585 while (tail != curr && (tail = prev) != NULL);
11586
11587 /* But wait, there's more! Input sections up to group_size
11588 bytes before the stub section can be handled by it too.
11589 Don't do this if we have a really large section after the
11590 stubs, as adding more stubs increases the chance that
11591 branches may not reach into the stub section. */
11592 if (!stubs_always_before_branch && !big_sec)
11593 {
11594 total = 0;
11595 while (prev != NULL
11596 && ((total += tail->output_offset - prev->output_offset)
11597 < (ppc64_elf_section_data (prev) != NULL
11598 && ppc64_elf_section_data (prev)->has_14bit_branch
11599 ? (group_size = stub_group_size >> 10)
11600 : group_size))
11601 && htab->sec_info[prev->id].toc_off == curr_toc)
11602 {
11603 tail = prev;
11604 prev = htab->sec_info[tail->id].u.list;
11605 htab->sec_info[tail->id].u.group = group;
11606 }
11607 }
11608 tail = prev;
11609 }
11610 }
11611 return TRUE;
11612 }
11613
11614 static const unsigned char glink_eh_frame_cie[] =
11615 {
11616 0, 0, 0, 16, /* length. */
11617 0, 0, 0, 0, /* id. */
11618 1, /* CIE version. */
11619 'z', 'R', 0, /* Augmentation string. */
11620 4, /* Code alignment. */
11621 0x78, /* Data alignment. */
11622 65, /* RA reg. */
11623 1, /* Augmentation size. */
11624 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
11625 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
11626 };
11627
11628 /* Stripping output sections is normally done before dynamic section
11629 symbols have been allocated. This function is called later, and
11630 handles cases like htab->brlt which is mapped to its own output
11631 section. */
11632
11633 static void
11634 maybe_strip_output (struct bfd_link_info *info, asection *isec)
11635 {
11636 if (isec->size == 0
11637 && isec->output_section->size == 0
11638 && !(isec->output_section->flags & SEC_KEEP)
11639 && !bfd_section_removed_from_list (info->output_bfd,
11640 isec->output_section)
11641 && elf_section_data (isec->output_section)->dynindx == 0)
11642 {
11643 isec->output_section->flags |= SEC_EXCLUDE;
11644 bfd_section_list_remove (info->output_bfd, isec->output_section);
11645 info->output_bfd->section_count--;
11646 }
11647 }
11648
11649 /* Determine and set the size of the stub section for a final link.
11650
11651 The basic idea here is to examine all the relocations looking for
11652 PC-relative calls to a target that is unreachable with a "bl"
11653 instruction. */
11654
11655 bfd_boolean
11656 ppc64_elf_size_stubs (struct bfd_link_info *info)
11657 {
11658 bfd_size_type stub_group_size;
11659 bfd_boolean stubs_always_before_branch;
11660 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11661
11662 if (htab == NULL)
11663 return FALSE;
11664
11665 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
11666 htab->params->plt_thread_safe = 1;
11667 if (!htab->opd_abi)
11668 htab->params->plt_thread_safe = 0;
11669 else if (htab->params->plt_thread_safe == -1)
11670 {
11671 static const char *const thread_starter[] =
11672 {
11673 "pthread_create",
11674 /* libstdc++ */
11675 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11676 /* librt */
11677 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11678 "mq_notify", "create_timer",
11679 /* libanl */
11680 "getaddrinfo_a",
11681 /* libgomp */
11682 "GOMP_parallel",
11683 "GOMP_parallel_start",
11684 "GOMP_parallel_loop_static",
11685 "GOMP_parallel_loop_static_start",
11686 "GOMP_parallel_loop_dynamic",
11687 "GOMP_parallel_loop_dynamic_start",
11688 "GOMP_parallel_loop_guided",
11689 "GOMP_parallel_loop_guided_start",
11690 "GOMP_parallel_loop_runtime",
11691 "GOMP_parallel_loop_runtime_start",
11692 "GOMP_parallel_sections",
11693 "GOMP_parallel_sections_start",
11694 /* libgo */
11695 "__go_go",
11696 };
11697 unsigned i;
11698
11699 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
11700 {
11701 struct elf_link_hash_entry *h;
11702 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11703 FALSE, FALSE, TRUE);
11704 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
11705 if (htab->params->plt_thread_safe)
11706 break;
11707 }
11708 }
11709 stubs_always_before_branch = htab->params->group_size < 0;
11710 if (htab->params->group_size < 0)
11711 stub_group_size = -htab->params->group_size;
11712 else
11713 stub_group_size = htab->params->group_size;
11714
11715 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
11716 return FALSE;
11717
11718 #define STUB_SHRINK_ITER 20
11719 /* Loop until no stubs added. After iteration 20 of this loop we may
11720 exit on a stub section shrinking. This is to break out of a
11721 pathological case where adding stubs on one iteration decreases
11722 section gaps (perhaps due to alignment), which then requires
11723 fewer or smaller stubs on the next iteration. */
11724
11725 while (1)
11726 {
11727 bfd *input_bfd;
11728 unsigned int bfd_indx;
11729 struct map_stub *group;
11730
11731 htab->stub_iteration += 1;
11732
11733 for (input_bfd = info->input_bfds, bfd_indx = 0;
11734 input_bfd != NULL;
11735 input_bfd = input_bfd->link.next, bfd_indx++)
11736 {
11737 Elf_Internal_Shdr *symtab_hdr;
11738 asection *section;
11739 Elf_Internal_Sym *local_syms = NULL;
11740
11741 if (!is_ppc64_elf (input_bfd))
11742 continue;
11743
11744 /* We'll need the symbol table in a second. */
11745 symtab_hdr = &elf_symtab_hdr (input_bfd);
11746 if (symtab_hdr->sh_info == 0)
11747 continue;
11748
11749 /* Walk over each section attached to the input bfd. */
11750 for (section = input_bfd->sections;
11751 section != NULL;
11752 section = section->next)
11753 {
11754 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
11755
11756 /* If there aren't any relocs, then there's nothing more
11757 to do. */
11758 if ((section->flags & SEC_RELOC) == 0
11759 || (section->flags & SEC_ALLOC) == 0
11760 || (section->flags & SEC_LOAD) == 0
11761 || (section->flags & SEC_CODE) == 0
11762 || section->reloc_count == 0)
11763 continue;
11764
11765 /* If this section is a link-once section that will be
11766 discarded, then don't create any stubs. */
11767 if (section->output_section == NULL
11768 || section->output_section->owner != info->output_bfd)
11769 continue;
11770
11771 /* Get the relocs. */
11772 internal_relocs
11773 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
11774 info->keep_memory);
11775 if (internal_relocs == NULL)
11776 goto error_ret_free_local;
11777
11778 /* Now examine each relocation. */
11779 irela = internal_relocs;
11780 irelaend = irela + section->reloc_count;
11781 for (; irela < irelaend; irela++)
11782 {
11783 enum elf_ppc64_reloc_type r_type;
11784 unsigned int r_indx;
11785 enum ppc_stub_type stub_type;
11786 struct ppc_stub_hash_entry *stub_entry;
11787 asection *sym_sec, *code_sec;
11788 bfd_vma sym_value, code_value;
11789 bfd_vma destination;
11790 unsigned long local_off;
11791 bfd_boolean ok_dest;
11792 struct ppc_link_hash_entry *hash;
11793 struct ppc_link_hash_entry *fdh;
11794 struct elf_link_hash_entry *h;
11795 Elf_Internal_Sym *sym;
11796 char *stub_name;
11797 const asection *id_sec;
11798 struct _opd_sec_data *opd;
11799 struct plt_entry *plt_ent;
11800
11801 r_type = ELF64_R_TYPE (irela->r_info);
11802 r_indx = ELF64_R_SYM (irela->r_info);
11803
11804 if (r_type >= R_PPC64_max)
11805 {
11806 bfd_set_error (bfd_error_bad_value);
11807 goto error_ret_free_internal;
11808 }
11809
11810 /* Only look for stubs on branch instructions. */
11811 if (r_type != R_PPC64_REL24
11812 && r_type != R_PPC64_REL24_NOTOC
11813 && r_type != R_PPC64_REL14
11814 && r_type != R_PPC64_REL14_BRTAKEN
11815 && r_type != R_PPC64_REL14_BRNTAKEN)
11816 continue;
11817
11818 /* Now determine the call target, its name, value,
11819 section. */
11820 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11821 r_indx, input_bfd))
11822 goto error_ret_free_internal;
11823 hash = (struct ppc_link_hash_entry *) h;
11824
11825 ok_dest = FALSE;
11826 fdh = NULL;
11827 sym_value = 0;
11828 if (hash == NULL)
11829 {
11830 sym_value = sym->st_value;
11831 if (sym_sec != NULL
11832 && sym_sec->output_section != NULL)
11833 ok_dest = TRUE;
11834 }
11835 else if (hash->elf.root.type == bfd_link_hash_defined
11836 || hash->elf.root.type == bfd_link_hash_defweak)
11837 {
11838 sym_value = hash->elf.root.u.def.value;
11839 if (sym_sec->output_section != NULL)
11840 ok_dest = TRUE;
11841 }
11842 else if (hash->elf.root.type == bfd_link_hash_undefweak
11843 || hash->elf.root.type == bfd_link_hash_undefined)
11844 {
11845 /* Recognise an old ABI func code entry sym, and
11846 use the func descriptor sym instead if it is
11847 defined. */
11848 if (hash->elf.root.root.string[0] == '.'
11849 && hash->oh != NULL)
11850 {
11851 fdh = ppc_follow_link (hash->oh);
11852 if (fdh->elf.root.type == bfd_link_hash_defined
11853 || fdh->elf.root.type == bfd_link_hash_defweak)
11854 {
11855 sym_sec = fdh->elf.root.u.def.section;
11856 sym_value = fdh->elf.root.u.def.value;
11857 if (sym_sec->output_section != NULL)
11858 ok_dest = TRUE;
11859 }
11860 else
11861 fdh = NULL;
11862 }
11863 }
11864 else
11865 {
11866 bfd_set_error (bfd_error_bad_value);
11867 goto error_ret_free_internal;
11868 }
11869
11870 destination = 0;
11871 local_off = 0;
11872 if (ok_dest)
11873 {
11874 sym_value += irela->r_addend;
11875 destination = (sym_value
11876 + sym_sec->output_offset
11877 + sym_sec->output_section->vma);
11878 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
11879 ? hash->elf.other
11880 : sym->st_other);
11881 }
11882
11883 code_sec = sym_sec;
11884 code_value = sym_value;
11885 opd = get_opd_info (sym_sec);
11886 if (opd != NULL)
11887 {
11888 bfd_vma dest;
11889
11890 if (hash == NULL && opd->adjust != NULL)
11891 {
11892 long adjust = opd->adjust[OPD_NDX (sym_value)];
11893 if (adjust == -1)
11894 continue;
11895 code_value += adjust;
11896 sym_value += adjust;
11897 }
11898 dest = opd_entry_value (sym_sec, sym_value,
11899 &code_sec, &code_value, FALSE);
11900 if (dest != (bfd_vma) -1)
11901 {
11902 destination = dest;
11903 if (fdh != NULL)
11904 {
11905 /* Fixup old ABI sym to point at code
11906 entry. */
11907 hash->elf.root.type = bfd_link_hash_defweak;
11908 hash->elf.root.u.def.section = code_sec;
11909 hash->elf.root.u.def.value = code_value;
11910 }
11911 }
11912 }
11913
11914 /* Determine what (if any) linker stub is needed. */
11915 plt_ent = NULL;
11916 stub_type = ppc_type_of_stub (section, irela, &hash,
11917 &plt_ent, destination,
11918 local_off);
11919
11920 if (r_type == R_PPC64_REL24_NOTOC)
11921 {
11922 if (stub_type == ppc_stub_plt_call)
11923 stub_type = ppc_stub_plt_call_notoc;
11924 else if (stub_type == ppc_stub_long_branch
11925 || (code_sec != NULL
11926 && code_sec->output_section != NULL
11927 && (((hash ? hash->elf.other : sym->st_other)
11928 & STO_PPC64_LOCAL_MASK)
11929 != 1 << STO_PPC64_LOCAL_BIT)))
11930 stub_type = ppc_stub_long_branch_notoc;
11931 }
11932 else if (stub_type != ppc_stub_plt_call)
11933 {
11934 /* Check whether we need a TOC adjusting stub.
11935 Since the linker pastes together pieces from
11936 different object files when creating the
11937 _init and _fini functions, it may be that a
11938 call to what looks like a local sym is in
11939 fact a call needing a TOC adjustment. */
11940 if ((code_sec != NULL
11941 && code_sec->output_section != NULL
11942 && (htab->sec_info[code_sec->id].toc_off
11943 != htab->sec_info[section->id].toc_off)
11944 && (code_sec->has_toc_reloc
11945 || code_sec->makes_toc_func_call))
11946 || (((hash ? hash->elf.other : sym->st_other)
11947 & STO_PPC64_LOCAL_MASK)
11948 == 1 << STO_PPC64_LOCAL_BIT))
11949 stub_type = ppc_stub_long_branch_r2off;
11950 }
11951
11952 if (stub_type == ppc_stub_none)
11953 continue;
11954
11955 /* __tls_get_addr calls might be eliminated. */
11956 if (stub_type != ppc_stub_plt_call
11957 && stub_type != ppc_stub_plt_call_notoc
11958 && hash != NULL
11959 && (hash == htab->tls_get_addr
11960 || hash == htab->tls_get_addr_fd)
11961 && section->has_tls_reloc
11962 && irela != internal_relocs)
11963 {
11964 /* Get tls info. */
11965 unsigned char *tls_mask;
11966
11967 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
11968 irela - 1, input_bfd))
11969 goto error_ret_free_internal;
11970 if ((*tls_mask & TLS_TLS) != 0)
11971 continue;
11972 }
11973
11974 if (stub_type == ppc_stub_plt_call)
11975 {
11976 if (!htab->opd_abi
11977 && htab->params->plt_localentry0 != 0
11978 && is_elfv2_localentry0 (&hash->elf))
11979 htab->has_plt_localentry0 = 1;
11980 else if (irela + 1 < irelaend
11981 && irela[1].r_offset == irela->r_offset + 4
11982 && (ELF64_R_TYPE (irela[1].r_info)
11983 == R_PPC64_TOCSAVE))
11984 {
11985 if (!tocsave_find (htab, INSERT,
11986 &local_syms, irela + 1, input_bfd))
11987 goto error_ret_free_internal;
11988 }
11989 else
11990 stub_type = ppc_stub_plt_call_r2save;
11991 }
11992
11993 /* Support for grouping stub sections. */
11994 id_sec = htab->sec_info[section->id].u.group->link_sec;
11995
11996 /* Get the name of this stub. */
11997 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11998 if (!stub_name)
11999 goto error_ret_free_internal;
12000
12001 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12002 stub_name, FALSE, FALSE);
12003 if (stub_entry != NULL)
12004 {
12005 enum ppc_stub_type old_type;
12006 /* A stub has already been created, but it may
12007 not be the required type. We shouldn't be
12008 transitioning from plt_call to long_branch
12009 stubs or vice versa, but we might be
12010 upgrading from plt_call to plt_call_r2save or
12011 from long_branch to long_branch_r2off. */
12012 free (stub_name);
12013 old_type = stub_entry->stub_type;
12014 switch (old_type)
12015 {
12016 default:
12017 abort ();
12018
12019 case ppc_stub_save_res:
12020 continue;
12021
12022 case ppc_stub_plt_call:
12023 case ppc_stub_plt_call_r2save:
12024 case ppc_stub_plt_call_notoc:
12025 case ppc_stub_plt_call_both:
12026 if (stub_type == ppc_stub_plt_call)
12027 continue;
12028 else if (stub_type == ppc_stub_plt_call_r2save)
12029 {
12030 if (old_type == ppc_stub_plt_call_notoc)
12031 stub_type = ppc_stub_plt_call_both;
12032 }
12033 else if (stub_type == ppc_stub_plt_call_notoc)
12034 {
12035 if (old_type == ppc_stub_plt_call_r2save)
12036 stub_type = ppc_stub_plt_call_both;
12037 }
12038 else
12039 abort ();
12040 break;
12041
12042 case ppc_stub_plt_branch:
12043 case ppc_stub_plt_branch_r2off:
12044 case ppc_stub_plt_branch_notoc:
12045 case ppc_stub_plt_branch_both:
12046 old_type += (ppc_stub_long_branch
12047 - ppc_stub_plt_branch);
12048 /* Fall through. */
12049 case ppc_stub_long_branch:
12050 case ppc_stub_long_branch_r2off:
12051 case ppc_stub_long_branch_notoc:
12052 case ppc_stub_long_branch_both:
12053 if (stub_type == ppc_stub_long_branch)
12054 continue;
12055 else if (stub_type == ppc_stub_long_branch_r2off)
12056 {
12057 if (old_type == ppc_stub_long_branch_notoc)
12058 stub_type = ppc_stub_long_branch_both;
12059 }
12060 else if (stub_type == ppc_stub_long_branch_notoc)
12061 {
12062 if (old_type == ppc_stub_long_branch_r2off)
12063 stub_type = ppc_stub_long_branch_both;
12064 }
12065 else
12066 abort ();
12067 break;
12068 }
12069 if (old_type < stub_type)
12070 stub_entry->stub_type = stub_type;
12071 continue;
12072 }
12073
12074 stub_entry = ppc_add_stub (stub_name, section, info);
12075 if (stub_entry == NULL)
12076 {
12077 free (stub_name);
12078 error_ret_free_internal:
12079 if (elf_section_data (section)->relocs == NULL)
12080 free (internal_relocs);
12081 error_ret_free_local:
12082 if (local_syms != NULL
12083 && (symtab_hdr->contents
12084 != (unsigned char *) local_syms))
12085 free (local_syms);
12086 return FALSE;
12087 }
12088
12089 stub_entry->stub_type = stub_type;
12090 if (stub_type >= ppc_stub_plt_call
12091 && stub_type <= ppc_stub_plt_call_both)
12092 {
12093 stub_entry->target_value = sym_value;
12094 stub_entry->target_section = sym_sec;
12095 }
12096 else
12097 {
12098 stub_entry->target_value = code_value;
12099 stub_entry->target_section = code_sec;
12100 }
12101 stub_entry->h = hash;
12102 stub_entry->plt_ent = plt_ent;
12103 stub_entry->symtype
12104 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
12105 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12106
12107 if (stub_entry->h != NULL)
12108 htab->stub_globals += 1;
12109 }
12110
12111 /* We're done with the internal relocs, free them. */
12112 if (elf_section_data (section)->relocs != internal_relocs)
12113 free (internal_relocs);
12114 }
12115
12116 if (local_syms != NULL
12117 && symtab_hdr->contents != (unsigned char *) local_syms)
12118 {
12119 if (!info->keep_memory)
12120 free (local_syms);
12121 else
12122 symtab_hdr->contents = (unsigned char *) local_syms;
12123 }
12124 }
12125
12126 /* We may have added some stubs. Find out the new size of the
12127 stub sections. */
12128 for (group = htab->group; group != NULL; group = group->next)
12129 {
12130 group->lr_restore = 0;
12131 group->eh_size = 0;
12132 if (group->stub_sec != NULL)
12133 {
12134 asection *stub_sec = group->stub_sec;
12135
12136 if (htab->stub_iteration <= STUB_SHRINK_ITER
12137 || stub_sec->rawsize < stub_sec->size)
12138 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12139 stub_sec->rawsize = stub_sec->size;
12140 stub_sec->size = 0;
12141 stub_sec->reloc_count = 0;
12142 stub_sec->flags &= ~SEC_RELOC;
12143 }
12144 }
12145
12146 if (htab->stub_iteration <= STUB_SHRINK_ITER
12147 || htab->brlt->rawsize < htab->brlt->size)
12148 htab->brlt->rawsize = htab->brlt->size;
12149 htab->brlt->size = 0;
12150 htab->brlt->reloc_count = 0;
12151 htab->brlt->flags &= ~SEC_RELOC;
12152 if (htab->relbrlt != NULL)
12153 htab->relbrlt->size = 0;
12154
12155 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12156
12157 for (group = htab->group; group != NULL; group = group->next)
12158 if (group->needs_save_res)
12159 group->stub_sec->size += htab->sfpr->size;
12160
12161 if (info->emitrelocations
12162 && htab->glink != NULL && htab->glink->size != 0)
12163 {
12164 htab->glink->reloc_count = 1;
12165 htab->glink->flags |= SEC_RELOC;
12166 }
12167
12168 if (htab->glink_eh_frame != NULL
12169 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12170 && htab->glink_eh_frame->output_section->size > 8)
12171 {
12172 size_t size = 0, align = 4;
12173
12174 for (group = htab->group; group != NULL; group = group->next)
12175 if (group->eh_size != 0)
12176 size += (group->eh_size + 17 + align - 1) & -align;
12177 if (htab->glink != NULL && htab->glink->size != 0)
12178 size += (24 + align - 1) & -align;
12179 if (size != 0)
12180 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12181 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12182 size = (size + align - 1) & -align;
12183 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12184 htab->glink_eh_frame->size = size;
12185 }
12186
12187 if (htab->params->plt_stub_align != 0)
12188 for (group = htab->group; group != NULL; group = group->next)
12189 if (group->stub_sec != NULL)
12190 {
12191 int align = abs (htab->params->plt_stub_align);
12192 group->stub_sec->size
12193 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12194 }
12195
12196 for (group = htab->group; group != NULL; group = group->next)
12197 if (group->stub_sec != NULL
12198 && group->stub_sec->rawsize != group->stub_sec->size
12199 && (htab->stub_iteration <= STUB_SHRINK_ITER
12200 || group->stub_sec->rawsize < group->stub_sec->size))
12201 break;
12202
12203 if (group == NULL
12204 && (htab->brlt->rawsize == htab->brlt->size
12205 || (htab->stub_iteration > STUB_SHRINK_ITER
12206 && htab->brlt->rawsize > htab->brlt->size))
12207 && (htab->glink_eh_frame == NULL
12208 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12209 break;
12210
12211 /* Ask the linker to do its stuff. */
12212 (*htab->params->layout_sections_again) ();
12213 }
12214
12215 if (htab->glink_eh_frame != NULL
12216 && htab->glink_eh_frame->size != 0)
12217 {
12218 bfd_vma val;
12219 bfd_byte *p, *last_fde;
12220 size_t last_fde_len, size, align, pad;
12221 struct map_stub *group;
12222
12223 /* It is necessary to at least have a rough outline of the
12224 linker generated CIEs and FDEs written before
12225 bfd_elf_discard_info is run, in order for these FDEs to be
12226 indexed in .eh_frame_hdr. */
12227 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12228 if (p == NULL)
12229 return FALSE;
12230 htab->glink_eh_frame->contents = p;
12231 last_fde = p;
12232 align = 4;
12233
12234 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12235 /* CIE length (rewrite in case little-endian). */
12236 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12237 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12238 p += last_fde_len + 4;
12239
12240 for (group = htab->group; group != NULL; group = group->next)
12241 if (group->eh_size != 0)
12242 {
12243 group->eh_base = p - htab->glink_eh_frame->contents;
12244 last_fde = p;
12245 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
12246 /* FDE length. */
12247 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12248 p += 4;
12249 /* CIE pointer. */
12250 val = p - htab->glink_eh_frame->contents;
12251 bfd_put_32 (htab->elf.dynobj, val, p);
12252 p += 4;
12253 /* Offset to stub section, written later. */
12254 p += 4;
12255 /* stub section size. */
12256 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
12257 p += 4;
12258 /* Augmentation. */
12259 p += 1;
12260 /* Make sure we don't have all nops. This is enough for
12261 elf-eh-frame.c to detect the last non-nop opcode. */
12262 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
12263 p = last_fde + last_fde_len + 4;
12264 }
12265 if (htab->glink != NULL && htab->glink->size != 0)
12266 {
12267 last_fde = p;
12268 last_fde_len = ((24 + align - 1) & -align) - 4;
12269 /* FDE length. */
12270 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12271 p += 4;
12272 /* CIE pointer. */
12273 val = p - htab->glink_eh_frame->contents;
12274 bfd_put_32 (htab->elf.dynobj, val, p);
12275 p += 4;
12276 /* Offset to .glink, written later. */
12277 p += 4;
12278 /* .glink size. */
12279 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12280 p += 4;
12281 /* Augmentation. */
12282 p += 1;
12283
12284 *p++ = DW_CFA_advance_loc + 1;
12285 *p++ = DW_CFA_register;
12286 *p++ = 65;
12287 *p++ = htab->opd_abi ? 12 : 0;
12288 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
12289 *p++ = DW_CFA_restore_extended;
12290 *p++ = 65;
12291 p += ((24 + align - 1) & -align) - 24;
12292 }
12293 /* Subsume any padding into the last FDE if user .eh_frame
12294 sections are aligned more than glink_eh_frame. Otherwise any
12295 zero padding will be seen as a terminator. */
12296 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12297 size = p - htab->glink_eh_frame->contents;
12298 pad = ((size + align - 1) & -align) - size;
12299 htab->glink_eh_frame->size = size + pad;
12300 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12301 }
12302
12303 maybe_strip_output (info, htab->brlt);
12304 if (htab->glink_eh_frame != NULL)
12305 maybe_strip_output (info, htab->glink_eh_frame);
12306
12307 return TRUE;
12308 }
12309
12310 /* Called after we have determined section placement. If sections
12311 move, we'll be called again. Provide a value for TOCstart. */
12312
12313 bfd_vma
12314 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12315 {
12316 asection *s;
12317 bfd_vma TOCstart, adjust;
12318
12319 if (info != NULL)
12320 {
12321 struct elf_link_hash_entry *h;
12322 struct elf_link_hash_table *htab = elf_hash_table (info);
12323
12324 if (is_elf_hash_table (htab)
12325 && htab->hgot != NULL)
12326 h = htab->hgot;
12327 else
12328 {
12329 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12330 if (is_elf_hash_table (htab))
12331 htab->hgot = h;
12332 }
12333 if (h != NULL
12334 && h->root.type == bfd_link_hash_defined
12335 && !h->root.linker_def
12336 && (!is_elf_hash_table (htab)
12337 || h->def_regular))
12338 {
12339 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12340 + h->root.u.def.section->output_offset
12341 + h->root.u.def.section->output_section->vma);
12342 _bfd_set_gp_value (obfd, TOCstart);
12343 return TOCstart;
12344 }
12345 }
12346
12347 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12348 order. The TOC starts where the first of these sections starts. */
12349 s = bfd_get_section_by_name (obfd, ".got");
12350 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12351 s = bfd_get_section_by_name (obfd, ".toc");
12352 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12353 s = bfd_get_section_by_name (obfd, ".tocbss");
12354 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12355 s = bfd_get_section_by_name (obfd, ".plt");
12356 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12357 {
12358 /* This may happen for
12359 o references to TOC base (SYM@toc / TOC[tc0]) without a
12360 .toc directive
12361 o bad linker script
12362 o --gc-sections and empty TOC sections
12363
12364 FIXME: Warn user? */
12365
12366 /* Look for a likely section. We probably won't even be
12367 using TOCstart. */
12368 for (s = obfd->sections; s != NULL; s = s->next)
12369 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12370 | SEC_EXCLUDE))
12371 == (SEC_ALLOC | SEC_SMALL_DATA))
12372 break;
12373 if (s == NULL)
12374 for (s = obfd->sections; s != NULL; s = s->next)
12375 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12376 == (SEC_ALLOC | SEC_SMALL_DATA))
12377 break;
12378 if (s == NULL)
12379 for (s = obfd->sections; s != NULL; s = s->next)
12380 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12381 == SEC_ALLOC)
12382 break;
12383 if (s == NULL)
12384 for (s = obfd->sections; s != NULL; s = s->next)
12385 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12386 break;
12387 }
12388
12389 TOCstart = 0;
12390 if (s != NULL)
12391 TOCstart = s->output_section->vma + s->output_offset;
12392
12393 /* Force alignment. */
12394 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12395 TOCstart -= adjust;
12396 _bfd_set_gp_value (obfd, TOCstart);
12397
12398 if (info != NULL && s != NULL)
12399 {
12400 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12401
12402 if (htab != NULL)
12403 {
12404 if (htab->elf.hgot != NULL)
12405 {
12406 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12407 htab->elf.hgot->root.u.def.section = s;
12408 }
12409 }
12410 else
12411 {
12412 struct bfd_link_hash_entry *bh = NULL;
12413 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12414 s, TOC_BASE_OFF - adjust,
12415 NULL, FALSE, FALSE, &bh);
12416 }
12417 }
12418 return TOCstart;
12419 }
12420
12421 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12422 write out any global entry stubs, and PLT relocations. */
12423
12424 static bfd_boolean
12425 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
12426 {
12427 struct bfd_link_info *info;
12428 struct ppc_link_hash_table *htab;
12429 struct plt_entry *ent;
12430 asection *s;
12431
12432 if (h->root.type == bfd_link_hash_indirect)
12433 return TRUE;
12434
12435 info = inf;
12436 htab = ppc_hash_table (info);
12437 if (htab == NULL)
12438 return FALSE;
12439
12440 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12441 if (ent->plt.offset != (bfd_vma) -1)
12442 {
12443 /* This symbol has an entry in the procedure linkage
12444 table. Set it up. */
12445 Elf_Internal_Rela rela;
12446 asection *plt, *relplt;
12447 bfd_byte *loc;
12448
12449 if (!htab->elf.dynamic_sections_created
12450 || h->dynindx == -1)
12451 {
12452 if (!(h->def_regular
12453 && (h->root.type == bfd_link_hash_defined
12454 || h->root.type == bfd_link_hash_defweak)))
12455 continue;
12456 if (h->type == STT_GNU_IFUNC)
12457 {
12458 plt = htab->elf.iplt;
12459 relplt = htab->elf.irelplt;
12460 htab->local_ifunc_resolver = 1;
12461 if (htab->opd_abi)
12462 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
12463 else
12464 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12465 }
12466 else
12467 {
12468 plt = htab->pltlocal;
12469 if (bfd_link_pic (info))
12470 {
12471 relplt = htab->relpltlocal;
12472 if (htab->opd_abi)
12473 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
12474 else
12475 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12476 }
12477 else
12478 relplt = NULL;
12479 }
12480 rela.r_addend = (h->root.u.def.value
12481 + h->root.u.def.section->output_offset
12482 + h->root.u.def.section->output_section->vma
12483 + ent->addend);
12484
12485 if (relplt == NULL)
12486 {
12487 loc = plt->contents + ent->plt.offset;
12488 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
12489 if (htab->opd_abi)
12490 {
12491 bfd_vma toc = elf_gp (info->output_bfd);
12492 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
12493 bfd_put_64 (info->output_bfd, toc, loc + 8);
12494 }
12495 }
12496 else
12497 {
12498 rela.r_offset = (plt->output_section->vma
12499 + plt->output_offset
12500 + ent->plt.offset);
12501 loc = relplt->contents + (relplt->reloc_count++
12502 * sizeof (Elf64_External_Rela));
12503 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12504 }
12505 }
12506 else
12507 {
12508 rela.r_offset = (htab->elf.splt->output_section->vma
12509 + htab->elf.splt->output_offset
12510 + ent->plt.offset);
12511 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
12512 rela.r_addend = ent->addend;
12513 loc = (htab->elf.srelplt->contents
12514 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
12515 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
12516 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
12517 htab->maybe_local_ifunc_resolver = 1;
12518 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12519 }
12520 }
12521
12522 if (!h->pointer_equality_needed)
12523 return TRUE;
12524
12525 if (h->def_regular)
12526 return TRUE;
12527
12528 s = htab->global_entry;
12529 if (s == NULL || s->size == 0)
12530 return TRUE;
12531
12532 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12533 if (ent->plt.offset != (bfd_vma) -1
12534 && ent->addend == 0)
12535 {
12536 bfd_byte *p;
12537 asection *plt;
12538 bfd_vma off;
12539
12540 p = s->contents + h->root.u.def.value;
12541 plt = htab->elf.splt;
12542 if (!htab->elf.dynamic_sections_created
12543 || h->dynindx == -1)
12544 {
12545 if (h->type == STT_GNU_IFUNC)
12546 plt = htab->elf.iplt;
12547 else
12548 plt = htab->pltlocal;
12549 }
12550 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
12551 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12552
12553 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12554 {
12555 info->callbacks->einfo
12556 (_("%P: linkage table error against `%pT'\n"),
12557 h->root.root.string);
12558 bfd_set_error (bfd_error_bad_value);
12559 htab->stub_error = TRUE;
12560 }
12561
12562 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12563 if (htab->params->emit_stub_syms)
12564 {
12565 size_t len = strlen (h->root.root.string);
12566 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12567
12568 if (name == NULL)
12569 return FALSE;
12570
12571 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12572 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12573 if (h == NULL)
12574 return FALSE;
12575 if (h->root.type == bfd_link_hash_new)
12576 {
12577 h->root.type = bfd_link_hash_defined;
12578 h->root.u.def.section = s;
12579 h->root.u.def.value = p - s->contents;
12580 h->ref_regular = 1;
12581 h->def_regular = 1;
12582 h->ref_regular_nonweak = 1;
12583 h->forced_local = 1;
12584 h->non_elf = 0;
12585 h->root.linker_def = 1;
12586 }
12587 }
12588
12589 if (PPC_HA (off) != 0)
12590 {
12591 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12592 p += 4;
12593 }
12594 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12595 p += 4;
12596 bfd_put_32 (s->owner, MTCTR_R12, p);
12597 p += 4;
12598 bfd_put_32 (s->owner, BCTR, p);
12599 break;
12600 }
12601 return TRUE;
12602 }
12603
12604 /* Write PLT relocs for locals. */
12605
12606 static bfd_boolean
12607 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
12608 {
12609 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12610 bfd *ibfd;
12611
12612 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12613 {
12614 struct got_entry **lgot_ents, **end_lgot_ents;
12615 struct plt_entry **local_plt, **lplt, **end_local_plt;
12616 Elf_Internal_Shdr *symtab_hdr;
12617 bfd_size_type locsymcount;
12618 Elf_Internal_Sym *local_syms = NULL;
12619 struct plt_entry *ent;
12620
12621 if (!is_ppc64_elf (ibfd))
12622 continue;
12623
12624 lgot_ents = elf_local_got_ents (ibfd);
12625 if (!lgot_ents)
12626 continue;
12627
12628 symtab_hdr = &elf_symtab_hdr (ibfd);
12629 locsymcount = symtab_hdr->sh_info;
12630 end_lgot_ents = lgot_ents + locsymcount;
12631 local_plt = (struct plt_entry **) end_lgot_ents;
12632 end_local_plt = local_plt + locsymcount;
12633 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
12634 for (ent = *lplt; ent != NULL; ent = ent->next)
12635 if (ent->plt.offset != (bfd_vma) -1)
12636 {
12637 Elf_Internal_Sym *sym;
12638 asection *sym_sec;
12639 asection *plt, *relplt;
12640 bfd_byte *loc;
12641 bfd_vma val;
12642
12643 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
12644 lplt - local_plt, ibfd))
12645 {
12646 if (local_syms != NULL
12647 && symtab_hdr->contents != (unsigned char *) local_syms)
12648 free (local_syms);
12649 return FALSE;
12650 }
12651
12652 val = sym->st_value + ent->addend;
12653 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
12654 if (sym_sec != NULL && sym_sec->output_section != NULL)
12655 val += sym_sec->output_offset + sym_sec->output_section->vma;
12656
12657 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12658 {
12659 htab->local_ifunc_resolver = 1;
12660 plt = htab->elf.iplt;
12661 relplt = htab->elf.irelplt;
12662 }
12663 else
12664 {
12665 plt = htab->pltlocal;
12666 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
12667 }
12668
12669 if (relplt == NULL)
12670 {
12671 loc = plt->contents + ent->plt.offset;
12672 bfd_put_64 (info->output_bfd, val, loc);
12673 if (htab->opd_abi)
12674 {
12675 bfd_vma toc = elf_gp (ibfd);
12676 bfd_put_64 (info->output_bfd, toc, loc + 8);
12677 }
12678 }
12679 else
12680 {
12681 Elf_Internal_Rela rela;
12682 rela.r_offset = (ent->plt.offset
12683 + plt->output_offset
12684 + plt->output_section->vma);
12685 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12686 {
12687 if (htab->opd_abi)
12688 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
12689 else
12690 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12691 }
12692 else
12693 {
12694 if (htab->opd_abi)
12695 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
12696 else
12697 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12698 }
12699 rela.r_addend = val;
12700 loc = relplt->contents + (relplt->reloc_count++
12701 * sizeof (Elf64_External_Rela));
12702 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12703 }
12704 }
12705
12706 if (local_syms != NULL
12707 && symtab_hdr->contents != (unsigned char *) local_syms)
12708 {
12709 if (!info->keep_memory)
12710 free (local_syms);
12711 else
12712 symtab_hdr->contents = (unsigned char *) local_syms;
12713 }
12714 }
12715 return TRUE;
12716 }
12717
12718 /* Build all the stubs associated with the current output file.
12719 The stubs are kept in a hash table attached to the main linker
12720 hash table. This function is called via gldelf64ppc_finish. */
12721
12722 bfd_boolean
12723 ppc64_elf_build_stubs (struct bfd_link_info *info,
12724 char **stats)
12725 {
12726 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12727 struct map_stub *group;
12728 asection *stub_sec;
12729 bfd_byte *p;
12730 int stub_sec_count = 0;
12731
12732 if (htab == NULL)
12733 return FALSE;
12734
12735 /* Allocate memory to hold the linker stubs. */
12736 for (group = htab->group; group != NULL; group = group->next)
12737 {
12738 group->eh_size = 0;
12739 group->lr_restore = 0;
12740 if ((stub_sec = group->stub_sec) != NULL
12741 && stub_sec->size != 0)
12742 {
12743 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
12744 stub_sec->size);
12745 if (stub_sec->contents == NULL)
12746 return FALSE;
12747 stub_sec->size = 0;
12748 }
12749 }
12750
12751 if (htab->glink != NULL && htab->glink->size != 0)
12752 {
12753 unsigned int indx;
12754 bfd_vma plt0;
12755
12756 /* Build the .glink plt call stub. */
12757 if (htab->params->emit_stub_syms)
12758 {
12759 struct elf_link_hash_entry *h;
12760 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12761 TRUE, FALSE, FALSE);
12762 if (h == NULL)
12763 return FALSE;
12764 if (h->root.type == bfd_link_hash_new)
12765 {
12766 h->root.type = bfd_link_hash_defined;
12767 h->root.u.def.section = htab->glink;
12768 h->root.u.def.value = 8;
12769 h->ref_regular = 1;
12770 h->def_regular = 1;
12771 h->ref_regular_nonweak = 1;
12772 h->forced_local = 1;
12773 h->non_elf = 0;
12774 h->root.linker_def = 1;
12775 }
12776 }
12777 plt0 = (htab->elf.splt->output_section->vma
12778 + htab->elf.splt->output_offset
12779 - 16);
12780 if (info->emitrelocations)
12781 {
12782 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12783 if (r == NULL)
12784 return FALSE;
12785 r->r_offset = (htab->glink->output_offset
12786 + htab->glink->output_section->vma);
12787 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12788 r->r_addend = plt0;
12789 }
12790 p = htab->glink->contents;
12791 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
12792 bfd_put_64 (htab->glink->owner, plt0, p);
12793 p += 8;
12794 if (htab->opd_abi)
12795 {
12796 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12797 p += 4;
12798 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12799 p += 4;
12800 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12801 p += 4;
12802 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12803 p += 4;
12804 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12805 p += 4;
12806 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12807 p += 4;
12808 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12809 p += 4;
12810 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12811 p += 4;
12812 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12813 p += 4;
12814 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12815 p += 4;
12816 }
12817 else
12818 {
12819 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
12820 p += 4;
12821 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12822 p += 4;
12823 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12824 p += 4;
12825 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
12826 p += 4;
12827 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12828 p += 4;
12829 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
12830 p += 4;
12831 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
12832 p += 4;
12833 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12834 p += 4;
12835 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
12836 p += 4;
12837 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12838 p += 4;
12839 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
12840 p += 4;
12841 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12842 p += 4;
12843 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
12844 p += 4;
12845 }
12846 bfd_put_32 (htab->glink->owner, BCTR, p);
12847 p += 4;
12848 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
12849
12850 /* Build the .glink lazy link call stubs. */
12851 indx = 0;
12852 while (p < htab->glink->contents + htab->glink->size)
12853 {
12854 if (htab->opd_abi)
12855 {
12856 if (indx < 0x8000)
12857 {
12858 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12859 p += 4;
12860 }
12861 else
12862 {
12863 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12864 p += 4;
12865 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
12866 p);
12867 p += 4;
12868 }
12869 }
12870 bfd_put_32 (htab->glink->owner,
12871 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
12872 indx++;
12873 p += 4;
12874 }
12875 }
12876
12877 /* Build .glink global entry stubs, and PLT relocs for globals. */
12878 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
12879
12880 if (!write_plt_relocs_for_local_syms (info))
12881 return FALSE;
12882
12883 if (htab->brlt != NULL && htab->brlt->size != 0)
12884 {
12885 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
12886 htab->brlt->size);
12887 if (htab->brlt->contents == NULL)
12888 return FALSE;
12889 }
12890 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
12891 {
12892 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
12893 htab->relbrlt->size);
12894 if (htab->relbrlt->contents == NULL)
12895 return FALSE;
12896 }
12897
12898 /* Build the stubs as directed by the stub hash table. */
12899 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
12900
12901 for (group = htab->group; group != NULL; group = group->next)
12902 if (group->needs_save_res)
12903 group->stub_sec->size += htab->sfpr->size;
12904
12905 if (htab->relbrlt != NULL)
12906 htab->relbrlt->reloc_count = 0;
12907
12908 if (htab->params->plt_stub_align != 0)
12909 for (group = htab->group; group != NULL; group = group->next)
12910 if ((stub_sec = group->stub_sec) != NULL)
12911 {
12912 int align = abs (htab->params->plt_stub_align);
12913 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
12914 }
12915
12916 for (group = htab->group; group != NULL; group = group->next)
12917 if (group->needs_save_res)
12918 {
12919 stub_sec = group->stub_sec;
12920 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
12921 htab->sfpr->contents, htab->sfpr->size);
12922 if (htab->params->emit_stub_syms)
12923 {
12924 unsigned int i;
12925
12926 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
12927 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
12928 return FALSE;
12929 }
12930 }
12931
12932 if (htab->glink_eh_frame != NULL
12933 && htab->glink_eh_frame->size != 0)
12934 {
12935 bfd_vma val;
12936 size_t align = 4;
12937
12938 p = htab->glink_eh_frame->contents;
12939 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12940
12941 for (group = htab->group; group != NULL; group = group->next)
12942 if (group->eh_size != 0)
12943 {
12944 /* Offset to stub section. */
12945 val = (group->stub_sec->output_section->vma
12946 + group->stub_sec->output_offset);
12947 val -= (htab->glink_eh_frame->output_section->vma
12948 + htab->glink_eh_frame->output_offset
12949 + (p + 8 - htab->glink_eh_frame->contents));
12950 if (val + 0x80000000 > 0xffffffff)
12951 {
12952 _bfd_error_handler
12953 (_("%s offset too large for .eh_frame sdata4 encoding"),
12954 group->stub_sec->name);
12955 return FALSE;
12956 }
12957 bfd_put_32 (htab->elf.dynobj, val, p + 8);
12958 p += (group->eh_size + 17 + 3) & -4;
12959 }
12960 if (htab->glink != NULL && htab->glink->size != 0)
12961 {
12962 /* Offset to .glink. */
12963 val = (htab->glink->output_section->vma
12964 + htab->glink->output_offset
12965 + 8);
12966 val -= (htab->glink_eh_frame->output_section->vma
12967 + htab->glink_eh_frame->output_offset
12968 + (p + 8 - htab->glink_eh_frame->contents));
12969 if (val + 0x80000000 > 0xffffffff)
12970 {
12971 _bfd_error_handler
12972 (_("%s offset too large for .eh_frame sdata4 encoding"),
12973 htab->glink->name);
12974 return FALSE;
12975 }
12976 bfd_put_32 (htab->elf.dynobj, val, p + 8);
12977 p += (24 + align - 1) & -align;
12978 }
12979 }
12980
12981 for (group = htab->group; group != NULL; group = group->next)
12982 if ((stub_sec = group->stub_sec) != NULL)
12983 {
12984 stub_sec_count += 1;
12985 if (stub_sec->rawsize != stub_sec->size
12986 && (htab->stub_iteration <= STUB_SHRINK_ITER
12987 || stub_sec->rawsize < stub_sec->size))
12988 break;
12989 }
12990
12991 if (group != NULL)
12992 {
12993 htab->stub_error = TRUE;
12994 _bfd_error_handler (_("stubs don't match calculated size"));
12995 }
12996
12997 if (htab->stub_error)
12998 return FALSE;
12999
13000 if (stats != NULL)
13001 {
13002 size_t len;
13003 *stats = bfd_malloc (500);
13004 if (*stats == NULL)
13005 return FALSE;
13006
13007 len = sprintf (*stats,
13008 ngettext ("linker stubs in %u group\n",
13009 "linker stubs in %u groups\n",
13010 stub_sec_count),
13011 stub_sec_count);
13012 sprintf (*stats + len, _(" branch %lu\n"
13013 " branch toc adj %lu\n"
13014 " branch notoc %lu\n"
13015 " branch both %lu\n"
13016 " long branch %lu\n"
13017 " long toc adj %lu\n"
13018 " long notoc %lu\n"
13019 " long both %lu\n"
13020 " plt call %lu\n"
13021 " plt call save %lu\n"
13022 " plt call notoc %lu\n"
13023 " plt call both %lu\n"
13024 " global entry %lu"),
13025 htab->stub_count[ppc_stub_long_branch - 1],
13026 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13027 htab->stub_count[ppc_stub_long_branch_notoc - 1],
13028 htab->stub_count[ppc_stub_long_branch_both - 1],
13029 htab->stub_count[ppc_stub_plt_branch - 1],
13030 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13031 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
13032 htab->stub_count[ppc_stub_plt_branch_both - 1],
13033 htab->stub_count[ppc_stub_plt_call - 1],
13034 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13035 htab->stub_count[ppc_stub_plt_call_notoc - 1],
13036 htab->stub_count[ppc_stub_plt_call_both - 1],
13037 htab->stub_count[ppc_stub_global_entry - 1]);
13038 }
13039 return TRUE;
13040 }
13041
13042 /* What to do when ld finds relocations against symbols defined in
13043 discarded sections. */
13044
13045 static unsigned int
13046 ppc64_elf_action_discarded (asection *sec)
13047 {
13048 if (strcmp (".opd", sec->name) == 0)
13049 return 0;
13050
13051 if (strcmp (".toc", sec->name) == 0)
13052 return 0;
13053
13054 if (strcmp (".toc1", sec->name) == 0)
13055 return 0;
13056
13057 return _bfd_elf_default_action_discarded (sec);
13058 }
13059
13060 /* The RELOCATE_SECTION function is called by the ELF backend linker
13061 to handle the relocations for a section.
13062
13063 The relocs are always passed as Rela structures; if the section
13064 actually uses Rel structures, the r_addend field will always be
13065 zero.
13066
13067 This function is responsible for adjust the section contents as
13068 necessary, and (if using Rela relocs and generating a
13069 relocatable output file) adjusting the reloc addend as
13070 necessary.
13071
13072 This function does not have to worry about setting the reloc
13073 address or the reloc symbol index.
13074
13075 LOCAL_SYMS is a pointer to the swapped in local symbols.
13076
13077 LOCAL_SECTIONS is an array giving the section in the input file
13078 corresponding to the st_shndx field of each local symbol.
13079
13080 The global hash table entry for the global symbols can be found
13081 via elf_sym_hashes (input_bfd).
13082
13083 When generating relocatable output, this function must handle
13084 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13085 going to be the section symbol corresponding to the output
13086 section, which means that the addend must be adjusted
13087 accordingly. */
13088
13089 static bfd_boolean
13090 ppc64_elf_relocate_section (bfd *output_bfd,
13091 struct bfd_link_info *info,
13092 bfd *input_bfd,
13093 asection *input_section,
13094 bfd_byte *contents,
13095 Elf_Internal_Rela *relocs,
13096 Elf_Internal_Sym *local_syms,
13097 asection **local_sections)
13098 {
13099 struct ppc_link_hash_table *htab;
13100 Elf_Internal_Shdr *symtab_hdr;
13101 struct elf_link_hash_entry **sym_hashes;
13102 Elf_Internal_Rela *rel;
13103 Elf_Internal_Rela *wrel;
13104 Elf_Internal_Rela *relend;
13105 Elf_Internal_Rela outrel;
13106 bfd_byte *loc;
13107 struct got_entry **local_got_ents;
13108 bfd_vma TOCstart;
13109 bfd_boolean ret = TRUE;
13110 bfd_boolean is_opd;
13111 /* Assume 'at' branch hints. */
13112 bfd_boolean is_isa_v2 = TRUE;
13113 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13114
13115 /* Initialize howto table if needed. */
13116 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13117 ppc_howto_init ();
13118
13119 htab = ppc_hash_table (info);
13120 if (htab == NULL)
13121 return FALSE;
13122
13123 /* Don't relocate stub sections. */
13124 if (input_section->owner == htab->params->stub_bfd)
13125 return TRUE;
13126
13127 BFD_ASSERT (is_ppc64_elf (input_bfd));
13128
13129 local_got_ents = elf_local_got_ents (input_bfd);
13130 TOCstart = elf_gp (output_bfd);
13131 symtab_hdr = &elf_symtab_hdr (input_bfd);
13132 sym_hashes = elf_sym_hashes (input_bfd);
13133 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13134
13135 rel = wrel = relocs;
13136 relend = relocs + input_section->reloc_count;
13137 for (; rel < relend; wrel++, rel++)
13138 {
13139 enum elf_ppc64_reloc_type r_type;
13140 bfd_vma addend;
13141 bfd_reloc_status_type r;
13142 Elf_Internal_Sym *sym;
13143 asection *sec;
13144 struct elf_link_hash_entry *h_elf;
13145 struct ppc_link_hash_entry *h;
13146 struct ppc_link_hash_entry *fdh;
13147 const char *sym_name;
13148 unsigned long r_symndx, toc_symndx;
13149 bfd_vma toc_addend;
13150 unsigned char tls_mask, tls_gd, tls_type;
13151 unsigned char sym_type;
13152 bfd_vma relocation;
13153 bfd_boolean unresolved_reloc, save_unresolved_reloc;
13154 bfd_boolean warned;
13155 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13156 unsigned int insn;
13157 unsigned int mask;
13158 struct ppc_stub_hash_entry *stub_entry;
13159 bfd_vma max_br_offset;
13160 bfd_vma from;
13161 Elf_Internal_Rela orig_rel;
13162 reloc_howto_type *howto;
13163 struct reloc_howto_struct alt_howto;
13164
13165 again:
13166 orig_rel = *rel;
13167
13168 r_type = ELF64_R_TYPE (rel->r_info);
13169 r_symndx = ELF64_R_SYM (rel->r_info);
13170
13171 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13172 symbol of the previous ADDR64 reloc. The symbol gives us the
13173 proper TOC base to use. */
13174 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13175 && wrel != relocs
13176 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13177 && is_opd)
13178 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13179
13180 sym = NULL;
13181 sec = NULL;
13182 h_elf = NULL;
13183 sym_name = NULL;
13184 unresolved_reloc = FALSE;
13185 warned = FALSE;
13186
13187 if (r_symndx < symtab_hdr->sh_info)
13188 {
13189 /* It's a local symbol. */
13190 struct _opd_sec_data *opd;
13191
13192 sym = local_syms + r_symndx;
13193 sec = local_sections[r_symndx];
13194 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13195 sym_type = ELF64_ST_TYPE (sym->st_info);
13196 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13197 opd = get_opd_info (sec);
13198 if (opd != NULL && opd->adjust != NULL)
13199 {
13200 long adjust = opd->adjust[OPD_NDX (sym->st_value
13201 + rel->r_addend)];
13202 if (adjust == -1)
13203 relocation = 0;
13204 else
13205 {
13206 /* If this is a relocation against the opd section sym
13207 and we have edited .opd, adjust the reloc addend so
13208 that ld -r and ld --emit-relocs output is correct.
13209 If it is a reloc against some other .opd symbol,
13210 then the symbol value will be adjusted later. */
13211 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13212 rel->r_addend += adjust;
13213 else
13214 relocation += adjust;
13215 }
13216 }
13217 }
13218 else
13219 {
13220 bfd_boolean ignored;
13221
13222 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13223 r_symndx, symtab_hdr, sym_hashes,
13224 h_elf, sec, relocation,
13225 unresolved_reloc, warned, ignored);
13226 sym_name = h_elf->root.root.string;
13227 sym_type = h_elf->type;
13228 if (sec != NULL
13229 && sec->owner == output_bfd
13230 && strcmp (sec->name, ".opd") == 0)
13231 {
13232 /* This is a symbol defined in a linker script. All
13233 such are defined in output sections, even those
13234 defined by simple assignment from a symbol defined in
13235 an input section. Transfer the symbol to an
13236 appropriate input .opd section, so that a branch to
13237 this symbol will be mapped to the location specified
13238 by the opd entry. */
13239 struct bfd_link_order *lo;
13240 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13241 if (lo->type == bfd_indirect_link_order)
13242 {
13243 asection *isec = lo->u.indirect.section;
13244 if (h_elf->root.u.def.value >= isec->output_offset
13245 && h_elf->root.u.def.value < (isec->output_offset
13246 + isec->size))
13247 {
13248 h_elf->root.u.def.value -= isec->output_offset;
13249 h_elf->root.u.def.section = isec;
13250 sec = isec;
13251 break;
13252 }
13253 }
13254 }
13255 }
13256 h = (struct ppc_link_hash_entry *) h_elf;
13257
13258 if (sec != NULL && discarded_section (sec))
13259 {
13260 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13261 input_bfd, input_section,
13262 contents + rel->r_offset);
13263 wrel->r_offset = rel->r_offset;
13264 wrel->r_info = 0;
13265 wrel->r_addend = 0;
13266
13267 /* For ld -r, remove relocations in debug sections against
13268 symbols defined in discarded sections. Not done for
13269 non-debug to preserve relocs in .eh_frame which the
13270 eh_frame editing code expects to be present. */
13271 if (bfd_link_relocatable (info)
13272 && (input_section->flags & SEC_DEBUGGING))
13273 wrel--;
13274
13275 continue;
13276 }
13277
13278 if (bfd_link_relocatable (info))
13279 goto copy_reloc;
13280
13281 if (h != NULL && &h->elf == htab->elf.hgot)
13282 {
13283 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13284 sec = bfd_abs_section_ptr;
13285 unresolved_reloc = FALSE;
13286 }
13287
13288 /* TLS optimizations. Replace instruction sequences and relocs
13289 based on information we collected in tls_optimize. We edit
13290 RELOCS so that --emit-relocs will output something sensible
13291 for the final instruction stream. */
13292 tls_mask = 0;
13293 tls_gd = 0;
13294 toc_symndx = 0;
13295 if (h != NULL)
13296 tls_mask = h->tls_mask;
13297 else if (local_got_ents != NULL)
13298 {
13299 struct plt_entry **local_plt = (struct plt_entry **)
13300 (local_got_ents + symtab_hdr->sh_info);
13301 unsigned char *lgot_masks = (unsigned char *)
13302 (local_plt + symtab_hdr->sh_info);
13303 tls_mask = lgot_masks[r_symndx];
13304 }
13305 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
13306 && (r_type == R_PPC64_TLS
13307 || r_type == R_PPC64_TLSGD
13308 || r_type == R_PPC64_TLSLD))
13309 {
13310 /* Check for toc tls entries. */
13311 unsigned char *toc_tls;
13312
13313 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13314 &local_syms, rel, input_bfd))
13315 return FALSE;
13316
13317 if (toc_tls)
13318 tls_mask = *toc_tls;
13319 }
13320
13321 /* Check that tls relocs are used with tls syms, and non-tls
13322 relocs are used with non-tls syms. */
13323 if (r_symndx != STN_UNDEF
13324 && r_type != R_PPC64_NONE
13325 && (h == NULL
13326 || h->elf.root.type == bfd_link_hash_defined
13327 || h->elf.root.type == bfd_link_hash_defweak)
13328 && (IS_PPC64_TLS_RELOC (r_type)
13329 != (sym_type == STT_TLS
13330 || (sym_type == STT_SECTION
13331 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13332 {
13333 if ((tls_mask & TLS_TLS) != 0
13334 && (r_type == R_PPC64_TLS
13335 || r_type == R_PPC64_TLSGD
13336 || r_type == R_PPC64_TLSLD))
13337 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13338 ;
13339 else
13340 info->callbacks->einfo
13341 (!IS_PPC64_TLS_RELOC (r_type)
13342 /* xgettext:c-format */
13343 ? _("%H: %s used with TLS symbol `%pT'\n")
13344 /* xgettext:c-format */
13345 : _("%H: %s used with non-TLS symbol `%pT'\n"),
13346 input_bfd, input_section, rel->r_offset,
13347 ppc64_elf_howto_table[r_type]->name,
13348 sym_name);
13349 }
13350
13351 /* Ensure reloc mapping code below stays sane. */
13352 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13353 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13354 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13355 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13356 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13357 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13358 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13359 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13360 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13361 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13362 abort ();
13363
13364 switch (r_type)
13365 {
13366 default:
13367 break;
13368
13369 case R_PPC64_LO_DS_OPT:
13370 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13371 if ((insn & (0x3f << 26)) != 58u << 26)
13372 abort ();
13373 insn += (14u << 26) - (58u << 26);
13374 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13375 r_type = R_PPC64_TOC16_LO;
13376 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13377 break;
13378
13379 case R_PPC64_TOC16:
13380 case R_PPC64_TOC16_LO:
13381 case R_PPC64_TOC16_DS:
13382 case R_PPC64_TOC16_LO_DS:
13383 {
13384 /* Check for toc tls entries. */
13385 unsigned char *toc_tls;
13386 int retval;
13387
13388 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13389 &local_syms, rel, input_bfd);
13390 if (retval == 0)
13391 return FALSE;
13392
13393 if (toc_tls)
13394 {
13395 tls_mask = *toc_tls;
13396 if (r_type == R_PPC64_TOC16_DS
13397 || r_type == R_PPC64_TOC16_LO_DS)
13398 {
13399 if ((tls_mask & TLS_TLS) != 0
13400 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13401 goto toctprel;
13402 }
13403 else
13404 {
13405 /* If we found a GD reloc pair, then we might be
13406 doing a GD->IE transition. */
13407 if (retval == 2)
13408 {
13409 tls_gd = TLS_TPRELGD;
13410 if ((tls_mask & TLS_TLS) != 0
13411 && (tls_mask & TLS_GD) == 0)
13412 goto tls_ldgd_opt;
13413 }
13414 else if (retval == 3)
13415 {
13416 if ((tls_mask & TLS_TLS) != 0
13417 && (tls_mask & TLS_LD) == 0)
13418 goto tls_ldgd_opt;
13419 }
13420 }
13421 }
13422 }
13423 break;
13424
13425 case R_PPC64_GOT_TPREL16_HI:
13426 case R_PPC64_GOT_TPREL16_HA:
13427 if ((tls_mask & TLS_TLS) != 0
13428 && (tls_mask & TLS_TPREL) == 0)
13429 {
13430 rel->r_offset -= d_offset;
13431 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13432 r_type = R_PPC64_NONE;
13433 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13434 }
13435 break;
13436
13437 case R_PPC64_GOT_TPREL16_DS:
13438 case R_PPC64_GOT_TPREL16_LO_DS:
13439 if ((tls_mask & TLS_TLS) != 0
13440 && (tls_mask & TLS_TPREL) == 0)
13441 {
13442 toctprel:
13443 insn = bfd_get_32 (input_bfd,
13444 contents + rel->r_offset - d_offset);
13445 insn &= 31 << 21;
13446 insn |= 0x3c0d0000; /* addis 0,13,0 */
13447 bfd_put_32 (input_bfd, insn,
13448 contents + rel->r_offset - d_offset);
13449 r_type = R_PPC64_TPREL16_HA;
13450 if (toc_symndx != 0)
13451 {
13452 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13453 rel->r_addend = toc_addend;
13454 /* We changed the symbol. Start over in order to
13455 get h, sym, sec etc. right. */
13456 goto again;
13457 }
13458 else
13459 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13460 }
13461 break;
13462
13463 case R_PPC64_TLS:
13464 if ((tls_mask & TLS_TLS) != 0
13465 && (tls_mask & TLS_TPREL) == 0)
13466 {
13467 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13468 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13469 if (insn == 0)
13470 abort ();
13471 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
13472 /* Was PPC64_TLS which sits on insn boundary, now
13473 PPC64_TPREL16_LO which is at low-order half-word. */
13474 rel->r_offset += d_offset;
13475 r_type = R_PPC64_TPREL16_LO;
13476 if (toc_symndx != 0)
13477 {
13478 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13479 rel->r_addend = toc_addend;
13480 /* We changed the symbol. Start over in order to
13481 get h, sym, sec etc. right. */
13482 goto again;
13483 }
13484 else
13485 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13486 }
13487 break;
13488
13489 case R_PPC64_GOT_TLSGD16_HI:
13490 case R_PPC64_GOT_TLSGD16_HA:
13491 tls_gd = TLS_TPRELGD;
13492 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
13493 goto tls_gdld_hi;
13494 break;
13495
13496 case R_PPC64_GOT_TLSLD16_HI:
13497 case R_PPC64_GOT_TLSLD16_HA:
13498 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
13499 {
13500 tls_gdld_hi:
13501 if ((tls_mask & tls_gd) != 0)
13502 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13503 + R_PPC64_GOT_TPREL16_DS);
13504 else
13505 {
13506 rel->r_offset -= d_offset;
13507 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13508 r_type = R_PPC64_NONE;
13509 }
13510 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13511 }
13512 break;
13513
13514 case R_PPC64_GOT_TLSGD16:
13515 case R_PPC64_GOT_TLSGD16_LO:
13516 tls_gd = TLS_TPRELGD;
13517 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
13518 goto tls_ldgd_opt;
13519 break;
13520
13521 case R_PPC64_GOT_TLSLD16:
13522 case R_PPC64_GOT_TLSLD16_LO:
13523 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
13524 {
13525 unsigned int insn1, insn2;
13526 bfd_vma offset;
13527
13528 tls_ldgd_opt:
13529 offset = (bfd_vma) -1;
13530 /* If not using the newer R_PPC64_TLSGD/LD to mark
13531 __tls_get_addr calls, we must trust that the call
13532 stays with its arg setup insns, ie. that the next
13533 reloc is the __tls_get_addr call associated with
13534 the current reloc. Edit both insns. */
13535 if (input_section->has_tls_get_addr_call
13536 && rel + 1 < relend
13537 && branch_reloc_hash_match (input_bfd, rel + 1,
13538 htab->tls_get_addr,
13539 htab->tls_get_addr_fd))
13540 offset = rel[1].r_offset;
13541 /* We read the low GOT_TLS (or TOC16) insn because we
13542 need to keep the destination reg. It may be
13543 something other than the usual r3, and moved to r3
13544 before the call by intervening code. */
13545 insn1 = bfd_get_32 (input_bfd,
13546 contents + rel->r_offset - d_offset);
13547 if ((tls_mask & tls_gd) != 0)
13548 {
13549 /* IE */
13550 insn1 &= (0x1f << 21) | (0x1f << 16);
13551 insn1 |= 58 << 26; /* ld */
13552 insn2 = 0x7c636a14; /* add 3,3,13 */
13553 if (offset != (bfd_vma) -1)
13554 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13555 if ((tls_mask & TLS_EXPLICIT) == 0)
13556 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13557 + R_PPC64_GOT_TPREL16_DS);
13558 else
13559 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13560 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13561 }
13562 else
13563 {
13564 /* LE */
13565 insn1 &= 0x1f << 21;
13566 insn1 |= 0x3c0d0000; /* addis r,13,0 */
13567 insn2 = 0x38630000; /* addi 3,3,0 */
13568 if (tls_gd == 0)
13569 {
13570 /* Was an LD reloc. */
13571 if (toc_symndx)
13572 sec = local_sections[toc_symndx];
13573 for (r_symndx = 0;
13574 r_symndx < symtab_hdr->sh_info;
13575 r_symndx++)
13576 if (local_sections[r_symndx] == sec)
13577 break;
13578 if (r_symndx >= symtab_hdr->sh_info)
13579 r_symndx = STN_UNDEF;
13580 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13581 if (r_symndx != STN_UNDEF)
13582 rel->r_addend -= (local_syms[r_symndx].st_value
13583 + sec->output_offset
13584 + sec->output_section->vma);
13585 }
13586 else if (toc_symndx != 0)
13587 {
13588 r_symndx = toc_symndx;
13589 rel->r_addend = toc_addend;
13590 }
13591 r_type = R_PPC64_TPREL16_HA;
13592 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13593 if (offset != (bfd_vma) -1)
13594 {
13595 rel[1].r_info = ELF64_R_INFO (r_symndx,
13596 R_PPC64_TPREL16_LO);
13597 rel[1].r_offset = offset + d_offset;
13598 rel[1].r_addend = rel->r_addend;
13599 }
13600 }
13601 bfd_put_32 (input_bfd, insn1,
13602 contents + rel->r_offset - d_offset);
13603 if (offset != (bfd_vma) -1)
13604 bfd_put_32 (input_bfd, insn2, contents + offset);
13605 if ((tls_mask & tls_gd) == 0
13606 && (tls_gd == 0 || toc_symndx != 0))
13607 {
13608 /* We changed the symbol. Start over in order
13609 to get h, sym, sec etc. right. */
13610 goto again;
13611 }
13612 }
13613 break;
13614
13615 case R_PPC64_TLSGD:
13616 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
13617 && rel + 1 < relend)
13618 {
13619 unsigned int insn2;
13620 bfd_vma offset = rel->r_offset;
13621
13622 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
13623 {
13624 bfd_put_32 (output_bfd, NOP, contents + offset);
13625 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13626 break;
13627 }
13628
13629 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
13630 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
13631
13632 if ((tls_mask & TLS_TPRELGD) != 0)
13633 {
13634 /* IE */
13635 r_type = R_PPC64_NONE;
13636 insn2 = 0x7c636a14; /* add 3,3,13 */
13637 }
13638 else
13639 {
13640 /* LE */
13641 if (toc_symndx != 0)
13642 {
13643 r_symndx = toc_symndx;
13644 rel->r_addend = toc_addend;
13645 }
13646 r_type = R_PPC64_TPREL16_LO;
13647 rel->r_offset = offset + d_offset;
13648 insn2 = 0x38630000; /* addi 3,3,0 */
13649 }
13650 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13651 /* Zap the reloc on the _tls_get_addr call too. */
13652 BFD_ASSERT (offset == rel[1].r_offset);
13653 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13654 bfd_put_32 (input_bfd, insn2, contents + offset);
13655 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13656 goto again;
13657 }
13658 break;
13659
13660 case R_PPC64_TLSLD:
13661 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
13662 && rel + 1 < relend)
13663 {
13664 unsigned int insn2;
13665 bfd_vma offset = rel->r_offset;
13666
13667 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
13668 {
13669 bfd_put_32 (output_bfd, NOP, contents + offset);
13670 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13671 break;
13672 }
13673
13674 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
13675 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
13676
13677 if (toc_symndx)
13678 sec = local_sections[toc_symndx];
13679 for (r_symndx = 0;
13680 r_symndx < symtab_hdr->sh_info;
13681 r_symndx++)
13682 if (local_sections[r_symndx] == sec)
13683 break;
13684 if (r_symndx >= symtab_hdr->sh_info)
13685 r_symndx = STN_UNDEF;
13686 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13687 if (r_symndx != STN_UNDEF)
13688 rel->r_addend -= (local_syms[r_symndx].st_value
13689 + sec->output_offset
13690 + sec->output_section->vma);
13691
13692 r_type = R_PPC64_TPREL16_LO;
13693 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13694 rel->r_offset = offset + d_offset;
13695 /* Zap the reloc on the _tls_get_addr call too. */
13696 BFD_ASSERT (offset == rel[1].r_offset);
13697 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13698 insn2 = 0x38630000; /* addi 3,3,0 */
13699 bfd_put_32 (input_bfd, insn2, contents + offset);
13700 goto again;
13701 }
13702 break;
13703
13704 case R_PPC64_DTPMOD64:
13705 if (rel + 1 < relend
13706 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13707 && rel[1].r_offset == rel->r_offset + 8)
13708 {
13709 if ((tls_mask & TLS_GD) == 0)
13710 {
13711 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13712 if ((tls_mask & TLS_TPRELGD) != 0)
13713 r_type = R_PPC64_TPREL64;
13714 else
13715 {
13716 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13717 r_type = R_PPC64_NONE;
13718 }
13719 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13720 }
13721 }
13722 else
13723 {
13724 if ((tls_mask & TLS_LD) == 0)
13725 {
13726 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13727 r_type = R_PPC64_NONE;
13728 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13729 }
13730 }
13731 break;
13732
13733 case R_PPC64_TPREL64:
13734 if ((tls_mask & TLS_TPREL) == 0)
13735 {
13736 r_type = R_PPC64_NONE;
13737 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13738 }
13739 break;
13740
13741 case R_PPC64_ENTRY:
13742 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13743 if (!bfd_link_pic (info)
13744 && !info->traditional_format
13745 && relocation + 0x80008000 <= 0xffffffff)
13746 {
13747 unsigned int insn1, insn2;
13748
13749 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13750 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13751 if ((insn1 & ~0xfffc) == LD_R2_0R12
13752 && insn2 == ADD_R2_R2_R12)
13753 {
13754 bfd_put_32 (input_bfd,
13755 LIS_R2 + PPC_HA (relocation),
13756 contents + rel->r_offset);
13757 bfd_put_32 (input_bfd,
13758 ADDI_R2_R2 + PPC_LO (relocation),
13759 contents + rel->r_offset + 4);
13760 }
13761 }
13762 else
13763 {
13764 relocation -= (rel->r_offset
13765 + input_section->output_offset
13766 + input_section->output_section->vma);
13767 if (relocation + 0x80008000 <= 0xffffffff)
13768 {
13769 unsigned int insn1, insn2;
13770
13771 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13772 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13773 if ((insn1 & ~0xfffc) == LD_R2_0R12
13774 && insn2 == ADD_R2_R2_R12)
13775 {
13776 bfd_put_32 (input_bfd,
13777 ADDIS_R2_R12 + PPC_HA (relocation),
13778 contents + rel->r_offset);
13779 bfd_put_32 (input_bfd,
13780 ADDI_R2_R2 + PPC_LO (relocation),
13781 contents + rel->r_offset + 4);
13782 }
13783 }
13784 }
13785 break;
13786
13787 case R_PPC64_REL16_HA:
13788 /* If we are generating a non-PIC executable, edit
13789 . 0: addis 2,12,.TOC.-0b@ha
13790 . addi 2,2,.TOC.-0b@l
13791 used by ELFv2 global entry points to set up r2, to
13792 . lis 2,.TOC.@ha
13793 . addi 2,2,.TOC.@l
13794 if .TOC. is in range. */
13795 if (!bfd_link_pic (info)
13796 && !info->traditional_format
13797 && !htab->opd_abi
13798 && rel->r_addend == d_offset
13799 && h != NULL && &h->elf == htab->elf.hgot
13800 && rel + 1 < relend
13801 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
13802 && rel[1].r_offset == rel->r_offset + 4
13803 && rel[1].r_addend == rel->r_addend + 4
13804 && relocation + 0x80008000 <= 0xffffffff)
13805 {
13806 unsigned int insn1, insn2;
13807 bfd_vma offset = rel->r_offset - d_offset;
13808 insn1 = bfd_get_32 (input_bfd, contents + offset);
13809 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
13810 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
13811 && (insn2 & 0xffff0000) == ADDI_R2_R2)
13812 {
13813 r_type = R_PPC64_ADDR16_HA;
13814 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13815 rel->r_addend -= d_offset;
13816 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
13817 rel[1].r_addend -= d_offset + 4;
13818 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
13819 }
13820 }
13821 break;
13822 }
13823
13824 /* Handle other relocations that tweak non-addend part of insn. */
13825 insn = 0;
13826 max_br_offset = 1 << 25;
13827 addend = rel->r_addend;
13828 reloc_dest = DEST_NORMAL;
13829 switch (r_type)
13830 {
13831 default:
13832 break;
13833
13834 case R_PPC64_TOCSAVE:
13835 if (relocation + addend == (rel->r_offset
13836 + input_section->output_offset
13837 + input_section->output_section->vma)
13838 && tocsave_find (htab, NO_INSERT,
13839 &local_syms, rel, input_bfd))
13840 {
13841 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13842 if (insn == NOP
13843 || insn == CROR_151515 || insn == CROR_313131)
13844 bfd_put_32 (input_bfd,
13845 STD_R2_0R1 + STK_TOC (htab),
13846 contents + rel->r_offset);
13847 }
13848 break;
13849
13850 /* Branch taken prediction relocations. */
13851 case R_PPC64_ADDR14_BRTAKEN:
13852 case R_PPC64_REL14_BRTAKEN:
13853 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
13854 /* Fall through. */
13855
13856 /* Branch not taken prediction relocations. */
13857 case R_PPC64_ADDR14_BRNTAKEN:
13858 case R_PPC64_REL14_BRNTAKEN:
13859 insn |= bfd_get_32 (input_bfd,
13860 contents + rel->r_offset) & ~(0x01 << 21);
13861 /* Fall through. */
13862
13863 case R_PPC64_REL14:
13864 max_br_offset = 1 << 15;
13865 /* Fall through. */
13866
13867 case R_PPC64_REL24:
13868 case R_PPC64_REL24_NOTOC:
13869 case R_PPC64_PLTCALL:
13870 /* Calls to functions with a different TOC, such as calls to
13871 shared objects, need to alter the TOC pointer. This is
13872 done using a linkage stub. A REL24 branching to these
13873 linkage stubs needs to be followed by a nop, as the nop
13874 will be replaced with an instruction to restore the TOC
13875 base pointer. */
13876 fdh = h;
13877 if (h != NULL
13878 && h->oh != NULL
13879 && h->oh->is_func_descriptor)
13880 fdh = ppc_follow_link (h->oh);
13881 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13882 htab);
13883 if (r_type == R_PPC64_PLTCALL
13884 && stub_entry != NULL
13885 && stub_entry->stub_type >= ppc_stub_plt_call
13886 && stub_entry->stub_type <= ppc_stub_plt_call_both)
13887 stub_entry = NULL;
13888
13889 if (stub_entry != NULL
13890 && ((stub_entry->stub_type >= ppc_stub_plt_call
13891 && stub_entry->stub_type <= ppc_stub_plt_call_both)
13892 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13893 || stub_entry->stub_type == ppc_stub_plt_branch_both
13894 || stub_entry->stub_type == ppc_stub_long_branch_r2off
13895 || stub_entry->stub_type == ppc_stub_long_branch_both))
13896 {
13897 bfd_boolean can_plt_call = FALSE;
13898
13899 if (stub_entry->stub_type == ppc_stub_plt_call
13900 && !htab->opd_abi
13901 && htab->params->plt_localentry0 != 0
13902 && is_elfv2_localentry0 (&h->elf))
13903 {
13904 /* The function doesn't use or change r2. */
13905 can_plt_call = TRUE;
13906 }
13907 else if (r_type == R_PPC64_REL24_NOTOC)
13908 {
13909 /* NOTOC calls don't need to restore r2. */
13910 can_plt_call = TRUE;
13911 }
13912
13913 /* All of these stubs may modify r2, so there must be a
13914 branch and link followed by a nop. The nop is
13915 replaced by an insn to restore r2. */
13916 else if (rel->r_offset + 8 <= input_section->size)
13917 {
13918 unsigned long br;
13919
13920 br = bfd_get_32 (input_bfd,
13921 contents + rel->r_offset);
13922 if ((br & 1) != 0)
13923 {
13924 unsigned long nop;
13925
13926 nop = bfd_get_32 (input_bfd,
13927 contents + rel->r_offset + 4);
13928 if (nop == LD_R2_0R1 + STK_TOC (htab))
13929 can_plt_call = TRUE;
13930 else if (nop == NOP
13931 || nop == CROR_151515
13932 || nop == CROR_313131)
13933 {
13934 if (h != NULL
13935 && (h == htab->tls_get_addr_fd
13936 || h == htab->tls_get_addr)
13937 && htab->params->tls_get_addr_opt)
13938 {
13939 /* Special stub used, leave nop alone. */
13940 }
13941 else
13942 bfd_put_32 (input_bfd,
13943 LD_R2_0R1 + STK_TOC (htab),
13944 contents + rel->r_offset + 4);
13945 can_plt_call = TRUE;
13946 }
13947 }
13948 }
13949
13950 if (!can_plt_call && h != NULL)
13951 {
13952 const char *name = h->elf.root.root.string;
13953
13954 if (*name == '.')
13955 ++name;
13956
13957 if (strncmp (name, "__libc_start_main", 17) == 0
13958 && (name[17] == 0 || name[17] == '@'))
13959 {
13960 /* Allow crt1 branch to go via a toc adjusting
13961 stub. Other calls that never return could do
13962 the same, if we could detect such. */
13963 can_plt_call = TRUE;
13964 }
13965 }
13966
13967 if (!can_plt_call)
13968 {
13969 /* g++ as of 20130507 emits self-calls without a
13970 following nop. This is arguably wrong since we
13971 have conflicting information. On the one hand a
13972 global symbol and on the other a local call
13973 sequence, but don't error for this special case.
13974 It isn't possible to cheaply verify we have
13975 exactly such a call. Allow all calls to the same
13976 section. */
13977 asection *code_sec = sec;
13978
13979 if (get_opd_info (sec) != NULL)
13980 {
13981 bfd_vma off = (relocation + addend
13982 - sec->output_section->vma
13983 - sec->output_offset);
13984
13985 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
13986 }
13987 if (code_sec == input_section)
13988 can_plt_call = TRUE;
13989 }
13990
13991 if (!can_plt_call)
13992 {
13993 if (stub_entry->stub_type >= ppc_stub_plt_call
13994 && stub_entry->stub_type <= ppc_stub_plt_call_both)
13995 info->callbacks->einfo
13996 /* xgettext:c-format */
13997 (_("%H: call to `%pT' lacks nop, can't restore toc; "
13998 "recompile with -fPIC\n"),
13999 input_bfd, input_section, rel->r_offset, sym_name);
14000 else
14001 info->callbacks->einfo
14002 /* xgettext:c-format */
14003 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14004 "(-mcmodel=small toc adjust stub)\n"),
14005 input_bfd, input_section, rel->r_offset, sym_name);
14006
14007 bfd_set_error (bfd_error_bad_value);
14008 ret = FALSE;
14009 }
14010
14011 if (can_plt_call
14012 && stub_entry->stub_type >= ppc_stub_plt_call
14013 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14014 unresolved_reloc = FALSE;
14015 }
14016
14017 if ((stub_entry == NULL
14018 || stub_entry->stub_type == ppc_stub_long_branch
14019 || stub_entry->stub_type == ppc_stub_plt_branch)
14020 && get_opd_info (sec) != NULL)
14021 {
14022 /* The branch destination is the value of the opd entry. */
14023 bfd_vma off = (relocation + addend
14024 - sec->output_section->vma
14025 - sec->output_offset);
14026 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14027 if (dest != (bfd_vma) -1)
14028 {
14029 relocation = dest;
14030 addend = 0;
14031 reloc_dest = DEST_OPD;
14032 }
14033 }
14034
14035 /* If the branch is out of reach we ought to have a long
14036 branch stub. */
14037 from = (rel->r_offset
14038 + input_section->output_offset
14039 + input_section->output_section->vma);
14040
14041 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14042 ? fdh->elf.other
14043 : sym->st_other);
14044
14045 if (stub_entry != NULL
14046 && (stub_entry->stub_type == ppc_stub_long_branch
14047 || stub_entry->stub_type == ppc_stub_plt_branch)
14048 && (r_type == R_PPC64_ADDR14_BRTAKEN
14049 || r_type == R_PPC64_ADDR14_BRNTAKEN
14050 || (relocation + addend - from + max_br_offset
14051 < 2 * max_br_offset)))
14052 /* Don't use the stub if this branch is in range. */
14053 stub_entry = NULL;
14054
14055 if (stub_entry != NULL
14056 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
14057 || stub_entry->stub_type == ppc_stub_long_branch_both
14058 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
14059 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14060 && (r_type != R_PPC64_REL24_NOTOC
14061 || ((fdh ? fdh->elf.other : sym->st_other)
14062 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
14063 && (relocation + addend - from + max_br_offset
14064 < 2 * max_br_offset))
14065 stub_entry = NULL;
14066
14067 if (stub_entry != NULL
14068 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
14069 || stub_entry->stub_type == ppc_stub_long_branch_both
14070 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14071 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14072 && r_type == R_PPC64_REL24_NOTOC
14073 && (relocation + addend - from + max_br_offset
14074 < 2 * max_br_offset))
14075 stub_entry = NULL;
14076
14077 if (stub_entry != NULL)
14078 {
14079 /* Munge up the value and addend so that we call the stub
14080 rather than the procedure directly. */
14081 asection *stub_sec = stub_entry->group->stub_sec;
14082
14083 if (stub_entry->stub_type == ppc_stub_save_res)
14084 relocation += (stub_sec->output_offset
14085 + stub_sec->output_section->vma
14086 + stub_sec->size - htab->sfpr->size
14087 - htab->sfpr->output_offset
14088 - htab->sfpr->output_section->vma);
14089 else
14090 relocation = (stub_entry->stub_offset
14091 + stub_sec->output_offset
14092 + stub_sec->output_section->vma);
14093 addend = 0;
14094 reloc_dest = DEST_STUB;
14095
14096 if (((stub_entry->stub_type == ppc_stub_plt_call
14097 && ALWAYS_EMIT_R2SAVE)
14098 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14099 || stub_entry->stub_type == ppc_stub_plt_call_both)
14100 && !(h != NULL
14101 && (h == htab->tls_get_addr_fd
14102 || h == htab->tls_get_addr)
14103 && htab->params->tls_get_addr_opt)
14104 && rel + 1 < relend
14105 && rel[1].r_offset == rel->r_offset + 4
14106 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14107 relocation += 4;
14108 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
14109 || stub_entry->stub_type == ppc_stub_plt_branch_both
14110 || stub_entry->stub_type == ppc_stub_plt_call_both)
14111 && r_type == R_PPC64_REL24_NOTOC)
14112 relocation += 4;
14113 }
14114
14115 if (insn != 0)
14116 {
14117 if (is_isa_v2)
14118 {
14119 /* Set 'a' bit. This is 0b00010 in BO field for branch
14120 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14121 for branch on CTR insns (BO == 1a00t or 1a01t). */
14122 if ((insn & (0x14 << 21)) == (0x04 << 21))
14123 insn |= 0x02 << 21;
14124 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14125 insn |= 0x08 << 21;
14126 else
14127 break;
14128 }
14129 else
14130 {
14131 /* Invert 'y' bit if not the default. */
14132 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14133 insn ^= 0x01 << 21;
14134 }
14135
14136 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14137 }
14138
14139 /* NOP out calls to undefined weak functions.
14140 We can thus call a weak function without first
14141 checking whether the function is defined. */
14142 else if (h != NULL
14143 && h->elf.root.type == bfd_link_hash_undefweak
14144 && h->elf.dynindx == -1
14145 && (r_type == R_PPC64_REL24
14146 || r_type == R_PPC64_REL24_NOTOC)
14147 && relocation == 0
14148 && addend == 0)
14149 {
14150 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14151 goto copy_reloc;
14152 }
14153 break;
14154 }
14155
14156 /* Set `addend'. */
14157 tls_type = 0;
14158 save_unresolved_reloc = unresolved_reloc;
14159 switch (r_type)
14160 {
14161 default:
14162 /* xgettext:c-format */
14163 _bfd_error_handler (_("%pB: %s unsupported"),
14164 input_bfd, ppc64_elf_howto_table[r_type]->name);
14165
14166 bfd_set_error (bfd_error_bad_value);
14167 ret = FALSE;
14168 goto copy_reloc;
14169
14170 case R_PPC64_NONE:
14171 case R_PPC64_TLS:
14172 case R_PPC64_TLSGD:
14173 case R_PPC64_TLSLD:
14174 case R_PPC64_TOCSAVE:
14175 case R_PPC64_GNU_VTINHERIT:
14176 case R_PPC64_GNU_VTENTRY:
14177 case R_PPC64_ENTRY:
14178 goto copy_reloc;
14179
14180 /* GOT16 relocations. Like an ADDR16 using the symbol's
14181 address in the GOT as relocation value instead of the
14182 symbol's value itself. Also, create a GOT entry for the
14183 symbol and put the symbol value there. */
14184 case R_PPC64_GOT_TLSGD16:
14185 case R_PPC64_GOT_TLSGD16_LO:
14186 case R_PPC64_GOT_TLSGD16_HI:
14187 case R_PPC64_GOT_TLSGD16_HA:
14188 tls_type = TLS_TLS | TLS_GD;
14189 goto dogot;
14190
14191 case R_PPC64_GOT_TLSLD16:
14192 case R_PPC64_GOT_TLSLD16_LO:
14193 case R_PPC64_GOT_TLSLD16_HI:
14194 case R_PPC64_GOT_TLSLD16_HA:
14195 tls_type = TLS_TLS | TLS_LD;
14196 goto dogot;
14197
14198 case R_PPC64_GOT_TPREL16_DS:
14199 case R_PPC64_GOT_TPREL16_LO_DS:
14200 case R_PPC64_GOT_TPREL16_HI:
14201 case R_PPC64_GOT_TPREL16_HA:
14202 tls_type = TLS_TLS | TLS_TPREL;
14203 goto dogot;
14204
14205 case R_PPC64_GOT_DTPREL16_DS:
14206 case R_PPC64_GOT_DTPREL16_LO_DS:
14207 case R_PPC64_GOT_DTPREL16_HI:
14208 case R_PPC64_GOT_DTPREL16_HA:
14209 tls_type = TLS_TLS | TLS_DTPREL;
14210 goto dogot;
14211
14212 case R_PPC64_GOT16:
14213 case R_PPC64_GOT16_LO:
14214 case R_PPC64_GOT16_HI:
14215 case R_PPC64_GOT16_HA:
14216 case R_PPC64_GOT16_DS:
14217 case R_PPC64_GOT16_LO_DS:
14218 dogot:
14219 {
14220 /* Relocation is to the entry for this symbol in the global
14221 offset table. */
14222 asection *got;
14223 bfd_vma *offp;
14224 bfd_vma off;
14225 unsigned long indx = 0;
14226 struct got_entry *ent;
14227
14228 if (tls_type == (TLS_TLS | TLS_LD)
14229 && (h == NULL
14230 || !h->elf.def_dynamic))
14231 ent = ppc64_tlsld_got (input_bfd);
14232 else
14233 {
14234 if (h != NULL)
14235 {
14236 if (!htab->elf.dynamic_sections_created
14237 || h->elf.dynindx == -1
14238 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14239 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
14240 /* This is actually a static link, or it is a
14241 -Bsymbolic link and the symbol is defined
14242 locally, or the symbol was forced to be local
14243 because of a version file. */
14244 ;
14245 else
14246 {
14247 indx = h->elf.dynindx;
14248 unresolved_reloc = FALSE;
14249 }
14250 ent = h->elf.got.glist;
14251 }
14252 else
14253 {
14254 if (local_got_ents == NULL)
14255 abort ();
14256 ent = local_got_ents[r_symndx];
14257 }
14258
14259 for (; ent != NULL; ent = ent->next)
14260 if (ent->addend == orig_rel.r_addend
14261 && ent->owner == input_bfd
14262 && ent->tls_type == tls_type)
14263 break;
14264 }
14265
14266 if (ent == NULL)
14267 abort ();
14268 if (ent->is_indirect)
14269 ent = ent->got.ent;
14270 offp = &ent->got.offset;
14271 got = ppc64_elf_tdata (ent->owner)->got;
14272 if (got == NULL)
14273 abort ();
14274
14275 /* The offset must always be a multiple of 8. We use the
14276 least significant bit to record whether we have already
14277 processed this entry. */
14278 off = *offp;
14279 if ((off & 1) != 0)
14280 off &= ~1;
14281 else
14282 {
14283 /* Generate relocs for the dynamic linker, except in
14284 the case of TLSLD where we'll use one entry per
14285 module. */
14286 asection *relgot;
14287 bfd_boolean ifunc;
14288
14289 *offp = off | 1;
14290 relgot = NULL;
14291 ifunc = (h != NULL
14292 ? h->elf.type == STT_GNU_IFUNC
14293 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14294 if (ifunc)
14295 {
14296 relgot = htab->elf.irelplt;
14297 if (indx == 0)
14298 htab->local_ifunc_resolver = 1;
14299 else if (is_static_defined (&h->elf))
14300 htab->maybe_local_ifunc_resolver = 1;
14301 }
14302 else if (indx != 0
14303 || (bfd_link_pic (info)
14304 && (h == NULL
14305 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
14306 || (tls_type == (TLS_TLS | TLS_LD)
14307 && !h->elf.def_dynamic))
14308 && !(tls_type == (TLS_TLS | TLS_TPREL)
14309 && bfd_link_executable (info)
14310 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
14311 relgot = ppc64_elf_tdata (ent->owner)->relgot;
14312 if (relgot != NULL)
14313 {
14314 outrel.r_offset = (got->output_section->vma
14315 + got->output_offset
14316 + off);
14317 outrel.r_addend = addend;
14318 if (tls_type & (TLS_LD | TLS_GD))
14319 {
14320 outrel.r_addend = 0;
14321 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14322 if (tls_type == (TLS_TLS | TLS_GD))
14323 {
14324 loc = relgot->contents;
14325 loc += (relgot->reloc_count++
14326 * sizeof (Elf64_External_Rela));
14327 bfd_elf64_swap_reloca_out (output_bfd,
14328 &outrel, loc);
14329 outrel.r_offset += 8;
14330 outrel.r_addend = addend;
14331 outrel.r_info
14332 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14333 }
14334 }
14335 else if (tls_type == (TLS_TLS | TLS_DTPREL))
14336 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14337 else if (tls_type == (TLS_TLS | TLS_TPREL))
14338 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14339 else if (indx != 0)
14340 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14341 else
14342 {
14343 if (ifunc)
14344 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14345 else
14346 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14347
14348 /* Write the .got section contents for the sake
14349 of prelink. */
14350 loc = got->contents + off;
14351 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14352 loc);
14353 }
14354
14355 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14356 {
14357 outrel.r_addend += relocation;
14358 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14359 {
14360 if (htab->elf.tls_sec == NULL)
14361 outrel.r_addend = 0;
14362 else
14363 outrel.r_addend -= htab->elf.tls_sec->vma;
14364 }
14365 }
14366 loc = relgot->contents;
14367 loc += (relgot->reloc_count++
14368 * sizeof (Elf64_External_Rela));
14369 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14370 }
14371
14372 /* Init the .got section contents here if we're not
14373 emitting a reloc. */
14374 else
14375 {
14376 relocation += addend;
14377 if (tls_type != 0)
14378 {
14379 if (htab->elf.tls_sec == NULL)
14380 relocation = 0;
14381 else
14382 {
14383 if (tls_type & TLS_LD)
14384 relocation = 0;
14385 else
14386 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14387 if (tls_type & TLS_TPREL)
14388 relocation += DTP_OFFSET - TP_OFFSET;
14389 }
14390
14391 if (tls_type & (TLS_GD | TLS_LD))
14392 {
14393 bfd_put_64 (output_bfd, relocation,
14394 got->contents + off + 8);
14395 relocation = 1;
14396 }
14397 }
14398 bfd_put_64 (output_bfd, relocation,
14399 got->contents + off);
14400 }
14401 }
14402
14403 if (off >= (bfd_vma) -2)
14404 abort ();
14405
14406 relocation = got->output_section->vma + got->output_offset + off;
14407 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14408 }
14409 break;
14410
14411 case R_PPC64_PLT16_HA:
14412 case R_PPC64_PLT16_HI:
14413 case R_PPC64_PLT16_LO:
14414 case R_PPC64_PLT16_LO_DS:
14415 case R_PPC64_PLT32:
14416 case R_PPC64_PLT64:
14417 case R_PPC64_PLTSEQ:
14418 case R_PPC64_PLTCALL:
14419 /* Relocation is to the entry for this symbol in the
14420 procedure linkage table. */
14421 unresolved_reloc = TRUE;
14422 {
14423 struct plt_entry **plt_list = NULL;
14424 if (h != NULL)
14425 plt_list = &h->elf.plt.plist;
14426 else if (local_got_ents != NULL)
14427 {
14428 struct plt_entry **local_plt = (struct plt_entry **)
14429 (local_got_ents + symtab_hdr->sh_info);
14430 plt_list = local_plt + r_symndx;
14431 }
14432 if (plt_list)
14433 {
14434 struct plt_entry *ent;
14435
14436 for (ent = *plt_list; ent != NULL; ent = ent->next)
14437 if (ent->plt.offset != (bfd_vma) -1
14438 && ent->addend == orig_rel.r_addend)
14439 {
14440 asection *plt;
14441 bfd_vma got;
14442
14443 plt = htab->elf.splt;
14444 if (!htab->elf.dynamic_sections_created
14445 || h == NULL
14446 || h->elf.dynindx == -1)
14447 {
14448 if (h != NULL
14449 ? h->elf.type == STT_GNU_IFUNC
14450 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14451 plt = htab->elf.iplt;
14452 else
14453 plt = htab->pltlocal;
14454 }
14455 relocation = (plt->output_section->vma
14456 + plt->output_offset
14457 + ent->plt.offset);
14458 if (r_type == R_PPC64_PLT16_HA
14459 || r_type == R_PPC64_PLT16_HI
14460 || r_type == R_PPC64_PLT16_LO
14461 || r_type == R_PPC64_PLT16_LO_DS)
14462 {
14463 got = (elf_gp (output_bfd)
14464 + htab->sec_info[input_section->id].toc_off);
14465 relocation -= got;
14466 }
14467 addend = 0;
14468 unresolved_reloc = FALSE;
14469 break;
14470 }
14471 }
14472 }
14473 break;
14474
14475 case R_PPC64_TOC:
14476 /* Relocation value is TOC base. */
14477 relocation = TOCstart;
14478 if (r_symndx == STN_UNDEF)
14479 relocation += htab->sec_info[input_section->id].toc_off;
14480 else if (unresolved_reloc)
14481 ;
14482 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14483 relocation += htab->sec_info[sec->id].toc_off;
14484 else
14485 unresolved_reloc = TRUE;
14486 goto dodyn;
14487
14488 /* TOC16 relocs. We want the offset relative to the TOC base,
14489 which is the address of the start of the TOC plus 0x8000.
14490 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14491 in this order. */
14492 case R_PPC64_TOC16:
14493 case R_PPC64_TOC16_LO:
14494 case R_PPC64_TOC16_HI:
14495 case R_PPC64_TOC16_DS:
14496 case R_PPC64_TOC16_LO_DS:
14497 case R_PPC64_TOC16_HA:
14498 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14499 break;
14500
14501 /* Relocate against the beginning of the section. */
14502 case R_PPC64_SECTOFF:
14503 case R_PPC64_SECTOFF_LO:
14504 case R_PPC64_SECTOFF_HI:
14505 case R_PPC64_SECTOFF_DS:
14506 case R_PPC64_SECTOFF_LO_DS:
14507 case R_PPC64_SECTOFF_HA:
14508 if (sec != NULL)
14509 addend -= sec->output_section->vma;
14510 break;
14511
14512 case R_PPC64_REL16:
14513 case R_PPC64_REL16_LO:
14514 case R_PPC64_REL16_HI:
14515 case R_PPC64_REL16_HA:
14516 case R_PPC64_REL16DX_HA:
14517 break;
14518
14519 case R_PPC64_REL14:
14520 case R_PPC64_REL14_BRNTAKEN:
14521 case R_PPC64_REL14_BRTAKEN:
14522 case R_PPC64_REL24:
14523 case R_PPC64_REL24_NOTOC:
14524 break;
14525
14526 case R_PPC64_TPREL16:
14527 case R_PPC64_TPREL16_LO:
14528 case R_PPC64_TPREL16_HI:
14529 case R_PPC64_TPREL16_HA:
14530 case R_PPC64_TPREL16_DS:
14531 case R_PPC64_TPREL16_LO_DS:
14532 case R_PPC64_TPREL16_HIGH:
14533 case R_PPC64_TPREL16_HIGHA:
14534 case R_PPC64_TPREL16_HIGHER:
14535 case R_PPC64_TPREL16_HIGHERA:
14536 case R_PPC64_TPREL16_HIGHEST:
14537 case R_PPC64_TPREL16_HIGHESTA:
14538 if (h != NULL
14539 && h->elf.root.type == bfd_link_hash_undefweak
14540 && h->elf.dynindx == -1)
14541 {
14542 /* Make this relocation against an undefined weak symbol
14543 resolve to zero. This is really just a tweak, since
14544 code using weak externs ought to check that they are
14545 defined before using them. */
14546 bfd_byte *p = contents + rel->r_offset - d_offset;
14547
14548 insn = bfd_get_32 (input_bfd, p);
14549 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14550 if (insn != 0)
14551 bfd_put_32 (input_bfd, insn, p);
14552 break;
14553 }
14554 if (htab->elf.tls_sec != NULL)
14555 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14556 /* The TPREL16 relocs shouldn't really be used in shared
14557 libs or with non-local symbols as that will result in
14558 DT_TEXTREL being set, but support them anyway. */
14559 goto dodyn;
14560
14561 case R_PPC64_DTPREL16:
14562 case R_PPC64_DTPREL16_LO:
14563 case R_PPC64_DTPREL16_HI:
14564 case R_PPC64_DTPREL16_HA:
14565 case R_PPC64_DTPREL16_DS:
14566 case R_PPC64_DTPREL16_LO_DS:
14567 case R_PPC64_DTPREL16_HIGH:
14568 case R_PPC64_DTPREL16_HIGHA:
14569 case R_PPC64_DTPREL16_HIGHER:
14570 case R_PPC64_DTPREL16_HIGHERA:
14571 case R_PPC64_DTPREL16_HIGHEST:
14572 case R_PPC64_DTPREL16_HIGHESTA:
14573 if (htab->elf.tls_sec != NULL)
14574 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14575 break;
14576
14577 case R_PPC64_ADDR64_LOCAL:
14578 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14579 ? h->elf.other
14580 : sym->st_other);
14581 break;
14582
14583 case R_PPC64_DTPMOD64:
14584 relocation = 1;
14585 addend = 0;
14586 goto dodyn;
14587
14588 case R_PPC64_TPREL64:
14589 if (htab->elf.tls_sec != NULL)
14590 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14591 goto dodyn;
14592
14593 case R_PPC64_DTPREL64:
14594 if (htab->elf.tls_sec != NULL)
14595 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14596 /* Fall through. */
14597
14598 /* Relocations that may need to be propagated if this is a
14599 dynamic object. */
14600 case R_PPC64_REL30:
14601 case R_PPC64_REL32:
14602 case R_PPC64_REL64:
14603 case R_PPC64_ADDR14:
14604 case R_PPC64_ADDR14_BRNTAKEN:
14605 case R_PPC64_ADDR14_BRTAKEN:
14606 case R_PPC64_ADDR16:
14607 case R_PPC64_ADDR16_DS:
14608 case R_PPC64_ADDR16_HA:
14609 case R_PPC64_ADDR16_HI:
14610 case R_PPC64_ADDR16_HIGH:
14611 case R_PPC64_ADDR16_HIGHA:
14612 case R_PPC64_ADDR16_HIGHER:
14613 case R_PPC64_ADDR16_HIGHERA:
14614 case R_PPC64_ADDR16_HIGHEST:
14615 case R_PPC64_ADDR16_HIGHESTA:
14616 case R_PPC64_ADDR16_LO:
14617 case R_PPC64_ADDR16_LO_DS:
14618 case R_PPC64_ADDR24:
14619 case R_PPC64_ADDR32:
14620 case R_PPC64_ADDR64:
14621 case R_PPC64_UADDR16:
14622 case R_PPC64_UADDR32:
14623 case R_PPC64_UADDR64:
14624 dodyn:
14625 if ((input_section->flags & SEC_ALLOC) == 0)
14626 break;
14627
14628 if (NO_OPD_RELOCS && is_opd)
14629 break;
14630
14631 if (bfd_link_pic (info)
14632 ? ((h == NULL
14633 || h->dyn_relocs != NULL)
14634 && ((h != NULL && pc_dynrelocs (h))
14635 || must_be_dyn_reloc (info, r_type)))
14636 : (h != NULL
14637 ? h->dyn_relocs != NULL
14638 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14639 {
14640 bfd_boolean skip, relocate;
14641 asection *sreloc;
14642 bfd_vma out_off;
14643 long indx = 0;
14644
14645 /* When generating a dynamic object, these relocations
14646 are copied into the output file to be resolved at run
14647 time. */
14648
14649 skip = FALSE;
14650 relocate = FALSE;
14651
14652 out_off = _bfd_elf_section_offset (output_bfd, info,
14653 input_section, rel->r_offset);
14654 if (out_off == (bfd_vma) -1)
14655 skip = TRUE;
14656 else if (out_off == (bfd_vma) -2)
14657 skip = TRUE, relocate = TRUE;
14658 out_off += (input_section->output_section->vma
14659 + input_section->output_offset);
14660 outrel.r_offset = out_off;
14661 outrel.r_addend = rel->r_addend;
14662
14663 /* Optimize unaligned reloc use. */
14664 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14665 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14666 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14667 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14668 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14669 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14670 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14671 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14672 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14673
14674 if (skip)
14675 memset (&outrel, 0, sizeof outrel);
14676 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14677 && !is_opd
14678 && r_type != R_PPC64_TOC)
14679 {
14680 indx = h->elf.dynindx;
14681 BFD_ASSERT (indx != -1);
14682 outrel.r_info = ELF64_R_INFO (indx, r_type);
14683 }
14684 else
14685 {
14686 /* This symbol is local, or marked to become local,
14687 or this is an opd section reloc which must point
14688 at a local function. */
14689 outrel.r_addend += relocation;
14690 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14691 {
14692 if (is_opd && h != NULL)
14693 {
14694 /* Lie about opd entries. This case occurs
14695 when building shared libraries and we
14696 reference a function in another shared
14697 lib. The same thing happens for a weak
14698 definition in an application that's
14699 overridden by a strong definition in a
14700 shared lib. (I believe this is a generic
14701 bug in binutils handling of weak syms.)
14702 In these cases we won't use the opd
14703 entry in this lib. */
14704 unresolved_reloc = FALSE;
14705 }
14706 if (!is_opd
14707 && r_type == R_PPC64_ADDR64
14708 && (h != NULL
14709 ? h->elf.type == STT_GNU_IFUNC
14710 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14711 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14712 else
14713 {
14714 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14715
14716 /* We need to relocate .opd contents for ld.so.
14717 Prelink also wants simple and consistent rules
14718 for relocs. This make all RELATIVE relocs have
14719 *r_offset equal to r_addend. */
14720 relocate = TRUE;
14721 }
14722 }
14723 else
14724 {
14725 if (h != NULL
14726 ? h->elf.type == STT_GNU_IFUNC
14727 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14728 {
14729 info->callbacks->einfo
14730 /* xgettext:c-format */
14731 (_("%H: %s for indirect "
14732 "function `%pT' unsupported\n"),
14733 input_bfd, input_section, rel->r_offset,
14734 ppc64_elf_howto_table[r_type]->name,
14735 sym_name);
14736 ret = FALSE;
14737 }
14738 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14739 ;
14740 else if (sec == NULL || sec->owner == NULL)
14741 {
14742 bfd_set_error (bfd_error_bad_value);
14743 return FALSE;
14744 }
14745 else
14746 {
14747 asection *osec;
14748
14749 osec = sec->output_section;
14750 indx = elf_section_data (osec)->dynindx;
14751
14752 if (indx == 0)
14753 {
14754 if ((osec->flags & SEC_READONLY) == 0
14755 && htab->elf.data_index_section != NULL)
14756 osec = htab->elf.data_index_section;
14757 else
14758 osec = htab->elf.text_index_section;
14759 indx = elf_section_data (osec)->dynindx;
14760 }
14761 BFD_ASSERT (indx != 0);
14762
14763 /* We are turning this relocation into one
14764 against a section symbol, so subtract out
14765 the output section's address but not the
14766 offset of the input section in the output
14767 section. */
14768 outrel.r_addend -= osec->vma;
14769 }
14770
14771 outrel.r_info = ELF64_R_INFO (indx, r_type);
14772 }
14773 }
14774
14775 sreloc = elf_section_data (input_section)->sreloc;
14776 if (h != NULL
14777 ? h->elf.type == STT_GNU_IFUNC
14778 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14779 {
14780 sreloc = htab->elf.irelplt;
14781 if (indx == 0)
14782 htab->local_ifunc_resolver = 1;
14783 else if (is_static_defined (&h->elf))
14784 htab->maybe_local_ifunc_resolver = 1;
14785 }
14786 if (sreloc == NULL)
14787 abort ();
14788
14789 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14790 >= sreloc->size)
14791 abort ();
14792 loc = sreloc->contents;
14793 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14794 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14795
14796 /* If this reloc is against an external symbol, it will
14797 be computed at runtime, so there's no need to do
14798 anything now. However, for the sake of prelink ensure
14799 that the section contents are a known value. */
14800 if (!relocate)
14801 {
14802 unresolved_reloc = FALSE;
14803 /* The value chosen here is quite arbitrary as ld.so
14804 ignores section contents except for the special
14805 case of .opd where the contents might be accessed
14806 before relocation. Choose zero, as that won't
14807 cause reloc overflow. */
14808 relocation = 0;
14809 addend = 0;
14810 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14811 to improve backward compatibility with older
14812 versions of ld. */
14813 if (r_type == R_PPC64_ADDR64)
14814 addend = outrel.r_addend;
14815 /* Adjust pc_relative relocs to have zero in *r_offset. */
14816 else if (ppc64_elf_howto_table[r_type]->pc_relative)
14817 addend = outrel.r_offset;
14818 }
14819 }
14820 break;
14821
14822 case R_PPC64_COPY:
14823 case R_PPC64_GLOB_DAT:
14824 case R_PPC64_JMP_SLOT:
14825 case R_PPC64_JMP_IREL:
14826 case R_PPC64_RELATIVE:
14827 /* We shouldn't ever see these dynamic relocs in relocatable
14828 files. */
14829 /* Fall through. */
14830
14831 case R_PPC64_PLTGOT16:
14832 case R_PPC64_PLTGOT16_DS:
14833 case R_PPC64_PLTGOT16_HA:
14834 case R_PPC64_PLTGOT16_HI:
14835 case R_PPC64_PLTGOT16_LO:
14836 case R_PPC64_PLTGOT16_LO_DS:
14837 case R_PPC64_PLTREL32:
14838 case R_PPC64_PLTREL64:
14839 /* These ones haven't been implemented yet. */
14840
14841 info->callbacks->einfo
14842 /* xgettext:c-format */
14843 (_("%P: %pB: %s is not supported for `%pT'\n"),
14844 input_bfd,
14845 ppc64_elf_howto_table[r_type]->name, sym_name);
14846
14847 bfd_set_error (bfd_error_invalid_operation);
14848 ret = FALSE;
14849 goto copy_reloc;
14850 }
14851
14852 /* Multi-instruction sequences that access the TOC can be
14853 optimized, eg. addis ra,r2,0; addi rb,ra,x;
14854 to nop; addi rb,r2,x; */
14855 switch (r_type)
14856 {
14857 default:
14858 break;
14859
14860 case R_PPC64_GOT_TLSLD16_HI:
14861 case R_PPC64_GOT_TLSGD16_HI:
14862 case R_PPC64_GOT_TPREL16_HI:
14863 case R_PPC64_GOT_DTPREL16_HI:
14864 case R_PPC64_GOT16_HI:
14865 case R_PPC64_TOC16_HI:
14866 /* These relocs would only be useful if building up an
14867 offset to later add to r2, perhaps in an indexed
14868 addressing mode instruction. Don't try to optimize.
14869 Unfortunately, the possibility of someone building up an
14870 offset like this or even with the HA relocs, means that
14871 we need to check the high insn when optimizing the low
14872 insn. */
14873 break;
14874
14875 case R_PPC64_PLTCALL:
14876 if (unresolved_reloc)
14877 {
14878 /* No plt entry. Make this into a direct call. */
14879 bfd_byte *p = contents + rel->r_offset;
14880 insn = bfd_get_32 (input_bfd, p);
14881 insn &= 1;
14882 bfd_put_32 (input_bfd, B_DOT | insn, p);
14883 bfd_put_32 (input_bfd, NOP, p + 4);
14884 unresolved_reloc = save_unresolved_reloc;
14885 r_type = R_PPC64_REL24;
14886 }
14887 break;
14888
14889 case R_PPC64_PLTSEQ:
14890 if (unresolved_reloc)
14891 {
14892 unresolved_reloc = FALSE;
14893 goto nop_it;
14894 }
14895 break;
14896
14897 case R_PPC64_PLT16_HA:
14898 if (unresolved_reloc)
14899 {
14900 unresolved_reloc = FALSE;
14901 goto nop_it;
14902 }
14903 /* Fall through. */
14904 case R_PPC64_GOT_TLSLD16_HA:
14905 case R_PPC64_GOT_TLSGD16_HA:
14906 case R_PPC64_GOT_TPREL16_HA:
14907 case R_PPC64_GOT_DTPREL16_HA:
14908 case R_PPC64_GOT16_HA:
14909 case R_PPC64_TOC16_HA:
14910 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14911 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14912 {
14913 bfd_byte *p;
14914 nop_it:
14915 p = contents + (rel->r_offset & ~3);
14916 bfd_put_32 (input_bfd, NOP, p);
14917 goto copy_reloc;
14918 }
14919 break;
14920
14921 case R_PPC64_PLT16_LO:
14922 case R_PPC64_PLT16_LO_DS:
14923 if (unresolved_reloc)
14924 {
14925 unresolved_reloc = FALSE;
14926 goto nop_it;
14927 }
14928 /* Fall through. */
14929 case R_PPC64_GOT_TLSLD16_LO:
14930 case R_PPC64_GOT_TLSGD16_LO:
14931 case R_PPC64_GOT_TPREL16_LO_DS:
14932 case R_PPC64_GOT_DTPREL16_LO_DS:
14933 case R_PPC64_GOT16_LO:
14934 case R_PPC64_GOT16_LO_DS:
14935 case R_PPC64_TOC16_LO:
14936 case R_PPC64_TOC16_LO_DS:
14937 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14938 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14939 {
14940 bfd_byte *p = contents + (rel->r_offset & ~3);
14941 insn = bfd_get_32 (input_bfd, p);
14942 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14943 {
14944 /* Transform addic to addi when we change reg. */
14945 insn &= ~((0x3f << 26) | (0x1f << 16));
14946 insn |= (14u << 26) | (2 << 16);
14947 }
14948 else
14949 {
14950 insn &= ~(0x1f << 16);
14951 insn |= 2 << 16;
14952 }
14953 bfd_put_32 (input_bfd, insn, p);
14954 }
14955 break;
14956
14957 case R_PPC64_TPREL16_HA:
14958 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
14959 {
14960 bfd_byte *p = contents + (rel->r_offset & ~3);
14961 insn = bfd_get_32 (input_bfd, p);
14962 if ((insn & ((0x3f << 26) | 0x1f << 16))
14963 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
14964 /* xgettext:c-format */
14965 info->callbacks->minfo
14966 (_("%H: warning: %s unexpected insn %#x.\n"),
14967 input_bfd, input_section, rel->r_offset,
14968 ppc64_elf_howto_table[r_type]->name, insn);
14969 else
14970 {
14971 bfd_put_32 (input_bfd, NOP, p);
14972 goto copy_reloc;
14973 }
14974 }
14975 break;
14976
14977 case R_PPC64_TPREL16_LO:
14978 case R_PPC64_TPREL16_LO_DS:
14979 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
14980 {
14981 bfd_byte *p = contents + (rel->r_offset & ~3);
14982 insn = bfd_get_32 (input_bfd, p);
14983 insn &= ~(0x1f << 16);
14984 insn |= 13 << 16;
14985 bfd_put_32 (input_bfd, insn, p);
14986 }
14987 break;
14988 }
14989
14990 /* Do any further special processing. */
14991 switch (r_type)
14992 {
14993 default:
14994 break;
14995
14996 case R_PPC64_REL16_HA:
14997 case R_PPC64_REL16DX_HA:
14998 case R_PPC64_ADDR16_HA:
14999 case R_PPC64_ADDR16_HIGHA:
15000 case R_PPC64_ADDR16_HIGHERA:
15001 case R_PPC64_ADDR16_HIGHESTA:
15002 case R_PPC64_TOC16_HA:
15003 case R_PPC64_SECTOFF_HA:
15004 case R_PPC64_TPREL16_HA:
15005 case R_PPC64_TPREL16_HIGHA:
15006 case R_PPC64_TPREL16_HIGHERA:
15007 case R_PPC64_TPREL16_HIGHESTA:
15008 case R_PPC64_DTPREL16_HA:
15009 case R_PPC64_DTPREL16_HIGHA:
15010 case R_PPC64_DTPREL16_HIGHERA:
15011 case R_PPC64_DTPREL16_HIGHESTA:
15012 /* It's just possible that this symbol is a weak symbol
15013 that's not actually defined anywhere. In that case,
15014 'sec' would be NULL, and we should leave the symbol
15015 alone (it will be set to zero elsewhere in the link). */
15016 if (sec == NULL)
15017 break;
15018 /* Fall through. */
15019
15020 case R_PPC64_GOT16_HA:
15021 case R_PPC64_PLTGOT16_HA:
15022 case R_PPC64_PLT16_HA:
15023 case R_PPC64_GOT_TLSGD16_HA:
15024 case R_PPC64_GOT_TLSLD16_HA:
15025 case R_PPC64_GOT_TPREL16_HA:
15026 case R_PPC64_GOT_DTPREL16_HA:
15027 /* Add 0x10000 if sign bit in 0:15 is set.
15028 Bits 0:15 are not used. */
15029 addend += 0x8000;
15030 break;
15031
15032 case R_PPC64_ADDR16_DS:
15033 case R_PPC64_ADDR16_LO_DS:
15034 case R_PPC64_GOT16_DS:
15035 case R_PPC64_GOT16_LO_DS:
15036 case R_PPC64_PLT16_LO_DS:
15037 case R_PPC64_SECTOFF_DS:
15038 case R_PPC64_SECTOFF_LO_DS:
15039 case R_PPC64_TOC16_DS:
15040 case R_PPC64_TOC16_LO_DS:
15041 case R_PPC64_PLTGOT16_DS:
15042 case R_PPC64_PLTGOT16_LO_DS:
15043 case R_PPC64_GOT_TPREL16_DS:
15044 case R_PPC64_GOT_TPREL16_LO_DS:
15045 case R_PPC64_GOT_DTPREL16_DS:
15046 case R_PPC64_GOT_DTPREL16_LO_DS:
15047 case R_PPC64_TPREL16_DS:
15048 case R_PPC64_TPREL16_LO_DS:
15049 case R_PPC64_DTPREL16_DS:
15050 case R_PPC64_DTPREL16_LO_DS:
15051 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15052 mask = 3;
15053 /* If this reloc is against an lq, lxv, or stxv insn, then
15054 the value must be a multiple of 16. This is somewhat of
15055 a hack, but the "correct" way to do this by defining _DQ
15056 forms of all the _DS relocs bloats all reloc switches in
15057 this file. It doesn't make much sense to use these
15058 relocs in data, so testing the insn should be safe. */
15059 if ((insn & (0x3f << 26)) == (56u << 26)
15060 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15061 mask = 15;
15062 relocation += addend;
15063 addend = insn & (mask ^ 3);
15064 if ((relocation & mask) != 0)
15065 {
15066 relocation ^= relocation & mask;
15067 info->callbacks->einfo
15068 /* xgettext:c-format */
15069 (_("%H: error: %s not a multiple of %u\n"),
15070 input_bfd, input_section, rel->r_offset,
15071 ppc64_elf_howto_table[r_type]->name,
15072 mask + 1);
15073 bfd_set_error (bfd_error_bad_value);
15074 ret = FALSE;
15075 goto copy_reloc;
15076 }
15077 break;
15078 }
15079
15080 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15081 because such sections are not SEC_ALLOC and thus ld.so will
15082 not process them. */
15083 howto = ppc64_elf_howto_table[(int) r_type];
15084 if (unresolved_reloc
15085 && !((input_section->flags & SEC_DEBUGGING) != 0
15086 && h->elf.def_dynamic)
15087 && _bfd_elf_section_offset (output_bfd, info, input_section,
15088 rel->r_offset) != (bfd_vma) -1)
15089 {
15090 info->callbacks->einfo
15091 /* xgettext:c-format */
15092 (_("%H: unresolvable %s against `%pT'\n"),
15093 input_bfd, input_section, rel->r_offset,
15094 howto->name,
15095 h->elf.root.root.string);
15096 ret = FALSE;
15097 }
15098
15099 /* 16-bit fields in insns mostly have signed values, but a
15100 few insns have 16-bit unsigned values. Really, we should
15101 have different reloc types. */
15102 if (howto->complain_on_overflow != complain_overflow_dont
15103 && howto->dst_mask == 0xffff
15104 && (input_section->flags & SEC_CODE) != 0)
15105 {
15106 enum complain_overflow complain = complain_overflow_signed;
15107
15108 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15109 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15110 complain = complain_overflow_bitfield;
15111 else if (howto->rightshift == 0
15112 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15113 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15114 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15115 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15116 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15117 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15118 complain = complain_overflow_unsigned;
15119 if (howto->complain_on_overflow != complain)
15120 {
15121 alt_howto = *howto;
15122 alt_howto.complain_on_overflow = complain;
15123 howto = &alt_howto;
15124 }
15125 }
15126
15127 if (r_type == R_PPC64_REL16DX_HA)
15128 {
15129 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15130 if (rel->r_offset + 4 > input_section->size)
15131 r = bfd_reloc_outofrange;
15132 else
15133 {
15134 relocation += addend;
15135 relocation -= (rel->r_offset
15136 + input_section->output_offset
15137 + input_section->output_section->vma);
15138 relocation = (bfd_signed_vma) relocation >> 16;
15139 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15140 insn &= ~0x1fffc1;
15141 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15142 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15143 r = bfd_reloc_ok;
15144 if (relocation + 0x8000 > 0xffff)
15145 r = bfd_reloc_overflow;
15146 }
15147 }
15148 else
15149 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15150 rel->r_offset, relocation, addend);
15151
15152 if (r != bfd_reloc_ok)
15153 {
15154 char *more_info = NULL;
15155 const char *reloc_name = howto->name;
15156
15157 if (reloc_dest != DEST_NORMAL)
15158 {
15159 more_info = bfd_malloc (strlen (reloc_name) + 8);
15160 if (more_info != NULL)
15161 {
15162 strcpy (more_info, reloc_name);
15163 strcat (more_info, (reloc_dest == DEST_OPD
15164 ? " (OPD)" : " (stub)"));
15165 reloc_name = more_info;
15166 }
15167 }
15168
15169 if (r == bfd_reloc_overflow)
15170 {
15171 /* On code like "if (foo) foo();" don't report overflow
15172 on a branch to zero when foo is undefined. */
15173 if (!warned
15174 && (reloc_dest == DEST_STUB
15175 || !(h != NULL
15176 && (h->elf.root.type == bfd_link_hash_undefweak
15177 || h->elf.root.type == bfd_link_hash_undefined)
15178 && is_branch_reloc (r_type))))
15179 info->callbacks->reloc_overflow (info, &h->elf.root,
15180 sym_name, reloc_name,
15181 orig_rel.r_addend,
15182 input_bfd, input_section,
15183 rel->r_offset);
15184 }
15185 else
15186 {
15187 info->callbacks->einfo
15188 /* xgettext:c-format */
15189 (_("%H: %s against `%pT': error %d\n"),
15190 input_bfd, input_section, rel->r_offset,
15191 reloc_name, sym_name, (int) r);
15192 ret = FALSE;
15193 }
15194 if (more_info != NULL)
15195 free (more_info);
15196 }
15197 copy_reloc:
15198 if (wrel != rel)
15199 *wrel = *rel;
15200 }
15201
15202 if (wrel != rel)
15203 {
15204 Elf_Internal_Shdr *rel_hdr;
15205 size_t deleted = rel - wrel;
15206
15207 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15208 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15209 if (rel_hdr->sh_size == 0)
15210 {
15211 /* It is too late to remove an empty reloc section. Leave
15212 one NONE reloc.
15213 ??? What is wrong with an empty section??? */
15214 rel_hdr->sh_size = rel_hdr->sh_entsize;
15215 deleted -= 1;
15216 }
15217 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15218 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15219 input_section->reloc_count -= deleted;
15220 }
15221
15222 /* If we're emitting relocations, then shortly after this function
15223 returns, reloc offsets and addends for this section will be
15224 adjusted. Worse, reloc symbol indices will be for the output
15225 file rather than the input. Save a copy of the relocs for
15226 opd_entry_value. */
15227 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15228 {
15229 bfd_size_type amt;
15230 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15231 rel = bfd_alloc (input_bfd, amt);
15232 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15233 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15234 if (rel == NULL)
15235 return FALSE;
15236 memcpy (rel, relocs, amt);
15237 }
15238 return ret;
15239 }
15240
15241 /* Adjust the value of any local symbols in opd sections. */
15242
15243 static int
15244 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15245 const char *name ATTRIBUTE_UNUSED,
15246 Elf_Internal_Sym *elfsym,
15247 asection *input_sec,
15248 struct elf_link_hash_entry *h)
15249 {
15250 struct _opd_sec_data *opd;
15251 long adjust;
15252 bfd_vma value;
15253
15254 if (h != NULL)
15255 return 1;
15256
15257 opd = get_opd_info (input_sec);
15258 if (opd == NULL || opd->adjust == NULL)
15259 return 1;
15260
15261 value = elfsym->st_value - input_sec->output_offset;
15262 if (!bfd_link_relocatable (info))
15263 value -= input_sec->output_section->vma;
15264
15265 adjust = opd->adjust[OPD_NDX (value)];
15266 if (adjust == -1)
15267 return 2;
15268
15269 elfsym->st_value += adjust;
15270 return 1;
15271 }
15272
15273 /* Finish up dynamic symbol handling. We set the contents of various
15274 dynamic sections here. */
15275
15276 static bfd_boolean
15277 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15278 struct bfd_link_info *info,
15279 struct elf_link_hash_entry *h,
15280 Elf_Internal_Sym *sym)
15281 {
15282 struct ppc_link_hash_table *htab;
15283 struct plt_entry *ent;
15284
15285 htab = ppc_hash_table (info);
15286 if (htab == NULL)
15287 return FALSE;
15288
15289 if (!htab->opd_abi && !h->def_regular)
15290 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15291 if (ent->plt.offset != (bfd_vma) -1)
15292 {
15293 /* Mark the symbol as undefined, rather than as
15294 defined in glink. Leave the value if there were
15295 any relocations where pointer equality matters
15296 (this is a clue for the dynamic linker, to make
15297 function pointer comparisons work between an
15298 application and shared library), otherwise set it
15299 to zero. */
15300 sym->st_shndx = SHN_UNDEF;
15301 if (!h->pointer_equality_needed)
15302 sym->st_value = 0;
15303 else if (!h->ref_regular_nonweak)
15304 {
15305 /* This breaks function pointer comparisons, but
15306 that is better than breaking tests for a NULL
15307 function pointer. */
15308 sym->st_value = 0;
15309 }
15310 break;
15311 }
15312
15313 if (h->needs_copy)
15314 {
15315 /* This symbol needs a copy reloc. Set it up. */
15316 Elf_Internal_Rela rela;
15317 asection *srel;
15318 bfd_byte *loc;
15319
15320 if (h->dynindx == -1
15321 || (h->root.type != bfd_link_hash_defined
15322 && h->root.type != bfd_link_hash_defweak)
15323 || htab->elf.srelbss == NULL
15324 || htab->elf.sreldynrelro == NULL)
15325 abort ();
15326
15327 rela.r_offset = (h->root.u.def.value
15328 + h->root.u.def.section->output_section->vma
15329 + h->root.u.def.section->output_offset);
15330 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15331 rela.r_addend = 0;
15332 if (h->root.u.def.section == htab->elf.sdynrelro)
15333 srel = htab->elf.sreldynrelro;
15334 else
15335 srel = htab->elf.srelbss;
15336 loc = srel->contents;
15337 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
15338 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15339 }
15340
15341 return TRUE;
15342 }
15343
15344 /* Used to decide how to sort relocs in an optimal manner for the
15345 dynamic linker, before writing them out. */
15346
15347 static enum elf_reloc_type_class
15348 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15349 const asection *rel_sec,
15350 const Elf_Internal_Rela *rela)
15351 {
15352 enum elf_ppc64_reloc_type r_type;
15353 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15354
15355 if (rel_sec == htab->elf.irelplt)
15356 return reloc_class_ifunc;
15357
15358 r_type = ELF64_R_TYPE (rela->r_info);
15359 switch (r_type)
15360 {
15361 case R_PPC64_RELATIVE:
15362 return reloc_class_relative;
15363 case R_PPC64_JMP_SLOT:
15364 return reloc_class_plt;
15365 case R_PPC64_COPY:
15366 return reloc_class_copy;
15367 default:
15368 return reloc_class_normal;
15369 }
15370 }
15371
15372 /* Finish up the dynamic sections. */
15373
15374 static bfd_boolean
15375 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15376 struct bfd_link_info *info)
15377 {
15378 struct ppc_link_hash_table *htab;
15379 bfd *dynobj;
15380 asection *sdyn;
15381
15382 htab = ppc_hash_table (info);
15383 if (htab == NULL)
15384 return FALSE;
15385
15386 dynobj = htab->elf.dynobj;
15387 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15388
15389 if (htab->elf.dynamic_sections_created)
15390 {
15391 Elf64_External_Dyn *dyncon, *dynconend;
15392
15393 if (sdyn == NULL || htab->elf.sgot == NULL)
15394 abort ();
15395
15396 dyncon = (Elf64_External_Dyn *) sdyn->contents;
15397 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15398 for (; dyncon < dynconend; dyncon++)
15399 {
15400 Elf_Internal_Dyn dyn;
15401 asection *s;
15402
15403 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15404
15405 switch (dyn.d_tag)
15406 {
15407 default:
15408 continue;
15409
15410 case DT_PPC64_GLINK:
15411 s = htab->glink;
15412 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15413 /* We stupidly defined DT_PPC64_GLINK to be the start
15414 of glink rather than the first entry point, which is
15415 what ld.so needs, and now have a bigger stub to
15416 support automatic multiple TOCs. */
15417 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
15418 break;
15419
15420 case DT_PPC64_OPD:
15421 s = bfd_get_section_by_name (output_bfd, ".opd");
15422 if (s == NULL)
15423 continue;
15424 dyn.d_un.d_ptr = s->vma;
15425 break;
15426
15427 case DT_PPC64_OPT:
15428 if (htab->do_multi_toc && htab->multi_toc_needed)
15429 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15430 if (htab->has_plt_localentry0)
15431 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
15432 break;
15433
15434 case DT_PPC64_OPDSZ:
15435 s = bfd_get_section_by_name (output_bfd, ".opd");
15436 if (s == NULL)
15437 continue;
15438 dyn.d_un.d_val = s->size;
15439 break;
15440
15441 case DT_PLTGOT:
15442 s = htab->elf.splt;
15443 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15444 break;
15445
15446 case DT_JMPREL:
15447 s = htab->elf.srelplt;
15448 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15449 break;
15450
15451 case DT_PLTRELSZ:
15452 dyn.d_un.d_val = htab->elf.srelplt->size;
15453 break;
15454
15455 case DT_TEXTREL:
15456 if (htab->local_ifunc_resolver)
15457 info->callbacks->einfo
15458 (_("%X%P: text relocations and GNU indirect "
15459 "functions will result in a segfault at runtime\n"));
15460 else if (htab->maybe_local_ifunc_resolver)
15461 info->callbacks->einfo
15462 (_("%P: warning: text relocations and GNU indirect "
15463 "functions may result in a segfault at runtime\n"));
15464 continue;
15465 }
15466
15467 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15468 }
15469 }
15470
15471 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15472 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
15473 {
15474 /* Fill in the first entry in the global offset table.
15475 We use it to hold the link-time TOCbase. */
15476 bfd_put_64 (output_bfd,
15477 elf_gp (output_bfd) + TOC_BASE_OFF,
15478 htab->elf.sgot->contents);
15479
15480 /* Set .got entry size. */
15481 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
15482 = 8;
15483 }
15484
15485 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15486 && htab->elf.splt->output_section != bfd_abs_section_ptr)
15487 {
15488 /* Set .plt entry size. */
15489 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15490 = PLT_ENTRY_SIZE (htab);
15491 }
15492
15493 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15494 brlt ourselves if emitrelocations. */
15495 if (htab->brlt != NULL
15496 && htab->brlt->reloc_count != 0
15497 && !_bfd_elf_link_output_relocs (output_bfd,
15498 htab->brlt,
15499 elf_section_data (htab->brlt)->rela.hdr,
15500 elf_section_data (htab->brlt)->relocs,
15501 NULL))
15502 return FALSE;
15503
15504 if (htab->glink != NULL
15505 && htab->glink->reloc_count != 0
15506 && !_bfd_elf_link_output_relocs (output_bfd,
15507 htab->glink,
15508 elf_section_data (htab->glink)->rela.hdr,
15509 elf_section_data (htab->glink)->relocs,
15510 NULL))
15511 return FALSE;
15512
15513
15514 if (htab->glink_eh_frame != NULL
15515 && htab->glink_eh_frame->size != 0
15516 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15517 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15518 htab->glink_eh_frame,
15519 htab->glink_eh_frame->contents))
15520 return FALSE;
15521
15522 /* We need to handle writing out multiple GOT sections ourselves,
15523 since we didn't add them to DYNOBJ. We know dynobj is the first
15524 bfd. */
15525 while ((dynobj = dynobj->link.next) != NULL)
15526 {
15527 asection *s;
15528
15529 if (!is_ppc64_elf (dynobj))
15530 continue;
15531
15532 s = ppc64_elf_tdata (dynobj)->got;
15533 if (s != NULL
15534 && s->size != 0
15535 && s->output_section != bfd_abs_section_ptr
15536 && !bfd_set_section_contents (output_bfd, s->output_section,
15537 s->contents, s->output_offset,
15538 s->size))
15539 return FALSE;
15540 s = ppc64_elf_tdata (dynobj)->relgot;
15541 if (s != NULL
15542 && s->size != 0
15543 && s->output_section != bfd_abs_section_ptr
15544 && !bfd_set_section_contents (output_bfd, s->output_section,
15545 s->contents, s->output_offset,
15546 s->size))
15547 return FALSE;
15548 }
15549
15550 return TRUE;
15551 }
15552
15553 #include "elf64-target.h"
15554
15555 /* FreeBSD support */
15556
15557 #undef TARGET_LITTLE_SYM
15558 #undef TARGET_LITTLE_NAME
15559
15560 #undef TARGET_BIG_SYM
15561 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
15562 #undef TARGET_BIG_NAME
15563 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15564
15565 #undef ELF_OSABI
15566 #define ELF_OSABI ELFOSABI_FREEBSD
15567
15568 #undef elf64_bed
15569 #define elf64_bed elf64_powerpc_fbsd_bed
15570
15571 #include "elf64-target.h"
This page took 0.383162 seconds and 5 git commands to generate.