a795ad1578d237be13584c3ca0530c6b0524e2b7
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5 Largely rewritten by Alan Modra.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22
23
24 /* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
27
28 #include "sysdep.h"
29 #include <stdarg.h>
30 #include "bfd.h"
31 #include "bfdlink.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elf/ppc64.h"
35 #include "elf64-ppc.h"
36 #include "dwarf2.h"
37
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_vma opd_entry_value
57 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
58
59 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
60 #define TARGET_LITTLE_NAME "elf64-powerpcle"
61 #define TARGET_BIG_SYM powerpc_elf64_vec
62 #define TARGET_BIG_NAME "elf64-powerpc"
63 #define ELF_ARCH bfd_arch_powerpc
64 #define ELF_TARGET_ID PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE EM_PPC64
66 #define ELF_MAXPAGESIZE 0x10000
67 #define ELF_COMMONPAGESIZE 0x1000
68 #define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
69 #define elf_info_to_howto ppc64_elf_info_to_howto
70
71 #define elf_backend_want_got_sym 0
72 #define elf_backend_want_plt_sym 0
73 #define elf_backend_plt_alignment 3
74 #define elf_backend_plt_not_loaded 1
75 #define elf_backend_got_header_size 8
76 #define elf_backend_want_dynrelro 1
77 #define elf_backend_can_gc_sections 1
78 #define elf_backend_can_refcount 1
79 #define elf_backend_rela_normal 1
80 #define elf_backend_dtrel_excludes_plt 1
81 #define elf_backend_default_execstack 0
82
83 #define bfd_elf64_mkobject ppc64_elf_mkobject
84 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
85 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
86 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
87 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
88 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
89 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
91 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
92 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
93
94 #define elf_backend_object_p ppc64_elf_object_p
95 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
96 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
97 #define elf_backend_write_core_note ppc64_elf_write_core_note
98 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
99 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
100 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
101 #define elf_backend_check_directives ppc64_elf_before_check_relocs
102 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
103 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
104 #define elf_backend_check_relocs ppc64_elf_check_relocs
105 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
106 #define elf_backend_gc_keep ppc64_elf_gc_keep
107 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
108 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
109 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
110 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
111 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
112 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
113 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
114 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
115 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
116 #define elf_backend_action_discarded ppc64_elf_action_discarded
117 #define elf_backend_relocate_section ppc64_elf_relocate_section
118 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
119 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
120 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
121 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
122 #define elf_backend_special_sections ppc64_elf_special_sections
123 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
124 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
125 #define elf_backend_get_reloc_section bfd_get_section_by_name
126
127 /* The name of the dynamic interpreter. This is put in the .interp
128 section. */
129 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
130
131 /* The size in bytes of an entry in the procedure linkage table. */
132 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
133 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
134
135 /* The initial size of the plt reserved for the dynamic linker. */
136 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
137
138 /* Offsets to some stack save slots. */
139 #define STK_LR 16
140 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
141 /* This one is dodgy. ELFv2 does not have a linker word, so use the
142 CR save slot. Used only by optimised __tls_get_addr call stub,
143 relying on __tls_get_addr_opt not saving CR.. */
144 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
145
146 /* TOC base pointers offset from start of TOC. */
147 #define TOC_BASE_OFF 0x8000
148 /* TOC base alignment. */
149 #define TOC_BASE_ALIGN 256
150
151 /* Offset of tp and dtp pointers from start of TLS block. */
152 #define TP_OFFSET 0x7000
153 #define DTP_OFFSET 0x8000
154
155 /* .plt call stub instructions. The normal stub is like this, but
156 sometimes the .plt entry crosses a 64k boundary and we need to
157 insert an addi to adjust r11. */
158 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
159 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
160 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
161 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
162 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
163 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
164 #define BCTR 0x4e800420 /* bctr */
165
166 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
167 #define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
168 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
169 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
170 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
171
172 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
173 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
174 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
175 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
176 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
177 #define BNECTR 0x4ca20420 /* bnectr+ */
178 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
179
180 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
181 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
182 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
183
184 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
185 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
186 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
187
188 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
189 #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
190 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
191 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
192 #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
193 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
194 #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
195 #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
196 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
197 #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
198 #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
199 #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
200
201 /* __glink_PLTresolve stub instructions. We enter with the index in R0. */
202 #define GLINK_PLTRESOLVE_SIZE(htab) \
203 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
204 /* 0: */
205 /* .quad plt0-1f */
206 /* __glink: */
207 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
208 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
209 /* 1: */
210 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
211 /* ld %2,(0b-1b)(%11) */
212 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
213 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
214 /* ld %12,0(%11) */
215 /* ld %2,8(%11) */
216 /* mtctr %12 */
217 /* ld %11,16(%11) */
218 /* bctr */
219 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
220 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
221 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
222 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
223 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
224
225 /* Pad with this. */
226 #define NOP 0x60000000
227
228 /* Some other nops. */
229 #define CROR_151515 0x4def7b82
230 #define CROR_313131 0x4ffffb82
231
232 /* .glink entries for the first 32k functions are two instructions. */
233 #define LI_R0_0 0x38000000 /* li %r0,0 */
234 #define B_DOT 0x48000000 /* b . */
235
236 /* After that, we need two instructions to load the index, followed by
237 a branch. */
238 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
239 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
240
241 /* Instructions used by the save and restore reg functions. */
242 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
243 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
244 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
245 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
246 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
247 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
248 #define LI_R12_0 0x39800000 /* li %r12,0 */
249 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
250 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
251 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
252 #define BLR 0x4e800020 /* blr */
253
254 /* Since .opd is an array of descriptors and each entry will end up
255 with identical R_PPC64_RELATIVE relocs, there is really no need to
256 propagate .opd relocs; The dynamic linker should be taught to
257 relocate .opd without reloc entries. */
258 #ifndef NO_OPD_RELOCS
259 #define NO_OPD_RELOCS 0
260 #endif
261
262 #ifndef ARRAY_SIZE
263 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
264 #endif
265
266 static inline int
267 abiversion (bfd *abfd)
268 {
269 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
270 }
271
272 static inline void
273 set_abiversion (bfd *abfd, int ver)
274 {
275 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
276 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
277 }
278 \f
279 /* Relocation HOWTO's. */
280 /* Like other ELF RELA targets that don't apply multiple
281 field-altering relocations to the same localation, src_mask is
282 always zero and pcrel_offset is the same as pc_relative.
283 PowerPC can always use a zero bitpos, even when the field is not at
284 the LSB. For example, a REL24 could use rightshift=2, bisize=24
285 and bitpos=2 which matches the ABI description, or as we do here,
286 rightshift=0, bitsize=26 and bitpos=0. */
287 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
288 complain, special_func) \
289 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
290 complain_overflow_ ## complain, special_func, \
291 #type, FALSE, 0, mask, pc_relative)
292
293 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
294
295 static reloc_howto_type ppc64_elf_howto_raw[] =
296 {
297 /* This reloc does nothing. */
298 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
299 bfd_elf_generic_reloc),
300
301 /* A standard 32 bit relocation. */
302 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
303 bfd_elf_generic_reloc),
304
305 /* An absolute 26 bit branch; the lower two bits must be zero.
306 FIXME: we don't check that, we just clear them. */
307 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
308 bfd_elf_generic_reloc),
309
310 /* A standard 16 bit relocation. */
311 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
312 bfd_elf_generic_reloc),
313
314 /* A 16 bit relocation without overflow. */
315 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
316 bfd_elf_generic_reloc),
317
318 /* Bits 16-31 of an address. */
319 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
320 bfd_elf_generic_reloc),
321
322 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
323 bits, treated as a signed number, is negative. */
324 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
325 ppc64_elf_ha_reloc),
326
327 /* An absolute 16 bit branch; the lower two bits must be zero.
328 FIXME: we don't check that, we just clear them. */
329 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
330 ppc64_elf_branch_reloc),
331
332 /* An absolute 16 bit branch, for which bit 10 should be set to
333 indicate that the branch is expected to be taken. The lower two
334 bits must be zero. */
335 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
336 ppc64_elf_brtaken_reloc),
337
338 /* An absolute 16 bit branch, for which bit 10 should be set to
339 indicate that the branch is not expected to be taken. The lower
340 two bits must be zero. */
341 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
342 ppc64_elf_brtaken_reloc),
343
344 /* A relative 26 bit branch; the lower two bits must be zero. */
345 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
346 ppc64_elf_branch_reloc),
347
348 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
349 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
350 ppc64_elf_branch_reloc),
351
352 /* A relative 16 bit branch; the lower two bits must be zero. */
353 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
354 ppc64_elf_branch_reloc),
355
356 /* A relative 16 bit branch. Bit 10 should be set to indicate that
357 the branch is expected to be taken. The lower two bits must be
358 zero. */
359 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
360 ppc64_elf_brtaken_reloc),
361
362 /* A relative 16 bit branch. Bit 10 should be set to indicate that
363 the branch is not expected to be taken. The lower two bits must
364 be zero. */
365 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
366 ppc64_elf_brtaken_reloc),
367
368 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
369 symbol. */
370 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
371 ppc64_elf_unhandled_reloc),
372
373 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
374 the symbol. */
375 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
376 ppc64_elf_unhandled_reloc),
377
378 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
379 the symbol. */
380 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
381 ppc64_elf_unhandled_reloc),
382
383 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
384 the symbol. */
385 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
386 ppc64_elf_unhandled_reloc),
387
388 /* This is used only by the dynamic linker. The symbol should exist
389 both in the object being run and in some shared library. The
390 dynamic linker copies the data addressed by the symbol from the
391 shared library into the object, because the object being
392 run has to have the data at some particular address. */
393 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
394 ppc64_elf_unhandled_reloc),
395
396 /* Like R_PPC64_ADDR64, but used when setting global offset table
397 entries. */
398 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
399 ppc64_elf_unhandled_reloc),
400
401 /* Created by the link editor. Marks a procedure linkage table
402 entry for a symbol. */
403 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
404 ppc64_elf_unhandled_reloc),
405
406 /* Used only by the dynamic linker. When the object is run, this
407 doubleword64 is set to the load address of the object, plus the
408 addend. */
409 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
410 bfd_elf_generic_reloc),
411
412 /* Like R_PPC64_ADDR32, but may be unaligned. */
413 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
414 bfd_elf_generic_reloc),
415
416 /* Like R_PPC64_ADDR16, but may be unaligned. */
417 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
418 bfd_elf_generic_reloc),
419
420 /* 32-bit PC relative. */
421 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
422 bfd_elf_generic_reloc),
423
424 /* 32-bit relocation to the symbol's procedure linkage table. */
425 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
426 ppc64_elf_unhandled_reloc),
427
428 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
429 FIXME: R_PPC64_PLTREL32 not supported. */
430 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
431 ppc64_elf_unhandled_reloc),
432
433 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
434 the symbol. */
435 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
436 ppc64_elf_unhandled_reloc),
437
438 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
439 the symbol. */
440 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
441 ppc64_elf_unhandled_reloc),
442
443 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
444 the symbol. */
445 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
446 ppc64_elf_unhandled_reloc),
447
448 /* 16-bit section relative relocation. */
449 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
450 ppc64_elf_sectoff_reloc),
451
452 /* Like R_PPC64_SECTOFF, but no overflow warning. */
453 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
454 ppc64_elf_sectoff_reloc),
455
456 /* 16-bit upper half section relative relocation. */
457 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
458 ppc64_elf_sectoff_reloc),
459
460 /* 16-bit upper half adjusted section relative relocation. */
461 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
462 ppc64_elf_sectoff_ha_reloc),
463
464 /* Like R_PPC64_REL24 without touching the two least significant bits. */
465 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
466 bfd_elf_generic_reloc),
467
468 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
469
470 /* A standard 64-bit relocation. */
471 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
472 bfd_elf_generic_reloc),
473
474 /* The bits 32-47 of an address. */
475 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
476 bfd_elf_generic_reloc),
477
478 /* The bits 32-47 of an address, plus 1 if the contents of the low
479 16 bits, treated as a signed number, is negative. */
480 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
481 ppc64_elf_ha_reloc),
482
483 /* The bits 48-63 of an address. */
484 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
485 bfd_elf_generic_reloc),
486
487 /* The bits 48-63 of an address, plus 1 if the contents of the low
488 16 bits, treated as a signed number, is negative. */
489 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
490 ppc64_elf_ha_reloc),
491
492 /* Like ADDR64, but may be unaligned. */
493 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
494 bfd_elf_generic_reloc),
495
496 /* 64-bit relative relocation. */
497 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
498 bfd_elf_generic_reloc),
499
500 /* 64-bit relocation to the symbol's procedure linkage table. */
501 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
502 ppc64_elf_unhandled_reloc),
503
504 /* 64-bit PC relative relocation to the symbol's procedure linkage
505 table. */
506 /* FIXME: R_PPC64_PLTREL64 not supported. */
507 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
508 ppc64_elf_unhandled_reloc),
509
510 /* 16 bit TOC-relative relocation. */
511 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
512 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
513 ppc64_elf_toc_reloc),
514
515 /* 16 bit TOC-relative relocation without overflow. */
516 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
517 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
518 ppc64_elf_toc_reloc),
519
520 /* 16 bit TOC-relative relocation, high 16 bits. */
521 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
522 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
523 ppc64_elf_toc_reloc),
524
525 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
526 contents of the low 16 bits, treated as a signed number, is
527 negative. */
528 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
529 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
530 ppc64_elf_toc_ha_reloc),
531
532 /* 64-bit relocation; insert value of TOC base (.TOC.). */
533 /* R_PPC64_TOC 51 doubleword64 .TOC. */
534 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
535 ppc64_elf_toc64_reloc),
536
537 /* Like R_PPC64_GOT16, but also informs the link editor that the
538 value to relocate may (!) refer to a PLT entry which the link
539 editor (a) may replace with the symbol value. If the link editor
540 is unable to fully resolve the symbol, it may (b) create a PLT
541 entry and store the address to the new PLT entry in the GOT.
542 This permits lazy resolution of function symbols at run time.
543 The link editor may also skip all of this and just (c) emit a
544 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
545 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
546 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
547 ppc64_elf_unhandled_reloc),
548
549 /* Like R_PPC64_PLTGOT16, but without overflow. */
550 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
551 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
552 ppc64_elf_unhandled_reloc),
553
554 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
555 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
556 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
557 ppc64_elf_unhandled_reloc),
558
559 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
560 1 if the contents of the low 16 bits, treated as a signed number,
561 is negative. */
562 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
563 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
564 ppc64_elf_unhandled_reloc),
565
566 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
567 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
568 bfd_elf_generic_reloc),
569
570 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
571 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
572 bfd_elf_generic_reloc),
573
574 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
575 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
576 ppc64_elf_unhandled_reloc),
577
578 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
579 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
580 ppc64_elf_unhandled_reloc),
581
582 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
583 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
584 ppc64_elf_unhandled_reloc),
585
586 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
587 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
588 ppc64_elf_sectoff_reloc),
589
590 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
591 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
592 ppc64_elf_sectoff_reloc),
593
594 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
595 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
596 ppc64_elf_toc_reloc),
597
598 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
599 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
600 ppc64_elf_toc_reloc),
601
602 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
603 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
604 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
605 ppc64_elf_unhandled_reloc),
606
607 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
608 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
609 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
610 ppc64_elf_unhandled_reloc),
611
612 /* Marker relocs for TLS. */
613 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
614 bfd_elf_generic_reloc),
615
616 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
617 bfd_elf_generic_reloc),
618
619 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
620 bfd_elf_generic_reloc),
621
622 /* Marker reloc for optimizing r2 save in prologue rather than on
623 each plt call stub. */
624 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
625 bfd_elf_generic_reloc),
626
627 /* Marker relocs on inline plt call instructions. */
628 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
629 bfd_elf_generic_reloc),
630
631 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
632 bfd_elf_generic_reloc),
633
634 /* Computes the load module index of the load module that contains the
635 definition of its TLS sym. */
636 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
637 ppc64_elf_unhandled_reloc),
638
639 /* Computes a dtv-relative displacement, the difference between the value
640 of sym+add and the base address of the thread-local storage block that
641 contains the definition of sym, minus 0x8000. */
642 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
643 ppc64_elf_unhandled_reloc),
644
645 /* A 16 bit dtprel reloc. */
646 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
647 ppc64_elf_unhandled_reloc),
648
649 /* Like DTPREL16, but no overflow. */
650 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
651 ppc64_elf_unhandled_reloc),
652
653 /* Like DTPREL16_LO, but next higher group of 16 bits. */
654 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
655 ppc64_elf_unhandled_reloc),
656
657 /* Like DTPREL16_HI, but adjust for low 16 bits. */
658 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
659 ppc64_elf_unhandled_reloc),
660
661 /* Like DTPREL16_HI, but next higher group of 16 bits. */
662 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
663 ppc64_elf_unhandled_reloc),
664
665 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
666 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
667 ppc64_elf_unhandled_reloc),
668
669 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
670 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
671 ppc64_elf_unhandled_reloc),
672
673 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
674 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
675 ppc64_elf_unhandled_reloc),
676
677 /* Like DTPREL16, but for insns with a DS field. */
678 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
679 ppc64_elf_unhandled_reloc),
680
681 /* Like DTPREL16_DS, but no overflow. */
682 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
683 ppc64_elf_unhandled_reloc),
684
685 /* Computes a tp-relative displacement, the difference between the value of
686 sym+add and the value of the thread pointer (r13). */
687 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
688 ppc64_elf_unhandled_reloc),
689
690 /* A 16 bit tprel reloc. */
691 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
692 ppc64_elf_unhandled_reloc),
693
694 /* Like TPREL16, but no overflow. */
695 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
696 ppc64_elf_unhandled_reloc),
697
698 /* Like TPREL16_LO, but next higher group of 16 bits. */
699 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
700 ppc64_elf_unhandled_reloc),
701
702 /* Like TPREL16_HI, but adjust for low 16 bits. */
703 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
704 ppc64_elf_unhandled_reloc),
705
706 /* Like TPREL16_HI, but next higher group of 16 bits. */
707 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
708 ppc64_elf_unhandled_reloc),
709
710 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
711 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
712 ppc64_elf_unhandled_reloc),
713
714 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
715 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
716 ppc64_elf_unhandled_reloc),
717
718 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
719 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
720 ppc64_elf_unhandled_reloc),
721
722 /* Like TPREL16, but for insns with a DS field. */
723 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
724 ppc64_elf_unhandled_reloc),
725
726 /* Like TPREL16_DS, but no overflow. */
727 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
728 ppc64_elf_unhandled_reloc),
729
730 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
731 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
732 to the first entry relative to the TOC base (r2). */
733 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
734 ppc64_elf_unhandled_reloc),
735
736 /* Like GOT_TLSGD16, but no overflow. */
737 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
738 ppc64_elf_unhandled_reloc),
739
740 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
741 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
742 ppc64_elf_unhandled_reloc),
743
744 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
745 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
746 ppc64_elf_unhandled_reloc),
747
748 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
749 with values (sym+add)@dtpmod and zero, and computes the offset to the
750 first entry relative to the TOC base (r2). */
751 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
752 ppc64_elf_unhandled_reloc),
753
754 /* Like GOT_TLSLD16, but no overflow. */
755 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
756 ppc64_elf_unhandled_reloc),
757
758 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
759 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
760 ppc64_elf_unhandled_reloc),
761
762 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
763 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
764 ppc64_elf_unhandled_reloc),
765
766 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
767 the offset to the entry relative to the TOC base (r2). */
768 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
769 ppc64_elf_unhandled_reloc),
770
771 /* Like GOT_DTPREL16_DS, but no overflow. */
772 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
773 ppc64_elf_unhandled_reloc),
774
775 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
776 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
777 ppc64_elf_unhandled_reloc),
778
779 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
780 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
781 ppc64_elf_unhandled_reloc),
782
783 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
784 offset to the entry relative to the TOC base (r2). */
785 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
786 ppc64_elf_unhandled_reloc),
787
788 /* Like GOT_TPREL16_DS, but no overflow. */
789 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
790 ppc64_elf_unhandled_reloc),
791
792 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
793 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
794 ppc64_elf_unhandled_reloc),
795
796 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
797 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
798 ppc64_elf_unhandled_reloc),
799
800 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
801 ppc64_elf_unhandled_reloc),
802
803 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
804 bfd_elf_generic_reloc),
805
806 /* A 16 bit relative relocation. */
807 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
808 bfd_elf_generic_reloc),
809
810 /* A 16 bit relative relocation without overflow. */
811 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
812 bfd_elf_generic_reloc),
813
814 /* The high order 16 bits of a relative address. */
815 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
816 bfd_elf_generic_reloc),
817
818 /* The high order 16 bits of a relative address, plus 1 if the contents of
819 the low 16 bits, treated as a signed number, is negative. */
820 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
821 ppc64_elf_ha_reloc),
822
823 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
824 bfd_elf_generic_reloc),
825
826 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
827 ppc64_elf_ha_reloc),
828
829 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
830 bfd_elf_generic_reloc),
831
832 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
833 ppc64_elf_ha_reloc),
834
835 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
836 bfd_elf_generic_reloc),
837
838 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
839 ppc64_elf_ha_reloc),
840
841 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
842 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
843 ppc64_elf_ha_reloc),
844
845 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
846 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
847 ppc64_elf_ha_reloc),
848
849 /* Like R_PPC64_ADDR16_HI, but no overflow. */
850 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
851 bfd_elf_generic_reloc),
852
853 /* Like R_PPC64_ADDR16_HA, but no overflow. */
854 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
855 ppc64_elf_ha_reloc),
856
857 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
858 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
859 ppc64_elf_unhandled_reloc),
860
861 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
862 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
863 ppc64_elf_unhandled_reloc),
864
865 /* Like R_PPC64_TPREL16_HI, but no overflow. */
866 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
867 ppc64_elf_unhandled_reloc),
868
869 /* Like R_PPC64_TPREL16_HA, but no overflow. */
870 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
871 ppc64_elf_unhandled_reloc),
872
873 /* Marker reloc on ELFv2 large-model function entry. */
874 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
875 bfd_elf_generic_reloc),
876
877 /* Like ADDR64, but use local entry point of function. */
878 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
879 bfd_elf_generic_reloc),
880
881 /* GNU extension to record C++ vtable hierarchy. */
882 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
883 NULL),
884
885 /* GNU extension to record C++ vtable member usage. */
886 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
887 NULL),
888 };
889
890 \f
891 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
892 be done. */
893
894 static void
895 ppc_howto_init (void)
896 {
897 unsigned int i, type;
898
899 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
900 {
901 type = ppc64_elf_howto_raw[i].type;
902 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
903 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
904 }
905 }
906
907 static reloc_howto_type *
908 ppc64_elf_reloc_type_lookup (bfd *abfd,
909 bfd_reloc_code_real_type code)
910 {
911 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
912
913 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
914 /* Initialize howto table if needed. */
915 ppc_howto_init ();
916
917 switch (code)
918 {
919 default:
920 /* xgettext:c-format */
921 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
922 (int) code);
923 bfd_set_error (bfd_error_bad_value);
924 return NULL;
925
926 case BFD_RELOC_NONE: r = R_PPC64_NONE;
927 break;
928 case BFD_RELOC_32: r = R_PPC64_ADDR32;
929 break;
930 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
931 break;
932 case BFD_RELOC_16: r = R_PPC64_ADDR16;
933 break;
934 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
935 break;
936 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
937 break;
938 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
939 break;
940 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
941 break;
942 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
943 break;
944 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
945 break;
946 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
947 break;
948 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
949 break;
950 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
951 break;
952 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
953 break;
954 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
955 break;
956 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
957 break;
958 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
959 break;
960 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
961 break;
962 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
963 break;
964 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
965 break;
966 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
967 break;
968 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
969 break;
970 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
971 break;
972 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
973 break;
974 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
975 break;
976 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
977 break;
978 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
979 break;
980 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
981 break;
982 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
983 break;
984 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
985 break;
986 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
987 break;
988 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
989 break;
990 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
991 break;
992 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
993 break;
994 case BFD_RELOC_64: r = R_PPC64_ADDR64;
995 break;
996 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
997 break;
998 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
999 break;
1000 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1001 break;
1002 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1003 break;
1004 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1005 break;
1006 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1007 break;
1008 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1009 break;
1010 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1011 break;
1012 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1013 break;
1014 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1015 break;
1016 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1017 break;
1018 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1019 break;
1020 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1021 break;
1022 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1023 break;
1024 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1025 break;
1026 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1027 break;
1028 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1029 break;
1030 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1031 break;
1032 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1033 break;
1034 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1035 break;
1036 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1037 break;
1038 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1039 break;
1040 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1041 break;
1042 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1043 break;
1044 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1045 break;
1046 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1047 break;
1048 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1049 break;
1050 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1051 break;
1052 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1053 break;
1054 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1055 break;
1056 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1057 break;
1058 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1059 break;
1060 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1061 break;
1062 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1063 break;
1064 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1065 break;
1066 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1067 break;
1068 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1069 break;
1070 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1071 break;
1072 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1073 break;
1074 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1075 break;
1076 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1077 break;
1078 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1079 break;
1080 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1081 break;
1082 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1083 break;
1084 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1085 break;
1086 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1087 break;
1088 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1089 break;
1090 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1091 break;
1092 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1093 break;
1094 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1095 break;
1096 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1097 break;
1098 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1099 break;
1100 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1101 break;
1102 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1103 break;
1104 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1105 break;
1106 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1107 break;
1108 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1109 break;
1110 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1111 break;
1112 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1113 break;
1114 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1115 break;
1116 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1117 break;
1118 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1119 break;
1120 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1121 break;
1122 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1123 break;
1124 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1125 break;
1126 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1127 break;
1128 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1129 break;
1130 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1131 break;
1132 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1133 break;
1134 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1135 break;
1136 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1137 break;
1138 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1139 break;
1140 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1141 break;
1142 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1143 break;
1144 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1145 break;
1146 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1147 break;
1148 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1149 break;
1150 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1151 break;
1152 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1153 break;
1154 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1155 break;
1156 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1157 break;
1158 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1159 break;
1160 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1161 break;
1162 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1163 break;
1164 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1165 break;
1166 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1167 break;
1168 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1169 break;
1170 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1171 break;
1172 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1173 break;
1174 }
1175
1176 return ppc64_elf_howto_table[r];
1177 };
1178
1179 static reloc_howto_type *
1180 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1181 const char *r_name)
1182 {
1183 unsigned int i;
1184
1185 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1186 if (ppc64_elf_howto_raw[i].name != NULL
1187 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1188 return &ppc64_elf_howto_raw[i];
1189
1190 return NULL;
1191 }
1192
1193 /* Set the howto pointer for a PowerPC ELF reloc. */
1194
1195 static bfd_boolean
1196 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1197 Elf_Internal_Rela *dst)
1198 {
1199 unsigned int type;
1200
1201 /* Initialize howto table if needed. */
1202 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1203 ppc_howto_init ();
1204
1205 type = ELF64_R_TYPE (dst->r_info);
1206 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1207 {
1208 /* xgettext:c-format */
1209 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1210 abfd, type);
1211 bfd_set_error (bfd_error_bad_value);
1212 return FALSE;
1213 }
1214 cache_ptr->howto = ppc64_elf_howto_table[type];
1215 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1216 {
1217 /* xgettext:c-format */
1218 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1219 abfd, type);
1220 bfd_set_error (bfd_error_bad_value);
1221 return FALSE;
1222 }
1223
1224 return TRUE;
1225 }
1226
1227 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1228
1229 static bfd_reloc_status_type
1230 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1231 void *data, asection *input_section,
1232 bfd *output_bfd, char **error_message)
1233 {
1234 enum elf_ppc64_reloc_type r_type;
1235 long insn;
1236 bfd_size_type octets;
1237 bfd_vma value;
1238
1239 /* If this is a relocatable link (output_bfd test tells us), just
1240 call the generic function. Any adjustment will be done at final
1241 link time. */
1242 if (output_bfd != NULL)
1243 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1244 input_section, output_bfd, error_message);
1245
1246 /* Adjust the addend for sign extension of the low 16 bits.
1247 We won't actually be using the low 16 bits, so trashing them
1248 doesn't matter. */
1249 reloc_entry->addend += 0x8000;
1250 r_type = reloc_entry->howto->type;
1251 if (r_type != R_PPC64_REL16DX_HA)
1252 return bfd_reloc_continue;
1253
1254 value = 0;
1255 if (!bfd_is_com_section (symbol->section))
1256 value = symbol->value;
1257 value += (reloc_entry->addend
1258 + symbol->section->output_offset
1259 + symbol->section->output_section->vma);
1260 value -= (reloc_entry->address
1261 + input_section->output_offset
1262 + input_section->output_section->vma);
1263 value = (bfd_signed_vma) value >> 16;
1264
1265 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1266 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1267 insn &= ~0x1fffc1;
1268 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1269 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1270 if (value + 0x8000 > 0xffff)
1271 return bfd_reloc_overflow;
1272 return bfd_reloc_ok;
1273 }
1274
1275 static bfd_reloc_status_type
1276 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1277 void *data, asection *input_section,
1278 bfd *output_bfd, char **error_message)
1279 {
1280 if (output_bfd != NULL)
1281 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1282 input_section, output_bfd, error_message);
1283
1284 if (strcmp (symbol->section->name, ".opd") == 0
1285 && (symbol->section->owner->flags & DYNAMIC) == 0)
1286 {
1287 bfd_vma dest = opd_entry_value (symbol->section,
1288 symbol->value + reloc_entry->addend,
1289 NULL, NULL, FALSE);
1290 if (dest != (bfd_vma) -1)
1291 reloc_entry->addend = dest - (symbol->value
1292 + symbol->section->output_section->vma
1293 + symbol->section->output_offset);
1294 }
1295 else
1296 {
1297 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1298
1299 if (symbol->section->owner != abfd
1300 && symbol->section->owner != NULL
1301 && abiversion (symbol->section->owner) >= 2)
1302 {
1303 unsigned int i;
1304
1305 for (i = 0; i < symbol->section->owner->symcount; ++i)
1306 {
1307 asymbol *symdef = symbol->section->owner->outsymbols[i];
1308
1309 if (strcmp (symdef->name, symbol->name) == 0)
1310 {
1311 elfsym = (elf_symbol_type *) symdef;
1312 break;
1313 }
1314 }
1315 }
1316 reloc_entry->addend
1317 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1318 }
1319 return bfd_reloc_continue;
1320 }
1321
1322 static bfd_reloc_status_type
1323 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1324 void *data, asection *input_section,
1325 bfd *output_bfd, char **error_message)
1326 {
1327 long insn;
1328 enum elf_ppc64_reloc_type r_type;
1329 bfd_size_type octets;
1330 /* Assume 'at' branch hints. */
1331 bfd_boolean is_isa_v2 = TRUE;
1332
1333 /* If this is a relocatable link (output_bfd test tells us), just
1334 call the generic function. Any adjustment will be done at final
1335 link time. */
1336 if (output_bfd != NULL)
1337 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1338 input_section, output_bfd, error_message);
1339
1340 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1341 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1342 insn &= ~(0x01 << 21);
1343 r_type = reloc_entry->howto->type;
1344 if (r_type == R_PPC64_ADDR14_BRTAKEN
1345 || r_type == R_PPC64_REL14_BRTAKEN)
1346 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1347
1348 if (is_isa_v2)
1349 {
1350 /* Set 'a' bit. This is 0b00010 in BO field for branch
1351 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1352 for branch on CTR insns (BO == 1a00t or 1a01t). */
1353 if ((insn & (0x14 << 21)) == (0x04 << 21))
1354 insn |= 0x02 << 21;
1355 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1356 insn |= 0x08 << 21;
1357 else
1358 goto out;
1359 }
1360 else
1361 {
1362 bfd_vma target = 0;
1363 bfd_vma from;
1364
1365 if (!bfd_is_com_section (symbol->section))
1366 target = symbol->value;
1367 target += symbol->section->output_section->vma;
1368 target += symbol->section->output_offset;
1369 target += reloc_entry->addend;
1370
1371 from = (reloc_entry->address
1372 + input_section->output_offset
1373 + input_section->output_section->vma);
1374
1375 /* Invert 'y' bit if not the default. */
1376 if ((bfd_signed_vma) (target - from) < 0)
1377 insn ^= 0x01 << 21;
1378 }
1379 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1380 out:
1381 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1382 input_section, output_bfd, error_message);
1383 }
1384
1385 static bfd_reloc_status_type
1386 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1387 void *data, asection *input_section,
1388 bfd *output_bfd, char **error_message)
1389 {
1390 /* If this is a relocatable link (output_bfd test tells us), just
1391 call the generic function. Any adjustment will be done at final
1392 link time. */
1393 if (output_bfd != NULL)
1394 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1395 input_section, output_bfd, error_message);
1396
1397 /* Subtract the symbol section base address. */
1398 reloc_entry->addend -= symbol->section->output_section->vma;
1399 return bfd_reloc_continue;
1400 }
1401
1402 static bfd_reloc_status_type
1403 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1404 void *data, asection *input_section,
1405 bfd *output_bfd, char **error_message)
1406 {
1407 /* If this is a relocatable link (output_bfd test tells us), just
1408 call the generic function. Any adjustment will be done at final
1409 link time. */
1410 if (output_bfd != NULL)
1411 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1412 input_section, output_bfd, error_message);
1413
1414 /* Subtract the symbol section base address. */
1415 reloc_entry->addend -= symbol->section->output_section->vma;
1416
1417 /* Adjust the addend for sign extension of the low 16 bits. */
1418 reloc_entry->addend += 0x8000;
1419 return bfd_reloc_continue;
1420 }
1421
1422 static bfd_reloc_status_type
1423 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1424 void *data, asection *input_section,
1425 bfd *output_bfd, char **error_message)
1426 {
1427 bfd_vma TOCstart;
1428
1429 /* If this is a relocatable link (output_bfd test tells us), just
1430 call the generic function. Any adjustment will be done at final
1431 link time. */
1432 if (output_bfd != NULL)
1433 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1434 input_section, output_bfd, error_message);
1435
1436 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1437 if (TOCstart == 0)
1438 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1439
1440 /* Subtract the TOC base address. */
1441 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1442 return bfd_reloc_continue;
1443 }
1444
1445 static bfd_reloc_status_type
1446 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1447 void *data, asection *input_section,
1448 bfd *output_bfd, char **error_message)
1449 {
1450 bfd_vma TOCstart;
1451
1452 /* If this is a relocatable link (output_bfd test tells us), just
1453 call the generic function. Any adjustment will be done at final
1454 link time. */
1455 if (output_bfd != NULL)
1456 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1457 input_section, output_bfd, error_message);
1458
1459 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1460 if (TOCstart == 0)
1461 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1462
1463 /* Subtract the TOC base address. */
1464 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1465
1466 /* Adjust the addend for sign extension of the low 16 bits. */
1467 reloc_entry->addend += 0x8000;
1468 return bfd_reloc_continue;
1469 }
1470
1471 static bfd_reloc_status_type
1472 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1473 void *data, asection *input_section,
1474 bfd *output_bfd, char **error_message)
1475 {
1476 bfd_vma TOCstart;
1477 bfd_size_type octets;
1478
1479 /* If this is a relocatable link (output_bfd test tells us), just
1480 call the generic function. Any adjustment will be done at final
1481 link time. */
1482 if (output_bfd != NULL)
1483 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1484 input_section, output_bfd, error_message);
1485
1486 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1487 if (TOCstart == 0)
1488 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1489
1490 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1491 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1492 return bfd_reloc_ok;
1493 }
1494
1495 static bfd_reloc_status_type
1496 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1497 void *data, asection *input_section,
1498 bfd *output_bfd, char **error_message)
1499 {
1500 /* If this is a relocatable link (output_bfd test tells us), just
1501 call the generic function. Any adjustment will be done at final
1502 link time. */
1503 if (output_bfd != NULL)
1504 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1505 input_section, output_bfd, error_message);
1506
1507 if (error_message != NULL)
1508 {
1509 static char buf[60];
1510 sprintf (buf, "generic linker can't handle %s",
1511 reloc_entry->howto->name);
1512 *error_message = buf;
1513 }
1514 return bfd_reloc_dangerous;
1515 }
1516
1517 /* Track GOT entries needed for a given symbol. We might need more
1518 than one got entry per symbol. */
1519 struct got_entry
1520 {
1521 struct got_entry *next;
1522
1523 /* The symbol addend that we'll be placing in the GOT. */
1524 bfd_vma addend;
1525
1526 /* Unlike other ELF targets, we use separate GOT entries for the same
1527 symbol referenced from different input files. This is to support
1528 automatic multiple TOC/GOT sections, where the TOC base can vary
1529 from one input file to another. After partitioning into TOC groups
1530 we merge entries within the group.
1531
1532 Point to the BFD owning this GOT entry. */
1533 bfd *owner;
1534
1535 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1536 TLS_TPREL or TLS_DTPREL for tls entries. */
1537 unsigned char tls_type;
1538
1539 /* Non-zero if got.ent points to real entry. */
1540 unsigned char is_indirect;
1541
1542 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1543 union
1544 {
1545 bfd_signed_vma refcount;
1546 bfd_vma offset;
1547 struct got_entry *ent;
1548 } got;
1549 };
1550
1551 /* The same for PLT. */
1552 struct plt_entry
1553 {
1554 struct plt_entry *next;
1555
1556 bfd_vma addend;
1557
1558 union
1559 {
1560 bfd_signed_vma refcount;
1561 bfd_vma offset;
1562 } plt;
1563 };
1564
1565 struct ppc64_elf_obj_tdata
1566 {
1567 struct elf_obj_tdata elf;
1568
1569 /* Shortcuts to dynamic linker sections. */
1570 asection *got;
1571 asection *relgot;
1572
1573 /* Used during garbage collection. We attach global symbols defined
1574 on removed .opd entries to this section so that the sym is removed. */
1575 asection *deleted_section;
1576
1577 /* TLS local dynamic got entry handling. Support for multiple GOT
1578 sections means we potentially need one of these for each input bfd. */
1579 struct got_entry tlsld_got;
1580
1581 union
1582 {
1583 /* A copy of relocs before they are modified for --emit-relocs. */
1584 Elf_Internal_Rela *relocs;
1585
1586 /* Section contents. */
1587 bfd_byte *contents;
1588 } opd;
1589
1590 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1591 the reloc to be in the range -32768 to 32767. */
1592 unsigned int has_small_toc_reloc : 1;
1593
1594 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1595 instruction not one we handle. */
1596 unsigned int unexpected_toc_insn : 1;
1597 };
1598
1599 #define ppc64_elf_tdata(bfd) \
1600 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1601
1602 #define ppc64_tlsld_got(bfd) \
1603 (&ppc64_elf_tdata (bfd)->tlsld_got)
1604
1605 #define is_ppc64_elf(bfd) \
1606 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1607 && elf_object_id (bfd) == PPC64_ELF_DATA)
1608
1609 /* Override the generic function because we store some extras. */
1610
1611 static bfd_boolean
1612 ppc64_elf_mkobject (bfd *abfd)
1613 {
1614 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1615 PPC64_ELF_DATA);
1616 }
1617
1618 /* Fix bad default arch selected for a 64 bit input bfd when the
1619 default is 32 bit. Also select arch based on apuinfo. */
1620
1621 static bfd_boolean
1622 ppc64_elf_object_p (bfd *abfd)
1623 {
1624 if (!abfd->arch_info->the_default)
1625 return TRUE;
1626
1627 if (abfd->arch_info->bits_per_word == 32)
1628 {
1629 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1630
1631 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1632 {
1633 /* Relies on arch after 32 bit default being 64 bit default. */
1634 abfd->arch_info = abfd->arch_info->next;
1635 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1636 }
1637 }
1638 return _bfd_elf_ppc_set_arch (abfd);
1639 }
1640
1641 /* Support for core dump NOTE sections. */
1642
1643 static bfd_boolean
1644 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1645 {
1646 size_t offset, size;
1647
1648 if (note->descsz != 504)
1649 return FALSE;
1650
1651 /* pr_cursig */
1652 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1653
1654 /* pr_pid */
1655 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1656
1657 /* pr_reg */
1658 offset = 112;
1659 size = 384;
1660
1661 /* Make a ".reg/999" section. */
1662 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1663 size, note->descpos + offset);
1664 }
1665
1666 static bfd_boolean
1667 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1668 {
1669 if (note->descsz != 136)
1670 return FALSE;
1671
1672 elf_tdata (abfd)->core->pid
1673 = bfd_get_32 (abfd, note->descdata + 24);
1674 elf_tdata (abfd)->core->program
1675 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1676 elf_tdata (abfd)->core->command
1677 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1678
1679 return TRUE;
1680 }
1681
1682 static char *
1683 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1684 ...)
1685 {
1686 switch (note_type)
1687 {
1688 default:
1689 return NULL;
1690
1691 case NT_PRPSINFO:
1692 {
1693 char data[136] ATTRIBUTE_NONSTRING;
1694 va_list ap;
1695
1696 va_start (ap, note_type);
1697 memset (data, 0, sizeof (data));
1698 strncpy (data + 40, va_arg (ap, const char *), 16);
1699 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1700 DIAGNOSTIC_PUSH;
1701 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1702 -Wstringop-truncation:
1703 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1704 */
1705 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1706 #endif
1707 strncpy (data + 56, va_arg (ap, const char *), 80);
1708 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1709 DIAGNOSTIC_POP;
1710 #endif
1711 va_end (ap);
1712 return elfcore_write_note (abfd, buf, bufsiz,
1713 "CORE", note_type, data, sizeof (data));
1714 }
1715
1716 case NT_PRSTATUS:
1717 {
1718 char data[504];
1719 va_list ap;
1720 long pid;
1721 int cursig;
1722 const void *greg;
1723
1724 va_start (ap, note_type);
1725 memset (data, 0, 112);
1726 pid = va_arg (ap, long);
1727 bfd_put_32 (abfd, pid, data + 32);
1728 cursig = va_arg (ap, int);
1729 bfd_put_16 (abfd, cursig, data + 12);
1730 greg = va_arg (ap, const void *);
1731 memcpy (data + 112, greg, 384);
1732 memset (data + 496, 0, 8);
1733 va_end (ap);
1734 return elfcore_write_note (abfd, buf, bufsiz,
1735 "CORE", note_type, data, sizeof (data));
1736 }
1737 }
1738 }
1739
1740 /* Add extra PPC sections. */
1741
1742 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1743 {
1744 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1745 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1746 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1747 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1748 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1749 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1750 { NULL, 0, 0, 0, 0 }
1751 };
1752
1753 enum _ppc64_sec_type {
1754 sec_normal = 0,
1755 sec_opd = 1,
1756 sec_toc = 2
1757 };
1758
1759 struct _ppc64_elf_section_data
1760 {
1761 struct bfd_elf_section_data elf;
1762
1763 union
1764 {
1765 /* An array with one entry for each opd function descriptor,
1766 and some spares since opd entries may be either 16 or 24 bytes. */
1767 #define OPD_NDX(OFF) ((OFF) >> 4)
1768 struct _opd_sec_data
1769 {
1770 /* Points to the function code section for local opd entries. */
1771 asection **func_sec;
1772
1773 /* After editing .opd, adjust references to opd local syms. */
1774 long *adjust;
1775 } opd;
1776
1777 /* An array for toc sections, indexed by offset/8. */
1778 struct _toc_sec_data
1779 {
1780 /* Specifies the relocation symbol index used at a given toc offset. */
1781 unsigned *symndx;
1782
1783 /* And the relocation addend. */
1784 bfd_vma *add;
1785 } toc;
1786 } u;
1787
1788 enum _ppc64_sec_type sec_type:2;
1789
1790 /* Flag set when small branches are detected. Used to
1791 select suitable defaults for the stub group size. */
1792 unsigned int has_14bit_branch:1;
1793
1794 /* Flag set when PLTCALL relocs are detected. */
1795 unsigned int has_pltcall:1;
1796 };
1797
1798 #define ppc64_elf_section_data(sec) \
1799 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
1800
1801 static bfd_boolean
1802 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
1803 {
1804 if (!sec->used_by_bfd)
1805 {
1806 struct _ppc64_elf_section_data *sdata;
1807 bfd_size_type amt = sizeof (*sdata);
1808
1809 sdata = bfd_zalloc (abfd, amt);
1810 if (sdata == NULL)
1811 return FALSE;
1812 sec->used_by_bfd = sdata;
1813 }
1814
1815 return _bfd_elf_new_section_hook (abfd, sec);
1816 }
1817
1818 static struct _opd_sec_data *
1819 get_opd_info (asection * sec)
1820 {
1821 if (sec != NULL
1822 && ppc64_elf_section_data (sec) != NULL
1823 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
1824 return &ppc64_elf_section_data (sec)->u.opd;
1825 return NULL;
1826 }
1827 \f
1828 /* Parameters for the qsort hook. */
1829 static bfd_boolean synthetic_relocatable;
1830 static asection *synthetic_opd;
1831
1832 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
1833
1834 static int
1835 compare_symbols (const void *ap, const void *bp)
1836 {
1837 const asymbol *a = *(const asymbol **) ap;
1838 const asymbol *b = *(const asymbol **) bp;
1839
1840 /* Section symbols first. */
1841 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
1842 return -1;
1843 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
1844 return 1;
1845
1846 /* then .opd symbols. */
1847 if (synthetic_opd != NULL)
1848 {
1849 if (strcmp (a->section->name, ".opd") == 0
1850 && strcmp (b->section->name, ".opd") != 0)
1851 return -1;
1852 if (strcmp (a->section->name, ".opd") != 0
1853 && strcmp (b->section->name, ".opd") == 0)
1854 return 1;
1855 }
1856
1857 /* then other code symbols. */
1858 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1859 == (SEC_CODE | SEC_ALLOC))
1860 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1861 != (SEC_CODE | SEC_ALLOC)))
1862 return -1;
1863
1864 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1865 != (SEC_CODE | SEC_ALLOC))
1866 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1867 == (SEC_CODE | SEC_ALLOC)))
1868 return 1;
1869
1870 if (synthetic_relocatable)
1871 {
1872 if (a->section->id < b->section->id)
1873 return -1;
1874
1875 if (a->section->id > b->section->id)
1876 return 1;
1877 }
1878
1879 if (a->value + a->section->vma < b->value + b->section->vma)
1880 return -1;
1881
1882 if (a->value + a->section->vma > b->value + b->section->vma)
1883 return 1;
1884
1885 /* For syms with the same value, prefer strong dynamic global function
1886 syms over other syms. */
1887 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
1888 return -1;
1889
1890 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
1891 return 1;
1892
1893 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
1894 return -1;
1895
1896 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
1897 return 1;
1898
1899 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
1900 return -1;
1901
1902 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
1903 return 1;
1904
1905 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
1906 return -1;
1907
1908 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
1909 return 1;
1910
1911 return a > b;
1912 }
1913
1914 /* Search SYMS for a symbol of the given VALUE. */
1915
1916 static asymbol *
1917 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
1918 {
1919 long mid;
1920
1921 if (id == (unsigned) -1)
1922 {
1923 while (lo < hi)
1924 {
1925 mid = (lo + hi) >> 1;
1926 if (syms[mid]->value + syms[mid]->section->vma < value)
1927 lo = mid + 1;
1928 else if (syms[mid]->value + syms[mid]->section->vma > value)
1929 hi = mid;
1930 else
1931 return syms[mid];
1932 }
1933 }
1934 else
1935 {
1936 while (lo < hi)
1937 {
1938 mid = (lo + hi) >> 1;
1939 if (syms[mid]->section->id < id)
1940 lo = mid + 1;
1941 else if (syms[mid]->section->id > id)
1942 hi = mid;
1943 else if (syms[mid]->value < value)
1944 lo = mid + 1;
1945 else if (syms[mid]->value > value)
1946 hi = mid;
1947 else
1948 return syms[mid];
1949 }
1950 }
1951 return NULL;
1952 }
1953
1954 static bfd_boolean
1955 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
1956 {
1957 bfd_vma vma = *(bfd_vma *) ptr;
1958 return ((section->flags & SEC_ALLOC) != 0
1959 && section->vma <= vma
1960 && vma < section->vma + section->size);
1961 }
1962
1963 /* Create synthetic symbols, effectively restoring "dot-symbol" function
1964 entry syms. Also generate @plt symbols for the glink branch table.
1965 Returns count of synthetic symbols in RET or -1 on error. */
1966
1967 static long
1968 ppc64_elf_get_synthetic_symtab (bfd *abfd,
1969 long static_count, asymbol **static_syms,
1970 long dyn_count, asymbol **dyn_syms,
1971 asymbol **ret)
1972 {
1973 asymbol *s;
1974 size_t i, j, count;
1975 char *names;
1976 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
1977 asection *opd = NULL;
1978 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
1979 asymbol **syms;
1980 int abi = abiversion (abfd);
1981
1982 *ret = NULL;
1983
1984 if (abi < 2)
1985 {
1986 opd = bfd_get_section_by_name (abfd, ".opd");
1987 if (opd == NULL && abi == 1)
1988 return 0;
1989 }
1990
1991 syms = NULL;
1992 codesecsym = 0;
1993 codesecsymend = 0;
1994 secsymend = 0;
1995 opdsymend = 0;
1996 symcount = 0;
1997 if (opd != NULL)
1998 {
1999 symcount = static_count;
2000 if (!relocatable)
2001 symcount += dyn_count;
2002 if (symcount == 0)
2003 return 0;
2004
2005 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2006 if (syms == NULL)
2007 return -1;
2008
2009 if (!relocatable && static_count != 0 && dyn_count != 0)
2010 {
2011 /* Use both symbol tables. */
2012 memcpy (syms, static_syms, static_count * sizeof (*syms));
2013 memcpy (syms + static_count, dyn_syms,
2014 (dyn_count + 1) * sizeof (*syms));
2015 }
2016 else if (!relocatable && static_count == 0)
2017 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2018 else
2019 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2020
2021 /* Trim uninteresting symbols. Interesting symbols are section,
2022 function, and notype symbols. */
2023 for (i = 0, j = 0; i < symcount; ++i)
2024 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2025 | BSF_RELC | BSF_SRELC)) == 0)
2026 syms[j++] = syms[i];
2027 symcount = j;
2028
2029 synthetic_relocatable = relocatable;
2030 synthetic_opd = opd;
2031 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2032
2033 if (!relocatable && symcount > 1)
2034 {
2035 /* Trim duplicate syms, since we may have merged the normal
2036 and dynamic symbols. Actually, we only care about syms
2037 that have different values, so trim any with the same
2038 value. Don't consider ifunc and ifunc resolver symbols
2039 duplicates however, because GDB wants to know whether a
2040 text symbol is an ifunc resolver. */
2041 for (i = 1, j = 1; i < symcount; ++i)
2042 {
2043 const asymbol *s0 = syms[i - 1];
2044 const asymbol *s1 = syms[i];
2045
2046 if ((s0->value + s0->section->vma
2047 != s1->value + s1->section->vma)
2048 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2049 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2050 syms[j++] = syms[i];
2051 }
2052 symcount = j;
2053 }
2054
2055 i = 0;
2056 /* Note that here and in compare_symbols we can't compare opd and
2057 sym->section directly. With separate debug info files, the
2058 symbols will be extracted from the debug file while abfd passed
2059 to this function is the real binary. */
2060 if (strcmp (syms[i]->section->name, ".opd") == 0)
2061 ++i;
2062 codesecsym = i;
2063
2064 for (; i < symcount; ++i)
2065 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2066 | SEC_THREAD_LOCAL))
2067 != (SEC_CODE | SEC_ALLOC))
2068 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2069 break;
2070 codesecsymend = i;
2071
2072 for (; i < symcount; ++i)
2073 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2074 break;
2075 secsymend = i;
2076
2077 for (; i < symcount; ++i)
2078 if (strcmp (syms[i]->section->name, ".opd") != 0)
2079 break;
2080 opdsymend = i;
2081
2082 for (; i < symcount; ++i)
2083 if (((syms[i]->section->flags
2084 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2085 != (SEC_CODE | SEC_ALLOC))
2086 break;
2087 symcount = i;
2088 }
2089 count = 0;
2090
2091 if (relocatable)
2092 {
2093 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2094 arelent *r;
2095 size_t size;
2096 size_t relcount;
2097
2098 if (opdsymend == secsymend)
2099 goto done;
2100
2101 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2102 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2103 if (relcount == 0)
2104 goto done;
2105
2106 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2107 {
2108 count = -1;
2109 goto done;
2110 }
2111
2112 size = 0;
2113 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2114 {
2115 asymbol *sym;
2116
2117 while (r < opd->relocation + relcount
2118 && r->address < syms[i]->value + opd->vma)
2119 ++r;
2120
2121 if (r == opd->relocation + relcount)
2122 break;
2123
2124 if (r->address != syms[i]->value + opd->vma)
2125 continue;
2126
2127 if (r->howto->type != R_PPC64_ADDR64)
2128 continue;
2129
2130 sym = *r->sym_ptr_ptr;
2131 if (!sym_exists_at (syms, opdsymend, symcount,
2132 sym->section->id, sym->value + r->addend))
2133 {
2134 ++count;
2135 size += sizeof (asymbol);
2136 size += strlen (syms[i]->name) + 2;
2137 }
2138 }
2139
2140 if (size == 0)
2141 goto done;
2142 s = *ret = bfd_malloc (size);
2143 if (s == NULL)
2144 {
2145 count = -1;
2146 goto done;
2147 }
2148
2149 names = (char *) (s + count);
2150
2151 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2152 {
2153 asymbol *sym;
2154
2155 while (r < opd->relocation + relcount
2156 && r->address < syms[i]->value + opd->vma)
2157 ++r;
2158
2159 if (r == opd->relocation + relcount)
2160 break;
2161
2162 if (r->address != syms[i]->value + opd->vma)
2163 continue;
2164
2165 if (r->howto->type != R_PPC64_ADDR64)
2166 continue;
2167
2168 sym = *r->sym_ptr_ptr;
2169 if (!sym_exists_at (syms, opdsymend, symcount,
2170 sym->section->id, sym->value + r->addend))
2171 {
2172 size_t len;
2173
2174 *s = *syms[i];
2175 s->flags |= BSF_SYNTHETIC;
2176 s->section = sym->section;
2177 s->value = sym->value + r->addend;
2178 s->name = names;
2179 *names++ = '.';
2180 len = strlen (syms[i]->name);
2181 memcpy (names, syms[i]->name, len + 1);
2182 names += len + 1;
2183 /* Have udata.p point back to the original symbol this
2184 synthetic symbol was derived from. */
2185 s->udata.p = syms[i];
2186 s++;
2187 }
2188 }
2189 }
2190 else
2191 {
2192 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2193 bfd_byte *contents = NULL;
2194 size_t size;
2195 size_t plt_count = 0;
2196 bfd_vma glink_vma = 0, resolv_vma = 0;
2197 asection *dynamic, *glink = NULL, *relplt = NULL;
2198 arelent *p;
2199
2200 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2201 {
2202 free_contents_and_exit_err:
2203 count = -1;
2204 free_contents_and_exit:
2205 if (contents)
2206 free (contents);
2207 goto done;
2208 }
2209
2210 size = 0;
2211 for (i = secsymend; i < opdsymend; ++i)
2212 {
2213 bfd_vma ent;
2214
2215 /* Ignore bogus symbols. */
2216 if (syms[i]->value > opd->size - 8)
2217 continue;
2218
2219 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2220 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2221 {
2222 ++count;
2223 size += sizeof (asymbol);
2224 size += strlen (syms[i]->name) + 2;
2225 }
2226 }
2227
2228 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2229 if (dyn_count != 0
2230 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2231 {
2232 bfd_byte *dynbuf, *extdyn, *extdynend;
2233 size_t extdynsize;
2234 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2235
2236 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2237 goto free_contents_and_exit_err;
2238
2239 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2240 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2241
2242 extdyn = dynbuf;
2243 extdynend = extdyn + dynamic->size;
2244 for (; extdyn < extdynend; extdyn += extdynsize)
2245 {
2246 Elf_Internal_Dyn dyn;
2247 (*swap_dyn_in) (abfd, extdyn, &dyn);
2248
2249 if (dyn.d_tag == DT_NULL)
2250 break;
2251
2252 if (dyn.d_tag == DT_PPC64_GLINK)
2253 {
2254 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2255 See comment in ppc64_elf_finish_dynamic_sections. */
2256 glink_vma = dyn.d_un.d_val + 8 * 4;
2257 /* The .glink section usually does not survive the final
2258 link; search for the section (usually .text) where the
2259 glink stubs now reside. */
2260 glink = bfd_sections_find_if (abfd, section_covers_vma,
2261 &glink_vma);
2262 break;
2263 }
2264 }
2265
2266 free (dynbuf);
2267 }
2268
2269 if (glink != NULL)
2270 {
2271 /* Determine __glink trampoline by reading the relative branch
2272 from the first glink stub. */
2273 bfd_byte buf[4];
2274 unsigned int off = 0;
2275
2276 while (bfd_get_section_contents (abfd, glink, buf,
2277 glink_vma + off - glink->vma, 4))
2278 {
2279 unsigned int insn = bfd_get_32 (abfd, buf);
2280 insn ^= B_DOT;
2281 if ((insn & ~0x3fffffc) == 0)
2282 {
2283 resolv_vma
2284 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2285 break;
2286 }
2287 off += 4;
2288 if (off > 4)
2289 break;
2290 }
2291
2292 if (resolv_vma)
2293 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2294
2295 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2296 if (relplt != NULL)
2297 {
2298 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2299 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
2300 goto free_contents_and_exit_err;
2301
2302 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2303 size += plt_count * sizeof (asymbol);
2304
2305 p = relplt->relocation;
2306 for (i = 0; i < plt_count; i++, p++)
2307 {
2308 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2309 if (p->addend != 0)
2310 size += sizeof ("+0x") - 1 + 16;
2311 }
2312 }
2313 }
2314
2315 if (size == 0)
2316 goto free_contents_and_exit;
2317 s = *ret = bfd_malloc (size);
2318 if (s == NULL)
2319 goto free_contents_and_exit_err;
2320
2321 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2322
2323 for (i = secsymend; i < opdsymend; ++i)
2324 {
2325 bfd_vma ent;
2326
2327 if (syms[i]->value > opd->size - 8)
2328 continue;
2329
2330 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2331 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2332 {
2333 size_t lo, hi;
2334 size_t len;
2335 asection *sec = abfd->sections;
2336
2337 *s = *syms[i];
2338 lo = codesecsym;
2339 hi = codesecsymend;
2340 while (lo < hi)
2341 {
2342 size_t mid = (lo + hi) >> 1;
2343 if (syms[mid]->section->vma < ent)
2344 lo = mid + 1;
2345 else if (syms[mid]->section->vma > ent)
2346 hi = mid;
2347 else
2348 {
2349 sec = syms[mid]->section;
2350 break;
2351 }
2352 }
2353
2354 if (lo >= hi && lo > codesecsym)
2355 sec = syms[lo - 1]->section;
2356
2357 for (; sec != NULL; sec = sec->next)
2358 {
2359 if (sec->vma > ent)
2360 break;
2361 /* SEC_LOAD may not be set if SEC is from a separate debug
2362 info file. */
2363 if ((sec->flags & SEC_ALLOC) == 0)
2364 break;
2365 if ((sec->flags & SEC_CODE) != 0)
2366 s->section = sec;
2367 }
2368 s->flags |= BSF_SYNTHETIC;
2369 s->value = ent - s->section->vma;
2370 s->name = names;
2371 *names++ = '.';
2372 len = strlen (syms[i]->name);
2373 memcpy (names, syms[i]->name, len + 1);
2374 names += len + 1;
2375 /* Have udata.p point back to the original symbol this
2376 synthetic symbol was derived from. */
2377 s->udata.p = syms[i];
2378 s++;
2379 }
2380 }
2381 free (contents);
2382
2383 if (glink != NULL && relplt != NULL)
2384 {
2385 if (resolv_vma)
2386 {
2387 /* Add a symbol for the main glink trampoline. */
2388 memset (s, 0, sizeof *s);
2389 s->the_bfd = abfd;
2390 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2391 s->section = glink;
2392 s->value = resolv_vma - glink->vma;
2393 s->name = names;
2394 memcpy (names, "__glink_PLTresolve",
2395 sizeof ("__glink_PLTresolve"));
2396 names += sizeof ("__glink_PLTresolve");
2397 s++;
2398 count++;
2399 }
2400
2401 /* FIXME: It would be very much nicer to put sym@plt on the
2402 stub rather than on the glink branch table entry. The
2403 objdump disassembler would then use a sensible symbol
2404 name on plt calls. The difficulty in doing so is
2405 a) finding the stubs, and,
2406 b) matching stubs against plt entries, and,
2407 c) there can be multiple stubs for a given plt entry.
2408
2409 Solving (a) could be done by code scanning, but older
2410 ppc64 binaries used different stubs to current code.
2411 (b) is the tricky one since you need to known the toc
2412 pointer for at least one function that uses a pic stub to
2413 be able to calculate the plt address referenced.
2414 (c) means gdb would need to set multiple breakpoints (or
2415 find the glink branch itself) when setting breakpoints
2416 for pending shared library loads. */
2417 p = relplt->relocation;
2418 for (i = 0; i < plt_count; i++, p++)
2419 {
2420 size_t len;
2421
2422 *s = **p->sym_ptr_ptr;
2423 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2424 we are defining a symbol, ensure one of them is set. */
2425 if ((s->flags & BSF_LOCAL) == 0)
2426 s->flags |= BSF_GLOBAL;
2427 s->flags |= BSF_SYNTHETIC;
2428 s->section = glink;
2429 s->value = glink_vma - glink->vma;
2430 s->name = names;
2431 s->udata.p = NULL;
2432 len = strlen ((*p->sym_ptr_ptr)->name);
2433 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2434 names += len;
2435 if (p->addend != 0)
2436 {
2437 memcpy (names, "+0x", sizeof ("+0x") - 1);
2438 names += sizeof ("+0x") - 1;
2439 bfd_sprintf_vma (abfd, names, p->addend);
2440 names += strlen (names);
2441 }
2442 memcpy (names, "@plt", sizeof ("@plt"));
2443 names += sizeof ("@plt");
2444 s++;
2445 if (abi < 2)
2446 {
2447 glink_vma += 8;
2448 if (i >= 0x8000)
2449 glink_vma += 4;
2450 }
2451 else
2452 glink_vma += 4;
2453 }
2454 count += plt_count;
2455 }
2456 }
2457
2458 done:
2459 free (syms);
2460 return count;
2461 }
2462 \f
2463 /* The following functions are specific to the ELF linker, while
2464 functions above are used generally. Those named ppc64_elf_* are
2465 called by the main ELF linker code. They appear in this file more
2466 or less in the order in which they are called. eg.
2467 ppc64_elf_check_relocs is called early in the link process,
2468 ppc64_elf_finish_dynamic_sections is one of the last functions
2469 called.
2470
2471 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2472 functions have both a function code symbol and a function descriptor
2473 symbol. A call to foo in a relocatable object file looks like:
2474
2475 . .text
2476 . x:
2477 . bl .foo
2478 . nop
2479
2480 The function definition in another object file might be:
2481
2482 . .section .opd
2483 . foo: .quad .foo
2484 . .quad .TOC.@tocbase
2485 . .quad 0
2486 .
2487 . .text
2488 . .foo: blr
2489
2490 When the linker resolves the call during a static link, the branch
2491 unsurprisingly just goes to .foo and the .opd information is unused.
2492 If the function definition is in a shared library, things are a little
2493 different: The call goes via a plt call stub, the opd information gets
2494 copied to the plt, and the linker patches the nop.
2495
2496 . x:
2497 . bl .foo_stub
2498 . ld 2,40(1)
2499 .
2500 .
2501 . .foo_stub:
2502 . std 2,40(1) # in practice, the call stub
2503 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2504 . addi 11,11,Lfoo@toc@l # this is the general idea
2505 . ld 12,0(11)
2506 . ld 2,8(11)
2507 . mtctr 12
2508 . ld 11,16(11)
2509 . bctr
2510 .
2511 . .section .plt
2512 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2513
2514 The "reloc ()" notation is supposed to indicate that the linker emits
2515 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2516 copying.
2517
2518 What are the difficulties here? Well, firstly, the relocations
2519 examined by the linker in check_relocs are against the function code
2520 sym .foo, while the dynamic relocation in the plt is emitted against
2521 the function descriptor symbol, foo. Somewhere along the line, we need
2522 to carefully copy dynamic link information from one symbol to the other.
2523 Secondly, the generic part of the elf linker will make .foo a dynamic
2524 symbol as is normal for most other backends. We need foo dynamic
2525 instead, at least for an application final link. However, when
2526 creating a shared library containing foo, we need to have both symbols
2527 dynamic so that references to .foo are satisfied during the early
2528 stages of linking. Otherwise the linker might decide to pull in a
2529 definition from some other object, eg. a static library.
2530
2531 Update: As of August 2004, we support a new convention. Function
2532 calls may use the function descriptor symbol, ie. "bl foo". This
2533 behaves exactly as "bl .foo". */
2534
2535 /* Of those relocs that might be copied as dynamic relocs, this
2536 function selects those that must be copied when linking a shared
2537 library or PIE, even when the symbol is local. */
2538
2539 static int
2540 must_be_dyn_reloc (struct bfd_link_info *info,
2541 enum elf_ppc64_reloc_type r_type)
2542 {
2543 switch (r_type)
2544 {
2545 default:
2546 /* Only relative relocs can be resolved when the object load
2547 address isn't fixed. DTPREL64 is excluded because the
2548 dynamic linker needs to differentiate global dynamic from
2549 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2550 return 1;
2551
2552 case R_PPC64_REL32:
2553 case R_PPC64_REL64:
2554 case R_PPC64_REL30:
2555 return 0;
2556
2557 case R_PPC64_TPREL16:
2558 case R_PPC64_TPREL16_LO:
2559 case R_PPC64_TPREL16_HI:
2560 case R_PPC64_TPREL16_HA:
2561 case R_PPC64_TPREL16_DS:
2562 case R_PPC64_TPREL16_LO_DS:
2563 case R_PPC64_TPREL16_HIGH:
2564 case R_PPC64_TPREL16_HIGHA:
2565 case R_PPC64_TPREL16_HIGHER:
2566 case R_PPC64_TPREL16_HIGHERA:
2567 case R_PPC64_TPREL16_HIGHEST:
2568 case R_PPC64_TPREL16_HIGHESTA:
2569 case R_PPC64_TPREL64:
2570 /* These relocations are relative but in a shared library the
2571 linker doesn't know the thread pointer base. */
2572 return bfd_link_dll (info);
2573 }
2574 }
2575
2576 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2577 copying dynamic variables from a shared lib into an app's dynbss
2578 section, and instead use a dynamic relocation to point into the
2579 shared lib. With code that gcc generates, it's vital that this be
2580 enabled; In the PowerPC64 ABI, the address of a function is actually
2581 the address of a function descriptor, which resides in the .opd
2582 section. gcc uses the descriptor directly rather than going via the
2583 GOT as some other ABI's do, which means that initialized function
2584 pointers must reference the descriptor. Thus, a function pointer
2585 initialized to the address of a function in a shared library will
2586 either require a copy reloc, or a dynamic reloc. Using a copy reloc
2587 redefines the function descriptor symbol to point to the copy. This
2588 presents a problem as a plt entry for that function is also
2589 initialized from the function descriptor symbol and the copy reloc
2590 may not be initialized first. */
2591 #define ELIMINATE_COPY_RELOCS 1
2592
2593 /* Section name for stubs is the associated section name plus this
2594 string. */
2595 #define STUB_SUFFIX ".stub"
2596
2597 /* Linker stubs.
2598 ppc_stub_long_branch:
2599 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2600 destination, but a 24 bit branch in a stub section will reach.
2601 . b dest
2602
2603 ppc_stub_plt_branch:
2604 Similar to the above, but a 24 bit branch in the stub section won't
2605 reach its destination.
2606 . addis %r11,%r2,xxx@toc@ha
2607 . ld %r12,xxx@toc@l(%r11)
2608 . mtctr %r12
2609 . bctr
2610
2611 ppc_stub_plt_call:
2612 Used to call a function in a shared library. If it so happens that
2613 the plt entry referenced crosses a 64k boundary, then an extra
2614 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2615 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
2616 . addis %r11,%r2,xxx@toc@ha
2617 . ld %r12,xxx+0@toc@l(%r11)
2618 . mtctr %r12
2619 . ld %r2,xxx+8@toc@l(%r11)
2620 . ld %r11,xxx+16@toc@l(%r11)
2621 . bctr
2622
2623 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2624 code to adjust the value and save r2 to support multiple toc sections.
2625 A ppc_stub_long_branch with an r2 offset looks like:
2626 . std %r2,40(%r1)
2627 . addis %r2,%r2,off@ha
2628 . addi %r2,%r2,off@l
2629 . b dest
2630
2631 A ppc_stub_plt_branch with an r2 offset looks like:
2632 . std %r2,40(%r1)
2633 . addis %r11,%r2,xxx@toc@ha
2634 . ld %r12,xxx@toc@l(%r11)
2635 . addis %r2,%r2,off@ha
2636 . addi %r2,%r2,off@l
2637 . mtctr %r12
2638 . bctr
2639
2640 All of the above stubs are shown as their ELFv1 variants. ELFv2
2641 variants exist too, simpler for plt calls since a new toc pointer
2642 and static chain are not loaded by the stub. In addition, ELFv2
2643 has some more complex stubs to handle calls marked with NOTOC
2644 relocs from functions where r2 is not a valid toc pointer. These
2645 come in two flavours, the ones shown below, and _both variants that
2646 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2647 one call is from a function where r2 is used as the toc pointer but
2648 needs a toc adjusting stub for small-model multi-toc, and another
2649 call is from a function where r2 is not valid.
2650 ppc_stub_long_branch_notoc:
2651 . mflr %r12
2652 . bcl 20,31,1f
2653 . 1:
2654 . mflr %r11
2655 . mtlr %r12
2656 . addis %r12,%r11,dest-1b@ha
2657 . addi %r12,%r12,dest-1b@l
2658 . b dest
2659
2660 ppc_stub_plt_branch_notoc:
2661 . mflr %r12
2662 . bcl 20,31,1f
2663 . 1:
2664 . mflr %r11
2665 . mtlr %r12
2666 . lis %r12,xxx-1b@highest
2667 . ori %r12,%r12,xxx-1b@higher
2668 . sldi %r12,%r12,32
2669 . oris %r12,%r12,xxx-1b@high
2670 . ori %r12,%r12,xxx-1b@l
2671 . add %r12,%r11,%r12
2672 . mtctr %r12
2673 . bctr
2674
2675 ppc_stub_plt_call_notoc:
2676 . mflr %r12
2677 . bcl 20,31,1f
2678 . 1:
2679 . mflr %r11
2680 . mtlr %r12
2681 . lis %r12,xxx-1b@highest
2682 . ori %r12,%r12,xxx-1b@higher
2683 . sldi %r12,%r12,32
2684 . oris %r12,%r12,xxx-1b@high
2685 . ori %r12,%r12,xxx-1b@l
2686 . ldx %r12,%r11,%r12
2687 . mtctr %r12
2688 . bctr
2689
2690 In cases where the high instructions would add zero, they are
2691 omitted and following instructions modified in some cases.
2692
2693 For a given stub group (a set of sections all using the same toc
2694 pointer value) there will be just one stub type used for any
2695 particular function symbol. For example, if printf is called from
2696 code with the tocsave optimization (ie. r2 saved in function
2697 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2698 and from other code without the tocsave optimization requiring a
2699 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2700 type will be created. Calls with the tocsave optimization will
2701 enter this stub after the instruction saving r2. A similar
2702 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2703 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2704 to call an external function like printf. If other calls to printf
2705 require a ppc_stub_plt_call linkage stub then a single
2706 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2707 call. If other calls to printf require a ppc_stub_plt_call_r2save
2708 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2709 be created and calls not requiring r2 to be saved will enter the
2710 stub after the r2 save instruction. There is an analogous
2711 hierarchy of long branch and plt branch stubs for local call
2712 linkage. */
2713
2714 enum ppc_stub_type
2715 {
2716 ppc_stub_none,
2717 ppc_stub_long_branch,
2718 ppc_stub_long_branch_r2off,
2719 ppc_stub_long_branch_notoc,
2720 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
2721 ppc_stub_plt_branch,
2722 ppc_stub_plt_branch_r2off,
2723 ppc_stub_plt_branch_notoc,
2724 ppc_stub_plt_branch_both,
2725 ppc_stub_plt_call,
2726 ppc_stub_plt_call_r2save,
2727 ppc_stub_plt_call_notoc,
2728 ppc_stub_plt_call_both,
2729 ppc_stub_global_entry,
2730 ppc_stub_save_res
2731 };
2732
2733 /* Information on stub grouping. */
2734 struct map_stub
2735 {
2736 /* The stub section. */
2737 asection *stub_sec;
2738 /* This is the section to which stubs in the group will be attached. */
2739 asection *link_sec;
2740 /* Next group. */
2741 struct map_stub *next;
2742 /* Whether to emit a copy of register save/restore functions in this
2743 group. */
2744 int needs_save_res;
2745 /* Current offset within stubs after the insn restoring lr in a
2746 _notoc or _both stub using bcl for pc-relative addressing, or
2747 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2748 unsigned int lr_restore;
2749 /* Accumulated size of EH info emitted to describe return address
2750 if stubs modify lr. Does not include 17 byte FDE header. */
2751 unsigned int eh_size;
2752 /* Offset in glink_eh_frame to the start of EH info for this group. */
2753 unsigned int eh_base;
2754 };
2755
2756 struct ppc_stub_hash_entry
2757 {
2758 /* Base hash table entry structure. */
2759 struct bfd_hash_entry root;
2760
2761 enum ppc_stub_type stub_type;
2762
2763 /* Group information. */
2764 struct map_stub *group;
2765
2766 /* Offset within stub_sec of the beginning of this stub. */
2767 bfd_vma stub_offset;
2768
2769 /* Given the symbol's value and its section we can determine its final
2770 value when building the stubs (so the stub knows where to jump. */
2771 bfd_vma target_value;
2772 asection *target_section;
2773
2774 /* The symbol table entry, if any, that this was derived from. */
2775 struct ppc_link_hash_entry *h;
2776 struct plt_entry *plt_ent;
2777
2778 /* Symbol type. */
2779 unsigned char symtype;
2780
2781 /* Symbol st_other. */
2782 unsigned char other;
2783 };
2784
2785 struct ppc_branch_hash_entry
2786 {
2787 /* Base hash table entry structure. */
2788 struct bfd_hash_entry root;
2789
2790 /* Offset within branch lookup table. */
2791 unsigned int offset;
2792
2793 /* Generation marker. */
2794 unsigned int iter;
2795 };
2796
2797 /* Used to track dynamic relocations for local symbols. */
2798 struct ppc_dyn_relocs
2799 {
2800 struct ppc_dyn_relocs *next;
2801
2802 /* The input section of the reloc. */
2803 asection *sec;
2804
2805 /* Total number of relocs copied for the input section. */
2806 unsigned int count : 31;
2807
2808 /* Whether this entry is for STT_GNU_IFUNC symbols. */
2809 unsigned int ifunc : 1;
2810 };
2811
2812 struct ppc_link_hash_entry
2813 {
2814 struct elf_link_hash_entry elf;
2815
2816 union
2817 {
2818 /* A pointer to the most recently used stub hash entry against this
2819 symbol. */
2820 struct ppc_stub_hash_entry *stub_cache;
2821
2822 /* A pointer to the next symbol starting with a '.' */
2823 struct ppc_link_hash_entry *next_dot_sym;
2824 } u;
2825
2826 /* Track dynamic relocs copied for this symbol. */
2827 struct elf_dyn_relocs *dyn_relocs;
2828
2829 /* Link between function code and descriptor symbols. */
2830 struct ppc_link_hash_entry *oh;
2831
2832 /* Flag function code and descriptor symbols. */
2833 unsigned int is_func:1;
2834 unsigned int is_func_descriptor:1;
2835 unsigned int fake:1;
2836
2837 /* Whether global opd/toc sym has been adjusted or not.
2838 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
2839 should be set for all globals defined in any opd/toc section. */
2840 unsigned int adjust_done:1;
2841
2842 /* Set if this is an out-of-line register save/restore function,
2843 with non-standard calling convention. */
2844 unsigned int save_res:1;
2845
2846 /* Set if a duplicate symbol with non-zero localentry is detected,
2847 even when the duplicate symbol does not provide a definition. */
2848 unsigned int non_zero_localentry:1;
2849
2850 /* Contexts in which symbol is used in the GOT (or TOC).
2851 Bits are or'd into the mask as the corresponding relocs are
2852 encountered during check_relocs, with TLS_TLS being set when any
2853 of the other TLS bits are set. tls_optimize clears bits when
2854 optimizing to indicate the corresponding GOT entry type is not
2855 needed. If set, TLS_TLS is never cleared. tls_optimize may also
2856 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
2857 separate flag rather than setting TPREL just for convenience in
2858 distinguishing the two cases.
2859 These flags are also kept for local symbols. */
2860 #define TLS_TLS 1 /* Any TLS reloc. */
2861 #define TLS_GD 2 /* GD reloc. */
2862 #define TLS_LD 4 /* LD reloc. */
2863 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
2864 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
2865 #define TLS_MARK 32 /* __tls_get_addr call marked. */
2866 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
2867 #define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
2868 unsigned char tls_mask;
2869
2870 /* The above field is also used to mark function symbols. In which
2871 case TLS_TLS will be 0. */
2872 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2873 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
2874 #define NON_GOT 256 /* local symbol plt, not stored. */
2875 };
2876
2877 /* ppc64 ELF linker hash table. */
2878
2879 struct ppc_link_hash_table
2880 {
2881 struct elf_link_hash_table elf;
2882
2883 /* The stub hash table. */
2884 struct bfd_hash_table stub_hash_table;
2885
2886 /* Another hash table for plt_branch stubs. */
2887 struct bfd_hash_table branch_hash_table;
2888
2889 /* Hash table for function prologue tocsave. */
2890 htab_t tocsave_htab;
2891
2892 /* Various options and other info passed from the linker. */
2893 struct ppc64_elf_params *params;
2894
2895 /* The size of sec_info below. */
2896 unsigned int sec_info_arr_size;
2897
2898 /* Per-section array of extra section info. Done this way rather
2899 than as part of ppc64_elf_section_data so we have the info for
2900 non-ppc64 sections. */
2901 struct
2902 {
2903 /* Along with elf_gp, specifies the TOC pointer used by this section. */
2904 bfd_vma toc_off;
2905
2906 union
2907 {
2908 /* The section group that this section belongs to. */
2909 struct map_stub *group;
2910 /* A temp section list pointer. */
2911 asection *list;
2912 } u;
2913 } *sec_info;
2914
2915 /* Linked list of groups. */
2916 struct map_stub *group;
2917
2918 /* Temp used when calculating TOC pointers. */
2919 bfd_vma toc_curr;
2920 bfd *toc_bfd;
2921 asection *toc_first_sec;
2922
2923 /* Used when adding symbols. */
2924 struct ppc_link_hash_entry *dot_syms;
2925
2926 /* Shortcuts to get to dynamic linker sections. */
2927 asection *glink;
2928 asection *global_entry;
2929 asection *sfpr;
2930 asection *pltlocal;
2931 asection *relpltlocal;
2932 asection *brlt;
2933 asection *relbrlt;
2934 asection *glink_eh_frame;
2935
2936 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
2937 struct ppc_link_hash_entry *tls_get_addr;
2938 struct ppc_link_hash_entry *tls_get_addr_fd;
2939
2940 /* The size of reliplt used by got entry relocs. */
2941 bfd_size_type got_reli_size;
2942
2943 /* Statistics. */
2944 unsigned long stub_count[ppc_stub_global_entry];
2945
2946 /* Number of stubs against global syms. */
2947 unsigned long stub_globals;
2948
2949 /* Set if we're linking code with function descriptors. */
2950 unsigned int opd_abi:1;
2951
2952 /* Support for multiple toc sections. */
2953 unsigned int do_multi_toc:1;
2954 unsigned int multi_toc_needed:1;
2955 unsigned int second_toc_pass:1;
2956 unsigned int do_toc_opt:1;
2957
2958 /* Set if tls optimization is enabled. */
2959 unsigned int do_tls_opt:1;
2960
2961 /* Set if inline plt calls should be converted to direct calls. */
2962 unsigned int can_convert_all_inline_plt:1;
2963
2964 /* Set on error. */
2965 unsigned int stub_error:1;
2966
2967 /* Whether func_desc_adjust needs to be run over symbols. */
2968 unsigned int need_func_desc_adj:1;
2969
2970 /* Whether there exist local gnu indirect function resolvers,
2971 referenced by dynamic relocations. */
2972 unsigned int local_ifunc_resolver:1;
2973 unsigned int maybe_local_ifunc_resolver:1;
2974
2975 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
2976 unsigned int has_plt_localentry0:1;
2977
2978 /* Incremented every time we size stubs. */
2979 unsigned int stub_iteration;
2980
2981 /* Small local sym cache. */
2982 struct sym_cache sym_cache;
2983 };
2984
2985 /* Rename some of the generic section flags to better document how they
2986 are used here. */
2987
2988 /* Nonzero if this section has TLS related relocations. */
2989 #define has_tls_reloc sec_flg0
2990
2991 /* Nonzero if this section has an old-style call to __tls_get_addr. */
2992 #define has_tls_get_addr_call sec_flg1
2993
2994 /* Nonzero if this section has any toc or got relocs. */
2995 #define has_toc_reloc sec_flg2
2996
2997 /* Nonzero if this section has a call to another section that uses
2998 the toc or got. */
2999 #define makes_toc_func_call sec_flg3
3000
3001 /* Recursion protection when determining above flag. */
3002 #define call_check_in_progress sec_flg4
3003 #define call_check_done sec_flg5
3004
3005 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3006
3007 #define ppc_hash_table(p) \
3008 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3009 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3010
3011 #define ppc_stub_hash_lookup(table, string, create, copy) \
3012 ((struct ppc_stub_hash_entry *) \
3013 bfd_hash_lookup ((table), (string), (create), (copy)))
3014
3015 #define ppc_branch_hash_lookup(table, string, create, copy) \
3016 ((struct ppc_branch_hash_entry *) \
3017 bfd_hash_lookup ((table), (string), (create), (copy)))
3018
3019 /* Create an entry in the stub hash table. */
3020
3021 static struct bfd_hash_entry *
3022 stub_hash_newfunc (struct bfd_hash_entry *entry,
3023 struct bfd_hash_table *table,
3024 const char *string)
3025 {
3026 /* Allocate the structure if it has not already been allocated by a
3027 subclass. */
3028 if (entry == NULL)
3029 {
3030 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3031 if (entry == NULL)
3032 return entry;
3033 }
3034
3035 /* Call the allocation method of the superclass. */
3036 entry = bfd_hash_newfunc (entry, table, string);
3037 if (entry != NULL)
3038 {
3039 struct ppc_stub_hash_entry *eh;
3040
3041 /* Initialize the local fields. */
3042 eh = (struct ppc_stub_hash_entry *) entry;
3043 eh->stub_type = ppc_stub_none;
3044 eh->group = NULL;
3045 eh->stub_offset = 0;
3046 eh->target_value = 0;
3047 eh->target_section = NULL;
3048 eh->h = NULL;
3049 eh->plt_ent = NULL;
3050 eh->other = 0;
3051 }
3052
3053 return entry;
3054 }
3055
3056 /* Create an entry in the branch hash table. */
3057
3058 static struct bfd_hash_entry *
3059 branch_hash_newfunc (struct bfd_hash_entry *entry,
3060 struct bfd_hash_table *table,
3061 const char *string)
3062 {
3063 /* Allocate the structure if it has not already been allocated by a
3064 subclass. */
3065 if (entry == NULL)
3066 {
3067 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3068 if (entry == NULL)
3069 return entry;
3070 }
3071
3072 /* Call the allocation method of the superclass. */
3073 entry = bfd_hash_newfunc (entry, table, string);
3074 if (entry != NULL)
3075 {
3076 struct ppc_branch_hash_entry *eh;
3077
3078 /* Initialize the local fields. */
3079 eh = (struct ppc_branch_hash_entry *) entry;
3080 eh->offset = 0;
3081 eh->iter = 0;
3082 }
3083
3084 return entry;
3085 }
3086
3087 /* Create an entry in a ppc64 ELF linker hash table. */
3088
3089 static struct bfd_hash_entry *
3090 link_hash_newfunc (struct bfd_hash_entry *entry,
3091 struct bfd_hash_table *table,
3092 const char *string)
3093 {
3094 /* Allocate the structure if it has not already been allocated by a
3095 subclass. */
3096 if (entry == NULL)
3097 {
3098 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3099 if (entry == NULL)
3100 return entry;
3101 }
3102
3103 /* Call the allocation method of the superclass. */
3104 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3105 if (entry != NULL)
3106 {
3107 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3108
3109 memset (&eh->u.stub_cache, 0,
3110 (sizeof (struct ppc_link_hash_entry)
3111 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3112
3113 /* When making function calls, old ABI code references function entry
3114 points (dot symbols), while new ABI code references the function
3115 descriptor symbol. We need to make any combination of reference and
3116 definition work together, without breaking archive linking.
3117
3118 For a defined function "foo" and an undefined call to "bar":
3119 An old object defines "foo" and ".foo", references ".bar" (possibly
3120 "bar" too).
3121 A new object defines "foo" and references "bar".
3122
3123 A new object thus has no problem with its undefined symbols being
3124 satisfied by definitions in an old object. On the other hand, the
3125 old object won't have ".bar" satisfied by a new object.
3126
3127 Keep a list of newly added dot-symbols. */
3128
3129 if (string[0] == '.')
3130 {
3131 struct ppc_link_hash_table *htab;
3132
3133 htab = (struct ppc_link_hash_table *) table;
3134 eh->u.next_dot_sym = htab->dot_syms;
3135 htab->dot_syms = eh;
3136 }
3137 }
3138
3139 return entry;
3140 }
3141
3142 struct tocsave_entry
3143 {
3144 asection *sec;
3145 bfd_vma offset;
3146 };
3147
3148 static hashval_t
3149 tocsave_htab_hash (const void *p)
3150 {
3151 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3152 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3153 }
3154
3155 static int
3156 tocsave_htab_eq (const void *p1, const void *p2)
3157 {
3158 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3159 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3160 return e1->sec == e2->sec && e1->offset == e2->offset;
3161 }
3162
3163 /* Destroy a ppc64 ELF linker hash table. */
3164
3165 static void
3166 ppc64_elf_link_hash_table_free (bfd *obfd)
3167 {
3168 struct ppc_link_hash_table *htab;
3169
3170 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3171 if (htab->tocsave_htab)
3172 htab_delete (htab->tocsave_htab);
3173 bfd_hash_table_free (&htab->branch_hash_table);
3174 bfd_hash_table_free (&htab->stub_hash_table);
3175 _bfd_elf_link_hash_table_free (obfd);
3176 }
3177
3178 /* Create a ppc64 ELF linker hash table. */
3179
3180 static struct bfd_link_hash_table *
3181 ppc64_elf_link_hash_table_create (bfd *abfd)
3182 {
3183 struct ppc_link_hash_table *htab;
3184 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3185
3186 htab = bfd_zmalloc (amt);
3187 if (htab == NULL)
3188 return NULL;
3189
3190 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3191 sizeof (struct ppc_link_hash_entry),
3192 PPC64_ELF_DATA))
3193 {
3194 free (htab);
3195 return NULL;
3196 }
3197
3198 /* Init the stub hash table too. */
3199 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3200 sizeof (struct ppc_stub_hash_entry)))
3201 {
3202 _bfd_elf_link_hash_table_free (abfd);
3203 return NULL;
3204 }
3205
3206 /* And the branch hash table. */
3207 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3208 sizeof (struct ppc_branch_hash_entry)))
3209 {
3210 bfd_hash_table_free (&htab->stub_hash_table);
3211 _bfd_elf_link_hash_table_free (abfd);
3212 return NULL;
3213 }
3214
3215 htab->tocsave_htab = htab_try_create (1024,
3216 tocsave_htab_hash,
3217 tocsave_htab_eq,
3218 NULL);
3219 if (htab->tocsave_htab == NULL)
3220 {
3221 ppc64_elf_link_hash_table_free (abfd);
3222 return NULL;
3223 }
3224 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3225
3226 /* Initializing two fields of the union is just cosmetic. We really
3227 only care about glist, but when compiled on a 32-bit host the
3228 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3229 debugger inspection of these fields look nicer. */
3230 htab->elf.init_got_refcount.refcount = 0;
3231 htab->elf.init_got_refcount.glist = NULL;
3232 htab->elf.init_plt_refcount.refcount = 0;
3233 htab->elf.init_plt_refcount.glist = NULL;
3234 htab->elf.init_got_offset.offset = 0;
3235 htab->elf.init_got_offset.glist = NULL;
3236 htab->elf.init_plt_offset.offset = 0;
3237 htab->elf.init_plt_offset.glist = NULL;
3238
3239 return &htab->elf.root;
3240 }
3241
3242 /* Create sections for linker generated code. */
3243
3244 static bfd_boolean
3245 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3246 {
3247 struct ppc_link_hash_table *htab;
3248 flagword flags;
3249
3250 htab = ppc_hash_table (info);
3251
3252 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3253 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3254 if (htab->params->save_restore_funcs)
3255 {
3256 /* Create .sfpr for code to save and restore fp regs. */
3257 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3258 flags);
3259 if (htab->sfpr == NULL
3260 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3261 return FALSE;
3262 }
3263
3264 if (bfd_link_relocatable (info))
3265 return TRUE;
3266
3267 /* Create .glink for lazy dynamic linking support. */
3268 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3269 flags);
3270 if (htab->glink == NULL
3271 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
3272 return FALSE;
3273
3274 /* The part of .glink used by global entry stubs, separate so that
3275 it can be aligned appropriately without affecting htab->glink. */
3276 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3277 flags);
3278 if (htab->global_entry == NULL
3279 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
3280 return FALSE;
3281
3282 if (!info->no_ld_generated_unwind_info)
3283 {
3284 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3285 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3286 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3287 ".eh_frame",
3288 flags);
3289 if (htab->glink_eh_frame == NULL
3290 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3291 return FALSE;
3292 }
3293
3294 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3295 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3296 if (htab->elf.iplt == NULL
3297 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
3298 return FALSE;
3299
3300 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3301 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3302 htab->elf.irelplt
3303 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3304 if (htab->elf.irelplt == NULL
3305 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
3306 return FALSE;
3307
3308 /* Create branch lookup table for plt_branch stubs. */
3309 flags = (SEC_ALLOC | SEC_LOAD
3310 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3311 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3312 flags);
3313 if (htab->brlt == NULL
3314 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
3315 return FALSE;
3316
3317 /* Local plt entries, put in .branch_lt but a separate section for
3318 convenience. */
3319 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3320 flags);
3321 if (htab->pltlocal == NULL
3322 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
3323 return FALSE;
3324
3325 if (!bfd_link_pic (info))
3326 return TRUE;
3327
3328 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3329 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3330 htab->relbrlt
3331 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3332 if (htab->relbrlt == NULL
3333 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3334 return FALSE;
3335
3336 htab->relpltlocal
3337 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3338 if (htab->relpltlocal == NULL
3339 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
3340 return FALSE;
3341
3342 return TRUE;
3343 }
3344
3345 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3346
3347 bfd_boolean
3348 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3349 struct ppc64_elf_params *params)
3350 {
3351 struct ppc_link_hash_table *htab;
3352
3353 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3354
3355 /* Always hook our dynamic sections into the first bfd, which is the
3356 linker created stub bfd. This ensures that the GOT header is at
3357 the start of the output TOC section. */
3358 htab = ppc_hash_table (info);
3359 htab->elf.dynobj = params->stub_bfd;
3360 htab->params = params;
3361
3362 return create_linkage_sections (htab->elf.dynobj, info);
3363 }
3364
3365 /* Build a name for an entry in the stub hash table. */
3366
3367 static char *
3368 ppc_stub_name (const asection *input_section,
3369 const asection *sym_sec,
3370 const struct ppc_link_hash_entry *h,
3371 const Elf_Internal_Rela *rel)
3372 {
3373 char *stub_name;
3374 ssize_t len;
3375
3376 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3377 offsets from a sym as a branch target? In fact, we could
3378 probably assume the addend is always zero. */
3379 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3380
3381 if (h)
3382 {
3383 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3384 stub_name = bfd_malloc (len);
3385 if (stub_name == NULL)
3386 return stub_name;
3387
3388 len = sprintf (stub_name, "%08x.%s+%x",
3389 input_section->id & 0xffffffff,
3390 h->elf.root.root.string,
3391 (int) rel->r_addend & 0xffffffff);
3392 }
3393 else
3394 {
3395 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3396 stub_name = bfd_malloc (len);
3397 if (stub_name == NULL)
3398 return stub_name;
3399
3400 len = sprintf (stub_name, "%08x.%x:%x+%x",
3401 input_section->id & 0xffffffff,
3402 sym_sec->id & 0xffffffff,
3403 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3404 (int) rel->r_addend & 0xffffffff);
3405 }
3406 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3407 stub_name[len - 2] = 0;
3408 return stub_name;
3409 }
3410
3411 /* Look up an entry in the stub hash. Stub entries are cached because
3412 creating the stub name takes a bit of time. */
3413
3414 static struct ppc_stub_hash_entry *
3415 ppc_get_stub_entry (const asection *input_section,
3416 const asection *sym_sec,
3417 struct ppc_link_hash_entry *h,
3418 const Elf_Internal_Rela *rel,
3419 struct ppc_link_hash_table *htab)
3420 {
3421 struct ppc_stub_hash_entry *stub_entry;
3422 struct map_stub *group;
3423
3424 /* If this input section is part of a group of sections sharing one
3425 stub section, then use the id of the first section in the group.
3426 Stub names need to include a section id, as there may well be
3427 more than one stub used to reach say, printf, and we need to
3428 distinguish between them. */
3429 group = htab->sec_info[input_section->id].u.group;
3430 if (group == NULL)
3431 return NULL;
3432
3433 if (h != NULL && h->u.stub_cache != NULL
3434 && h->u.stub_cache->h == h
3435 && h->u.stub_cache->group == group)
3436 {
3437 stub_entry = h->u.stub_cache;
3438 }
3439 else
3440 {
3441 char *stub_name;
3442
3443 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3444 if (stub_name == NULL)
3445 return NULL;
3446
3447 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3448 stub_name, FALSE, FALSE);
3449 if (h != NULL)
3450 h->u.stub_cache = stub_entry;
3451
3452 free (stub_name);
3453 }
3454
3455 return stub_entry;
3456 }
3457
3458 /* Add a new stub entry to the stub hash. Not all fields of the new
3459 stub entry are initialised. */
3460
3461 static struct ppc_stub_hash_entry *
3462 ppc_add_stub (const char *stub_name,
3463 asection *section,
3464 struct bfd_link_info *info)
3465 {
3466 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3467 struct map_stub *group;
3468 asection *link_sec;
3469 asection *stub_sec;
3470 struct ppc_stub_hash_entry *stub_entry;
3471
3472 group = htab->sec_info[section->id].u.group;
3473 link_sec = group->link_sec;
3474 stub_sec = group->stub_sec;
3475 if (stub_sec == NULL)
3476 {
3477 size_t namelen;
3478 bfd_size_type len;
3479 char *s_name;
3480
3481 namelen = strlen (link_sec->name);
3482 len = namelen + sizeof (STUB_SUFFIX);
3483 s_name = bfd_alloc (htab->params->stub_bfd, len);
3484 if (s_name == NULL)
3485 return NULL;
3486
3487 memcpy (s_name, link_sec->name, namelen);
3488 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3489 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3490 if (stub_sec == NULL)
3491 return NULL;
3492 group->stub_sec = stub_sec;
3493 }
3494
3495 /* Enter this entry into the linker stub hash table. */
3496 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3497 TRUE, FALSE);
3498 if (stub_entry == NULL)
3499 {
3500 /* xgettext:c-format */
3501 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3502 section->owner, stub_name);
3503 return NULL;
3504 }
3505
3506 stub_entry->group = group;
3507 stub_entry->stub_offset = 0;
3508 return stub_entry;
3509 }
3510
3511 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3512 not already done. */
3513
3514 static bfd_boolean
3515 create_got_section (bfd *abfd, struct bfd_link_info *info)
3516 {
3517 asection *got, *relgot;
3518 flagword flags;
3519 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3520
3521 if (!is_ppc64_elf (abfd))
3522 return FALSE;
3523 if (htab == NULL)
3524 return FALSE;
3525
3526 if (!htab->elf.sgot
3527 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3528 return FALSE;
3529
3530 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3531 | SEC_LINKER_CREATED);
3532
3533 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3534 if (!got
3535 || !bfd_set_section_alignment (abfd, got, 3))
3536 return FALSE;
3537
3538 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3539 flags | SEC_READONLY);
3540 if (!relgot
3541 || !bfd_set_section_alignment (abfd, relgot, 3))
3542 return FALSE;
3543
3544 ppc64_elf_tdata (abfd)->got = got;
3545 ppc64_elf_tdata (abfd)->relgot = relgot;
3546 return TRUE;
3547 }
3548
3549 /* Follow indirect and warning symbol links. */
3550
3551 static inline struct bfd_link_hash_entry *
3552 follow_link (struct bfd_link_hash_entry *h)
3553 {
3554 while (h->type == bfd_link_hash_indirect
3555 || h->type == bfd_link_hash_warning)
3556 h = h->u.i.link;
3557 return h;
3558 }
3559
3560 static inline struct elf_link_hash_entry *
3561 elf_follow_link (struct elf_link_hash_entry *h)
3562 {
3563 return (struct elf_link_hash_entry *) follow_link (&h->root);
3564 }
3565
3566 static inline struct ppc_link_hash_entry *
3567 ppc_follow_link (struct ppc_link_hash_entry *h)
3568 {
3569 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3570 }
3571
3572 /* Merge PLT info on FROM with that on TO. */
3573
3574 static void
3575 move_plt_plist (struct ppc_link_hash_entry *from,
3576 struct ppc_link_hash_entry *to)
3577 {
3578 if (from->elf.plt.plist != NULL)
3579 {
3580 if (to->elf.plt.plist != NULL)
3581 {
3582 struct plt_entry **entp;
3583 struct plt_entry *ent;
3584
3585 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3586 {
3587 struct plt_entry *dent;
3588
3589 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3590 if (dent->addend == ent->addend)
3591 {
3592 dent->plt.refcount += ent->plt.refcount;
3593 *entp = ent->next;
3594 break;
3595 }
3596 if (dent == NULL)
3597 entp = &ent->next;
3598 }
3599 *entp = to->elf.plt.plist;
3600 }
3601
3602 to->elf.plt.plist = from->elf.plt.plist;
3603 from->elf.plt.plist = NULL;
3604 }
3605 }
3606
3607 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3608
3609 static void
3610 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3611 struct elf_link_hash_entry *dir,
3612 struct elf_link_hash_entry *ind)
3613 {
3614 struct ppc_link_hash_entry *edir, *eind;
3615
3616 edir = (struct ppc_link_hash_entry *) dir;
3617 eind = (struct ppc_link_hash_entry *) ind;
3618
3619 edir->is_func |= eind->is_func;
3620 edir->is_func_descriptor |= eind->is_func_descriptor;
3621 edir->tls_mask |= eind->tls_mask;
3622 if (eind->oh != NULL)
3623 edir->oh = ppc_follow_link (eind->oh);
3624
3625 if (edir->elf.versioned != versioned_hidden)
3626 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3627 edir->elf.ref_regular |= eind->elf.ref_regular;
3628 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3629 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3630 edir->elf.needs_plt |= eind->elf.needs_plt;
3631 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3632
3633 /* If we were called to copy over info for a weak sym, don't copy
3634 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3635 in order to simplify readonly_dynrelocs and save a field in the
3636 symbol hash entry, but that means dyn_relocs can't be used in any
3637 tests about a specific symbol, or affect other symbol flags which
3638 are then tested. */
3639 if (eind->elf.root.type != bfd_link_hash_indirect)
3640 return;
3641
3642 /* Copy over any dynamic relocs we may have on the indirect sym. */
3643 if (eind->dyn_relocs != NULL)
3644 {
3645 if (edir->dyn_relocs != NULL)
3646 {
3647 struct elf_dyn_relocs **pp;
3648 struct elf_dyn_relocs *p;
3649
3650 /* Add reloc counts against the indirect sym to the direct sym
3651 list. Merge any entries against the same section. */
3652 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3653 {
3654 struct elf_dyn_relocs *q;
3655
3656 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3657 if (q->sec == p->sec)
3658 {
3659 q->pc_count += p->pc_count;
3660 q->count += p->count;
3661 *pp = p->next;
3662 break;
3663 }
3664 if (q == NULL)
3665 pp = &p->next;
3666 }
3667 *pp = edir->dyn_relocs;
3668 }
3669
3670 edir->dyn_relocs = eind->dyn_relocs;
3671 eind->dyn_relocs = NULL;
3672 }
3673
3674 /* Copy over got entries that we may have already seen to the
3675 symbol which just became indirect. */
3676 if (eind->elf.got.glist != NULL)
3677 {
3678 if (edir->elf.got.glist != NULL)
3679 {
3680 struct got_entry **entp;
3681 struct got_entry *ent;
3682
3683 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3684 {
3685 struct got_entry *dent;
3686
3687 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3688 if (dent->addend == ent->addend
3689 && dent->owner == ent->owner
3690 && dent->tls_type == ent->tls_type)
3691 {
3692 dent->got.refcount += ent->got.refcount;
3693 *entp = ent->next;
3694 break;
3695 }
3696 if (dent == NULL)
3697 entp = &ent->next;
3698 }
3699 *entp = edir->elf.got.glist;
3700 }
3701
3702 edir->elf.got.glist = eind->elf.got.glist;
3703 eind->elf.got.glist = NULL;
3704 }
3705
3706 /* And plt entries. */
3707 move_plt_plist (eind, edir);
3708
3709 if (eind->elf.dynindx != -1)
3710 {
3711 if (edir->elf.dynindx != -1)
3712 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3713 edir->elf.dynstr_index);
3714 edir->elf.dynindx = eind->elf.dynindx;
3715 edir->elf.dynstr_index = eind->elf.dynstr_index;
3716 eind->elf.dynindx = -1;
3717 eind->elf.dynstr_index = 0;
3718 }
3719 }
3720
3721 /* Find the function descriptor hash entry from the given function code
3722 hash entry FH. Link the entries via their OH fields. */
3723
3724 static struct ppc_link_hash_entry *
3725 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3726 {
3727 struct ppc_link_hash_entry *fdh = fh->oh;
3728
3729 if (fdh == NULL)
3730 {
3731 const char *fd_name = fh->elf.root.root.string + 1;
3732
3733 fdh = (struct ppc_link_hash_entry *)
3734 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3735 if (fdh == NULL)
3736 return fdh;
3737
3738 fdh->is_func_descriptor = 1;
3739 fdh->oh = fh;
3740 fh->is_func = 1;
3741 fh->oh = fdh;
3742 }
3743
3744 fdh = ppc_follow_link (fdh);
3745 fdh->is_func_descriptor = 1;
3746 fdh->oh = fh;
3747 return fdh;
3748 }
3749
3750 /* Make a fake function descriptor sym for the undefined code sym FH. */
3751
3752 static struct ppc_link_hash_entry *
3753 make_fdh (struct bfd_link_info *info,
3754 struct ppc_link_hash_entry *fh)
3755 {
3756 bfd *abfd = fh->elf.root.u.undef.abfd;
3757 struct bfd_link_hash_entry *bh = NULL;
3758 struct ppc_link_hash_entry *fdh;
3759 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3760 ? BSF_WEAK
3761 : BSF_GLOBAL);
3762
3763 if (!_bfd_generic_link_add_one_symbol (info, abfd,
3764 fh->elf.root.root.string + 1,
3765 flags, bfd_und_section_ptr, 0,
3766 NULL, FALSE, FALSE, &bh))
3767 return NULL;
3768
3769 fdh = (struct ppc_link_hash_entry *) bh;
3770 fdh->elf.non_elf = 0;
3771 fdh->fake = 1;
3772 fdh->is_func_descriptor = 1;
3773 fdh->oh = fh;
3774 fh->is_func = 1;
3775 fh->oh = fdh;
3776 return fdh;
3777 }
3778
3779 /* Fix function descriptor symbols defined in .opd sections to be
3780 function type. */
3781
3782 static bfd_boolean
3783 ppc64_elf_add_symbol_hook (bfd *ibfd,
3784 struct bfd_link_info *info,
3785 Elf_Internal_Sym *isym,
3786 const char **name,
3787 flagword *flags ATTRIBUTE_UNUSED,
3788 asection **sec,
3789 bfd_vma *value)
3790 {
3791 if (*sec != NULL
3792 && strcmp ((*sec)->name, ".opd") == 0)
3793 {
3794 asection *code_sec;
3795
3796 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3797 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
3798 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
3799
3800 /* If the symbol is a function defined in .opd, and the function
3801 code is in a discarded group, let it appear to be undefined. */
3802 if (!bfd_link_relocatable (info)
3803 && (*sec)->reloc_count != 0
3804 && opd_entry_value (*sec, *value, &code_sec, NULL,
3805 FALSE) != (bfd_vma) -1
3806 && discarded_section (code_sec))
3807 {
3808 *sec = bfd_und_section_ptr;
3809 isym->st_shndx = SHN_UNDEF;
3810 }
3811 }
3812 else if (*sec != NULL
3813 && strcmp ((*sec)->name, ".toc") == 0
3814 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
3815 {
3816 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3817 if (htab != NULL)
3818 htab->params->object_in_toc = 1;
3819 }
3820
3821 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3822 {
3823 if (abiversion (ibfd) == 0)
3824 set_abiversion (ibfd, 2);
3825 else if (abiversion (ibfd) == 1)
3826 {
3827 _bfd_error_handler (_("symbol '%s' has invalid st_other"
3828 " for ABI version 1"), *name);
3829 bfd_set_error (bfd_error_bad_value);
3830 return FALSE;
3831 }
3832 }
3833
3834 return TRUE;
3835 }
3836
3837 /* Merge non-visibility st_other attributes: local entry point. */
3838
3839 static void
3840 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
3841 const Elf_Internal_Sym *isym,
3842 bfd_boolean definition,
3843 bfd_boolean dynamic)
3844 {
3845 if (definition && (!dynamic || !h->def_regular))
3846 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
3847 | ELF_ST_VISIBILITY (h->other));
3848 }
3849
3850 /* Hook called on merging a symbol. We use this to clear "fake" since
3851 we now have a real symbol. */
3852
3853 static bfd_boolean
3854 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
3855 const Elf_Internal_Sym *isym,
3856 asection **psec ATTRIBUTE_UNUSED,
3857 bfd_boolean newdef ATTRIBUTE_UNUSED,
3858 bfd_boolean olddef ATTRIBUTE_UNUSED,
3859 bfd *oldbfd ATTRIBUTE_UNUSED,
3860 const asection *oldsec ATTRIBUTE_UNUSED)
3861 {
3862 ((struct ppc_link_hash_entry *) h)->fake = 0;
3863 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3864 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
3865 return TRUE;
3866 }
3867
3868 /* This function makes an old ABI object reference to ".bar" cause the
3869 inclusion of a new ABI object archive that defines "bar".
3870 NAME is a symbol defined in an archive. Return a symbol in the hash
3871 table that might be satisfied by the archive symbols. */
3872
3873 static struct elf_link_hash_entry *
3874 ppc64_elf_archive_symbol_lookup (bfd *abfd,
3875 struct bfd_link_info *info,
3876 const char *name)
3877 {
3878 struct elf_link_hash_entry *h;
3879 char *dot_name;
3880 size_t len;
3881
3882 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
3883 if (h != NULL
3884 /* Don't return this sym if it is a fake function descriptor
3885 created by add_symbol_adjust. */
3886 && !((struct ppc_link_hash_entry *) h)->fake)
3887 return h;
3888
3889 if (name[0] == '.')
3890 return h;
3891
3892 len = strlen (name);
3893 dot_name = bfd_alloc (abfd, len + 2);
3894 if (dot_name == NULL)
3895 return (struct elf_link_hash_entry *) -1;
3896 dot_name[0] = '.';
3897 memcpy (dot_name + 1, name, len + 1);
3898 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
3899 bfd_release (abfd, dot_name);
3900 return h;
3901 }
3902
3903 /* This function satisfies all old ABI object references to ".bar" if a
3904 new ABI object defines "bar". Well, at least, undefined dot symbols
3905 are made weak. This stops later archive searches from including an
3906 object if we already have a function descriptor definition. It also
3907 prevents the linker complaining about undefined symbols.
3908 We also check and correct mismatched symbol visibility here. The
3909 most restrictive visibility of the function descriptor and the
3910 function entry symbol is used. */
3911
3912 static bfd_boolean
3913 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
3914 {
3915 struct ppc_link_hash_table *htab;
3916 struct ppc_link_hash_entry *fdh;
3917
3918 if (eh->elf.root.type == bfd_link_hash_warning)
3919 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
3920
3921 if (eh->elf.root.type == bfd_link_hash_indirect)
3922 return TRUE;
3923
3924 if (eh->elf.root.root.string[0] != '.')
3925 abort ();
3926
3927 htab = ppc_hash_table (info);
3928 if (htab == NULL)
3929 return FALSE;
3930
3931 fdh = lookup_fdh (eh, htab);
3932 if (fdh == NULL
3933 && !bfd_link_relocatable (info)
3934 && (eh->elf.root.type == bfd_link_hash_undefined
3935 || eh->elf.root.type == bfd_link_hash_undefweak)
3936 && eh->elf.ref_regular)
3937 {
3938 /* Make an undefined function descriptor sym, in order to
3939 pull in an --as-needed shared lib. Archives are handled
3940 elsewhere. */
3941 fdh = make_fdh (info, eh);
3942 if (fdh == NULL)
3943 return FALSE;
3944 }
3945
3946 if (fdh != NULL)
3947 {
3948 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
3949 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
3950
3951 /* Make both descriptor and entry symbol have the most
3952 constraining visibility of either symbol. */
3953 if (entry_vis < descr_vis)
3954 fdh->elf.other += entry_vis - descr_vis;
3955 else if (entry_vis > descr_vis)
3956 eh->elf.other += descr_vis - entry_vis;
3957
3958 /* Propagate reference flags from entry symbol to function
3959 descriptor symbol. */
3960 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
3961 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
3962 fdh->elf.ref_regular |= eh->elf.ref_regular;
3963 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
3964
3965 if (!fdh->elf.forced_local
3966 && fdh->elf.dynindx == -1
3967 && fdh->elf.versioned != versioned_hidden
3968 && (bfd_link_dll (info)
3969 || fdh->elf.def_dynamic
3970 || fdh->elf.ref_dynamic)
3971 && (eh->elf.ref_regular
3972 || eh->elf.def_regular))
3973 {
3974 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
3975 return FALSE;
3976 }
3977 }
3978
3979 return TRUE;
3980 }
3981
3982 /* Set up opd section info and abiversion for IBFD, and process list
3983 of dot-symbols we made in link_hash_newfunc. */
3984
3985 static bfd_boolean
3986 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
3987 {
3988 struct ppc_link_hash_table *htab;
3989 struct ppc_link_hash_entry **p, *eh;
3990 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
3991
3992 if (opd != NULL && opd->size != 0)
3993 {
3994 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
3995 ppc64_elf_section_data (opd)->sec_type = sec_opd;
3996
3997 if (abiversion (ibfd) == 0)
3998 set_abiversion (ibfd, 1);
3999 else if (abiversion (ibfd) >= 2)
4000 {
4001 /* xgettext:c-format */
4002 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4003 ibfd, abiversion (ibfd));
4004 bfd_set_error (bfd_error_bad_value);
4005 return FALSE;
4006 }
4007 }
4008
4009 if (is_ppc64_elf (info->output_bfd))
4010 {
4011 /* For input files without an explicit abiversion in e_flags
4012 we should have flagged any with symbol st_other bits set
4013 as ELFv1 and above flagged those with .opd as ELFv2.
4014 Set the output abiversion if not yet set, and for any input
4015 still ambiguous, take its abiversion from the output.
4016 Differences in ABI are reported later. */
4017 if (abiversion (info->output_bfd) == 0)
4018 set_abiversion (info->output_bfd, abiversion (ibfd));
4019 else if (abiversion (ibfd) == 0)
4020 set_abiversion (ibfd, abiversion (info->output_bfd));
4021 }
4022
4023 htab = ppc_hash_table (info);
4024 if (htab == NULL)
4025 return TRUE;
4026
4027 if (opd != NULL && opd->size != 0
4028 && (ibfd->flags & DYNAMIC) == 0
4029 && (opd->flags & SEC_RELOC) != 0
4030 && opd->reloc_count != 0
4031 && !bfd_is_abs_section (opd->output_section)
4032 && info->gc_sections)
4033 {
4034 /* Garbage collection needs some extra help with .opd sections.
4035 We don't want to necessarily keep everything referenced by
4036 relocs in .opd, as that would keep all functions. Instead,
4037 if we reference an .opd symbol (a function descriptor), we
4038 want to keep the function code symbol's section. This is
4039 easy for global symbols, but for local syms we need to keep
4040 information about the associated function section. */
4041 bfd_size_type amt;
4042 asection **opd_sym_map;
4043 Elf_Internal_Shdr *symtab_hdr;
4044 Elf_Internal_Rela *relocs, *rel_end, *rel;
4045
4046 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4047 opd_sym_map = bfd_zalloc (ibfd, amt);
4048 if (opd_sym_map == NULL)
4049 return FALSE;
4050 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4051 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4052 info->keep_memory);
4053 if (relocs == NULL)
4054 return FALSE;
4055 symtab_hdr = &elf_symtab_hdr (ibfd);
4056 rel_end = relocs + opd->reloc_count - 1;
4057 for (rel = relocs; rel < rel_end; rel++)
4058 {
4059 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4060 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4061
4062 if (r_type == R_PPC64_ADDR64
4063 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4064 && r_symndx < symtab_hdr->sh_info)
4065 {
4066 Elf_Internal_Sym *isym;
4067 asection *s;
4068
4069 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4070 if (isym == NULL)
4071 {
4072 if (elf_section_data (opd)->relocs != relocs)
4073 free (relocs);
4074 return FALSE;
4075 }
4076
4077 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4078 if (s != NULL && s != opd)
4079 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4080 }
4081 }
4082 if (elf_section_data (opd)->relocs != relocs)
4083 free (relocs);
4084 }
4085
4086 p = &htab->dot_syms;
4087 while ((eh = *p) != NULL)
4088 {
4089 *p = NULL;
4090 if (&eh->elf == htab->elf.hgot)
4091 ;
4092 else if (htab->elf.hgot == NULL
4093 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4094 htab->elf.hgot = &eh->elf;
4095 else if (abiversion (ibfd) <= 1)
4096 {
4097 htab->need_func_desc_adj = 1;
4098 if (!add_symbol_adjust (eh, info))
4099 return FALSE;
4100 }
4101 p = &eh->u.next_dot_sym;
4102 }
4103 return TRUE;
4104 }
4105
4106 /* Undo hash table changes when an --as-needed input file is determined
4107 not to be needed. */
4108
4109 static bfd_boolean
4110 ppc64_elf_notice_as_needed (bfd *ibfd,
4111 struct bfd_link_info *info,
4112 enum notice_asneeded_action act)
4113 {
4114 if (act == notice_not_needed)
4115 {
4116 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4117
4118 if (htab == NULL)
4119 return FALSE;
4120
4121 htab->dot_syms = NULL;
4122 }
4123 return _bfd_elf_notice_as_needed (ibfd, info, act);
4124 }
4125
4126 /* If --just-symbols against a final linked binary, then assume we need
4127 toc adjusting stubs when calling functions defined there. */
4128
4129 static void
4130 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4131 {
4132 if ((sec->flags & SEC_CODE) != 0
4133 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4134 && is_ppc64_elf (sec->owner))
4135 {
4136 if (abiversion (sec->owner) >= 2
4137 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4138 sec->has_toc_reloc = 1;
4139 }
4140 _bfd_elf_link_just_syms (sec, info);
4141 }
4142
4143 static struct plt_entry **
4144 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4145 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4146 {
4147 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4148 struct plt_entry **local_plt;
4149 unsigned char *local_got_tls_masks;
4150
4151 if (local_got_ents == NULL)
4152 {
4153 bfd_size_type size = symtab_hdr->sh_info;
4154
4155 size *= (sizeof (*local_got_ents)
4156 + sizeof (*local_plt)
4157 + sizeof (*local_got_tls_masks));
4158 local_got_ents = bfd_zalloc (abfd, size);
4159 if (local_got_ents == NULL)
4160 return NULL;
4161 elf_local_got_ents (abfd) = local_got_ents;
4162 }
4163
4164 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4165 {
4166 struct got_entry *ent;
4167
4168 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4169 if (ent->addend == r_addend
4170 && ent->owner == abfd
4171 && ent->tls_type == tls_type)
4172 break;
4173 if (ent == NULL)
4174 {
4175 bfd_size_type amt = sizeof (*ent);
4176 ent = bfd_alloc (abfd, amt);
4177 if (ent == NULL)
4178 return FALSE;
4179 ent->next = local_got_ents[r_symndx];
4180 ent->addend = r_addend;
4181 ent->owner = abfd;
4182 ent->tls_type = tls_type;
4183 ent->is_indirect = FALSE;
4184 ent->got.refcount = 0;
4185 local_got_ents[r_symndx] = ent;
4186 }
4187 ent->got.refcount += 1;
4188 }
4189
4190 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4191 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4192 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4193
4194 return local_plt + r_symndx;
4195 }
4196
4197 static bfd_boolean
4198 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4199 {
4200 struct plt_entry *ent;
4201
4202 for (ent = *plist; ent != NULL; ent = ent->next)
4203 if (ent->addend == addend)
4204 break;
4205 if (ent == NULL)
4206 {
4207 bfd_size_type amt = sizeof (*ent);
4208 ent = bfd_alloc (abfd, amt);
4209 if (ent == NULL)
4210 return FALSE;
4211 ent->next = *plist;
4212 ent->addend = addend;
4213 ent->plt.refcount = 0;
4214 *plist = ent;
4215 }
4216 ent->plt.refcount += 1;
4217 return TRUE;
4218 }
4219
4220 static bfd_boolean
4221 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4222 {
4223 return (r_type == R_PPC64_REL24
4224 || r_type == R_PPC64_REL24_NOTOC
4225 || r_type == R_PPC64_REL14
4226 || r_type == R_PPC64_REL14_BRTAKEN
4227 || r_type == R_PPC64_REL14_BRNTAKEN
4228 || r_type == R_PPC64_ADDR24
4229 || r_type == R_PPC64_ADDR14
4230 || r_type == R_PPC64_ADDR14_BRTAKEN
4231 || r_type == R_PPC64_ADDR14_BRNTAKEN
4232 || r_type == R_PPC64_PLTCALL);
4233 }
4234
4235 /* Relocs on inline plt call sequence insns prior to the call. */
4236
4237 static bfd_boolean
4238 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4239 {
4240 return (r_type == R_PPC64_PLT16_HA
4241 || r_type == R_PPC64_PLT16_HI
4242 || r_type == R_PPC64_PLT16_LO
4243 || r_type == R_PPC64_PLT16_LO_DS
4244 || r_type == R_PPC64_PLTSEQ);
4245 }
4246
4247 /* Look through the relocs for a section during the first phase, and
4248 calculate needed space in the global offset table, procedure
4249 linkage table, and dynamic reloc sections. */
4250
4251 static bfd_boolean
4252 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4253 asection *sec, const Elf_Internal_Rela *relocs)
4254 {
4255 struct ppc_link_hash_table *htab;
4256 Elf_Internal_Shdr *symtab_hdr;
4257 struct elf_link_hash_entry **sym_hashes;
4258 const Elf_Internal_Rela *rel;
4259 const Elf_Internal_Rela *rel_end;
4260 asection *sreloc;
4261 struct elf_link_hash_entry *tga, *dottga;
4262 bfd_boolean is_opd;
4263
4264 if (bfd_link_relocatable (info))
4265 return TRUE;
4266
4267 /* Don't do anything special with non-loaded, non-alloced sections.
4268 In particular, any relocs in such sections should not affect GOT
4269 and PLT reference counting (ie. we don't allow them to create GOT
4270 or PLT entries), there's no possibility or desire to optimize TLS
4271 relocs, and there's not much point in propagating relocs to shared
4272 libs that the dynamic linker won't relocate. */
4273 if ((sec->flags & SEC_ALLOC) == 0)
4274 return TRUE;
4275
4276 BFD_ASSERT (is_ppc64_elf (abfd));
4277
4278 htab = ppc_hash_table (info);
4279 if (htab == NULL)
4280 return FALSE;
4281
4282 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4283 FALSE, FALSE, TRUE);
4284 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4285 FALSE, FALSE, TRUE);
4286 symtab_hdr = &elf_symtab_hdr (abfd);
4287 sym_hashes = elf_sym_hashes (abfd);
4288 sreloc = NULL;
4289 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4290 rel_end = relocs + sec->reloc_count;
4291 for (rel = relocs; rel < rel_end; rel++)
4292 {
4293 unsigned long r_symndx;
4294 struct elf_link_hash_entry *h;
4295 enum elf_ppc64_reloc_type r_type;
4296 int tls_type;
4297 struct _ppc64_elf_section_data *ppc64_sec;
4298 struct plt_entry **ifunc, **plt_list;
4299
4300 r_symndx = ELF64_R_SYM (rel->r_info);
4301 if (r_symndx < symtab_hdr->sh_info)
4302 h = NULL;
4303 else
4304 {
4305 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4306 h = elf_follow_link (h);
4307
4308 if (h == htab->elf.hgot)
4309 sec->has_toc_reloc = 1;
4310 }
4311
4312 tls_type = 0;
4313 ifunc = NULL;
4314 if (h != NULL)
4315 {
4316 if (h->type == STT_GNU_IFUNC)
4317 {
4318 h->needs_plt = 1;
4319 ifunc = &h->plt.plist;
4320 }
4321 }
4322 else
4323 {
4324 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4325 abfd, r_symndx);
4326 if (isym == NULL)
4327 return FALSE;
4328
4329 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4330 {
4331 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4332 rel->r_addend,
4333 NON_GOT | PLT_IFUNC);
4334 if (ifunc == NULL)
4335 return FALSE;
4336 }
4337 }
4338
4339 r_type = ELF64_R_TYPE (rel->r_info);
4340 switch (r_type)
4341 {
4342 case R_PPC64_TLSGD:
4343 case R_PPC64_TLSLD:
4344 /* These special tls relocs tie a call to __tls_get_addr with
4345 its parameter symbol. */
4346 if (h != NULL)
4347 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4348 else
4349 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4350 rel->r_addend,
4351 NON_GOT | TLS_TLS | TLS_MARK))
4352 return FALSE;
4353 sec->has_tls_reloc = 1;
4354 break;
4355
4356 case R_PPC64_GOT_TLSLD16:
4357 case R_PPC64_GOT_TLSLD16_LO:
4358 case R_PPC64_GOT_TLSLD16_HI:
4359 case R_PPC64_GOT_TLSLD16_HA:
4360 tls_type = TLS_TLS | TLS_LD;
4361 goto dogottls;
4362
4363 case R_PPC64_GOT_TLSGD16:
4364 case R_PPC64_GOT_TLSGD16_LO:
4365 case R_PPC64_GOT_TLSGD16_HI:
4366 case R_PPC64_GOT_TLSGD16_HA:
4367 tls_type = TLS_TLS | TLS_GD;
4368 goto dogottls;
4369
4370 case R_PPC64_GOT_TPREL16_DS:
4371 case R_PPC64_GOT_TPREL16_LO_DS:
4372 case R_PPC64_GOT_TPREL16_HI:
4373 case R_PPC64_GOT_TPREL16_HA:
4374 if (bfd_link_dll (info))
4375 info->flags |= DF_STATIC_TLS;
4376 tls_type = TLS_TLS | TLS_TPREL;
4377 goto dogottls;
4378
4379 case R_PPC64_GOT_DTPREL16_DS:
4380 case R_PPC64_GOT_DTPREL16_LO_DS:
4381 case R_PPC64_GOT_DTPREL16_HI:
4382 case R_PPC64_GOT_DTPREL16_HA:
4383 tls_type = TLS_TLS | TLS_DTPREL;
4384 dogottls:
4385 sec->has_tls_reloc = 1;
4386 /* Fall through */
4387
4388 case R_PPC64_GOT16:
4389 case R_PPC64_GOT16_DS:
4390 case R_PPC64_GOT16_HA:
4391 case R_PPC64_GOT16_HI:
4392 case R_PPC64_GOT16_LO:
4393 case R_PPC64_GOT16_LO_DS:
4394 /* This symbol requires a global offset table entry. */
4395 sec->has_toc_reloc = 1;
4396 if (r_type == R_PPC64_GOT_TLSLD16
4397 || r_type == R_PPC64_GOT_TLSGD16
4398 || r_type == R_PPC64_GOT_TPREL16_DS
4399 || r_type == R_PPC64_GOT_DTPREL16_DS
4400 || r_type == R_PPC64_GOT16
4401 || r_type == R_PPC64_GOT16_DS)
4402 {
4403 htab->do_multi_toc = 1;
4404 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4405 }
4406
4407 if (ppc64_elf_tdata (abfd)->got == NULL
4408 && !create_got_section (abfd, info))
4409 return FALSE;
4410
4411 if (h != NULL)
4412 {
4413 struct ppc_link_hash_entry *eh;
4414 struct got_entry *ent;
4415
4416 eh = (struct ppc_link_hash_entry *) h;
4417 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4418 if (ent->addend == rel->r_addend
4419 && ent->owner == abfd
4420 && ent->tls_type == tls_type)
4421 break;
4422 if (ent == NULL)
4423 {
4424 bfd_size_type amt = sizeof (*ent);
4425 ent = bfd_alloc (abfd, amt);
4426 if (ent == NULL)
4427 return FALSE;
4428 ent->next = eh->elf.got.glist;
4429 ent->addend = rel->r_addend;
4430 ent->owner = abfd;
4431 ent->tls_type = tls_type;
4432 ent->is_indirect = FALSE;
4433 ent->got.refcount = 0;
4434 eh->elf.got.glist = ent;
4435 }
4436 ent->got.refcount += 1;
4437 eh->tls_mask |= tls_type;
4438 }
4439 else
4440 /* This is a global offset table entry for a local symbol. */
4441 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4442 rel->r_addend, tls_type))
4443 return FALSE;
4444
4445 /* We may also need a plt entry if the symbol turns out to be
4446 an ifunc. */
4447 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
4448 {
4449 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4450 return FALSE;
4451 }
4452 break;
4453
4454 case R_PPC64_PLT16_HA:
4455 case R_PPC64_PLT16_HI:
4456 case R_PPC64_PLT16_LO:
4457 case R_PPC64_PLT16_LO_DS:
4458 case R_PPC64_PLT32:
4459 case R_PPC64_PLT64:
4460 /* This symbol requires a procedure linkage table entry. */
4461 plt_list = ifunc;
4462 if (h != NULL)
4463 {
4464 h->needs_plt = 1;
4465 if (h->root.root.string[0] == '.'
4466 && h->root.root.string[1] != '\0')
4467 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4468 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
4469 plt_list = &h->plt.plist;
4470 }
4471 if (plt_list == NULL)
4472 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4473 rel->r_addend,
4474 NON_GOT | PLT_KEEP);
4475 if (!update_plt_info (abfd, plt_list, rel->r_addend))
4476 return FALSE;
4477 break;
4478
4479 /* The following relocations don't need to propagate the
4480 relocation if linking a shared object since they are
4481 section relative. */
4482 case R_PPC64_SECTOFF:
4483 case R_PPC64_SECTOFF_LO:
4484 case R_PPC64_SECTOFF_HI:
4485 case R_PPC64_SECTOFF_HA:
4486 case R_PPC64_SECTOFF_DS:
4487 case R_PPC64_SECTOFF_LO_DS:
4488 case R_PPC64_DTPREL16:
4489 case R_PPC64_DTPREL16_LO:
4490 case R_PPC64_DTPREL16_HI:
4491 case R_PPC64_DTPREL16_HA:
4492 case R_PPC64_DTPREL16_DS:
4493 case R_PPC64_DTPREL16_LO_DS:
4494 case R_PPC64_DTPREL16_HIGH:
4495 case R_PPC64_DTPREL16_HIGHA:
4496 case R_PPC64_DTPREL16_HIGHER:
4497 case R_PPC64_DTPREL16_HIGHERA:
4498 case R_PPC64_DTPREL16_HIGHEST:
4499 case R_PPC64_DTPREL16_HIGHESTA:
4500 break;
4501
4502 /* Nor do these. */
4503 case R_PPC64_REL16:
4504 case R_PPC64_REL16_LO:
4505 case R_PPC64_REL16_HI:
4506 case R_PPC64_REL16_HA:
4507 case R_PPC64_REL16_HIGH:
4508 case R_PPC64_REL16_HIGHA:
4509 case R_PPC64_REL16_HIGHER:
4510 case R_PPC64_REL16_HIGHERA:
4511 case R_PPC64_REL16_HIGHEST:
4512 case R_PPC64_REL16_HIGHESTA:
4513 case R_PPC64_REL16DX_HA:
4514 break;
4515
4516 /* Not supported as a dynamic relocation. */
4517 case R_PPC64_ADDR64_LOCAL:
4518 if (bfd_link_pic (info))
4519 {
4520 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4521 ppc_howto_init ();
4522 /* xgettext:c-format */
4523 info->callbacks->einfo (_("%H: %s reloc unsupported "
4524 "in shared libraries and PIEs\n"),
4525 abfd, sec, rel->r_offset,
4526 ppc64_elf_howto_table[r_type]->name);
4527 bfd_set_error (bfd_error_bad_value);
4528 return FALSE;
4529 }
4530 break;
4531
4532 case R_PPC64_TOC16:
4533 case R_PPC64_TOC16_DS:
4534 htab->do_multi_toc = 1;
4535 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4536 /* Fall through. */
4537 case R_PPC64_TOC16_LO:
4538 case R_PPC64_TOC16_HI:
4539 case R_PPC64_TOC16_HA:
4540 case R_PPC64_TOC16_LO_DS:
4541 sec->has_toc_reloc = 1;
4542 break;
4543
4544 /* Marker reloc. */
4545 case R_PPC64_ENTRY:
4546 break;
4547
4548 /* This relocation describes the C++ object vtable hierarchy.
4549 Reconstruct it for later use during GC. */
4550 case R_PPC64_GNU_VTINHERIT:
4551 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4552 return FALSE;
4553 break;
4554
4555 /* This relocation describes which C++ vtable entries are actually
4556 used. Record for later use during GC. */
4557 case R_PPC64_GNU_VTENTRY:
4558 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4559 return FALSE;
4560 break;
4561
4562 case R_PPC64_REL14:
4563 case R_PPC64_REL14_BRTAKEN:
4564 case R_PPC64_REL14_BRNTAKEN:
4565 {
4566 asection *dest = NULL;
4567
4568 /* Heuristic: If jumping outside our section, chances are
4569 we are going to need a stub. */
4570 if (h != NULL)
4571 {
4572 /* If the sym is weak it may be overridden later, so
4573 don't assume we know where a weak sym lives. */
4574 if (h->root.type == bfd_link_hash_defined)
4575 dest = h->root.u.def.section;
4576 }
4577 else
4578 {
4579 Elf_Internal_Sym *isym;
4580
4581 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4582 abfd, r_symndx);
4583 if (isym == NULL)
4584 return FALSE;
4585
4586 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4587 }
4588
4589 if (dest != sec)
4590 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4591 }
4592 goto rel24;
4593
4594 case R_PPC64_PLTCALL:
4595 ppc64_elf_section_data (sec)->has_pltcall = 1;
4596 /* Fall through. */
4597
4598 case R_PPC64_REL24:
4599 case R_PPC64_REL24_NOTOC:
4600 rel24:
4601 plt_list = ifunc;
4602 if (h != NULL)
4603 {
4604 h->needs_plt = 1;
4605 if (h->root.root.string[0] == '.'
4606 && h->root.root.string[1] != '\0')
4607 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4608
4609 if (h == tga || h == dottga)
4610 {
4611 sec->has_tls_reloc = 1;
4612 if (rel != relocs
4613 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4614 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4615 /* We have a new-style __tls_get_addr call with
4616 a marker reloc. */
4617 ;
4618 else
4619 /* Mark this section as having an old-style call. */
4620 sec->has_tls_get_addr_call = 1;
4621 }
4622 plt_list = &h->plt.plist;
4623 }
4624
4625 /* We may need a .plt entry if the function this reloc
4626 refers to is in a shared lib. */
4627 if (plt_list
4628 && !update_plt_info (abfd, plt_list, rel->r_addend))
4629 return FALSE;
4630 break;
4631
4632 case R_PPC64_ADDR14:
4633 case R_PPC64_ADDR14_BRNTAKEN:
4634 case R_PPC64_ADDR14_BRTAKEN:
4635 case R_PPC64_ADDR24:
4636 goto dodyn;
4637
4638 case R_PPC64_TPREL64:
4639 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4640 if (bfd_link_dll (info))
4641 info->flags |= DF_STATIC_TLS;
4642 goto dotlstoc;
4643
4644 case R_PPC64_DTPMOD64:
4645 if (rel + 1 < rel_end
4646 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4647 && rel[1].r_offset == rel->r_offset + 8)
4648 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4649 else
4650 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4651 goto dotlstoc;
4652
4653 case R_PPC64_DTPREL64:
4654 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4655 if (rel != relocs
4656 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4657 && rel[-1].r_offset == rel->r_offset - 8)
4658 /* This is the second reloc of a dtpmod, dtprel pair.
4659 Don't mark with TLS_DTPREL. */
4660 goto dodyn;
4661
4662 dotlstoc:
4663 sec->has_tls_reloc = 1;
4664 if (h != NULL)
4665 {
4666 struct ppc_link_hash_entry *eh;
4667 eh = (struct ppc_link_hash_entry *) h;
4668 eh->tls_mask |= tls_type;
4669 }
4670 else
4671 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4672 rel->r_addend, tls_type))
4673 return FALSE;
4674
4675 ppc64_sec = ppc64_elf_section_data (sec);
4676 if (ppc64_sec->sec_type != sec_toc)
4677 {
4678 bfd_size_type amt;
4679
4680 /* One extra to simplify get_tls_mask. */
4681 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4682 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4683 if (ppc64_sec->u.toc.symndx == NULL)
4684 return FALSE;
4685 amt = sec->size * sizeof (bfd_vma) / 8;
4686 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4687 if (ppc64_sec->u.toc.add == NULL)
4688 return FALSE;
4689 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4690 ppc64_sec->sec_type = sec_toc;
4691 }
4692 BFD_ASSERT (rel->r_offset % 8 == 0);
4693 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
4694 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
4695
4696 /* Mark the second slot of a GD or LD entry.
4697 -1 to indicate GD and -2 to indicate LD. */
4698 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4699 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
4700 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4701 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
4702 goto dodyn;
4703
4704 case R_PPC64_TPREL16:
4705 case R_PPC64_TPREL16_LO:
4706 case R_PPC64_TPREL16_HI:
4707 case R_PPC64_TPREL16_HA:
4708 case R_PPC64_TPREL16_DS:
4709 case R_PPC64_TPREL16_LO_DS:
4710 case R_PPC64_TPREL16_HIGH:
4711 case R_PPC64_TPREL16_HIGHA:
4712 case R_PPC64_TPREL16_HIGHER:
4713 case R_PPC64_TPREL16_HIGHERA:
4714 case R_PPC64_TPREL16_HIGHEST:
4715 case R_PPC64_TPREL16_HIGHESTA:
4716 if (bfd_link_dll (info))
4717 info->flags |= DF_STATIC_TLS;
4718 goto dodyn;
4719
4720 case R_PPC64_ADDR64:
4721 if (is_opd
4722 && rel + 1 < rel_end
4723 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
4724 {
4725 if (h != NULL)
4726 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4727 }
4728 /* Fall through. */
4729
4730 case R_PPC64_ADDR16:
4731 case R_PPC64_ADDR16_DS:
4732 case R_PPC64_ADDR16_HA:
4733 case R_PPC64_ADDR16_HI:
4734 case R_PPC64_ADDR16_HIGH:
4735 case R_PPC64_ADDR16_HIGHA:
4736 case R_PPC64_ADDR16_HIGHER:
4737 case R_PPC64_ADDR16_HIGHERA:
4738 case R_PPC64_ADDR16_HIGHEST:
4739 case R_PPC64_ADDR16_HIGHESTA:
4740 case R_PPC64_ADDR16_LO:
4741 case R_PPC64_ADDR16_LO_DS:
4742 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
4743 && rel->r_addend == 0)
4744 {
4745 /* We may need a .plt entry if this reloc refers to a
4746 function in a shared lib. */
4747 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4748 return FALSE;
4749 h->pointer_equality_needed = 1;
4750 }
4751 /* Fall through. */
4752
4753 case R_PPC64_REL30:
4754 case R_PPC64_REL32:
4755 case R_PPC64_REL64:
4756 case R_PPC64_ADDR32:
4757 case R_PPC64_UADDR16:
4758 case R_PPC64_UADDR32:
4759 case R_PPC64_UADDR64:
4760 case R_PPC64_TOC:
4761 if (h != NULL && !bfd_link_pic (info))
4762 /* We may need a copy reloc. */
4763 h->non_got_ref = 1;
4764
4765 /* Don't propagate .opd relocs. */
4766 if (NO_OPD_RELOCS && is_opd)
4767 break;
4768
4769 /* If we are creating a shared library, and this is a reloc
4770 against a global symbol, or a non PC relative reloc
4771 against a local symbol, then we need to copy the reloc
4772 into the shared library. However, if we are linking with
4773 -Bsymbolic, we do not need to copy a reloc against a
4774 global symbol which is defined in an object we are
4775 including in the link (i.e., DEF_REGULAR is set). At
4776 this point we have not seen all the input files, so it is
4777 possible that DEF_REGULAR is not set now but will be set
4778 later (it is never cleared). In case of a weak definition,
4779 DEF_REGULAR may be cleared later by a strong definition in
4780 a shared library. We account for that possibility below by
4781 storing information in the dyn_relocs field of the hash
4782 table entry. A similar situation occurs when creating
4783 shared libraries and symbol visibility changes render the
4784 symbol local.
4785
4786 If on the other hand, we are creating an executable, we
4787 may need to keep relocations for symbols satisfied by a
4788 dynamic library if we manage to avoid copy relocs for the
4789 symbol. */
4790 dodyn:
4791 if ((bfd_link_pic (info)
4792 && (must_be_dyn_reloc (info, r_type)
4793 || (h != NULL
4794 && (!SYMBOLIC_BIND (info, h)
4795 || h->root.type == bfd_link_hash_defweak
4796 || !h->def_regular))))
4797 || (ELIMINATE_COPY_RELOCS
4798 && !bfd_link_pic (info)
4799 && h != NULL
4800 && (h->root.type == bfd_link_hash_defweak
4801 || !h->def_regular))
4802 || (!bfd_link_pic (info)
4803 && ifunc != NULL))
4804 {
4805 /* We must copy these reloc types into the output file.
4806 Create a reloc section in dynobj and make room for
4807 this reloc. */
4808 if (sreloc == NULL)
4809 {
4810 sreloc = _bfd_elf_make_dynamic_reloc_section
4811 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
4812
4813 if (sreloc == NULL)
4814 return FALSE;
4815 }
4816
4817 /* If this is a global symbol, we count the number of
4818 relocations we need for this symbol. */
4819 if (h != NULL)
4820 {
4821 struct elf_dyn_relocs *p;
4822 struct elf_dyn_relocs **head;
4823
4824 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
4825 p = *head;
4826 if (p == NULL || p->sec != sec)
4827 {
4828 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4829 if (p == NULL)
4830 return FALSE;
4831 p->next = *head;
4832 *head = p;
4833 p->sec = sec;
4834 p->count = 0;
4835 p->pc_count = 0;
4836 }
4837 p->count += 1;
4838 if (!must_be_dyn_reloc (info, r_type))
4839 p->pc_count += 1;
4840 }
4841 else
4842 {
4843 /* Track dynamic relocs needed for local syms too.
4844 We really need local syms available to do this
4845 easily. Oh well. */
4846 struct ppc_dyn_relocs *p;
4847 struct ppc_dyn_relocs **head;
4848 bfd_boolean is_ifunc;
4849 asection *s;
4850 void *vpp;
4851 Elf_Internal_Sym *isym;
4852
4853 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4854 abfd, r_symndx);
4855 if (isym == NULL)
4856 return FALSE;
4857
4858 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4859 if (s == NULL)
4860 s = sec;
4861
4862 vpp = &elf_section_data (s)->local_dynrel;
4863 head = (struct ppc_dyn_relocs **) vpp;
4864 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
4865 p = *head;
4866 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
4867 p = p->next;
4868 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
4869 {
4870 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4871 if (p == NULL)
4872 return FALSE;
4873 p->next = *head;
4874 *head = p;
4875 p->sec = sec;
4876 p->ifunc = is_ifunc;
4877 p->count = 0;
4878 }
4879 p->count += 1;
4880 }
4881 }
4882 break;
4883
4884 default:
4885 break;
4886 }
4887 }
4888
4889 return TRUE;
4890 }
4891
4892 /* Merge backend specific data from an object file to the output
4893 object file when linking. */
4894
4895 static bfd_boolean
4896 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4897 {
4898 bfd *obfd = info->output_bfd;
4899 unsigned long iflags, oflags;
4900
4901 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
4902 return TRUE;
4903
4904 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
4905 return TRUE;
4906
4907 if (!_bfd_generic_verify_endian_match (ibfd, info))
4908 return FALSE;
4909
4910 iflags = elf_elfheader (ibfd)->e_flags;
4911 oflags = elf_elfheader (obfd)->e_flags;
4912
4913 if (iflags & ~EF_PPC64_ABI)
4914 {
4915 _bfd_error_handler
4916 /* xgettext:c-format */
4917 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
4918 bfd_set_error (bfd_error_bad_value);
4919 return FALSE;
4920 }
4921 else if (iflags != oflags && iflags != 0)
4922 {
4923 _bfd_error_handler
4924 /* xgettext:c-format */
4925 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
4926 ibfd, iflags, oflags);
4927 bfd_set_error (bfd_error_bad_value);
4928 return FALSE;
4929 }
4930
4931 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
4932 return FALSE;
4933
4934 /* Merge Tag_compatibility attributes and any common GNU ones. */
4935 return _bfd_elf_merge_object_attributes (ibfd, info);
4936 }
4937
4938 static bfd_boolean
4939 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
4940 {
4941 /* Print normal ELF private data. */
4942 _bfd_elf_print_private_bfd_data (abfd, ptr);
4943
4944 if (elf_elfheader (abfd)->e_flags != 0)
4945 {
4946 FILE *file = ptr;
4947
4948 fprintf (file, _("private flags = 0x%lx:"),
4949 elf_elfheader (abfd)->e_flags);
4950
4951 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
4952 fprintf (file, _(" [abiv%ld]"),
4953 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
4954 fputc ('\n', file);
4955 }
4956
4957 return TRUE;
4958 }
4959
4960 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
4961 of the code entry point, and its section, which must be in the same
4962 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
4963
4964 static bfd_vma
4965 opd_entry_value (asection *opd_sec,
4966 bfd_vma offset,
4967 asection **code_sec,
4968 bfd_vma *code_off,
4969 bfd_boolean in_code_sec)
4970 {
4971 bfd *opd_bfd = opd_sec->owner;
4972 Elf_Internal_Rela *relocs;
4973 Elf_Internal_Rela *lo, *hi, *look;
4974 bfd_vma val;
4975
4976 /* No relocs implies we are linking a --just-symbols object, or looking
4977 at a final linked executable with addr2line or somesuch. */
4978 if (opd_sec->reloc_count == 0)
4979 {
4980 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
4981
4982 if (contents == NULL)
4983 {
4984 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
4985 return (bfd_vma) -1;
4986 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
4987 }
4988
4989 /* PR 17512: file: 64b9dfbb. */
4990 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
4991 return (bfd_vma) -1;
4992
4993 val = bfd_get_64 (opd_bfd, contents + offset);
4994 if (code_sec != NULL)
4995 {
4996 asection *sec, *likely = NULL;
4997
4998 if (in_code_sec)
4999 {
5000 sec = *code_sec;
5001 if (sec->vma <= val
5002 && val < sec->vma + sec->size)
5003 likely = sec;
5004 else
5005 val = -1;
5006 }
5007 else
5008 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5009 if (sec->vma <= val
5010 && (sec->flags & SEC_LOAD) != 0
5011 && (sec->flags & SEC_ALLOC) != 0)
5012 likely = sec;
5013 if (likely != NULL)
5014 {
5015 *code_sec = likely;
5016 if (code_off != NULL)
5017 *code_off = val - likely->vma;
5018 }
5019 }
5020 return val;
5021 }
5022
5023 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5024
5025 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5026 if (relocs == NULL)
5027 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5028 /* PR 17512: file: df8e1fd6. */
5029 if (relocs == NULL)
5030 return (bfd_vma) -1;
5031
5032 /* Go find the opd reloc at the sym address. */
5033 lo = relocs;
5034 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5035 val = (bfd_vma) -1;
5036 while (lo < hi)
5037 {
5038 look = lo + (hi - lo) / 2;
5039 if (look->r_offset < offset)
5040 lo = look + 1;
5041 else if (look->r_offset > offset)
5042 hi = look;
5043 else
5044 {
5045 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5046
5047 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5048 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5049 {
5050 unsigned long symndx = ELF64_R_SYM (look->r_info);
5051 asection *sec = NULL;
5052
5053 if (symndx >= symtab_hdr->sh_info
5054 && elf_sym_hashes (opd_bfd) != NULL)
5055 {
5056 struct elf_link_hash_entry **sym_hashes;
5057 struct elf_link_hash_entry *rh;
5058
5059 sym_hashes = elf_sym_hashes (opd_bfd);
5060 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5061 if (rh != NULL)
5062 {
5063 rh = elf_follow_link (rh);
5064 if (rh->root.type != bfd_link_hash_defined
5065 && rh->root.type != bfd_link_hash_defweak)
5066 break;
5067 if (rh->root.u.def.section->owner == opd_bfd)
5068 {
5069 val = rh->root.u.def.value;
5070 sec = rh->root.u.def.section;
5071 }
5072 }
5073 }
5074
5075 if (sec == NULL)
5076 {
5077 Elf_Internal_Sym *sym;
5078
5079 if (symndx < symtab_hdr->sh_info)
5080 {
5081 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5082 if (sym == NULL)
5083 {
5084 size_t symcnt = symtab_hdr->sh_info;
5085 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5086 symcnt, 0,
5087 NULL, NULL, NULL);
5088 if (sym == NULL)
5089 break;
5090 symtab_hdr->contents = (bfd_byte *) sym;
5091 }
5092 sym += symndx;
5093 }
5094 else
5095 {
5096 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5097 1, symndx,
5098 NULL, NULL, NULL);
5099 if (sym == NULL)
5100 break;
5101 }
5102 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5103 if (sec == NULL)
5104 break;
5105 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5106 val = sym->st_value;
5107 }
5108
5109 val += look->r_addend;
5110 if (code_off != NULL)
5111 *code_off = val;
5112 if (code_sec != NULL)
5113 {
5114 if (in_code_sec && *code_sec != sec)
5115 return -1;
5116 else
5117 *code_sec = sec;
5118 }
5119 if (sec->output_section != NULL)
5120 val += sec->output_section->vma + sec->output_offset;
5121 }
5122 break;
5123 }
5124 }
5125
5126 return val;
5127 }
5128
5129 /* If the ELF symbol SYM might be a function in SEC, return the
5130 function size and set *CODE_OFF to the function's entry point,
5131 otherwise return zero. */
5132
5133 static bfd_size_type
5134 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5135 bfd_vma *code_off)
5136 {
5137 bfd_size_type size;
5138
5139 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5140 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5141 return 0;
5142
5143 size = 0;
5144 if (!(sym->flags & BSF_SYNTHETIC))
5145 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5146
5147 if (strcmp (sym->section->name, ".opd") == 0)
5148 {
5149 struct _opd_sec_data *opd = get_opd_info (sym->section);
5150 bfd_vma symval = sym->value;
5151
5152 if (opd != NULL
5153 && opd->adjust != NULL
5154 && elf_section_data (sym->section)->relocs != NULL)
5155 {
5156 /* opd_entry_value will use cached relocs that have been
5157 adjusted, but with raw symbols. That means both local
5158 and global symbols need adjusting. */
5159 long adjust = opd->adjust[OPD_NDX (symval)];
5160 if (adjust == -1)
5161 return 0;
5162 symval += adjust;
5163 }
5164
5165 if (opd_entry_value (sym->section, symval,
5166 &sec, code_off, TRUE) == (bfd_vma) -1)
5167 return 0;
5168 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5169 symbol. This size has nothing to do with the code size of the
5170 function, which is what we're supposed to return, but the
5171 code size isn't available without looking up the dot-sym.
5172 However, doing that would be a waste of time particularly
5173 since elf_find_function will look at the dot-sym anyway.
5174 Now, elf_find_function will keep the largest size of any
5175 function sym found at the code address of interest, so return
5176 1 here to avoid it incorrectly caching a larger function size
5177 for a small function. This does mean we return the wrong
5178 size for a new-ABI function of size 24, but all that does is
5179 disable caching for such functions. */
5180 if (size == 24)
5181 size = 1;
5182 }
5183 else
5184 {
5185 if (sym->section != sec)
5186 return 0;
5187 *code_off = sym->value;
5188 }
5189 if (size == 0)
5190 size = 1;
5191 return size;
5192 }
5193
5194 /* Return true if symbol is a strong function defined in an ELFv2
5195 object with st_other localentry bits of zero, ie. its local entry
5196 point coincides with its global entry point. */
5197
5198 static bfd_boolean
5199 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5200 {
5201 return (h != NULL
5202 && h->type == STT_FUNC
5203 && h->root.type == bfd_link_hash_defined
5204 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5205 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
5206 && is_ppc64_elf (h->root.u.def.section->owner)
5207 && abiversion (h->root.u.def.section->owner) >= 2);
5208 }
5209
5210 /* Return true if symbol is defined in a regular object file. */
5211
5212 static bfd_boolean
5213 is_static_defined (struct elf_link_hash_entry *h)
5214 {
5215 return ((h->root.type == bfd_link_hash_defined
5216 || h->root.type == bfd_link_hash_defweak)
5217 && h->root.u.def.section != NULL
5218 && h->root.u.def.section->output_section != NULL);
5219 }
5220
5221 /* If FDH is a function descriptor symbol, return the associated code
5222 entry symbol if it is defined. Return NULL otherwise. */
5223
5224 static struct ppc_link_hash_entry *
5225 defined_code_entry (struct ppc_link_hash_entry *fdh)
5226 {
5227 if (fdh->is_func_descriptor)
5228 {
5229 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5230 if (fh->elf.root.type == bfd_link_hash_defined
5231 || fh->elf.root.type == bfd_link_hash_defweak)
5232 return fh;
5233 }
5234 return NULL;
5235 }
5236
5237 /* If FH is a function code entry symbol, return the associated
5238 function descriptor symbol if it is defined. Return NULL otherwise. */
5239
5240 static struct ppc_link_hash_entry *
5241 defined_func_desc (struct ppc_link_hash_entry *fh)
5242 {
5243 if (fh->oh != NULL
5244 && fh->oh->is_func_descriptor)
5245 {
5246 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5247 if (fdh->elf.root.type == bfd_link_hash_defined
5248 || fdh->elf.root.type == bfd_link_hash_defweak)
5249 return fdh;
5250 }
5251 return NULL;
5252 }
5253
5254 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5255
5256 /* Garbage collect sections, after first dealing with dot-symbols. */
5257
5258 static bfd_boolean
5259 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5260 {
5261 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5262
5263 if (htab != NULL && htab->need_func_desc_adj)
5264 {
5265 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5266 htab->need_func_desc_adj = 0;
5267 }
5268 return bfd_elf_gc_sections (abfd, info);
5269 }
5270
5271 /* Mark all our entry sym sections, both opd and code section. */
5272
5273 static void
5274 ppc64_elf_gc_keep (struct bfd_link_info *info)
5275 {
5276 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5277 struct bfd_sym_chain *sym;
5278
5279 if (htab == NULL)
5280 return;
5281
5282 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5283 {
5284 struct ppc_link_hash_entry *eh, *fh;
5285 asection *sec;
5286
5287 eh = (struct ppc_link_hash_entry *)
5288 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5289 if (eh == NULL)
5290 continue;
5291 if (eh->elf.root.type != bfd_link_hash_defined
5292 && eh->elf.root.type != bfd_link_hash_defweak)
5293 continue;
5294
5295 fh = defined_code_entry (eh);
5296 if (fh != NULL)
5297 {
5298 sec = fh->elf.root.u.def.section;
5299 sec->flags |= SEC_KEEP;
5300 }
5301 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5302 && opd_entry_value (eh->elf.root.u.def.section,
5303 eh->elf.root.u.def.value,
5304 &sec, NULL, FALSE) != (bfd_vma) -1)
5305 sec->flags |= SEC_KEEP;
5306
5307 sec = eh->elf.root.u.def.section;
5308 sec->flags |= SEC_KEEP;
5309 }
5310 }
5311
5312 /* Mark sections containing dynamically referenced symbols. When
5313 building shared libraries, we must assume that any visible symbol is
5314 referenced. */
5315
5316 static bfd_boolean
5317 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5318 {
5319 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5320 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5321 struct ppc_link_hash_entry *fdh;
5322 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5323
5324 /* Dynamic linking info is on the func descriptor sym. */
5325 fdh = defined_func_desc (eh);
5326 if (fdh != NULL)
5327 eh = fdh;
5328
5329 if ((eh->elf.root.type == bfd_link_hash_defined
5330 || eh->elf.root.type == bfd_link_hash_defweak)
5331 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5332 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5333 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5334 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5335 && (!bfd_link_executable (info)
5336 || info->gc_keep_exported
5337 || info->export_dynamic
5338 || (eh->elf.dynamic
5339 && d != NULL
5340 && (*d->match) (&d->head, NULL,
5341 eh->elf.root.root.string)))
5342 && (eh->elf.versioned >= versioned
5343 || !bfd_hide_sym_by_version (info->version_info,
5344 eh->elf.root.root.string)))))
5345 {
5346 asection *code_sec;
5347 struct ppc_link_hash_entry *fh;
5348
5349 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5350
5351 /* Function descriptor syms cause the associated
5352 function code sym section to be marked. */
5353 fh = defined_code_entry (eh);
5354 if (fh != NULL)
5355 {
5356 code_sec = fh->elf.root.u.def.section;
5357 code_sec->flags |= SEC_KEEP;
5358 }
5359 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5360 && opd_entry_value (eh->elf.root.u.def.section,
5361 eh->elf.root.u.def.value,
5362 &code_sec, NULL, FALSE) != (bfd_vma) -1)
5363 code_sec->flags |= SEC_KEEP;
5364 }
5365
5366 return TRUE;
5367 }
5368
5369 /* Return the section that should be marked against GC for a given
5370 relocation. */
5371
5372 static asection *
5373 ppc64_elf_gc_mark_hook (asection *sec,
5374 struct bfd_link_info *info,
5375 Elf_Internal_Rela *rel,
5376 struct elf_link_hash_entry *h,
5377 Elf_Internal_Sym *sym)
5378 {
5379 asection *rsec;
5380
5381 /* Syms return NULL if we're marking .opd, so we avoid marking all
5382 function sections, as all functions are referenced in .opd. */
5383 rsec = NULL;
5384 if (get_opd_info (sec) != NULL)
5385 return rsec;
5386
5387 if (h != NULL)
5388 {
5389 enum elf_ppc64_reloc_type r_type;
5390 struct ppc_link_hash_entry *eh, *fh, *fdh;
5391
5392 r_type = ELF64_R_TYPE (rel->r_info);
5393 switch (r_type)
5394 {
5395 case R_PPC64_GNU_VTINHERIT:
5396 case R_PPC64_GNU_VTENTRY:
5397 break;
5398
5399 default:
5400 switch (h->root.type)
5401 {
5402 case bfd_link_hash_defined:
5403 case bfd_link_hash_defweak:
5404 eh = (struct ppc_link_hash_entry *) h;
5405 fdh = defined_func_desc (eh);
5406 if (fdh != NULL)
5407 {
5408 /* -mcall-aixdesc code references the dot-symbol on
5409 a call reloc. Mark the function descriptor too
5410 against garbage collection. */
5411 fdh->elf.mark = 1;
5412 if (fdh->elf.is_weakalias)
5413 weakdef (&fdh->elf)->mark = 1;
5414 eh = fdh;
5415 }
5416
5417 /* Function descriptor syms cause the associated
5418 function code sym section to be marked. */
5419 fh = defined_code_entry (eh);
5420 if (fh != NULL)
5421 {
5422 /* They also mark their opd section. */
5423 eh->elf.root.u.def.section->gc_mark = 1;
5424
5425 rsec = fh->elf.root.u.def.section;
5426 }
5427 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5428 && opd_entry_value (eh->elf.root.u.def.section,
5429 eh->elf.root.u.def.value,
5430 &rsec, NULL, FALSE) != (bfd_vma) -1)
5431 eh->elf.root.u.def.section->gc_mark = 1;
5432 else
5433 rsec = h->root.u.def.section;
5434 break;
5435
5436 case bfd_link_hash_common:
5437 rsec = h->root.u.c.p->section;
5438 break;
5439
5440 default:
5441 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5442 }
5443 }
5444 }
5445 else
5446 {
5447 struct _opd_sec_data *opd;
5448
5449 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5450 opd = get_opd_info (rsec);
5451 if (opd != NULL && opd->func_sec != NULL)
5452 {
5453 rsec->gc_mark = 1;
5454
5455 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
5456 }
5457 }
5458
5459 return rsec;
5460 }
5461
5462 /* The maximum size of .sfpr. */
5463 #define SFPR_MAX (218*4)
5464
5465 struct sfpr_def_parms
5466 {
5467 const char name[12];
5468 unsigned char lo, hi;
5469 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5470 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
5471 };
5472
5473 /* Auto-generate _save*, _rest* functions in .sfpr.
5474 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5475 instead. */
5476
5477 static bfd_boolean
5478 sfpr_define (struct bfd_link_info *info,
5479 const struct sfpr_def_parms *parm,
5480 asection *stub_sec)
5481 {
5482 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5483 unsigned int i;
5484 size_t len = strlen (parm->name);
5485 bfd_boolean writing = FALSE;
5486 char sym[16];
5487
5488 if (htab == NULL)
5489 return FALSE;
5490
5491 memcpy (sym, parm->name, len);
5492 sym[len + 2] = 0;
5493
5494 for (i = parm->lo; i <= parm->hi; i++)
5495 {
5496 struct ppc_link_hash_entry *h;
5497
5498 sym[len + 0] = i / 10 + '0';
5499 sym[len + 1] = i % 10 + '0';
5500 h = (struct ppc_link_hash_entry *)
5501 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
5502 if (stub_sec != NULL)
5503 {
5504 if (h != NULL
5505 && h->elf.root.type == bfd_link_hash_defined
5506 && h->elf.root.u.def.section == htab->sfpr)
5507 {
5508 struct elf_link_hash_entry *s;
5509 char buf[32];
5510 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5511 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5512 if (s == NULL)
5513 return FALSE;
5514 if (s->root.type == bfd_link_hash_new
5515 || (s->root.type = bfd_link_hash_defined
5516 && s->root.u.def.section == stub_sec))
5517 {
5518 s->root.type = bfd_link_hash_defined;
5519 s->root.u.def.section = stub_sec;
5520 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
5521 + h->elf.root.u.def.value);
5522 s->ref_regular = 1;
5523 s->def_regular = 1;
5524 s->ref_regular_nonweak = 1;
5525 s->forced_local = 1;
5526 s->non_elf = 0;
5527 s->root.linker_def = 1;
5528 }
5529 }
5530 continue;
5531 }
5532 if (h != NULL)
5533 {
5534 h->save_res = 1;
5535 if (!h->elf.def_regular)
5536 {
5537 h->elf.root.type = bfd_link_hash_defined;
5538 h->elf.root.u.def.section = htab->sfpr;
5539 h->elf.root.u.def.value = htab->sfpr->size;
5540 h->elf.type = STT_FUNC;
5541 h->elf.def_regular = 1;
5542 h->elf.non_elf = 0;
5543 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5544 writing = TRUE;
5545 if (htab->sfpr->contents == NULL)
5546 {
5547 htab->sfpr->contents
5548 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5549 if (htab->sfpr->contents == NULL)
5550 return FALSE;
5551 }
5552 }
5553 }
5554 if (writing)
5555 {
5556 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5557 if (i != parm->hi)
5558 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5559 else
5560 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5561 htab->sfpr->size = p - htab->sfpr->contents;
5562 }
5563 }
5564
5565 return TRUE;
5566 }
5567
5568 static bfd_byte *
5569 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5570 {
5571 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5572 return p + 4;
5573 }
5574
5575 static bfd_byte *
5576 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5577 {
5578 p = savegpr0 (abfd, p, r);
5579 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5580 p = p + 4;
5581 bfd_put_32 (abfd, BLR, p);
5582 return p + 4;
5583 }
5584
5585 static bfd_byte *
5586 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5587 {
5588 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5589 return p + 4;
5590 }
5591
5592 static bfd_byte *
5593 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5594 {
5595 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5596 p = p + 4;
5597 p = restgpr0 (abfd, p, r);
5598 bfd_put_32 (abfd, MTLR_R0, p);
5599 p = p + 4;
5600 if (r == 29)
5601 {
5602 p = restgpr0 (abfd, p, 30);
5603 p = restgpr0 (abfd, p, 31);
5604 }
5605 bfd_put_32 (abfd, BLR, p);
5606 return p + 4;
5607 }
5608
5609 static bfd_byte *
5610 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5611 {
5612 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5613 return p + 4;
5614 }
5615
5616 static bfd_byte *
5617 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5618 {
5619 p = savegpr1 (abfd, p, r);
5620 bfd_put_32 (abfd, BLR, p);
5621 return p + 4;
5622 }
5623
5624 static bfd_byte *
5625 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5626 {
5627 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5628 return p + 4;
5629 }
5630
5631 static bfd_byte *
5632 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5633 {
5634 p = restgpr1 (abfd, p, r);
5635 bfd_put_32 (abfd, BLR, p);
5636 return p + 4;
5637 }
5638
5639 static bfd_byte *
5640 savefpr (bfd *abfd, bfd_byte *p, int r)
5641 {
5642 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5643 return p + 4;
5644 }
5645
5646 static bfd_byte *
5647 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5648 {
5649 p = savefpr (abfd, p, r);
5650 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5651 p = p + 4;
5652 bfd_put_32 (abfd, BLR, p);
5653 return p + 4;
5654 }
5655
5656 static bfd_byte *
5657 restfpr (bfd *abfd, bfd_byte *p, int r)
5658 {
5659 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5660 return p + 4;
5661 }
5662
5663 static bfd_byte *
5664 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5665 {
5666 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5667 p = p + 4;
5668 p = restfpr (abfd, p, r);
5669 bfd_put_32 (abfd, MTLR_R0, p);
5670 p = p + 4;
5671 if (r == 29)
5672 {
5673 p = restfpr (abfd, p, 30);
5674 p = restfpr (abfd, p, 31);
5675 }
5676 bfd_put_32 (abfd, BLR, p);
5677 return p + 4;
5678 }
5679
5680 static bfd_byte *
5681 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5682 {
5683 p = savefpr (abfd, p, r);
5684 bfd_put_32 (abfd, BLR, p);
5685 return p + 4;
5686 }
5687
5688 static bfd_byte *
5689 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5690 {
5691 p = restfpr (abfd, p, r);
5692 bfd_put_32 (abfd, BLR, p);
5693 return p + 4;
5694 }
5695
5696 static bfd_byte *
5697 savevr (bfd *abfd, bfd_byte *p, int r)
5698 {
5699 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5700 p = p + 4;
5701 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5702 return p + 4;
5703 }
5704
5705 static bfd_byte *
5706 savevr_tail (bfd *abfd, bfd_byte *p, int r)
5707 {
5708 p = savevr (abfd, p, r);
5709 bfd_put_32 (abfd, BLR, p);
5710 return p + 4;
5711 }
5712
5713 static bfd_byte *
5714 restvr (bfd *abfd, bfd_byte *p, int r)
5715 {
5716 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5717 p = p + 4;
5718 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5719 return p + 4;
5720 }
5721
5722 static bfd_byte *
5723 restvr_tail (bfd *abfd, bfd_byte *p, int r)
5724 {
5725 p = restvr (abfd, p, r);
5726 bfd_put_32 (abfd, BLR, p);
5727 return p + 4;
5728 }
5729
5730 /* Called via elf_link_hash_traverse to transfer dynamic linking
5731 information on function code symbol entries to their corresponding
5732 function descriptor symbol entries. */
5733
5734 static bfd_boolean
5735 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5736 {
5737 struct bfd_link_info *info;
5738 struct ppc_link_hash_table *htab;
5739 struct ppc_link_hash_entry *fh;
5740 struct ppc_link_hash_entry *fdh;
5741 bfd_boolean force_local;
5742
5743 fh = (struct ppc_link_hash_entry *) h;
5744 if (fh->elf.root.type == bfd_link_hash_indirect)
5745 return TRUE;
5746
5747 if (!fh->is_func)
5748 return TRUE;
5749
5750 if (fh->elf.root.root.string[0] != '.'
5751 || fh->elf.root.root.string[1] == '\0')
5752 return TRUE;
5753
5754 info = inf;
5755 htab = ppc_hash_table (info);
5756 if (htab == NULL)
5757 return FALSE;
5758
5759 /* Find the corresponding function descriptor symbol. */
5760 fdh = lookup_fdh (fh, htab);
5761
5762 /* Resolve undefined references to dot-symbols as the value
5763 in the function descriptor, if we have one in a regular object.
5764 This is to satisfy cases like ".quad .foo". Calls to functions
5765 in dynamic objects are handled elsewhere. */
5766 if ((fh->elf.root.type == bfd_link_hash_undefined
5767 || fh->elf.root.type == bfd_link_hash_undefweak)
5768 && (fdh->elf.root.type == bfd_link_hash_defined
5769 || fdh->elf.root.type == bfd_link_hash_defweak)
5770 && get_opd_info (fdh->elf.root.u.def.section) != NULL
5771 && opd_entry_value (fdh->elf.root.u.def.section,
5772 fdh->elf.root.u.def.value,
5773 &fh->elf.root.u.def.section,
5774 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
5775 {
5776 fh->elf.root.type = fdh->elf.root.type;
5777 fh->elf.forced_local = 1;
5778 fh->elf.def_regular = fdh->elf.def_regular;
5779 fh->elf.def_dynamic = fdh->elf.def_dynamic;
5780 }
5781
5782 if (!fh->elf.dynamic)
5783 {
5784 struct plt_entry *ent;
5785
5786 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5787 if (ent->plt.refcount > 0)
5788 break;
5789 if (ent == NULL)
5790 return TRUE;
5791 }
5792
5793 /* Create a descriptor as undefined if necessary. */
5794 if (fdh == NULL
5795 && !bfd_link_executable (info)
5796 && (fh->elf.root.type == bfd_link_hash_undefined
5797 || fh->elf.root.type == bfd_link_hash_undefweak))
5798 {
5799 fdh = make_fdh (info, fh);
5800 if (fdh == NULL)
5801 return FALSE;
5802 }
5803
5804 /* We can't support overriding of symbols on a fake descriptor. */
5805 if (fdh != NULL
5806 && fdh->fake
5807 && (fh->elf.root.type == bfd_link_hash_defined
5808 || fh->elf.root.type == bfd_link_hash_defweak))
5809 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
5810
5811 /* Transfer dynamic linking information to the function descriptor. */
5812 if (fdh != NULL)
5813 {
5814 fdh->elf.ref_regular |= fh->elf.ref_regular;
5815 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5816 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5817 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
5818 fdh->elf.dynamic |= fh->elf.dynamic;
5819 fdh->elf.needs_plt |= (fh->elf.needs_plt
5820 || fh->elf.type == STT_FUNC
5821 || fh->elf.type == STT_GNU_IFUNC);
5822 move_plt_plist (fh, fdh);
5823
5824 if (!fdh->elf.forced_local
5825 && fh->elf.dynindx != -1)
5826 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5827 return FALSE;
5828 }
5829
5830 /* Now that the info is on the function descriptor, clear the
5831 function code sym info. Any function code syms for which we
5832 don't have a definition in a regular file, we force local.
5833 This prevents a shared library from exporting syms that have
5834 been imported from another library. Function code syms that
5835 are really in the library we must leave global to prevent the
5836 linker dragging in a definition from a static library. */
5837 force_local = (!fh->elf.def_regular
5838 || fdh == NULL
5839 || !fdh->elf.def_regular
5840 || fdh->elf.forced_local);
5841 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5842
5843 return TRUE;
5844 }
5845
5846 static const struct sfpr_def_parms save_res_funcs[] =
5847 {
5848 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5849 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5850 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5851 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5852 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5853 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5854 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5855 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5856 { "._savef", 14, 31, savefpr, savefpr1_tail },
5857 { "._restf", 14, 31, restfpr, restfpr1_tail },
5858 { "_savevr_", 20, 31, savevr, savevr_tail },
5859 { "_restvr_", 20, 31, restvr, restvr_tail }
5860 };
5861
5862 /* Called near the start of bfd_elf_size_dynamic_sections. We use
5863 this hook to a) provide some gcc support functions, and b) transfer
5864 dynamic linking information gathered so far on function code symbol
5865 entries, to their corresponding function descriptor symbol entries. */
5866
5867 static bfd_boolean
5868 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5869 struct bfd_link_info *info)
5870 {
5871 struct ppc_link_hash_table *htab;
5872
5873 htab = ppc_hash_table (info);
5874 if (htab == NULL)
5875 return FALSE;
5876
5877 /* Provide any missing _save* and _rest* functions. */
5878 if (htab->sfpr != NULL)
5879 {
5880 unsigned int i;
5881
5882 htab->sfpr->size = 0;
5883 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
5884 if (!sfpr_define (info, &save_res_funcs[i], NULL))
5885 return FALSE;
5886 if (htab->sfpr->size == 0)
5887 htab->sfpr->flags |= SEC_EXCLUDE;
5888 }
5889
5890 if (bfd_link_relocatable (info))
5891 return TRUE;
5892
5893 if (htab->elf.hgot != NULL)
5894 {
5895 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
5896 /* Make .TOC. defined so as to prevent it being made dynamic.
5897 The wrong value here is fixed later in ppc64_elf_set_toc. */
5898 if (!htab->elf.hgot->def_regular
5899 || htab->elf.hgot->root.type != bfd_link_hash_defined)
5900 {
5901 htab->elf.hgot->root.type = bfd_link_hash_defined;
5902 htab->elf.hgot->root.u.def.value = 0;
5903 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
5904 htab->elf.hgot->def_regular = 1;
5905 htab->elf.hgot->root.linker_def = 1;
5906 }
5907 htab->elf.hgot->type = STT_OBJECT;
5908 htab->elf.hgot->other
5909 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5910 }
5911
5912 if (htab->need_func_desc_adj)
5913 {
5914 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5915 htab->need_func_desc_adj = 0;
5916 }
5917
5918 return TRUE;
5919 }
5920
5921 /* Find dynamic relocs for H that apply to read-only sections. */
5922
5923 static asection *
5924 readonly_dynrelocs (struct elf_link_hash_entry *h)
5925 {
5926 struct ppc_link_hash_entry *eh;
5927 struct elf_dyn_relocs *p;
5928
5929 eh = (struct ppc_link_hash_entry *) h;
5930 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5931 {
5932 asection *s = p->sec->output_section;
5933
5934 if (s != NULL && (s->flags & SEC_READONLY) != 0)
5935 return p->sec;
5936 }
5937 return NULL;
5938 }
5939
5940 /* Return true if we have dynamic relocs against H or any of its weak
5941 aliases, that apply to read-only sections. Cannot be used after
5942 size_dynamic_sections. */
5943
5944 static bfd_boolean
5945 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
5946 {
5947 struct ppc_link_hash_entry *eh;
5948
5949 eh = (struct ppc_link_hash_entry *) h;
5950 do
5951 {
5952 if (readonly_dynrelocs (&eh->elf))
5953 return TRUE;
5954 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
5955 }
5956 while (eh != NULL && &eh->elf != h);
5957
5958 return FALSE;
5959 }
5960
5961 /* Return whether EH has pc-relative dynamic relocs. */
5962
5963 static bfd_boolean
5964 pc_dynrelocs (struct ppc_link_hash_entry *eh)
5965 {
5966 struct elf_dyn_relocs *p;
5967
5968 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5969 if (p->pc_count != 0)
5970 return TRUE;
5971 return FALSE;
5972 }
5973
5974 /* Return true if a global entry stub will be created for H. Valid
5975 for ELFv2 before plt entries have been allocated. */
5976
5977 static bfd_boolean
5978 global_entry_stub (struct elf_link_hash_entry *h)
5979 {
5980 struct plt_entry *pent;
5981
5982 if (!h->pointer_equality_needed
5983 || h->def_regular)
5984 return FALSE;
5985
5986 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
5987 if (pent->plt.refcount > 0
5988 && pent->addend == 0)
5989 return TRUE;
5990
5991 return FALSE;
5992 }
5993
5994 /* Adjust a symbol defined by a dynamic object and referenced by a
5995 regular object. The current definition is in some section of the
5996 dynamic object, but we're not including those sections. We have to
5997 change the definition to something the rest of the link can
5998 understand. */
5999
6000 static bfd_boolean
6001 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6002 struct elf_link_hash_entry *h)
6003 {
6004 struct ppc_link_hash_table *htab;
6005 asection *s, *srel;
6006
6007 htab = ppc_hash_table (info);
6008 if (htab == NULL)
6009 return FALSE;
6010
6011 /* Deal with function syms. */
6012 if (h->type == STT_FUNC
6013 || h->type == STT_GNU_IFUNC
6014 || h->needs_plt)
6015 {
6016 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6017 || SYMBOL_CALLS_LOCAL (info, h)
6018 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6019 /* Discard dyn_relocs when non-pic if we've decided that a
6020 function symbol is local and not an ifunc. We keep dynamic
6021 relocs for ifuncs when local rather than always emitting a
6022 plt call stub for them and defining the symbol on the call
6023 stub. We can't do that for ELFv1 anyway (a function symbol
6024 is defined on a descriptor, not code) and it can be faster at
6025 run-time due to not needing to bounce through a stub. The
6026 dyn_relocs for ifuncs will be applied even in a static
6027 executable. */
6028 if (!bfd_link_pic (info)
6029 && h->type != STT_GNU_IFUNC
6030 && local)
6031 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6032
6033 /* Clear procedure linkage table information for any symbol that
6034 won't need a .plt entry. */
6035 struct plt_entry *ent;
6036 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6037 if (ent->plt.refcount > 0)
6038 break;
6039 if (ent == NULL
6040 || (h->type != STT_GNU_IFUNC
6041 && local
6042 && (htab->can_convert_all_inline_plt
6043 || (((struct ppc_link_hash_entry *) h)->tls_mask
6044 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6045 {
6046 h->plt.plist = NULL;
6047 h->needs_plt = 0;
6048 h->pointer_equality_needed = 0;
6049 }
6050 else if (abiversion (info->output_bfd) >= 2)
6051 {
6052 /* Taking a function's address in a read/write section
6053 doesn't require us to define the function symbol in the
6054 executable on a global entry stub. A dynamic reloc can
6055 be used instead. The reason we prefer a few more dynamic
6056 relocs is that calling via a global entry stub costs a
6057 few more instructions, and pointer_equality_needed causes
6058 extra work in ld.so when resolving these symbols. */
6059 if (global_entry_stub (h))
6060 {
6061 if (!readonly_dynrelocs (h))
6062 {
6063 h->pointer_equality_needed = 0;
6064 /* If we haven't seen a branch reloc and the symbol
6065 isn't an ifunc then we don't need a plt entry. */
6066 if (!h->needs_plt)
6067 h->plt.plist = NULL;
6068 }
6069 else if (!bfd_link_pic (info))
6070 /* We are going to be defining the function symbol on the
6071 plt stub, so no dyn_relocs needed when non-pic. */
6072 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6073 }
6074
6075 /* ELFv2 function symbols can't have copy relocs. */
6076 return TRUE;
6077 }
6078 else if (!h->needs_plt
6079 && !readonly_dynrelocs (h))
6080 {
6081 /* If we haven't seen a branch reloc and the symbol isn't an
6082 ifunc then we don't need a plt entry. */
6083 h->plt.plist = NULL;
6084 h->pointer_equality_needed = 0;
6085 return TRUE;
6086 }
6087 }
6088 else
6089 h->plt.plist = NULL;
6090
6091 /* If this is a weak symbol, and there is a real definition, the
6092 processor independent code will have arranged for us to see the
6093 real definition first, and we can just use the same value. */
6094 if (h->is_weakalias)
6095 {
6096 struct elf_link_hash_entry *def = weakdef (h);
6097 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6098 h->root.u.def.section = def->root.u.def.section;
6099 h->root.u.def.value = def->root.u.def.value;
6100 if (def->root.u.def.section == htab->elf.sdynbss
6101 || def->root.u.def.section == htab->elf.sdynrelro)
6102 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6103 return TRUE;
6104 }
6105
6106 /* If we are creating a shared library, we must presume that the
6107 only references to the symbol are via the global offset table.
6108 For such cases we need not do anything here; the relocations will
6109 be handled correctly by relocate_section. */
6110 if (bfd_link_pic (info))
6111 return TRUE;
6112
6113 /* If there are no references to this symbol that do not use the
6114 GOT, we don't need to generate a copy reloc. */
6115 if (!h->non_got_ref)
6116 return TRUE;
6117
6118 /* Don't generate a copy reloc for symbols defined in the executable. */
6119 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6120
6121 /* If -z nocopyreloc was given, don't generate them either. */
6122 || info->nocopyreloc
6123
6124 /* If we don't find any dynamic relocs in read-only sections, then
6125 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6126 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
6127
6128 /* Protected variables do not work with .dynbss. The copy in
6129 .dynbss won't be used by the shared library with the protected
6130 definition for the variable. Text relocations are preferable
6131 to an incorrect program. */
6132 || h->protected_def)
6133 return TRUE;
6134
6135 if (h->plt.plist != NULL)
6136 {
6137 /* We should never get here, but unfortunately there are versions
6138 of gcc out there that improperly (for this ABI) put initialized
6139 function pointers, vtable refs and suchlike in read-only
6140 sections. Allow them to proceed, but warn that this might
6141 break at runtime. */
6142 info->callbacks->einfo
6143 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6144 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6145 h->root.root.string);
6146 }
6147
6148 /* This is a reference to a symbol defined by a dynamic object which
6149 is not a function. */
6150
6151 /* We must allocate the symbol in our .dynbss section, which will
6152 become part of the .bss section of the executable. There will be
6153 an entry for this symbol in the .dynsym section. The dynamic
6154 object will contain position independent code, so all references
6155 from the dynamic object to this symbol will go through the global
6156 offset table. The dynamic linker will use the .dynsym entry to
6157 determine the address it must put in the global offset table, so
6158 both the dynamic object and the regular object will refer to the
6159 same memory location for the variable. */
6160 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6161 {
6162 s = htab->elf.sdynrelro;
6163 srel = htab->elf.sreldynrelro;
6164 }
6165 else
6166 {
6167 s = htab->elf.sdynbss;
6168 srel = htab->elf.srelbss;
6169 }
6170 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6171 {
6172 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6173 linker to copy the initial value out of the dynamic object
6174 and into the runtime process image. */
6175 srel->size += sizeof (Elf64_External_Rela);
6176 h->needs_copy = 1;
6177 }
6178
6179 /* We no longer want dyn_relocs. */
6180 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6181 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6182 }
6183
6184 /* If given a function descriptor symbol, hide both the function code
6185 sym and the descriptor. */
6186 static void
6187 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6188 struct elf_link_hash_entry *h,
6189 bfd_boolean force_local)
6190 {
6191 struct ppc_link_hash_entry *eh;
6192 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6193
6194 if (ppc_hash_table (info) == NULL)
6195 return;
6196
6197 eh = (struct ppc_link_hash_entry *) h;
6198 if (eh->is_func_descriptor)
6199 {
6200 struct ppc_link_hash_entry *fh = eh->oh;
6201
6202 if (fh == NULL)
6203 {
6204 const char *p, *q;
6205 struct elf_link_hash_table *htab = elf_hash_table (info);
6206 char save;
6207
6208 /* We aren't supposed to use alloca in BFD because on
6209 systems which do not have alloca the version in libiberty
6210 calls xmalloc, which might cause the program to crash
6211 when it runs out of memory. This function doesn't have a
6212 return status, so there's no way to gracefully return an
6213 error. So cheat. We know that string[-1] can be safely
6214 accessed; It's either a string in an ELF string table,
6215 or allocated in an objalloc structure. */
6216
6217 p = eh->elf.root.root.string - 1;
6218 save = *p;
6219 *(char *) p = '.';
6220 fh = (struct ppc_link_hash_entry *)
6221 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6222 *(char *) p = save;
6223
6224 /* Unfortunately, if it so happens that the string we were
6225 looking for was allocated immediately before this string,
6226 then we overwrote the string terminator. That's the only
6227 reason the lookup should fail. */
6228 if (fh == NULL)
6229 {
6230 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6231 while (q >= eh->elf.root.root.string && *q == *p)
6232 --q, --p;
6233 if (q < eh->elf.root.root.string && *p == '.')
6234 fh = (struct ppc_link_hash_entry *)
6235 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6236 }
6237 if (fh != NULL)
6238 {
6239 eh->oh = fh;
6240 fh->oh = eh;
6241 }
6242 }
6243 if (fh != NULL)
6244 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6245 }
6246 }
6247
6248 static bfd_boolean
6249 get_sym_h (struct elf_link_hash_entry **hp,
6250 Elf_Internal_Sym **symp,
6251 asection **symsecp,
6252 unsigned char **tls_maskp,
6253 Elf_Internal_Sym **locsymsp,
6254 unsigned long r_symndx,
6255 bfd *ibfd)
6256 {
6257 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6258
6259 if (r_symndx >= symtab_hdr->sh_info)
6260 {
6261 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6262 struct elf_link_hash_entry *h;
6263
6264 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6265 h = elf_follow_link (h);
6266
6267 if (hp != NULL)
6268 *hp = h;
6269
6270 if (symp != NULL)
6271 *symp = NULL;
6272
6273 if (symsecp != NULL)
6274 {
6275 asection *symsec = NULL;
6276 if (h->root.type == bfd_link_hash_defined
6277 || h->root.type == bfd_link_hash_defweak)
6278 symsec = h->root.u.def.section;
6279 *symsecp = symsec;
6280 }
6281
6282 if (tls_maskp != NULL)
6283 {
6284 struct ppc_link_hash_entry *eh;
6285
6286 eh = (struct ppc_link_hash_entry *) h;
6287 *tls_maskp = &eh->tls_mask;
6288 }
6289 }
6290 else
6291 {
6292 Elf_Internal_Sym *sym;
6293 Elf_Internal_Sym *locsyms = *locsymsp;
6294
6295 if (locsyms == NULL)
6296 {
6297 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6298 if (locsyms == NULL)
6299 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6300 symtab_hdr->sh_info,
6301 0, NULL, NULL, NULL);
6302 if (locsyms == NULL)
6303 return FALSE;
6304 *locsymsp = locsyms;
6305 }
6306 sym = locsyms + r_symndx;
6307
6308 if (hp != NULL)
6309 *hp = NULL;
6310
6311 if (symp != NULL)
6312 *symp = sym;
6313
6314 if (symsecp != NULL)
6315 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6316
6317 if (tls_maskp != NULL)
6318 {
6319 struct got_entry **lgot_ents;
6320 unsigned char *tls_mask;
6321
6322 tls_mask = NULL;
6323 lgot_ents = elf_local_got_ents (ibfd);
6324 if (lgot_ents != NULL)
6325 {
6326 struct plt_entry **local_plt = (struct plt_entry **)
6327 (lgot_ents + symtab_hdr->sh_info);
6328 unsigned char *lgot_masks = (unsigned char *)
6329 (local_plt + symtab_hdr->sh_info);
6330 tls_mask = &lgot_masks[r_symndx];
6331 }
6332 *tls_maskp = tls_mask;
6333 }
6334 }
6335 return TRUE;
6336 }
6337
6338 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6339 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6340 type suitable for optimization, and 1 otherwise. */
6341
6342 static int
6343 get_tls_mask (unsigned char **tls_maskp,
6344 unsigned long *toc_symndx,
6345 bfd_vma *toc_addend,
6346 Elf_Internal_Sym **locsymsp,
6347 const Elf_Internal_Rela *rel,
6348 bfd *ibfd)
6349 {
6350 unsigned long r_symndx;
6351 int next_r;
6352 struct elf_link_hash_entry *h;
6353 Elf_Internal_Sym *sym;
6354 asection *sec;
6355 bfd_vma off;
6356
6357 r_symndx = ELF64_R_SYM (rel->r_info);
6358 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6359 return 0;
6360
6361 if ((*tls_maskp != NULL
6362 && (**tls_maskp & TLS_TLS) != 0
6363 && **tls_maskp != (TLS_TLS | TLS_MARK))
6364 || sec == NULL
6365 || ppc64_elf_section_data (sec) == NULL
6366 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6367 return 1;
6368
6369 /* Look inside a TOC section too. */
6370 if (h != NULL)
6371 {
6372 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6373 off = h->root.u.def.value;
6374 }
6375 else
6376 off = sym->st_value;
6377 off += rel->r_addend;
6378 BFD_ASSERT (off % 8 == 0);
6379 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6380 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6381 if (toc_symndx != NULL)
6382 *toc_symndx = r_symndx;
6383 if (toc_addend != NULL)
6384 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6385 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6386 return 0;
6387 if ((h == NULL || is_static_defined (h))
6388 && (next_r == -1 || next_r == -2))
6389 return 1 - next_r;
6390 return 1;
6391 }
6392
6393 /* Find (or create) an entry in the tocsave hash table. */
6394
6395 static struct tocsave_entry *
6396 tocsave_find (struct ppc_link_hash_table *htab,
6397 enum insert_option insert,
6398 Elf_Internal_Sym **local_syms,
6399 const Elf_Internal_Rela *irela,
6400 bfd *ibfd)
6401 {
6402 unsigned long r_indx;
6403 struct elf_link_hash_entry *h;
6404 Elf_Internal_Sym *sym;
6405 struct tocsave_entry ent, *p;
6406 hashval_t hash;
6407 struct tocsave_entry **slot;
6408
6409 r_indx = ELF64_R_SYM (irela->r_info);
6410 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6411 return NULL;
6412 if (ent.sec == NULL || ent.sec->output_section == NULL)
6413 {
6414 _bfd_error_handler
6415 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
6416 return NULL;
6417 }
6418
6419 if (h != NULL)
6420 ent.offset = h->root.u.def.value;
6421 else
6422 ent.offset = sym->st_value;
6423 ent.offset += irela->r_addend;
6424
6425 hash = tocsave_htab_hash (&ent);
6426 slot = ((struct tocsave_entry **)
6427 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6428 if (slot == NULL)
6429 return NULL;
6430
6431 if (*slot == NULL)
6432 {
6433 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6434 if (p == NULL)
6435 return NULL;
6436 *p = ent;
6437 *slot = p;
6438 }
6439 return *slot;
6440 }
6441
6442 /* Adjust all global syms defined in opd sections. In gcc generated
6443 code for the old ABI, these will already have been done. */
6444
6445 static bfd_boolean
6446 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6447 {
6448 struct ppc_link_hash_entry *eh;
6449 asection *sym_sec;
6450 struct _opd_sec_data *opd;
6451
6452 if (h->root.type == bfd_link_hash_indirect)
6453 return TRUE;
6454
6455 if (h->root.type != bfd_link_hash_defined
6456 && h->root.type != bfd_link_hash_defweak)
6457 return TRUE;
6458
6459 eh = (struct ppc_link_hash_entry *) h;
6460 if (eh->adjust_done)
6461 return TRUE;
6462
6463 sym_sec = eh->elf.root.u.def.section;
6464 opd = get_opd_info (sym_sec);
6465 if (opd != NULL && opd->adjust != NULL)
6466 {
6467 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
6468 if (adjust == -1)
6469 {
6470 /* This entry has been deleted. */
6471 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6472 if (dsec == NULL)
6473 {
6474 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6475 if (discarded_section (dsec))
6476 {
6477 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6478 break;
6479 }
6480 }
6481 eh->elf.root.u.def.value = 0;
6482 eh->elf.root.u.def.section = dsec;
6483 }
6484 else
6485 eh->elf.root.u.def.value += adjust;
6486 eh->adjust_done = 1;
6487 }
6488 return TRUE;
6489 }
6490
6491 /* Handles decrementing dynamic reloc counts for the reloc specified by
6492 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
6493 have already been determined. */
6494
6495 static bfd_boolean
6496 dec_dynrel_count (bfd_vma r_info,
6497 asection *sec,
6498 struct bfd_link_info *info,
6499 Elf_Internal_Sym **local_syms,
6500 struct elf_link_hash_entry *h,
6501 Elf_Internal_Sym *sym)
6502 {
6503 enum elf_ppc64_reloc_type r_type;
6504 asection *sym_sec = NULL;
6505
6506 /* Can this reloc be dynamic? This switch, and later tests here
6507 should be kept in sync with the code in check_relocs. */
6508 r_type = ELF64_R_TYPE (r_info);
6509 switch (r_type)
6510 {
6511 default:
6512 return TRUE;
6513
6514 case R_PPC64_TPREL16:
6515 case R_PPC64_TPREL16_LO:
6516 case R_PPC64_TPREL16_HI:
6517 case R_PPC64_TPREL16_HA:
6518 case R_PPC64_TPREL16_DS:
6519 case R_PPC64_TPREL16_LO_DS:
6520 case R_PPC64_TPREL16_HIGH:
6521 case R_PPC64_TPREL16_HIGHA:
6522 case R_PPC64_TPREL16_HIGHER:
6523 case R_PPC64_TPREL16_HIGHERA:
6524 case R_PPC64_TPREL16_HIGHEST:
6525 case R_PPC64_TPREL16_HIGHESTA:
6526 case R_PPC64_TPREL64:
6527 case R_PPC64_DTPMOD64:
6528 case R_PPC64_DTPREL64:
6529 case R_PPC64_ADDR64:
6530 case R_PPC64_REL30:
6531 case R_PPC64_REL32:
6532 case R_PPC64_REL64:
6533 case R_PPC64_ADDR14:
6534 case R_PPC64_ADDR14_BRNTAKEN:
6535 case R_PPC64_ADDR14_BRTAKEN:
6536 case R_PPC64_ADDR16:
6537 case R_PPC64_ADDR16_DS:
6538 case R_PPC64_ADDR16_HA:
6539 case R_PPC64_ADDR16_HI:
6540 case R_PPC64_ADDR16_HIGH:
6541 case R_PPC64_ADDR16_HIGHA:
6542 case R_PPC64_ADDR16_HIGHER:
6543 case R_PPC64_ADDR16_HIGHERA:
6544 case R_PPC64_ADDR16_HIGHEST:
6545 case R_PPC64_ADDR16_HIGHESTA:
6546 case R_PPC64_ADDR16_LO:
6547 case R_PPC64_ADDR16_LO_DS:
6548 case R_PPC64_ADDR24:
6549 case R_PPC64_ADDR32:
6550 case R_PPC64_UADDR16:
6551 case R_PPC64_UADDR32:
6552 case R_PPC64_UADDR64:
6553 case R_PPC64_TOC:
6554 break;
6555 }
6556
6557 if (local_syms != NULL)
6558 {
6559 unsigned long r_symndx;
6560 bfd *ibfd = sec->owner;
6561
6562 r_symndx = ELF64_R_SYM (r_info);
6563 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6564 return FALSE;
6565 }
6566
6567 if ((bfd_link_pic (info)
6568 && (must_be_dyn_reloc (info, r_type)
6569 || (h != NULL
6570 && (!SYMBOLIC_BIND (info, h)
6571 || h->root.type == bfd_link_hash_defweak
6572 || !h->def_regular))))
6573 || (ELIMINATE_COPY_RELOCS
6574 && !bfd_link_pic (info)
6575 && h != NULL
6576 && (h->root.type == bfd_link_hash_defweak
6577 || !h->def_regular)))
6578 ;
6579 else
6580 return TRUE;
6581
6582 if (h != NULL)
6583 {
6584 struct elf_dyn_relocs *p;
6585 struct elf_dyn_relocs **pp;
6586 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6587
6588 /* elf_gc_sweep may have already removed all dyn relocs associated
6589 with local syms for a given section. Also, symbol flags are
6590 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6591 report a dynreloc miscount. */
6592 if (*pp == NULL && info->gc_sections)
6593 return TRUE;
6594
6595 while ((p = *pp) != NULL)
6596 {
6597 if (p->sec == sec)
6598 {
6599 if (!must_be_dyn_reloc (info, r_type))
6600 p->pc_count -= 1;
6601 p->count -= 1;
6602 if (p->count == 0)
6603 *pp = p->next;
6604 return TRUE;
6605 }
6606 pp = &p->next;
6607 }
6608 }
6609 else
6610 {
6611 struct ppc_dyn_relocs *p;
6612 struct ppc_dyn_relocs **pp;
6613 void *vpp;
6614 bfd_boolean is_ifunc;
6615
6616 if (local_syms == NULL)
6617 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6618 if (sym_sec == NULL)
6619 sym_sec = sec;
6620
6621 vpp = &elf_section_data (sym_sec)->local_dynrel;
6622 pp = (struct ppc_dyn_relocs **) vpp;
6623
6624 if (*pp == NULL && info->gc_sections)
6625 return TRUE;
6626
6627 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6628 while ((p = *pp) != NULL)
6629 {
6630 if (p->sec == sec && p->ifunc == is_ifunc)
6631 {
6632 p->count -= 1;
6633 if (p->count == 0)
6634 *pp = p->next;
6635 return TRUE;
6636 }
6637 pp = &p->next;
6638 }
6639 }
6640
6641 /* xgettext:c-format */
6642 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6643 sec->owner, sec);
6644 bfd_set_error (bfd_error_bad_value);
6645 return FALSE;
6646 }
6647
6648 /* Remove unused Official Procedure Descriptor entries. Currently we
6649 only remove those associated with functions in discarded link-once
6650 sections, or weakly defined functions that have been overridden. It
6651 would be possible to remove many more entries for statically linked
6652 applications. */
6653
6654 bfd_boolean
6655 ppc64_elf_edit_opd (struct bfd_link_info *info)
6656 {
6657 bfd *ibfd;
6658 bfd_boolean some_edited = FALSE;
6659 asection *need_pad = NULL;
6660 struct ppc_link_hash_table *htab;
6661
6662 htab = ppc_hash_table (info);
6663 if (htab == NULL)
6664 return FALSE;
6665
6666 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6667 {
6668 asection *sec;
6669 Elf_Internal_Rela *relstart, *rel, *relend;
6670 Elf_Internal_Shdr *symtab_hdr;
6671 Elf_Internal_Sym *local_syms;
6672 struct _opd_sec_data *opd;
6673 bfd_boolean need_edit, add_aux_fields, broken;
6674 bfd_size_type cnt_16b = 0;
6675
6676 if (!is_ppc64_elf (ibfd))
6677 continue;
6678
6679 sec = bfd_get_section_by_name (ibfd, ".opd");
6680 if (sec == NULL || sec->size == 0)
6681 continue;
6682
6683 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6684 continue;
6685
6686 if (sec->output_section == bfd_abs_section_ptr)
6687 continue;
6688
6689 /* Look through the section relocs. */
6690 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6691 continue;
6692
6693 local_syms = NULL;
6694 symtab_hdr = &elf_symtab_hdr (ibfd);
6695
6696 /* Read the relocations. */
6697 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6698 info->keep_memory);
6699 if (relstart == NULL)
6700 return FALSE;
6701
6702 /* First run through the relocs to check they are sane, and to
6703 determine whether we need to edit this opd section. */
6704 need_edit = FALSE;
6705 broken = FALSE;
6706 need_pad = sec;
6707 relend = relstart + sec->reloc_count;
6708 for (rel = relstart; rel < relend; )
6709 {
6710 enum elf_ppc64_reloc_type r_type;
6711 unsigned long r_symndx;
6712 asection *sym_sec;
6713 struct elf_link_hash_entry *h;
6714 Elf_Internal_Sym *sym;
6715 bfd_vma offset;
6716
6717 /* .opd contains an array of 16 or 24 byte entries. We're
6718 only interested in the reloc pointing to a function entry
6719 point. */
6720 offset = rel->r_offset;
6721 if (rel + 1 == relend
6722 || rel[1].r_offset != offset + 8)
6723 {
6724 /* If someone messes with .opd alignment then after a
6725 "ld -r" we might have padding in the middle of .opd.
6726 Also, there's nothing to prevent someone putting
6727 something silly in .opd with the assembler. No .opd
6728 optimization for them! */
6729 broken_opd:
6730 _bfd_error_handler
6731 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
6732 broken = TRUE;
6733 break;
6734 }
6735
6736 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6737 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6738 {
6739 _bfd_error_handler
6740 /* xgettext:c-format */
6741 (_("%pB: unexpected reloc type %u in .opd section"),
6742 ibfd, r_type);
6743 broken = TRUE;
6744 break;
6745 }
6746
6747 r_symndx = ELF64_R_SYM (rel->r_info);
6748 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6749 r_symndx, ibfd))
6750 goto error_ret;
6751
6752 if (sym_sec == NULL || sym_sec->owner == NULL)
6753 {
6754 const char *sym_name;
6755 if (h != NULL)
6756 sym_name = h->root.root.string;
6757 else
6758 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6759 sym_sec);
6760
6761 _bfd_error_handler
6762 /* xgettext:c-format */
6763 (_("%pB: undefined sym `%s' in .opd section"),
6764 ibfd, sym_name);
6765 broken = TRUE;
6766 break;
6767 }
6768
6769 /* opd entries are always for functions defined in the
6770 current input bfd. If the symbol isn't defined in the
6771 input bfd, then we won't be using the function in this
6772 bfd; It must be defined in a linkonce section in another
6773 bfd, or is weak. It's also possible that we are
6774 discarding the function due to a linker script /DISCARD/,
6775 which we test for via the output_section. */
6776 if (sym_sec->owner != ibfd
6777 || sym_sec->output_section == bfd_abs_section_ptr)
6778 need_edit = TRUE;
6779
6780 rel += 2;
6781 if (rel + 1 == relend
6782 || (rel + 2 < relend
6783 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
6784 ++rel;
6785
6786 if (rel == relend)
6787 {
6788 if (sec->size == offset + 24)
6789 {
6790 need_pad = NULL;
6791 break;
6792 }
6793 if (sec->size == offset + 16)
6794 {
6795 cnt_16b++;
6796 break;
6797 }
6798 goto broken_opd;
6799 }
6800 else if (rel + 1 < relend
6801 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6802 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6803 {
6804 if (rel[0].r_offset == offset + 16)
6805 cnt_16b++;
6806 else if (rel[0].r_offset != offset + 24)
6807 goto broken_opd;
6808 }
6809 else
6810 goto broken_opd;
6811 }
6812
6813 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
6814
6815 if (!broken && (need_edit || add_aux_fields))
6816 {
6817 Elf_Internal_Rela *write_rel;
6818 Elf_Internal_Shdr *rel_hdr;
6819 bfd_byte *rptr, *wptr;
6820 bfd_byte *new_contents;
6821 bfd_size_type amt;
6822
6823 new_contents = NULL;
6824 amt = OPD_NDX (sec->size) * sizeof (long);
6825 opd = &ppc64_elf_section_data (sec)->u.opd;
6826 opd->adjust = bfd_zalloc (sec->owner, amt);
6827 if (opd->adjust == NULL)
6828 return FALSE;
6829
6830 /* This seems a waste of time as input .opd sections are all
6831 zeros as generated by gcc, but I suppose there's no reason
6832 this will always be so. We might start putting something in
6833 the third word of .opd entries. */
6834 if ((sec->flags & SEC_IN_MEMORY) == 0)
6835 {
6836 bfd_byte *loc;
6837 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6838 {
6839 if (loc != NULL)
6840 free (loc);
6841 error_ret:
6842 if (local_syms != NULL
6843 && symtab_hdr->contents != (unsigned char *) local_syms)
6844 free (local_syms);
6845 if (elf_section_data (sec)->relocs != relstart)
6846 free (relstart);
6847 return FALSE;
6848 }
6849 sec->contents = loc;
6850 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6851 }
6852
6853 elf_section_data (sec)->relocs = relstart;
6854
6855 new_contents = sec->contents;
6856 if (add_aux_fields)
6857 {
6858 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6859 if (new_contents == NULL)
6860 return FALSE;
6861 need_pad = NULL;
6862 }
6863 wptr = new_contents;
6864 rptr = sec->contents;
6865 write_rel = relstart;
6866 for (rel = relstart; rel < relend; )
6867 {
6868 unsigned long r_symndx;
6869 asection *sym_sec;
6870 struct elf_link_hash_entry *h;
6871 struct ppc_link_hash_entry *fdh = NULL;
6872 Elf_Internal_Sym *sym;
6873 long opd_ent_size;
6874 Elf_Internal_Rela *next_rel;
6875 bfd_boolean skip;
6876
6877 r_symndx = ELF64_R_SYM (rel->r_info);
6878 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6879 r_symndx, ibfd))
6880 goto error_ret;
6881
6882 next_rel = rel + 2;
6883 if (next_rel + 1 == relend
6884 || (next_rel + 2 < relend
6885 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
6886 ++next_rel;
6887
6888 /* See if the .opd entry is full 24 byte or
6889 16 byte (with fd_aux entry overlapped with next
6890 fd_func). */
6891 opd_ent_size = 24;
6892 if (next_rel == relend)
6893 {
6894 if (sec->size == rel->r_offset + 16)
6895 opd_ent_size = 16;
6896 }
6897 else if (next_rel->r_offset == rel->r_offset + 16)
6898 opd_ent_size = 16;
6899
6900 if (h != NULL
6901 && h->root.root.string[0] == '.')
6902 {
6903 fdh = ((struct ppc_link_hash_entry *) h)->oh;
6904 if (fdh != NULL)
6905 {
6906 fdh = ppc_follow_link (fdh);
6907 if (fdh->elf.root.type != bfd_link_hash_defined
6908 && fdh->elf.root.type != bfd_link_hash_defweak)
6909 fdh = NULL;
6910 }
6911 }
6912
6913 skip = (sym_sec->owner != ibfd
6914 || sym_sec->output_section == bfd_abs_section_ptr);
6915 if (skip)
6916 {
6917 if (fdh != NULL && sym_sec->owner == ibfd)
6918 {
6919 /* Arrange for the function descriptor sym
6920 to be dropped. */
6921 fdh->elf.root.u.def.value = 0;
6922 fdh->elf.root.u.def.section = sym_sec;
6923 }
6924 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
6925
6926 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
6927 rel = next_rel;
6928 else
6929 while (1)
6930 {
6931 if (!dec_dynrel_count (rel->r_info, sec, info,
6932 NULL, h, sym))
6933 goto error_ret;
6934
6935 if (++rel == next_rel)
6936 break;
6937
6938 r_symndx = ELF64_R_SYM (rel->r_info);
6939 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6940 r_symndx, ibfd))
6941 goto error_ret;
6942 }
6943 }
6944 else
6945 {
6946 /* We'll be keeping this opd entry. */
6947 long adjust;
6948
6949 if (fdh != NULL)
6950 {
6951 /* Redefine the function descriptor symbol to
6952 this location in the opd section. It is
6953 necessary to update the value here rather
6954 than using an array of adjustments as we do
6955 for local symbols, because various places
6956 in the generic ELF code use the value
6957 stored in u.def.value. */
6958 fdh->elf.root.u.def.value = wptr - new_contents;
6959 fdh->adjust_done = 1;
6960 }
6961
6962 /* Local syms are a bit tricky. We could
6963 tweak them as they can be cached, but
6964 we'd need to look through the local syms
6965 for the function descriptor sym which we
6966 don't have at the moment. So keep an
6967 array of adjustments. */
6968 adjust = (wptr - new_contents) - (rptr - sec->contents);
6969 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
6970
6971 if (wptr != rptr)
6972 memcpy (wptr, rptr, opd_ent_size);
6973 wptr += opd_ent_size;
6974 if (add_aux_fields && opd_ent_size == 16)
6975 {
6976 memset (wptr, '\0', 8);
6977 wptr += 8;
6978 }
6979
6980 /* We need to adjust any reloc offsets to point to the
6981 new opd entries. */
6982 for ( ; rel != next_rel; ++rel)
6983 {
6984 rel->r_offset += adjust;
6985 if (write_rel != rel)
6986 memcpy (write_rel, rel, sizeof (*rel));
6987 ++write_rel;
6988 }
6989 }
6990
6991 rptr += opd_ent_size;
6992 }
6993
6994 sec->size = wptr - new_contents;
6995 sec->reloc_count = write_rel - relstart;
6996 if (add_aux_fields)
6997 {
6998 free (sec->contents);
6999 sec->contents = new_contents;
7000 }
7001
7002 /* Fudge the header size too, as this is used later in
7003 elf_bfd_final_link if we are emitting relocs. */
7004 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7005 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7006 some_edited = TRUE;
7007 }
7008 else if (elf_section_data (sec)->relocs != relstart)
7009 free (relstart);
7010
7011 if (local_syms != NULL
7012 && symtab_hdr->contents != (unsigned char *) local_syms)
7013 {
7014 if (!info->keep_memory)
7015 free (local_syms);
7016 else
7017 symtab_hdr->contents = (unsigned char *) local_syms;
7018 }
7019 }
7020
7021 if (some_edited)
7022 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7023
7024 /* If we are doing a final link and the last .opd entry is just 16 byte
7025 long, add a 8 byte padding after it. */
7026 if (need_pad != NULL && !bfd_link_relocatable (info))
7027 {
7028 bfd_byte *p;
7029
7030 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7031 {
7032 BFD_ASSERT (need_pad->size > 0);
7033
7034 p = bfd_malloc (need_pad->size + 8);
7035 if (p == NULL)
7036 return FALSE;
7037
7038 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7039 p, 0, need_pad->size))
7040 return FALSE;
7041
7042 need_pad->contents = p;
7043 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7044 }
7045 else
7046 {
7047 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7048 if (p == NULL)
7049 return FALSE;
7050
7051 need_pad->contents = p;
7052 }
7053
7054 memset (need_pad->contents + need_pad->size, 0, 8);
7055 need_pad->size += 8;
7056 }
7057
7058 return TRUE;
7059 }
7060
7061 /* Analyze inline PLT call relocations to see whether calls to locally
7062 defined functions can be converted to direct calls. */
7063
7064 bfd_boolean
7065 ppc64_elf_inline_plt (struct bfd_link_info *info)
7066 {
7067 struct ppc_link_hash_table *htab;
7068 bfd *ibfd;
7069 asection *sec;
7070 bfd_vma low_vma, high_vma, limit;
7071
7072 htab = ppc_hash_table (info);
7073 if (htab == NULL)
7074 return FALSE;
7075
7076 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7077 reduced somewhat to cater for possible stubs that might be added
7078 between the call and its destination. */
7079 if (htab->params->group_size < 0)
7080 {
7081 limit = -htab->params->group_size;
7082 if (limit == 1)
7083 limit = 0x1e00000;
7084 }
7085 else
7086 {
7087 limit = htab->params->group_size;
7088 if (limit == 1)
7089 limit = 0x1c00000;
7090 }
7091
7092 low_vma = -1;
7093 high_vma = 0;
7094 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7095 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7096 {
7097 if (low_vma > sec->vma)
7098 low_vma = sec->vma;
7099 if (high_vma < sec->vma + sec->size)
7100 high_vma = sec->vma + sec->size;
7101 }
7102
7103 /* If a "bl" can reach anywhere in local code sections, then we can
7104 convert all inline PLT sequences to direct calls when the symbol
7105 is local. */
7106 if (high_vma - low_vma < limit)
7107 {
7108 htab->can_convert_all_inline_plt = 1;
7109 return TRUE;
7110 }
7111
7112 /* Otherwise, go looking through relocs for cases where a direct
7113 call won't reach. Mark the symbol on any such reloc to disable
7114 the optimization and keep the PLT entry as it seems likely that
7115 this will be better than creating trampolines. Note that this
7116 will disable the optimization for all inline PLT calls to a
7117 particular symbol, not just those that won't reach. The
7118 difficulty in doing a more precise optimization is that the
7119 linker needs to make a decision depending on whether a
7120 particular R_PPC64_PLTCALL insn can be turned into a direct
7121 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7122 the sequence, and there is nothing that ties those relocs
7123 together except their symbol. */
7124
7125 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7126 {
7127 Elf_Internal_Shdr *symtab_hdr;
7128 Elf_Internal_Sym *local_syms;
7129
7130 if (!is_ppc64_elf (ibfd))
7131 continue;
7132
7133 local_syms = NULL;
7134 symtab_hdr = &elf_symtab_hdr (ibfd);
7135
7136 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7137 if (ppc64_elf_section_data (sec)->has_pltcall
7138 && !bfd_is_abs_section (sec->output_section))
7139 {
7140 Elf_Internal_Rela *relstart, *rel, *relend;
7141
7142 /* Read the relocations. */
7143 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7144 info->keep_memory);
7145 if (relstart == NULL)
7146 return FALSE;
7147
7148 relend = relstart + sec->reloc_count;
7149 for (rel = relstart; rel < relend; )
7150 {
7151 enum elf_ppc64_reloc_type r_type;
7152 unsigned long r_symndx;
7153 asection *sym_sec;
7154 struct elf_link_hash_entry *h;
7155 Elf_Internal_Sym *sym;
7156 unsigned char *tls_maskp;
7157
7158 r_type = ELF64_R_TYPE (rel->r_info);
7159 if (r_type != R_PPC64_PLTCALL)
7160 continue;
7161
7162 r_symndx = ELF64_R_SYM (rel->r_info);
7163 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7164 r_symndx, ibfd))
7165 {
7166 if (elf_section_data (sec)->relocs != relstart)
7167 free (relstart);
7168 if (local_syms != NULL
7169 && symtab_hdr->contents != (bfd_byte *) local_syms)
7170 free (local_syms);
7171 return FALSE;
7172 }
7173
7174 if (sym_sec != NULL && sym_sec->output_section != NULL)
7175 {
7176 bfd_vma from, to;
7177 if (h != NULL)
7178 to = h->root.u.def.value;
7179 else
7180 to = sym->st_value;
7181 to += (rel->r_addend
7182 + sym_sec->output_offset
7183 + sym_sec->output_section->vma);
7184 from = (rel->r_offset
7185 + sec->output_offset
7186 + sec->output_section->vma);
7187 if (to - from + limit < 2 * limit)
7188 *tls_maskp &= ~PLT_KEEP;
7189 }
7190 }
7191 if (elf_section_data (sec)->relocs != relstart)
7192 free (relstart);
7193 }
7194
7195 if (local_syms != NULL
7196 && symtab_hdr->contents != (unsigned char *) local_syms)
7197 {
7198 if (!info->keep_memory)
7199 free (local_syms);
7200 else
7201 symtab_hdr->contents = (unsigned char *) local_syms;
7202 }
7203 }
7204
7205 return TRUE;
7206 }
7207
7208 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7209
7210 asection *
7211 ppc64_elf_tls_setup (struct bfd_link_info *info)
7212 {
7213 struct ppc_link_hash_table *htab;
7214
7215 htab = ppc_hash_table (info);
7216 if (htab == NULL)
7217 return NULL;
7218
7219 if (abiversion (info->output_bfd) == 1)
7220 htab->opd_abi = 1;
7221
7222 if (htab->params->no_multi_toc)
7223 htab->do_multi_toc = 0;
7224 else if (!htab->do_multi_toc)
7225 htab->params->no_multi_toc = 1;
7226
7227 /* Default to --no-plt-localentry, as this option can cause problems
7228 with symbol interposition. For example, glibc libpthread.so and
7229 libc.so duplicate many pthread symbols, with a fallback
7230 implementation in libc.so. In some cases the fallback does more
7231 work than the pthread implementation. __pthread_condattr_destroy
7232 is one such symbol: the libpthread.so implementation is
7233 localentry:0 while the libc.so implementation is localentry:8.
7234 An app that "cleverly" uses dlopen to only load necessary
7235 libraries at runtime may omit loading libpthread.so when not
7236 running multi-threaded, which then results in the libc.so
7237 fallback symbols being used and ld.so complaining. Now there
7238 are workarounds in ld (see non_zero_localentry) to detect the
7239 pthread situation, but that may not be the only case where
7240 --plt-localentry can cause trouble. */
7241 if (htab->params->plt_localentry0 < 0)
7242 htab->params->plt_localentry0 = 0;
7243 if (htab->params->plt_localentry0
7244 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7245 FALSE, FALSE, FALSE) == NULL)
7246 _bfd_error_handler
7247 (_("warning: --plt-localentry is especially dangerous without "
7248 "ld.so support to detect ABI violations"));
7249
7250 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7251 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7252 FALSE, FALSE, TRUE));
7253 /* Move dynamic linking info to the function descriptor sym. */
7254 if (htab->tls_get_addr != NULL)
7255 func_desc_adjust (&htab->tls_get_addr->elf, info);
7256 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7257 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7258 FALSE, FALSE, TRUE));
7259 if (htab->params->tls_get_addr_opt)
7260 {
7261 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7262
7263 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7264 FALSE, FALSE, TRUE);
7265 if (opt != NULL)
7266 func_desc_adjust (opt, info);
7267 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7268 FALSE, FALSE, TRUE);
7269 if (opt_fd != NULL
7270 && (opt_fd->root.type == bfd_link_hash_defined
7271 || opt_fd->root.type == bfd_link_hash_defweak))
7272 {
7273 /* If glibc supports an optimized __tls_get_addr call stub,
7274 signalled by the presence of __tls_get_addr_opt, and we'll
7275 be calling __tls_get_addr via a plt call stub, then
7276 make __tls_get_addr point to __tls_get_addr_opt. */
7277 tga_fd = &htab->tls_get_addr_fd->elf;
7278 if (htab->elf.dynamic_sections_created
7279 && tga_fd != NULL
7280 && (tga_fd->type == STT_FUNC
7281 || tga_fd->needs_plt)
7282 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7283 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
7284 {
7285 struct plt_entry *ent;
7286
7287 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7288 if (ent->plt.refcount > 0)
7289 break;
7290 if (ent != NULL)
7291 {
7292 tga_fd->root.type = bfd_link_hash_indirect;
7293 tga_fd->root.u.i.link = &opt_fd->root;
7294 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7295 opt_fd->mark = 1;
7296 if (opt_fd->dynindx != -1)
7297 {
7298 /* Use __tls_get_addr_opt in dynamic relocations. */
7299 opt_fd->dynindx = -1;
7300 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7301 opt_fd->dynstr_index);
7302 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7303 return NULL;
7304 }
7305 htab->tls_get_addr_fd
7306 = (struct ppc_link_hash_entry *) opt_fd;
7307 tga = &htab->tls_get_addr->elf;
7308 if (opt != NULL && tga != NULL)
7309 {
7310 tga->root.type = bfd_link_hash_indirect;
7311 tga->root.u.i.link = &opt->root;
7312 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7313 opt->mark = 1;
7314 _bfd_elf_link_hash_hide_symbol (info, opt,
7315 tga->forced_local);
7316 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7317 }
7318 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7319 htab->tls_get_addr_fd->is_func_descriptor = 1;
7320 if (htab->tls_get_addr != NULL)
7321 {
7322 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7323 htab->tls_get_addr->is_func = 1;
7324 }
7325 }
7326 }
7327 }
7328 else if (htab->params->tls_get_addr_opt < 0)
7329 htab->params->tls_get_addr_opt = 0;
7330 }
7331 return _bfd_elf_tls_setup (info->output_bfd, info);
7332 }
7333
7334 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7335 HASH1 or HASH2. */
7336
7337 static bfd_boolean
7338 branch_reloc_hash_match (const bfd *ibfd,
7339 const Elf_Internal_Rela *rel,
7340 const struct ppc_link_hash_entry *hash1,
7341 const struct ppc_link_hash_entry *hash2)
7342 {
7343 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7344 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7345 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7346
7347 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7348 {
7349 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7350 struct elf_link_hash_entry *h;
7351
7352 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7353 h = elf_follow_link (h);
7354 if (h == &hash1->elf || h == &hash2->elf)
7355 return TRUE;
7356 }
7357 return FALSE;
7358 }
7359
7360 /* Run through all the TLS relocs looking for optimization
7361 opportunities. The linker has been hacked (see ppc64elf.em) to do
7362 a preliminary section layout so that we know the TLS segment
7363 offsets. We can't optimize earlier because some optimizations need
7364 to know the tp offset, and we need to optimize before allocating
7365 dynamic relocations. */
7366
7367 bfd_boolean
7368 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7369 {
7370 bfd *ibfd;
7371 asection *sec;
7372 struct ppc_link_hash_table *htab;
7373 unsigned char *toc_ref;
7374 int pass;
7375
7376 if (!bfd_link_executable (info))
7377 return TRUE;
7378
7379 htab = ppc_hash_table (info);
7380 if (htab == NULL)
7381 return FALSE;
7382
7383 /* Make two passes over the relocs. On the first pass, mark toc
7384 entries involved with tls relocs, and check that tls relocs
7385 involved in setting up a tls_get_addr call are indeed followed by
7386 such a call. If they are not, we can't do any tls optimization.
7387 On the second pass twiddle tls_mask flags to notify
7388 relocate_section that optimization can be done, and adjust got
7389 and plt refcounts. */
7390 toc_ref = NULL;
7391 for (pass = 0; pass < 2; ++pass)
7392 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7393 {
7394 Elf_Internal_Sym *locsyms = NULL;
7395 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7396
7397 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7398 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7399 {
7400 Elf_Internal_Rela *relstart, *rel, *relend;
7401 bfd_boolean found_tls_get_addr_arg = 0;
7402
7403 /* Read the relocations. */
7404 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7405 info->keep_memory);
7406 if (relstart == NULL)
7407 {
7408 free (toc_ref);
7409 return FALSE;
7410 }
7411
7412 relend = relstart + sec->reloc_count;
7413 for (rel = relstart; rel < relend; rel++)
7414 {
7415 enum elf_ppc64_reloc_type r_type;
7416 unsigned long r_symndx;
7417 struct elf_link_hash_entry *h;
7418 Elf_Internal_Sym *sym;
7419 asection *sym_sec;
7420 unsigned char *tls_mask;
7421 unsigned char tls_set, tls_clear, tls_type = 0;
7422 bfd_vma value;
7423 bfd_boolean ok_tprel, is_local;
7424 long toc_ref_index = 0;
7425 int expecting_tls_get_addr = 0;
7426 bfd_boolean ret = FALSE;
7427
7428 r_symndx = ELF64_R_SYM (rel->r_info);
7429 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7430 r_symndx, ibfd))
7431 {
7432 err_free_rel:
7433 if (elf_section_data (sec)->relocs != relstart)
7434 free (relstart);
7435 if (toc_ref != NULL)
7436 free (toc_ref);
7437 if (locsyms != NULL
7438 && (elf_symtab_hdr (ibfd).contents
7439 != (unsigned char *) locsyms))
7440 free (locsyms);
7441 return ret;
7442 }
7443
7444 if (h != NULL)
7445 {
7446 if (h->root.type == bfd_link_hash_defined
7447 || h->root.type == bfd_link_hash_defweak)
7448 value = h->root.u.def.value;
7449 else if (h->root.type == bfd_link_hash_undefweak)
7450 value = 0;
7451 else
7452 {
7453 found_tls_get_addr_arg = 0;
7454 continue;
7455 }
7456 }
7457 else
7458 /* Symbols referenced by TLS relocs must be of type
7459 STT_TLS. So no need for .opd local sym adjust. */
7460 value = sym->st_value;
7461
7462 ok_tprel = FALSE;
7463 is_local = FALSE;
7464 if (h == NULL
7465 || !h->def_dynamic)
7466 {
7467 is_local = TRUE;
7468 if (h != NULL
7469 && h->root.type == bfd_link_hash_undefweak)
7470 ok_tprel = TRUE;
7471 else if (sym_sec != NULL
7472 && sym_sec->output_section != NULL)
7473 {
7474 value += sym_sec->output_offset;
7475 value += sym_sec->output_section->vma;
7476 value -= htab->elf.tls_sec->vma;
7477 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7478 < (bfd_vma) 1 << 32);
7479 }
7480 }
7481
7482 r_type = ELF64_R_TYPE (rel->r_info);
7483 /* If this section has old-style __tls_get_addr calls
7484 without marker relocs, then check that each
7485 __tls_get_addr call reloc is preceded by a reloc
7486 that conceivably belongs to the __tls_get_addr arg
7487 setup insn. If we don't find matching arg setup
7488 relocs, don't do any tls optimization. */
7489 if (pass == 0
7490 && sec->has_tls_get_addr_call
7491 && h != NULL
7492 && (h == &htab->tls_get_addr->elf
7493 || h == &htab->tls_get_addr_fd->elf)
7494 && !found_tls_get_addr_arg
7495 && is_branch_reloc (r_type))
7496 {
7497 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7498 "TLS optimization disabled\n"),
7499 ibfd, sec, rel->r_offset);
7500 ret = TRUE;
7501 goto err_free_rel;
7502 }
7503
7504 found_tls_get_addr_arg = 0;
7505 switch (r_type)
7506 {
7507 case R_PPC64_GOT_TLSLD16:
7508 case R_PPC64_GOT_TLSLD16_LO:
7509 expecting_tls_get_addr = 1;
7510 found_tls_get_addr_arg = 1;
7511 /* Fall through. */
7512
7513 case R_PPC64_GOT_TLSLD16_HI:
7514 case R_PPC64_GOT_TLSLD16_HA:
7515 /* These relocs should never be against a symbol
7516 defined in a shared lib. Leave them alone if
7517 that turns out to be the case. */
7518 if (!is_local)
7519 continue;
7520
7521 /* LD -> LE */
7522 tls_set = 0;
7523 tls_clear = TLS_LD;
7524 tls_type = TLS_TLS | TLS_LD;
7525 break;
7526
7527 case R_PPC64_GOT_TLSGD16:
7528 case R_PPC64_GOT_TLSGD16_LO:
7529 expecting_tls_get_addr = 1;
7530 found_tls_get_addr_arg = 1;
7531 /* Fall through. */
7532
7533 case R_PPC64_GOT_TLSGD16_HI:
7534 case R_PPC64_GOT_TLSGD16_HA:
7535 if (ok_tprel)
7536 /* GD -> LE */
7537 tls_set = 0;
7538 else
7539 /* GD -> IE */
7540 tls_set = TLS_TLS | TLS_TPRELGD;
7541 tls_clear = TLS_GD;
7542 tls_type = TLS_TLS | TLS_GD;
7543 break;
7544
7545 case R_PPC64_GOT_TPREL16_DS:
7546 case R_PPC64_GOT_TPREL16_LO_DS:
7547 case R_PPC64_GOT_TPREL16_HI:
7548 case R_PPC64_GOT_TPREL16_HA:
7549 if (ok_tprel)
7550 {
7551 /* IE -> LE */
7552 tls_set = 0;
7553 tls_clear = TLS_TPREL;
7554 tls_type = TLS_TLS | TLS_TPREL;
7555 break;
7556 }
7557 continue;
7558
7559 case R_PPC64_TLSGD:
7560 case R_PPC64_TLSLD:
7561 if (rel + 1 < relend
7562 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7563 {
7564 if (pass != 0
7565 && (ELF64_R_TYPE (rel[1].r_info)
7566 != R_PPC64_PLTSEQ))
7567 {
7568 r_symndx = ELF64_R_SYM (rel[1].r_info);
7569 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
7570 r_symndx, ibfd))
7571 goto err_free_rel;
7572 if (h != NULL)
7573 {
7574 struct plt_entry *ent = NULL;
7575
7576 for (ent = h->plt.plist;
7577 ent != NULL;
7578 ent = ent->next)
7579 if (ent->addend == rel[1].r_addend)
7580 break;
7581
7582 if (ent != NULL
7583 && ent->plt.refcount > 0)
7584 ent->plt.refcount -= 1;
7585 }
7586 }
7587 continue;
7588 }
7589 found_tls_get_addr_arg = 1;
7590 /* Fall through. */
7591
7592 case R_PPC64_TLS:
7593 case R_PPC64_TOC16:
7594 case R_PPC64_TOC16_LO:
7595 if (sym_sec == NULL || sym_sec != toc)
7596 continue;
7597
7598 /* Mark this toc entry as referenced by a TLS
7599 code sequence. We can do that now in the
7600 case of R_PPC64_TLS, and after checking for
7601 tls_get_addr for the TOC16 relocs. */
7602 if (toc_ref == NULL)
7603 toc_ref
7604 = bfd_zmalloc (toc->output_section->rawsize / 8);
7605 if (toc_ref == NULL)
7606 goto err_free_rel;
7607
7608 if (h != NULL)
7609 value = h->root.u.def.value;
7610 else
7611 value = sym->st_value;
7612 value += rel->r_addend;
7613 if (value % 8 != 0)
7614 continue;
7615 BFD_ASSERT (value < toc->size
7616 && toc->output_offset % 8 == 0);
7617 toc_ref_index = (value + toc->output_offset) / 8;
7618 if (r_type == R_PPC64_TLS
7619 || r_type == R_PPC64_TLSGD
7620 || r_type == R_PPC64_TLSLD)
7621 {
7622 toc_ref[toc_ref_index] = 1;
7623 continue;
7624 }
7625
7626 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7627 continue;
7628
7629 tls_set = 0;
7630 tls_clear = 0;
7631 expecting_tls_get_addr = 2;
7632 break;
7633
7634 case R_PPC64_TPREL64:
7635 if (pass == 0
7636 || sec != toc
7637 || toc_ref == NULL
7638 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7639 continue;
7640 if (ok_tprel)
7641 {
7642 /* IE -> LE */
7643 tls_set = TLS_EXPLICIT;
7644 tls_clear = TLS_TPREL;
7645 break;
7646 }
7647 continue;
7648
7649 case R_PPC64_DTPMOD64:
7650 if (pass == 0
7651 || sec != toc
7652 || toc_ref == NULL
7653 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7654 continue;
7655 if (rel + 1 < relend
7656 && (rel[1].r_info
7657 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7658 && rel[1].r_offset == rel->r_offset + 8)
7659 {
7660 if (ok_tprel)
7661 /* GD -> LE */
7662 tls_set = TLS_EXPLICIT | TLS_GD;
7663 else
7664 /* GD -> IE */
7665 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7666 tls_clear = TLS_GD;
7667 }
7668 else
7669 {
7670 if (!is_local)
7671 continue;
7672
7673 /* LD -> LE */
7674 tls_set = TLS_EXPLICIT;
7675 tls_clear = TLS_LD;
7676 }
7677 break;
7678
7679 default:
7680 continue;
7681 }
7682
7683 if (pass == 0)
7684 {
7685 if (!expecting_tls_get_addr
7686 || !sec->has_tls_get_addr_call)
7687 continue;
7688
7689 if (rel + 1 < relend
7690 && branch_reloc_hash_match (ibfd, rel + 1,
7691 htab->tls_get_addr,
7692 htab->tls_get_addr_fd))
7693 {
7694 if (expecting_tls_get_addr == 2)
7695 {
7696 /* Check for toc tls entries. */
7697 unsigned char *toc_tls;
7698 int retval;
7699
7700 retval = get_tls_mask (&toc_tls, NULL, NULL,
7701 &locsyms,
7702 rel, ibfd);
7703 if (retval == 0)
7704 goto err_free_rel;
7705 if (toc_tls != NULL)
7706 {
7707 if ((*toc_tls & TLS_TLS) != 0
7708 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
7709 found_tls_get_addr_arg = 1;
7710 if (retval > 1)
7711 toc_ref[toc_ref_index] = 1;
7712 }
7713 }
7714 continue;
7715 }
7716
7717 /* Uh oh, we didn't find the expected call. We
7718 could just mark this symbol to exclude it
7719 from tls optimization but it's safer to skip
7720 the entire optimization. */
7721 /* xgettext:c-format */
7722 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
7723 "TLS optimization disabled\n"),
7724 ibfd, sec, rel->r_offset);
7725 ret = TRUE;
7726 goto err_free_rel;
7727 }
7728
7729 /* If we don't have old-style __tls_get_addr calls
7730 without TLSGD/TLSLD marker relocs, and we haven't
7731 found a new-style __tls_get_addr call with a
7732 marker for this symbol, then we either have a
7733 broken object file or an -mlongcall style
7734 indirect call to __tls_get_addr without a marker.
7735 Disable optimization in this case. */
7736 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
7737 && (tls_set & TLS_EXPLICIT) == 0
7738 && !sec->has_tls_get_addr_call
7739 && ((*tls_mask & (TLS_TLS | TLS_MARK))
7740 != (TLS_TLS | TLS_MARK)))
7741 continue;
7742
7743 if (expecting_tls_get_addr)
7744 {
7745 struct plt_entry *ent = NULL;
7746
7747 if (htab->tls_get_addr != NULL)
7748 for (ent = htab->tls_get_addr->elf.plt.plist;
7749 ent != NULL;
7750 ent = ent->next)
7751 if (ent->addend == 0)
7752 break;
7753
7754 if (ent == NULL && htab->tls_get_addr_fd != NULL)
7755 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7756 ent != NULL;
7757 ent = ent->next)
7758 if (ent->addend == 0)
7759 break;
7760
7761 if (ent != NULL
7762 && ent->plt.refcount > 0)
7763 ent->plt.refcount -= 1;
7764 }
7765
7766 if (tls_clear == 0)
7767 continue;
7768
7769 if ((tls_set & TLS_EXPLICIT) == 0)
7770 {
7771 struct got_entry *ent;
7772
7773 /* Adjust got entry for this reloc. */
7774 if (h != NULL)
7775 ent = h->got.glist;
7776 else
7777 ent = elf_local_got_ents (ibfd)[r_symndx];
7778
7779 for (; ent != NULL; ent = ent->next)
7780 if (ent->addend == rel->r_addend
7781 && ent->owner == ibfd
7782 && ent->tls_type == tls_type)
7783 break;
7784 if (ent == NULL)
7785 abort ();
7786
7787 if (tls_set == 0)
7788 {
7789 /* We managed to get rid of a got entry. */
7790 if (ent->got.refcount > 0)
7791 ent->got.refcount -= 1;
7792 }
7793 }
7794 else
7795 {
7796 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7797 we'll lose one or two dyn relocs. */
7798 if (!dec_dynrel_count (rel->r_info, sec, info,
7799 NULL, h, sym))
7800 return FALSE;
7801
7802 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7803 {
7804 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7805 NULL, h, sym))
7806 return FALSE;
7807 }
7808 }
7809
7810 *tls_mask |= tls_set;
7811 *tls_mask &= ~tls_clear;
7812 }
7813
7814 if (elf_section_data (sec)->relocs != relstart)
7815 free (relstart);
7816 }
7817
7818 if (locsyms != NULL
7819 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7820 {
7821 if (!info->keep_memory)
7822 free (locsyms);
7823 else
7824 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7825 }
7826 }
7827
7828 if (toc_ref != NULL)
7829 free (toc_ref);
7830 htab->do_tls_opt = 1;
7831 return TRUE;
7832 }
7833
7834 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7835 the values of any global symbols in a toc section that has been
7836 edited. Globals in toc sections should be a rarity, so this function
7837 sets a flag if any are found in toc sections other than the one just
7838 edited, so that further hash table traversals can be avoided. */
7839
7840 struct adjust_toc_info
7841 {
7842 asection *toc;
7843 unsigned long *skip;
7844 bfd_boolean global_toc_syms;
7845 };
7846
7847 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7848
7849 static bfd_boolean
7850 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7851 {
7852 struct ppc_link_hash_entry *eh;
7853 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7854 unsigned long i;
7855
7856 if (h->root.type != bfd_link_hash_defined
7857 && h->root.type != bfd_link_hash_defweak)
7858 return TRUE;
7859
7860 eh = (struct ppc_link_hash_entry *) h;
7861 if (eh->adjust_done)
7862 return TRUE;
7863
7864 if (eh->elf.root.u.def.section == toc_inf->toc)
7865 {
7866 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7867 i = toc_inf->toc->rawsize >> 3;
7868 else
7869 i = eh->elf.root.u.def.value >> 3;
7870
7871 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
7872 {
7873 _bfd_error_handler
7874 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7875 do
7876 ++i;
7877 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
7878 eh->elf.root.u.def.value = (bfd_vma) i << 3;
7879 }
7880
7881 eh->elf.root.u.def.value -= toc_inf->skip[i];
7882 eh->adjust_done = 1;
7883 }
7884 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7885 toc_inf->global_toc_syms = TRUE;
7886
7887 return TRUE;
7888 }
7889
7890 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
7891 on a _LO variety toc/got reloc. */
7892
7893 static bfd_boolean
7894 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
7895 {
7896 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7897 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
7898 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7899 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7900 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7901 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7902 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7903 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7904 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7905 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7906 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7907 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7908 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7909 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7910 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7911 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7912 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7913 /* Exclude lfqu by testing reloc. If relocs are ever
7914 defined for the reduced D field in psq_lu then those
7915 will need testing too. */
7916 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7917 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7918 && (insn & 1) == 0)
7919 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7920 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7921 /* Exclude stfqu. psq_stu as above for psq_lu. */
7922 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7923 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7924 && (insn & 1) == 0));
7925 }
7926
7927 /* Examine all relocs referencing .toc sections in order to remove
7928 unused .toc entries. */
7929
7930 bfd_boolean
7931 ppc64_elf_edit_toc (struct bfd_link_info *info)
7932 {
7933 bfd *ibfd;
7934 struct adjust_toc_info toc_inf;
7935 struct ppc_link_hash_table *htab = ppc_hash_table (info);
7936
7937 htab->do_toc_opt = 1;
7938 toc_inf.global_toc_syms = TRUE;
7939 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7940 {
7941 asection *toc, *sec;
7942 Elf_Internal_Shdr *symtab_hdr;
7943 Elf_Internal_Sym *local_syms;
7944 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
7945 unsigned long *skip, *drop;
7946 unsigned char *used;
7947 unsigned char *keep, last, some_unused;
7948
7949 if (!is_ppc64_elf (ibfd))
7950 continue;
7951
7952 toc = bfd_get_section_by_name (ibfd, ".toc");
7953 if (toc == NULL
7954 || toc->size == 0
7955 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
7956 || discarded_section (toc))
7957 continue;
7958
7959 toc_relocs = NULL;
7960 local_syms = NULL;
7961 symtab_hdr = &elf_symtab_hdr (ibfd);
7962
7963 /* Look at sections dropped from the final link. */
7964 skip = NULL;
7965 relstart = NULL;
7966 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7967 {
7968 if (sec->reloc_count == 0
7969 || !discarded_section (sec)
7970 || get_opd_info (sec)
7971 || (sec->flags & SEC_ALLOC) == 0
7972 || (sec->flags & SEC_DEBUGGING) != 0)
7973 continue;
7974
7975 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7976 if (relstart == NULL)
7977 goto error_ret;
7978
7979 /* Run through the relocs to see which toc entries might be
7980 unused. */
7981 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7982 {
7983 enum elf_ppc64_reloc_type r_type;
7984 unsigned long r_symndx;
7985 asection *sym_sec;
7986 struct elf_link_hash_entry *h;
7987 Elf_Internal_Sym *sym;
7988 bfd_vma val;
7989
7990 r_type = ELF64_R_TYPE (rel->r_info);
7991 switch (r_type)
7992 {
7993 default:
7994 continue;
7995
7996 case R_PPC64_TOC16:
7997 case R_PPC64_TOC16_LO:
7998 case R_PPC64_TOC16_HI:
7999 case R_PPC64_TOC16_HA:
8000 case R_PPC64_TOC16_DS:
8001 case R_PPC64_TOC16_LO_DS:
8002 break;
8003 }
8004
8005 r_symndx = ELF64_R_SYM (rel->r_info);
8006 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8007 r_symndx, ibfd))
8008 goto error_ret;
8009
8010 if (sym_sec != toc)
8011 continue;
8012
8013 if (h != NULL)
8014 val = h->root.u.def.value;
8015 else
8016 val = sym->st_value;
8017 val += rel->r_addend;
8018
8019 if (val >= toc->size)
8020 continue;
8021
8022 /* Anything in the toc ought to be aligned to 8 bytes.
8023 If not, don't mark as unused. */
8024 if (val & 7)
8025 continue;
8026
8027 if (skip == NULL)
8028 {
8029 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8030 if (skip == NULL)
8031 goto error_ret;
8032 }
8033
8034 skip[val >> 3] = ref_from_discarded;
8035 }
8036
8037 if (elf_section_data (sec)->relocs != relstart)
8038 free (relstart);
8039 }
8040
8041 /* For largetoc loads of address constants, we can convert
8042 . addis rx,2,addr@got@ha
8043 . ld ry,addr@got@l(rx)
8044 to
8045 . addis rx,2,addr@toc@ha
8046 . addi ry,rx,addr@toc@l
8047 when addr is within 2G of the toc pointer. This then means
8048 that the word storing "addr" in the toc is no longer needed. */
8049
8050 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8051 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8052 && toc->reloc_count != 0)
8053 {
8054 /* Read toc relocs. */
8055 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8056 info->keep_memory);
8057 if (toc_relocs == NULL)
8058 goto error_ret;
8059
8060 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8061 {
8062 enum elf_ppc64_reloc_type r_type;
8063 unsigned long r_symndx;
8064 asection *sym_sec;
8065 struct elf_link_hash_entry *h;
8066 Elf_Internal_Sym *sym;
8067 bfd_vma val, addr;
8068
8069 r_type = ELF64_R_TYPE (rel->r_info);
8070 if (r_type != R_PPC64_ADDR64)
8071 continue;
8072
8073 r_symndx = ELF64_R_SYM (rel->r_info);
8074 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8075 r_symndx, ibfd))
8076 goto error_ret;
8077
8078 if (sym_sec == NULL
8079 || sym_sec->output_section == NULL
8080 || discarded_section (sym_sec))
8081 continue;
8082
8083 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8084 continue;
8085
8086 if (h != NULL)
8087 {
8088 if (h->type == STT_GNU_IFUNC)
8089 continue;
8090 val = h->root.u.def.value;
8091 }
8092 else
8093 {
8094 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8095 continue;
8096 val = sym->st_value;
8097 }
8098 val += rel->r_addend;
8099 val += sym_sec->output_section->vma + sym_sec->output_offset;
8100
8101 /* We don't yet know the exact toc pointer value, but we
8102 know it will be somewhere in the toc section. Don't
8103 optimize if the difference from any possible toc
8104 pointer is outside [ff..f80008000, 7fff7fff]. */
8105 addr = toc->output_section->vma + TOC_BASE_OFF;
8106 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8107 continue;
8108
8109 addr = toc->output_section->vma + toc->output_section->rawsize;
8110 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8111 continue;
8112
8113 if (skip == NULL)
8114 {
8115 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8116 if (skip == NULL)
8117 goto error_ret;
8118 }
8119
8120 skip[rel->r_offset >> 3]
8121 |= can_optimize | ((rel - toc_relocs) << 2);
8122 }
8123 }
8124
8125 if (skip == NULL)
8126 continue;
8127
8128 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8129 if (used == NULL)
8130 {
8131 error_ret:
8132 if (local_syms != NULL
8133 && symtab_hdr->contents != (unsigned char *) local_syms)
8134 free (local_syms);
8135 if (sec != NULL
8136 && relstart != NULL
8137 && elf_section_data (sec)->relocs != relstart)
8138 free (relstart);
8139 if (toc_relocs != NULL
8140 && elf_section_data (toc)->relocs != toc_relocs)
8141 free (toc_relocs);
8142 if (skip != NULL)
8143 free (skip);
8144 return FALSE;
8145 }
8146
8147 /* Now check all kept sections that might reference the toc.
8148 Check the toc itself last. */
8149 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8150 : ibfd->sections);
8151 sec != NULL;
8152 sec = (sec == toc ? NULL
8153 : sec->next == NULL ? toc
8154 : sec->next == toc && toc->next ? toc->next
8155 : sec->next))
8156 {
8157 int repeat;
8158
8159 if (sec->reloc_count == 0
8160 || discarded_section (sec)
8161 || get_opd_info (sec)
8162 || (sec->flags & SEC_ALLOC) == 0
8163 || (sec->flags & SEC_DEBUGGING) != 0)
8164 continue;
8165
8166 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8167 info->keep_memory);
8168 if (relstart == NULL)
8169 {
8170 free (used);
8171 goto error_ret;
8172 }
8173
8174 /* Mark toc entries referenced as used. */
8175 do
8176 {
8177 repeat = 0;
8178 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8179 {
8180 enum elf_ppc64_reloc_type r_type;
8181 unsigned long r_symndx;
8182 asection *sym_sec;
8183 struct elf_link_hash_entry *h;
8184 Elf_Internal_Sym *sym;
8185 bfd_vma val;
8186 enum {no_check, check_lo, check_ha} insn_check;
8187
8188 r_type = ELF64_R_TYPE (rel->r_info);
8189 switch (r_type)
8190 {
8191 default:
8192 insn_check = no_check;
8193 break;
8194
8195 case R_PPC64_GOT_TLSLD16_HA:
8196 case R_PPC64_GOT_TLSGD16_HA:
8197 case R_PPC64_GOT_TPREL16_HA:
8198 case R_PPC64_GOT_DTPREL16_HA:
8199 case R_PPC64_GOT16_HA:
8200 case R_PPC64_TOC16_HA:
8201 insn_check = check_ha;
8202 break;
8203
8204 case R_PPC64_GOT_TLSLD16_LO:
8205 case R_PPC64_GOT_TLSGD16_LO:
8206 case R_PPC64_GOT_TPREL16_LO_DS:
8207 case R_PPC64_GOT_DTPREL16_LO_DS:
8208 case R_PPC64_GOT16_LO:
8209 case R_PPC64_GOT16_LO_DS:
8210 case R_PPC64_TOC16_LO:
8211 case R_PPC64_TOC16_LO_DS:
8212 insn_check = check_lo;
8213 break;
8214 }
8215
8216 if (insn_check != no_check)
8217 {
8218 bfd_vma off = rel->r_offset & ~3;
8219 unsigned char buf[4];
8220 unsigned int insn;
8221
8222 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8223 {
8224 free (used);
8225 goto error_ret;
8226 }
8227 insn = bfd_get_32 (ibfd, buf);
8228 if (insn_check == check_lo
8229 ? !ok_lo_toc_insn (insn, r_type)
8230 : ((insn & ((0x3f << 26) | 0x1f << 16))
8231 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8232 {
8233 char str[12];
8234
8235 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8236 sprintf (str, "%#08x", insn);
8237 info->callbacks->einfo
8238 /* xgettext:c-format */
8239 (_("%H: toc optimization is not supported for"
8240 " %s instruction\n"),
8241 ibfd, sec, rel->r_offset & ~3, str);
8242 }
8243 }
8244
8245 switch (r_type)
8246 {
8247 case R_PPC64_TOC16:
8248 case R_PPC64_TOC16_LO:
8249 case R_PPC64_TOC16_HI:
8250 case R_PPC64_TOC16_HA:
8251 case R_PPC64_TOC16_DS:
8252 case R_PPC64_TOC16_LO_DS:
8253 /* In case we're taking addresses of toc entries. */
8254 case R_PPC64_ADDR64:
8255 break;
8256
8257 default:
8258 continue;
8259 }
8260
8261 r_symndx = ELF64_R_SYM (rel->r_info);
8262 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8263 r_symndx, ibfd))
8264 {
8265 free (used);
8266 goto error_ret;
8267 }
8268
8269 if (sym_sec != toc)
8270 continue;
8271
8272 if (h != NULL)
8273 val = h->root.u.def.value;
8274 else
8275 val = sym->st_value;
8276 val += rel->r_addend;
8277
8278 if (val >= toc->size)
8279 continue;
8280
8281 if ((skip[val >> 3] & can_optimize) != 0)
8282 {
8283 bfd_vma off;
8284 unsigned char opc;
8285
8286 switch (r_type)
8287 {
8288 case R_PPC64_TOC16_HA:
8289 break;
8290
8291 case R_PPC64_TOC16_LO_DS:
8292 off = rel->r_offset;
8293 off += (bfd_big_endian (ibfd) ? -2 : 3);
8294 if (!bfd_get_section_contents (ibfd, sec, &opc,
8295 off, 1))
8296 {
8297 free (used);
8298 goto error_ret;
8299 }
8300 if ((opc & (0x3f << 2)) == (58u << 2))
8301 break;
8302 /* Fall through. */
8303
8304 default:
8305 /* Wrong sort of reloc, or not a ld. We may
8306 as well clear ref_from_discarded too. */
8307 skip[val >> 3] = 0;
8308 }
8309 }
8310
8311 if (sec != toc)
8312 used[val >> 3] = 1;
8313 /* For the toc section, we only mark as used if this
8314 entry itself isn't unused. */
8315 else if ((used[rel->r_offset >> 3]
8316 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8317 && !used[val >> 3])
8318 {
8319 /* Do all the relocs again, to catch reference
8320 chains. */
8321 repeat = 1;
8322 used[val >> 3] = 1;
8323 }
8324 }
8325 }
8326 while (repeat);
8327
8328 if (elf_section_data (sec)->relocs != relstart)
8329 free (relstart);
8330 }
8331
8332 /* Merge the used and skip arrays. Assume that TOC
8333 doublewords not appearing as either used or unused belong
8334 to an entry more than one doubleword in size. */
8335 for (drop = skip, keep = used, last = 0, some_unused = 0;
8336 drop < skip + (toc->size + 7) / 8;
8337 ++drop, ++keep)
8338 {
8339 if (*keep)
8340 {
8341 *drop &= ~ref_from_discarded;
8342 if ((*drop & can_optimize) != 0)
8343 some_unused = 1;
8344 last = 0;
8345 }
8346 else if ((*drop & ref_from_discarded) != 0)
8347 {
8348 some_unused = 1;
8349 last = ref_from_discarded;
8350 }
8351 else
8352 *drop = last;
8353 }
8354
8355 free (used);
8356
8357 if (some_unused)
8358 {
8359 bfd_byte *contents, *src;
8360 unsigned long off;
8361 Elf_Internal_Sym *sym;
8362 bfd_boolean local_toc_syms = FALSE;
8363
8364 /* Shuffle the toc contents, and at the same time convert the
8365 skip array from booleans into offsets. */
8366 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8367 goto error_ret;
8368
8369 elf_section_data (toc)->this_hdr.contents = contents;
8370
8371 for (src = contents, off = 0, drop = skip;
8372 src < contents + toc->size;
8373 src += 8, ++drop)
8374 {
8375 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8376 off += 8;
8377 else if (off != 0)
8378 {
8379 *drop = off;
8380 memcpy (src - off, src, 8);
8381 }
8382 }
8383 *drop = off;
8384 toc->rawsize = toc->size;
8385 toc->size = src - contents - off;
8386
8387 /* Adjust addends for relocs against the toc section sym,
8388 and optimize any accesses we can. */
8389 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8390 {
8391 if (sec->reloc_count == 0
8392 || discarded_section (sec))
8393 continue;
8394
8395 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8396 info->keep_memory);
8397 if (relstart == NULL)
8398 goto error_ret;
8399
8400 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8401 {
8402 enum elf_ppc64_reloc_type r_type;
8403 unsigned long r_symndx;
8404 asection *sym_sec;
8405 struct elf_link_hash_entry *h;
8406 bfd_vma val;
8407
8408 r_type = ELF64_R_TYPE (rel->r_info);
8409 switch (r_type)
8410 {
8411 default:
8412 continue;
8413
8414 case R_PPC64_TOC16:
8415 case R_PPC64_TOC16_LO:
8416 case R_PPC64_TOC16_HI:
8417 case R_PPC64_TOC16_HA:
8418 case R_PPC64_TOC16_DS:
8419 case R_PPC64_TOC16_LO_DS:
8420 case R_PPC64_ADDR64:
8421 break;
8422 }
8423
8424 r_symndx = ELF64_R_SYM (rel->r_info);
8425 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8426 r_symndx, ibfd))
8427 goto error_ret;
8428
8429 if (sym_sec != toc)
8430 continue;
8431
8432 if (h != NULL)
8433 val = h->root.u.def.value;
8434 else
8435 {
8436 val = sym->st_value;
8437 if (val != 0)
8438 local_toc_syms = TRUE;
8439 }
8440
8441 val += rel->r_addend;
8442
8443 if (val > toc->rawsize)
8444 val = toc->rawsize;
8445 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8446 continue;
8447 else if ((skip[val >> 3] & can_optimize) != 0)
8448 {
8449 Elf_Internal_Rela *tocrel
8450 = toc_relocs + (skip[val >> 3] >> 2);
8451 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8452
8453 switch (r_type)
8454 {
8455 case R_PPC64_TOC16_HA:
8456 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8457 break;
8458
8459 case R_PPC64_TOC16_LO_DS:
8460 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8461 break;
8462
8463 default:
8464 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8465 ppc_howto_init ();
8466 info->callbacks->einfo
8467 /* xgettext:c-format */
8468 (_("%H: %s references "
8469 "optimized away TOC entry\n"),
8470 ibfd, sec, rel->r_offset,
8471 ppc64_elf_howto_table[r_type]->name);
8472 bfd_set_error (bfd_error_bad_value);
8473 goto error_ret;
8474 }
8475 rel->r_addend = tocrel->r_addend;
8476 elf_section_data (sec)->relocs = relstart;
8477 continue;
8478 }
8479
8480 if (h != NULL || sym->st_value != 0)
8481 continue;
8482
8483 rel->r_addend -= skip[val >> 3];
8484 elf_section_data (sec)->relocs = relstart;
8485 }
8486
8487 if (elf_section_data (sec)->relocs != relstart)
8488 free (relstart);
8489 }
8490
8491 /* We shouldn't have local or global symbols defined in the TOC,
8492 but handle them anyway. */
8493 if (local_syms != NULL)
8494 for (sym = local_syms;
8495 sym < local_syms + symtab_hdr->sh_info;
8496 ++sym)
8497 if (sym->st_value != 0
8498 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8499 {
8500 unsigned long i;
8501
8502 if (sym->st_value > toc->rawsize)
8503 i = toc->rawsize >> 3;
8504 else
8505 i = sym->st_value >> 3;
8506
8507 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8508 {
8509 if (local_toc_syms)
8510 _bfd_error_handler
8511 (_("%s defined on removed toc entry"),
8512 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8513 do
8514 ++i;
8515 while ((skip[i] & (ref_from_discarded | can_optimize)));
8516 sym->st_value = (bfd_vma) i << 3;
8517 }
8518
8519 sym->st_value -= skip[i];
8520 symtab_hdr->contents = (unsigned char *) local_syms;
8521 }
8522
8523 /* Adjust any global syms defined in this toc input section. */
8524 if (toc_inf.global_toc_syms)
8525 {
8526 toc_inf.toc = toc;
8527 toc_inf.skip = skip;
8528 toc_inf.global_toc_syms = FALSE;
8529 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8530 &toc_inf);
8531 }
8532
8533 if (toc->reloc_count != 0)
8534 {
8535 Elf_Internal_Shdr *rel_hdr;
8536 Elf_Internal_Rela *wrel;
8537 bfd_size_type sz;
8538
8539 /* Remove unused toc relocs, and adjust those we keep. */
8540 if (toc_relocs == NULL)
8541 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8542 info->keep_memory);
8543 if (toc_relocs == NULL)
8544 goto error_ret;
8545
8546 wrel = toc_relocs;
8547 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8548 if ((skip[rel->r_offset >> 3]
8549 & (ref_from_discarded | can_optimize)) == 0)
8550 {
8551 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8552 wrel->r_info = rel->r_info;
8553 wrel->r_addend = rel->r_addend;
8554 ++wrel;
8555 }
8556 else if (!dec_dynrel_count (rel->r_info, toc, info,
8557 &local_syms, NULL, NULL))
8558 goto error_ret;
8559
8560 elf_section_data (toc)->relocs = toc_relocs;
8561 toc->reloc_count = wrel - toc_relocs;
8562 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8563 sz = rel_hdr->sh_entsize;
8564 rel_hdr->sh_size = toc->reloc_count * sz;
8565 }
8566 }
8567 else if (toc_relocs != NULL
8568 && elf_section_data (toc)->relocs != toc_relocs)
8569 free (toc_relocs);
8570
8571 if (local_syms != NULL
8572 && symtab_hdr->contents != (unsigned char *) local_syms)
8573 {
8574 if (!info->keep_memory)
8575 free (local_syms);
8576 else
8577 symtab_hdr->contents = (unsigned char *) local_syms;
8578 }
8579 free (skip);
8580 }
8581
8582 return TRUE;
8583 }
8584
8585 /* Return true iff input section I references the TOC using
8586 instructions limited to +/-32k offsets. */
8587
8588 bfd_boolean
8589 ppc64_elf_has_small_toc_reloc (asection *i)
8590 {
8591 return (is_ppc64_elf (i->owner)
8592 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8593 }
8594
8595 /* Allocate space for one GOT entry. */
8596
8597 static void
8598 allocate_got (struct elf_link_hash_entry *h,
8599 struct bfd_link_info *info,
8600 struct got_entry *gent)
8601 {
8602 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8603 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8604 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8605 ? 16 : 8);
8606 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8607 ? 2 : 1) * sizeof (Elf64_External_Rela);
8608 asection *got = ppc64_elf_tdata (gent->owner)->got;
8609
8610 gent->got.offset = got->size;
8611 got->size += entsize;
8612
8613 if (h->type == STT_GNU_IFUNC)
8614 {
8615 htab->elf.irelplt->size += rentsize;
8616 htab->got_reli_size += rentsize;
8617 }
8618 else if (((bfd_link_pic (info)
8619 && !((gent->tls_type & TLS_TPREL) != 0
8620 && bfd_link_executable (info)
8621 && SYMBOL_REFERENCES_LOCAL (info, h)))
8622 || (htab->elf.dynamic_sections_created
8623 && h->dynindx != -1
8624 && !SYMBOL_REFERENCES_LOCAL (info, h)))
8625 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8626 {
8627 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8628 relgot->size += rentsize;
8629 }
8630 }
8631
8632 /* This function merges got entries in the same toc group. */
8633
8634 static void
8635 merge_got_entries (struct got_entry **pent)
8636 {
8637 struct got_entry *ent, *ent2;
8638
8639 for (ent = *pent; ent != NULL; ent = ent->next)
8640 if (!ent->is_indirect)
8641 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8642 if (!ent2->is_indirect
8643 && ent2->addend == ent->addend
8644 && ent2->tls_type == ent->tls_type
8645 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8646 {
8647 ent2->is_indirect = TRUE;
8648 ent2->got.ent = ent;
8649 }
8650 }
8651
8652 /* If H is undefined, make it dynamic if that makes sense. */
8653
8654 static bfd_boolean
8655 ensure_undef_dynamic (struct bfd_link_info *info,
8656 struct elf_link_hash_entry *h)
8657 {
8658 struct elf_link_hash_table *htab = elf_hash_table (info);
8659
8660 if (htab->dynamic_sections_created
8661 && ((info->dynamic_undefined_weak != 0
8662 && h->root.type == bfd_link_hash_undefweak)
8663 || h->root.type == bfd_link_hash_undefined)
8664 && h->dynindx == -1
8665 && !h->forced_local
8666 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
8667 return bfd_elf_link_record_dynamic_symbol (info, h);
8668 return TRUE;
8669 }
8670
8671 /* Allocate space in .plt, .got and associated reloc sections for
8672 dynamic relocs. */
8673
8674 static bfd_boolean
8675 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8676 {
8677 struct bfd_link_info *info;
8678 struct ppc_link_hash_table *htab;
8679 asection *s;
8680 struct ppc_link_hash_entry *eh;
8681 struct got_entry **pgent, *gent;
8682
8683 if (h->root.type == bfd_link_hash_indirect)
8684 return TRUE;
8685
8686 info = (struct bfd_link_info *) inf;
8687 htab = ppc_hash_table (info);
8688 if (htab == NULL)
8689 return FALSE;
8690
8691 eh = (struct ppc_link_hash_entry *) h;
8692 /* Run through the TLS GD got entries first if we're changing them
8693 to TPREL. */
8694 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
8695 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8696 if (gent->got.refcount > 0
8697 && (gent->tls_type & TLS_GD) != 0)
8698 {
8699 /* This was a GD entry that has been converted to TPREL. If
8700 there happens to be a TPREL entry we can use that one. */
8701 struct got_entry *ent;
8702 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8703 if (ent->got.refcount > 0
8704 && (ent->tls_type & TLS_TPREL) != 0
8705 && ent->addend == gent->addend
8706 && ent->owner == gent->owner)
8707 {
8708 gent->got.refcount = 0;
8709 break;
8710 }
8711
8712 /* If not, then we'll be using our own TPREL entry. */
8713 if (gent->got.refcount != 0)
8714 gent->tls_type = TLS_TLS | TLS_TPREL;
8715 }
8716
8717 /* Remove any list entry that won't generate a word in the GOT before
8718 we call merge_got_entries. Otherwise we risk merging to empty
8719 entries. */
8720 pgent = &h->got.glist;
8721 while ((gent = *pgent) != NULL)
8722 if (gent->got.refcount > 0)
8723 {
8724 if ((gent->tls_type & TLS_LD) != 0
8725 && !h->def_dynamic)
8726 {
8727 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8728 *pgent = gent->next;
8729 }
8730 else
8731 pgent = &gent->next;
8732 }
8733 else
8734 *pgent = gent->next;
8735
8736 if (!htab->do_multi_toc)
8737 merge_got_entries (&h->got.glist);
8738
8739 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8740 if (!gent->is_indirect)
8741 {
8742 /* Make sure this symbol is output as a dynamic symbol. */
8743 if (!ensure_undef_dynamic (info, h))
8744 return FALSE;
8745
8746 if (!is_ppc64_elf (gent->owner))
8747 abort ();
8748
8749 allocate_got (h, info, gent);
8750 }
8751
8752 /* If no dynamic sections we can't have dynamic relocs, except for
8753 IFUNCs which are handled even in static executables. */
8754 if (!htab->elf.dynamic_sections_created
8755 && h->type != STT_GNU_IFUNC)
8756 eh->dyn_relocs = NULL;
8757
8758 /* Discard relocs on undefined symbols that must be local. */
8759 else if (h->root.type == bfd_link_hash_undefined
8760 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8761 eh->dyn_relocs = NULL;
8762
8763 /* Also discard relocs on undefined weak syms with non-default
8764 visibility, or when dynamic_undefined_weak says so. */
8765 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8766 eh->dyn_relocs = NULL;
8767
8768 if (eh->dyn_relocs != NULL)
8769 {
8770 struct elf_dyn_relocs *p, **pp;
8771
8772 /* In the shared -Bsymbolic case, discard space allocated for
8773 dynamic pc-relative relocs against symbols which turn out to
8774 be defined in regular objects. For the normal shared case,
8775 discard space for relocs that have become local due to symbol
8776 visibility changes. */
8777
8778 if (bfd_link_pic (info))
8779 {
8780 /* Relocs that use pc_count are those that appear on a call
8781 insn, or certain REL relocs (see must_be_dyn_reloc) that
8782 can be generated via assembly. We want calls to
8783 protected symbols to resolve directly to the function
8784 rather than going via the plt. If people want function
8785 pointer comparisons to work as expected then they should
8786 avoid writing weird assembly. */
8787 if (SYMBOL_CALLS_LOCAL (info, h))
8788 {
8789 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8790 {
8791 p->count -= p->pc_count;
8792 p->pc_count = 0;
8793 if (p->count == 0)
8794 *pp = p->next;
8795 else
8796 pp = &p->next;
8797 }
8798 }
8799
8800 if (eh->dyn_relocs != NULL)
8801 {
8802 /* Make sure this symbol is output as a dynamic symbol. */
8803 if (!ensure_undef_dynamic (info, h))
8804 return FALSE;
8805 }
8806 }
8807 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
8808 {
8809 /* For the non-pic case, discard space for relocs against
8810 symbols which turn out to need copy relocs or are not
8811 dynamic. */
8812 if (h->dynamic_adjusted
8813 && !h->def_regular
8814 && !ELF_COMMON_DEF_P (h))
8815 {
8816 /* Make sure this symbol is output as a dynamic symbol. */
8817 if (!ensure_undef_dynamic (info, h))
8818 return FALSE;
8819
8820 if (h->dynindx == -1)
8821 eh->dyn_relocs = NULL;
8822 }
8823 else
8824 eh->dyn_relocs = NULL;
8825 }
8826
8827 /* Finally, allocate space. */
8828 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8829 {
8830 asection *sreloc = elf_section_data (p->sec)->sreloc;
8831 if (eh->elf.type == STT_GNU_IFUNC)
8832 sreloc = htab->elf.irelplt;
8833 sreloc->size += p->count * sizeof (Elf64_External_Rela);
8834 }
8835 }
8836
8837 /* We might need a PLT entry when the symbol
8838 a) is dynamic, or
8839 b) is an ifunc, or
8840 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
8841 d) has plt16 relocs and we are linking statically. */
8842 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
8843 || h->type == STT_GNU_IFUNC
8844 || (h->needs_plt && h->dynamic_adjusted)
8845 || (h->needs_plt
8846 && h->def_regular
8847 && !htab->elf.dynamic_sections_created
8848 && !htab->can_convert_all_inline_plt
8849 && (((struct ppc_link_hash_entry *) h)->tls_mask
8850 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
8851 {
8852 struct plt_entry *pent;
8853 bfd_boolean doneone = FALSE;
8854 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8855 if (pent->plt.refcount > 0)
8856 {
8857 if (!htab->elf.dynamic_sections_created
8858 || h->dynindx == -1)
8859 {
8860 if (h->type == STT_GNU_IFUNC)
8861 {
8862 s = htab->elf.iplt;
8863 pent->plt.offset = s->size;
8864 s->size += PLT_ENTRY_SIZE (htab);
8865 s = htab->elf.irelplt;
8866 }
8867 else
8868 {
8869 s = htab->pltlocal;
8870 pent->plt.offset = s->size;
8871 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
8872 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
8873 }
8874 }
8875 else
8876 {
8877 /* If this is the first .plt entry, make room for the special
8878 first entry. */
8879 s = htab->elf.splt;
8880 if (s->size == 0)
8881 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
8882
8883 pent->plt.offset = s->size;
8884
8885 /* Make room for this entry. */
8886 s->size += PLT_ENTRY_SIZE (htab);
8887
8888 /* Make room for the .glink code. */
8889 s = htab->glink;
8890 if (s->size == 0)
8891 s->size += GLINK_PLTRESOLVE_SIZE (htab);
8892 if (htab->opd_abi)
8893 {
8894 /* We need bigger stubs past index 32767. */
8895 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
8896 s->size += 4;
8897 s->size += 2*4;
8898 }
8899 else
8900 s->size += 4;
8901
8902 /* We also need to make an entry in the .rela.plt section. */
8903 s = htab->elf.srelplt;
8904 }
8905 if (s != NULL)
8906 s->size += sizeof (Elf64_External_Rela);
8907 doneone = TRUE;
8908 }
8909 else
8910 pent->plt.offset = (bfd_vma) -1;
8911 if (!doneone)
8912 {
8913 h->plt.plist = NULL;
8914 h->needs_plt = 0;
8915 }
8916 }
8917 else
8918 {
8919 h->plt.plist = NULL;
8920 h->needs_plt = 0;
8921 }
8922
8923 return TRUE;
8924 }
8925
8926 #define PPC_LO(v) ((v) & 0xffff)
8927 #define PPC_HI(v) (((v) >> 16) & 0xffff)
8928 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
8929
8930 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
8931 to set up space for global entry stubs. These are put in glink,
8932 after the branch table. */
8933
8934 static bfd_boolean
8935 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
8936 {
8937 struct bfd_link_info *info;
8938 struct ppc_link_hash_table *htab;
8939 struct plt_entry *pent;
8940 asection *s, *plt;
8941
8942 if (h->root.type == bfd_link_hash_indirect)
8943 return TRUE;
8944
8945 if (!h->pointer_equality_needed)
8946 return TRUE;
8947
8948 if (h->def_regular)
8949 return TRUE;
8950
8951 info = inf;
8952 htab = ppc_hash_table (info);
8953 if (htab == NULL)
8954 return FALSE;
8955
8956 s = htab->global_entry;
8957 plt = htab->elf.splt;
8958 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8959 if (pent->plt.offset != (bfd_vma) -1
8960 && pent->addend == 0)
8961 {
8962 /* For ELFv2, if this symbol is not defined in a regular file
8963 and we are not generating a shared library or pie, then we
8964 need to define the symbol in the executable on a call stub.
8965 This is to avoid text relocations. */
8966 bfd_vma off, stub_align, stub_off, stub_size;
8967 unsigned int align_power;
8968
8969 stub_size = 16;
8970 stub_off = s->size;
8971 if (htab->params->plt_stub_align >= 0)
8972 align_power = htab->params->plt_stub_align;
8973 else
8974 align_power = -htab->params->plt_stub_align;
8975 /* Setting section alignment is delayed until we know it is
8976 non-empty. Otherwise the .text output section will be
8977 aligned at least to plt_stub_align even when no global
8978 entry stubs are needed. */
8979 if (s->alignment_power < align_power)
8980 s->alignment_power = align_power;
8981 stub_align = (bfd_vma) 1 << align_power;
8982 if (htab->params->plt_stub_align >= 0
8983 || ((((stub_off + stub_size - 1) & -stub_align)
8984 - (stub_off & -stub_align))
8985 > ((stub_size - 1) & -stub_align)))
8986 stub_off = (stub_off + stub_align - 1) & -stub_align;
8987 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
8988 off -= stub_off + s->output_offset + s->output_section->vma;
8989 /* Note that for --plt-stub-align negative we have a possible
8990 dependency between stub offset and size. Break that
8991 dependency by assuming the max stub size when calculating
8992 the stub offset. */
8993 if (PPC_HA (off) == 0)
8994 stub_size -= 4;
8995 h->root.type = bfd_link_hash_defined;
8996 h->root.u.def.section = s;
8997 h->root.u.def.value = stub_off;
8998 s->size = stub_off + stub_size;
8999 break;
9000 }
9001 return TRUE;
9002 }
9003
9004 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9005 read-only sections. */
9006
9007 static bfd_boolean
9008 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
9009 {
9010 asection *sec;
9011
9012 if (h->root.type == bfd_link_hash_indirect)
9013 return TRUE;
9014
9015 sec = readonly_dynrelocs (h);
9016 if (sec != NULL)
9017 {
9018 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9019
9020 info->flags |= DF_TEXTREL;
9021 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9022 " in read-only section `%pA'\n"),
9023 sec->owner, h->root.root.string, sec);
9024
9025 /* Not an error, just cut short the traversal. */
9026 return FALSE;
9027 }
9028 return TRUE;
9029 }
9030
9031 /* Set the sizes of the dynamic sections. */
9032
9033 static bfd_boolean
9034 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9035 struct bfd_link_info *info)
9036 {
9037 struct ppc_link_hash_table *htab;
9038 bfd *dynobj;
9039 asection *s;
9040 bfd_boolean relocs;
9041 bfd *ibfd;
9042 struct got_entry *first_tlsld;
9043
9044 htab = ppc_hash_table (info);
9045 if (htab == NULL)
9046 return FALSE;
9047
9048 dynobj = htab->elf.dynobj;
9049 if (dynobj == NULL)
9050 abort ();
9051
9052 if (htab->elf.dynamic_sections_created)
9053 {
9054 /* Set the contents of the .interp section to the interpreter. */
9055 if (bfd_link_executable (info) && !info->nointerp)
9056 {
9057 s = bfd_get_linker_section (dynobj, ".interp");
9058 if (s == NULL)
9059 abort ();
9060 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9061 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9062 }
9063 }
9064
9065 /* Set up .got offsets for local syms, and space for local dynamic
9066 relocs. */
9067 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9068 {
9069 struct got_entry **lgot_ents;
9070 struct got_entry **end_lgot_ents;
9071 struct plt_entry **local_plt;
9072 struct plt_entry **end_local_plt;
9073 unsigned char *lgot_masks;
9074 bfd_size_type locsymcount;
9075 Elf_Internal_Shdr *symtab_hdr;
9076
9077 if (!is_ppc64_elf (ibfd))
9078 continue;
9079
9080 for (s = ibfd->sections; s != NULL; s = s->next)
9081 {
9082 struct ppc_dyn_relocs *p;
9083
9084 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9085 {
9086 if (!bfd_is_abs_section (p->sec)
9087 && bfd_is_abs_section (p->sec->output_section))
9088 {
9089 /* Input section has been discarded, either because
9090 it is a copy of a linkonce section or due to
9091 linker script /DISCARD/, so we'll be discarding
9092 the relocs too. */
9093 }
9094 else if (p->count != 0)
9095 {
9096 asection *srel = elf_section_data (p->sec)->sreloc;
9097 if (p->ifunc)
9098 srel = htab->elf.irelplt;
9099 srel->size += p->count * sizeof (Elf64_External_Rela);
9100 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9101 info->flags |= DF_TEXTREL;
9102 }
9103 }
9104 }
9105
9106 lgot_ents = elf_local_got_ents (ibfd);
9107 if (!lgot_ents)
9108 continue;
9109
9110 symtab_hdr = &elf_symtab_hdr (ibfd);
9111 locsymcount = symtab_hdr->sh_info;
9112 end_lgot_ents = lgot_ents + locsymcount;
9113 local_plt = (struct plt_entry **) end_lgot_ents;
9114 end_local_plt = local_plt + locsymcount;
9115 lgot_masks = (unsigned char *) end_local_plt;
9116 s = ppc64_elf_tdata (ibfd)->got;
9117 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9118 {
9119 struct got_entry **pent, *ent;
9120
9121 pent = lgot_ents;
9122 while ((ent = *pent) != NULL)
9123 if (ent->got.refcount > 0)
9124 {
9125 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9126 {
9127 ppc64_tlsld_got (ibfd)->got.refcount += 1;
9128 *pent = ent->next;
9129 }
9130 else
9131 {
9132 unsigned int ent_size = 8;
9133 unsigned int rel_size = sizeof (Elf64_External_Rela);
9134
9135 ent->got.offset = s->size;
9136 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9137 {
9138 ent_size *= 2;
9139 rel_size *= 2;
9140 }
9141 s->size += ent_size;
9142 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9143 {
9144 htab->elf.irelplt->size += rel_size;
9145 htab->got_reli_size += rel_size;
9146 }
9147 else if (bfd_link_pic (info)
9148 && !((ent->tls_type & TLS_TPREL) != 0
9149 && bfd_link_executable (info)))
9150 {
9151 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9152 srel->size += rel_size;
9153 }
9154 pent = &ent->next;
9155 }
9156 }
9157 else
9158 *pent = ent->next;
9159 }
9160
9161 /* Allocate space for plt calls to local syms. */
9162 lgot_masks = (unsigned char *) end_local_plt;
9163 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
9164 {
9165 struct plt_entry *ent;
9166
9167 for (ent = *local_plt; ent != NULL; ent = ent->next)
9168 if (ent->plt.refcount > 0)
9169 {
9170 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9171 {
9172 s = htab->elf.iplt;
9173 ent->plt.offset = s->size;
9174 s->size += PLT_ENTRY_SIZE (htab);
9175 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9176 }
9177 else if (htab->can_convert_all_inline_plt
9178 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
9179 ent->plt.offset = (bfd_vma) -1;
9180 else
9181 {
9182 s = htab->pltlocal;
9183 ent->plt.offset = s->size;
9184 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9185 if (bfd_link_pic (info))
9186 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9187 }
9188 }
9189 else
9190 ent->plt.offset = (bfd_vma) -1;
9191 }
9192 }
9193
9194 /* Allocate global sym .plt and .got entries, and space for global
9195 sym dynamic relocs. */
9196 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9197
9198 if (!htab->opd_abi && !bfd_link_pic (info))
9199 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9200
9201 first_tlsld = NULL;
9202 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9203 {
9204 struct got_entry *ent;
9205
9206 if (!is_ppc64_elf (ibfd))
9207 continue;
9208
9209 ent = ppc64_tlsld_got (ibfd);
9210 if (ent->got.refcount > 0)
9211 {
9212 if (!htab->do_multi_toc && first_tlsld != NULL)
9213 {
9214 ent->is_indirect = TRUE;
9215 ent->got.ent = first_tlsld;
9216 }
9217 else
9218 {
9219 if (first_tlsld == NULL)
9220 first_tlsld = ent;
9221 s = ppc64_elf_tdata (ibfd)->got;
9222 ent->got.offset = s->size;
9223 ent->owner = ibfd;
9224 s->size += 16;
9225 if (bfd_link_pic (info))
9226 {
9227 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9228 srel->size += sizeof (Elf64_External_Rela);
9229 }
9230 }
9231 }
9232 else
9233 ent->got.offset = (bfd_vma) -1;
9234 }
9235
9236 /* We now have determined the sizes of the various dynamic sections.
9237 Allocate memory for them. */
9238 relocs = FALSE;
9239 for (s = dynobj->sections; s != NULL; s = s->next)
9240 {
9241 if ((s->flags & SEC_LINKER_CREATED) == 0)
9242 continue;
9243
9244 if (s == htab->brlt || s == htab->relbrlt)
9245 /* These haven't been allocated yet; don't strip. */
9246 continue;
9247 else if (s == htab->elf.sgot
9248 || s == htab->elf.splt
9249 || s == htab->elf.iplt
9250 || s == htab->pltlocal
9251 || s == htab->glink
9252 || s == htab->global_entry
9253 || s == htab->elf.sdynbss
9254 || s == htab->elf.sdynrelro)
9255 {
9256 /* Strip this section if we don't need it; see the
9257 comment below. */
9258 }
9259 else if (s == htab->glink_eh_frame)
9260 {
9261 if (!bfd_is_abs_section (s->output_section))
9262 /* Not sized yet. */
9263 continue;
9264 }
9265 else if (CONST_STRNEQ (s->name, ".rela"))
9266 {
9267 if (s->size != 0)
9268 {
9269 if (s != htab->elf.srelplt)
9270 relocs = TRUE;
9271
9272 /* We use the reloc_count field as a counter if we need
9273 to copy relocs into the output file. */
9274 s->reloc_count = 0;
9275 }
9276 }
9277 else
9278 {
9279 /* It's not one of our sections, so don't allocate space. */
9280 continue;
9281 }
9282
9283 if (s->size == 0)
9284 {
9285 /* If we don't need this section, strip it from the
9286 output file. This is mostly to handle .rela.bss and
9287 .rela.plt. We must create both sections in
9288 create_dynamic_sections, because they must be created
9289 before the linker maps input sections to output
9290 sections. The linker does that before
9291 adjust_dynamic_symbol is called, and it is that
9292 function which decides whether anything needs to go
9293 into these sections. */
9294 s->flags |= SEC_EXCLUDE;
9295 continue;
9296 }
9297
9298 if (bfd_is_abs_section (s->output_section))
9299 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9300 s->name);
9301
9302 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9303 continue;
9304
9305 /* Allocate memory for the section contents. We use bfd_zalloc
9306 here in case unused entries are not reclaimed before the
9307 section's contents are written out. This should not happen,
9308 but this way if it does we get a R_PPC64_NONE reloc in .rela
9309 sections instead of garbage.
9310 We also rely on the section contents being zero when writing
9311 the GOT and .dynrelro. */
9312 s->contents = bfd_zalloc (dynobj, s->size);
9313 if (s->contents == NULL)
9314 return FALSE;
9315 }
9316
9317 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9318 {
9319 if (!is_ppc64_elf (ibfd))
9320 continue;
9321
9322 s = ppc64_elf_tdata (ibfd)->got;
9323 if (s != NULL && s != htab->elf.sgot)
9324 {
9325 if (s->size == 0)
9326 s->flags |= SEC_EXCLUDE;
9327 else
9328 {
9329 s->contents = bfd_zalloc (ibfd, s->size);
9330 if (s->contents == NULL)
9331 return FALSE;
9332 }
9333 }
9334 s = ppc64_elf_tdata (ibfd)->relgot;
9335 if (s != NULL)
9336 {
9337 if (s->size == 0)
9338 s->flags |= SEC_EXCLUDE;
9339 else
9340 {
9341 s->contents = bfd_zalloc (ibfd, s->size);
9342 if (s->contents == NULL)
9343 return FALSE;
9344 relocs = TRUE;
9345 s->reloc_count = 0;
9346 }
9347 }
9348 }
9349
9350 if (htab->elf.dynamic_sections_created)
9351 {
9352 bfd_boolean tls_opt;
9353
9354 /* Add some entries to the .dynamic section. We fill in the
9355 values later, in ppc64_elf_finish_dynamic_sections, but we
9356 must add the entries now so that we get the correct size for
9357 the .dynamic section. The DT_DEBUG entry is filled in by the
9358 dynamic linker and used by the debugger. */
9359 #define add_dynamic_entry(TAG, VAL) \
9360 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9361
9362 if (bfd_link_executable (info))
9363 {
9364 if (!add_dynamic_entry (DT_DEBUG, 0))
9365 return FALSE;
9366 }
9367
9368 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
9369 {
9370 if (!add_dynamic_entry (DT_PLTGOT, 0)
9371 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9372 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9373 || !add_dynamic_entry (DT_JMPREL, 0)
9374 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9375 return FALSE;
9376 }
9377
9378 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
9379 {
9380 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9381 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9382 return FALSE;
9383 }
9384
9385 tls_opt = (htab->params->tls_get_addr_opt
9386 && htab->tls_get_addr_fd != NULL
9387 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9388 if (tls_opt || !htab->opd_abi)
9389 {
9390 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9391 return FALSE;
9392 }
9393
9394 if (relocs)
9395 {
9396 if (!add_dynamic_entry (DT_RELA, 0)
9397 || !add_dynamic_entry (DT_RELASZ, 0)
9398 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9399 return FALSE;
9400
9401 /* If any dynamic relocs apply to a read-only section,
9402 then we need a DT_TEXTREL entry. */
9403 if ((info->flags & DF_TEXTREL) == 0)
9404 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
9405
9406 if ((info->flags & DF_TEXTREL) != 0)
9407 {
9408 if (!add_dynamic_entry (DT_TEXTREL, 0))
9409 return FALSE;
9410 }
9411 }
9412 }
9413 #undef add_dynamic_entry
9414
9415 return TRUE;
9416 }
9417
9418 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
9419
9420 static bfd_boolean
9421 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9422 {
9423 if (h->plt.plist != NULL
9424 && !h->def_regular
9425 && !h->pointer_equality_needed)
9426 return FALSE;
9427
9428 return _bfd_elf_hash_symbol (h);
9429 }
9430
9431 /* Determine the type of stub needed, if any, for a call. */
9432
9433 static inline enum ppc_stub_type
9434 ppc_type_of_stub (asection *input_sec,
9435 const Elf_Internal_Rela *rel,
9436 struct ppc_link_hash_entry **hash,
9437 struct plt_entry **plt_ent,
9438 bfd_vma destination,
9439 unsigned long local_off)
9440 {
9441 struct ppc_link_hash_entry *h = *hash;
9442 bfd_vma location;
9443 bfd_vma branch_offset;
9444 bfd_vma max_branch_offset;
9445 enum elf_ppc64_reloc_type r_type;
9446
9447 if (h != NULL)
9448 {
9449 struct plt_entry *ent;
9450 struct ppc_link_hash_entry *fdh = h;
9451 if (h->oh != NULL
9452 && h->oh->is_func_descriptor)
9453 {
9454 fdh = ppc_follow_link (h->oh);
9455 *hash = fdh;
9456 }
9457
9458 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9459 if (ent->addend == rel->r_addend
9460 && ent->plt.offset != (bfd_vma) -1)
9461 {
9462 *plt_ent = ent;
9463 return ppc_stub_plt_call;
9464 }
9465
9466 /* Here, we know we don't have a plt entry. If we don't have a
9467 either a defined function descriptor or a defined entry symbol
9468 in a regular object file, then it is pointless trying to make
9469 any other type of stub. */
9470 if (!is_static_defined (&fdh->elf)
9471 && !is_static_defined (&h->elf))
9472 return ppc_stub_none;
9473 }
9474 else if (elf_local_got_ents (input_sec->owner) != NULL)
9475 {
9476 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9477 struct plt_entry **local_plt = (struct plt_entry **)
9478 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9479 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9480
9481 if (local_plt[r_symndx] != NULL)
9482 {
9483 struct plt_entry *ent;
9484
9485 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9486 if (ent->addend == rel->r_addend
9487 && ent->plt.offset != (bfd_vma) -1)
9488 {
9489 *plt_ent = ent;
9490 return ppc_stub_plt_call;
9491 }
9492 }
9493 }
9494
9495 /* Determine where the call point is. */
9496 location = (input_sec->output_offset
9497 + input_sec->output_section->vma
9498 + rel->r_offset);
9499
9500 branch_offset = destination - location;
9501 r_type = ELF64_R_TYPE (rel->r_info);
9502
9503 /* Determine if a long branch stub is needed. */
9504 max_branch_offset = 1 << 25;
9505 if (r_type == R_PPC64_REL14
9506 || r_type == R_PPC64_REL14_BRTAKEN
9507 || r_type == R_PPC64_REL14_BRNTAKEN)
9508 max_branch_offset = 1 << 15;
9509
9510 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
9511 /* We need a stub. Figure out whether a long_branch or plt_branch
9512 is needed later. */
9513 return ppc_stub_long_branch;
9514
9515 return ppc_stub_none;
9516 }
9517
9518 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
9519 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
9520 . mflr %r12
9521 . bcl 20,31,1f
9522 .1: mflr %r11
9523 . mtlr %r12
9524 . lis %r12,xxx-1b@highest
9525 . ori %r12,%r12,xxx-1b@higher
9526 . sldi %r12,%r12,32
9527 . oris %r12,%r12,xxx-1b@high
9528 . ori %r12,%r12,xxx-1b@l
9529 . add/ldx %r12,%r11,%r12 */
9530
9531 static bfd_byte *
9532 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
9533 {
9534 bfd_put_32 (abfd, MFLR_R12, p);
9535 p += 4;
9536 bfd_put_32 (abfd, BCL_20_31, p);
9537 p += 4;
9538 bfd_put_32 (abfd, MFLR_R11, p);
9539 p += 4;
9540 bfd_put_32 (abfd, MTLR_R12, p);
9541 p += 4;
9542 if (off + 0x8000 < 0x10000)
9543 {
9544 if (load)
9545 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
9546 else
9547 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
9548 p += 4;
9549 }
9550 else if (off + 0x80008000ULL < 0x100000000ULL)
9551 {
9552 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
9553 p += 4;
9554 if (load)
9555 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
9556 else
9557 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
9558 p += 4;
9559 }
9560 else
9561 {
9562 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9563 {
9564 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
9565 p += 4;
9566 }
9567 else
9568 {
9569 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
9570 p += 4;
9571 if (((off >> 32) & 0xffff) != 0)
9572 {
9573 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
9574 p += 4;
9575 }
9576 }
9577 if (((off >> 32) & 0xffffffffULL) != 0)
9578 {
9579 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
9580 p += 4;
9581 }
9582 if (PPC_HI (off) != 0)
9583 {
9584 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
9585 p += 4;
9586 }
9587 if (PPC_LO (off) != 0)
9588 {
9589 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
9590 p += 4;
9591 }
9592 if (load)
9593 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
9594 else
9595 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
9596 p += 4;
9597 }
9598 return p;
9599 }
9600
9601 static unsigned int
9602 size_offset (bfd_vma off)
9603 {
9604 unsigned int size;
9605 if (off + 0x8000 < 0x10000)
9606 size = 4;
9607 else if (off + 0x80008000ULL < 0x100000000ULL)
9608 size = 8;
9609 else
9610 {
9611 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9612 size = 4;
9613 else
9614 {
9615 size = 4;
9616 if (((off >> 32) & 0xffff) != 0)
9617 size += 4;
9618 }
9619 if (((off >> 32) & 0xffffffffULL) != 0)
9620 size += 4;
9621 if (PPC_HI (off) != 0)
9622 size += 4;
9623 if (PPC_LO (off) != 0)
9624 size += 4;
9625 size += 4;
9626 }
9627 return size + 16;
9628 }
9629
9630 static unsigned int
9631 num_relocs_for_offset (bfd_vma off)
9632 {
9633 unsigned int num_rel;
9634 if (off + 0x8000 < 0x10000)
9635 num_rel = 1;
9636 else if (off + 0x80008000ULL < 0x100000000ULL)
9637 num_rel = 2;
9638 else
9639 {
9640 num_rel = 1;
9641 if (off + 0x800000000000ULL >= 0x1000000000000ULL
9642 && ((off >> 32) & 0xffff) != 0)
9643 num_rel += 1;
9644 if (PPC_HI (off) != 0)
9645 num_rel += 1;
9646 if (PPC_LO (off) != 0)
9647 num_rel += 1;
9648 }
9649 return num_rel;
9650 }
9651
9652 static Elf_Internal_Rela *
9653 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
9654 bfd_vma roff, bfd_vma targ, bfd_vma off)
9655 {
9656 bfd_vma relative_targ = targ - (roff - 8);
9657 if (bfd_big_endian (info->output_bfd))
9658 roff += 2;
9659 r->r_offset = roff;
9660 r->r_addend = relative_targ + roff;
9661 if (off + 0x8000 < 0x10000)
9662 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
9663 else if (off + 0x80008000ULL < 0x100000000ULL)
9664 {
9665 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
9666 ++r;
9667 roff += 4;
9668 r->r_offset = roff;
9669 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
9670 r->r_addend = relative_targ + roff;
9671 }
9672 else
9673 {
9674 if (off + 0x800000000000ULL < 0x1000000000000ULL)
9675 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
9676 else
9677 {
9678 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
9679 if (((off >> 32) & 0xffff) != 0)
9680 {
9681 ++r;
9682 roff += 4;
9683 r->r_offset = roff;
9684 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
9685 r->r_addend = relative_targ + roff;
9686 }
9687 }
9688 if (((off >> 32) & 0xffffffffULL) != 0)
9689 roff += 4;
9690 if (PPC_HI (off) != 0)
9691 {
9692 ++r;
9693 roff += 4;
9694 r->r_offset = roff;
9695 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
9696 r->r_addend = relative_targ + roff;
9697 }
9698 if (PPC_LO (off) != 0)
9699 {
9700 ++r;
9701 roff += 4;
9702 r->r_offset = roff;
9703 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
9704 r->r_addend = relative_targ + roff;
9705 }
9706 }
9707 return r;
9708 }
9709
9710 /* Emit .eh_frame opcode to advance pc by DELTA. */
9711
9712 static bfd_byte *
9713 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
9714 {
9715 delta /= 4;
9716 if (delta < 64)
9717 *eh++ = DW_CFA_advance_loc + delta;
9718 else if (delta < 256)
9719 {
9720 *eh++ = DW_CFA_advance_loc1;
9721 *eh++ = delta;
9722 }
9723 else if (delta < 65536)
9724 {
9725 *eh++ = DW_CFA_advance_loc2;
9726 bfd_put_16 (abfd, delta, eh);
9727 eh += 2;
9728 }
9729 else
9730 {
9731 *eh++ = DW_CFA_advance_loc4;
9732 bfd_put_32 (abfd, delta, eh);
9733 eh += 4;
9734 }
9735 return eh;
9736 }
9737
9738 /* Size of required .eh_frame opcode to advance pc by DELTA. */
9739
9740 static unsigned int
9741 eh_advance_size (unsigned int delta)
9742 {
9743 if (delta < 64 * 4)
9744 /* DW_CFA_advance_loc+[1..63]. */
9745 return 1;
9746 if (delta < 256 * 4)
9747 /* DW_CFA_advance_loc1, byte. */
9748 return 2;
9749 if (delta < 65536 * 4)
9750 /* DW_CFA_advance_loc2, 2 bytes. */
9751 return 3;
9752 /* DW_CFA_advance_loc4, 4 bytes. */
9753 return 5;
9754 }
9755
9756 /* With power7 weakly ordered memory model, it is possible for ld.so
9757 to update a plt entry in one thread and have another thread see a
9758 stale zero toc entry. To avoid this we need some sort of acquire
9759 barrier in the call stub. One solution is to make the load of the
9760 toc word seem to appear to depend on the load of the function entry
9761 word. Another solution is to test for r2 being zero, and branch to
9762 the appropriate glink entry if so.
9763
9764 . fake dep barrier compare
9765 . ld 12,xxx(2) ld 12,xxx(2)
9766 . mtctr 12 mtctr 12
9767 . xor 11,12,12 ld 2,xxx+8(2)
9768 . add 2,2,11 cmpldi 2,0
9769 . ld 2,xxx+8(2) bnectr+
9770 . bctr b <glink_entry>
9771
9772 The solution involving the compare turns out to be faster, so
9773 that's what we use unless the branch won't reach. */
9774
9775 #define ALWAYS_USE_FAKE_DEP 0
9776 #define ALWAYS_EMIT_R2SAVE 0
9777
9778 static inline unsigned int
9779 plt_stub_size (struct ppc_link_hash_table *htab,
9780 struct ppc_stub_hash_entry *stub_entry,
9781 bfd_vma off)
9782 {
9783 unsigned size;
9784
9785 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
9786 {
9787 size = 8 + size_offset (off - 8);
9788 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
9789 size += 4;
9790 return size;
9791 }
9792
9793 size = 12;
9794 if (ALWAYS_EMIT_R2SAVE
9795 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9796 size += 4;
9797 if (PPC_HA (off) != 0)
9798 size += 4;
9799 if (htab->opd_abi)
9800 {
9801 size += 4;
9802 if (htab->params->plt_static_chain)
9803 size += 4;
9804 if (htab->params->plt_thread_safe
9805 && htab->elf.dynamic_sections_created
9806 && stub_entry->h != NULL
9807 && stub_entry->h->elf.dynindx != -1)
9808 size += 8;
9809 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
9810 size += 4;
9811 }
9812 if (stub_entry->h != NULL
9813 && (stub_entry->h == htab->tls_get_addr_fd
9814 || stub_entry->h == htab->tls_get_addr)
9815 && htab->params->tls_get_addr_opt)
9816 {
9817 size += 7 * 4;
9818 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
9819 size += 6 * 4;
9820 }
9821 return size;
9822 }
9823
9824 /* Depending on the sign of plt_stub_align:
9825 If positive, return the padding to align to a 2**plt_stub_align
9826 boundary.
9827 If negative, if this stub would cross fewer 2**plt_stub_align
9828 boundaries if we align, then return the padding needed to do so. */
9829
9830 static inline unsigned int
9831 plt_stub_pad (struct ppc_link_hash_table *htab,
9832 struct ppc_stub_hash_entry *stub_entry,
9833 bfd_vma plt_off)
9834 {
9835 int stub_align;
9836 unsigned stub_size;
9837 bfd_vma stub_off = stub_entry->group->stub_sec->size;
9838
9839 if (htab->params->plt_stub_align >= 0)
9840 {
9841 stub_align = 1 << htab->params->plt_stub_align;
9842 if ((stub_off & (stub_align - 1)) != 0)
9843 return stub_align - (stub_off & (stub_align - 1));
9844 return 0;
9845 }
9846
9847 stub_align = 1 << -htab->params->plt_stub_align;
9848 stub_size = plt_stub_size (htab, stub_entry, plt_off);
9849 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
9850 > ((stub_size - 1) & -stub_align))
9851 return stub_align - (stub_off & (stub_align - 1));
9852 return 0;
9853 }
9854
9855 /* Build a .plt call stub. */
9856
9857 static inline bfd_byte *
9858 build_plt_stub (struct ppc_link_hash_table *htab,
9859 struct ppc_stub_hash_entry *stub_entry,
9860 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9861 {
9862 bfd *obfd = htab->params->stub_bfd;
9863 bfd_boolean plt_load_toc = htab->opd_abi;
9864 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
9865 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
9866 && htab->elf.dynamic_sections_created
9867 && stub_entry->h != NULL
9868 && stub_entry->h->elf.dynindx != -1);
9869 bfd_boolean use_fake_dep = plt_thread_safe;
9870 bfd_vma cmp_branch_off = 0;
9871
9872 if (!ALWAYS_USE_FAKE_DEP
9873 && plt_load_toc
9874 && plt_thread_safe
9875 && !((stub_entry->h == htab->tls_get_addr_fd
9876 || stub_entry->h == htab->tls_get_addr)
9877 && htab->params->tls_get_addr_opt))
9878 {
9879 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
9880 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
9881 / PLT_ENTRY_SIZE (htab));
9882 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
9883 bfd_vma to, from;
9884
9885 if (pltindex > 32768)
9886 glinkoff += (pltindex - 32768) * 4;
9887 to = (glinkoff
9888 + htab->glink->output_offset
9889 + htab->glink->output_section->vma);
9890 from = (p - stub_entry->group->stub_sec->contents
9891 + 4 * (ALWAYS_EMIT_R2SAVE
9892 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9893 + 4 * (PPC_HA (offset) != 0)
9894 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
9895 != PPC_HA (offset))
9896 + 4 * (plt_static_chain != 0)
9897 + 20
9898 + stub_entry->group->stub_sec->output_offset
9899 + stub_entry->group->stub_sec->output_section->vma);
9900 cmp_branch_off = to - from;
9901 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
9902 }
9903
9904 if (PPC_HA (offset) != 0)
9905 {
9906 if (r != NULL)
9907 {
9908 if (ALWAYS_EMIT_R2SAVE
9909 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9910 r[0].r_offset += 4;
9911 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9912 r[1].r_offset = r[0].r_offset + 4;
9913 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9914 r[1].r_addend = r[0].r_addend;
9915 if (plt_load_toc)
9916 {
9917 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9918 {
9919 r[2].r_offset = r[1].r_offset + 4;
9920 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9921 r[2].r_addend = r[0].r_addend;
9922 }
9923 else
9924 {
9925 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
9926 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9927 r[2].r_addend = r[0].r_addend + 8;
9928 if (plt_static_chain)
9929 {
9930 r[3].r_offset = r[2].r_offset + 4;
9931 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9932 r[3].r_addend = r[0].r_addend + 16;
9933 }
9934 }
9935 }
9936 }
9937 if (ALWAYS_EMIT_R2SAVE
9938 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9939 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
9940 if (plt_load_toc)
9941 {
9942 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
9943 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
9944 }
9945 else
9946 {
9947 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9948 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
9949 }
9950 if (plt_load_toc
9951 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9952 {
9953 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
9954 offset = 0;
9955 }
9956 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
9957 if (plt_load_toc)
9958 {
9959 if (use_fake_dep)
9960 {
9961 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
9962 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
9963 }
9964 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
9965 if (plt_static_chain)
9966 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
9967 }
9968 }
9969 else
9970 {
9971 if (r != NULL)
9972 {
9973 if (ALWAYS_EMIT_R2SAVE
9974 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9975 r[0].r_offset += 4;
9976 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9977 if (plt_load_toc)
9978 {
9979 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9980 {
9981 r[1].r_offset = r[0].r_offset + 4;
9982 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9983 r[1].r_addend = r[0].r_addend;
9984 }
9985 else
9986 {
9987 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
9988 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9989 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
9990 if (plt_static_chain)
9991 {
9992 r[2].r_offset = r[1].r_offset + 4;
9993 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9994 r[2].r_addend = r[0].r_addend + 8;
9995 }
9996 }
9997 }
9998 }
9999 if (ALWAYS_EMIT_R2SAVE
10000 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10001 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10002 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10003 if (plt_load_toc
10004 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10005 {
10006 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10007 offset = 0;
10008 }
10009 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10010 if (plt_load_toc)
10011 {
10012 if (use_fake_dep)
10013 {
10014 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10015 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10016 }
10017 if (plt_static_chain)
10018 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10019 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10020 }
10021 }
10022 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10023 {
10024 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10025 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10026 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10027 }
10028 else
10029 bfd_put_32 (obfd, BCTR, p), p += 4;
10030 return p;
10031 }
10032
10033 /* Build a special .plt call stub for __tls_get_addr. */
10034
10035 #define LD_R11_0R3 0xe9630000
10036 #define LD_R12_0R3 0xe9830000
10037 #define MR_R0_R3 0x7c601b78
10038 #define CMPDI_R11_0 0x2c2b0000
10039 #define ADD_R3_R12_R13 0x7c6c6a14
10040 #define BEQLR 0x4d820020
10041 #define MR_R3_R0 0x7c030378
10042 #define STD_R11_0R1 0xf9610000
10043 #define BCTRL 0x4e800421
10044 #define LD_R11_0R1 0xe9610000
10045 #define MTLR_R11 0x7d6803a6
10046
10047 static inline bfd_byte *
10048 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10049 struct ppc_stub_hash_entry *stub_entry,
10050 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10051 {
10052 bfd *obfd = htab->params->stub_bfd;
10053 bfd_byte *loc = p;
10054
10055 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10056 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10057 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10058 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10059 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10060 bfd_put_32 (obfd, BEQLR, p), p += 4;
10061 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10062 if (r != NULL)
10063 r[0].r_offset += 7 * 4;
10064 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
10065 return build_plt_stub (htab, stub_entry, p, offset, r);
10066
10067 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10068 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10069
10070 if (r != NULL)
10071 r[0].r_offset += 2 * 4;
10072 p = build_plt_stub (htab, stub_entry, p, offset, r);
10073 bfd_put_32 (obfd, BCTRL, p - 4);
10074
10075 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10076 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10077 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10078 bfd_put_32 (obfd, BLR, p), p += 4;
10079
10080 if (htab->glink_eh_frame != NULL
10081 && htab->glink_eh_frame->size != 0)
10082 {
10083 bfd_byte *base, *eh;
10084 unsigned int lr_used, delta;
10085
10086 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10087 eh = base + stub_entry->group->eh_size;
10088 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10089 delta = lr_used - stub_entry->group->lr_restore;
10090 stub_entry->group->lr_restore = lr_used + 16;
10091 eh = eh_advance (htab->elf.dynobj, eh, delta);
10092 *eh++ = DW_CFA_offset_extended_sf;
10093 *eh++ = 65;
10094 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10095 *eh++ = DW_CFA_advance_loc + 4;
10096 *eh++ = DW_CFA_restore_extended;
10097 *eh++ = 65;
10098 stub_entry->group->eh_size = eh - base;
10099 }
10100 return p;
10101 }
10102
10103 static Elf_Internal_Rela *
10104 get_relocs (asection *sec, int count)
10105 {
10106 Elf_Internal_Rela *relocs;
10107 struct bfd_elf_section_data *elfsec_data;
10108
10109 elfsec_data = elf_section_data (sec);
10110 relocs = elfsec_data->relocs;
10111 if (relocs == NULL)
10112 {
10113 bfd_size_type relsize;
10114 relsize = sec->reloc_count * sizeof (*relocs);
10115 relocs = bfd_alloc (sec->owner, relsize);
10116 if (relocs == NULL)
10117 return NULL;
10118 elfsec_data->relocs = relocs;
10119 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10120 sizeof (Elf_Internal_Shdr));
10121 if (elfsec_data->rela.hdr == NULL)
10122 return NULL;
10123 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10124 * sizeof (Elf64_External_Rela));
10125 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10126 sec->reloc_count = 0;
10127 }
10128 relocs += sec->reloc_count;
10129 sec->reloc_count += count;
10130 return relocs;
10131 }
10132
10133 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10134 forms, to the equivalent relocs against the global symbol given by
10135 STUB_ENTRY->H. */
10136
10137 static bfd_boolean
10138 use_global_in_relocs (struct ppc_link_hash_table *htab,
10139 struct ppc_stub_hash_entry *stub_entry,
10140 Elf_Internal_Rela *r, unsigned int num_rel)
10141 {
10142 struct elf_link_hash_entry **hashes;
10143 unsigned long symndx;
10144 struct ppc_link_hash_entry *h;
10145 bfd_vma symval;
10146
10147 /* Relocs are always against symbols in their own object file. Fake
10148 up global sym hashes for the stub bfd (which has no symbols). */
10149 hashes = elf_sym_hashes (htab->params->stub_bfd);
10150 if (hashes == NULL)
10151 {
10152 bfd_size_type hsize;
10153
10154 /* When called the first time, stub_globals will contain the
10155 total number of symbols seen during stub sizing. After
10156 allocating, stub_globals is used as an index to fill the
10157 hashes array. */
10158 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10159 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10160 if (hashes == NULL)
10161 return FALSE;
10162 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10163 htab->stub_globals = 1;
10164 }
10165 symndx = htab->stub_globals++;
10166 h = stub_entry->h;
10167 hashes[symndx] = &h->elf;
10168 if (h->oh != NULL && h->oh->is_func)
10169 h = ppc_follow_link (h->oh);
10170 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10171 || h->elf.root.type == bfd_link_hash_defweak);
10172 symval = (h->elf.root.u.def.value
10173 + h->elf.root.u.def.section->output_offset
10174 + h->elf.root.u.def.section->output_section->vma);
10175 while (num_rel-- != 0)
10176 {
10177 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10178 if (h->elf.root.u.def.section != stub_entry->target_section)
10179 {
10180 /* H is an opd symbol. The addend must be zero, and the
10181 branch reloc is the only one we can convert. */
10182 r->r_addend = 0;
10183 break;
10184 }
10185 else
10186 r->r_addend -= symval;
10187 --r;
10188 }
10189 return TRUE;
10190 }
10191
10192 static bfd_vma
10193 get_r2off (struct bfd_link_info *info,
10194 struct ppc_stub_hash_entry *stub_entry)
10195 {
10196 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10197 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10198
10199 if (r2off == 0)
10200 {
10201 /* Support linking -R objects. Get the toc pointer from the
10202 opd entry. */
10203 char buf[8];
10204 if (!htab->opd_abi)
10205 return r2off;
10206 asection *opd = stub_entry->h->elf.root.u.def.section;
10207 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10208
10209 if (strcmp (opd->name, ".opd") != 0
10210 || opd->reloc_count != 0)
10211 {
10212 info->callbacks->einfo
10213 (_("%P: cannot find opd entry toc for `%pT'\n"),
10214 stub_entry->h->elf.root.root.string);
10215 bfd_set_error (bfd_error_bad_value);
10216 return (bfd_vma) -1;
10217 }
10218 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10219 return (bfd_vma) -1;
10220 r2off = bfd_get_64 (opd->owner, buf);
10221 r2off -= elf_gp (info->output_bfd);
10222 }
10223 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10224 return r2off;
10225 }
10226
10227 static bfd_boolean
10228 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10229 {
10230 struct ppc_stub_hash_entry *stub_entry;
10231 struct ppc_branch_hash_entry *br_entry;
10232 struct bfd_link_info *info;
10233 struct ppc_link_hash_table *htab;
10234 bfd_byte *loc;
10235 bfd_byte *p, *relp;
10236 bfd_vma targ, off;
10237 Elf_Internal_Rela *r;
10238 asection *plt;
10239 int num_rel;
10240
10241 /* Massage our args to the form they really have. */
10242 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10243 info = in_arg;
10244
10245 htab = ppc_hash_table (info);
10246 if (htab == NULL)
10247 return FALSE;
10248
10249 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
10250 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10251
10252 htab->stub_count[stub_entry->stub_type - 1] += 1;
10253 switch (stub_entry->stub_type)
10254 {
10255 case ppc_stub_long_branch:
10256 case ppc_stub_long_branch_r2off:
10257 /* Branches are relative. This is where we are going to. */
10258 targ = (stub_entry->target_value
10259 + stub_entry->target_section->output_offset
10260 + stub_entry->target_section->output_section->vma);
10261 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10262
10263 /* And this is where we are coming from. */
10264 off = (stub_entry->stub_offset
10265 + stub_entry->group->stub_sec->output_offset
10266 + stub_entry->group->stub_sec->output_section->vma);
10267 off = targ - off;
10268
10269 p = loc;
10270 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10271 {
10272 bfd_vma r2off = get_r2off (info, stub_entry);
10273
10274 if (r2off == (bfd_vma) -1)
10275 {
10276 htab->stub_error = TRUE;
10277 return FALSE;
10278 }
10279 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10280 p += 4;
10281 if (PPC_HA (r2off) != 0)
10282 {
10283 bfd_put_32 (htab->params->stub_bfd,
10284 ADDIS_R2_R2 | PPC_HA (r2off), p);
10285 p += 4;
10286 }
10287 if (PPC_LO (r2off) != 0)
10288 {
10289 bfd_put_32 (htab->params->stub_bfd,
10290 ADDI_R2_R2 | PPC_LO (r2off), p);
10291 p += 4;
10292 }
10293 off -= p - loc;
10294 }
10295 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10296 p += 4;
10297
10298 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10299 {
10300 _bfd_error_handler
10301 (_("long branch stub `%s' offset overflow"),
10302 stub_entry->root.string);
10303 htab->stub_error = TRUE;
10304 return FALSE;
10305 }
10306
10307 if (info->emitrelocations)
10308 {
10309 r = get_relocs (stub_entry->group->stub_sec, 1);
10310 if (r == NULL)
10311 return FALSE;
10312 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
10313 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10314 r->r_addend = targ;
10315 if (stub_entry->h != NULL
10316 && !use_global_in_relocs (htab, stub_entry, r, 1))
10317 return FALSE;
10318 }
10319 break;
10320
10321 case ppc_stub_plt_branch:
10322 case ppc_stub_plt_branch_r2off:
10323 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10324 stub_entry->root.string + 9,
10325 FALSE, FALSE);
10326 if (br_entry == NULL)
10327 {
10328 _bfd_error_handler (_("can't find branch stub `%s'"),
10329 stub_entry->root.string);
10330 htab->stub_error = TRUE;
10331 return FALSE;
10332 }
10333
10334 targ = (stub_entry->target_value
10335 + stub_entry->target_section->output_offset
10336 + stub_entry->target_section->output_section->vma);
10337 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10338 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10339
10340 bfd_put_64 (htab->brlt->owner, targ,
10341 htab->brlt->contents + br_entry->offset);
10342
10343 if (br_entry->iter == htab->stub_iteration)
10344 {
10345 br_entry->iter = 0;
10346
10347 if (htab->relbrlt != NULL)
10348 {
10349 /* Create a reloc for the branch lookup table entry. */
10350 Elf_Internal_Rela rela;
10351 bfd_byte *rl;
10352
10353 rela.r_offset = (br_entry->offset
10354 + htab->brlt->output_offset
10355 + htab->brlt->output_section->vma);
10356 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10357 rela.r_addend = targ;
10358
10359 rl = htab->relbrlt->contents;
10360 rl += (htab->relbrlt->reloc_count++
10361 * sizeof (Elf64_External_Rela));
10362 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10363 }
10364 else if (info->emitrelocations)
10365 {
10366 r = get_relocs (htab->brlt, 1);
10367 if (r == NULL)
10368 return FALSE;
10369 /* brlt, being SEC_LINKER_CREATED does not go through the
10370 normal reloc processing. Symbols and offsets are not
10371 translated from input file to output file form, so
10372 set up the offset per the output file. */
10373 r->r_offset = (br_entry->offset
10374 + htab->brlt->output_offset
10375 + htab->brlt->output_section->vma);
10376 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10377 r->r_addend = targ;
10378 }
10379 }
10380
10381 targ = (br_entry->offset
10382 + htab->brlt->output_offset
10383 + htab->brlt->output_section->vma);
10384
10385 off = (elf_gp (info->output_bfd)
10386 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10387 off = targ - off;
10388
10389 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10390 {
10391 info->callbacks->einfo
10392 (_("%P: linkage table error against `%pT'\n"),
10393 stub_entry->root.string);
10394 bfd_set_error (bfd_error_bad_value);
10395 htab->stub_error = TRUE;
10396 return FALSE;
10397 }
10398
10399 if (info->emitrelocations)
10400 {
10401 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
10402 if (r == NULL)
10403 return FALSE;
10404 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10405 if (bfd_big_endian (info->output_bfd))
10406 r[0].r_offset += 2;
10407 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10408 r[0].r_offset += 4;
10409 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10410 r[0].r_addend = targ;
10411 if (PPC_HA (off) != 0)
10412 {
10413 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10414 r[1].r_offset = r[0].r_offset + 4;
10415 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10416 r[1].r_addend = r[0].r_addend;
10417 }
10418 }
10419
10420 p = loc;
10421 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10422 {
10423 if (PPC_HA (off) != 0)
10424 {
10425 bfd_put_32 (htab->params->stub_bfd,
10426 ADDIS_R12_R2 | PPC_HA (off), p);
10427 p += 4;
10428 bfd_put_32 (htab->params->stub_bfd,
10429 LD_R12_0R12 | PPC_LO (off), p);
10430 }
10431 else
10432 bfd_put_32 (htab->params->stub_bfd,
10433 LD_R12_0R2 | PPC_LO (off), p);
10434 }
10435 else
10436 {
10437 bfd_vma r2off = get_r2off (info, stub_entry);
10438
10439 if (r2off == (bfd_vma) -1)
10440 {
10441 htab->stub_error = TRUE;
10442 return FALSE;
10443 }
10444
10445 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10446 p += 4;
10447 if (PPC_HA (off) != 0)
10448 {
10449 bfd_put_32 (htab->params->stub_bfd,
10450 ADDIS_R12_R2 | PPC_HA (off), p);
10451 p += 4;
10452 bfd_put_32 (htab->params->stub_bfd,
10453 LD_R12_0R12 | PPC_LO (off), p);
10454 }
10455 else
10456 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
10457
10458 if (PPC_HA (r2off) != 0)
10459 {
10460 p += 4;
10461 bfd_put_32 (htab->params->stub_bfd,
10462 ADDIS_R2_R2 | PPC_HA (r2off), p);
10463 }
10464 if (PPC_LO (r2off) != 0)
10465 {
10466 p += 4;
10467 bfd_put_32 (htab->params->stub_bfd,
10468 ADDI_R2_R2 | PPC_LO (r2off), p);
10469 }
10470 }
10471 p += 4;
10472 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10473 p += 4;
10474 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10475 p += 4;
10476 break;
10477
10478 case ppc_stub_long_branch_notoc:
10479 case ppc_stub_long_branch_both:
10480 case ppc_stub_plt_branch_notoc:
10481 case ppc_stub_plt_branch_both:
10482 case ppc_stub_plt_call_notoc:
10483 case ppc_stub_plt_call_both:
10484 p = loc;
10485 off = (stub_entry->stub_offset
10486 + stub_entry->group->stub_sec->output_offset
10487 + stub_entry->group->stub_sec->output_section->vma);
10488 if (stub_entry->stub_type == ppc_stub_long_branch_both
10489 || stub_entry->stub_type == ppc_stub_plt_branch_both
10490 || stub_entry->stub_type == ppc_stub_plt_call_both)
10491 {
10492 off += 4;
10493 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10494 p += 4;
10495 }
10496 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10497 {
10498 targ = stub_entry->plt_ent->plt.offset & ~1;
10499 if (targ >= (bfd_vma) -2)
10500 abort ();
10501
10502 plt = htab->elf.splt;
10503 if (!htab->elf.dynamic_sections_created
10504 || stub_entry->h == NULL
10505 || stub_entry->h->elf.dynindx == -1)
10506 {
10507 if (stub_entry->symtype == STT_GNU_IFUNC)
10508 plt = htab->elf.iplt;
10509 else
10510 plt = htab->pltlocal;
10511 }
10512 targ += plt->output_offset + plt->output_section->vma;
10513 }
10514 else
10515 targ = (stub_entry->target_value
10516 + stub_entry->target_section->output_offset
10517 + stub_entry->target_section->output_section->vma);
10518 off = targ - off;
10519
10520 relp = p;
10521 num_rel = 0;
10522 /* The notoc stubs calculate their target (either a PLT entry or
10523 the global entry point of a function) relative to the PC
10524 returned by the "bcl" two instructions past the start of the
10525 sequence emitted by build_offset. The offset is therefore 8
10526 less than calculated from the start of the sequence. */
10527 off -= 8;
10528 p = build_offset (htab->params->stub_bfd, p, off,
10529 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
10530 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
10531 {
10532 bfd_vma from;
10533 num_rel = 1;
10534 from = (stub_entry->stub_offset
10535 + stub_entry->group->stub_sec->output_offset
10536 + stub_entry->group->stub_sec->output_section->vma
10537 + (p - loc));
10538 bfd_put_32 (htab->params->stub_bfd,
10539 B_DOT | ((targ - from) & 0x3fffffc), p);
10540 }
10541 else
10542 {
10543 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10544 p += 4;
10545 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10546 }
10547 p += 4;
10548
10549 if (info->emitrelocations)
10550 {
10551 bfd_vma roff;
10552 num_rel += num_relocs_for_offset (off);
10553 r = get_relocs (stub_entry->group->stub_sec, num_rel);
10554 if (r == NULL)
10555 return FALSE;
10556 roff = relp + 16 - stub_entry->group->stub_sec->contents;
10557 r = emit_relocs_for_offset (info, r, roff, targ, off);
10558 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
10559 || stub_entry->stub_type == ppc_stub_long_branch_both)
10560 {
10561 ++r;
10562 roff = p - 4 - stub_entry->group->stub_sec->contents;
10563 r->r_offset = roff;
10564 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10565 r->r_addend = targ;
10566 if (stub_entry->h != NULL
10567 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
10568 return FALSE;
10569 }
10570 }
10571
10572 if (htab->glink_eh_frame != NULL
10573 && htab->glink_eh_frame->size != 0)
10574 {
10575 bfd_byte *base, *eh;
10576 unsigned int lr_used, delta;
10577
10578 base = (htab->glink_eh_frame->contents
10579 + stub_entry->group->eh_base + 17);
10580 eh = base + stub_entry->group->eh_size;
10581 lr_used = stub_entry->stub_offset + 8;
10582 if (stub_entry->stub_type == ppc_stub_long_branch_both
10583 || stub_entry->stub_type == ppc_stub_plt_branch_both
10584 || stub_entry->stub_type == ppc_stub_plt_call_both)
10585 lr_used += 4;
10586 delta = lr_used - stub_entry->group->lr_restore;
10587 stub_entry->group->lr_restore = lr_used + 8;
10588 eh = eh_advance (htab->elf.dynobj, eh, delta);
10589 *eh++ = DW_CFA_register;
10590 *eh++ = 65;
10591 *eh++ = 12;
10592 *eh++ = DW_CFA_advance_loc + 2;
10593 *eh++ = DW_CFA_restore_extended;
10594 *eh++ = 65;
10595 stub_entry->group->eh_size = eh - base;
10596 }
10597 break;
10598
10599 case ppc_stub_plt_call:
10600 case ppc_stub_plt_call_r2save:
10601 if (stub_entry->h != NULL
10602 && stub_entry->h->is_func_descriptor
10603 && stub_entry->h->oh != NULL)
10604 {
10605 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10606
10607 /* If the old-ABI "dot-symbol" is undefined make it weak so
10608 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
10609 if (fh->elf.root.type == bfd_link_hash_undefined
10610 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
10611 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
10612 fh->elf.root.type = bfd_link_hash_undefweak;
10613 }
10614
10615 /* Now build the stub. */
10616 targ = stub_entry->plt_ent->plt.offset & ~1;
10617 if (targ >= (bfd_vma) -2)
10618 abort ();
10619
10620 plt = htab->elf.splt;
10621 if (!htab->elf.dynamic_sections_created
10622 || stub_entry->h == NULL
10623 || stub_entry->h->elf.dynindx == -1)
10624 {
10625 if (stub_entry->symtype == STT_GNU_IFUNC)
10626 plt = htab->elf.iplt;
10627 else
10628 plt = htab->pltlocal;
10629 }
10630 targ += plt->output_offset + plt->output_section->vma;
10631
10632 off = (elf_gp (info->output_bfd)
10633 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10634 off = targ - off;
10635
10636 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10637 {
10638 info->callbacks->einfo
10639 /* xgettext:c-format */
10640 (_("%P: linkage table error against `%pT'\n"),
10641 stub_entry->h != NULL
10642 ? stub_entry->h->elf.root.root.string
10643 : "<local sym>");
10644 bfd_set_error (bfd_error_bad_value);
10645 htab->stub_error = TRUE;
10646 return FALSE;
10647 }
10648
10649 r = NULL;
10650 if (info->emitrelocations)
10651 {
10652 r = get_relocs (stub_entry->group->stub_sec,
10653 ((PPC_HA (off) != 0)
10654 + (htab->opd_abi
10655 ? 2 + (htab->params->plt_static_chain
10656 && PPC_HA (off + 16) == PPC_HA (off))
10657 : 1)));
10658 if (r == NULL)
10659 return FALSE;
10660 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10661 if (bfd_big_endian (info->output_bfd))
10662 r[0].r_offset += 2;
10663 r[0].r_addend = targ;
10664 }
10665 if (stub_entry->h != NULL
10666 && (stub_entry->h == htab->tls_get_addr_fd
10667 || stub_entry->h == htab->tls_get_addr)
10668 && htab->params->tls_get_addr_opt)
10669 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10670 else
10671 p = build_plt_stub (htab, stub_entry, loc, off, r);
10672 break;
10673
10674 case ppc_stub_save_res:
10675 return TRUE;
10676
10677 default:
10678 BFD_FAIL ();
10679 return FALSE;
10680 }
10681
10682 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
10683
10684 if (htab->params->emit_stub_syms)
10685 {
10686 struct elf_link_hash_entry *h;
10687 size_t len1, len2;
10688 char *name;
10689 const char *const stub_str[] = { "long_branch",
10690 "long_branch",
10691 "long_branch",
10692 "long_branch",
10693 "plt_branch",
10694 "plt_branch",
10695 "plt_branch",
10696 "plt_branch",
10697 "plt_call",
10698 "plt_call",
10699 "plt_call",
10700 "plt_call" };
10701
10702 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10703 len2 = strlen (stub_entry->root.string);
10704 name = bfd_malloc (len1 + len2 + 2);
10705 if (name == NULL)
10706 return FALSE;
10707 memcpy (name, stub_entry->root.string, 9);
10708 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10709 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10710 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10711 if (h == NULL)
10712 return FALSE;
10713 if (h->root.type == bfd_link_hash_new)
10714 {
10715 h->root.type = bfd_link_hash_defined;
10716 h->root.u.def.section = stub_entry->group->stub_sec;
10717 h->root.u.def.value = stub_entry->stub_offset;
10718 h->ref_regular = 1;
10719 h->def_regular = 1;
10720 h->ref_regular_nonweak = 1;
10721 h->forced_local = 1;
10722 h->non_elf = 0;
10723 h->root.linker_def = 1;
10724 }
10725 }
10726
10727 return TRUE;
10728 }
10729
10730 /* As above, but don't actually build the stub. Just bump offset so
10731 we know stub section sizes, and select plt_branch stubs where
10732 long_branch stubs won't do. */
10733
10734 static bfd_boolean
10735 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10736 {
10737 struct ppc_stub_hash_entry *stub_entry;
10738 struct bfd_link_info *info;
10739 struct ppc_link_hash_table *htab;
10740 asection *plt;
10741 bfd_vma targ, off, r2off;
10742 unsigned int size, extra, lr_used, delta;
10743
10744 /* Massage our args to the form they really have. */
10745 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10746 info = in_arg;
10747
10748 htab = ppc_hash_table (info);
10749 if (htab == NULL)
10750 return FALSE;
10751
10752 /* Make a note of the offset within the stubs for this entry. */
10753 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10754
10755 if (stub_entry->h != NULL
10756 && stub_entry->h->save_res
10757 && stub_entry->h->elf.root.type == bfd_link_hash_defined
10758 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
10759 {
10760 /* Don't make stubs to out-of-line register save/restore
10761 functions. Instead, emit copies of the functions. */
10762 stub_entry->group->needs_save_res = 1;
10763 stub_entry->stub_type = ppc_stub_save_res;
10764 return TRUE;
10765 }
10766
10767 switch (stub_entry->stub_type)
10768 {
10769 case ppc_stub_plt_branch:
10770 case ppc_stub_plt_branch_r2off:
10771 /* Reset the stub type from the plt branch variant in case we now
10772 can reach with a shorter stub. */
10773 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10774 /* Fall through. */
10775 case ppc_stub_long_branch:
10776 case ppc_stub_long_branch_r2off:
10777 targ = (stub_entry->target_value
10778 + stub_entry->target_section->output_offset
10779 + stub_entry->target_section->output_section->vma);
10780 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10781 off = (stub_entry->stub_offset
10782 + stub_entry->group->stub_sec->output_offset
10783 + stub_entry->group->stub_sec->output_section->vma);
10784
10785 size = 4;
10786 r2off = 0;
10787 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10788 {
10789 r2off = get_r2off (info, stub_entry);
10790 if (r2off == (bfd_vma) -1)
10791 {
10792 htab->stub_error = TRUE;
10793 return FALSE;
10794 }
10795 size = 8;
10796 if (PPC_HA (r2off) != 0)
10797 size += 4;
10798 if (PPC_LO (r2off) != 0)
10799 size += 4;
10800 off += size - 4;
10801 }
10802 off = targ - off;
10803
10804 /* If the branch offset is too big, use a ppc_stub_plt_branch.
10805 Do the same for -R objects without function descriptors. */
10806 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
10807 && r2off == 0
10808 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
10809 || off + (1 << 25) >= (bfd_vma) (1 << 26))
10810 {
10811 struct ppc_branch_hash_entry *br_entry;
10812
10813 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10814 stub_entry->root.string + 9,
10815 TRUE, FALSE);
10816 if (br_entry == NULL)
10817 {
10818 _bfd_error_handler (_("can't build branch stub `%s'"),
10819 stub_entry->root.string);
10820 htab->stub_error = TRUE;
10821 return FALSE;
10822 }
10823
10824 if (br_entry->iter != htab->stub_iteration)
10825 {
10826 br_entry->iter = htab->stub_iteration;
10827 br_entry->offset = htab->brlt->size;
10828 htab->brlt->size += 8;
10829
10830 if (htab->relbrlt != NULL)
10831 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10832 else if (info->emitrelocations)
10833 {
10834 htab->brlt->reloc_count += 1;
10835 htab->brlt->flags |= SEC_RELOC;
10836 }
10837 }
10838
10839 targ = (br_entry->offset
10840 + htab->brlt->output_offset
10841 + htab->brlt->output_section->vma);
10842 off = (elf_gp (info->output_bfd)
10843 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10844 off = targ - off;
10845
10846 if (info->emitrelocations)
10847 {
10848 stub_entry->group->stub_sec->reloc_count
10849 += 1 + (PPC_HA (off) != 0);
10850 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10851 }
10852
10853 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
10854 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10855 {
10856 size = 12;
10857 if (PPC_HA (off) != 0)
10858 size = 16;
10859 }
10860 else
10861 {
10862 size = 16;
10863 if (PPC_HA (off) != 0)
10864 size += 4;
10865
10866 if (PPC_HA (r2off) != 0)
10867 size += 4;
10868 if (PPC_LO (r2off) != 0)
10869 size += 4;
10870 }
10871 }
10872 else if (info->emitrelocations)
10873 {
10874 stub_entry->group->stub_sec->reloc_count += 1;
10875 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10876 }
10877 break;
10878
10879 case ppc_stub_plt_branch_notoc:
10880 case ppc_stub_plt_branch_both:
10881 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10882 /* Fall through. */
10883 case ppc_stub_long_branch_notoc:
10884 case ppc_stub_long_branch_both:
10885 off = (stub_entry->stub_offset
10886 + stub_entry->group->stub_sec->output_offset
10887 + stub_entry->group->stub_sec->output_section->vma);
10888 size = 0;
10889 if (stub_entry->stub_type == ppc_stub_long_branch_both)
10890 size = 4;
10891 off += size;
10892 targ = (stub_entry->target_value
10893 + stub_entry->target_section->output_offset
10894 + stub_entry->target_section->output_section->vma);
10895 off = targ - off;
10896
10897 if (info->emitrelocations)
10898 {
10899 stub_entry->group->stub_sec->reloc_count
10900 += num_relocs_for_offset (off);
10901 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10902 }
10903
10904 extra = size_offset (off - 8);
10905 /* Include branch insn plus those in the offset sequence. */
10906 size += 4 + extra;
10907 /* The branch insn is at the end, or "extra" bytes along. So
10908 its offset will be "extra" bytes less that that already
10909 calculated. */
10910 off -= extra;
10911
10912 /* After the bcl, lr has been modified so we need to emit
10913 .eh_frame info saying the return address is in r12. */
10914 lr_used = stub_entry->stub_offset + 8;
10915 if (stub_entry->stub_type == ppc_stub_long_branch_both)
10916 lr_used += 4;
10917 /* The eh_frame info will consist of a DW_CFA_advance_loc or
10918 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
10919 DW_CFA_restore_extended 65. */
10920 delta = lr_used - stub_entry->group->lr_restore;
10921 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10922 stub_entry->group->lr_restore = lr_used + 8;
10923
10924 /* If the branch can't reach, use a plt_branch. */
10925 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10926 {
10927 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
10928 - ppc_stub_long_branch_notoc);
10929 size += 4;
10930 }
10931 else if (info->emitrelocations)
10932 stub_entry->group->stub_sec->reloc_count +=1;
10933 break;
10934
10935 case ppc_stub_plt_call_notoc:
10936 case ppc_stub_plt_call_both:
10937 off = (stub_entry->stub_offset
10938 + stub_entry->group->stub_sec->output_offset
10939 + stub_entry->group->stub_sec->output_section->vma);
10940 if (stub_entry->stub_type == ppc_stub_plt_call_both)
10941 off += 4;
10942 targ = stub_entry->plt_ent->plt.offset & ~1;
10943 if (targ >= (bfd_vma) -2)
10944 abort ();
10945
10946 plt = htab->elf.splt;
10947 if (!htab->elf.dynamic_sections_created
10948 || stub_entry->h == NULL
10949 || stub_entry->h->elf.dynindx == -1)
10950 {
10951 if (stub_entry->symtype == STT_GNU_IFUNC)
10952 plt = htab->elf.iplt;
10953 else
10954 plt = htab->pltlocal;
10955 }
10956 targ += plt->output_offset + plt->output_section->vma;
10957 off = targ - off;
10958
10959 if (htab->params->plt_stub_align != 0)
10960 {
10961 unsigned pad = plt_stub_pad (htab, stub_entry, off);
10962
10963 stub_entry->group->stub_sec->size += pad;
10964 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10965 off -= pad;
10966 }
10967
10968 if (info->emitrelocations)
10969 {
10970 stub_entry->group->stub_sec->reloc_count
10971 += num_relocs_for_offset (off - 8);
10972 stub_entry->group->stub_sec->flags |= SEC_RELOC;
10973 }
10974
10975 size = plt_stub_size (htab, stub_entry, off);
10976
10977 /* After the bcl, lr has been modified so we need to emit
10978 .eh_frame info saying the return address is in r12. */
10979 lr_used = stub_entry->stub_offset + 8;
10980 if (stub_entry->stub_type == ppc_stub_plt_call_both)
10981 lr_used += 4;
10982 /* The eh_frame info will consist of a DW_CFA_advance_loc or
10983 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
10984 DW_CFA_restore_extended 65. */
10985 delta = lr_used - stub_entry->group->lr_restore;
10986 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10987 stub_entry->group->lr_restore = lr_used + 8;
10988 break;
10989
10990 case ppc_stub_plt_call:
10991 case ppc_stub_plt_call_r2save:
10992 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
10993 if (targ >= (bfd_vma) -2)
10994 abort ();
10995 plt = htab->elf.splt;
10996 if (!htab->elf.dynamic_sections_created
10997 || stub_entry->h == NULL
10998 || stub_entry->h->elf.dynindx == -1)
10999 {
11000 if (stub_entry->symtype == STT_GNU_IFUNC)
11001 plt = htab->elf.iplt;
11002 else
11003 plt = htab->pltlocal;
11004 }
11005 targ += plt->output_offset + plt->output_section->vma;
11006
11007 off = (elf_gp (info->output_bfd)
11008 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11009 off = targ - off;
11010
11011 if (htab->params->plt_stub_align != 0)
11012 {
11013 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11014
11015 stub_entry->group->stub_sec->size += pad;
11016 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11017 }
11018
11019 if (info->emitrelocations)
11020 {
11021 stub_entry->group->stub_sec->reloc_count
11022 += ((PPC_HA (off) != 0)
11023 + (htab->opd_abi
11024 ? 2 + (htab->params->plt_static_chain
11025 && PPC_HA (off + 16) == PPC_HA (off))
11026 : 1));
11027 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11028 }
11029
11030 size = plt_stub_size (htab, stub_entry, off);
11031
11032 if (stub_entry->h != NULL
11033 && (stub_entry->h == htab->tls_get_addr_fd
11034 || stub_entry->h == htab->tls_get_addr)
11035 && htab->params->tls_get_addr_opt
11036 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11037 {
11038 /* After the bctrl, lr has been modified so we need to
11039 emit .eh_frame info saying the return address is
11040 on the stack. In fact we put the EH info specifying
11041 that the return address is on the stack *at* the
11042 call rather than after it, because the EH info for a
11043 call needs to be specified by that point.
11044 See libgcc/unwind-dw2.c execute_cfa_program. */
11045 lr_used = stub_entry->stub_offset + size - 20;
11046 /* The eh_frame info will consist of a DW_CFA_advance_loc
11047 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11048 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11049 delta = lr_used - stub_entry->group->lr_restore;
11050 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11051 stub_entry->group->lr_restore = size - 4;
11052 }
11053 break;
11054
11055 default:
11056 BFD_FAIL ();
11057 return FALSE;
11058 }
11059
11060 stub_entry->group->stub_sec->size += size;
11061 return TRUE;
11062 }
11063
11064 /* Set up various things so that we can make a list of input sections
11065 for each output section included in the link. Returns -1 on error,
11066 0 when no stubs will be needed, and 1 on success. */
11067
11068 int
11069 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11070 {
11071 unsigned int id;
11072 bfd_size_type amt;
11073 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11074
11075 if (htab == NULL)
11076 return -1;
11077
11078 htab->sec_info_arr_size = _bfd_section_id;
11079 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11080 htab->sec_info = bfd_zmalloc (amt);
11081 if (htab->sec_info == NULL)
11082 return -1;
11083
11084 /* Set toc_off for com, und, abs and ind sections. */
11085 for (id = 0; id < 3; id++)
11086 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11087
11088 return 1;
11089 }
11090
11091 /* Set up for first pass at multitoc partitioning. */
11092
11093 void
11094 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11095 {
11096 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11097
11098 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11099 htab->toc_bfd = NULL;
11100 htab->toc_first_sec = NULL;
11101 }
11102
11103 /* The linker repeatedly calls this function for each TOC input section
11104 and linker generated GOT section. Group input bfds such that the toc
11105 within a group is less than 64k in size. */
11106
11107 bfd_boolean
11108 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11109 {
11110 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11111 bfd_vma addr, off, limit;
11112
11113 if (htab == NULL)
11114 return FALSE;
11115
11116 if (!htab->second_toc_pass)
11117 {
11118 /* Keep track of the first .toc or .got section for this input bfd. */
11119 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11120
11121 if (new_bfd)
11122 {
11123 htab->toc_bfd = isec->owner;
11124 htab->toc_first_sec = isec;
11125 }
11126
11127 addr = isec->output_offset + isec->output_section->vma;
11128 off = addr - htab->toc_curr;
11129 limit = 0x80008000;
11130 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11131 limit = 0x10000;
11132 if (off + isec->size > limit)
11133 {
11134 addr = (htab->toc_first_sec->output_offset
11135 + htab->toc_first_sec->output_section->vma);
11136 htab->toc_curr = addr;
11137 htab->toc_curr &= -TOC_BASE_ALIGN;
11138 }
11139
11140 /* toc_curr is the base address of this toc group. Set elf_gp
11141 for the input section to be the offset relative to the
11142 output toc base plus 0x8000. Making the input elf_gp an
11143 offset allows us to move the toc as a whole without
11144 recalculating input elf_gp. */
11145 off = htab->toc_curr - elf_gp (info->output_bfd);
11146 off += TOC_BASE_OFF;
11147
11148 /* Die if someone uses a linker script that doesn't keep input
11149 file .toc and .got together. */
11150 if (new_bfd
11151 && elf_gp (isec->owner) != 0
11152 && elf_gp (isec->owner) != off)
11153 return FALSE;
11154
11155 elf_gp (isec->owner) = off;
11156 return TRUE;
11157 }
11158
11159 /* During the second pass toc_first_sec points to the start of
11160 a toc group, and toc_curr is used to track the old elf_gp.
11161 We use toc_bfd to ensure we only look at each bfd once. */
11162 if (htab->toc_bfd == isec->owner)
11163 return TRUE;
11164 htab->toc_bfd = isec->owner;
11165
11166 if (htab->toc_first_sec == NULL
11167 || htab->toc_curr != elf_gp (isec->owner))
11168 {
11169 htab->toc_curr = elf_gp (isec->owner);
11170 htab->toc_first_sec = isec;
11171 }
11172 addr = (htab->toc_first_sec->output_offset
11173 + htab->toc_first_sec->output_section->vma);
11174 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11175 elf_gp (isec->owner) = off;
11176
11177 return TRUE;
11178 }
11179
11180 /* Called via elf_link_hash_traverse to merge GOT entries for global
11181 symbol H. */
11182
11183 static bfd_boolean
11184 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11185 {
11186 if (h->root.type == bfd_link_hash_indirect)
11187 return TRUE;
11188
11189 merge_got_entries (&h->got.glist);
11190
11191 return TRUE;
11192 }
11193
11194 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11195 symbol H. */
11196
11197 static bfd_boolean
11198 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11199 {
11200 struct got_entry *gent;
11201
11202 if (h->root.type == bfd_link_hash_indirect)
11203 return TRUE;
11204
11205 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11206 if (!gent->is_indirect)
11207 allocate_got (h, (struct bfd_link_info *) inf, gent);
11208 return TRUE;
11209 }
11210
11211 /* Called on the first multitoc pass after the last call to
11212 ppc64_elf_next_toc_section. This function removes duplicate GOT
11213 entries. */
11214
11215 bfd_boolean
11216 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11217 {
11218 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11219 struct bfd *ibfd, *ibfd2;
11220 bfd_boolean done_something;
11221
11222 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11223
11224 if (!htab->do_multi_toc)
11225 return FALSE;
11226
11227 /* Merge global sym got entries within a toc group. */
11228 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11229
11230 /* And tlsld_got. */
11231 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11232 {
11233 struct got_entry *ent, *ent2;
11234
11235 if (!is_ppc64_elf (ibfd))
11236 continue;
11237
11238 ent = ppc64_tlsld_got (ibfd);
11239 if (!ent->is_indirect
11240 && ent->got.offset != (bfd_vma) -1)
11241 {
11242 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11243 {
11244 if (!is_ppc64_elf (ibfd2))
11245 continue;
11246
11247 ent2 = ppc64_tlsld_got (ibfd2);
11248 if (!ent2->is_indirect
11249 && ent2->got.offset != (bfd_vma) -1
11250 && elf_gp (ibfd2) == elf_gp (ibfd))
11251 {
11252 ent2->is_indirect = TRUE;
11253 ent2->got.ent = ent;
11254 }
11255 }
11256 }
11257 }
11258
11259 /* Zap sizes of got sections. */
11260 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11261 htab->elf.irelplt->size -= htab->got_reli_size;
11262 htab->got_reli_size = 0;
11263
11264 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11265 {
11266 asection *got, *relgot;
11267
11268 if (!is_ppc64_elf (ibfd))
11269 continue;
11270
11271 got = ppc64_elf_tdata (ibfd)->got;
11272 if (got != NULL)
11273 {
11274 got->rawsize = got->size;
11275 got->size = 0;
11276 relgot = ppc64_elf_tdata (ibfd)->relgot;
11277 relgot->rawsize = relgot->size;
11278 relgot->size = 0;
11279 }
11280 }
11281
11282 /* Now reallocate the got, local syms first. We don't need to
11283 allocate section contents again since we never increase size. */
11284 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11285 {
11286 struct got_entry **lgot_ents;
11287 struct got_entry **end_lgot_ents;
11288 struct plt_entry **local_plt;
11289 struct plt_entry **end_local_plt;
11290 unsigned char *lgot_masks;
11291 bfd_size_type locsymcount;
11292 Elf_Internal_Shdr *symtab_hdr;
11293 asection *s;
11294
11295 if (!is_ppc64_elf (ibfd))
11296 continue;
11297
11298 lgot_ents = elf_local_got_ents (ibfd);
11299 if (!lgot_ents)
11300 continue;
11301
11302 symtab_hdr = &elf_symtab_hdr (ibfd);
11303 locsymcount = symtab_hdr->sh_info;
11304 end_lgot_ents = lgot_ents + locsymcount;
11305 local_plt = (struct plt_entry **) end_lgot_ents;
11306 end_local_plt = local_plt + locsymcount;
11307 lgot_masks = (unsigned char *) end_local_plt;
11308 s = ppc64_elf_tdata (ibfd)->got;
11309 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11310 {
11311 struct got_entry *ent;
11312
11313 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11314 {
11315 unsigned int ent_size = 8;
11316 unsigned int rel_size = sizeof (Elf64_External_Rela);
11317
11318 ent->got.offset = s->size;
11319 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11320 {
11321 ent_size *= 2;
11322 rel_size *= 2;
11323 }
11324 s->size += ent_size;
11325 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
11326 {
11327 htab->elf.irelplt->size += rel_size;
11328 htab->got_reli_size += rel_size;
11329 }
11330 else if (bfd_link_pic (info)
11331 && !((ent->tls_type & TLS_TPREL) != 0
11332 && bfd_link_executable (info)))
11333 {
11334 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11335 srel->size += rel_size;
11336 }
11337 }
11338 }
11339 }
11340
11341 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11342
11343 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11344 {
11345 struct got_entry *ent;
11346
11347 if (!is_ppc64_elf (ibfd))
11348 continue;
11349
11350 ent = ppc64_tlsld_got (ibfd);
11351 if (!ent->is_indirect
11352 && ent->got.offset != (bfd_vma) -1)
11353 {
11354 asection *s = ppc64_elf_tdata (ibfd)->got;
11355 ent->got.offset = s->size;
11356 s->size += 16;
11357 if (bfd_link_pic (info))
11358 {
11359 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11360 srel->size += sizeof (Elf64_External_Rela);
11361 }
11362 }
11363 }
11364
11365 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11366 if (!done_something)
11367 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11368 {
11369 asection *got;
11370
11371 if (!is_ppc64_elf (ibfd))
11372 continue;
11373
11374 got = ppc64_elf_tdata (ibfd)->got;
11375 if (got != NULL)
11376 {
11377 done_something = got->rawsize != got->size;
11378 if (done_something)
11379 break;
11380 }
11381 }
11382
11383 if (done_something)
11384 (*htab->params->layout_sections_again) ();
11385
11386 /* Set up for second pass over toc sections to recalculate elf_gp
11387 on input sections. */
11388 htab->toc_bfd = NULL;
11389 htab->toc_first_sec = NULL;
11390 htab->second_toc_pass = TRUE;
11391 return done_something;
11392 }
11393
11394 /* Called after second pass of multitoc partitioning. */
11395
11396 void
11397 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11398 {
11399 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11400
11401 /* After the second pass, toc_curr tracks the TOC offset used
11402 for code sections below in ppc64_elf_next_input_section. */
11403 htab->toc_curr = TOC_BASE_OFF;
11404 }
11405
11406 /* No toc references were found in ISEC. If the code in ISEC makes no
11407 calls, then there's no need to use toc adjusting stubs when branching
11408 into ISEC. Actually, indirect calls from ISEC are OK as they will
11409 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11410 needed, and 2 if a cyclical call-graph was found but no other reason
11411 for a stub was detected. If called from the top level, a return of
11412 2 means the same as a return of 0. */
11413
11414 static int
11415 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11416 {
11417 int ret;
11418
11419 /* Mark this section as checked. */
11420 isec->call_check_done = 1;
11421
11422 /* We know none of our code bearing sections will need toc stubs. */
11423 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11424 return 0;
11425
11426 if (isec->size == 0)
11427 return 0;
11428
11429 if (isec->output_section == NULL)
11430 return 0;
11431
11432 ret = 0;
11433 if (isec->reloc_count != 0)
11434 {
11435 Elf_Internal_Rela *relstart, *rel;
11436 Elf_Internal_Sym *local_syms;
11437 struct ppc_link_hash_table *htab;
11438
11439 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11440 info->keep_memory);
11441 if (relstart == NULL)
11442 return -1;
11443
11444 /* Look for branches to outside of this section. */
11445 local_syms = NULL;
11446 htab = ppc_hash_table (info);
11447 if (htab == NULL)
11448 return -1;
11449
11450 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11451 {
11452 enum elf_ppc64_reloc_type r_type;
11453 unsigned long r_symndx;
11454 struct elf_link_hash_entry *h;
11455 struct ppc_link_hash_entry *eh;
11456 Elf_Internal_Sym *sym;
11457 asection *sym_sec;
11458 struct _opd_sec_data *opd;
11459 bfd_vma sym_value;
11460 bfd_vma dest;
11461
11462 r_type = ELF64_R_TYPE (rel->r_info);
11463 if (r_type != R_PPC64_REL24
11464 && r_type != R_PPC64_REL24_NOTOC
11465 && r_type != R_PPC64_REL14
11466 && r_type != R_PPC64_REL14_BRTAKEN
11467 && r_type != R_PPC64_REL14_BRNTAKEN
11468 && r_type != R_PPC64_PLTCALL)
11469 continue;
11470
11471 r_symndx = ELF64_R_SYM (rel->r_info);
11472 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11473 isec->owner))
11474 {
11475 ret = -1;
11476 break;
11477 }
11478
11479 /* Calls to dynamic lib functions go through a plt call stub
11480 that uses r2. */
11481 eh = (struct ppc_link_hash_entry *) h;
11482 if (eh != NULL
11483 && (eh->elf.plt.plist != NULL
11484 || (eh->oh != NULL
11485 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11486 {
11487 ret = 1;
11488 break;
11489 }
11490
11491 if (sym_sec == NULL)
11492 /* Ignore other undefined symbols. */
11493 continue;
11494
11495 /* Assume branches to other sections not included in the
11496 link need stubs too, to cover -R and absolute syms. */
11497 if (sym_sec->output_section == NULL)
11498 {
11499 ret = 1;
11500 break;
11501 }
11502
11503 if (h == NULL)
11504 sym_value = sym->st_value;
11505 else
11506 {
11507 if (h->root.type != bfd_link_hash_defined
11508 && h->root.type != bfd_link_hash_defweak)
11509 abort ();
11510 sym_value = h->root.u.def.value;
11511 }
11512 sym_value += rel->r_addend;
11513
11514 /* If this branch reloc uses an opd sym, find the code section. */
11515 opd = get_opd_info (sym_sec);
11516 if (opd != NULL)
11517 {
11518 if (h == NULL && opd->adjust != NULL)
11519 {
11520 long adjust;
11521
11522 adjust = opd->adjust[OPD_NDX (sym_value)];
11523 if (adjust == -1)
11524 /* Assume deleted functions won't ever be called. */
11525 continue;
11526 sym_value += adjust;
11527 }
11528
11529 dest = opd_entry_value (sym_sec, sym_value,
11530 &sym_sec, NULL, FALSE);
11531 if (dest == (bfd_vma) -1)
11532 continue;
11533 }
11534 else
11535 dest = (sym_value
11536 + sym_sec->output_offset
11537 + sym_sec->output_section->vma);
11538
11539 /* Ignore branch to self. */
11540 if (sym_sec == isec)
11541 continue;
11542
11543 /* If the called function uses the toc, we need a stub. */
11544 if (sym_sec->has_toc_reloc
11545 || sym_sec->makes_toc_func_call)
11546 {
11547 ret = 1;
11548 break;
11549 }
11550
11551 /* Assume any branch that needs a long branch stub might in fact
11552 need a plt_branch stub. A plt_branch stub uses r2. */
11553 else if (dest - (isec->output_offset
11554 + isec->output_section->vma
11555 + rel->r_offset) + (1 << 25)
11556 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11557 ? h->other
11558 : sym->st_other))
11559 {
11560 ret = 1;
11561 break;
11562 }
11563
11564 /* If calling back to a section in the process of being
11565 tested, we can't say for sure that no toc adjusting stubs
11566 are needed, so don't return zero. */
11567 else if (sym_sec->call_check_in_progress)
11568 ret = 2;
11569
11570 /* Branches to another section that itself doesn't have any TOC
11571 references are OK. Recursively call ourselves to check. */
11572 else if (!sym_sec->call_check_done)
11573 {
11574 int recur;
11575
11576 /* Mark current section as indeterminate, so that other
11577 sections that call back to current won't be marked as
11578 known. */
11579 isec->call_check_in_progress = 1;
11580 recur = toc_adjusting_stub_needed (info, sym_sec);
11581 isec->call_check_in_progress = 0;
11582
11583 if (recur != 0)
11584 {
11585 ret = recur;
11586 if (recur != 2)
11587 break;
11588 }
11589 }
11590 }
11591
11592 if (local_syms != NULL
11593 && (elf_symtab_hdr (isec->owner).contents
11594 != (unsigned char *) local_syms))
11595 free (local_syms);
11596 if (elf_section_data (isec)->relocs != relstart)
11597 free (relstart);
11598 }
11599
11600 if ((ret & 1) == 0
11601 && isec->map_head.s != NULL
11602 && (strcmp (isec->output_section->name, ".init") == 0
11603 || strcmp (isec->output_section->name, ".fini") == 0))
11604 {
11605 if (isec->map_head.s->has_toc_reloc
11606 || isec->map_head.s->makes_toc_func_call)
11607 ret = 1;
11608 else if (!isec->map_head.s->call_check_done)
11609 {
11610 int recur;
11611 isec->call_check_in_progress = 1;
11612 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11613 isec->call_check_in_progress = 0;
11614 if (recur != 0)
11615 ret = recur;
11616 }
11617 }
11618
11619 if (ret == 1)
11620 isec->makes_toc_func_call = 1;
11621
11622 return ret;
11623 }
11624
11625 /* The linker repeatedly calls this function for each input section,
11626 in the order that input sections are linked into output sections.
11627 Build lists of input sections to determine groupings between which
11628 we may insert linker stubs. */
11629
11630 bfd_boolean
11631 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11632 {
11633 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11634
11635 if (htab == NULL)
11636 return FALSE;
11637
11638 if ((isec->output_section->flags & SEC_CODE) != 0
11639 && isec->output_section->id < htab->sec_info_arr_size)
11640 {
11641 /* This happens to make the list in reverse order,
11642 which is what we want. */
11643 htab->sec_info[isec->id].u.list
11644 = htab->sec_info[isec->output_section->id].u.list;
11645 htab->sec_info[isec->output_section->id].u.list = isec;
11646 }
11647
11648 if (htab->multi_toc_needed)
11649 {
11650 /* Analyse sections that aren't already flagged as needing a
11651 valid toc pointer. Exclude .fixup for the linux kernel.
11652 .fixup contains branches, but only back to the function that
11653 hit an exception. */
11654 if (!(isec->has_toc_reloc
11655 || (isec->flags & SEC_CODE) == 0
11656 || strcmp (isec->name, ".fixup") == 0
11657 || isec->call_check_done))
11658 {
11659 if (toc_adjusting_stub_needed (info, isec) < 0)
11660 return FALSE;
11661 }
11662 /* Make all sections use the TOC assigned for this object file.
11663 This will be wrong for pasted sections; We fix that in
11664 check_pasted_section(). */
11665 if (elf_gp (isec->owner) != 0)
11666 htab->toc_curr = elf_gp (isec->owner);
11667 }
11668
11669 htab->sec_info[isec->id].toc_off = htab->toc_curr;
11670 return TRUE;
11671 }
11672
11673 /* Check that all .init and .fini sections use the same toc, if they
11674 have toc relocs. */
11675
11676 static bfd_boolean
11677 check_pasted_section (struct bfd_link_info *info, const char *name)
11678 {
11679 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11680
11681 if (o != NULL)
11682 {
11683 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11684 bfd_vma toc_off = 0;
11685 asection *i;
11686
11687 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11688 if (i->has_toc_reloc)
11689 {
11690 if (toc_off == 0)
11691 toc_off = htab->sec_info[i->id].toc_off;
11692 else if (toc_off != htab->sec_info[i->id].toc_off)
11693 return FALSE;
11694 }
11695
11696 if (toc_off == 0)
11697 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11698 if (i->makes_toc_func_call)
11699 {
11700 toc_off = htab->sec_info[i->id].toc_off;
11701 break;
11702 }
11703
11704 /* Make sure the whole pasted function uses the same toc offset. */
11705 if (toc_off != 0)
11706 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11707 htab->sec_info[i->id].toc_off = toc_off;
11708 }
11709 return TRUE;
11710 }
11711
11712 bfd_boolean
11713 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11714 {
11715 return (check_pasted_section (info, ".init")
11716 & check_pasted_section (info, ".fini"));
11717 }
11718
11719 /* See whether we can group stub sections together. Grouping stub
11720 sections may result in fewer stubs. More importantly, we need to
11721 put all .init* and .fini* stubs at the beginning of the .init or
11722 .fini output sections respectively, because glibc splits the
11723 _init and _fini functions into multiple parts. Putting a stub in
11724 the middle of a function is not a good idea. */
11725
11726 static bfd_boolean
11727 group_sections (struct bfd_link_info *info,
11728 bfd_size_type stub_group_size,
11729 bfd_boolean stubs_always_before_branch)
11730 {
11731 struct ppc_link_hash_table *htab;
11732 asection *osec;
11733 bfd_boolean suppress_size_errors;
11734
11735 htab = ppc_hash_table (info);
11736 if (htab == NULL)
11737 return FALSE;
11738
11739 suppress_size_errors = FALSE;
11740 if (stub_group_size == 1)
11741 {
11742 /* Default values. */
11743 if (stubs_always_before_branch)
11744 stub_group_size = 0x1e00000;
11745 else
11746 stub_group_size = 0x1c00000;
11747 suppress_size_errors = TRUE;
11748 }
11749
11750 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
11751 {
11752 asection *tail;
11753
11754 if (osec->id >= htab->sec_info_arr_size)
11755 continue;
11756
11757 tail = htab->sec_info[osec->id].u.list;
11758 while (tail != NULL)
11759 {
11760 asection *curr;
11761 asection *prev;
11762 bfd_size_type total;
11763 bfd_boolean big_sec;
11764 bfd_vma curr_toc;
11765 struct map_stub *group;
11766 bfd_size_type group_size;
11767
11768 curr = tail;
11769 total = tail->size;
11770 group_size = (ppc64_elf_section_data (tail) != NULL
11771 && ppc64_elf_section_data (tail)->has_14bit_branch
11772 ? stub_group_size >> 10 : stub_group_size);
11773
11774 big_sec = total > group_size;
11775 if (big_sec && !suppress_size_errors)
11776 /* xgettext:c-format */
11777 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
11778 tail->owner, tail);
11779 curr_toc = htab->sec_info[tail->id].toc_off;
11780
11781 while ((prev = htab->sec_info[curr->id].u.list) != NULL
11782 && ((total += curr->output_offset - prev->output_offset)
11783 < (ppc64_elf_section_data (prev) != NULL
11784 && ppc64_elf_section_data (prev)->has_14bit_branch
11785 ? (group_size = stub_group_size >> 10) : group_size))
11786 && htab->sec_info[prev->id].toc_off == curr_toc)
11787 curr = prev;
11788
11789 /* OK, the size from the start of CURR to the end is less
11790 than group_size and thus can be handled by one stub
11791 section. (or the tail section is itself larger than
11792 group_size, in which case we may be toast.) We should
11793 really be keeping track of the total size of stubs added
11794 here, as stubs contribute to the final output section
11795 size. That's a little tricky, and this way will only
11796 break if stubs added make the total size more than 2^25,
11797 ie. for the default stub_group_size, if stubs total more
11798 than 2097152 bytes, or nearly 75000 plt call stubs. */
11799 group = bfd_alloc (curr->owner, sizeof (*group));
11800 if (group == NULL)
11801 return FALSE;
11802 group->link_sec = curr;
11803 group->stub_sec = NULL;
11804 group->needs_save_res = 0;
11805 group->lr_restore = 0;
11806 group->eh_size = 0;
11807 group->eh_base = 0;
11808 group->next = htab->group;
11809 htab->group = group;
11810 do
11811 {
11812 prev = htab->sec_info[tail->id].u.list;
11813 /* Set up this stub group. */
11814 htab->sec_info[tail->id].u.group = group;
11815 }
11816 while (tail != curr && (tail = prev) != NULL);
11817
11818 /* But wait, there's more! Input sections up to group_size
11819 bytes before the stub section can be handled by it too.
11820 Don't do this if we have a really large section after the
11821 stubs, as adding more stubs increases the chance that
11822 branches may not reach into the stub section. */
11823 if (!stubs_always_before_branch && !big_sec)
11824 {
11825 total = 0;
11826 while (prev != NULL
11827 && ((total += tail->output_offset - prev->output_offset)
11828 < (ppc64_elf_section_data (prev) != NULL
11829 && ppc64_elf_section_data (prev)->has_14bit_branch
11830 ? (group_size = stub_group_size >> 10)
11831 : group_size))
11832 && htab->sec_info[prev->id].toc_off == curr_toc)
11833 {
11834 tail = prev;
11835 prev = htab->sec_info[tail->id].u.list;
11836 htab->sec_info[tail->id].u.group = group;
11837 }
11838 }
11839 tail = prev;
11840 }
11841 }
11842 return TRUE;
11843 }
11844
11845 static const unsigned char glink_eh_frame_cie[] =
11846 {
11847 0, 0, 0, 16, /* length. */
11848 0, 0, 0, 0, /* id. */
11849 1, /* CIE version. */
11850 'z', 'R', 0, /* Augmentation string. */
11851 4, /* Code alignment. */
11852 0x78, /* Data alignment. */
11853 65, /* RA reg. */
11854 1, /* Augmentation size. */
11855 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
11856 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
11857 };
11858
11859 /* Stripping output sections is normally done before dynamic section
11860 symbols have been allocated. This function is called later, and
11861 handles cases like htab->brlt which is mapped to its own output
11862 section. */
11863
11864 static void
11865 maybe_strip_output (struct bfd_link_info *info, asection *isec)
11866 {
11867 if (isec->size == 0
11868 && isec->output_section->size == 0
11869 && !(isec->output_section->flags & SEC_KEEP)
11870 && !bfd_section_removed_from_list (info->output_bfd,
11871 isec->output_section)
11872 && elf_section_data (isec->output_section)->dynindx == 0)
11873 {
11874 isec->output_section->flags |= SEC_EXCLUDE;
11875 bfd_section_list_remove (info->output_bfd, isec->output_section);
11876 info->output_bfd->section_count--;
11877 }
11878 }
11879
11880 /* Determine and set the size of the stub section for a final link.
11881
11882 The basic idea here is to examine all the relocations looking for
11883 PC-relative calls to a target that is unreachable with a "bl"
11884 instruction. */
11885
11886 bfd_boolean
11887 ppc64_elf_size_stubs (struct bfd_link_info *info)
11888 {
11889 bfd_size_type stub_group_size;
11890 bfd_boolean stubs_always_before_branch;
11891 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11892
11893 if (htab == NULL)
11894 return FALSE;
11895
11896 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
11897 htab->params->plt_thread_safe = 1;
11898 if (!htab->opd_abi)
11899 htab->params->plt_thread_safe = 0;
11900 else if (htab->params->plt_thread_safe == -1)
11901 {
11902 static const char *const thread_starter[] =
11903 {
11904 "pthread_create",
11905 /* libstdc++ */
11906 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11907 /* librt */
11908 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11909 "mq_notify", "create_timer",
11910 /* libanl */
11911 "getaddrinfo_a",
11912 /* libgomp */
11913 "GOMP_parallel",
11914 "GOMP_parallel_start",
11915 "GOMP_parallel_loop_static",
11916 "GOMP_parallel_loop_static_start",
11917 "GOMP_parallel_loop_dynamic",
11918 "GOMP_parallel_loop_dynamic_start",
11919 "GOMP_parallel_loop_guided",
11920 "GOMP_parallel_loop_guided_start",
11921 "GOMP_parallel_loop_runtime",
11922 "GOMP_parallel_loop_runtime_start",
11923 "GOMP_parallel_sections",
11924 "GOMP_parallel_sections_start",
11925 /* libgo */
11926 "__go_go",
11927 };
11928 unsigned i;
11929
11930 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
11931 {
11932 struct elf_link_hash_entry *h;
11933 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11934 FALSE, FALSE, TRUE);
11935 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
11936 if (htab->params->plt_thread_safe)
11937 break;
11938 }
11939 }
11940 stubs_always_before_branch = htab->params->group_size < 0;
11941 if (htab->params->group_size < 0)
11942 stub_group_size = -htab->params->group_size;
11943 else
11944 stub_group_size = htab->params->group_size;
11945
11946 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
11947 return FALSE;
11948
11949 #define STUB_SHRINK_ITER 20
11950 /* Loop until no stubs added. After iteration 20 of this loop we may
11951 exit on a stub section shrinking. This is to break out of a
11952 pathological case where adding stubs on one iteration decreases
11953 section gaps (perhaps due to alignment), which then requires
11954 fewer or smaller stubs on the next iteration. */
11955
11956 while (1)
11957 {
11958 bfd *input_bfd;
11959 unsigned int bfd_indx;
11960 struct map_stub *group;
11961
11962 htab->stub_iteration += 1;
11963
11964 for (input_bfd = info->input_bfds, bfd_indx = 0;
11965 input_bfd != NULL;
11966 input_bfd = input_bfd->link.next, bfd_indx++)
11967 {
11968 Elf_Internal_Shdr *symtab_hdr;
11969 asection *section;
11970 Elf_Internal_Sym *local_syms = NULL;
11971
11972 if (!is_ppc64_elf (input_bfd))
11973 continue;
11974
11975 /* We'll need the symbol table in a second. */
11976 symtab_hdr = &elf_symtab_hdr (input_bfd);
11977 if (symtab_hdr->sh_info == 0)
11978 continue;
11979
11980 /* Walk over each section attached to the input bfd. */
11981 for (section = input_bfd->sections;
11982 section != NULL;
11983 section = section->next)
11984 {
11985 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
11986
11987 /* If there aren't any relocs, then there's nothing more
11988 to do. */
11989 if ((section->flags & SEC_RELOC) == 0
11990 || (section->flags & SEC_ALLOC) == 0
11991 || (section->flags & SEC_LOAD) == 0
11992 || (section->flags & SEC_CODE) == 0
11993 || section->reloc_count == 0)
11994 continue;
11995
11996 /* If this section is a link-once section that will be
11997 discarded, then don't create any stubs. */
11998 if (section->output_section == NULL
11999 || section->output_section->owner != info->output_bfd)
12000 continue;
12001
12002 /* Get the relocs. */
12003 internal_relocs
12004 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12005 info->keep_memory);
12006 if (internal_relocs == NULL)
12007 goto error_ret_free_local;
12008
12009 /* Now examine each relocation. */
12010 irela = internal_relocs;
12011 irelaend = irela + section->reloc_count;
12012 for (; irela < irelaend; irela++)
12013 {
12014 enum elf_ppc64_reloc_type r_type;
12015 unsigned int r_indx;
12016 enum ppc_stub_type stub_type;
12017 struct ppc_stub_hash_entry *stub_entry;
12018 asection *sym_sec, *code_sec;
12019 bfd_vma sym_value, code_value;
12020 bfd_vma destination;
12021 unsigned long local_off;
12022 bfd_boolean ok_dest;
12023 struct ppc_link_hash_entry *hash;
12024 struct ppc_link_hash_entry *fdh;
12025 struct elf_link_hash_entry *h;
12026 Elf_Internal_Sym *sym;
12027 char *stub_name;
12028 const asection *id_sec;
12029 struct _opd_sec_data *opd;
12030 struct plt_entry *plt_ent;
12031
12032 r_type = ELF64_R_TYPE (irela->r_info);
12033 r_indx = ELF64_R_SYM (irela->r_info);
12034
12035 if (r_type >= R_PPC64_max)
12036 {
12037 bfd_set_error (bfd_error_bad_value);
12038 goto error_ret_free_internal;
12039 }
12040
12041 /* Only look for stubs on branch instructions. */
12042 if (r_type != R_PPC64_REL24
12043 && r_type != R_PPC64_REL24_NOTOC
12044 && r_type != R_PPC64_REL14
12045 && r_type != R_PPC64_REL14_BRTAKEN
12046 && r_type != R_PPC64_REL14_BRNTAKEN)
12047 continue;
12048
12049 /* Now determine the call target, its name, value,
12050 section. */
12051 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12052 r_indx, input_bfd))
12053 goto error_ret_free_internal;
12054 hash = (struct ppc_link_hash_entry *) h;
12055
12056 ok_dest = FALSE;
12057 fdh = NULL;
12058 sym_value = 0;
12059 if (hash == NULL)
12060 {
12061 sym_value = sym->st_value;
12062 if (sym_sec != NULL
12063 && sym_sec->output_section != NULL)
12064 ok_dest = TRUE;
12065 }
12066 else if (hash->elf.root.type == bfd_link_hash_defined
12067 || hash->elf.root.type == bfd_link_hash_defweak)
12068 {
12069 sym_value = hash->elf.root.u.def.value;
12070 if (sym_sec->output_section != NULL)
12071 ok_dest = TRUE;
12072 }
12073 else if (hash->elf.root.type == bfd_link_hash_undefweak
12074 || hash->elf.root.type == bfd_link_hash_undefined)
12075 {
12076 /* Recognise an old ABI func code entry sym, and
12077 use the func descriptor sym instead if it is
12078 defined. */
12079 if (hash->elf.root.root.string[0] == '.'
12080 && hash->oh != NULL)
12081 {
12082 fdh = ppc_follow_link (hash->oh);
12083 if (fdh->elf.root.type == bfd_link_hash_defined
12084 || fdh->elf.root.type == bfd_link_hash_defweak)
12085 {
12086 sym_sec = fdh->elf.root.u.def.section;
12087 sym_value = fdh->elf.root.u.def.value;
12088 if (sym_sec->output_section != NULL)
12089 ok_dest = TRUE;
12090 }
12091 else
12092 fdh = NULL;
12093 }
12094 }
12095 else
12096 {
12097 bfd_set_error (bfd_error_bad_value);
12098 goto error_ret_free_internal;
12099 }
12100
12101 destination = 0;
12102 local_off = 0;
12103 if (ok_dest)
12104 {
12105 sym_value += irela->r_addend;
12106 destination = (sym_value
12107 + sym_sec->output_offset
12108 + sym_sec->output_section->vma);
12109 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12110 ? hash->elf.other
12111 : sym->st_other);
12112 }
12113
12114 code_sec = sym_sec;
12115 code_value = sym_value;
12116 opd = get_opd_info (sym_sec);
12117 if (opd != NULL)
12118 {
12119 bfd_vma dest;
12120
12121 if (hash == NULL && opd->adjust != NULL)
12122 {
12123 long adjust = opd->adjust[OPD_NDX (sym_value)];
12124 if (adjust == -1)
12125 continue;
12126 code_value += adjust;
12127 sym_value += adjust;
12128 }
12129 dest = opd_entry_value (sym_sec, sym_value,
12130 &code_sec, &code_value, FALSE);
12131 if (dest != (bfd_vma) -1)
12132 {
12133 destination = dest;
12134 if (fdh != NULL)
12135 {
12136 /* Fixup old ABI sym to point at code
12137 entry. */
12138 hash->elf.root.type = bfd_link_hash_defweak;
12139 hash->elf.root.u.def.section = code_sec;
12140 hash->elf.root.u.def.value = code_value;
12141 }
12142 }
12143 }
12144
12145 /* Determine what (if any) linker stub is needed. */
12146 plt_ent = NULL;
12147 stub_type = ppc_type_of_stub (section, irela, &hash,
12148 &plt_ent, destination,
12149 local_off);
12150
12151 if (r_type == R_PPC64_REL24_NOTOC)
12152 {
12153 if (stub_type == ppc_stub_plt_call)
12154 stub_type = ppc_stub_plt_call_notoc;
12155 else if (stub_type == ppc_stub_long_branch
12156 || (code_sec != NULL
12157 && code_sec->output_section != NULL
12158 && (((hash ? hash->elf.other : sym->st_other)
12159 & STO_PPC64_LOCAL_MASK)
12160 != 1 << STO_PPC64_LOCAL_BIT)))
12161 stub_type = ppc_stub_long_branch_notoc;
12162 }
12163 else if (stub_type != ppc_stub_plt_call)
12164 {
12165 /* Check whether we need a TOC adjusting stub.
12166 Since the linker pastes together pieces from
12167 different object files when creating the
12168 _init and _fini functions, it may be that a
12169 call to what looks like a local sym is in
12170 fact a call needing a TOC adjustment. */
12171 if ((code_sec != NULL
12172 && code_sec->output_section != NULL
12173 && (htab->sec_info[code_sec->id].toc_off
12174 != htab->sec_info[section->id].toc_off)
12175 && (code_sec->has_toc_reloc
12176 || code_sec->makes_toc_func_call))
12177 || (((hash ? hash->elf.other : sym->st_other)
12178 & STO_PPC64_LOCAL_MASK)
12179 == 1 << STO_PPC64_LOCAL_BIT))
12180 stub_type = ppc_stub_long_branch_r2off;
12181 }
12182
12183 if (stub_type == ppc_stub_none)
12184 continue;
12185
12186 /* __tls_get_addr calls might be eliminated. */
12187 if (stub_type != ppc_stub_plt_call
12188 && stub_type != ppc_stub_plt_call_notoc
12189 && hash != NULL
12190 && (hash == htab->tls_get_addr
12191 || hash == htab->tls_get_addr_fd)
12192 && section->has_tls_reloc
12193 && irela != internal_relocs)
12194 {
12195 /* Get tls info. */
12196 unsigned char *tls_mask;
12197
12198 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12199 irela - 1, input_bfd))
12200 goto error_ret_free_internal;
12201 if ((*tls_mask & TLS_TLS) != 0)
12202 continue;
12203 }
12204
12205 if (stub_type == ppc_stub_plt_call)
12206 {
12207 if (!htab->opd_abi
12208 && htab->params->plt_localentry0 != 0
12209 && is_elfv2_localentry0 (&hash->elf))
12210 htab->has_plt_localentry0 = 1;
12211 else if (irela + 1 < irelaend
12212 && irela[1].r_offset == irela->r_offset + 4
12213 && (ELF64_R_TYPE (irela[1].r_info)
12214 == R_PPC64_TOCSAVE))
12215 {
12216 if (!tocsave_find (htab, INSERT,
12217 &local_syms, irela + 1, input_bfd))
12218 goto error_ret_free_internal;
12219 }
12220 else
12221 stub_type = ppc_stub_plt_call_r2save;
12222 }
12223
12224 /* Support for grouping stub sections. */
12225 id_sec = htab->sec_info[section->id].u.group->link_sec;
12226
12227 /* Get the name of this stub. */
12228 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12229 if (!stub_name)
12230 goto error_ret_free_internal;
12231
12232 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12233 stub_name, FALSE, FALSE);
12234 if (stub_entry != NULL)
12235 {
12236 enum ppc_stub_type old_type;
12237 /* A stub has already been created, but it may
12238 not be the required type. We shouldn't be
12239 transitioning from plt_call to long_branch
12240 stubs or vice versa, but we might be
12241 upgrading from plt_call to plt_call_r2save or
12242 from long_branch to long_branch_r2off. */
12243 free (stub_name);
12244 old_type = stub_entry->stub_type;
12245 switch (old_type)
12246 {
12247 default:
12248 abort ();
12249
12250 case ppc_stub_save_res:
12251 continue;
12252
12253 case ppc_stub_plt_call:
12254 case ppc_stub_plt_call_r2save:
12255 case ppc_stub_plt_call_notoc:
12256 case ppc_stub_plt_call_both:
12257 if (stub_type == ppc_stub_plt_call)
12258 continue;
12259 else if (stub_type == ppc_stub_plt_call_r2save)
12260 {
12261 if (old_type == ppc_stub_plt_call_notoc)
12262 stub_type = ppc_stub_plt_call_both;
12263 }
12264 else if (stub_type == ppc_stub_plt_call_notoc)
12265 {
12266 if (old_type == ppc_stub_plt_call_r2save)
12267 stub_type = ppc_stub_plt_call_both;
12268 }
12269 else
12270 abort ();
12271 break;
12272
12273 case ppc_stub_plt_branch:
12274 case ppc_stub_plt_branch_r2off:
12275 case ppc_stub_plt_branch_notoc:
12276 case ppc_stub_plt_branch_both:
12277 old_type += (ppc_stub_long_branch
12278 - ppc_stub_plt_branch);
12279 /* Fall through. */
12280 case ppc_stub_long_branch:
12281 case ppc_stub_long_branch_r2off:
12282 case ppc_stub_long_branch_notoc:
12283 case ppc_stub_long_branch_both:
12284 if (stub_type == ppc_stub_long_branch)
12285 continue;
12286 else if (stub_type == ppc_stub_long_branch_r2off)
12287 {
12288 if (old_type == ppc_stub_long_branch_notoc)
12289 stub_type = ppc_stub_long_branch_both;
12290 }
12291 else if (stub_type == ppc_stub_long_branch_notoc)
12292 {
12293 if (old_type == ppc_stub_long_branch_r2off)
12294 stub_type = ppc_stub_long_branch_both;
12295 }
12296 else
12297 abort ();
12298 break;
12299 }
12300 if (old_type < stub_type)
12301 stub_entry->stub_type = stub_type;
12302 continue;
12303 }
12304
12305 stub_entry = ppc_add_stub (stub_name, section, info);
12306 if (stub_entry == NULL)
12307 {
12308 free (stub_name);
12309 error_ret_free_internal:
12310 if (elf_section_data (section)->relocs == NULL)
12311 free (internal_relocs);
12312 error_ret_free_local:
12313 if (local_syms != NULL
12314 && (symtab_hdr->contents
12315 != (unsigned char *) local_syms))
12316 free (local_syms);
12317 return FALSE;
12318 }
12319
12320 stub_entry->stub_type = stub_type;
12321 if (stub_type >= ppc_stub_plt_call
12322 && stub_type <= ppc_stub_plt_call_both)
12323 {
12324 stub_entry->target_value = sym_value;
12325 stub_entry->target_section = sym_sec;
12326 }
12327 else
12328 {
12329 stub_entry->target_value = code_value;
12330 stub_entry->target_section = code_sec;
12331 }
12332 stub_entry->h = hash;
12333 stub_entry->plt_ent = plt_ent;
12334 stub_entry->symtype
12335 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
12336 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12337
12338 if (hash != NULL
12339 && (hash->elf.root.type == bfd_link_hash_defined
12340 || hash->elf.root.type == bfd_link_hash_defweak))
12341 htab->stub_globals += 1;
12342 }
12343
12344 /* We're done with the internal relocs, free them. */
12345 if (elf_section_data (section)->relocs != internal_relocs)
12346 free (internal_relocs);
12347 }
12348
12349 if (local_syms != NULL
12350 && symtab_hdr->contents != (unsigned char *) local_syms)
12351 {
12352 if (!info->keep_memory)
12353 free (local_syms);
12354 else
12355 symtab_hdr->contents = (unsigned char *) local_syms;
12356 }
12357 }
12358
12359 /* We may have added some stubs. Find out the new size of the
12360 stub sections. */
12361 for (group = htab->group; group != NULL; group = group->next)
12362 {
12363 group->lr_restore = 0;
12364 group->eh_size = 0;
12365 if (group->stub_sec != NULL)
12366 {
12367 asection *stub_sec = group->stub_sec;
12368
12369 if (htab->stub_iteration <= STUB_SHRINK_ITER
12370 || stub_sec->rawsize < stub_sec->size)
12371 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12372 stub_sec->rawsize = stub_sec->size;
12373 stub_sec->size = 0;
12374 stub_sec->reloc_count = 0;
12375 stub_sec->flags &= ~SEC_RELOC;
12376 }
12377 }
12378
12379 if (htab->stub_iteration <= STUB_SHRINK_ITER
12380 || htab->brlt->rawsize < htab->brlt->size)
12381 htab->brlt->rawsize = htab->brlt->size;
12382 htab->brlt->size = 0;
12383 htab->brlt->reloc_count = 0;
12384 htab->brlt->flags &= ~SEC_RELOC;
12385 if (htab->relbrlt != NULL)
12386 htab->relbrlt->size = 0;
12387
12388 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12389
12390 for (group = htab->group; group != NULL; group = group->next)
12391 if (group->needs_save_res)
12392 group->stub_sec->size += htab->sfpr->size;
12393
12394 if (info->emitrelocations
12395 && htab->glink != NULL && htab->glink->size != 0)
12396 {
12397 htab->glink->reloc_count = 1;
12398 htab->glink->flags |= SEC_RELOC;
12399 }
12400
12401 if (htab->glink_eh_frame != NULL
12402 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12403 && htab->glink_eh_frame->output_section->size > 8)
12404 {
12405 size_t size = 0, align = 4;
12406
12407 for (group = htab->group; group != NULL; group = group->next)
12408 if (group->eh_size != 0)
12409 size += (group->eh_size + 17 + align - 1) & -align;
12410 if (htab->glink != NULL && htab->glink->size != 0)
12411 size += (24 + align - 1) & -align;
12412 if (size != 0)
12413 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12414 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12415 size = (size + align - 1) & -align;
12416 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12417 htab->glink_eh_frame->size = size;
12418 }
12419
12420 if (htab->params->plt_stub_align != 0)
12421 for (group = htab->group; group != NULL; group = group->next)
12422 if (group->stub_sec != NULL)
12423 {
12424 int align = abs (htab->params->plt_stub_align);
12425 group->stub_sec->size
12426 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12427 }
12428
12429 for (group = htab->group; group != NULL; group = group->next)
12430 if (group->stub_sec != NULL
12431 && group->stub_sec->rawsize != group->stub_sec->size
12432 && (htab->stub_iteration <= STUB_SHRINK_ITER
12433 || group->stub_sec->rawsize < group->stub_sec->size))
12434 break;
12435
12436 if (group == NULL
12437 && (htab->brlt->rawsize == htab->brlt->size
12438 || (htab->stub_iteration > STUB_SHRINK_ITER
12439 && htab->brlt->rawsize > htab->brlt->size))
12440 && (htab->glink_eh_frame == NULL
12441 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12442 break;
12443
12444 /* Ask the linker to do its stuff. */
12445 (*htab->params->layout_sections_again) ();
12446 }
12447
12448 if (htab->glink_eh_frame != NULL
12449 && htab->glink_eh_frame->size != 0)
12450 {
12451 bfd_vma val;
12452 bfd_byte *p, *last_fde;
12453 size_t last_fde_len, size, align, pad;
12454 struct map_stub *group;
12455
12456 /* It is necessary to at least have a rough outline of the
12457 linker generated CIEs and FDEs written before
12458 bfd_elf_discard_info is run, in order for these FDEs to be
12459 indexed in .eh_frame_hdr. */
12460 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12461 if (p == NULL)
12462 return FALSE;
12463 htab->glink_eh_frame->contents = p;
12464 last_fde = p;
12465 align = 4;
12466
12467 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12468 /* CIE length (rewrite in case little-endian). */
12469 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12470 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12471 p += last_fde_len + 4;
12472
12473 for (group = htab->group; group != NULL; group = group->next)
12474 if (group->eh_size != 0)
12475 {
12476 group->eh_base = p - htab->glink_eh_frame->contents;
12477 last_fde = p;
12478 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
12479 /* FDE length. */
12480 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12481 p += 4;
12482 /* CIE pointer. */
12483 val = p - htab->glink_eh_frame->contents;
12484 bfd_put_32 (htab->elf.dynobj, val, p);
12485 p += 4;
12486 /* Offset to stub section, written later. */
12487 p += 4;
12488 /* stub section size. */
12489 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
12490 p += 4;
12491 /* Augmentation. */
12492 p += 1;
12493 /* Make sure we don't have all nops. This is enough for
12494 elf-eh-frame.c to detect the last non-nop opcode. */
12495 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
12496 p = last_fde + last_fde_len + 4;
12497 }
12498 if (htab->glink != NULL && htab->glink->size != 0)
12499 {
12500 last_fde = p;
12501 last_fde_len = ((24 + align - 1) & -align) - 4;
12502 /* FDE length. */
12503 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12504 p += 4;
12505 /* CIE pointer. */
12506 val = p - htab->glink_eh_frame->contents;
12507 bfd_put_32 (htab->elf.dynobj, val, p);
12508 p += 4;
12509 /* Offset to .glink, written later. */
12510 p += 4;
12511 /* .glink size. */
12512 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12513 p += 4;
12514 /* Augmentation. */
12515 p += 1;
12516
12517 *p++ = DW_CFA_advance_loc + 1;
12518 *p++ = DW_CFA_register;
12519 *p++ = 65;
12520 *p++ = htab->opd_abi ? 12 : 0;
12521 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
12522 *p++ = DW_CFA_restore_extended;
12523 *p++ = 65;
12524 p += ((24 + align - 1) & -align) - 24;
12525 }
12526 /* Subsume any padding into the last FDE if user .eh_frame
12527 sections are aligned more than glink_eh_frame. Otherwise any
12528 zero padding will be seen as a terminator. */
12529 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12530 size = p - htab->glink_eh_frame->contents;
12531 pad = ((size + align - 1) & -align) - size;
12532 htab->glink_eh_frame->size = size + pad;
12533 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12534 }
12535
12536 maybe_strip_output (info, htab->brlt);
12537 if (htab->glink_eh_frame != NULL)
12538 maybe_strip_output (info, htab->glink_eh_frame);
12539
12540 return TRUE;
12541 }
12542
12543 /* Called after we have determined section placement. If sections
12544 move, we'll be called again. Provide a value for TOCstart. */
12545
12546 bfd_vma
12547 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12548 {
12549 asection *s;
12550 bfd_vma TOCstart, adjust;
12551
12552 if (info != NULL)
12553 {
12554 struct elf_link_hash_entry *h;
12555 struct elf_link_hash_table *htab = elf_hash_table (info);
12556
12557 if (is_elf_hash_table (htab)
12558 && htab->hgot != NULL)
12559 h = htab->hgot;
12560 else
12561 {
12562 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12563 if (is_elf_hash_table (htab))
12564 htab->hgot = h;
12565 }
12566 if (h != NULL
12567 && h->root.type == bfd_link_hash_defined
12568 && !h->root.linker_def
12569 && (!is_elf_hash_table (htab)
12570 || h->def_regular))
12571 {
12572 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12573 + h->root.u.def.section->output_offset
12574 + h->root.u.def.section->output_section->vma);
12575 _bfd_set_gp_value (obfd, TOCstart);
12576 return TOCstart;
12577 }
12578 }
12579
12580 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12581 order. The TOC starts where the first of these sections starts. */
12582 s = bfd_get_section_by_name (obfd, ".got");
12583 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12584 s = bfd_get_section_by_name (obfd, ".toc");
12585 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12586 s = bfd_get_section_by_name (obfd, ".tocbss");
12587 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12588 s = bfd_get_section_by_name (obfd, ".plt");
12589 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12590 {
12591 /* This may happen for
12592 o references to TOC base (SYM@toc / TOC[tc0]) without a
12593 .toc directive
12594 o bad linker script
12595 o --gc-sections and empty TOC sections
12596
12597 FIXME: Warn user? */
12598
12599 /* Look for a likely section. We probably won't even be
12600 using TOCstart. */
12601 for (s = obfd->sections; s != NULL; s = s->next)
12602 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12603 | SEC_EXCLUDE))
12604 == (SEC_ALLOC | SEC_SMALL_DATA))
12605 break;
12606 if (s == NULL)
12607 for (s = obfd->sections; s != NULL; s = s->next)
12608 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12609 == (SEC_ALLOC | SEC_SMALL_DATA))
12610 break;
12611 if (s == NULL)
12612 for (s = obfd->sections; s != NULL; s = s->next)
12613 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12614 == SEC_ALLOC)
12615 break;
12616 if (s == NULL)
12617 for (s = obfd->sections; s != NULL; s = s->next)
12618 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12619 break;
12620 }
12621
12622 TOCstart = 0;
12623 if (s != NULL)
12624 TOCstart = s->output_section->vma + s->output_offset;
12625
12626 /* Force alignment. */
12627 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12628 TOCstart -= adjust;
12629 _bfd_set_gp_value (obfd, TOCstart);
12630
12631 if (info != NULL && s != NULL)
12632 {
12633 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12634
12635 if (htab != NULL)
12636 {
12637 if (htab->elf.hgot != NULL)
12638 {
12639 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12640 htab->elf.hgot->root.u.def.section = s;
12641 }
12642 }
12643 else
12644 {
12645 struct bfd_link_hash_entry *bh = NULL;
12646 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12647 s, TOC_BASE_OFF - adjust,
12648 NULL, FALSE, FALSE, &bh);
12649 }
12650 }
12651 return TOCstart;
12652 }
12653
12654 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12655 write out any global entry stubs, and PLT relocations. */
12656
12657 static bfd_boolean
12658 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
12659 {
12660 struct bfd_link_info *info;
12661 struct ppc_link_hash_table *htab;
12662 struct plt_entry *ent;
12663 asection *s;
12664
12665 if (h->root.type == bfd_link_hash_indirect)
12666 return TRUE;
12667
12668 info = inf;
12669 htab = ppc_hash_table (info);
12670 if (htab == NULL)
12671 return FALSE;
12672
12673 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12674 if (ent->plt.offset != (bfd_vma) -1)
12675 {
12676 /* This symbol has an entry in the procedure linkage
12677 table. Set it up. */
12678 Elf_Internal_Rela rela;
12679 asection *plt, *relplt;
12680 bfd_byte *loc;
12681
12682 if (!htab->elf.dynamic_sections_created
12683 || h->dynindx == -1)
12684 {
12685 if (!(h->def_regular
12686 && (h->root.type == bfd_link_hash_defined
12687 || h->root.type == bfd_link_hash_defweak)))
12688 continue;
12689 if (h->type == STT_GNU_IFUNC)
12690 {
12691 plt = htab->elf.iplt;
12692 relplt = htab->elf.irelplt;
12693 htab->local_ifunc_resolver = 1;
12694 if (htab->opd_abi)
12695 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
12696 else
12697 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12698 }
12699 else
12700 {
12701 plt = htab->pltlocal;
12702 if (bfd_link_pic (info))
12703 {
12704 relplt = htab->relpltlocal;
12705 if (htab->opd_abi)
12706 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
12707 else
12708 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12709 }
12710 else
12711 relplt = NULL;
12712 }
12713 rela.r_addend = (h->root.u.def.value
12714 + h->root.u.def.section->output_offset
12715 + h->root.u.def.section->output_section->vma
12716 + ent->addend);
12717
12718 if (relplt == NULL)
12719 {
12720 loc = plt->contents + ent->plt.offset;
12721 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
12722 if (htab->opd_abi)
12723 {
12724 bfd_vma toc = elf_gp (info->output_bfd);
12725 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
12726 bfd_put_64 (info->output_bfd, toc, loc + 8);
12727 }
12728 }
12729 else
12730 {
12731 rela.r_offset = (plt->output_section->vma
12732 + plt->output_offset
12733 + ent->plt.offset);
12734 loc = relplt->contents + (relplt->reloc_count++
12735 * sizeof (Elf64_External_Rela));
12736 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12737 }
12738 }
12739 else
12740 {
12741 rela.r_offset = (htab->elf.splt->output_section->vma
12742 + htab->elf.splt->output_offset
12743 + ent->plt.offset);
12744 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
12745 rela.r_addend = ent->addend;
12746 loc = (htab->elf.srelplt->contents
12747 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
12748 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
12749 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
12750 htab->maybe_local_ifunc_resolver = 1;
12751 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12752 }
12753 }
12754
12755 if (!h->pointer_equality_needed)
12756 return TRUE;
12757
12758 if (h->def_regular)
12759 return TRUE;
12760
12761 s = htab->global_entry;
12762 if (s == NULL || s->size == 0)
12763 return TRUE;
12764
12765 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12766 if (ent->plt.offset != (bfd_vma) -1
12767 && ent->addend == 0)
12768 {
12769 bfd_byte *p;
12770 asection *plt;
12771 bfd_vma off;
12772
12773 p = s->contents + h->root.u.def.value;
12774 plt = htab->elf.splt;
12775 if (!htab->elf.dynamic_sections_created
12776 || h->dynindx == -1)
12777 {
12778 if (h->type == STT_GNU_IFUNC)
12779 plt = htab->elf.iplt;
12780 else
12781 plt = htab->pltlocal;
12782 }
12783 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
12784 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12785
12786 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12787 {
12788 info->callbacks->einfo
12789 (_("%P: linkage table error against `%pT'\n"),
12790 h->root.root.string);
12791 bfd_set_error (bfd_error_bad_value);
12792 htab->stub_error = TRUE;
12793 }
12794
12795 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12796 if (htab->params->emit_stub_syms)
12797 {
12798 size_t len = strlen (h->root.root.string);
12799 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12800
12801 if (name == NULL)
12802 return FALSE;
12803
12804 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12805 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12806 if (h == NULL)
12807 return FALSE;
12808 if (h->root.type == bfd_link_hash_new)
12809 {
12810 h->root.type = bfd_link_hash_defined;
12811 h->root.u.def.section = s;
12812 h->root.u.def.value = p - s->contents;
12813 h->ref_regular = 1;
12814 h->def_regular = 1;
12815 h->ref_regular_nonweak = 1;
12816 h->forced_local = 1;
12817 h->non_elf = 0;
12818 h->root.linker_def = 1;
12819 }
12820 }
12821
12822 if (PPC_HA (off) != 0)
12823 {
12824 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12825 p += 4;
12826 }
12827 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12828 p += 4;
12829 bfd_put_32 (s->owner, MTCTR_R12, p);
12830 p += 4;
12831 bfd_put_32 (s->owner, BCTR, p);
12832 break;
12833 }
12834 return TRUE;
12835 }
12836
12837 /* Write PLT relocs for locals. */
12838
12839 static bfd_boolean
12840 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
12841 {
12842 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12843 bfd *ibfd;
12844
12845 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12846 {
12847 struct got_entry **lgot_ents, **end_lgot_ents;
12848 struct plt_entry **local_plt, **lplt, **end_local_plt;
12849 Elf_Internal_Shdr *symtab_hdr;
12850 bfd_size_type locsymcount;
12851 Elf_Internal_Sym *local_syms = NULL;
12852 struct plt_entry *ent;
12853
12854 if (!is_ppc64_elf (ibfd))
12855 continue;
12856
12857 lgot_ents = elf_local_got_ents (ibfd);
12858 if (!lgot_ents)
12859 continue;
12860
12861 symtab_hdr = &elf_symtab_hdr (ibfd);
12862 locsymcount = symtab_hdr->sh_info;
12863 end_lgot_ents = lgot_ents + locsymcount;
12864 local_plt = (struct plt_entry **) end_lgot_ents;
12865 end_local_plt = local_plt + locsymcount;
12866 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
12867 for (ent = *lplt; ent != NULL; ent = ent->next)
12868 if (ent->plt.offset != (bfd_vma) -1)
12869 {
12870 Elf_Internal_Sym *sym;
12871 asection *sym_sec;
12872 asection *plt, *relplt;
12873 bfd_byte *loc;
12874 bfd_vma val;
12875
12876 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
12877 lplt - local_plt, ibfd))
12878 {
12879 if (local_syms != NULL
12880 && symtab_hdr->contents != (unsigned char *) local_syms)
12881 free (local_syms);
12882 return FALSE;
12883 }
12884
12885 val = sym->st_value + ent->addend;
12886 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
12887 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
12888 if (sym_sec != NULL && sym_sec->output_section != NULL)
12889 val += sym_sec->output_offset + sym_sec->output_section->vma;
12890
12891 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12892 {
12893 htab->local_ifunc_resolver = 1;
12894 plt = htab->elf.iplt;
12895 relplt = htab->elf.irelplt;
12896 }
12897 else
12898 {
12899 plt = htab->pltlocal;
12900 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
12901 }
12902
12903 if (relplt == NULL)
12904 {
12905 loc = plt->contents + ent->plt.offset;
12906 bfd_put_64 (info->output_bfd, val, loc);
12907 if (htab->opd_abi)
12908 {
12909 bfd_vma toc = elf_gp (ibfd);
12910 bfd_put_64 (info->output_bfd, toc, loc + 8);
12911 }
12912 }
12913 else
12914 {
12915 Elf_Internal_Rela rela;
12916 rela.r_offset = (ent->plt.offset
12917 + plt->output_offset
12918 + plt->output_section->vma);
12919 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12920 {
12921 if (htab->opd_abi)
12922 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
12923 else
12924 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12925 }
12926 else
12927 {
12928 if (htab->opd_abi)
12929 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
12930 else
12931 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12932 }
12933 rela.r_addend = val;
12934 loc = relplt->contents + (relplt->reloc_count++
12935 * sizeof (Elf64_External_Rela));
12936 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12937 }
12938 }
12939
12940 if (local_syms != NULL
12941 && symtab_hdr->contents != (unsigned char *) local_syms)
12942 {
12943 if (!info->keep_memory)
12944 free (local_syms);
12945 else
12946 symtab_hdr->contents = (unsigned char *) local_syms;
12947 }
12948 }
12949 return TRUE;
12950 }
12951
12952 /* Build all the stubs associated with the current output file.
12953 The stubs are kept in a hash table attached to the main linker
12954 hash table. This function is called via gldelf64ppc_finish. */
12955
12956 bfd_boolean
12957 ppc64_elf_build_stubs (struct bfd_link_info *info,
12958 char **stats)
12959 {
12960 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12961 struct map_stub *group;
12962 asection *stub_sec;
12963 bfd_byte *p;
12964 int stub_sec_count = 0;
12965
12966 if (htab == NULL)
12967 return FALSE;
12968
12969 /* Allocate memory to hold the linker stubs. */
12970 for (group = htab->group; group != NULL; group = group->next)
12971 {
12972 group->eh_size = 0;
12973 group->lr_restore = 0;
12974 if ((stub_sec = group->stub_sec) != NULL
12975 && stub_sec->size != 0)
12976 {
12977 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
12978 stub_sec->size);
12979 if (stub_sec->contents == NULL)
12980 return FALSE;
12981 stub_sec->size = 0;
12982 }
12983 }
12984
12985 if (htab->glink != NULL && htab->glink->size != 0)
12986 {
12987 unsigned int indx;
12988 bfd_vma plt0;
12989
12990 /* Build the .glink plt call stub. */
12991 if (htab->params->emit_stub_syms)
12992 {
12993 struct elf_link_hash_entry *h;
12994 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12995 TRUE, FALSE, FALSE);
12996 if (h == NULL)
12997 return FALSE;
12998 if (h->root.type == bfd_link_hash_new)
12999 {
13000 h->root.type = bfd_link_hash_defined;
13001 h->root.u.def.section = htab->glink;
13002 h->root.u.def.value = 8;
13003 h->ref_regular = 1;
13004 h->def_regular = 1;
13005 h->ref_regular_nonweak = 1;
13006 h->forced_local = 1;
13007 h->non_elf = 0;
13008 h->root.linker_def = 1;
13009 }
13010 }
13011 plt0 = (htab->elf.splt->output_section->vma
13012 + htab->elf.splt->output_offset
13013 - 16);
13014 if (info->emitrelocations)
13015 {
13016 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13017 if (r == NULL)
13018 return FALSE;
13019 r->r_offset = (htab->glink->output_offset
13020 + htab->glink->output_section->vma);
13021 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13022 r->r_addend = plt0;
13023 }
13024 p = htab->glink->contents;
13025 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13026 bfd_put_64 (htab->glink->owner, plt0, p);
13027 p += 8;
13028 if (htab->opd_abi)
13029 {
13030 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13031 p += 4;
13032 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13033 p += 4;
13034 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13035 p += 4;
13036 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13037 p += 4;
13038 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13039 p += 4;
13040 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13041 p += 4;
13042 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13043 p += 4;
13044 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13045 p += 4;
13046 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13047 p += 4;
13048 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13049 p += 4;
13050 }
13051 else
13052 {
13053 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13054 p += 4;
13055 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13056 p += 4;
13057 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13058 p += 4;
13059 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13060 p += 4;
13061 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13062 p += 4;
13063 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13064 p += 4;
13065 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13066 p += 4;
13067 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13068 p += 4;
13069 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13070 p += 4;
13071 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13072 p += 4;
13073 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13074 p += 4;
13075 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13076 p += 4;
13077 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13078 p += 4;
13079 }
13080 bfd_put_32 (htab->glink->owner, BCTR, p);
13081 p += 4;
13082 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13083
13084 /* Build the .glink lazy link call stubs. */
13085 indx = 0;
13086 while (p < htab->glink->contents + htab->glink->size)
13087 {
13088 if (htab->opd_abi)
13089 {
13090 if (indx < 0x8000)
13091 {
13092 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13093 p += 4;
13094 }
13095 else
13096 {
13097 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13098 p += 4;
13099 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13100 p);
13101 p += 4;
13102 }
13103 }
13104 bfd_put_32 (htab->glink->owner,
13105 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13106 indx++;
13107 p += 4;
13108 }
13109 }
13110
13111 /* Build .glink global entry stubs, and PLT relocs for globals. */
13112 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13113
13114 if (!write_plt_relocs_for_local_syms (info))
13115 return FALSE;
13116
13117 if (htab->brlt != NULL && htab->brlt->size != 0)
13118 {
13119 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13120 htab->brlt->size);
13121 if (htab->brlt->contents == NULL)
13122 return FALSE;
13123 }
13124 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13125 {
13126 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13127 htab->relbrlt->size);
13128 if (htab->relbrlt->contents == NULL)
13129 return FALSE;
13130 }
13131
13132 /* Build the stubs as directed by the stub hash table. */
13133 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13134
13135 for (group = htab->group; group != NULL; group = group->next)
13136 if (group->needs_save_res)
13137 group->stub_sec->size += htab->sfpr->size;
13138
13139 if (htab->relbrlt != NULL)
13140 htab->relbrlt->reloc_count = 0;
13141
13142 if (htab->params->plt_stub_align != 0)
13143 for (group = htab->group; group != NULL; group = group->next)
13144 if ((stub_sec = group->stub_sec) != NULL)
13145 {
13146 int align = abs (htab->params->plt_stub_align);
13147 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13148 }
13149
13150 for (group = htab->group; group != NULL; group = group->next)
13151 if (group->needs_save_res)
13152 {
13153 stub_sec = group->stub_sec;
13154 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13155 htab->sfpr->contents, htab->sfpr->size);
13156 if (htab->params->emit_stub_syms)
13157 {
13158 unsigned int i;
13159
13160 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13161 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13162 return FALSE;
13163 }
13164 }
13165
13166 if (htab->glink_eh_frame != NULL
13167 && htab->glink_eh_frame->size != 0)
13168 {
13169 bfd_vma val;
13170 size_t align = 4;
13171
13172 p = htab->glink_eh_frame->contents;
13173 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13174
13175 for (group = htab->group; group != NULL; group = group->next)
13176 if (group->eh_size != 0)
13177 {
13178 /* Offset to stub section. */
13179 val = (group->stub_sec->output_section->vma
13180 + group->stub_sec->output_offset);
13181 val -= (htab->glink_eh_frame->output_section->vma
13182 + htab->glink_eh_frame->output_offset
13183 + (p + 8 - htab->glink_eh_frame->contents));
13184 if (val + 0x80000000 > 0xffffffff)
13185 {
13186 _bfd_error_handler
13187 (_("%s offset too large for .eh_frame sdata4 encoding"),
13188 group->stub_sec->name);
13189 return FALSE;
13190 }
13191 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13192 p += (group->eh_size + 17 + 3) & -4;
13193 }
13194 if (htab->glink != NULL && htab->glink->size != 0)
13195 {
13196 /* Offset to .glink. */
13197 val = (htab->glink->output_section->vma
13198 + htab->glink->output_offset
13199 + 8);
13200 val -= (htab->glink_eh_frame->output_section->vma
13201 + htab->glink_eh_frame->output_offset
13202 + (p + 8 - htab->glink_eh_frame->contents));
13203 if (val + 0x80000000 > 0xffffffff)
13204 {
13205 _bfd_error_handler
13206 (_("%s offset too large for .eh_frame sdata4 encoding"),
13207 htab->glink->name);
13208 return FALSE;
13209 }
13210 bfd_put_32 (htab->elf.dynobj, val, p + 8);
13211 p += (24 + align - 1) & -align;
13212 }
13213 }
13214
13215 for (group = htab->group; group != NULL; group = group->next)
13216 if ((stub_sec = group->stub_sec) != NULL)
13217 {
13218 stub_sec_count += 1;
13219 if (stub_sec->rawsize != stub_sec->size
13220 && (htab->stub_iteration <= STUB_SHRINK_ITER
13221 || stub_sec->rawsize < stub_sec->size))
13222 break;
13223 }
13224
13225 if (group != NULL)
13226 {
13227 htab->stub_error = TRUE;
13228 _bfd_error_handler (_("stubs don't match calculated size"));
13229 }
13230
13231 if (htab->stub_error)
13232 return FALSE;
13233
13234 if (stats != NULL)
13235 {
13236 size_t len;
13237 *stats = bfd_malloc (500);
13238 if (*stats == NULL)
13239 return FALSE;
13240
13241 len = sprintf (*stats,
13242 ngettext ("linker stubs in %u group\n",
13243 "linker stubs in %u groups\n",
13244 stub_sec_count),
13245 stub_sec_count);
13246 sprintf (*stats + len, _(" branch %lu\n"
13247 " branch toc adj %lu\n"
13248 " branch notoc %lu\n"
13249 " branch both %lu\n"
13250 " long branch %lu\n"
13251 " long toc adj %lu\n"
13252 " long notoc %lu\n"
13253 " long both %lu\n"
13254 " plt call %lu\n"
13255 " plt call save %lu\n"
13256 " plt call notoc %lu\n"
13257 " plt call both %lu\n"
13258 " global entry %lu"),
13259 htab->stub_count[ppc_stub_long_branch - 1],
13260 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13261 htab->stub_count[ppc_stub_long_branch_notoc - 1],
13262 htab->stub_count[ppc_stub_long_branch_both - 1],
13263 htab->stub_count[ppc_stub_plt_branch - 1],
13264 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13265 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
13266 htab->stub_count[ppc_stub_plt_branch_both - 1],
13267 htab->stub_count[ppc_stub_plt_call - 1],
13268 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13269 htab->stub_count[ppc_stub_plt_call_notoc - 1],
13270 htab->stub_count[ppc_stub_plt_call_both - 1],
13271 htab->stub_count[ppc_stub_global_entry - 1]);
13272 }
13273 return TRUE;
13274 }
13275
13276 /* What to do when ld finds relocations against symbols defined in
13277 discarded sections. */
13278
13279 static unsigned int
13280 ppc64_elf_action_discarded (asection *sec)
13281 {
13282 if (strcmp (".opd", sec->name) == 0)
13283 return 0;
13284
13285 if (strcmp (".toc", sec->name) == 0)
13286 return 0;
13287
13288 if (strcmp (".toc1", sec->name) == 0)
13289 return 0;
13290
13291 return _bfd_elf_default_action_discarded (sec);
13292 }
13293
13294 /* The RELOCATE_SECTION function is called by the ELF backend linker
13295 to handle the relocations for a section.
13296
13297 The relocs are always passed as Rela structures; if the section
13298 actually uses Rel structures, the r_addend field will always be
13299 zero.
13300
13301 This function is responsible for adjust the section contents as
13302 necessary, and (if using Rela relocs and generating a
13303 relocatable output file) adjusting the reloc addend as
13304 necessary.
13305
13306 This function does not have to worry about setting the reloc
13307 address or the reloc symbol index.
13308
13309 LOCAL_SYMS is a pointer to the swapped in local symbols.
13310
13311 LOCAL_SECTIONS is an array giving the section in the input file
13312 corresponding to the st_shndx field of each local symbol.
13313
13314 The global hash table entry for the global symbols can be found
13315 via elf_sym_hashes (input_bfd).
13316
13317 When generating relocatable output, this function must handle
13318 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13319 going to be the section symbol corresponding to the output
13320 section, which means that the addend must be adjusted
13321 accordingly. */
13322
13323 static bfd_boolean
13324 ppc64_elf_relocate_section (bfd *output_bfd,
13325 struct bfd_link_info *info,
13326 bfd *input_bfd,
13327 asection *input_section,
13328 bfd_byte *contents,
13329 Elf_Internal_Rela *relocs,
13330 Elf_Internal_Sym *local_syms,
13331 asection **local_sections)
13332 {
13333 struct ppc_link_hash_table *htab;
13334 Elf_Internal_Shdr *symtab_hdr;
13335 struct elf_link_hash_entry **sym_hashes;
13336 Elf_Internal_Rela *rel;
13337 Elf_Internal_Rela *wrel;
13338 Elf_Internal_Rela *relend;
13339 Elf_Internal_Rela outrel;
13340 bfd_byte *loc;
13341 struct got_entry **local_got_ents;
13342 bfd_vma TOCstart;
13343 bfd_boolean ret = TRUE;
13344 bfd_boolean is_opd;
13345 /* Assume 'at' branch hints. */
13346 bfd_boolean is_isa_v2 = TRUE;
13347 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13348
13349 /* Initialize howto table if needed. */
13350 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13351 ppc_howto_init ();
13352
13353 htab = ppc_hash_table (info);
13354 if (htab == NULL)
13355 return FALSE;
13356
13357 /* Don't relocate stub sections. */
13358 if (input_section->owner == htab->params->stub_bfd)
13359 return TRUE;
13360
13361 if (!is_ppc64_elf (input_bfd))
13362 {
13363 bfd_set_error (bfd_error_wrong_format);
13364 return FALSE;
13365 }
13366
13367 local_got_ents = elf_local_got_ents (input_bfd);
13368 TOCstart = elf_gp (output_bfd);
13369 symtab_hdr = &elf_symtab_hdr (input_bfd);
13370 sym_hashes = elf_sym_hashes (input_bfd);
13371 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13372
13373 rel = wrel = relocs;
13374 relend = relocs + input_section->reloc_count;
13375 for (; rel < relend; wrel++, rel++)
13376 {
13377 enum elf_ppc64_reloc_type r_type;
13378 bfd_vma addend;
13379 bfd_reloc_status_type r;
13380 Elf_Internal_Sym *sym;
13381 asection *sec;
13382 struct elf_link_hash_entry *h_elf;
13383 struct ppc_link_hash_entry *h;
13384 struct ppc_link_hash_entry *fdh;
13385 const char *sym_name;
13386 unsigned long r_symndx, toc_symndx;
13387 bfd_vma toc_addend;
13388 unsigned char tls_mask, tls_gd, tls_type;
13389 unsigned char sym_type;
13390 bfd_vma relocation;
13391 bfd_boolean unresolved_reloc, save_unresolved_reloc;
13392 bfd_boolean warned;
13393 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13394 unsigned int insn;
13395 unsigned int mask;
13396 struct ppc_stub_hash_entry *stub_entry;
13397 bfd_vma max_br_offset;
13398 bfd_vma from;
13399 Elf_Internal_Rela orig_rel;
13400 reloc_howto_type *howto;
13401 struct reloc_howto_struct alt_howto;
13402
13403 again:
13404 orig_rel = *rel;
13405
13406 r_type = ELF64_R_TYPE (rel->r_info);
13407 r_symndx = ELF64_R_SYM (rel->r_info);
13408
13409 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13410 symbol of the previous ADDR64 reloc. The symbol gives us the
13411 proper TOC base to use. */
13412 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13413 && wrel != relocs
13414 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13415 && is_opd)
13416 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13417
13418 sym = NULL;
13419 sec = NULL;
13420 h_elf = NULL;
13421 sym_name = NULL;
13422 unresolved_reloc = FALSE;
13423 warned = FALSE;
13424
13425 if (r_symndx < symtab_hdr->sh_info)
13426 {
13427 /* It's a local symbol. */
13428 struct _opd_sec_data *opd;
13429
13430 sym = local_syms + r_symndx;
13431 sec = local_sections[r_symndx];
13432 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13433 sym_type = ELF64_ST_TYPE (sym->st_info);
13434 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13435 opd = get_opd_info (sec);
13436 if (opd != NULL && opd->adjust != NULL)
13437 {
13438 long adjust = opd->adjust[OPD_NDX (sym->st_value
13439 + rel->r_addend)];
13440 if (adjust == -1)
13441 relocation = 0;
13442 else
13443 {
13444 /* If this is a relocation against the opd section sym
13445 and we have edited .opd, adjust the reloc addend so
13446 that ld -r and ld --emit-relocs output is correct.
13447 If it is a reloc against some other .opd symbol,
13448 then the symbol value will be adjusted later. */
13449 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13450 rel->r_addend += adjust;
13451 else
13452 relocation += adjust;
13453 }
13454 }
13455 }
13456 else
13457 {
13458 bfd_boolean ignored;
13459
13460 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13461 r_symndx, symtab_hdr, sym_hashes,
13462 h_elf, sec, relocation,
13463 unresolved_reloc, warned, ignored);
13464 sym_name = h_elf->root.root.string;
13465 sym_type = h_elf->type;
13466 if (sec != NULL
13467 && sec->owner == output_bfd
13468 && strcmp (sec->name, ".opd") == 0)
13469 {
13470 /* This is a symbol defined in a linker script. All
13471 such are defined in output sections, even those
13472 defined by simple assignment from a symbol defined in
13473 an input section. Transfer the symbol to an
13474 appropriate input .opd section, so that a branch to
13475 this symbol will be mapped to the location specified
13476 by the opd entry. */
13477 struct bfd_link_order *lo;
13478 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13479 if (lo->type == bfd_indirect_link_order)
13480 {
13481 asection *isec = lo->u.indirect.section;
13482 if (h_elf->root.u.def.value >= isec->output_offset
13483 && h_elf->root.u.def.value < (isec->output_offset
13484 + isec->size))
13485 {
13486 h_elf->root.u.def.value -= isec->output_offset;
13487 h_elf->root.u.def.section = isec;
13488 sec = isec;
13489 break;
13490 }
13491 }
13492 }
13493 }
13494 h = (struct ppc_link_hash_entry *) h_elf;
13495
13496 if (sec != NULL && discarded_section (sec))
13497 {
13498 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13499 input_bfd, input_section,
13500 contents, rel->r_offset);
13501 wrel->r_offset = rel->r_offset;
13502 wrel->r_info = 0;
13503 wrel->r_addend = 0;
13504
13505 /* For ld -r, remove relocations in debug sections against
13506 symbols defined in discarded sections. Not done for
13507 non-debug to preserve relocs in .eh_frame which the
13508 eh_frame editing code expects to be present. */
13509 if (bfd_link_relocatable (info)
13510 && (input_section->flags & SEC_DEBUGGING))
13511 wrel--;
13512
13513 continue;
13514 }
13515
13516 if (bfd_link_relocatable (info))
13517 goto copy_reloc;
13518
13519 if (h != NULL && &h->elf == htab->elf.hgot)
13520 {
13521 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13522 sec = bfd_abs_section_ptr;
13523 unresolved_reloc = FALSE;
13524 }
13525
13526 /* TLS optimizations. Replace instruction sequences and relocs
13527 based on information we collected in tls_optimize. We edit
13528 RELOCS so that --emit-relocs will output something sensible
13529 for the final instruction stream. */
13530 tls_mask = 0;
13531 tls_gd = 0;
13532 toc_symndx = 0;
13533 if (h != NULL)
13534 tls_mask = h->tls_mask;
13535 else if (local_got_ents != NULL)
13536 {
13537 struct plt_entry **local_plt = (struct plt_entry **)
13538 (local_got_ents + symtab_hdr->sh_info);
13539 unsigned char *lgot_masks = (unsigned char *)
13540 (local_plt + symtab_hdr->sh_info);
13541 tls_mask = lgot_masks[r_symndx];
13542 }
13543 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
13544 && (r_type == R_PPC64_TLS
13545 || r_type == R_PPC64_TLSGD
13546 || r_type == R_PPC64_TLSLD))
13547 {
13548 /* Check for toc tls entries. */
13549 unsigned char *toc_tls;
13550
13551 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13552 &local_syms, rel, input_bfd))
13553 return FALSE;
13554
13555 if (toc_tls)
13556 tls_mask = *toc_tls;
13557 }
13558
13559 /* Check that tls relocs are used with tls syms, and non-tls
13560 relocs are used with non-tls syms. */
13561 if (r_symndx != STN_UNDEF
13562 && r_type != R_PPC64_NONE
13563 && (h == NULL
13564 || h->elf.root.type == bfd_link_hash_defined
13565 || h->elf.root.type == bfd_link_hash_defweak)
13566 && (IS_PPC64_TLS_RELOC (r_type)
13567 != (sym_type == STT_TLS
13568 || (sym_type == STT_SECTION
13569 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13570 {
13571 if ((tls_mask & TLS_TLS) != 0
13572 && (r_type == R_PPC64_TLS
13573 || r_type == R_PPC64_TLSGD
13574 || r_type == R_PPC64_TLSLD))
13575 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13576 ;
13577 else
13578 info->callbacks->einfo
13579 (!IS_PPC64_TLS_RELOC (r_type)
13580 /* xgettext:c-format */
13581 ? _("%H: %s used with TLS symbol `%pT'\n")
13582 /* xgettext:c-format */
13583 : _("%H: %s used with non-TLS symbol `%pT'\n"),
13584 input_bfd, input_section, rel->r_offset,
13585 ppc64_elf_howto_table[r_type]->name,
13586 sym_name);
13587 }
13588
13589 /* Ensure reloc mapping code below stays sane. */
13590 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13591 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13592 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13593 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13594 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13595 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13596 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13597 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13598 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13599 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13600 abort ();
13601
13602 switch (r_type)
13603 {
13604 default:
13605 break;
13606
13607 case R_PPC64_LO_DS_OPT:
13608 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13609 if ((insn & (0x3f << 26)) != 58u << 26)
13610 abort ();
13611 insn += (14u << 26) - (58u << 26);
13612 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13613 r_type = R_PPC64_TOC16_LO;
13614 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13615 break;
13616
13617 case R_PPC64_TOC16:
13618 case R_PPC64_TOC16_LO:
13619 case R_PPC64_TOC16_DS:
13620 case R_PPC64_TOC16_LO_DS:
13621 {
13622 /* Check for toc tls entries. */
13623 unsigned char *toc_tls;
13624 int retval;
13625
13626 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13627 &local_syms, rel, input_bfd);
13628 if (retval == 0)
13629 return FALSE;
13630
13631 if (toc_tls)
13632 {
13633 tls_mask = *toc_tls;
13634 if (r_type == R_PPC64_TOC16_DS
13635 || r_type == R_PPC64_TOC16_LO_DS)
13636 {
13637 if ((tls_mask & TLS_TLS) != 0
13638 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13639 goto toctprel;
13640 }
13641 else
13642 {
13643 /* If we found a GD reloc pair, then we might be
13644 doing a GD->IE transition. */
13645 if (retval == 2)
13646 {
13647 tls_gd = TLS_TPRELGD;
13648 if ((tls_mask & TLS_TLS) != 0
13649 && (tls_mask & TLS_GD) == 0)
13650 goto tls_ldgd_opt;
13651 }
13652 else if (retval == 3)
13653 {
13654 if ((tls_mask & TLS_TLS) != 0
13655 && (tls_mask & TLS_LD) == 0)
13656 goto tls_ldgd_opt;
13657 }
13658 }
13659 }
13660 }
13661 break;
13662
13663 case R_PPC64_GOT_TPREL16_HI:
13664 case R_PPC64_GOT_TPREL16_HA:
13665 if ((tls_mask & TLS_TLS) != 0
13666 && (tls_mask & TLS_TPREL) == 0)
13667 {
13668 rel->r_offset -= d_offset;
13669 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13670 r_type = R_PPC64_NONE;
13671 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13672 }
13673 break;
13674
13675 case R_PPC64_GOT_TPREL16_DS:
13676 case R_PPC64_GOT_TPREL16_LO_DS:
13677 if ((tls_mask & TLS_TLS) != 0
13678 && (tls_mask & TLS_TPREL) == 0)
13679 {
13680 toctprel:
13681 insn = bfd_get_32 (input_bfd,
13682 contents + rel->r_offset - d_offset);
13683 insn &= 31 << 21;
13684 insn |= 0x3c0d0000; /* addis 0,13,0 */
13685 bfd_put_32 (input_bfd, insn,
13686 contents + rel->r_offset - d_offset);
13687 r_type = R_PPC64_TPREL16_HA;
13688 if (toc_symndx != 0)
13689 {
13690 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13691 rel->r_addend = toc_addend;
13692 /* We changed the symbol. Start over in order to
13693 get h, sym, sec etc. right. */
13694 goto again;
13695 }
13696 else
13697 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13698 }
13699 break;
13700
13701 case R_PPC64_TLS:
13702 if ((tls_mask & TLS_TLS) != 0
13703 && (tls_mask & TLS_TPREL) == 0)
13704 {
13705 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13706 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13707 if (insn == 0)
13708 abort ();
13709 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
13710 /* Was PPC64_TLS which sits on insn boundary, now
13711 PPC64_TPREL16_LO which is at low-order half-word. */
13712 rel->r_offset += d_offset;
13713 r_type = R_PPC64_TPREL16_LO;
13714 if (toc_symndx != 0)
13715 {
13716 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13717 rel->r_addend = toc_addend;
13718 /* We changed the symbol. Start over in order to
13719 get h, sym, sec etc. right. */
13720 goto again;
13721 }
13722 else
13723 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13724 }
13725 break;
13726
13727 case R_PPC64_GOT_TLSGD16_HI:
13728 case R_PPC64_GOT_TLSGD16_HA:
13729 tls_gd = TLS_TPRELGD;
13730 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
13731 goto tls_gdld_hi;
13732 break;
13733
13734 case R_PPC64_GOT_TLSLD16_HI:
13735 case R_PPC64_GOT_TLSLD16_HA:
13736 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
13737 {
13738 tls_gdld_hi:
13739 if ((tls_mask & tls_gd) != 0)
13740 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13741 + R_PPC64_GOT_TPREL16_DS);
13742 else
13743 {
13744 rel->r_offset -= d_offset;
13745 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13746 r_type = R_PPC64_NONE;
13747 }
13748 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13749 }
13750 break;
13751
13752 case R_PPC64_GOT_TLSGD16:
13753 case R_PPC64_GOT_TLSGD16_LO:
13754 tls_gd = TLS_TPRELGD;
13755 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
13756 goto tls_ldgd_opt;
13757 break;
13758
13759 case R_PPC64_GOT_TLSLD16:
13760 case R_PPC64_GOT_TLSLD16_LO:
13761 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
13762 {
13763 unsigned int insn1, insn2;
13764 bfd_vma offset;
13765
13766 tls_ldgd_opt:
13767 offset = (bfd_vma) -1;
13768 /* If not using the newer R_PPC64_TLSGD/LD to mark
13769 __tls_get_addr calls, we must trust that the call
13770 stays with its arg setup insns, ie. that the next
13771 reloc is the __tls_get_addr call associated with
13772 the current reloc. Edit both insns. */
13773 if (input_section->has_tls_get_addr_call
13774 && rel + 1 < relend
13775 && branch_reloc_hash_match (input_bfd, rel + 1,
13776 htab->tls_get_addr,
13777 htab->tls_get_addr_fd))
13778 offset = rel[1].r_offset;
13779 /* We read the low GOT_TLS (or TOC16) insn because we
13780 need to keep the destination reg. It may be
13781 something other than the usual r3, and moved to r3
13782 before the call by intervening code. */
13783 insn1 = bfd_get_32 (input_bfd,
13784 contents + rel->r_offset - d_offset);
13785 if ((tls_mask & tls_gd) != 0)
13786 {
13787 /* IE */
13788 insn1 &= (0x1f << 21) | (0x1f << 16);
13789 insn1 |= 58 << 26; /* ld */
13790 insn2 = 0x7c636a14; /* add 3,3,13 */
13791 if (offset != (bfd_vma) -1)
13792 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13793 if ((tls_mask & TLS_EXPLICIT) == 0)
13794 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13795 + R_PPC64_GOT_TPREL16_DS);
13796 else
13797 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13798 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13799 }
13800 else
13801 {
13802 /* LE */
13803 insn1 &= 0x1f << 21;
13804 insn1 |= 0x3c0d0000; /* addis r,13,0 */
13805 insn2 = 0x38630000; /* addi 3,3,0 */
13806 if (tls_gd == 0)
13807 {
13808 /* Was an LD reloc. */
13809 if (toc_symndx)
13810 sec = local_sections[toc_symndx];
13811 for (r_symndx = 0;
13812 r_symndx < symtab_hdr->sh_info;
13813 r_symndx++)
13814 if (local_sections[r_symndx] == sec)
13815 break;
13816 if (r_symndx >= symtab_hdr->sh_info)
13817 r_symndx = STN_UNDEF;
13818 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13819 if (r_symndx != STN_UNDEF)
13820 rel->r_addend -= (local_syms[r_symndx].st_value
13821 + sec->output_offset
13822 + sec->output_section->vma);
13823 }
13824 else if (toc_symndx != 0)
13825 {
13826 r_symndx = toc_symndx;
13827 rel->r_addend = toc_addend;
13828 }
13829 r_type = R_PPC64_TPREL16_HA;
13830 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13831 if (offset != (bfd_vma) -1)
13832 {
13833 rel[1].r_info = ELF64_R_INFO (r_symndx,
13834 R_PPC64_TPREL16_LO);
13835 rel[1].r_offset = offset + d_offset;
13836 rel[1].r_addend = rel->r_addend;
13837 }
13838 }
13839 bfd_put_32 (input_bfd, insn1,
13840 contents + rel->r_offset - d_offset);
13841 if (offset != (bfd_vma) -1)
13842 {
13843 bfd_put_32 (input_bfd, insn2, contents + offset);
13844 if (offset + 8 <= input_section->size)
13845 {
13846 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
13847 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
13848 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
13849 }
13850 }
13851 if ((tls_mask & tls_gd) == 0
13852 && (tls_gd == 0 || toc_symndx != 0))
13853 {
13854 /* We changed the symbol. Start over in order
13855 to get h, sym, sec etc. right. */
13856 goto again;
13857 }
13858 }
13859 break;
13860
13861 case R_PPC64_TLSGD:
13862 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
13863 && rel + 1 < relend)
13864 {
13865 unsigned int insn2;
13866 bfd_vma offset = rel->r_offset;
13867
13868 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
13869 {
13870 bfd_put_32 (output_bfd, NOP, contents + offset);
13871 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13872 break;
13873 }
13874
13875 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
13876 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
13877
13878 if ((tls_mask & TLS_TPRELGD) != 0)
13879 {
13880 /* IE */
13881 r_type = R_PPC64_NONE;
13882 insn2 = 0x7c636a14; /* add 3,3,13 */
13883 }
13884 else
13885 {
13886 /* LE */
13887 if (toc_symndx != 0)
13888 {
13889 r_symndx = toc_symndx;
13890 rel->r_addend = toc_addend;
13891 }
13892 r_type = R_PPC64_TPREL16_LO;
13893 rel->r_offset = offset + d_offset;
13894 insn2 = 0x38630000; /* addi 3,3,0 */
13895 }
13896 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13897 /* Zap the reloc on the _tls_get_addr call too. */
13898 BFD_ASSERT (offset == rel[1].r_offset);
13899 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13900 bfd_put_32 (input_bfd, insn2, contents + offset);
13901 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13902 goto again;
13903 }
13904 break;
13905
13906 case R_PPC64_TLSLD:
13907 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
13908 && rel + 1 < relend)
13909 {
13910 unsigned int insn2;
13911 bfd_vma offset = rel->r_offset;
13912
13913 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
13914 {
13915 bfd_put_32 (output_bfd, NOP, contents + offset);
13916 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13917 break;
13918 }
13919
13920 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
13921 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
13922
13923 if (toc_symndx)
13924 sec = local_sections[toc_symndx];
13925 for (r_symndx = 0;
13926 r_symndx < symtab_hdr->sh_info;
13927 r_symndx++)
13928 if (local_sections[r_symndx] == sec)
13929 break;
13930 if (r_symndx >= symtab_hdr->sh_info)
13931 r_symndx = STN_UNDEF;
13932 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13933 if (r_symndx != STN_UNDEF)
13934 rel->r_addend -= (local_syms[r_symndx].st_value
13935 + sec->output_offset
13936 + sec->output_section->vma);
13937
13938 r_type = R_PPC64_TPREL16_LO;
13939 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13940 rel->r_offset = offset + d_offset;
13941 /* Zap the reloc on the _tls_get_addr call too. */
13942 BFD_ASSERT (offset == rel[1].r_offset);
13943 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13944 insn2 = 0x38630000; /* addi 3,3,0 */
13945 bfd_put_32 (input_bfd, insn2, contents + offset);
13946 goto again;
13947 }
13948 break;
13949
13950 case R_PPC64_DTPMOD64:
13951 if (rel + 1 < relend
13952 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13953 && rel[1].r_offset == rel->r_offset + 8)
13954 {
13955 if ((tls_mask & TLS_GD) == 0)
13956 {
13957 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13958 if ((tls_mask & TLS_TPRELGD) != 0)
13959 r_type = R_PPC64_TPREL64;
13960 else
13961 {
13962 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13963 r_type = R_PPC64_NONE;
13964 }
13965 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13966 }
13967 }
13968 else
13969 {
13970 if ((tls_mask & TLS_LD) == 0)
13971 {
13972 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13973 r_type = R_PPC64_NONE;
13974 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13975 }
13976 }
13977 break;
13978
13979 case R_PPC64_TPREL64:
13980 if ((tls_mask & TLS_TPREL) == 0)
13981 {
13982 r_type = R_PPC64_NONE;
13983 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13984 }
13985 break;
13986
13987 case R_PPC64_ENTRY:
13988 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13989 if (!bfd_link_pic (info)
13990 && !info->traditional_format
13991 && relocation + 0x80008000 <= 0xffffffff)
13992 {
13993 unsigned int insn1, insn2;
13994
13995 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13996 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13997 if ((insn1 & ~0xfffc) == LD_R2_0R12
13998 && insn2 == ADD_R2_R2_R12)
13999 {
14000 bfd_put_32 (input_bfd,
14001 LIS_R2 + PPC_HA (relocation),
14002 contents + rel->r_offset);
14003 bfd_put_32 (input_bfd,
14004 ADDI_R2_R2 + PPC_LO (relocation),
14005 contents + rel->r_offset + 4);
14006 }
14007 }
14008 else
14009 {
14010 relocation -= (rel->r_offset
14011 + input_section->output_offset
14012 + input_section->output_section->vma);
14013 if (relocation + 0x80008000 <= 0xffffffff)
14014 {
14015 unsigned int insn1, insn2;
14016
14017 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14018 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14019 if ((insn1 & ~0xfffc) == LD_R2_0R12
14020 && insn2 == ADD_R2_R2_R12)
14021 {
14022 bfd_put_32 (input_bfd,
14023 ADDIS_R2_R12 + PPC_HA (relocation),
14024 contents + rel->r_offset);
14025 bfd_put_32 (input_bfd,
14026 ADDI_R2_R2 + PPC_LO (relocation),
14027 contents + rel->r_offset + 4);
14028 }
14029 }
14030 }
14031 break;
14032
14033 case R_PPC64_REL16_HA:
14034 /* If we are generating a non-PIC executable, edit
14035 . 0: addis 2,12,.TOC.-0b@ha
14036 . addi 2,2,.TOC.-0b@l
14037 used by ELFv2 global entry points to set up r2, to
14038 . lis 2,.TOC.@ha
14039 . addi 2,2,.TOC.@l
14040 if .TOC. is in range. */
14041 if (!bfd_link_pic (info)
14042 && !info->traditional_format
14043 && !htab->opd_abi
14044 && rel->r_addend == d_offset
14045 && h != NULL && &h->elf == htab->elf.hgot
14046 && rel + 1 < relend
14047 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14048 && rel[1].r_offset == rel->r_offset + 4
14049 && rel[1].r_addend == rel->r_addend + 4
14050 && relocation + 0x80008000 <= 0xffffffff)
14051 {
14052 unsigned int insn1, insn2;
14053 bfd_vma offset = rel->r_offset - d_offset;
14054 insn1 = bfd_get_32 (input_bfd, contents + offset);
14055 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14056 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14057 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14058 {
14059 r_type = R_PPC64_ADDR16_HA;
14060 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14061 rel->r_addend -= d_offset;
14062 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14063 rel[1].r_addend -= d_offset + 4;
14064 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14065 }
14066 }
14067 break;
14068 }
14069
14070 /* Handle other relocations that tweak non-addend part of insn. */
14071 insn = 0;
14072 max_br_offset = 1 << 25;
14073 addend = rel->r_addend;
14074 reloc_dest = DEST_NORMAL;
14075 switch (r_type)
14076 {
14077 default:
14078 break;
14079
14080 case R_PPC64_TOCSAVE:
14081 if (relocation + addend == (rel->r_offset
14082 + input_section->output_offset
14083 + input_section->output_section->vma)
14084 && tocsave_find (htab, NO_INSERT,
14085 &local_syms, rel, input_bfd))
14086 {
14087 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14088 if (insn == NOP
14089 || insn == CROR_151515 || insn == CROR_313131)
14090 bfd_put_32 (input_bfd,
14091 STD_R2_0R1 + STK_TOC (htab),
14092 contents + rel->r_offset);
14093 }
14094 break;
14095
14096 /* Branch taken prediction relocations. */
14097 case R_PPC64_ADDR14_BRTAKEN:
14098 case R_PPC64_REL14_BRTAKEN:
14099 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14100 /* Fall through. */
14101
14102 /* Branch not taken prediction relocations. */
14103 case R_PPC64_ADDR14_BRNTAKEN:
14104 case R_PPC64_REL14_BRNTAKEN:
14105 insn |= bfd_get_32 (input_bfd,
14106 contents + rel->r_offset) & ~(0x01 << 21);
14107 /* Fall through. */
14108
14109 case R_PPC64_REL14:
14110 max_br_offset = 1 << 15;
14111 /* Fall through. */
14112
14113 case R_PPC64_REL24:
14114 case R_PPC64_REL24_NOTOC:
14115 case R_PPC64_PLTCALL:
14116 /* Calls to functions with a different TOC, such as calls to
14117 shared objects, need to alter the TOC pointer. This is
14118 done using a linkage stub. A REL24 branching to these
14119 linkage stubs needs to be followed by a nop, as the nop
14120 will be replaced with an instruction to restore the TOC
14121 base pointer. */
14122 fdh = h;
14123 if (h != NULL
14124 && h->oh != NULL
14125 && h->oh->is_func_descriptor)
14126 fdh = ppc_follow_link (h->oh);
14127 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14128 htab);
14129 if (r_type == R_PPC64_PLTCALL
14130 && stub_entry != NULL
14131 && stub_entry->stub_type >= ppc_stub_plt_call
14132 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14133 stub_entry = NULL;
14134
14135 if (stub_entry != NULL
14136 && ((stub_entry->stub_type >= ppc_stub_plt_call
14137 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14138 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14139 || stub_entry->stub_type == ppc_stub_plt_branch_both
14140 || stub_entry->stub_type == ppc_stub_long_branch_r2off
14141 || stub_entry->stub_type == ppc_stub_long_branch_both))
14142 {
14143 bfd_boolean can_plt_call = FALSE;
14144
14145 if (stub_entry->stub_type == ppc_stub_plt_call
14146 && !htab->opd_abi
14147 && htab->params->plt_localentry0 != 0
14148 && is_elfv2_localentry0 (&h->elf))
14149 {
14150 /* The function doesn't use or change r2. */
14151 can_plt_call = TRUE;
14152 }
14153 else if (r_type == R_PPC64_REL24_NOTOC)
14154 {
14155 /* NOTOC calls don't need to restore r2. */
14156 can_plt_call = TRUE;
14157 }
14158
14159 /* All of these stubs may modify r2, so there must be a
14160 branch and link followed by a nop. The nop is
14161 replaced by an insn to restore r2. */
14162 else if (rel->r_offset + 8 <= input_section->size)
14163 {
14164 unsigned long br;
14165
14166 br = bfd_get_32 (input_bfd,
14167 contents + rel->r_offset);
14168 if ((br & 1) != 0)
14169 {
14170 unsigned long nop;
14171
14172 nop = bfd_get_32 (input_bfd,
14173 contents + rel->r_offset + 4);
14174 if (nop == LD_R2_0R1 + STK_TOC (htab))
14175 can_plt_call = TRUE;
14176 else if (nop == NOP
14177 || nop == CROR_151515
14178 || nop == CROR_313131)
14179 {
14180 if (h != NULL
14181 && (h == htab->tls_get_addr_fd
14182 || h == htab->tls_get_addr)
14183 && htab->params->tls_get_addr_opt)
14184 {
14185 /* Special stub used, leave nop alone. */
14186 }
14187 else
14188 bfd_put_32 (input_bfd,
14189 LD_R2_0R1 + STK_TOC (htab),
14190 contents + rel->r_offset + 4);
14191 can_plt_call = TRUE;
14192 }
14193 }
14194 }
14195
14196 if (!can_plt_call && h != NULL)
14197 {
14198 const char *name = h->elf.root.root.string;
14199
14200 if (*name == '.')
14201 ++name;
14202
14203 if (strncmp (name, "__libc_start_main", 17) == 0
14204 && (name[17] == 0 || name[17] == '@'))
14205 {
14206 /* Allow crt1 branch to go via a toc adjusting
14207 stub. Other calls that never return could do
14208 the same, if we could detect such. */
14209 can_plt_call = TRUE;
14210 }
14211 }
14212
14213 if (!can_plt_call)
14214 {
14215 /* g++ as of 20130507 emits self-calls without a
14216 following nop. This is arguably wrong since we
14217 have conflicting information. On the one hand a
14218 global symbol and on the other a local call
14219 sequence, but don't error for this special case.
14220 It isn't possible to cheaply verify we have
14221 exactly such a call. Allow all calls to the same
14222 section. */
14223 asection *code_sec = sec;
14224
14225 if (get_opd_info (sec) != NULL)
14226 {
14227 bfd_vma off = (relocation + addend
14228 - sec->output_section->vma
14229 - sec->output_offset);
14230
14231 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14232 }
14233 if (code_sec == input_section)
14234 can_plt_call = TRUE;
14235 }
14236
14237 if (!can_plt_call)
14238 {
14239 if (stub_entry->stub_type >= ppc_stub_plt_call
14240 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14241 info->callbacks->einfo
14242 /* xgettext:c-format */
14243 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14244 "(plt call stub)\n"),
14245 input_bfd, input_section, rel->r_offset, sym_name);
14246 else
14247 info->callbacks->einfo
14248 /* xgettext:c-format */
14249 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14250 "(toc save/adjust stub)\n"),
14251 input_bfd, input_section, rel->r_offset, sym_name);
14252
14253 bfd_set_error (bfd_error_bad_value);
14254 ret = FALSE;
14255 }
14256
14257 if (can_plt_call
14258 && stub_entry->stub_type >= ppc_stub_plt_call
14259 && stub_entry->stub_type <= ppc_stub_plt_call_both)
14260 unresolved_reloc = FALSE;
14261 }
14262
14263 if ((stub_entry == NULL
14264 || stub_entry->stub_type == ppc_stub_long_branch
14265 || stub_entry->stub_type == ppc_stub_plt_branch)
14266 && get_opd_info (sec) != NULL)
14267 {
14268 /* The branch destination is the value of the opd entry. */
14269 bfd_vma off = (relocation + addend
14270 - sec->output_section->vma
14271 - sec->output_offset);
14272 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14273 if (dest != (bfd_vma) -1)
14274 {
14275 relocation = dest;
14276 addend = 0;
14277 reloc_dest = DEST_OPD;
14278 }
14279 }
14280
14281 /* If the branch is out of reach we ought to have a long
14282 branch stub. */
14283 from = (rel->r_offset
14284 + input_section->output_offset
14285 + input_section->output_section->vma);
14286
14287 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14288 ? fdh->elf.other
14289 : sym->st_other);
14290
14291 if (stub_entry != NULL
14292 && (stub_entry->stub_type == ppc_stub_long_branch
14293 || stub_entry->stub_type == ppc_stub_plt_branch)
14294 && (r_type == R_PPC64_ADDR14_BRTAKEN
14295 || r_type == R_PPC64_ADDR14_BRNTAKEN
14296 || (relocation + addend - from + max_br_offset
14297 < 2 * max_br_offset)))
14298 /* Don't use the stub if this branch is in range. */
14299 stub_entry = NULL;
14300
14301 if (stub_entry != NULL
14302 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
14303 || stub_entry->stub_type == ppc_stub_long_branch_both
14304 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
14305 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14306 && (r_type != R_PPC64_REL24_NOTOC
14307 || ((fdh ? fdh->elf.other : sym->st_other)
14308 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
14309 && (relocation + addend - from + max_br_offset
14310 < 2 * max_br_offset))
14311 stub_entry = NULL;
14312
14313 if (stub_entry != NULL
14314 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
14315 || stub_entry->stub_type == ppc_stub_long_branch_both
14316 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14317 || stub_entry->stub_type == ppc_stub_plt_branch_both)
14318 && r_type == R_PPC64_REL24_NOTOC
14319 && (relocation + addend - from + max_br_offset
14320 < 2 * max_br_offset))
14321 stub_entry = NULL;
14322
14323 if (stub_entry != NULL)
14324 {
14325 /* Munge up the value and addend so that we call the stub
14326 rather than the procedure directly. */
14327 asection *stub_sec = stub_entry->group->stub_sec;
14328
14329 if (stub_entry->stub_type == ppc_stub_save_res)
14330 relocation += (stub_sec->output_offset
14331 + stub_sec->output_section->vma
14332 + stub_sec->size - htab->sfpr->size
14333 - htab->sfpr->output_offset
14334 - htab->sfpr->output_section->vma);
14335 else
14336 relocation = (stub_entry->stub_offset
14337 + stub_sec->output_offset
14338 + stub_sec->output_section->vma);
14339 addend = 0;
14340 reloc_dest = DEST_STUB;
14341
14342 if (((stub_entry->stub_type == ppc_stub_plt_call
14343 && ALWAYS_EMIT_R2SAVE)
14344 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14345 || stub_entry->stub_type == ppc_stub_plt_call_both)
14346 && !(h != NULL
14347 && (h == htab->tls_get_addr_fd
14348 || h == htab->tls_get_addr)
14349 && htab->params->tls_get_addr_opt)
14350 && rel + 1 < relend
14351 && rel[1].r_offset == rel->r_offset + 4
14352 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14353 relocation += 4;
14354 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
14355 || stub_entry->stub_type == ppc_stub_plt_branch_both
14356 || stub_entry->stub_type == ppc_stub_plt_call_both)
14357 && r_type == R_PPC64_REL24_NOTOC)
14358 relocation += 4;
14359 }
14360
14361 if (insn != 0)
14362 {
14363 if (is_isa_v2)
14364 {
14365 /* Set 'a' bit. This is 0b00010 in BO field for branch
14366 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14367 for branch on CTR insns (BO == 1a00t or 1a01t). */
14368 if ((insn & (0x14 << 21)) == (0x04 << 21))
14369 insn |= 0x02 << 21;
14370 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14371 insn |= 0x08 << 21;
14372 else
14373 break;
14374 }
14375 else
14376 {
14377 /* Invert 'y' bit if not the default. */
14378 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14379 insn ^= 0x01 << 21;
14380 }
14381
14382 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14383 }
14384
14385 /* NOP out calls to undefined weak functions.
14386 We can thus call a weak function without first
14387 checking whether the function is defined. */
14388 else if (h != NULL
14389 && h->elf.root.type == bfd_link_hash_undefweak
14390 && h->elf.dynindx == -1
14391 && (r_type == R_PPC64_REL24
14392 || r_type == R_PPC64_REL24_NOTOC)
14393 && relocation == 0
14394 && addend == 0)
14395 {
14396 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14397 goto copy_reloc;
14398 }
14399 break;
14400 }
14401
14402 /* Set `addend'. */
14403 tls_type = 0;
14404 save_unresolved_reloc = unresolved_reloc;
14405 switch (r_type)
14406 {
14407 default:
14408 /* xgettext:c-format */
14409 _bfd_error_handler (_("%pB: %s unsupported"),
14410 input_bfd, ppc64_elf_howto_table[r_type]->name);
14411
14412 bfd_set_error (bfd_error_bad_value);
14413 ret = FALSE;
14414 goto copy_reloc;
14415
14416 case R_PPC64_NONE:
14417 case R_PPC64_TLS:
14418 case R_PPC64_TLSGD:
14419 case R_PPC64_TLSLD:
14420 case R_PPC64_TOCSAVE:
14421 case R_PPC64_GNU_VTINHERIT:
14422 case R_PPC64_GNU_VTENTRY:
14423 case R_PPC64_ENTRY:
14424 goto copy_reloc;
14425
14426 /* GOT16 relocations. Like an ADDR16 using the symbol's
14427 address in the GOT as relocation value instead of the
14428 symbol's value itself. Also, create a GOT entry for the
14429 symbol and put the symbol value there. */
14430 case R_PPC64_GOT_TLSGD16:
14431 case R_PPC64_GOT_TLSGD16_LO:
14432 case R_PPC64_GOT_TLSGD16_HI:
14433 case R_PPC64_GOT_TLSGD16_HA:
14434 tls_type = TLS_TLS | TLS_GD;
14435 goto dogot;
14436
14437 case R_PPC64_GOT_TLSLD16:
14438 case R_PPC64_GOT_TLSLD16_LO:
14439 case R_PPC64_GOT_TLSLD16_HI:
14440 case R_PPC64_GOT_TLSLD16_HA:
14441 tls_type = TLS_TLS | TLS_LD;
14442 goto dogot;
14443
14444 case R_PPC64_GOT_TPREL16_DS:
14445 case R_PPC64_GOT_TPREL16_LO_DS:
14446 case R_PPC64_GOT_TPREL16_HI:
14447 case R_PPC64_GOT_TPREL16_HA:
14448 tls_type = TLS_TLS | TLS_TPREL;
14449 goto dogot;
14450
14451 case R_PPC64_GOT_DTPREL16_DS:
14452 case R_PPC64_GOT_DTPREL16_LO_DS:
14453 case R_PPC64_GOT_DTPREL16_HI:
14454 case R_PPC64_GOT_DTPREL16_HA:
14455 tls_type = TLS_TLS | TLS_DTPREL;
14456 goto dogot;
14457
14458 case R_PPC64_GOT16:
14459 case R_PPC64_GOT16_LO:
14460 case R_PPC64_GOT16_HI:
14461 case R_PPC64_GOT16_HA:
14462 case R_PPC64_GOT16_DS:
14463 case R_PPC64_GOT16_LO_DS:
14464 dogot:
14465 {
14466 /* Relocation is to the entry for this symbol in the global
14467 offset table. */
14468 asection *got;
14469 bfd_vma *offp;
14470 bfd_vma off;
14471 unsigned long indx = 0;
14472 struct got_entry *ent;
14473
14474 if (tls_type == (TLS_TLS | TLS_LD)
14475 && (h == NULL
14476 || !h->elf.def_dynamic))
14477 ent = ppc64_tlsld_got (input_bfd);
14478 else
14479 {
14480 if (h != NULL)
14481 {
14482 if (!htab->elf.dynamic_sections_created
14483 || h->elf.dynindx == -1
14484 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14485 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
14486 /* This is actually a static link, or it is a
14487 -Bsymbolic link and the symbol is defined
14488 locally, or the symbol was forced to be local
14489 because of a version file. */
14490 ;
14491 else
14492 {
14493 indx = h->elf.dynindx;
14494 unresolved_reloc = FALSE;
14495 }
14496 ent = h->elf.got.glist;
14497 }
14498 else
14499 {
14500 if (local_got_ents == NULL)
14501 abort ();
14502 ent = local_got_ents[r_symndx];
14503 }
14504
14505 for (; ent != NULL; ent = ent->next)
14506 if (ent->addend == orig_rel.r_addend
14507 && ent->owner == input_bfd
14508 && ent->tls_type == tls_type)
14509 break;
14510 }
14511
14512 if (ent == NULL)
14513 abort ();
14514 if (ent->is_indirect)
14515 ent = ent->got.ent;
14516 offp = &ent->got.offset;
14517 got = ppc64_elf_tdata (ent->owner)->got;
14518 if (got == NULL)
14519 abort ();
14520
14521 /* The offset must always be a multiple of 8. We use the
14522 least significant bit to record whether we have already
14523 processed this entry. */
14524 off = *offp;
14525 if ((off & 1) != 0)
14526 off &= ~1;
14527 else
14528 {
14529 /* Generate relocs for the dynamic linker, except in
14530 the case of TLSLD where we'll use one entry per
14531 module. */
14532 asection *relgot;
14533 bfd_boolean ifunc;
14534
14535 *offp = off | 1;
14536 relgot = NULL;
14537 ifunc = (h != NULL
14538 ? h->elf.type == STT_GNU_IFUNC
14539 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14540 if (ifunc)
14541 {
14542 relgot = htab->elf.irelplt;
14543 if (indx == 0)
14544 htab->local_ifunc_resolver = 1;
14545 else if (is_static_defined (&h->elf))
14546 htab->maybe_local_ifunc_resolver = 1;
14547 }
14548 else if (indx != 0
14549 || (bfd_link_pic (info)
14550 && (h == NULL
14551 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
14552 || (tls_type == (TLS_TLS | TLS_LD)
14553 && !h->elf.def_dynamic))
14554 && !(tls_type == (TLS_TLS | TLS_TPREL)
14555 && bfd_link_executable (info)
14556 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
14557 relgot = ppc64_elf_tdata (ent->owner)->relgot;
14558 if (relgot != NULL)
14559 {
14560 outrel.r_offset = (got->output_section->vma
14561 + got->output_offset
14562 + off);
14563 outrel.r_addend = addend;
14564 if (tls_type & (TLS_LD | TLS_GD))
14565 {
14566 outrel.r_addend = 0;
14567 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14568 if (tls_type == (TLS_TLS | TLS_GD))
14569 {
14570 loc = relgot->contents;
14571 loc += (relgot->reloc_count++
14572 * sizeof (Elf64_External_Rela));
14573 bfd_elf64_swap_reloca_out (output_bfd,
14574 &outrel, loc);
14575 outrel.r_offset += 8;
14576 outrel.r_addend = addend;
14577 outrel.r_info
14578 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14579 }
14580 }
14581 else if (tls_type == (TLS_TLS | TLS_DTPREL))
14582 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14583 else if (tls_type == (TLS_TLS | TLS_TPREL))
14584 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14585 else if (indx != 0)
14586 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14587 else
14588 {
14589 if (ifunc)
14590 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14591 else
14592 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14593
14594 /* Write the .got section contents for the sake
14595 of prelink. */
14596 loc = got->contents + off;
14597 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14598 loc);
14599 }
14600
14601 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14602 {
14603 outrel.r_addend += relocation;
14604 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14605 {
14606 if (htab->elf.tls_sec == NULL)
14607 outrel.r_addend = 0;
14608 else
14609 outrel.r_addend -= htab->elf.tls_sec->vma;
14610 }
14611 }
14612 loc = relgot->contents;
14613 loc += (relgot->reloc_count++
14614 * sizeof (Elf64_External_Rela));
14615 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14616 }
14617
14618 /* Init the .got section contents here if we're not
14619 emitting a reloc. */
14620 else
14621 {
14622 relocation += addend;
14623 if (tls_type != 0)
14624 {
14625 if (htab->elf.tls_sec == NULL)
14626 relocation = 0;
14627 else
14628 {
14629 if (tls_type & TLS_LD)
14630 relocation = 0;
14631 else
14632 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14633 if (tls_type & TLS_TPREL)
14634 relocation += DTP_OFFSET - TP_OFFSET;
14635 }
14636
14637 if (tls_type & (TLS_GD | TLS_LD))
14638 {
14639 bfd_put_64 (output_bfd, relocation,
14640 got->contents + off + 8);
14641 relocation = 1;
14642 }
14643 }
14644 bfd_put_64 (output_bfd, relocation,
14645 got->contents + off);
14646 }
14647 }
14648
14649 if (off >= (bfd_vma) -2)
14650 abort ();
14651
14652 relocation = got->output_section->vma + got->output_offset + off;
14653 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14654 }
14655 break;
14656
14657 case R_PPC64_PLT16_HA:
14658 case R_PPC64_PLT16_HI:
14659 case R_PPC64_PLT16_LO:
14660 case R_PPC64_PLT16_LO_DS:
14661 case R_PPC64_PLT32:
14662 case R_PPC64_PLT64:
14663 case R_PPC64_PLTSEQ:
14664 case R_PPC64_PLTCALL:
14665 /* Relocation is to the entry for this symbol in the
14666 procedure linkage table. */
14667 unresolved_reloc = TRUE;
14668 {
14669 struct plt_entry **plt_list = NULL;
14670 if (h != NULL)
14671 plt_list = &h->elf.plt.plist;
14672 else if (local_got_ents != NULL)
14673 {
14674 struct plt_entry **local_plt = (struct plt_entry **)
14675 (local_got_ents + symtab_hdr->sh_info);
14676 plt_list = local_plt + r_symndx;
14677 }
14678 if (plt_list)
14679 {
14680 struct plt_entry *ent;
14681
14682 for (ent = *plt_list; ent != NULL; ent = ent->next)
14683 if (ent->plt.offset != (bfd_vma) -1
14684 && ent->addend == orig_rel.r_addend)
14685 {
14686 asection *plt;
14687 bfd_vma got;
14688
14689 plt = htab->elf.splt;
14690 if (!htab->elf.dynamic_sections_created
14691 || h == NULL
14692 || h->elf.dynindx == -1)
14693 {
14694 if (h != NULL
14695 ? h->elf.type == STT_GNU_IFUNC
14696 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14697 plt = htab->elf.iplt;
14698 else
14699 plt = htab->pltlocal;
14700 }
14701 relocation = (plt->output_section->vma
14702 + plt->output_offset
14703 + ent->plt.offset);
14704 if (r_type == R_PPC64_PLT16_HA
14705 || r_type == R_PPC64_PLT16_HI
14706 || r_type == R_PPC64_PLT16_LO
14707 || r_type == R_PPC64_PLT16_LO_DS)
14708 {
14709 got = (elf_gp (output_bfd)
14710 + htab->sec_info[input_section->id].toc_off);
14711 relocation -= got;
14712 }
14713 addend = 0;
14714 unresolved_reloc = FALSE;
14715 break;
14716 }
14717 }
14718 }
14719 break;
14720
14721 case R_PPC64_TOC:
14722 /* Relocation value is TOC base. */
14723 relocation = TOCstart;
14724 if (r_symndx == STN_UNDEF)
14725 relocation += htab->sec_info[input_section->id].toc_off;
14726 else if (unresolved_reloc)
14727 ;
14728 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14729 relocation += htab->sec_info[sec->id].toc_off;
14730 else
14731 unresolved_reloc = TRUE;
14732 goto dodyn;
14733
14734 /* TOC16 relocs. We want the offset relative to the TOC base,
14735 which is the address of the start of the TOC plus 0x8000.
14736 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14737 in this order. */
14738 case R_PPC64_TOC16:
14739 case R_PPC64_TOC16_LO:
14740 case R_PPC64_TOC16_HI:
14741 case R_PPC64_TOC16_DS:
14742 case R_PPC64_TOC16_LO_DS:
14743 case R_PPC64_TOC16_HA:
14744 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14745 break;
14746
14747 /* Relocate against the beginning of the section. */
14748 case R_PPC64_SECTOFF:
14749 case R_PPC64_SECTOFF_LO:
14750 case R_PPC64_SECTOFF_HI:
14751 case R_PPC64_SECTOFF_DS:
14752 case R_PPC64_SECTOFF_LO_DS:
14753 case R_PPC64_SECTOFF_HA:
14754 if (sec != NULL)
14755 addend -= sec->output_section->vma;
14756 break;
14757
14758 case R_PPC64_REL16:
14759 case R_PPC64_REL16_LO:
14760 case R_PPC64_REL16_HI:
14761 case R_PPC64_REL16_HA:
14762 case R_PPC64_REL16_HIGH:
14763 case R_PPC64_REL16_HIGHA:
14764 case R_PPC64_REL16_HIGHER:
14765 case R_PPC64_REL16_HIGHERA:
14766 case R_PPC64_REL16_HIGHEST:
14767 case R_PPC64_REL16_HIGHESTA:
14768 case R_PPC64_REL16DX_HA:
14769 break;
14770
14771 case R_PPC64_REL14:
14772 case R_PPC64_REL14_BRNTAKEN:
14773 case R_PPC64_REL14_BRTAKEN:
14774 case R_PPC64_REL24:
14775 case R_PPC64_REL24_NOTOC:
14776 break;
14777
14778 case R_PPC64_TPREL16:
14779 case R_PPC64_TPREL16_LO:
14780 case R_PPC64_TPREL16_HI:
14781 case R_PPC64_TPREL16_HA:
14782 case R_PPC64_TPREL16_DS:
14783 case R_PPC64_TPREL16_LO_DS:
14784 case R_PPC64_TPREL16_HIGH:
14785 case R_PPC64_TPREL16_HIGHA:
14786 case R_PPC64_TPREL16_HIGHER:
14787 case R_PPC64_TPREL16_HIGHERA:
14788 case R_PPC64_TPREL16_HIGHEST:
14789 case R_PPC64_TPREL16_HIGHESTA:
14790 if (h != NULL
14791 && h->elf.root.type == bfd_link_hash_undefweak
14792 && h->elf.dynindx == -1)
14793 {
14794 /* Make this relocation against an undefined weak symbol
14795 resolve to zero. This is really just a tweak, since
14796 code using weak externs ought to check that they are
14797 defined before using them. */
14798 bfd_byte *p = contents + rel->r_offset - d_offset;
14799
14800 insn = bfd_get_32 (input_bfd, p);
14801 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14802 if (insn != 0)
14803 bfd_put_32 (input_bfd, insn, p);
14804 break;
14805 }
14806 if (htab->elf.tls_sec != NULL)
14807 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14808 /* The TPREL16 relocs shouldn't really be used in shared
14809 libs or with non-local symbols as that will result in
14810 DT_TEXTREL being set, but support them anyway. */
14811 goto dodyn;
14812
14813 case R_PPC64_DTPREL16:
14814 case R_PPC64_DTPREL16_LO:
14815 case R_PPC64_DTPREL16_HI:
14816 case R_PPC64_DTPREL16_HA:
14817 case R_PPC64_DTPREL16_DS:
14818 case R_PPC64_DTPREL16_LO_DS:
14819 case R_PPC64_DTPREL16_HIGH:
14820 case R_PPC64_DTPREL16_HIGHA:
14821 case R_PPC64_DTPREL16_HIGHER:
14822 case R_PPC64_DTPREL16_HIGHERA:
14823 case R_PPC64_DTPREL16_HIGHEST:
14824 case R_PPC64_DTPREL16_HIGHESTA:
14825 if (htab->elf.tls_sec != NULL)
14826 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14827 break;
14828
14829 case R_PPC64_ADDR64_LOCAL:
14830 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14831 ? h->elf.other
14832 : sym->st_other);
14833 break;
14834
14835 case R_PPC64_DTPMOD64:
14836 relocation = 1;
14837 addend = 0;
14838 goto dodyn;
14839
14840 case R_PPC64_TPREL64:
14841 if (htab->elf.tls_sec != NULL)
14842 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14843 goto dodyn;
14844
14845 case R_PPC64_DTPREL64:
14846 if (htab->elf.tls_sec != NULL)
14847 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14848 /* Fall through. */
14849
14850 /* Relocations that may need to be propagated if this is a
14851 dynamic object. */
14852 case R_PPC64_REL30:
14853 case R_PPC64_REL32:
14854 case R_PPC64_REL64:
14855 case R_PPC64_ADDR14:
14856 case R_PPC64_ADDR14_BRNTAKEN:
14857 case R_PPC64_ADDR14_BRTAKEN:
14858 case R_PPC64_ADDR16:
14859 case R_PPC64_ADDR16_DS:
14860 case R_PPC64_ADDR16_HA:
14861 case R_PPC64_ADDR16_HI:
14862 case R_PPC64_ADDR16_HIGH:
14863 case R_PPC64_ADDR16_HIGHA:
14864 case R_PPC64_ADDR16_HIGHER:
14865 case R_PPC64_ADDR16_HIGHERA:
14866 case R_PPC64_ADDR16_HIGHEST:
14867 case R_PPC64_ADDR16_HIGHESTA:
14868 case R_PPC64_ADDR16_LO:
14869 case R_PPC64_ADDR16_LO_DS:
14870 case R_PPC64_ADDR24:
14871 case R_PPC64_ADDR32:
14872 case R_PPC64_ADDR64:
14873 case R_PPC64_UADDR16:
14874 case R_PPC64_UADDR32:
14875 case R_PPC64_UADDR64:
14876 dodyn:
14877 if ((input_section->flags & SEC_ALLOC) == 0)
14878 break;
14879
14880 if (NO_OPD_RELOCS && is_opd)
14881 break;
14882
14883 if (bfd_link_pic (info)
14884 ? ((h == NULL
14885 || h->dyn_relocs != NULL)
14886 && ((h != NULL && pc_dynrelocs (h))
14887 || must_be_dyn_reloc (info, r_type)))
14888 : (h != NULL
14889 ? h->dyn_relocs != NULL
14890 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14891 {
14892 bfd_boolean skip, relocate;
14893 asection *sreloc;
14894 bfd_vma out_off;
14895 long indx = 0;
14896
14897 /* When generating a dynamic object, these relocations
14898 are copied into the output file to be resolved at run
14899 time. */
14900
14901 skip = FALSE;
14902 relocate = FALSE;
14903
14904 out_off = _bfd_elf_section_offset (output_bfd, info,
14905 input_section, rel->r_offset);
14906 if (out_off == (bfd_vma) -1)
14907 skip = TRUE;
14908 else if (out_off == (bfd_vma) -2)
14909 skip = TRUE, relocate = TRUE;
14910 out_off += (input_section->output_section->vma
14911 + input_section->output_offset);
14912 outrel.r_offset = out_off;
14913 outrel.r_addend = rel->r_addend;
14914
14915 /* Optimize unaligned reloc use. */
14916 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14917 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14918 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14919 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14920 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14921 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14922 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14923 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14924 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14925
14926 if (skip)
14927 memset (&outrel, 0, sizeof outrel);
14928 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14929 && !is_opd
14930 && r_type != R_PPC64_TOC)
14931 {
14932 indx = h->elf.dynindx;
14933 BFD_ASSERT (indx != -1);
14934 outrel.r_info = ELF64_R_INFO (indx, r_type);
14935 }
14936 else
14937 {
14938 /* This symbol is local, or marked to become local,
14939 or this is an opd section reloc which must point
14940 at a local function. */
14941 outrel.r_addend += relocation;
14942 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14943 {
14944 if (is_opd && h != NULL)
14945 {
14946 /* Lie about opd entries. This case occurs
14947 when building shared libraries and we
14948 reference a function in another shared
14949 lib. The same thing happens for a weak
14950 definition in an application that's
14951 overridden by a strong definition in a
14952 shared lib. (I believe this is a generic
14953 bug in binutils handling of weak syms.)
14954 In these cases we won't use the opd
14955 entry in this lib. */
14956 unresolved_reloc = FALSE;
14957 }
14958 if (!is_opd
14959 && r_type == R_PPC64_ADDR64
14960 && (h != NULL
14961 ? h->elf.type == STT_GNU_IFUNC
14962 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14963 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14964 else
14965 {
14966 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14967
14968 /* We need to relocate .opd contents for ld.so.
14969 Prelink also wants simple and consistent rules
14970 for relocs. This make all RELATIVE relocs have
14971 *r_offset equal to r_addend. */
14972 relocate = TRUE;
14973 }
14974 }
14975 else
14976 {
14977 if (h != NULL
14978 ? h->elf.type == STT_GNU_IFUNC
14979 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14980 {
14981 info->callbacks->einfo
14982 /* xgettext:c-format */
14983 (_("%H: %s for indirect "
14984 "function `%pT' unsupported\n"),
14985 input_bfd, input_section, rel->r_offset,
14986 ppc64_elf_howto_table[r_type]->name,
14987 sym_name);
14988 ret = FALSE;
14989 }
14990 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14991 ;
14992 else if (sec == NULL || sec->owner == NULL)
14993 {
14994 bfd_set_error (bfd_error_bad_value);
14995 return FALSE;
14996 }
14997 else
14998 {
14999 asection *osec;
15000
15001 osec = sec->output_section;
15002 indx = elf_section_data (osec)->dynindx;
15003
15004 if (indx == 0)
15005 {
15006 if ((osec->flags & SEC_READONLY) == 0
15007 && htab->elf.data_index_section != NULL)
15008 osec = htab->elf.data_index_section;
15009 else
15010 osec = htab->elf.text_index_section;
15011 indx = elf_section_data (osec)->dynindx;
15012 }
15013 BFD_ASSERT (indx != 0);
15014
15015 /* We are turning this relocation into one
15016 against a section symbol, so subtract out
15017 the output section's address but not the
15018 offset of the input section in the output
15019 section. */
15020 outrel.r_addend -= osec->vma;
15021 }
15022
15023 outrel.r_info = ELF64_R_INFO (indx, r_type);
15024 }
15025 }
15026
15027 sreloc = elf_section_data (input_section)->sreloc;
15028 if (h != NULL
15029 ? h->elf.type == STT_GNU_IFUNC
15030 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15031 {
15032 sreloc = htab->elf.irelplt;
15033 if (indx == 0)
15034 htab->local_ifunc_resolver = 1;
15035 else if (is_static_defined (&h->elf))
15036 htab->maybe_local_ifunc_resolver = 1;
15037 }
15038 if (sreloc == NULL)
15039 abort ();
15040
15041 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15042 >= sreloc->size)
15043 abort ();
15044 loc = sreloc->contents;
15045 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15046 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15047
15048 /* If this reloc is against an external symbol, it will
15049 be computed at runtime, so there's no need to do
15050 anything now. However, for the sake of prelink ensure
15051 that the section contents are a known value. */
15052 if (!relocate)
15053 {
15054 unresolved_reloc = FALSE;
15055 /* The value chosen here is quite arbitrary as ld.so
15056 ignores section contents except for the special
15057 case of .opd where the contents might be accessed
15058 before relocation. Choose zero, as that won't
15059 cause reloc overflow. */
15060 relocation = 0;
15061 addend = 0;
15062 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15063 to improve backward compatibility with older
15064 versions of ld. */
15065 if (r_type == R_PPC64_ADDR64)
15066 addend = outrel.r_addend;
15067 /* Adjust pc_relative relocs to have zero in *r_offset. */
15068 else if (ppc64_elf_howto_table[r_type]->pc_relative)
15069 addend = outrel.r_offset;
15070 }
15071 }
15072 break;
15073
15074 case R_PPC64_COPY:
15075 case R_PPC64_GLOB_DAT:
15076 case R_PPC64_JMP_SLOT:
15077 case R_PPC64_JMP_IREL:
15078 case R_PPC64_RELATIVE:
15079 /* We shouldn't ever see these dynamic relocs in relocatable
15080 files. */
15081 /* Fall through. */
15082
15083 case R_PPC64_PLTGOT16:
15084 case R_PPC64_PLTGOT16_DS:
15085 case R_PPC64_PLTGOT16_HA:
15086 case R_PPC64_PLTGOT16_HI:
15087 case R_PPC64_PLTGOT16_LO:
15088 case R_PPC64_PLTGOT16_LO_DS:
15089 case R_PPC64_PLTREL32:
15090 case R_PPC64_PLTREL64:
15091 /* These ones haven't been implemented yet. */
15092
15093 info->callbacks->einfo
15094 /* xgettext:c-format */
15095 (_("%P: %pB: %s is not supported for `%pT'\n"),
15096 input_bfd,
15097 ppc64_elf_howto_table[r_type]->name, sym_name);
15098
15099 bfd_set_error (bfd_error_invalid_operation);
15100 ret = FALSE;
15101 goto copy_reloc;
15102 }
15103
15104 /* Multi-instruction sequences that access the TOC can be
15105 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15106 to nop; addi rb,r2,x; */
15107 switch (r_type)
15108 {
15109 default:
15110 break;
15111
15112 case R_PPC64_GOT_TLSLD16_HI:
15113 case R_PPC64_GOT_TLSGD16_HI:
15114 case R_PPC64_GOT_TPREL16_HI:
15115 case R_PPC64_GOT_DTPREL16_HI:
15116 case R_PPC64_GOT16_HI:
15117 case R_PPC64_TOC16_HI:
15118 /* These relocs would only be useful if building up an
15119 offset to later add to r2, perhaps in an indexed
15120 addressing mode instruction. Don't try to optimize.
15121 Unfortunately, the possibility of someone building up an
15122 offset like this or even with the HA relocs, means that
15123 we need to check the high insn when optimizing the low
15124 insn. */
15125 break;
15126
15127 case R_PPC64_PLTCALL:
15128 if (unresolved_reloc)
15129 {
15130 /* No plt entry. Make this into a direct call. */
15131 bfd_byte *p = contents + rel->r_offset;
15132 insn = bfd_get_32 (input_bfd, p);
15133 insn &= 1;
15134 bfd_put_32 (input_bfd, B_DOT | insn, p);
15135 bfd_put_32 (input_bfd, NOP, p + 4);
15136 unresolved_reloc = save_unresolved_reloc;
15137 r_type = R_PPC64_REL24;
15138 }
15139 break;
15140
15141 case R_PPC64_PLTSEQ:
15142 if (unresolved_reloc)
15143 {
15144 unresolved_reloc = FALSE;
15145 goto nop_it;
15146 }
15147 break;
15148
15149 case R_PPC64_PLT16_HA:
15150 if (unresolved_reloc)
15151 {
15152 unresolved_reloc = FALSE;
15153 goto nop_it;
15154 }
15155 /* Fall through. */
15156 case R_PPC64_GOT_TLSLD16_HA:
15157 case R_PPC64_GOT_TLSGD16_HA:
15158 case R_PPC64_GOT_TPREL16_HA:
15159 case R_PPC64_GOT_DTPREL16_HA:
15160 case R_PPC64_GOT16_HA:
15161 case R_PPC64_TOC16_HA:
15162 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15163 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15164 {
15165 bfd_byte *p;
15166 nop_it:
15167 p = contents + (rel->r_offset & ~3);
15168 bfd_put_32 (input_bfd, NOP, p);
15169 goto copy_reloc;
15170 }
15171 break;
15172
15173 case R_PPC64_PLT16_LO:
15174 case R_PPC64_PLT16_LO_DS:
15175 if (unresolved_reloc)
15176 {
15177 unresolved_reloc = FALSE;
15178 goto nop_it;
15179 }
15180 /* Fall through. */
15181 case R_PPC64_GOT_TLSLD16_LO:
15182 case R_PPC64_GOT_TLSGD16_LO:
15183 case R_PPC64_GOT_TPREL16_LO_DS:
15184 case R_PPC64_GOT_DTPREL16_LO_DS:
15185 case R_PPC64_GOT16_LO:
15186 case R_PPC64_GOT16_LO_DS:
15187 case R_PPC64_TOC16_LO:
15188 case R_PPC64_TOC16_LO_DS:
15189 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15190 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15191 {
15192 bfd_byte *p = contents + (rel->r_offset & ~3);
15193 insn = bfd_get_32 (input_bfd, p);
15194 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15195 {
15196 /* Transform addic to addi when we change reg. */
15197 insn &= ~((0x3f << 26) | (0x1f << 16));
15198 insn |= (14u << 26) | (2 << 16);
15199 }
15200 else
15201 {
15202 insn &= ~(0x1f << 16);
15203 insn |= 2 << 16;
15204 }
15205 bfd_put_32 (input_bfd, insn, p);
15206 }
15207 break;
15208
15209 case R_PPC64_TPREL16_HA:
15210 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15211 {
15212 bfd_byte *p = contents + (rel->r_offset & ~3);
15213 insn = bfd_get_32 (input_bfd, p);
15214 if ((insn & ((0x3f << 26) | 0x1f << 16))
15215 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15216 /* xgettext:c-format */
15217 info->callbacks->minfo
15218 (_("%H: warning: %s unexpected insn %#x.\n"),
15219 input_bfd, input_section, rel->r_offset,
15220 ppc64_elf_howto_table[r_type]->name, insn);
15221 else
15222 {
15223 bfd_put_32 (input_bfd, NOP, p);
15224 goto copy_reloc;
15225 }
15226 }
15227 break;
15228
15229 case R_PPC64_TPREL16_LO:
15230 case R_PPC64_TPREL16_LO_DS:
15231 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15232 {
15233 bfd_byte *p = contents + (rel->r_offset & ~3);
15234 insn = bfd_get_32 (input_bfd, p);
15235 insn &= ~(0x1f << 16);
15236 insn |= 13 << 16;
15237 bfd_put_32 (input_bfd, insn, p);
15238 }
15239 break;
15240 }
15241
15242 /* Do any further special processing. */
15243 switch (r_type)
15244 {
15245 default:
15246 break;
15247
15248 case R_PPC64_REL16_HA:
15249 case R_PPC64_REL16_HIGHA:
15250 case R_PPC64_REL16_HIGHERA:
15251 case R_PPC64_REL16_HIGHESTA:
15252 case R_PPC64_REL16DX_HA:
15253 case R_PPC64_ADDR16_HA:
15254 case R_PPC64_ADDR16_HIGHA:
15255 case R_PPC64_ADDR16_HIGHERA:
15256 case R_PPC64_ADDR16_HIGHESTA:
15257 case R_PPC64_TOC16_HA:
15258 case R_PPC64_SECTOFF_HA:
15259 case R_PPC64_TPREL16_HA:
15260 case R_PPC64_TPREL16_HIGHA:
15261 case R_PPC64_TPREL16_HIGHERA:
15262 case R_PPC64_TPREL16_HIGHESTA:
15263 case R_PPC64_DTPREL16_HA:
15264 case R_PPC64_DTPREL16_HIGHA:
15265 case R_PPC64_DTPREL16_HIGHERA:
15266 case R_PPC64_DTPREL16_HIGHESTA:
15267 /* It's just possible that this symbol is a weak symbol
15268 that's not actually defined anywhere. In that case,
15269 'sec' would be NULL, and we should leave the symbol
15270 alone (it will be set to zero elsewhere in the link). */
15271 if (sec == NULL)
15272 break;
15273 /* Fall through. */
15274
15275 case R_PPC64_GOT16_HA:
15276 case R_PPC64_PLTGOT16_HA:
15277 case R_PPC64_PLT16_HA:
15278 case R_PPC64_GOT_TLSGD16_HA:
15279 case R_PPC64_GOT_TLSLD16_HA:
15280 case R_PPC64_GOT_TPREL16_HA:
15281 case R_PPC64_GOT_DTPREL16_HA:
15282 /* Add 0x10000 if sign bit in 0:15 is set.
15283 Bits 0:15 are not used. */
15284 addend += 0x8000;
15285 break;
15286
15287 case R_PPC64_ADDR16_DS:
15288 case R_PPC64_ADDR16_LO_DS:
15289 case R_PPC64_GOT16_DS:
15290 case R_PPC64_GOT16_LO_DS:
15291 case R_PPC64_PLT16_LO_DS:
15292 case R_PPC64_SECTOFF_DS:
15293 case R_PPC64_SECTOFF_LO_DS:
15294 case R_PPC64_TOC16_DS:
15295 case R_PPC64_TOC16_LO_DS:
15296 case R_PPC64_PLTGOT16_DS:
15297 case R_PPC64_PLTGOT16_LO_DS:
15298 case R_PPC64_GOT_TPREL16_DS:
15299 case R_PPC64_GOT_TPREL16_LO_DS:
15300 case R_PPC64_GOT_DTPREL16_DS:
15301 case R_PPC64_GOT_DTPREL16_LO_DS:
15302 case R_PPC64_TPREL16_DS:
15303 case R_PPC64_TPREL16_LO_DS:
15304 case R_PPC64_DTPREL16_DS:
15305 case R_PPC64_DTPREL16_LO_DS:
15306 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15307 mask = 3;
15308 /* If this reloc is against an lq, lxv, or stxv insn, then
15309 the value must be a multiple of 16. This is somewhat of
15310 a hack, but the "correct" way to do this by defining _DQ
15311 forms of all the _DS relocs bloats all reloc switches in
15312 this file. It doesn't make much sense to use these
15313 relocs in data, so testing the insn should be safe. */
15314 if ((insn & (0x3f << 26)) == (56u << 26)
15315 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15316 mask = 15;
15317 relocation += addend;
15318 addend = insn & (mask ^ 3);
15319 if ((relocation & mask) != 0)
15320 {
15321 relocation ^= relocation & mask;
15322 info->callbacks->einfo
15323 /* xgettext:c-format */
15324 (_("%H: error: %s not a multiple of %u\n"),
15325 input_bfd, input_section, rel->r_offset,
15326 ppc64_elf_howto_table[r_type]->name,
15327 mask + 1);
15328 bfd_set_error (bfd_error_bad_value);
15329 ret = FALSE;
15330 goto copy_reloc;
15331 }
15332 break;
15333 }
15334
15335 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15336 because such sections are not SEC_ALLOC and thus ld.so will
15337 not process them. */
15338 howto = ppc64_elf_howto_table[(int) r_type];
15339 if (unresolved_reloc
15340 && !((input_section->flags & SEC_DEBUGGING) != 0
15341 && h->elf.def_dynamic)
15342 && _bfd_elf_section_offset (output_bfd, info, input_section,
15343 rel->r_offset) != (bfd_vma) -1)
15344 {
15345 info->callbacks->einfo
15346 /* xgettext:c-format */
15347 (_("%H: unresolvable %s against `%pT'\n"),
15348 input_bfd, input_section, rel->r_offset,
15349 howto->name,
15350 h->elf.root.root.string);
15351 ret = FALSE;
15352 }
15353
15354 /* 16-bit fields in insns mostly have signed values, but a
15355 few insns have 16-bit unsigned values. Really, we should
15356 have different reloc types. */
15357 if (howto->complain_on_overflow != complain_overflow_dont
15358 && howto->dst_mask == 0xffff
15359 && (input_section->flags & SEC_CODE) != 0)
15360 {
15361 enum complain_overflow complain = complain_overflow_signed;
15362
15363 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15364 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15365 complain = complain_overflow_bitfield;
15366 else if (howto->rightshift == 0
15367 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15368 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15369 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15370 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15371 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15372 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15373 complain = complain_overflow_unsigned;
15374 if (howto->complain_on_overflow != complain)
15375 {
15376 alt_howto = *howto;
15377 alt_howto.complain_on_overflow = complain;
15378 howto = &alt_howto;
15379 }
15380 }
15381
15382 if (r_type == R_PPC64_REL16DX_HA)
15383 {
15384 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15385 if (rel->r_offset + 4 > input_section->size)
15386 r = bfd_reloc_outofrange;
15387 else
15388 {
15389 relocation += addend;
15390 relocation -= (rel->r_offset
15391 + input_section->output_offset
15392 + input_section->output_section->vma);
15393 relocation = (bfd_signed_vma) relocation >> 16;
15394 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15395 insn &= ~0x1fffc1;
15396 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15397 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15398 r = bfd_reloc_ok;
15399 if (relocation + 0x8000 > 0xffff)
15400 r = bfd_reloc_overflow;
15401 }
15402 }
15403 else
15404 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15405 rel->r_offset, relocation, addend);
15406
15407 if (r != bfd_reloc_ok)
15408 {
15409 char *more_info = NULL;
15410 const char *reloc_name = howto->name;
15411
15412 if (reloc_dest != DEST_NORMAL)
15413 {
15414 more_info = bfd_malloc (strlen (reloc_name) + 8);
15415 if (more_info != NULL)
15416 {
15417 strcpy (more_info, reloc_name);
15418 strcat (more_info, (reloc_dest == DEST_OPD
15419 ? " (OPD)" : " (stub)"));
15420 reloc_name = more_info;
15421 }
15422 }
15423
15424 if (r == bfd_reloc_overflow)
15425 {
15426 /* On code like "if (foo) foo();" don't report overflow
15427 on a branch to zero when foo is undefined. */
15428 if (!warned
15429 && (reloc_dest == DEST_STUB
15430 || !(h != NULL
15431 && (h->elf.root.type == bfd_link_hash_undefweak
15432 || h->elf.root.type == bfd_link_hash_undefined)
15433 && is_branch_reloc (r_type))))
15434 info->callbacks->reloc_overflow (info, &h->elf.root,
15435 sym_name, reloc_name,
15436 orig_rel.r_addend,
15437 input_bfd, input_section,
15438 rel->r_offset);
15439 }
15440 else
15441 {
15442 info->callbacks->einfo
15443 /* xgettext:c-format */
15444 (_("%H: %s against `%pT': error %d\n"),
15445 input_bfd, input_section, rel->r_offset,
15446 reloc_name, sym_name, (int) r);
15447 ret = FALSE;
15448 }
15449 if (more_info != NULL)
15450 free (more_info);
15451 }
15452 copy_reloc:
15453 if (wrel != rel)
15454 *wrel = *rel;
15455 }
15456
15457 if (wrel != rel)
15458 {
15459 Elf_Internal_Shdr *rel_hdr;
15460 size_t deleted = rel - wrel;
15461
15462 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15463 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15464 if (rel_hdr->sh_size == 0)
15465 {
15466 /* It is too late to remove an empty reloc section. Leave
15467 one NONE reloc.
15468 ??? What is wrong with an empty section??? */
15469 rel_hdr->sh_size = rel_hdr->sh_entsize;
15470 deleted -= 1;
15471 }
15472 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15473 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15474 input_section->reloc_count -= deleted;
15475 }
15476
15477 /* If we're emitting relocations, then shortly after this function
15478 returns, reloc offsets and addends for this section will be
15479 adjusted. Worse, reloc symbol indices will be for the output
15480 file rather than the input. Save a copy of the relocs for
15481 opd_entry_value. */
15482 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15483 {
15484 bfd_size_type amt;
15485 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15486 rel = bfd_alloc (input_bfd, amt);
15487 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15488 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15489 if (rel == NULL)
15490 return FALSE;
15491 memcpy (rel, relocs, amt);
15492 }
15493 return ret;
15494 }
15495
15496 /* Adjust the value of any local symbols in opd sections. */
15497
15498 static int
15499 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15500 const char *name ATTRIBUTE_UNUSED,
15501 Elf_Internal_Sym *elfsym,
15502 asection *input_sec,
15503 struct elf_link_hash_entry *h)
15504 {
15505 struct _opd_sec_data *opd;
15506 long adjust;
15507 bfd_vma value;
15508
15509 if (h != NULL)
15510 return 1;
15511
15512 opd = get_opd_info (input_sec);
15513 if (opd == NULL || opd->adjust == NULL)
15514 return 1;
15515
15516 value = elfsym->st_value - input_sec->output_offset;
15517 if (!bfd_link_relocatable (info))
15518 value -= input_sec->output_section->vma;
15519
15520 adjust = opd->adjust[OPD_NDX (value)];
15521 if (adjust == -1)
15522 return 2;
15523
15524 elfsym->st_value += adjust;
15525 return 1;
15526 }
15527
15528 /* Finish up dynamic symbol handling. We set the contents of various
15529 dynamic sections here. */
15530
15531 static bfd_boolean
15532 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15533 struct bfd_link_info *info,
15534 struct elf_link_hash_entry *h,
15535 Elf_Internal_Sym *sym)
15536 {
15537 struct ppc_link_hash_table *htab;
15538 struct plt_entry *ent;
15539
15540 htab = ppc_hash_table (info);
15541 if (htab == NULL)
15542 return FALSE;
15543
15544 if (!htab->opd_abi && !h->def_regular)
15545 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15546 if (ent->plt.offset != (bfd_vma) -1)
15547 {
15548 /* Mark the symbol as undefined, rather than as
15549 defined in glink. Leave the value if there were
15550 any relocations where pointer equality matters
15551 (this is a clue for the dynamic linker, to make
15552 function pointer comparisons work between an
15553 application and shared library), otherwise set it
15554 to zero. */
15555 sym->st_shndx = SHN_UNDEF;
15556 if (!h->pointer_equality_needed)
15557 sym->st_value = 0;
15558 else if (!h->ref_regular_nonweak)
15559 {
15560 /* This breaks function pointer comparisons, but
15561 that is better than breaking tests for a NULL
15562 function pointer. */
15563 sym->st_value = 0;
15564 }
15565 break;
15566 }
15567
15568 if (h->needs_copy)
15569 {
15570 /* This symbol needs a copy reloc. Set it up. */
15571 Elf_Internal_Rela rela;
15572 asection *srel;
15573 bfd_byte *loc;
15574
15575 if (h->dynindx == -1
15576 || (h->root.type != bfd_link_hash_defined
15577 && h->root.type != bfd_link_hash_defweak)
15578 || htab->elf.srelbss == NULL
15579 || htab->elf.sreldynrelro == NULL)
15580 abort ();
15581
15582 rela.r_offset = (h->root.u.def.value
15583 + h->root.u.def.section->output_section->vma
15584 + h->root.u.def.section->output_offset);
15585 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15586 rela.r_addend = 0;
15587 if (h->root.u.def.section == htab->elf.sdynrelro)
15588 srel = htab->elf.sreldynrelro;
15589 else
15590 srel = htab->elf.srelbss;
15591 loc = srel->contents;
15592 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
15593 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15594 }
15595
15596 return TRUE;
15597 }
15598
15599 /* Used to decide how to sort relocs in an optimal manner for the
15600 dynamic linker, before writing them out. */
15601
15602 static enum elf_reloc_type_class
15603 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15604 const asection *rel_sec,
15605 const Elf_Internal_Rela *rela)
15606 {
15607 enum elf_ppc64_reloc_type r_type;
15608 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15609
15610 if (rel_sec == htab->elf.irelplt)
15611 return reloc_class_ifunc;
15612
15613 r_type = ELF64_R_TYPE (rela->r_info);
15614 switch (r_type)
15615 {
15616 case R_PPC64_RELATIVE:
15617 return reloc_class_relative;
15618 case R_PPC64_JMP_SLOT:
15619 return reloc_class_plt;
15620 case R_PPC64_COPY:
15621 return reloc_class_copy;
15622 default:
15623 return reloc_class_normal;
15624 }
15625 }
15626
15627 /* Finish up the dynamic sections. */
15628
15629 static bfd_boolean
15630 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15631 struct bfd_link_info *info)
15632 {
15633 struct ppc_link_hash_table *htab;
15634 bfd *dynobj;
15635 asection *sdyn;
15636
15637 htab = ppc_hash_table (info);
15638 if (htab == NULL)
15639 return FALSE;
15640
15641 dynobj = htab->elf.dynobj;
15642 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15643
15644 if (htab->elf.dynamic_sections_created)
15645 {
15646 Elf64_External_Dyn *dyncon, *dynconend;
15647
15648 if (sdyn == NULL || htab->elf.sgot == NULL)
15649 abort ();
15650
15651 dyncon = (Elf64_External_Dyn *) sdyn->contents;
15652 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15653 for (; dyncon < dynconend; dyncon++)
15654 {
15655 Elf_Internal_Dyn dyn;
15656 asection *s;
15657
15658 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15659
15660 switch (dyn.d_tag)
15661 {
15662 default:
15663 continue;
15664
15665 case DT_PPC64_GLINK:
15666 s = htab->glink;
15667 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15668 /* We stupidly defined DT_PPC64_GLINK to be the start
15669 of glink rather than the first entry point, which is
15670 what ld.so needs, and now have a bigger stub to
15671 support automatic multiple TOCs. */
15672 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
15673 break;
15674
15675 case DT_PPC64_OPD:
15676 s = bfd_get_section_by_name (output_bfd, ".opd");
15677 if (s == NULL)
15678 continue;
15679 dyn.d_un.d_ptr = s->vma;
15680 break;
15681
15682 case DT_PPC64_OPT:
15683 if (htab->do_multi_toc && htab->multi_toc_needed)
15684 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15685 if (htab->has_plt_localentry0)
15686 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
15687 break;
15688
15689 case DT_PPC64_OPDSZ:
15690 s = bfd_get_section_by_name (output_bfd, ".opd");
15691 if (s == NULL)
15692 continue;
15693 dyn.d_un.d_val = s->size;
15694 break;
15695
15696 case DT_PLTGOT:
15697 s = htab->elf.splt;
15698 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15699 break;
15700
15701 case DT_JMPREL:
15702 s = htab->elf.srelplt;
15703 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15704 break;
15705
15706 case DT_PLTRELSZ:
15707 dyn.d_un.d_val = htab->elf.srelplt->size;
15708 break;
15709
15710 case DT_TEXTREL:
15711 if (htab->local_ifunc_resolver)
15712 info->callbacks->einfo
15713 (_("%X%P: text relocations and GNU indirect "
15714 "functions will result in a segfault at runtime\n"));
15715 else if (htab->maybe_local_ifunc_resolver)
15716 info->callbacks->einfo
15717 (_("%P: warning: text relocations and GNU indirect "
15718 "functions may result in a segfault at runtime\n"));
15719 continue;
15720 }
15721
15722 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15723 }
15724 }
15725
15726 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15727 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
15728 {
15729 /* Fill in the first entry in the global offset table.
15730 We use it to hold the link-time TOCbase. */
15731 bfd_put_64 (output_bfd,
15732 elf_gp (output_bfd) + TOC_BASE_OFF,
15733 htab->elf.sgot->contents);
15734
15735 /* Set .got entry size. */
15736 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
15737 = 8;
15738 }
15739
15740 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15741 && htab->elf.splt->output_section != bfd_abs_section_ptr)
15742 {
15743 /* Set .plt entry size. */
15744 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15745 = PLT_ENTRY_SIZE (htab);
15746 }
15747
15748 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15749 brlt ourselves if emitrelocations. */
15750 if (htab->brlt != NULL
15751 && htab->brlt->reloc_count != 0
15752 && !_bfd_elf_link_output_relocs (output_bfd,
15753 htab->brlt,
15754 elf_section_data (htab->brlt)->rela.hdr,
15755 elf_section_data (htab->brlt)->relocs,
15756 NULL))
15757 return FALSE;
15758
15759 if (htab->glink != NULL
15760 && htab->glink->reloc_count != 0
15761 && !_bfd_elf_link_output_relocs (output_bfd,
15762 htab->glink,
15763 elf_section_data (htab->glink)->rela.hdr,
15764 elf_section_data (htab->glink)->relocs,
15765 NULL))
15766 return FALSE;
15767
15768
15769 if (htab->glink_eh_frame != NULL
15770 && htab->glink_eh_frame->size != 0
15771 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15772 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15773 htab->glink_eh_frame,
15774 htab->glink_eh_frame->contents))
15775 return FALSE;
15776
15777 /* We need to handle writing out multiple GOT sections ourselves,
15778 since we didn't add them to DYNOBJ. We know dynobj is the first
15779 bfd. */
15780 while ((dynobj = dynobj->link.next) != NULL)
15781 {
15782 asection *s;
15783
15784 if (!is_ppc64_elf (dynobj))
15785 continue;
15786
15787 s = ppc64_elf_tdata (dynobj)->got;
15788 if (s != NULL
15789 && s->size != 0
15790 && s->output_section != bfd_abs_section_ptr
15791 && !bfd_set_section_contents (output_bfd, s->output_section,
15792 s->contents, s->output_offset,
15793 s->size))
15794 return FALSE;
15795 s = ppc64_elf_tdata (dynobj)->relgot;
15796 if (s != NULL
15797 && s->size != 0
15798 && s->output_section != bfd_abs_section_ptr
15799 && !bfd_set_section_contents (output_bfd, s->output_section,
15800 s->contents, s->output_offset,
15801 s->size))
15802 return FALSE;
15803 }
15804
15805 return TRUE;
15806 }
15807
15808 #include "elf64-target.h"
15809
15810 /* FreeBSD support */
15811
15812 #undef TARGET_LITTLE_SYM
15813 #undef TARGET_LITTLE_NAME
15814
15815 #undef TARGET_BIG_SYM
15816 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
15817 #undef TARGET_BIG_NAME
15818 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15819
15820 #undef ELF_OSABI
15821 #define ELF_OSABI ELFOSABI_FREEBSD
15822
15823 #undef elf64_bed
15824 #define elf64_bed elf64_powerpc_fbsd_bed
15825
15826 #include "elf64-target.h"
This page took 0.432081 seconds and 4 git commands to generate.