Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5 Largely rewritten by Alan Modra.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22
23
24 /* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
27
28 #include "sysdep.h"
29 #include <stdarg.h>
30 #include "bfd.h"
31 #include "bfdlink.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elf/ppc64.h"
35 #include "elf64-ppc.h"
36 #include "dwarf2.h"
37
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_prefix_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58 static bfd_vma opd_entry_value
59 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
60
61 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
62 #define TARGET_LITTLE_NAME "elf64-powerpcle"
63 #define TARGET_BIG_SYM powerpc_elf64_vec
64 #define TARGET_BIG_NAME "elf64-powerpc"
65 #define ELF_ARCH bfd_arch_powerpc
66 #define ELF_TARGET_ID PPC64_ELF_DATA
67 #define ELF_MACHINE_CODE EM_PPC64
68 #define ELF_MAXPAGESIZE 0x10000
69 #define ELF_COMMONPAGESIZE 0x1000
70 #define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
71 #define elf_info_to_howto ppc64_elf_info_to_howto
72
73 #define elf_backend_want_got_sym 0
74 #define elf_backend_want_plt_sym 0
75 #define elf_backend_plt_alignment 3
76 #define elf_backend_plt_not_loaded 1
77 #define elf_backend_got_header_size 8
78 #define elf_backend_want_dynrelro 1
79 #define elf_backend_can_gc_sections 1
80 #define elf_backend_can_refcount 1
81 #define elf_backend_rela_normal 1
82 #define elf_backend_dtrel_excludes_plt 1
83 #define elf_backend_default_execstack 0
84
85 #define bfd_elf64_mkobject ppc64_elf_mkobject
86 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
87 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
88 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
89 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
90 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
91 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
92 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
93 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
94 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
95
96 #define elf_backend_object_p ppc64_elf_object_p
97 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
98 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
99 #define elf_backend_write_core_note ppc64_elf_write_core_note
100 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
101 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
102 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
103 #define elf_backend_check_directives ppc64_elf_before_check_relocs
104 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
105 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
106 #define elf_backend_check_relocs ppc64_elf_check_relocs
107 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
108 #define elf_backend_gc_keep ppc64_elf_gc_keep
109 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
110 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
111 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
112 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
113 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
114 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
115 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
116 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
117 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
118 #define elf_backend_action_discarded ppc64_elf_action_discarded
119 #define elf_backend_relocate_section ppc64_elf_relocate_section
120 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
121 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
122 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
123 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
124 #define elf_backend_special_sections ppc64_elf_special_sections
125 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
126 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
127 #define elf_backend_get_reloc_section bfd_get_section_by_name
128
129 /* The name of the dynamic interpreter. This is put in the .interp
130 section. */
131 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
132
133 /* The size in bytes of an entry in the procedure linkage table. */
134 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
135 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
136
137 /* The initial size of the plt reserved for the dynamic linker. */
138 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
139
140 /* Offsets to some stack save slots. */
141 #define STK_LR 16
142 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
143 /* This one is dodgy. ELFv2 does not have a linker word, so use the
144 CR save slot. Used only by optimised __tls_get_addr call stub,
145 relying on __tls_get_addr_opt not saving CR.. */
146 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
147
148 /* TOC base pointers offset from start of TOC. */
149 #define TOC_BASE_OFF 0x8000
150 /* TOC base alignment. */
151 #define TOC_BASE_ALIGN 256
152
153 /* Offset of tp and dtp pointers from start of TLS block. */
154 #define TP_OFFSET 0x7000
155 #define DTP_OFFSET 0x8000
156
157 /* .plt call stub instructions. The normal stub is like this, but
158 sometimes the .plt entry crosses a 64k boundary and we need to
159 insert an addi to adjust r11. */
160 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
161 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
162 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
163 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
164 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
165 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
166 #define BCTR 0x4e800420 /* bctr */
167
168 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
169 #define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
170 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
171 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
172 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
173
174 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
175 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
176 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
177 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
178 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
179 #define BNECTR 0x4ca20420 /* bnectr+ */
180 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
181
182 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
183 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
184 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
185
186 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
187 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
188 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
189
190 #define LI_R11_0 0x39600000 /* li %r11,0 */
191 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
192 #define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
193 #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
194 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
195 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
196 #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
197 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
198 #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
199 #define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
200 #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
201 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
202 #define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
203 #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
204 #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
205 #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
206 #define PADDI_R12_PC 0x0610000039800000ULL
207 #define PLD_R12_PC 0x04100000e5800000ULL
208 #define PNOP 0x0700000000000000ULL
209
210 /* __glink_PLTresolve stub instructions. We enter with the index in R0. */
211 #define GLINK_PLTRESOLVE_SIZE(htab) \
212 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
213 /* 0: */
214 /* .quad plt0-1f */
215 /* __glink: */
216 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
217 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
218 /* 1: */
219 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
220 /* ld %2,(0b-1b)(%11) */
221 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
222 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
223 /* ld %12,0(%11) */
224 /* ld %2,8(%11) */
225 /* mtctr %12 */
226 /* ld %11,16(%11) */
227 /* bctr */
228 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
229 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
230 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
231 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
232 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
233
234 /* Pad with this. */
235 #define NOP 0x60000000
236
237 /* Some other nops. */
238 #define CROR_151515 0x4def7b82
239 #define CROR_313131 0x4ffffb82
240
241 /* .glink entries for the first 32k functions are two instructions. */
242 #define LI_R0_0 0x38000000 /* li %r0,0 */
243 #define B_DOT 0x48000000 /* b . */
244
245 /* After that, we need two instructions to load the index, followed by
246 a branch. */
247 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
248 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
249
250 /* Instructions used by the save and restore reg functions. */
251 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
252 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
253 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
254 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
255 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
256 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
257 #define LI_R12_0 0x39800000 /* li %r12,0 */
258 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
259 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
260 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
261 #define BLR 0x4e800020 /* blr */
262
263 /* Since .opd is an array of descriptors and each entry will end up
264 with identical R_PPC64_RELATIVE relocs, there is really no need to
265 propagate .opd relocs; The dynamic linker should be taught to
266 relocate .opd without reloc entries. */
267 #ifndef NO_OPD_RELOCS
268 #define NO_OPD_RELOCS 0
269 #endif
270
271 #ifndef ARRAY_SIZE
272 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
273 #endif
274
275 static inline int
276 abiversion (bfd *abfd)
277 {
278 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
279 }
280
281 static inline void
282 set_abiversion (bfd *abfd, int ver)
283 {
284 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
285 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
286 }
287 \f
288 /* Relocation HOWTO's. */
289 /* Like other ELF RELA targets that don't apply multiple
290 field-altering relocations to the same localation, src_mask is
291 always zero and pcrel_offset is the same as pc_relative.
292 PowerPC can always use a zero bitpos, even when the field is not at
293 the LSB. For example, a REL24 could use rightshift=2, bisize=24
294 and bitpos=2 which matches the ABI description, or as we do here,
295 rightshift=0, bitsize=26 and bitpos=0. */
296 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
297 complain, special_func) \
298 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
299 complain_overflow_ ## complain, special_func, \
300 #type, FALSE, 0, mask, pc_relative)
301
302 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
303
304 static reloc_howto_type ppc64_elf_howto_raw[] =
305 {
306 /* This reloc does nothing. */
307 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
308 bfd_elf_generic_reloc),
309
310 /* A standard 32 bit relocation. */
311 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
312 bfd_elf_generic_reloc),
313
314 /* An absolute 26 bit branch; the lower two bits must be zero.
315 FIXME: we don't check that, we just clear them. */
316 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
317 bfd_elf_generic_reloc),
318
319 /* A standard 16 bit relocation. */
320 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
321 bfd_elf_generic_reloc),
322
323 /* A 16 bit relocation without overflow. */
324 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
325 bfd_elf_generic_reloc),
326
327 /* Bits 16-31 of an address. */
328 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
329 bfd_elf_generic_reloc),
330
331 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
332 bits, treated as a signed number, is negative. */
333 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
334 ppc64_elf_ha_reloc),
335
336 /* An absolute 16 bit branch; the lower two bits must be zero.
337 FIXME: we don't check that, we just clear them. */
338 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
339 ppc64_elf_branch_reloc),
340
341 /* An absolute 16 bit branch, for which bit 10 should be set to
342 indicate that the branch is expected to be taken. The lower two
343 bits must be zero. */
344 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
345 ppc64_elf_brtaken_reloc),
346
347 /* An absolute 16 bit branch, for which bit 10 should be set to
348 indicate that the branch is not expected to be taken. The lower
349 two bits must be zero. */
350 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
351 ppc64_elf_brtaken_reloc),
352
353 /* A relative 26 bit branch; the lower two bits must be zero. */
354 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
355 ppc64_elf_branch_reloc),
356
357 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
358 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
359 ppc64_elf_branch_reloc),
360
361 /* A relative 16 bit branch; the lower two bits must be zero. */
362 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
363 ppc64_elf_branch_reloc),
364
365 /* A relative 16 bit branch. Bit 10 should be set to indicate that
366 the branch is expected to be taken. The lower two bits must be
367 zero. */
368 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
369 ppc64_elf_brtaken_reloc),
370
371 /* A relative 16 bit branch. Bit 10 should be set to indicate that
372 the branch is not expected to be taken. The lower two bits must
373 be zero. */
374 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
375 ppc64_elf_brtaken_reloc),
376
377 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
378 symbol. */
379 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
380 ppc64_elf_unhandled_reloc),
381
382 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
383 the symbol. */
384 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
385 ppc64_elf_unhandled_reloc),
386
387 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
388 the symbol. */
389 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
390 ppc64_elf_unhandled_reloc),
391
392 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
393 the symbol. */
394 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
395 ppc64_elf_unhandled_reloc),
396
397 /* This is used only by the dynamic linker. The symbol should exist
398 both in the object being run and in some shared library. The
399 dynamic linker copies the data addressed by the symbol from the
400 shared library into the object, because the object being
401 run has to have the data at some particular address. */
402 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
403 ppc64_elf_unhandled_reloc),
404
405 /* Like R_PPC64_ADDR64, but used when setting global offset table
406 entries. */
407 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
408 ppc64_elf_unhandled_reloc),
409
410 /* Created by the link editor. Marks a procedure linkage table
411 entry for a symbol. */
412 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
413 ppc64_elf_unhandled_reloc),
414
415 /* Used only by the dynamic linker. When the object is run, this
416 doubleword64 is set to the load address of the object, plus the
417 addend. */
418 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
419 bfd_elf_generic_reloc),
420
421 /* Like R_PPC64_ADDR32, but may be unaligned. */
422 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
423 bfd_elf_generic_reloc),
424
425 /* Like R_PPC64_ADDR16, but may be unaligned. */
426 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
427 bfd_elf_generic_reloc),
428
429 /* 32-bit PC relative. */
430 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
431 bfd_elf_generic_reloc),
432
433 /* 32-bit relocation to the symbol's procedure linkage table. */
434 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
435 ppc64_elf_unhandled_reloc),
436
437 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
438 FIXME: R_PPC64_PLTREL32 not supported. */
439 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
440 ppc64_elf_unhandled_reloc),
441
442 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
443 the symbol. */
444 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
445 ppc64_elf_unhandled_reloc),
446
447 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
448 the symbol. */
449 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
450 ppc64_elf_unhandled_reloc),
451
452 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
453 the symbol. */
454 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
455 ppc64_elf_unhandled_reloc),
456
457 /* 16-bit section relative relocation. */
458 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
459 ppc64_elf_sectoff_reloc),
460
461 /* Like R_PPC64_SECTOFF, but no overflow warning. */
462 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
463 ppc64_elf_sectoff_reloc),
464
465 /* 16-bit upper half section relative relocation. */
466 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
467 ppc64_elf_sectoff_reloc),
468
469 /* 16-bit upper half adjusted section relative relocation. */
470 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
471 ppc64_elf_sectoff_ha_reloc),
472
473 /* Like R_PPC64_REL24 without touching the two least significant bits. */
474 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
475 bfd_elf_generic_reloc),
476
477 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
478
479 /* A standard 64-bit relocation. */
480 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
481 bfd_elf_generic_reloc),
482
483 /* The bits 32-47 of an address. */
484 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
485 bfd_elf_generic_reloc),
486
487 /* The bits 32-47 of an address, plus 1 if the contents of the low
488 16 bits, treated as a signed number, is negative. */
489 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
490 ppc64_elf_ha_reloc),
491
492 /* The bits 48-63 of an address. */
493 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
494 bfd_elf_generic_reloc),
495
496 /* The bits 48-63 of an address, plus 1 if the contents of the low
497 16 bits, treated as a signed number, is negative. */
498 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
499 ppc64_elf_ha_reloc),
500
501 /* Like ADDR64, but may be unaligned. */
502 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
503 bfd_elf_generic_reloc),
504
505 /* 64-bit relative relocation. */
506 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
507 bfd_elf_generic_reloc),
508
509 /* 64-bit relocation to the symbol's procedure linkage table. */
510 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
511 ppc64_elf_unhandled_reloc),
512
513 /* 64-bit PC relative relocation to the symbol's procedure linkage
514 table. */
515 /* FIXME: R_PPC64_PLTREL64 not supported. */
516 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
517 ppc64_elf_unhandled_reloc),
518
519 /* 16 bit TOC-relative relocation. */
520 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
521 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
522 ppc64_elf_toc_reloc),
523
524 /* 16 bit TOC-relative relocation without overflow. */
525 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
526 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
527 ppc64_elf_toc_reloc),
528
529 /* 16 bit TOC-relative relocation, high 16 bits. */
530 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
531 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
532 ppc64_elf_toc_reloc),
533
534 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
535 contents of the low 16 bits, treated as a signed number, is
536 negative. */
537 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
538 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
539 ppc64_elf_toc_ha_reloc),
540
541 /* 64-bit relocation; insert value of TOC base (.TOC.). */
542 /* R_PPC64_TOC 51 doubleword64 .TOC. */
543 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
544 ppc64_elf_toc64_reloc),
545
546 /* Like R_PPC64_GOT16, but also informs the link editor that the
547 value to relocate may (!) refer to a PLT entry which the link
548 editor (a) may replace with the symbol value. If the link editor
549 is unable to fully resolve the symbol, it may (b) create a PLT
550 entry and store the address to the new PLT entry in the GOT.
551 This permits lazy resolution of function symbols at run time.
552 The link editor may also skip all of this and just (c) emit a
553 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
554 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
555 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
556 ppc64_elf_unhandled_reloc),
557
558 /* Like R_PPC64_PLTGOT16, but without overflow. */
559 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
560 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
561 ppc64_elf_unhandled_reloc),
562
563 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
564 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
565 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
566 ppc64_elf_unhandled_reloc),
567
568 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
569 1 if the contents of the low 16 bits, treated as a signed number,
570 is negative. */
571 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
572 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
573 ppc64_elf_unhandled_reloc),
574
575 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
576 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
577 bfd_elf_generic_reloc),
578
579 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
580 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
581 bfd_elf_generic_reloc),
582
583 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
584 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
585 ppc64_elf_unhandled_reloc),
586
587 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
588 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
589 ppc64_elf_unhandled_reloc),
590
591 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
592 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
593 ppc64_elf_unhandled_reloc),
594
595 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
596 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
597 ppc64_elf_sectoff_reloc),
598
599 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
600 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
601 ppc64_elf_sectoff_reloc),
602
603 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
604 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
605 ppc64_elf_toc_reloc),
606
607 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
608 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
609 ppc64_elf_toc_reloc),
610
611 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
612 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
613 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
614 ppc64_elf_unhandled_reloc),
615
616 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
617 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
618 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
619 ppc64_elf_unhandled_reloc),
620
621 /* Marker relocs for TLS. */
622 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
623 bfd_elf_generic_reloc),
624
625 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
626 bfd_elf_generic_reloc),
627
628 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
629 bfd_elf_generic_reloc),
630
631 /* Marker reloc for optimizing r2 save in prologue rather than on
632 each plt call stub. */
633 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
634 bfd_elf_generic_reloc),
635
636 /* Marker relocs on inline plt call instructions. */
637 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
638 bfd_elf_generic_reloc),
639
640 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
641 bfd_elf_generic_reloc),
642
643 /* Computes the load module index of the load module that contains the
644 definition of its TLS sym. */
645 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
646 ppc64_elf_unhandled_reloc),
647
648 /* Computes a dtv-relative displacement, the difference between the value
649 of sym+add and the base address of the thread-local storage block that
650 contains the definition of sym, minus 0x8000. */
651 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
652 ppc64_elf_unhandled_reloc),
653
654 /* A 16 bit dtprel reloc. */
655 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
656 ppc64_elf_unhandled_reloc),
657
658 /* Like DTPREL16, but no overflow. */
659 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
660 ppc64_elf_unhandled_reloc),
661
662 /* Like DTPREL16_LO, but next higher group of 16 bits. */
663 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
664 ppc64_elf_unhandled_reloc),
665
666 /* Like DTPREL16_HI, but adjust for low 16 bits. */
667 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
668 ppc64_elf_unhandled_reloc),
669
670 /* Like DTPREL16_HI, but next higher group of 16 bits. */
671 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
672 ppc64_elf_unhandled_reloc),
673
674 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
675 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
676 ppc64_elf_unhandled_reloc),
677
678 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
679 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
680 ppc64_elf_unhandled_reloc),
681
682 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
683 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
684 ppc64_elf_unhandled_reloc),
685
686 /* Like DTPREL16, but for insns with a DS field. */
687 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
688 ppc64_elf_unhandled_reloc),
689
690 /* Like DTPREL16_DS, but no overflow. */
691 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
692 ppc64_elf_unhandled_reloc),
693
694 /* Computes a tp-relative displacement, the difference between the value of
695 sym+add and the value of the thread pointer (r13). */
696 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
697 ppc64_elf_unhandled_reloc),
698
699 /* A 16 bit tprel reloc. */
700 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
701 ppc64_elf_unhandled_reloc),
702
703 /* Like TPREL16, but no overflow. */
704 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
705 ppc64_elf_unhandled_reloc),
706
707 /* Like TPREL16_LO, but next higher group of 16 bits. */
708 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
709 ppc64_elf_unhandled_reloc),
710
711 /* Like TPREL16_HI, but adjust for low 16 bits. */
712 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
713 ppc64_elf_unhandled_reloc),
714
715 /* Like TPREL16_HI, but next higher group of 16 bits. */
716 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
717 ppc64_elf_unhandled_reloc),
718
719 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
720 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
721 ppc64_elf_unhandled_reloc),
722
723 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
724 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
725 ppc64_elf_unhandled_reloc),
726
727 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
728 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
729 ppc64_elf_unhandled_reloc),
730
731 /* Like TPREL16, but for insns with a DS field. */
732 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
733 ppc64_elf_unhandled_reloc),
734
735 /* Like TPREL16_DS, but no overflow. */
736 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
737 ppc64_elf_unhandled_reloc),
738
739 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
740 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
741 to the first entry relative to the TOC base (r2). */
742 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
743 ppc64_elf_unhandled_reloc),
744
745 /* Like GOT_TLSGD16, but no overflow. */
746 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
747 ppc64_elf_unhandled_reloc),
748
749 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
750 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
751 ppc64_elf_unhandled_reloc),
752
753 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
754 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
755 ppc64_elf_unhandled_reloc),
756
757 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
758 with values (sym+add)@dtpmod and zero, and computes the offset to the
759 first entry relative to the TOC base (r2). */
760 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
761 ppc64_elf_unhandled_reloc),
762
763 /* Like GOT_TLSLD16, but no overflow. */
764 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
765 ppc64_elf_unhandled_reloc),
766
767 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
768 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
769 ppc64_elf_unhandled_reloc),
770
771 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
772 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
773 ppc64_elf_unhandled_reloc),
774
775 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
776 the offset to the entry relative to the TOC base (r2). */
777 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
778 ppc64_elf_unhandled_reloc),
779
780 /* Like GOT_DTPREL16_DS, but no overflow. */
781 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
782 ppc64_elf_unhandled_reloc),
783
784 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
785 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
786 ppc64_elf_unhandled_reloc),
787
788 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
789 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
790 ppc64_elf_unhandled_reloc),
791
792 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
793 offset to the entry relative to the TOC base (r2). */
794 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
795 ppc64_elf_unhandled_reloc),
796
797 /* Like GOT_TPREL16_DS, but no overflow. */
798 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
799 ppc64_elf_unhandled_reloc),
800
801 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
802 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
803 ppc64_elf_unhandled_reloc),
804
805 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
806 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
807 ppc64_elf_unhandled_reloc),
808
809 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
810 ppc64_elf_unhandled_reloc),
811
812 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
813 bfd_elf_generic_reloc),
814
815 /* A 16 bit relative relocation. */
816 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
817 bfd_elf_generic_reloc),
818
819 /* A 16 bit relative relocation without overflow. */
820 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
821 bfd_elf_generic_reloc),
822
823 /* The high order 16 bits of a relative address. */
824 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
825 bfd_elf_generic_reloc),
826
827 /* The high order 16 bits of a relative address, plus 1 if the contents of
828 the low 16 bits, treated as a signed number, is negative. */
829 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
830 ppc64_elf_ha_reloc),
831
832 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
833 bfd_elf_generic_reloc),
834
835 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
836 ppc64_elf_ha_reloc),
837
838 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
839 bfd_elf_generic_reloc),
840
841 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
842 ppc64_elf_ha_reloc),
843
844 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
845 bfd_elf_generic_reloc),
846
847 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
848 ppc64_elf_ha_reloc),
849
850 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
851 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
852 ppc64_elf_ha_reloc),
853
854 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
855 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
856 ppc64_elf_ha_reloc),
857
858 /* Like R_PPC64_ADDR16_HI, but no overflow. */
859 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
860 bfd_elf_generic_reloc),
861
862 /* Like R_PPC64_ADDR16_HA, but no overflow. */
863 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
864 ppc64_elf_ha_reloc),
865
866 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
867 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
868 ppc64_elf_unhandled_reloc),
869
870 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
871 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
872 ppc64_elf_unhandled_reloc),
873
874 /* Like R_PPC64_TPREL16_HI, but no overflow. */
875 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
876 ppc64_elf_unhandled_reloc),
877
878 /* Like R_PPC64_TPREL16_HA, but no overflow. */
879 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
880 ppc64_elf_unhandled_reloc),
881
882 /* Marker reloc on ELFv2 large-model function entry. */
883 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
884 bfd_elf_generic_reloc),
885
886 /* Like ADDR64, but use local entry point of function. */
887 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
888 bfd_elf_generic_reloc),
889
890 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, FALSE, dont,
891 bfd_elf_generic_reloc),
892
893 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, FALSE, dont,
894 bfd_elf_generic_reloc),
895
896 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, FALSE, dont,
897 bfd_elf_generic_reloc),
898
899 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
900 ppc64_elf_prefix_reloc),
901
902 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, dont,
903 ppc64_elf_prefix_reloc),
904
905 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
906 ppc64_elf_prefix_reloc),
907
908 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
909 ppc64_elf_prefix_reloc),
910
911 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
912 ppc64_elf_prefix_reloc),
913
914 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
915 ppc64_elf_unhandled_reloc),
916
917 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
918 ppc64_elf_unhandled_reloc),
919
920 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
921 ppc64_elf_unhandled_reloc),
922
923 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
924 ppc64_elf_unhandled_reloc),
925
926 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
927 ppc64_elf_unhandled_reloc),
928
929 HOW (R_PPC64_GOT_TLSGD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
930 ppc64_elf_unhandled_reloc),
931
932 HOW (R_PPC64_GOT_TLSLD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
933 ppc64_elf_unhandled_reloc),
934
935 HOW (R_PPC64_GOT_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
936 ppc64_elf_unhandled_reloc),
937
938 HOW (R_PPC64_GOT_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
939 ppc64_elf_unhandled_reloc),
940
941 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
942 bfd_elf_generic_reloc),
943
944 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
945 ppc64_elf_ha_reloc),
946
947 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
948 bfd_elf_generic_reloc),
949
950 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
951 ppc64_elf_ha_reloc),
952
953 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
954 bfd_elf_generic_reloc),
955
956 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
957 ppc64_elf_ha_reloc),
958
959 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
960 bfd_elf_generic_reloc),
961
962 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
963 ppc64_elf_ha_reloc),
964
965 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
966 ppc64_elf_prefix_reloc),
967
968 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
969 ppc64_elf_prefix_reloc),
970
971 /* GNU extension to record C++ vtable hierarchy. */
972 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
973 NULL),
974
975 /* GNU extension to record C++ vtable member usage. */
976 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
977 NULL),
978 };
979
980 \f
981 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
982 be done. */
983
984 static void
985 ppc_howto_init (void)
986 {
987 unsigned int i, type;
988
989 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
990 {
991 type = ppc64_elf_howto_raw[i].type;
992 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
993 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
994 }
995 }
996
997 static reloc_howto_type *
998 ppc64_elf_reloc_type_lookup (bfd *abfd,
999 bfd_reloc_code_real_type code)
1000 {
1001 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1002
1003 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1004 /* Initialize howto table if needed. */
1005 ppc_howto_init ();
1006
1007 switch (code)
1008 {
1009 default:
1010 /* xgettext:c-format */
1011 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1012 (int) code);
1013 bfd_set_error (bfd_error_bad_value);
1014 return NULL;
1015
1016 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1017 break;
1018 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1019 break;
1020 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1021 break;
1022 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1023 break;
1024 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1025 break;
1026 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1027 break;
1028 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1029 break;
1030 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1031 break;
1032 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1033 break;
1034 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1035 break;
1036 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1037 break;
1038 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1039 break;
1040 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1041 break;
1042 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1043 break;
1044 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1045 break;
1046 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1047 break;
1048 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1049 break;
1050 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1051 break;
1052 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1053 break;
1054 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1055 break;
1056 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1057 break;
1058 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1059 break;
1060 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1061 break;
1062 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1063 break;
1064 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1065 break;
1066 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1067 break;
1068 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1069 break;
1070 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1071 break;
1072 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1073 break;
1074 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1075 break;
1076 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1077 break;
1078 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1079 break;
1080 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1081 break;
1082 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1083 break;
1084 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1085 break;
1086 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1087 break;
1088 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1089 break;
1090 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1091 break;
1092 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1093 break;
1094 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1095 break;
1096 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1097 break;
1098 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1099 break;
1100 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1101 break;
1102 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1103 break;
1104 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1105 break;
1106 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1107 break;
1108 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1109 break;
1110 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1111 break;
1112 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1113 break;
1114 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1115 break;
1116 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1117 break;
1118 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1119 break;
1120 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1121 break;
1122 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1123 break;
1124 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1125 break;
1126 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1127 break;
1128 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1129 break;
1130 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1131 break;
1132 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1133 break;
1134 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1135 break;
1136 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1137 break;
1138 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1139 break;
1140 case BFD_RELOC_PPC64_TLS_PCREL:
1141 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1142 break;
1143 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1144 break;
1145 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1146 break;
1147 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1148 break;
1149 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1150 break;
1151 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1152 break;
1153 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1154 break;
1155 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1156 break;
1157 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1158 break;
1159 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1160 break;
1161 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1162 break;
1163 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1164 break;
1165 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1166 break;
1167 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1168 break;
1169 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1170 break;
1171 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1172 break;
1173 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1174 break;
1175 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1176 break;
1177 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1178 break;
1179 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1180 break;
1181 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1182 break;
1183 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1184 break;
1185 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1186 break;
1187 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1188 break;
1189 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1190 break;
1191 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1192 break;
1193 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1194 break;
1195 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1196 break;
1197 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1198 break;
1199 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1200 break;
1201 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1202 break;
1203 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1204 break;
1205 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1206 break;
1207 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1208 break;
1209 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1210 break;
1211 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1212 break;
1213 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1214 break;
1215 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1216 break;
1217 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1218 break;
1219 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1220 break;
1221 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1222 break;
1223 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1224 break;
1225 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1226 break;
1227 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1228 break;
1229 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1230 break;
1231 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1232 break;
1233 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1234 break;
1235 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1236 break;
1237 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1238 break;
1239 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1240 break;
1241 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1242 break;
1243 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1244 break;
1245 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1246 break;
1247 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1248 break;
1249 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1250 break;
1251 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1252 break;
1253 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1254 break;
1255 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1256 break;
1257 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1258 break;
1259 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1260 break;
1261 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1262 break;
1263 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1264 break;
1265 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1266 break;
1267 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1268 break;
1269 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1270 break;
1271 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1272 break;
1273 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1274 break;
1275 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1276 break;
1277 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1278 break;
1279 case BFD_RELOC_PPC64_GOT_TLSGD34: r = R_PPC64_GOT_TLSGD34;
1280 break;
1281 case BFD_RELOC_PPC64_GOT_TLSLD34: r = R_PPC64_GOT_TLSLD34;
1282 break;
1283 case BFD_RELOC_PPC64_GOT_TPREL34: r = R_PPC64_GOT_TPREL34;
1284 break;
1285 case BFD_RELOC_PPC64_GOT_DTPREL34: r = R_PPC64_GOT_DTPREL34;
1286 break;
1287 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1288 break;
1289 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1290 break;
1291 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1292 break;
1293 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1294 break;
1295 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1296 break;
1297 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1298 break;
1299 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1300 break;
1301 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1302 break;
1303 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1304 break;
1305 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1306 break;
1307 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1308 break;
1309 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1310 break;
1311 }
1312
1313 return ppc64_elf_howto_table[r];
1314 };
1315
1316 static reloc_howto_type *
1317 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1318 const char *r_name)
1319 {
1320 unsigned int i;
1321
1322 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1323 if (ppc64_elf_howto_raw[i].name != NULL
1324 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1325 return &ppc64_elf_howto_raw[i];
1326
1327 return NULL;
1328 }
1329
1330 /* Set the howto pointer for a PowerPC ELF reloc. */
1331
1332 static bfd_boolean
1333 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1334 Elf_Internal_Rela *dst)
1335 {
1336 unsigned int type;
1337
1338 /* Initialize howto table if needed. */
1339 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1340 ppc_howto_init ();
1341
1342 type = ELF64_R_TYPE (dst->r_info);
1343 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1344 {
1345 /* xgettext:c-format */
1346 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1347 abfd, type);
1348 bfd_set_error (bfd_error_bad_value);
1349 return FALSE;
1350 }
1351 cache_ptr->howto = ppc64_elf_howto_table[type];
1352 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1353 {
1354 /* xgettext:c-format */
1355 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1356 abfd, type);
1357 bfd_set_error (bfd_error_bad_value);
1358 return FALSE;
1359 }
1360
1361 return TRUE;
1362 }
1363
1364 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1365
1366 static bfd_reloc_status_type
1367 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1368 void *data, asection *input_section,
1369 bfd *output_bfd, char **error_message)
1370 {
1371 enum elf_ppc64_reloc_type r_type;
1372 long insn;
1373 bfd_size_type octets;
1374 bfd_vma value;
1375
1376 /* If this is a relocatable link (output_bfd test tells us), just
1377 call the generic function. Any adjustment will be done at final
1378 link time. */
1379 if (output_bfd != NULL)
1380 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1381 input_section, output_bfd, error_message);
1382
1383 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1384 We won't actually be using the low bits, so trashing them
1385 doesn't matter. */
1386 r_type = reloc_entry->howto->type;
1387 if (r_type == R_PPC64_ADDR16_HIGHERA34
1388 || r_type == R_PPC64_ADDR16_HIGHESTA34
1389 || r_type == R_PPC64_REL16_HIGHERA34
1390 || r_type == R_PPC64_REL16_HIGHESTA34)
1391 reloc_entry->addend += 1ULL << 33;
1392 else
1393 reloc_entry->addend += 1U << 15;
1394 if (r_type != R_PPC64_REL16DX_HA)
1395 return bfd_reloc_continue;
1396
1397 value = 0;
1398 if (!bfd_is_com_section (symbol->section))
1399 value = symbol->value;
1400 value += (reloc_entry->addend
1401 + symbol->section->output_offset
1402 + symbol->section->output_section->vma);
1403 value -= (reloc_entry->address
1404 + input_section->output_offset
1405 + input_section->output_section->vma);
1406 value = (bfd_signed_vma) value >> 16;
1407
1408 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1409 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1410 insn &= ~0x1fffc1;
1411 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1412 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1413 if (value + 0x8000 > 0xffff)
1414 return bfd_reloc_overflow;
1415 return bfd_reloc_ok;
1416 }
1417
1418 static bfd_reloc_status_type
1419 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1420 void *data, asection *input_section,
1421 bfd *output_bfd, char **error_message)
1422 {
1423 if (output_bfd != NULL)
1424 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1425 input_section, output_bfd, error_message);
1426
1427 if (strcmp (symbol->section->name, ".opd") == 0
1428 && (symbol->section->owner->flags & DYNAMIC) == 0)
1429 {
1430 bfd_vma dest = opd_entry_value (symbol->section,
1431 symbol->value + reloc_entry->addend,
1432 NULL, NULL, FALSE);
1433 if (dest != (bfd_vma) -1)
1434 reloc_entry->addend = dest - (symbol->value
1435 + symbol->section->output_section->vma
1436 + symbol->section->output_offset);
1437 }
1438 else
1439 {
1440 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1441
1442 if (symbol->section->owner != abfd
1443 && symbol->section->owner != NULL
1444 && abiversion (symbol->section->owner) >= 2)
1445 {
1446 unsigned int i;
1447
1448 for (i = 0; i < symbol->section->owner->symcount; ++i)
1449 {
1450 asymbol *symdef = symbol->section->owner->outsymbols[i];
1451
1452 if (strcmp (symdef->name, symbol->name) == 0)
1453 {
1454 elfsym = (elf_symbol_type *) symdef;
1455 break;
1456 }
1457 }
1458 }
1459 reloc_entry->addend
1460 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1461 }
1462 return bfd_reloc_continue;
1463 }
1464
1465 static bfd_reloc_status_type
1466 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1467 void *data, asection *input_section,
1468 bfd *output_bfd, char **error_message)
1469 {
1470 long insn;
1471 enum elf_ppc64_reloc_type r_type;
1472 bfd_size_type octets;
1473 /* Assume 'at' branch hints. */
1474 bfd_boolean is_isa_v2 = TRUE;
1475
1476 /* If this is a relocatable link (output_bfd test tells us), just
1477 call the generic function. Any adjustment will be done at final
1478 link time. */
1479 if (output_bfd != NULL)
1480 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1481 input_section, output_bfd, error_message);
1482
1483 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1484 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1485 insn &= ~(0x01 << 21);
1486 r_type = reloc_entry->howto->type;
1487 if (r_type == R_PPC64_ADDR14_BRTAKEN
1488 || r_type == R_PPC64_REL14_BRTAKEN)
1489 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1490
1491 if (is_isa_v2)
1492 {
1493 /* Set 'a' bit. This is 0b00010 in BO field for branch
1494 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1495 for branch on CTR insns (BO == 1a00t or 1a01t). */
1496 if ((insn & (0x14 << 21)) == (0x04 << 21))
1497 insn |= 0x02 << 21;
1498 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1499 insn |= 0x08 << 21;
1500 else
1501 goto out;
1502 }
1503 else
1504 {
1505 bfd_vma target = 0;
1506 bfd_vma from;
1507
1508 if (!bfd_is_com_section (symbol->section))
1509 target = symbol->value;
1510 target += symbol->section->output_section->vma;
1511 target += symbol->section->output_offset;
1512 target += reloc_entry->addend;
1513
1514 from = (reloc_entry->address
1515 + input_section->output_offset
1516 + input_section->output_section->vma);
1517
1518 /* Invert 'y' bit if not the default. */
1519 if ((bfd_signed_vma) (target - from) < 0)
1520 insn ^= 0x01 << 21;
1521 }
1522 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1523 out:
1524 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1525 input_section, output_bfd, error_message);
1526 }
1527
1528 static bfd_reloc_status_type
1529 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1530 void *data, asection *input_section,
1531 bfd *output_bfd, char **error_message)
1532 {
1533 /* If this is a relocatable link (output_bfd test tells us), just
1534 call the generic function. Any adjustment will be done at final
1535 link time. */
1536 if (output_bfd != NULL)
1537 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1538 input_section, output_bfd, error_message);
1539
1540 /* Subtract the symbol section base address. */
1541 reloc_entry->addend -= symbol->section->output_section->vma;
1542 return bfd_reloc_continue;
1543 }
1544
1545 static bfd_reloc_status_type
1546 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1547 void *data, asection *input_section,
1548 bfd *output_bfd, char **error_message)
1549 {
1550 /* If this is a relocatable link (output_bfd test tells us), just
1551 call the generic function. Any adjustment will be done at final
1552 link time. */
1553 if (output_bfd != NULL)
1554 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1555 input_section, output_bfd, error_message);
1556
1557 /* Subtract the symbol section base address. */
1558 reloc_entry->addend -= symbol->section->output_section->vma;
1559
1560 /* Adjust the addend for sign extension of the low 16 bits. */
1561 reloc_entry->addend += 0x8000;
1562 return bfd_reloc_continue;
1563 }
1564
1565 static bfd_reloc_status_type
1566 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1567 void *data, asection *input_section,
1568 bfd *output_bfd, char **error_message)
1569 {
1570 bfd_vma TOCstart;
1571
1572 /* If this is a relocatable link (output_bfd test tells us), just
1573 call the generic function. Any adjustment will be done at final
1574 link time. */
1575 if (output_bfd != NULL)
1576 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1577 input_section, output_bfd, error_message);
1578
1579 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1580 if (TOCstart == 0)
1581 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1582
1583 /* Subtract the TOC base address. */
1584 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1585 return bfd_reloc_continue;
1586 }
1587
1588 static bfd_reloc_status_type
1589 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1590 void *data, asection *input_section,
1591 bfd *output_bfd, char **error_message)
1592 {
1593 bfd_vma TOCstart;
1594
1595 /* If this is a relocatable link (output_bfd test tells us), just
1596 call the generic function. Any adjustment will be done at final
1597 link time. */
1598 if (output_bfd != NULL)
1599 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1600 input_section, output_bfd, error_message);
1601
1602 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1603 if (TOCstart == 0)
1604 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1605
1606 /* Subtract the TOC base address. */
1607 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1608
1609 /* Adjust the addend for sign extension of the low 16 bits. */
1610 reloc_entry->addend += 0x8000;
1611 return bfd_reloc_continue;
1612 }
1613
1614 static bfd_reloc_status_type
1615 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1616 void *data, asection *input_section,
1617 bfd *output_bfd, char **error_message)
1618 {
1619 bfd_vma TOCstart;
1620 bfd_size_type octets;
1621
1622 /* If this is a relocatable link (output_bfd test tells us), just
1623 call the generic function. Any adjustment will be done at final
1624 link time. */
1625 if (output_bfd != NULL)
1626 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1627 input_section, output_bfd, error_message);
1628
1629 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1630 if (TOCstart == 0)
1631 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1632
1633 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1634 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1635 return bfd_reloc_ok;
1636 }
1637
1638 static bfd_reloc_status_type
1639 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1640 void *data, asection *input_section,
1641 bfd *output_bfd, char **error_message)
1642 {
1643 uint64_t insn;
1644 bfd_vma targ;
1645
1646 if (output_bfd != NULL)
1647 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1648 input_section, output_bfd, error_message);
1649
1650 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1651 insn <<= 32;
1652 insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1653
1654 targ = (symbol->section->output_section->vma
1655 + symbol->section->output_offset
1656 + reloc_entry->addend);
1657 if (!bfd_is_com_section (symbol->section))
1658 targ += symbol->value;
1659 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1660 targ += 1ULL << 33;
1661 if (reloc_entry->howto->pc_relative)
1662 {
1663 bfd_vma from = (reloc_entry->address
1664 + input_section->output_offset
1665 + input_section->output_section->vma);
1666 targ -=from;
1667 }
1668 targ >>= reloc_entry->howto->rightshift;
1669 insn &= ~reloc_entry->howto->dst_mask;
1670 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1671 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1672 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1673 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1674 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1675 >= 1ULL << reloc_entry->howto->bitsize))
1676 return bfd_reloc_overflow;
1677 return bfd_reloc_ok;
1678 }
1679
1680 static bfd_reloc_status_type
1681 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1682 void *data, asection *input_section,
1683 bfd *output_bfd, char **error_message)
1684 {
1685 /* If this is a relocatable link (output_bfd test tells us), just
1686 call the generic function. Any adjustment will be done at final
1687 link time. */
1688 if (output_bfd != NULL)
1689 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1690 input_section, output_bfd, error_message);
1691
1692 if (error_message != NULL)
1693 {
1694 static char buf[60];
1695 sprintf (buf, "generic linker can't handle %s",
1696 reloc_entry->howto->name);
1697 *error_message = buf;
1698 }
1699 return bfd_reloc_dangerous;
1700 }
1701
1702 /* Track GOT entries needed for a given symbol. We might need more
1703 than one got entry per symbol. */
1704 struct got_entry
1705 {
1706 struct got_entry *next;
1707
1708 /* The symbol addend that we'll be placing in the GOT. */
1709 bfd_vma addend;
1710
1711 /* Unlike other ELF targets, we use separate GOT entries for the same
1712 symbol referenced from different input files. This is to support
1713 automatic multiple TOC/GOT sections, where the TOC base can vary
1714 from one input file to another. After partitioning into TOC groups
1715 we merge entries within the group.
1716
1717 Point to the BFD owning this GOT entry. */
1718 bfd *owner;
1719
1720 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1721 TLS_TPREL or TLS_DTPREL for tls entries. */
1722 unsigned char tls_type;
1723
1724 /* Non-zero if got.ent points to real entry. */
1725 unsigned char is_indirect;
1726
1727 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1728 union
1729 {
1730 bfd_signed_vma refcount;
1731 bfd_vma offset;
1732 struct got_entry *ent;
1733 } got;
1734 };
1735
1736 /* The same for PLT. */
1737 struct plt_entry
1738 {
1739 struct plt_entry *next;
1740
1741 bfd_vma addend;
1742
1743 union
1744 {
1745 bfd_signed_vma refcount;
1746 bfd_vma offset;
1747 } plt;
1748 };
1749
1750 struct ppc64_elf_obj_tdata
1751 {
1752 struct elf_obj_tdata elf;
1753
1754 /* Shortcuts to dynamic linker sections. */
1755 asection *got;
1756 asection *relgot;
1757
1758 /* Used during garbage collection. We attach global symbols defined
1759 on removed .opd entries to this section so that the sym is removed. */
1760 asection *deleted_section;
1761
1762 /* TLS local dynamic got entry handling. Support for multiple GOT
1763 sections means we potentially need one of these for each input bfd. */
1764 struct got_entry tlsld_got;
1765
1766 union
1767 {
1768 /* A copy of relocs before they are modified for --emit-relocs. */
1769 Elf_Internal_Rela *relocs;
1770
1771 /* Section contents. */
1772 bfd_byte *contents;
1773 } opd;
1774
1775 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1776 the reloc to be in the range -32768 to 32767. */
1777 unsigned int has_small_toc_reloc : 1;
1778
1779 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1780 instruction not one we handle. */
1781 unsigned int unexpected_toc_insn : 1;
1782
1783 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1784 this file. */
1785 unsigned int has_optrel : 1;
1786 };
1787
1788 #define ppc64_elf_tdata(bfd) \
1789 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1790
1791 #define ppc64_tlsld_got(bfd) \
1792 (&ppc64_elf_tdata (bfd)->tlsld_got)
1793
1794 #define is_ppc64_elf(bfd) \
1795 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1796 && elf_object_id (bfd) == PPC64_ELF_DATA)
1797
1798 /* Override the generic function because we store some extras. */
1799
1800 static bfd_boolean
1801 ppc64_elf_mkobject (bfd *abfd)
1802 {
1803 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1804 PPC64_ELF_DATA);
1805 }
1806
1807 /* Fix bad default arch selected for a 64 bit input bfd when the
1808 default is 32 bit. Also select arch based on apuinfo. */
1809
1810 static bfd_boolean
1811 ppc64_elf_object_p (bfd *abfd)
1812 {
1813 if (!abfd->arch_info->the_default)
1814 return TRUE;
1815
1816 if (abfd->arch_info->bits_per_word == 32)
1817 {
1818 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1819
1820 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1821 {
1822 /* Relies on arch after 32 bit default being 64 bit default. */
1823 abfd->arch_info = abfd->arch_info->next;
1824 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1825 }
1826 }
1827 return _bfd_elf_ppc_set_arch (abfd);
1828 }
1829
1830 /* Support for core dump NOTE sections. */
1831
1832 static bfd_boolean
1833 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1834 {
1835 size_t offset, size;
1836
1837 if (note->descsz != 504)
1838 return FALSE;
1839
1840 /* pr_cursig */
1841 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1842
1843 /* pr_pid */
1844 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1845
1846 /* pr_reg */
1847 offset = 112;
1848 size = 384;
1849
1850 /* Make a ".reg/999" section. */
1851 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1852 size, note->descpos + offset);
1853 }
1854
1855 static bfd_boolean
1856 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1857 {
1858 if (note->descsz != 136)
1859 return FALSE;
1860
1861 elf_tdata (abfd)->core->pid
1862 = bfd_get_32 (abfd, note->descdata + 24);
1863 elf_tdata (abfd)->core->program
1864 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1865 elf_tdata (abfd)->core->command
1866 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1867
1868 return TRUE;
1869 }
1870
1871 static char *
1872 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1873 ...)
1874 {
1875 switch (note_type)
1876 {
1877 default:
1878 return NULL;
1879
1880 case NT_PRPSINFO:
1881 {
1882 char data[136] ATTRIBUTE_NONSTRING;
1883 va_list ap;
1884
1885 va_start (ap, note_type);
1886 memset (data, 0, sizeof (data));
1887 strncpy (data + 40, va_arg (ap, const char *), 16);
1888 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1889 DIAGNOSTIC_PUSH;
1890 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1891 -Wstringop-truncation:
1892 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1893 */
1894 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1895 #endif
1896 strncpy (data + 56, va_arg (ap, const char *), 80);
1897 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1898 DIAGNOSTIC_POP;
1899 #endif
1900 va_end (ap);
1901 return elfcore_write_note (abfd, buf, bufsiz,
1902 "CORE", note_type, data, sizeof (data));
1903 }
1904
1905 case NT_PRSTATUS:
1906 {
1907 char data[504];
1908 va_list ap;
1909 long pid;
1910 int cursig;
1911 const void *greg;
1912
1913 va_start (ap, note_type);
1914 memset (data, 0, 112);
1915 pid = va_arg (ap, long);
1916 bfd_put_32 (abfd, pid, data + 32);
1917 cursig = va_arg (ap, int);
1918 bfd_put_16 (abfd, cursig, data + 12);
1919 greg = va_arg (ap, const void *);
1920 memcpy (data + 112, greg, 384);
1921 memset (data + 496, 0, 8);
1922 va_end (ap);
1923 return elfcore_write_note (abfd, buf, bufsiz,
1924 "CORE", note_type, data, sizeof (data));
1925 }
1926 }
1927 }
1928
1929 /* Add extra PPC sections. */
1930
1931 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1932 {
1933 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1934 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1935 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1936 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1937 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1938 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1939 { NULL, 0, 0, 0, 0 }
1940 };
1941
1942 enum _ppc64_sec_type {
1943 sec_normal = 0,
1944 sec_opd = 1,
1945 sec_toc = 2
1946 };
1947
1948 struct _ppc64_elf_section_data
1949 {
1950 struct bfd_elf_section_data elf;
1951
1952 union
1953 {
1954 /* An array with one entry for each opd function descriptor,
1955 and some spares since opd entries may be either 16 or 24 bytes. */
1956 #define OPD_NDX(OFF) ((OFF) >> 4)
1957 struct _opd_sec_data
1958 {
1959 /* Points to the function code section for local opd entries. */
1960 asection **func_sec;
1961
1962 /* After editing .opd, adjust references to opd local syms. */
1963 long *adjust;
1964 } opd;
1965
1966 /* An array for toc sections, indexed by offset/8. */
1967 struct _toc_sec_data
1968 {
1969 /* Specifies the relocation symbol index used at a given toc offset. */
1970 unsigned *symndx;
1971
1972 /* And the relocation addend. */
1973 bfd_vma *add;
1974 } toc;
1975 } u;
1976
1977 enum _ppc64_sec_type sec_type:2;
1978
1979 /* Flag set when small branches are detected. Used to
1980 select suitable defaults for the stub group size. */
1981 unsigned int has_14bit_branch:1;
1982
1983 /* Flag set when PLTCALL relocs are detected. */
1984 unsigned int has_pltcall:1;
1985
1986 /* Flag set when section has PLT/GOT/TOC relocations that can be
1987 optimised. */
1988 unsigned int has_optrel:1;
1989 };
1990
1991 #define ppc64_elf_section_data(sec) \
1992 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
1993
1994 static bfd_boolean
1995 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
1996 {
1997 if (!sec->used_by_bfd)
1998 {
1999 struct _ppc64_elf_section_data *sdata;
2000 bfd_size_type amt = sizeof (*sdata);
2001
2002 sdata = bfd_zalloc (abfd, amt);
2003 if (sdata == NULL)
2004 return FALSE;
2005 sec->used_by_bfd = sdata;
2006 }
2007
2008 return _bfd_elf_new_section_hook (abfd, sec);
2009 }
2010
2011 static struct _opd_sec_data *
2012 get_opd_info (asection * sec)
2013 {
2014 if (sec != NULL
2015 && ppc64_elf_section_data (sec) != NULL
2016 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2017 return &ppc64_elf_section_data (sec)->u.opd;
2018 return NULL;
2019 }
2020 \f
2021 /* Parameters for the qsort hook. */
2022 static bfd_boolean synthetic_relocatable;
2023 static asection *synthetic_opd;
2024
2025 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2026
2027 static int
2028 compare_symbols (const void *ap, const void *bp)
2029 {
2030 const asymbol *a = *(const asymbol **) ap;
2031 const asymbol *b = *(const asymbol **) bp;
2032
2033 /* Section symbols first. */
2034 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2035 return -1;
2036 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2037 return 1;
2038
2039 /* then .opd symbols. */
2040 if (synthetic_opd != NULL)
2041 {
2042 if (strcmp (a->section->name, ".opd") == 0
2043 && strcmp (b->section->name, ".opd") != 0)
2044 return -1;
2045 if (strcmp (a->section->name, ".opd") != 0
2046 && strcmp (b->section->name, ".opd") == 0)
2047 return 1;
2048 }
2049
2050 /* then other code symbols. */
2051 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2052 == (SEC_CODE | SEC_ALLOC))
2053 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2054 != (SEC_CODE | SEC_ALLOC)))
2055 return -1;
2056
2057 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2058 != (SEC_CODE | SEC_ALLOC))
2059 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2060 == (SEC_CODE | SEC_ALLOC)))
2061 return 1;
2062
2063 if (synthetic_relocatable)
2064 {
2065 if (a->section->id < b->section->id)
2066 return -1;
2067
2068 if (a->section->id > b->section->id)
2069 return 1;
2070 }
2071
2072 if (a->value + a->section->vma < b->value + b->section->vma)
2073 return -1;
2074
2075 if (a->value + a->section->vma > b->value + b->section->vma)
2076 return 1;
2077
2078 /* For syms with the same value, prefer strong dynamic global function
2079 syms over other syms. */
2080 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2081 return -1;
2082
2083 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2084 return 1;
2085
2086 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2087 return -1;
2088
2089 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2090 return 1;
2091
2092 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2093 return -1;
2094
2095 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2096 return 1;
2097
2098 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2099 return -1;
2100
2101 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2102 return 1;
2103
2104 /* Finally, sort on where the symbol is in memory. The symbols will
2105 be in at most two malloc'd blocks, one for static syms, one for
2106 dynamic syms, and we distinguish the two blocks above by testing
2107 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2108 originally in the same order as the symbols (and we're not
2109 sorting the symbols themselves), this ensures a stable sort. */
2110 if (a < b)
2111 return -1;
2112 if (a > b)
2113 return 1;
2114 return 0;
2115 }
2116
2117 /* Search SYMS for a symbol of the given VALUE. */
2118
2119 static asymbol *
2120 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
2121 {
2122 long mid;
2123
2124 if (id == (unsigned) -1)
2125 {
2126 while (lo < hi)
2127 {
2128 mid = (lo + hi) >> 1;
2129 if (syms[mid]->value + syms[mid]->section->vma < value)
2130 lo = mid + 1;
2131 else if (syms[mid]->value + syms[mid]->section->vma > value)
2132 hi = mid;
2133 else
2134 return syms[mid];
2135 }
2136 }
2137 else
2138 {
2139 while (lo < hi)
2140 {
2141 mid = (lo + hi) >> 1;
2142 if (syms[mid]->section->id < id)
2143 lo = mid + 1;
2144 else if (syms[mid]->section->id > id)
2145 hi = mid;
2146 else if (syms[mid]->value < value)
2147 lo = mid + 1;
2148 else if (syms[mid]->value > value)
2149 hi = mid;
2150 else
2151 return syms[mid];
2152 }
2153 }
2154 return NULL;
2155 }
2156
2157 static bfd_boolean
2158 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2159 {
2160 bfd_vma vma = *(bfd_vma *) ptr;
2161 return ((section->flags & SEC_ALLOC) != 0
2162 && section->vma <= vma
2163 && vma < section->vma + section->size);
2164 }
2165
2166 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2167 entry syms. Also generate @plt symbols for the glink branch table.
2168 Returns count of synthetic symbols in RET or -1 on error. */
2169
2170 static long
2171 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2172 long static_count, asymbol **static_syms,
2173 long dyn_count, asymbol **dyn_syms,
2174 asymbol **ret)
2175 {
2176 asymbol *s;
2177 size_t i, j, count;
2178 char *names;
2179 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2180 asection *opd = NULL;
2181 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2182 asymbol **syms;
2183 int abi = abiversion (abfd);
2184
2185 *ret = NULL;
2186
2187 if (abi < 2)
2188 {
2189 opd = bfd_get_section_by_name (abfd, ".opd");
2190 if (opd == NULL && abi == 1)
2191 return 0;
2192 }
2193
2194 syms = NULL;
2195 codesecsym = 0;
2196 codesecsymend = 0;
2197 secsymend = 0;
2198 opdsymend = 0;
2199 symcount = 0;
2200 if (opd != NULL)
2201 {
2202 symcount = static_count;
2203 if (!relocatable)
2204 symcount += dyn_count;
2205 if (symcount == 0)
2206 return 0;
2207
2208 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2209 if (syms == NULL)
2210 return -1;
2211
2212 if (!relocatable && static_count != 0 && dyn_count != 0)
2213 {
2214 /* Use both symbol tables. */
2215 memcpy (syms, static_syms, static_count * sizeof (*syms));
2216 memcpy (syms + static_count, dyn_syms,
2217 (dyn_count + 1) * sizeof (*syms));
2218 }
2219 else if (!relocatable && static_count == 0)
2220 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2221 else
2222 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2223
2224 /* Trim uninteresting symbols. Interesting symbols are section,
2225 function, and notype symbols. */
2226 for (i = 0, j = 0; i < symcount; ++i)
2227 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2228 | BSF_RELC | BSF_SRELC)) == 0)
2229 syms[j++] = syms[i];
2230 symcount = j;
2231
2232 synthetic_relocatable = relocatable;
2233 synthetic_opd = opd;
2234 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2235
2236 if (!relocatable && symcount > 1)
2237 {
2238 /* Trim duplicate syms, since we may have merged the normal
2239 and dynamic symbols. Actually, we only care about syms
2240 that have different values, so trim any with the same
2241 value. Don't consider ifunc and ifunc resolver symbols
2242 duplicates however, because GDB wants to know whether a
2243 text symbol is an ifunc resolver. */
2244 for (i = 1, j = 1; i < symcount; ++i)
2245 {
2246 const asymbol *s0 = syms[i - 1];
2247 const asymbol *s1 = syms[i];
2248
2249 if ((s0->value + s0->section->vma
2250 != s1->value + s1->section->vma)
2251 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2252 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2253 syms[j++] = syms[i];
2254 }
2255 symcount = j;
2256 }
2257
2258 i = 0;
2259 /* Note that here and in compare_symbols we can't compare opd and
2260 sym->section directly. With separate debug info files, the
2261 symbols will be extracted from the debug file while abfd passed
2262 to this function is the real binary. */
2263 if (strcmp (syms[i]->section->name, ".opd") == 0)
2264 ++i;
2265 codesecsym = i;
2266
2267 for (; i < symcount; ++i)
2268 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2269 | SEC_THREAD_LOCAL))
2270 != (SEC_CODE | SEC_ALLOC))
2271 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2272 break;
2273 codesecsymend = i;
2274
2275 for (; i < symcount; ++i)
2276 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2277 break;
2278 secsymend = i;
2279
2280 for (; i < symcount; ++i)
2281 if (strcmp (syms[i]->section->name, ".opd") != 0)
2282 break;
2283 opdsymend = i;
2284
2285 for (; i < symcount; ++i)
2286 if (((syms[i]->section->flags
2287 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2288 != (SEC_CODE | SEC_ALLOC))
2289 break;
2290 symcount = i;
2291 }
2292 count = 0;
2293
2294 if (relocatable)
2295 {
2296 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2297 arelent *r;
2298 size_t size;
2299 size_t relcount;
2300
2301 if (opdsymend == secsymend)
2302 goto done;
2303
2304 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2305 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2306 if (relcount == 0)
2307 goto done;
2308
2309 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2310 {
2311 count = -1;
2312 goto done;
2313 }
2314
2315 size = 0;
2316 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2317 {
2318 asymbol *sym;
2319
2320 while (r < opd->relocation + relcount
2321 && r->address < syms[i]->value + opd->vma)
2322 ++r;
2323
2324 if (r == opd->relocation + relcount)
2325 break;
2326
2327 if (r->address != syms[i]->value + opd->vma)
2328 continue;
2329
2330 if (r->howto->type != R_PPC64_ADDR64)
2331 continue;
2332
2333 sym = *r->sym_ptr_ptr;
2334 if (!sym_exists_at (syms, opdsymend, symcount,
2335 sym->section->id, sym->value + r->addend))
2336 {
2337 ++count;
2338 size += sizeof (asymbol);
2339 size += strlen (syms[i]->name) + 2;
2340 }
2341 }
2342
2343 if (size == 0)
2344 goto done;
2345 s = *ret = bfd_malloc (size);
2346 if (s == NULL)
2347 {
2348 count = -1;
2349 goto done;
2350 }
2351
2352 names = (char *) (s + count);
2353
2354 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2355 {
2356 asymbol *sym;
2357
2358 while (r < opd->relocation + relcount
2359 && r->address < syms[i]->value + opd->vma)
2360 ++r;
2361
2362 if (r == opd->relocation + relcount)
2363 break;
2364
2365 if (r->address != syms[i]->value + opd->vma)
2366 continue;
2367
2368 if (r->howto->type != R_PPC64_ADDR64)
2369 continue;
2370
2371 sym = *r->sym_ptr_ptr;
2372 if (!sym_exists_at (syms, opdsymend, symcount,
2373 sym->section->id, sym->value + r->addend))
2374 {
2375 size_t len;
2376
2377 *s = *syms[i];
2378 s->flags |= BSF_SYNTHETIC;
2379 s->section = sym->section;
2380 s->value = sym->value + r->addend;
2381 s->name = names;
2382 *names++ = '.';
2383 len = strlen (syms[i]->name);
2384 memcpy (names, syms[i]->name, len + 1);
2385 names += len + 1;
2386 /* Have udata.p point back to the original symbol this
2387 synthetic symbol was derived from. */
2388 s->udata.p = syms[i];
2389 s++;
2390 }
2391 }
2392 }
2393 else
2394 {
2395 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2396 bfd_byte *contents = NULL;
2397 size_t size;
2398 size_t plt_count = 0;
2399 bfd_vma glink_vma = 0, resolv_vma = 0;
2400 asection *dynamic, *glink = NULL, *relplt = NULL;
2401 arelent *p;
2402
2403 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2404 {
2405 free_contents_and_exit_err:
2406 count = -1;
2407 free_contents_and_exit:
2408 if (contents)
2409 free (contents);
2410 goto done;
2411 }
2412
2413 size = 0;
2414 for (i = secsymend; i < opdsymend; ++i)
2415 {
2416 bfd_vma ent;
2417
2418 /* Ignore bogus symbols. */
2419 if (syms[i]->value > opd->size - 8)
2420 continue;
2421
2422 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2423 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2424 {
2425 ++count;
2426 size += sizeof (asymbol);
2427 size += strlen (syms[i]->name) + 2;
2428 }
2429 }
2430
2431 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2432 if (dyn_count != 0
2433 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2434 {
2435 bfd_byte *dynbuf, *extdyn, *extdynend;
2436 size_t extdynsize;
2437 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2438
2439 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2440 goto free_contents_and_exit_err;
2441
2442 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2443 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2444
2445 extdyn = dynbuf;
2446 extdynend = extdyn + dynamic->size;
2447 for (; extdyn < extdynend; extdyn += extdynsize)
2448 {
2449 Elf_Internal_Dyn dyn;
2450 (*swap_dyn_in) (abfd, extdyn, &dyn);
2451
2452 if (dyn.d_tag == DT_NULL)
2453 break;
2454
2455 if (dyn.d_tag == DT_PPC64_GLINK)
2456 {
2457 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2458 See comment in ppc64_elf_finish_dynamic_sections. */
2459 glink_vma = dyn.d_un.d_val + 8 * 4;
2460 /* The .glink section usually does not survive the final
2461 link; search for the section (usually .text) where the
2462 glink stubs now reside. */
2463 glink = bfd_sections_find_if (abfd, section_covers_vma,
2464 &glink_vma);
2465 break;
2466 }
2467 }
2468
2469 free (dynbuf);
2470 }
2471
2472 if (glink != NULL)
2473 {
2474 /* Determine __glink trampoline by reading the relative branch
2475 from the first glink stub. */
2476 bfd_byte buf[4];
2477 unsigned int off = 0;
2478
2479 while (bfd_get_section_contents (abfd, glink, buf,
2480 glink_vma + off - glink->vma, 4))
2481 {
2482 unsigned int insn = bfd_get_32 (abfd, buf);
2483 insn ^= B_DOT;
2484 if ((insn & ~0x3fffffc) == 0)
2485 {
2486 resolv_vma
2487 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2488 break;
2489 }
2490 off += 4;
2491 if (off > 4)
2492 break;
2493 }
2494
2495 if (resolv_vma)
2496 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2497
2498 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2499 if (relplt != NULL)
2500 {
2501 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2502 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
2503 goto free_contents_and_exit_err;
2504
2505 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2506 size += plt_count * sizeof (asymbol);
2507
2508 p = relplt->relocation;
2509 for (i = 0; i < plt_count; i++, p++)
2510 {
2511 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2512 if (p->addend != 0)
2513 size += sizeof ("+0x") - 1 + 16;
2514 }
2515 }
2516 }
2517
2518 if (size == 0)
2519 goto free_contents_and_exit;
2520 s = *ret = bfd_malloc (size);
2521 if (s == NULL)
2522 goto free_contents_and_exit_err;
2523
2524 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2525
2526 for (i = secsymend; i < opdsymend; ++i)
2527 {
2528 bfd_vma ent;
2529
2530 if (syms[i]->value > opd->size - 8)
2531 continue;
2532
2533 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2534 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2535 {
2536 size_t lo, hi;
2537 size_t len;
2538 asection *sec = abfd->sections;
2539
2540 *s = *syms[i];
2541 lo = codesecsym;
2542 hi = codesecsymend;
2543 while (lo < hi)
2544 {
2545 size_t mid = (lo + hi) >> 1;
2546 if (syms[mid]->section->vma < ent)
2547 lo = mid + 1;
2548 else if (syms[mid]->section->vma > ent)
2549 hi = mid;
2550 else
2551 {
2552 sec = syms[mid]->section;
2553 break;
2554 }
2555 }
2556
2557 if (lo >= hi && lo > codesecsym)
2558 sec = syms[lo - 1]->section;
2559
2560 for (; sec != NULL; sec = sec->next)
2561 {
2562 if (sec->vma > ent)
2563 break;
2564 /* SEC_LOAD may not be set if SEC is from a separate debug
2565 info file. */
2566 if ((sec->flags & SEC_ALLOC) == 0)
2567 break;
2568 if ((sec->flags & SEC_CODE) != 0)
2569 s->section = sec;
2570 }
2571 s->flags |= BSF_SYNTHETIC;
2572 s->value = ent - s->section->vma;
2573 s->name = names;
2574 *names++ = '.';
2575 len = strlen (syms[i]->name);
2576 memcpy (names, syms[i]->name, len + 1);
2577 names += len + 1;
2578 /* Have udata.p point back to the original symbol this
2579 synthetic symbol was derived from. */
2580 s->udata.p = syms[i];
2581 s++;
2582 }
2583 }
2584 free (contents);
2585
2586 if (glink != NULL && relplt != NULL)
2587 {
2588 if (resolv_vma)
2589 {
2590 /* Add a symbol for the main glink trampoline. */
2591 memset (s, 0, sizeof *s);
2592 s->the_bfd = abfd;
2593 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2594 s->section = glink;
2595 s->value = resolv_vma - glink->vma;
2596 s->name = names;
2597 memcpy (names, "__glink_PLTresolve",
2598 sizeof ("__glink_PLTresolve"));
2599 names += sizeof ("__glink_PLTresolve");
2600 s++;
2601 count++;
2602 }
2603
2604 /* FIXME: It would be very much nicer to put sym@plt on the
2605 stub rather than on the glink branch table entry. The
2606 objdump disassembler would then use a sensible symbol
2607 name on plt calls. The difficulty in doing so is
2608 a) finding the stubs, and,
2609 b) matching stubs against plt entries, and,
2610 c) there can be multiple stubs for a given plt entry.
2611
2612 Solving (a) could be done by code scanning, but older
2613 ppc64 binaries used different stubs to current code.
2614 (b) is the tricky one since you need to known the toc
2615 pointer for at least one function that uses a pic stub to
2616 be able to calculate the plt address referenced.
2617 (c) means gdb would need to set multiple breakpoints (or
2618 find the glink branch itself) when setting breakpoints
2619 for pending shared library loads. */
2620 p = relplt->relocation;
2621 for (i = 0; i < plt_count; i++, p++)
2622 {
2623 size_t len;
2624
2625 *s = **p->sym_ptr_ptr;
2626 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2627 we are defining a symbol, ensure one of them is set. */
2628 if ((s->flags & BSF_LOCAL) == 0)
2629 s->flags |= BSF_GLOBAL;
2630 s->flags |= BSF_SYNTHETIC;
2631 s->section = glink;
2632 s->value = glink_vma - glink->vma;
2633 s->name = names;
2634 s->udata.p = NULL;
2635 len = strlen ((*p->sym_ptr_ptr)->name);
2636 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2637 names += len;
2638 if (p->addend != 0)
2639 {
2640 memcpy (names, "+0x", sizeof ("+0x") - 1);
2641 names += sizeof ("+0x") - 1;
2642 bfd_sprintf_vma (abfd, names, p->addend);
2643 names += strlen (names);
2644 }
2645 memcpy (names, "@plt", sizeof ("@plt"));
2646 names += sizeof ("@plt");
2647 s++;
2648 if (abi < 2)
2649 {
2650 glink_vma += 8;
2651 if (i >= 0x8000)
2652 glink_vma += 4;
2653 }
2654 else
2655 glink_vma += 4;
2656 }
2657 count += plt_count;
2658 }
2659 }
2660
2661 done:
2662 free (syms);
2663 return count;
2664 }
2665 \f
2666 /* The following functions are specific to the ELF linker, while
2667 functions above are used generally. Those named ppc64_elf_* are
2668 called by the main ELF linker code. They appear in this file more
2669 or less in the order in which they are called. eg.
2670 ppc64_elf_check_relocs is called early in the link process,
2671 ppc64_elf_finish_dynamic_sections is one of the last functions
2672 called.
2673
2674 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2675 functions have both a function code symbol and a function descriptor
2676 symbol. A call to foo in a relocatable object file looks like:
2677
2678 . .text
2679 . x:
2680 . bl .foo
2681 . nop
2682
2683 The function definition in another object file might be:
2684
2685 . .section .opd
2686 . foo: .quad .foo
2687 . .quad .TOC.@tocbase
2688 . .quad 0
2689 .
2690 . .text
2691 . .foo: blr
2692
2693 When the linker resolves the call during a static link, the branch
2694 unsurprisingly just goes to .foo and the .opd information is unused.
2695 If the function definition is in a shared library, things are a little
2696 different: The call goes via a plt call stub, the opd information gets
2697 copied to the plt, and the linker patches the nop.
2698
2699 . x:
2700 . bl .foo_stub
2701 . ld 2,40(1)
2702 .
2703 .
2704 . .foo_stub:
2705 . std 2,40(1) # in practice, the call stub
2706 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2707 . addi 11,11,Lfoo@toc@l # this is the general idea
2708 . ld 12,0(11)
2709 . ld 2,8(11)
2710 . mtctr 12
2711 . ld 11,16(11)
2712 . bctr
2713 .
2714 . .section .plt
2715 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2716
2717 The "reloc ()" notation is supposed to indicate that the linker emits
2718 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2719 copying.
2720
2721 What are the difficulties here? Well, firstly, the relocations
2722 examined by the linker in check_relocs are against the function code
2723 sym .foo, while the dynamic relocation in the plt is emitted against
2724 the function descriptor symbol, foo. Somewhere along the line, we need
2725 to carefully copy dynamic link information from one symbol to the other.
2726 Secondly, the generic part of the elf linker will make .foo a dynamic
2727 symbol as is normal for most other backends. We need foo dynamic
2728 instead, at least for an application final link. However, when
2729 creating a shared library containing foo, we need to have both symbols
2730 dynamic so that references to .foo are satisfied during the early
2731 stages of linking. Otherwise the linker might decide to pull in a
2732 definition from some other object, eg. a static library.
2733
2734 Update: As of August 2004, we support a new convention. Function
2735 calls may use the function descriptor symbol, ie. "bl foo". This
2736 behaves exactly as "bl .foo". */
2737
2738 /* Of those relocs that might be copied as dynamic relocs, this
2739 function selects those that must be copied when linking a shared
2740 library or PIE, even when the symbol is local. */
2741
2742 static int
2743 must_be_dyn_reloc (struct bfd_link_info *info,
2744 enum elf_ppc64_reloc_type r_type)
2745 {
2746 switch (r_type)
2747 {
2748 default:
2749 /* Only relative relocs can be resolved when the object load
2750 address isn't fixed. DTPREL64 is excluded because the
2751 dynamic linker needs to differentiate global dynamic from
2752 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2753 return 1;
2754
2755 case R_PPC64_REL32:
2756 case R_PPC64_REL64:
2757 case R_PPC64_REL30:
2758 case R_PPC64_TOC16:
2759 case R_PPC64_TOC16_DS:
2760 case R_PPC64_TOC16_LO:
2761 case R_PPC64_TOC16_HI:
2762 case R_PPC64_TOC16_HA:
2763 case R_PPC64_TOC16_LO_DS:
2764 return 0;
2765
2766 case R_PPC64_TPREL16:
2767 case R_PPC64_TPREL16_LO:
2768 case R_PPC64_TPREL16_HI:
2769 case R_PPC64_TPREL16_HA:
2770 case R_PPC64_TPREL16_DS:
2771 case R_PPC64_TPREL16_LO_DS:
2772 case R_PPC64_TPREL16_HIGH:
2773 case R_PPC64_TPREL16_HIGHA:
2774 case R_PPC64_TPREL16_HIGHER:
2775 case R_PPC64_TPREL16_HIGHERA:
2776 case R_PPC64_TPREL16_HIGHEST:
2777 case R_PPC64_TPREL16_HIGHESTA:
2778 case R_PPC64_TPREL64:
2779 case R_PPC64_TPREL34:
2780 /* These relocations are relative but in a shared library the
2781 linker doesn't know the thread pointer base. */
2782 return bfd_link_dll (info);
2783 }
2784 }
2785
2786 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2787 copying dynamic variables from a shared lib into an app's dynbss
2788 section, and instead use a dynamic relocation to point into the
2789 shared lib. With code that gcc generates, it's vital that this be
2790 enabled; In the PowerPC64 ABI, the address of a function is actually
2791 the address of a function descriptor, which resides in the .opd
2792 section. gcc uses the descriptor directly rather than going via the
2793 GOT as some other ABI's do, which means that initialized function
2794 pointers must reference the descriptor. Thus, a function pointer
2795 initialized to the address of a function in a shared library will
2796 either require a copy reloc, or a dynamic reloc. Using a copy reloc
2797 redefines the function descriptor symbol to point to the copy. This
2798 presents a problem as a plt entry for that function is also
2799 initialized from the function descriptor symbol and the copy reloc
2800 may not be initialized first. */
2801 #define ELIMINATE_COPY_RELOCS 1
2802
2803 /* Section name for stubs is the associated section name plus this
2804 string. */
2805 #define STUB_SUFFIX ".stub"
2806
2807 /* Linker stubs.
2808 ppc_stub_long_branch:
2809 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2810 destination, but a 24 bit branch in a stub section will reach.
2811 . b dest
2812
2813 ppc_stub_plt_branch:
2814 Similar to the above, but a 24 bit branch in the stub section won't
2815 reach its destination.
2816 . addis %r11,%r2,xxx@toc@ha
2817 . ld %r12,xxx@toc@l(%r11)
2818 . mtctr %r12
2819 . bctr
2820
2821 ppc_stub_plt_call:
2822 Used to call a function in a shared library. If it so happens that
2823 the plt entry referenced crosses a 64k boundary, then an extra
2824 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2825 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
2826 . addis %r11,%r2,xxx@toc@ha
2827 . ld %r12,xxx+0@toc@l(%r11)
2828 . mtctr %r12
2829 . ld %r2,xxx+8@toc@l(%r11)
2830 . ld %r11,xxx+16@toc@l(%r11)
2831 . bctr
2832
2833 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2834 code to adjust the value and save r2 to support multiple toc sections.
2835 A ppc_stub_long_branch with an r2 offset looks like:
2836 . std %r2,40(%r1)
2837 . addis %r2,%r2,off@ha
2838 . addi %r2,%r2,off@l
2839 . b dest
2840
2841 A ppc_stub_plt_branch with an r2 offset looks like:
2842 . std %r2,40(%r1)
2843 . addis %r11,%r2,xxx@toc@ha
2844 . ld %r12,xxx@toc@l(%r11)
2845 . addis %r2,%r2,off@ha
2846 . addi %r2,%r2,off@l
2847 . mtctr %r12
2848 . bctr
2849
2850 All of the above stubs are shown as their ELFv1 variants. ELFv2
2851 variants exist too, simpler for plt calls since a new toc pointer
2852 and static chain are not loaded by the stub. In addition, ELFv2
2853 has some more complex stubs to handle calls marked with NOTOC
2854 relocs from functions where r2 is not a valid toc pointer. These
2855 come in two flavours, the ones shown below, and _both variants that
2856 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2857 one call is from a function where r2 is used as the toc pointer but
2858 needs a toc adjusting stub for small-model multi-toc, and another
2859 call is from a function where r2 is not valid.
2860 ppc_stub_long_branch_notoc:
2861 . mflr %r12
2862 . bcl 20,31,1f
2863 . 1:
2864 . mflr %r11
2865 . mtlr %r12
2866 . addis %r12,%r11,dest-1b@ha
2867 . addi %r12,%r12,dest-1b@l
2868 . b dest
2869
2870 ppc_stub_plt_branch_notoc:
2871 . mflr %r12
2872 . bcl 20,31,1f
2873 . 1:
2874 . mflr %r11
2875 . mtlr %r12
2876 . lis %r12,xxx-1b@highest
2877 . ori %r12,%r12,xxx-1b@higher
2878 . sldi %r12,%r12,32
2879 . oris %r12,%r12,xxx-1b@high
2880 . ori %r12,%r12,xxx-1b@l
2881 . add %r12,%r11,%r12
2882 . mtctr %r12
2883 . bctr
2884
2885 ppc_stub_plt_call_notoc:
2886 . mflr %r12
2887 . bcl 20,31,1f
2888 . 1:
2889 . mflr %r11
2890 . mtlr %r12
2891 . lis %r12,xxx-1b@highest
2892 . ori %r12,%r12,xxx-1b@higher
2893 . sldi %r12,%r12,32
2894 . oris %r12,%r12,xxx-1b@high
2895 . ori %r12,%r12,xxx-1b@l
2896 . ldx %r12,%r11,%r12
2897 . mtctr %r12
2898 . bctr
2899
2900 There are also ELFv1 powerxx variants of these stubs.
2901 ppc_stub_long_branch_notoc:
2902 . pla %r12,dest@pcrel
2903 . b dest
2904 ppc_stub_plt_branch_notoc:
2905 . lis %r11,(dest-1f)@highesta34
2906 . ori %r11,%r11,(dest-1f)@highera34
2907 . sldi %r11,%r11,34
2908 . 1: pla %r12,dest@pcrel
2909 . add %r12,%r11,%r12
2910 . mtctr %r12
2911 . bctr
2912 ppc_stub_plt_call_notoc:
2913 . lis %r11,(xxx-1f)@highesta34
2914 . ori %r11,%r11,(xxx-1f)@highera34
2915 . sldi %r11,%r11,34
2916 . 1: pla %r12,xxx@pcrel
2917 . ldx %r12,%r11,%r12
2918 . mtctr %r12
2919 . bctr
2920
2921 In cases where the high instructions would add zero, they are
2922 omitted and following instructions modified in some cases.
2923 For example, a powerxx ppc_stub_plt_call_notoc might simplify down
2924 to
2925 . pld %r12,xxx@pcrel
2926 . mtctr %r12
2927 . bctr
2928
2929 For a given stub group (a set of sections all using the same toc
2930 pointer value) there will be just one stub type used for any
2931 particular function symbol. For example, if printf is called from
2932 code with the tocsave optimization (ie. r2 saved in function
2933 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2934 and from other code without the tocsave optimization requiring a
2935 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2936 type will be created. Calls with the tocsave optimization will
2937 enter this stub after the instruction saving r2. A similar
2938 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2939 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2940 to call an external function like printf. If other calls to printf
2941 require a ppc_stub_plt_call linkage stub then a single
2942 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2943 call. If other calls to printf require a ppc_stub_plt_call_r2save
2944 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2945 be created and calls not requiring r2 to be saved will enter the
2946 stub after the r2 save instruction. There is an analogous
2947 hierarchy of long branch and plt branch stubs for local call
2948 linkage. */
2949
2950 enum ppc_stub_type
2951 {
2952 ppc_stub_none,
2953 ppc_stub_long_branch,
2954 ppc_stub_long_branch_r2off,
2955 ppc_stub_long_branch_notoc,
2956 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
2957 ppc_stub_plt_branch,
2958 ppc_stub_plt_branch_r2off,
2959 ppc_stub_plt_branch_notoc,
2960 ppc_stub_plt_branch_both,
2961 ppc_stub_plt_call,
2962 ppc_stub_plt_call_r2save,
2963 ppc_stub_plt_call_notoc,
2964 ppc_stub_plt_call_both,
2965 ppc_stub_global_entry,
2966 ppc_stub_save_res
2967 };
2968
2969 /* Information on stub grouping. */
2970 struct map_stub
2971 {
2972 /* The stub section. */
2973 asection *stub_sec;
2974 /* This is the section to which stubs in the group will be attached. */
2975 asection *link_sec;
2976 /* Next group. */
2977 struct map_stub *next;
2978 /* Whether to emit a copy of register save/restore functions in this
2979 group. */
2980 int needs_save_res;
2981 /* Current offset within stubs after the insn restoring lr in a
2982 _notoc or _both stub using bcl for pc-relative addressing, or
2983 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
2984 unsigned int lr_restore;
2985 /* Accumulated size of EH info emitted to describe return address
2986 if stubs modify lr. Does not include 17 byte FDE header. */
2987 unsigned int eh_size;
2988 /* Offset in glink_eh_frame to the start of EH info for this group. */
2989 unsigned int eh_base;
2990 };
2991
2992 struct ppc_stub_hash_entry
2993 {
2994 /* Base hash table entry structure. */
2995 struct bfd_hash_entry root;
2996
2997 enum ppc_stub_type stub_type;
2998
2999 /* Group information. */
3000 struct map_stub *group;
3001
3002 /* Offset within stub_sec of the beginning of this stub. */
3003 bfd_vma stub_offset;
3004
3005 /* Given the symbol's value and its section we can determine its final
3006 value when building the stubs (so the stub knows where to jump. */
3007 bfd_vma target_value;
3008 asection *target_section;
3009
3010 /* The symbol table entry, if any, that this was derived from. */
3011 struct ppc_link_hash_entry *h;
3012 struct plt_entry *plt_ent;
3013
3014 /* Symbol type. */
3015 unsigned char symtype;
3016
3017 /* Symbol st_other. */
3018 unsigned char other;
3019 };
3020
3021 struct ppc_branch_hash_entry
3022 {
3023 /* Base hash table entry structure. */
3024 struct bfd_hash_entry root;
3025
3026 /* Offset within branch lookup table. */
3027 unsigned int offset;
3028
3029 /* Generation marker. */
3030 unsigned int iter;
3031 };
3032
3033 /* Used to track dynamic relocations for local symbols. */
3034 struct ppc_dyn_relocs
3035 {
3036 struct ppc_dyn_relocs *next;
3037
3038 /* The input section of the reloc. */
3039 asection *sec;
3040
3041 /* Total number of relocs copied for the input section. */
3042 unsigned int count : 31;
3043
3044 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3045 unsigned int ifunc : 1;
3046 };
3047
3048 struct ppc_link_hash_entry
3049 {
3050 struct elf_link_hash_entry elf;
3051
3052 union
3053 {
3054 /* A pointer to the most recently used stub hash entry against this
3055 symbol. */
3056 struct ppc_stub_hash_entry *stub_cache;
3057
3058 /* A pointer to the next symbol starting with a '.' */
3059 struct ppc_link_hash_entry *next_dot_sym;
3060 } u;
3061
3062 /* Track dynamic relocs copied for this symbol. */
3063 struct elf_dyn_relocs *dyn_relocs;
3064
3065 /* Link between function code and descriptor symbols. */
3066 struct ppc_link_hash_entry *oh;
3067
3068 /* Flag function code and descriptor symbols. */
3069 unsigned int is_func:1;
3070 unsigned int is_func_descriptor:1;
3071 unsigned int fake:1;
3072
3073 /* Whether global opd/toc sym has been adjusted or not.
3074 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3075 should be set for all globals defined in any opd/toc section. */
3076 unsigned int adjust_done:1;
3077
3078 /* Set if this is an out-of-line register save/restore function,
3079 with non-standard calling convention. */
3080 unsigned int save_res:1;
3081
3082 /* Set if a duplicate symbol with non-zero localentry is detected,
3083 even when the duplicate symbol does not provide a definition. */
3084 unsigned int non_zero_localentry:1;
3085
3086 /* Contexts in which symbol is used in the GOT (or TOC).
3087 Bits are or'd into the mask as the corresponding relocs are
3088 encountered during check_relocs, with TLS_TLS being set when any
3089 of the other TLS bits are set. tls_optimize clears bits when
3090 optimizing to indicate the corresponding GOT entry type is not
3091 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3092 set TLS_GDIE when a GD reloc turns into an IE one.
3093 These flags are also kept for local symbols. */
3094 #define TLS_TLS 1 /* Any TLS reloc. */
3095 #define TLS_GD 2 /* GD reloc. */
3096 #define TLS_LD 4 /* LD reloc. */
3097 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
3098 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3099 #define TLS_MARK 32 /* __tls_get_addr call marked. */
3100 #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
3101 #define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
3102 unsigned char tls_mask;
3103
3104 /* The above field is also used to mark function symbols. In which
3105 case TLS_TLS will be 0. */
3106 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3107 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
3108 #define NON_GOT 256 /* local symbol plt, not stored. */
3109 };
3110
3111 /* ppc64 ELF linker hash table. */
3112
3113 struct ppc_link_hash_table
3114 {
3115 struct elf_link_hash_table elf;
3116
3117 /* The stub hash table. */
3118 struct bfd_hash_table stub_hash_table;
3119
3120 /* Another hash table for plt_branch stubs. */
3121 struct bfd_hash_table branch_hash_table;
3122
3123 /* Hash table for function prologue tocsave. */
3124 htab_t tocsave_htab;
3125
3126 /* Various options and other info passed from the linker. */
3127 struct ppc64_elf_params *params;
3128
3129 /* The size of sec_info below. */
3130 unsigned int sec_info_arr_size;
3131
3132 /* Per-section array of extra section info. Done this way rather
3133 than as part of ppc64_elf_section_data so we have the info for
3134 non-ppc64 sections. */
3135 struct
3136 {
3137 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3138 bfd_vma toc_off;
3139
3140 union
3141 {
3142 /* The section group that this section belongs to. */
3143 struct map_stub *group;
3144 /* A temp section list pointer. */
3145 asection *list;
3146 } u;
3147 } *sec_info;
3148
3149 /* Linked list of groups. */
3150 struct map_stub *group;
3151
3152 /* Temp used when calculating TOC pointers. */
3153 bfd_vma toc_curr;
3154 bfd *toc_bfd;
3155 asection *toc_first_sec;
3156
3157 /* Used when adding symbols. */
3158 struct ppc_link_hash_entry *dot_syms;
3159
3160 /* Shortcuts to get to dynamic linker sections. */
3161 asection *glink;
3162 asection *global_entry;
3163 asection *sfpr;
3164 asection *pltlocal;
3165 asection *relpltlocal;
3166 asection *brlt;
3167 asection *relbrlt;
3168 asection *glink_eh_frame;
3169
3170 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3171 struct ppc_link_hash_entry *tls_get_addr;
3172 struct ppc_link_hash_entry *tls_get_addr_fd;
3173
3174 /* The size of reliplt used by got entry relocs. */
3175 bfd_size_type got_reli_size;
3176
3177 /* Statistics. */
3178 unsigned long stub_count[ppc_stub_global_entry];
3179
3180 /* Number of stubs against global syms. */
3181 unsigned long stub_globals;
3182
3183 /* Set if we're linking code with function descriptors. */
3184 unsigned int opd_abi:1;
3185
3186 /* Support for multiple toc sections. */
3187 unsigned int do_multi_toc:1;
3188 unsigned int multi_toc_needed:1;
3189 unsigned int second_toc_pass:1;
3190 unsigned int do_toc_opt:1;
3191
3192 /* Set if tls optimization is enabled. */
3193 unsigned int do_tls_opt:1;
3194
3195 /* Set if inline plt calls should be converted to direct calls. */
3196 unsigned int can_convert_all_inline_plt:1;
3197
3198 /* Set on error. */
3199 unsigned int stub_error:1;
3200
3201 /* Whether func_desc_adjust needs to be run over symbols. */
3202 unsigned int need_func_desc_adj:1;
3203
3204 /* Whether there exist local gnu indirect function resolvers,
3205 referenced by dynamic relocations. */
3206 unsigned int local_ifunc_resolver:1;
3207 unsigned int maybe_local_ifunc_resolver:1;
3208
3209 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3210 unsigned int has_plt_localentry0:1;
3211
3212 /* Whether calls are made via the PLT from NOTOC functions. */
3213 unsigned int notoc_plt:1;
3214
3215 /* Whether to use powerxx instructions in linkage stubs. */
3216 unsigned int powerxx_stubs:1;
3217
3218 /* Incremented every time we size stubs. */
3219 unsigned int stub_iteration;
3220
3221 /* Small local sym cache. */
3222 struct sym_cache sym_cache;
3223 };
3224
3225 /* Rename some of the generic section flags to better document how they
3226 are used here. */
3227
3228 /* Nonzero if this section has TLS related relocations. */
3229 #define has_tls_reloc sec_flg0
3230
3231 /* Nonzero if this section has a call to __tls_get_addr lacking marker
3232 relocations. */
3233 #define nomark_tls_get_addr sec_flg1
3234
3235 /* Nonzero if this section has any toc or got relocs. */
3236 #define has_toc_reloc sec_flg2
3237
3238 /* Nonzero if this section has a call to another section that uses
3239 the toc or got. */
3240 #define makes_toc_func_call sec_flg3
3241
3242 /* Recursion protection when determining above flag. */
3243 #define call_check_in_progress sec_flg4
3244 #define call_check_done sec_flg5
3245
3246 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3247
3248 #define ppc_hash_table(p) \
3249 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3250 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3251
3252 #define ppc_stub_hash_lookup(table, string, create, copy) \
3253 ((struct ppc_stub_hash_entry *) \
3254 bfd_hash_lookup ((table), (string), (create), (copy)))
3255
3256 #define ppc_branch_hash_lookup(table, string, create, copy) \
3257 ((struct ppc_branch_hash_entry *) \
3258 bfd_hash_lookup ((table), (string), (create), (copy)))
3259
3260 /* Create an entry in the stub hash table. */
3261
3262 static struct bfd_hash_entry *
3263 stub_hash_newfunc (struct bfd_hash_entry *entry,
3264 struct bfd_hash_table *table,
3265 const char *string)
3266 {
3267 /* Allocate the structure if it has not already been allocated by a
3268 subclass. */
3269 if (entry == NULL)
3270 {
3271 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3272 if (entry == NULL)
3273 return entry;
3274 }
3275
3276 /* Call the allocation method of the superclass. */
3277 entry = bfd_hash_newfunc (entry, table, string);
3278 if (entry != NULL)
3279 {
3280 struct ppc_stub_hash_entry *eh;
3281
3282 /* Initialize the local fields. */
3283 eh = (struct ppc_stub_hash_entry *) entry;
3284 eh->stub_type = ppc_stub_none;
3285 eh->group = NULL;
3286 eh->stub_offset = 0;
3287 eh->target_value = 0;
3288 eh->target_section = NULL;
3289 eh->h = NULL;
3290 eh->plt_ent = NULL;
3291 eh->other = 0;
3292 }
3293
3294 return entry;
3295 }
3296
3297 /* Create an entry in the branch hash table. */
3298
3299 static struct bfd_hash_entry *
3300 branch_hash_newfunc (struct bfd_hash_entry *entry,
3301 struct bfd_hash_table *table,
3302 const char *string)
3303 {
3304 /* Allocate the structure if it has not already been allocated by a
3305 subclass. */
3306 if (entry == NULL)
3307 {
3308 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3309 if (entry == NULL)
3310 return entry;
3311 }
3312
3313 /* Call the allocation method of the superclass. */
3314 entry = bfd_hash_newfunc (entry, table, string);
3315 if (entry != NULL)
3316 {
3317 struct ppc_branch_hash_entry *eh;
3318
3319 /* Initialize the local fields. */
3320 eh = (struct ppc_branch_hash_entry *) entry;
3321 eh->offset = 0;
3322 eh->iter = 0;
3323 }
3324
3325 return entry;
3326 }
3327
3328 /* Create an entry in a ppc64 ELF linker hash table. */
3329
3330 static struct bfd_hash_entry *
3331 link_hash_newfunc (struct bfd_hash_entry *entry,
3332 struct bfd_hash_table *table,
3333 const char *string)
3334 {
3335 /* Allocate the structure if it has not already been allocated by a
3336 subclass. */
3337 if (entry == NULL)
3338 {
3339 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3340 if (entry == NULL)
3341 return entry;
3342 }
3343
3344 /* Call the allocation method of the superclass. */
3345 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3346 if (entry != NULL)
3347 {
3348 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3349
3350 memset (&eh->u.stub_cache, 0,
3351 (sizeof (struct ppc_link_hash_entry)
3352 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3353
3354 /* When making function calls, old ABI code references function entry
3355 points (dot symbols), while new ABI code references the function
3356 descriptor symbol. We need to make any combination of reference and
3357 definition work together, without breaking archive linking.
3358
3359 For a defined function "foo" and an undefined call to "bar":
3360 An old object defines "foo" and ".foo", references ".bar" (possibly
3361 "bar" too).
3362 A new object defines "foo" and references "bar".
3363
3364 A new object thus has no problem with its undefined symbols being
3365 satisfied by definitions in an old object. On the other hand, the
3366 old object won't have ".bar" satisfied by a new object.
3367
3368 Keep a list of newly added dot-symbols. */
3369
3370 if (string[0] == '.')
3371 {
3372 struct ppc_link_hash_table *htab;
3373
3374 htab = (struct ppc_link_hash_table *) table;
3375 eh->u.next_dot_sym = htab->dot_syms;
3376 htab->dot_syms = eh;
3377 }
3378 }
3379
3380 return entry;
3381 }
3382
3383 struct tocsave_entry
3384 {
3385 asection *sec;
3386 bfd_vma offset;
3387 };
3388
3389 static hashval_t
3390 tocsave_htab_hash (const void *p)
3391 {
3392 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3393 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3394 }
3395
3396 static int
3397 tocsave_htab_eq (const void *p1, const void *p2)
3398 {
3399 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3400 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3401 return e1->sec == e2->sec && e1->offset == e2->offset;
3402 }
3403
3404 /* Destroy a ppc64 ELF linker hash table. */
3405
3406 static void
3407 ppc64_elf_link_hash_table_free (bfd *obfd)
3408 {
3409 struct ppc_link_hash_table *htab;
3410
3411 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3412 if (htab->tocsave_htab)
3413 htab_delete (htab->tocsave_htab);
3414 bfd_hash_table_free (&htab->branch_hash_table);
3415 bfd_hash_table_free (&htab->stub_hash_table);
3416 _bfd_elf_link_hash_table_free (obfd);
3417 }
3418
3419 /* Create a ppc64 ELF linker hash table. */
3420
3421 static struct bfd_link_hash_table *
3422 ppc64_elf_link_hash_table_create (bfd *abfd)
3423 {
3424 struct ppc_link_hash_table *htab;
3425 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3426
3427 htab = bfd_zmalloc (amt);
3428 if (htab == NULL)
3429 return NULL;
3430
3431 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3432 sizeof (struct ppc_link_hash_entry),
3433 PPC64_ELF_DATA))
3434 {
3435 free (htab);
3436 return NULL;
3437 }
3438
3439 /* Init the stub hash table too. */
3440 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3441 sizeof (struct ppc_stub_hash_entry)))
3442 {
3443 _bfd_elf_link_hash_table_free (abfd);
3444 return NULL;
3445 }
3446
3447 /* And the branch hash table. */
3448 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3449 sizeof (struct ppc_branch_hash_entry)))
3450 {
3451 bfd_hash_table_free (&htab->stub_hash_table);
3452 _bfd_elf_link_hash_table_free (abfd);
3453 return NULL;
3454 }
3455
3456 htab->tocsave_htab = htab_try_create (1024,
3457 tocsave_htab_hash,
3458 tocsave_htab_eq,
3459 NULL);
3460 if (htab->tocsave_htab == NULL)
3461 {
3462 ppc64_elf_link_hash_table_free (abfd);
3463 return NULL;
3464 }
3465 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3466
3467 /* Initializing two fields of the union is just cosmetic. We really
3468 only care about glist, but when compiled on a 32-bit host the
3469 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3470 debugger inspection of these fields look nicer. */
3471 htab->elf.init_got_refcount.refcount = 0;
3472 htab->elf.init_got_refcount.glist = NULL;
3473 htab->elf.init_plt_refcount.refcount = 0;
3474 htab->elf.init_plt_refcount.glist = NULL;
3475 htab->elf.init_got_offset.offset = 0;
3476 htab->elf.init_got_offset.glist = NULL;
3477 htab->elf.init_plt_offset.offset = 0;
3478 htab->elf.init_plt_offset.glist = NULL;
3479
3480 return &htab->elf.root;
3481 }
3482
3483 /* Create sections for linker generated code. */
3484
3485 static bfd_boolean
3486 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3487 {
3488 struct ppc_link_hash_table *htab;
3489 flagword flags;
3490
3491 htab = ppc_hash_table (info);
3492
3493 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3494 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3495 if (htab->params->save_restore_funcs)
3496 {
3497 /* Create .sfpr for code to save and restore fp regs. */
3498 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3499 flags);
3500 if (htab->sfpr == NULL
3501 || !bfd_set_section_alignment (htab->sfpr, 2))
3502 return FALSE;
3503 }
3504
3505 if (bfd_link_relocatable (info))
3506 return TRUE;
3507
3508 /* Create .glink for lazy dynamic linking support. */
3509 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3510 flags);
3511 if (htab->glink == NULL
3512 || !bfd_set_section_alignment (htab->glink, 3))
3513 return FALSE;
3514
3515 /* The part of .glink used by global entry stubs, separate so that
3516 it can be aligned appropriately without affecting htab->glink. */
3517 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3518 flags);
3519 if (htab->global_entry == NULL
3520 || !bfd_set_section_alignment (htab->global_entry, 2))
3521 return FALSE;
3522
3523 if (!info->no_ld_generated_unwind_info)
3524 {
3525 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3526 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3527 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3528 ".eh_frame",
3529 flags);
3530 if (htab->glink_eh_frame == NULL
3531 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
3532 return FALSE;
3533 }
3534
3535 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3536 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3537 if (htab->elf.iplt == NULL
3538 || !bfd_set_section_alignment (htab->elf.iplt, 3))
3539 return FALSE;
3540
3541 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3542 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3543 htab->elf.irelplt
3544 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3545 if (htab->elf.irelplt == NULL
3546 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
3547 return FALSE;
3548
3549 /* Create branch lookup table for plt_branch stubs. */
3550 flags = (SEC_ALLOC | SEC_LOAD
3551 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3552 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3553 flags);
3554 if (htab->brlt == NULL
3555 || !bfd_set_section_alignment (htab->brlt, 3))
3556 return FALSE;
3557
3558 /* Local plt entries, put in .branch_lt but a separate section for
3559 convenience. */
3560 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3561 flags);
3562 if (htab->pltlocal == NULL
3563 || !bfd_set_section_alignment (htab->pltlocal, 3))
3564 return FALSE;
3565
3566 if (!bfd_link_pic (info))
3567 return TRUE;
3568
3569 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3570 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3571 htab->relbrlt
3572 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3573 if (htab->relbrlt == NULL
3574 || !bfd_set_section_alignment (htab->relbrlt, 3))
3575 return FALSE;
3576
3577 htab->relpltlocal
3578 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3579 if (htab->relpltlocal == NULL
3580 || !bfd_set_section_alignment (htab->relpltlocal, 3))
3581 return FALSE;
3582
3583 return TRUE;
3584 }
3585
3586 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3587
3588 bfd_boolean
3589 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3590 struct ppc64_elf_params *params)
3591 {
3592 struct ppc_link_hash_table *htab;
3593
3594 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3595
3596 /* Always hook our dynamic sections into the first bfd, which is the
3597 linker created stub bfd. This ensures that the GOT header is at
3598 the start of the output TOC section. */
3599 htab = ppc_hash_table (info);
3600 htab->elf.dynobj = params->stub_bfd;
3601 htab->params = params;
3602
3603 return create_linkage_sections (htab->elf.dynobj, info);
3604 }
3605
3606 /* Build a name for an entry in the stub hash table. */
3607
3608 static char *
3609 ppc_stub_name (const asection *input_section,
3610 const asection *sym_sec,
3611 const struct ppc_link_hash_entry *h,
3612 const Elf_Internal_Rela *rel)
3613 {
3614 char *stub_name;
3615 ssize_t len;
3616
3617 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3618 offsets from a sym as a branch target? In fact, we could
3619 probably assume the addend is always zero. */
3620 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3621
3622 if (h)
3623 {
3624 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3625 stub_name = bfd_malloc (len);
3626 if (stub_name == NULL)
3627 return stub_name;
3628
3629 len = sprintf (stub_name, "%08x.%s+%x",
3630 input_section->id & 0xffffffff,
3631 h->elf.root.root.string,
3632 (int) rel->r_addend & 0xffffffff);
3633 }
3634 else
3635 {
3636 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3637 stub_name = bfd_malloc (len);
3638 if (stub_name == NULL)
3639 return stub_name;
3640
3641 len = sprintf (stub_name, "%08x.%x:%x+%x",
3642 input_section->id & 0xffffffff,
3643 sym_sec->id & 0xffffffff,
3644 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3645 (int) rel->r_addend & 0xffffffff);
3646 }
3647 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3648 stub_name[len - 2] = 0;
3649 return stub_name;
3650 }
3651
3652 /* Look up an entry in the stub hash. Stub entries are cached because
3653 creating the stub name takes a bit of time. */
3654
3655 static struct ppc_stub_hash_entry *
3656 ppc_get_stub_entry (const asection *input_section,
3657 const asection *sym_sec,
3658 struct ppc_link_hash_entry *h,
3659 const Elf_Internal_Rela *rel,
3660 struct ppc_link_hash_table *htab)
3661 {
3662 struct ppc_stub_hash_entry *stub_entry;
3663 struct map_stub *group;
3664
3665 /* If this input section is part of a group of sections sharing one
3666 stub section, then use the id of the first section in the group.
3667 Stub names need to include a section id, as there may well be
3668 more than one stub used to reach say, printf, and we need to
3669 distinguish between them. */
3670 group = htab->sec_info[input_section->id].u.group;
3671 if (group == NULL)
3672 return NULL;
3673
3674 if (h != NULL && h->u.stub_cache != NULL
3675 && h->u.stub_cache->h == h
3676 && h->u.stub_cache->group == group)
3677 {
3678 stub_entry = h->u.stub_cache;
3679 }
3680 else
3681 {
3682 char *stub_name;
3683
3684 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3685 if (stub_name == NULL)
3686 return NULL;
3687
3688 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3689 stub_name, FALSE, FALSE);
3690 if (h != NULL)
3691 h->u.stub_cache = stub_entry;
3692
3693 free (stub_name);
3694 }
3695
3696 return stub_entry;
3697 }
3698
3699 /* Add a new stub entry to the stub hash. Not all fields of the new
3700 stub entry are initialised. */
3701
3702 static struct ppc_stub_hash_entry *
3703 ppc_add_stub (const char *stub_name,
3704 asection *section,
3705 struct bfd_link_info *info)
3706 {
3707 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3708 struct map_stub *group;
3709 asection *link_sec;
3710 asection *stub_sec;
3711 struct ppc_stub_hash_entry *stub_entry;
3712
3713 group = htab->sec_info[section->id].u.group;
3714 link_sec = group->link_sec;
3715 stub_sec = group->stub_sec;
3716 if (stub_sec == NULL)
3717 {
3718 size_t namelen;
3719 bfd_size_type len;
3720 char *s_name;
3721
3722 namelen = strlen (link_sec->name);
3723 len = namelen + sizeof (STUB_SUFFIX);
3724 s_name = bfd_alloc (htab->params->stub_bfd, len);
3725 if (s_name == NULL)
3726 return NULL;
3727
3728 memcpy (s_name, link_sec->name, namelen);
3729 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3730 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3731 if (stub_sec == NULL)
3732 return NULL;
3733 group->stub_sec = stub_sec;
3734 }
3735
3736 /* Enter this entry into the linker stub hash table. */
3737 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3738 TRUE, FALSE);
3739 if (stub_entry == NULL)
3740 {
3741 /* xgettext:c-format */
3742 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3743 section->owner, stub_name);
3744 return NULL;
3745 }
3746
3747 stub_entry->group = group;
3748 stub_entry->stub_offset = 0;
3749 return stub_entry;
3750 }
3751
3752 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3753 not already done. */
3754
3755 static bfd_boolean
3756 create_got_section (bfd *abfd, struct bfd_link_info *info)
3757 {
3758 asection *got, *relgot;
3759 flagword flags;
3760 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3761
3762 if (!is_ppc64_elf (abfd))
3763 return FALSE;
3764 if (htab == NULL)
3765 return FALSE;
3766
3767 if (!htab->elf.sgot
3768 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3769 return FALSE;
3770
3771 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3772 | SEC_LINKER_CREATED);
3773
3774 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3775 if (!got
3776 || !bfd_set_section_alignment (got, 3))
3777 return FALSE;
3778
3779 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3780 flags | SEC_READONLY);
3781 if (!relgot
3782 || !bfd_set_section_alignment (relgot, 3))
3783 return FALSE;
3784
3785 ppc64_elf_tdata (abfd)->got = got;
3786 ppc64_elf_tdata (abfd)->relgot = relgot;
3787 return TRUE;
3788 }
3789
3790 /* Follow indirect and warning symbol links. */
3791
3792 static inline struct bfd_link_hash_entry *
3793 follow_link (struct bfd_link_hash_entry *h)
3794 {
3795 while (h->type == bfd_link_hash_indirect
3796 || h->type == bfd_link_hash_warning)
3797 h = h->u.i.link;
3798 return h;
3799 }
3800
3801 static inline struct elf_link_hash_entry *
3802 elf_follow_link (struct elf_link_hash_entry *h)
3803 {
3804 return (struct elf_link_hash_entry *) follow_link (&h->root);
3805 }
3806
3807 static inline struct ppc_link_hash_entry *
3808 ppc_follow_link (struct ppc_link_hash_entry *h)
3809 {
3810 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3811 }
3812
3813 /* Merge PLT info on FROM with that on TO. */
3814
3815 static void
3816 move_plt_plist (struct ppc_link_hash_entry *from,
3817 struct ppc_link_hash_entry *to)
3818 {
3819 if (from->elf.plt.plist != NULL)
3820 {
3821 if (to->elf.plt.plist != NULL)
3822 {
3823 struct plt_entry **entp;
3824 struct plt_entry *ent;
3825
3826 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3827 {
3828 struct plt_entry *dent;
3829
3830 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3831 if (dent->addend == ent->addend)
3832 {
3833 dent->plt.refcount += ent->plt.refcount;
3834 *entp = ent->next;
3835 break;
3836 }
3837 if (dent == NULL)
3838 entp = &ent->next;
3839 }
3840 *entp = to->elf.plt.plist;
3841 }
3842
3843 to->elf.plt.plist = from->elf.plt.plist;
3844 from->elf.plt.plist = NULL;
3845 }
3846 }
3847
3848 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3849
3850 static void
3851 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3852 struct elf_link_hash_entry *dir,
3853 struct elf_link_hash_entry *ind)
3854 {
3855 struct ppc_link_hash_entry *edir, *eind;
3856
3857 edir = (struct ppc_link_hash_entry *) dir;
3858 eind = (struct ppc_link_hash_entry *) ind;
3859
3860 edir->is_func |= eind->is_func;
3861 edir->is_func_descriptor |= eind->is_func_descriptor;
3862 edir->tls_mask |= eind->tls_mask;
3863 if (eind->oh != NULL)
3864 edir->oh = ppc_follow_link (eind->oh);
3865
3866 if (edir->elf.versioned != versioned_hidden)
3867 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3868 edir->elf.ref_regular |= eind->elf.ref_regular;
3869 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3870 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3871 edir->elf.needs_plt |= eind->elf.needs_plt;
3872 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3873
3874 /* If we were called to copy over info for a weak sym, don't copy
3875 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3876 in order to simplify readonly_dynrelocs and save a field in the
3877 symbol hash entry, but that means dyn_relocs can't be used in any
3878 tests about a specific symbol, or affect other symbol flags which
3879 are then tested. */
3880 if (eind->elf.root.type != bfd_link_hash_indirect)
3881 return;
3882
3883 /* Copy over any dynamic relocs we may have on the indirect sym. */
3884 if (eind->dyn_relocs != NULL)
3885 {
3886 if (edir->dyn_relocs != NULL)
3887 {
3888 struct elf_dyn_relocs **pp;
3889 struct elf_dyn_relocs *p;
3890
3891 /* Add reloc counts against the indirect sym to the direct sym
3892 list. Merge any entries against the same section. */
3893 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3894 {
3895 struct elf_dyn_relocs *q;
3896
3897 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3898 if (q->sec == p->sec)
3899 {
3900 q->pc_count += p->pc_count;
3901 q->count += p->count;
3902 *pp = p->next;
3903 break;
3904 }
3905 if (q == NULL)
3906 pp = &p->next;
3907 }
3908 *pp = edir->dyn_relocs;
3909 }
3910
3911 edir->dyn_relocs = eind->dyn_relocs;
3912 eind->dyn_relocs = NULL;
3913 }
3914
3915 /* Copy over got entries that we may have already seen to the
3916 symbol which just became indirect. */
3917 if (eind->elf.got.glist != NULL)
3918 {
3919 if (edir->elf.got.glist != NULL)
3920 {
3921 struct got_entry **entp;
3922 struct got_entry *ent;
3923
3924 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3925 {
3926 struct got_entry *dent;
3927
3928 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3929 if (dent->addend == ent->addend
3930 && dent->owner == ent->owner
3931 && dent->tls_type == ent->tls_type)
3932 {
3933 dent->got.refcount += ent->got.refcount;
3934 *entp = ent->next;
3935 break;
3936 }
3937 if (dent == NULL)
3938 entp = &ent->next;
3939 }
3940 *entp = edir->elf.got.glist;
3941 }
3942
3943 edir->elf.got.glist = eind->elf.got.glist;
3944 eind->elf.got.glist = NULL;
3945 }
3946
3947 /* And plt entries. */
3948 move_plt_plist (eind, edir);
3949
3950 if (eind->elf.dynindx != -1)
3951 {
3952 if (edir->elf.dynindx != -1)
3953 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3954 edir->elf.dynstr_index);
3955 edir->elf.dynindx = eind->elf.dynindx;
3956 edir->elf.dynstr_index = eind->elf.dynstr_index;
3957 eind->elf.dynindx = -1;
3958 eind->elf.dynstr_index = 0;
3959 }
3960 }
3961
3962 /* Find the function descriptor hash entry from the given function code
3963 hash entry FH. Link the entries via their OH fields. */
3964
3965 static struct ppc_link_hash_entry *
3966 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3967 {
3968 struct ppc_link_hash_entry *fdh = fh->oh;
3969
3970 if (fdh == NULL)
3971 {
3972 const char *fd_name = fh->elf.root.root.string + 1;
3973
3974 fdh = (struct ppc_link_hash_entry *)
3975 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3976 if (fdh == NULL)
3977 return fdh;
3978
3979 fdh->is_func_descriptor = 1;
3980 fdh->oh = fh;
3981 fh->is_func = 1;
3982 fh->oh = fdh;
3983 }
3984
3985 fdh = ppc_follow_link (fdh);
3986 fdh->is_func_descriptor = 1;
3987 fdh->oh = fh;
3988 return fdh;
3989 }
3990
3991 /* Make a fake function descriptor sym for the undefined code sym FH. */
3992
3993 static struct ppc_link_hash_entry *
3994 make_fdh (struct bfd_link_info *info,
3995 struct ppc_link_hash_entry *fh)
3996 {
3997 bfd *abfd = fh->elf.root.u.undef.abfd;
3998 struct bfd_link_hash_entry *bh = NULL;
3999 struct ppc_link_hash_entry *fdh;
4000 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4001 ? BSF_WEAK
4002 : BSF_GLOBAL);
4003
4004 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4005 fh->elf.root.root.string + 1,
4006 flags, bfd_und_section_ptr, 0,
4007 NULL, FALSE, FALSE, &bh))
4008 return NULL;
4009
4010 fdh = (struct ppc_link_hash_entry *) bh;
4011 fdh->elf.non_elf = 0;
4012 fdh->fake = 1;
4013 fdh->is_func_descriptor = 1;
4014 fdh->oh = fh;
4015 fh->is_func = 1;
4016 fh->oh = fdh;
4017 return fdh;
4018 }
4019
4020 /* Fix function descriptor symbols defined in .opd sections to be
4021 function type. */
4022
4023 static bfd_boolean
4024 ppc64_elf_add_symbol_hook (bfd *ibfd,
4025 struct bfd_link_info *info,
4026 Elf_Internal_Sym *isym,
4027 const char **name,
4028 flagword *flags ATTRIBUTE_UNUSED,
4029 asection **sec,
4030 bfd_vma *value)
4031 {
4032 if (*sec != NULL
4033 && strcmp ((*sec)->name, ".opd") == 0)
4034 {
4035 asection *code_sec;
4036
4037 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4038 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4039 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4040
4041 /* If the symbol is a function defined in .opd, and the function
4042 code is in a discarded group, let it appear to be undefined. */
4043 if (!bfd_link_relocatable (info)
4044 && (*sec)->reloc_count != 0
4045 && opd_entry_value (*sec, *value, &code_sec, NULL,
4046 FALSE) != (bfd_vma) -1
4047 && discarded_section (code_sec))
4048 {
4049 *sec = bfd_und_section_ptr;
4050 isym->st_shndx = SHN_UNDEF;
4051 }
4052 }
4053 else if (*sec != NULL
4054 && strcmp ((*sec)->name, ".toc") == 0
4055 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4056 {
4057 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4058 if (htab != NULL)
4059 htab->params->object_in_toc = 1;
4060 }
4061
4062 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4063 {
4064 if (abiversion (ibfd) == 0)
4065 set_abiversion (ibfd, 2);
4066 else if (abiversion (ibfd) == 1)
4067 {
4068 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4069 " for ABI version 1"), *name);
4070 bfd_set_error (bfd_error_bad_value);
4071 return FALSE;
4072 }
4073 }
4074
4075 return TRUE;
4076 }
4077
4078 /* Merge non-visibility st_other attributes: local entry point. */
4079
4080 static void
4081 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4082 const Elf_Internal_Sym *isym,
4083 bfd_boolean definition,
4084 bfd_boolean dynamic)
4085 {
4086 if (definition && (!dynamic || !h->def_regular))
4087 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4088 | ELF_ST_VISIBILITY (h->other));
4089 }
4090
4091 /* Hook called on merging a symbol. We use this to clear "fake" since
4092 we now have a real symbol. */
4093
4094 static bfd_boolean
4095 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4096 const Elf_Internal_Sym *isym,
4097 asection **psec ATTRIBUTE_UNUSED,
4098 bfd_boolean newdef ATTRIBUTE_UNUSED,
4099 bfd_boolean olddef ATTRIBUTE_UNUSED,
4100 bfd *oldbfd ATTRIBUTE_UNUSED,
4101 const asection *oldsec ATTRIBUTE_UNUSED)
4102 {
4103 ((struct ppc_link_hash_entry *) h)->fake = 0;
4104 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4105 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
4106 return TRUE;
4107 }
4108
4109 /* This function makes an old ABI object reference to ".bar" cause the
4110 inclusion of a new ABI object archive that defines "bar".
4111 NAME is a symbol defined in an archive. Return a symbol in the hash
4112 table that might be satisfied by the archive symbols. */
4113
4114 static struct elf_link_hash_entry *
4115 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4116 struct bfd_link_info *info,
4117 const char *name)
4118 {
4119 struct elf_link_hash_entry *h;
4120 char *dot_name;
4121 size_t len;
4122
4123 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4124 if (h != NULL
4125 /* Don't return this sym if it is a fake function descriptor
4126 created by add_symbol_adjust. */
4127 && !((struct ppc_link_hash_entry *) h)->fake)
4128 return h;
4129
4130 if (name[0] == '.')
4131 return h;
4132
4133 len = strlen (name);
4134 dot_name = bfd_alloc (abfd, len + 2);
4135 if (dot_name == NULL)
4136 return (struct elf_link_hash_entry *) -1;
4137 dot_name[0] = '.';
4138 memcpy (dot_name + 1, name, len + 1);
4139 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4140 bfd_release (abfd, dot_name);
4141 return h;
4142 }
4143
4144 /* This function satisfies all old ABI object references to ".bar" if a
4145 new ABI object defines "bar". Well, at least, undefined dot symbols
4146 are made weak. This stops later archive searches from including an
4147 object if we already have a function descriptor definition. It also
4148 prevents the linker complaining about undefined symbols.
4149 We also check and correct mismatched symbol visibility here. The
4150 most restrictive visibility of the function descriptor and the
4151 function entry symbol is used. */
4152
4153 static bfd_boolean
4154 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4155 {
4156 struct ppc_link_hash_table *htab;
4157 struct ppc_link_hash_entry *fdh;
4158
4159 if (eh->elf.root.type == bfd_link_hash_warning)
4160 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4161
4162 if (eh->elf.root.type == bfd_link_hash_indirect)
4163 return TRUE;
4164
4165 if (eh->elf.root.root.string[0] != '.')
4166 abort ();
4167
4168 htab = ppc_hash_table (info);
4169 if (htab == NULL)
4170 return FALSE;
4171
4172 fdh = lookup_fdh (eh, htab);
4173 if (fdh == NULL
4174 && !bfd_link_relocatable (info)
4175 && (eh->elf.root.type == bfd_link_hash_undefined
4176 || eh->elf.root.type == bfd_link_hash_undefweak)
4177 && eh->elf.ref_regular)
4178 {
4179 /* Make an undefined function descriptor sym, in order to
4180 pull in an --as-needed shared lib. Archives are handled
4181 elsewhere. */
4182 fdh = make_fdh (info, eh);
4183 if (fdh == NULL)
4184 return FALSE;
4185 }
4186
4187 if (fdh != NULL)
4188 {
4189 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4190 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4191
4192 /* Make both descriptor and entry symbol have the most
4193 constraining visibility of either symbol. */
4194 if (entry_vis < descr_vis)
4195 fdh->elf.other += entry_vis - descr_vis;
4196 else if (entry_vis > descr_vis)
4197 eh->elf.other += descr_vis - entry_vis;
4198
4199 /* Propagate reference flags from entry symbol to function
4200 descriptor symbol. */
4201 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4202 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4203 fdh->elf.ref_regular |= eh->elf.ref_regular;
4204 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4205
4206 if (!fdh->elf.forced_local
4207 && fdh->elf.dynindx == -1
4208 && fdh->elf.versioned != versioned_hidden
4209 && (bfd_link_dll (info)
4210 || fdh->elf.def_dynamic
4211 || fdh->elf.ref_dynamic)
4212 && (eh->elf.ref_regular
4213 || eh->elf.def_regular))
4214 {
4215 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4216 return FALSE;
4217 }
4218 }
4219
4220 return TRUE;
4221 }
4222
4223 /* Set up opd section info and abiversion for IBFD, and process list
4224 of dot-symbols we made in link_hash_newfunc. */
4225
4226 static bfd_boolean
4227 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4228 {
4229 struct ppc_link_hash_table *htab;
4230 struct ppc_link_hash_entry **p, *eh;
4231 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4232
4233 if (opd != NULL && opd->size != 0)
4234 {
4235 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4236 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4237
4238 if (abiversion (ibfd) == 0)
4239 set_abiversion (ibfd, 1);
4240 else if (abiversion (ibfd) >= 2)
4241 {
4242 /* xgettext:c-format */
4243 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4244 ibfd, abiversion (ibfd));
4245 bfd_set_error (bfd_error_bad_value);
4246 return FALSE;
4247 }
4248 }
4249
4250 if (is_ppc64_elf (info->output_bfd))
4251 {
4252 /* For input files without an explicit abiversion in e_flags
4253 we should have flagged any with symbol st_other bits set
4254 as ELFv1 and above flagged those with .opd as ELFv2.
4255 Set the output abiversion if not yet set, and for any input
4256 still ambiguous, take its abiversion from the output.
4257 Differences in ABI are reported later. */
4258 if (abiversion (info->output_bfd) == 0)
4259 set_abiversion (info->output_bfd, abiversion (ibfd));
4260 else if (abiversion (ibfd) == 0)
4261 set_abiversion (ibfd, abiversion (info->output_bfd));
4262 }
4263
4264 htab = ppc_hash_table (info);
4265 if (htab == NULL)
4266 return TRUE;
4267
4268 if (opd != NULL && opd->size != 0
4269 && (ibfd->flags & DYNAMIC) == 0
4270 && (opd->flags & SEC_RELOC) != 0
4271 && opd->reloc_count != 0
4272 && !bfd_is_abs_section (opd->output_section)
4273 && info->gc_sections)
4274 {
4275 /* Garbage collection needs some extra help with .opd sections.
4276 We don't want to necessarily keep everything referenced by
4277 relocs in .opd, as that would keep all functions. Instead,
4278 if we reference an .opd symbol (a function descriptor), we
4279 want to keep the function code symbol's section. This is
4280 easy for global symbols, but for local syms we need to keep
4281 information about the associated function section. */
4282 bfd_size_type amt;
4283 asection **opd_sym_map;
4284 Elf_Internal_Shdr *symtab_hdr;
4285 Elf_Internal_Rela *relocs, *rel_end, *rel;
4286
4287 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4288 opd_sym_map = bfd_zalloc (ibfd, amt);
4289 if (opd_sym_map == NULL)
4290 return FALSE;
4291 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4292 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4293 info->keep_memory);
4294 if (relocs == NULL)
4295 return FALSE;
4296 symtab_hdr = &elf_symtab_hdr (ibfd);
4297 rel_end = relocs + opd->reloc_count - 1;
4298 for (rel = relocs; rel < rel_end; rel++)
4299 {
4300 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4301 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4302
4303 if (r_type == R_PPC64_ADDR64
4304 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4305 && r_symndx < symtab_hdr->sh_info)
4306 {
4307 Elf_Internal_Sym *isym;
4308 asection *s;
4309
4310 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4311 if (isym == NULL)
4312 {
4313 if (elf_section_data (opd)->relocs != relocs)
4314 free (relocs);
4315 return FALSE;
4316 }
4317
4318 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4319 if (s != NULL && s != opd)
4320 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4321 }
4322 }
4323 if (elf_section_data (opd)->relocs != relocs)
4324 free (relocs);
4325 }
4326
4327 p = &htab->dot_syms;
4328 while ((eh = *p) != NULL)
4329 {
4330 *p = NULL;
4331 if (&eh->elf == htab->elf.hgot)
4332 ;
4333 else if (htab->elf.hgot == NULL
4334 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4335 htab->elf.hgot = &eh->elf;
4336 else if (abiversion (ibfd) <= 1)
4337 {
4338 htab->need_func_desc_adj = 1;
4339 if (!add_symbol_adjust (eh, info))
4340 return FALSE;
4341 }
4342 p = &eh->u.next_dot_sym;
4343 }
4344 return TRUE;
4345 }
4346
4347 /* Undo hash table changes when an --as-needed input file is determined
4348 not to be needed. */
4349
4350 static bfd_boolean
4351 ppc64_elf_notice_as_needed (bfd *ibfd,
4352 struct bfd_link_info *info,
4353 enum notice_asneeded_action act)
4354 {
4355 if (act == notice_not_needed)
4356 {
4357 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4358
4359 if (htab == NULL)
4360 return FALSE;
4361
4362 htab->dot_syms = NULL;
4363 }
4364 return _bfd_elf_notice_as_needed (ibfd, info, act);
4365 }
4366
4367 /* If --just-symbols against a final linked binary, then assume we need
4368 toc adjusting stubs when calling functions defined there. */
4369
4370 static void
4371 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4372 {
4373 if ((sec->flags & SEC_CODE) != 0
4374 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4375 && is_ppc64_elf (sec->owner))
4376 {
4377 if (abiversion (sec->owner) >= 2
4378 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4379 sec->has_toc_reloc = 1;
4380 }
4381 _bfd_elf_link_just_syms (sec, info);
4382 }
4383
4384 static struct plt_entry **
4385 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4386 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4387 {
4388 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4389 struct plt_entry **local_plt;
4390 unsigned char *local_got_tls_masks;
4391
4392 if (local_got_ents == NULL)
4393 {
4394 bfd_size_type size = symtab_hdr->sh_info;
4395
4396 size *= (sizeof (*local_got_ents)
4397 + sizeof (*local_plt)
4398 + sizeof (*local_got_tls_masks));
4399 local_got_ents = bfd_zalloc (abfd, size);
4400 if (local_got_ents == NULL)
4401 return NULL;
4402 elf_local_got_ents (abfd) = local_got_ents;
4403 }
4404
4405 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4406 {
4407 struct got_entry *ent;
4408
4409 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4410 if (ent->addend == r_addend
4411 && ent->owner == abfd
4412 && ent->tls_type == tls_type)
4413 break;
4414 if (ent == NULL)
4415 {
4416 bfd_size_type amt = sizeof (*ent);
4417 ent = bfd_alloc (abfd, amt);
4418 if (ent == NULL)
4419 return FALSE;
4420 ent->next = local_got_ents[r_symndx];
4421 ent->addend = r_addend;
4422 ent->owner = abfd;
4423 ent->tls_type = tls_type;
4424 ent->is_indirect = FALSE;
4425 ent->got.refcount = 0;
4426 local_got_ents[r_symndx] = ent;
4427 }
4428 ent->got.refcount += 1;
4429 }
4430
4431 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4432 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4433 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4434
4435 return local_plt + r_symndx;
4436 }
4437
4438 static bfd_boolean
4439 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4440 {
4441 struct plt_entry *ent;
4442
4443 for (ent = *plist; ent != NULL; ent = ent->next)
4444 if (ent->addend == addend)
4445 break;
4446 if (ent == NULL)
4447 {
4448 bfd_size_type amt = sizeof (*ent);
4449 ent = bfd_alloc (abfd, amt);
4450 if (ent == NULL)
4451 return FALSE;
4452 ent->next = *plist;
4453 ent->addend = addend;
4454 ent->plt.refcount = 0;
4455 *plist = ent;
4456 }
4457 ent->plt.refcount += 1;
4458 return TRUE;
4459 }
4460
4461 static bfd_boolean
4462 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4463 {
4464 return (r_type == R_PPC64_REL24
4465 || r_type == R_PPC64_REL24_NOTOC
4466 || r_type == R_PPC64_REL14
4467 || r_type == R_PPC64_REL14_BRTAKEN
4468 || r_type == R_PPC64_REL14_BRNTAKEN
4469 || r_type == R_PPC64_ADDR24
4470 || r_type == R_PPC64_ADDR14
4471 || r_type == R_PPC64_ADDR14_BRTAKEN
4472 || r_type == R_PPC64_ADDR14_BRNTAKEN
4473 || r_type == R_PPC64_PLTCALL
4474 || r_type == R_PPC64_PLTCALL_NOTOC);
4475 }
4476
4477 /* Relocs on inline plt call sequence insns prior to the call. */
4478
4479 static bfd_boolean
4480 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4481 {
4482 return (r_type == R_PPC64_PLT16_HA
4483 || r_type == R_PPC64_PLT16_HI
4484 || r_type == R_PPC64_PLT16_LO
4485 || r_type == R_PPC64_PLT16_LO_DS
4486 || r_type == R_PPC64_PLT_PCREL34
4487 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4488 || r_type == R_PPC64_PLTSEQ
4489 || r_type == R_PPC64_PLTSEQ_NOTOC);
4490 }
4491
4492 /* Look through the relocs for a section during the first phase, and
4493 calculate needed space in the global offset table, procedure
4494 linkage table, and dynamic reloc sections. */
4495
4496 static bfd_boolean
4497 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4498 asection *sec, const Elf_Internal_Rela *relocs)
4499 {
4500 struct ppc_link_hash_table *htab;
4501 Elf_Internal_Shdr *symtab_hdr;
4502 struct elf_link_hash_entry **sym_hashes;
4503 const Elf_Internal_Rela *rel;
4504 const Elf_Internal_Rela *rel_end;
4505 asection *sreloc;
4506 struct elf_link_hash_entry *tga, *dottga;
4507 bfd_boolean is_opd;
4508
4509 if (bfd_link_relocatable (info))
4510 return TRUE;
4511
4512 /* Don't do anything special with non-loaded, non-alloced sections.
4513 In particular, any relocs in such sections should not affect GOT
4514 and PLT reference counting (ie. we don't allow them to create GOT
4515 or PLT entries), there's no possibility or desire to optimize TLS
4516 relocs, and there's not much point in propagating relocs to shared
4517 libs that the dynamic linker won't relocate. */
4518 if ((sec->flags & SEC_ALLOC) == 0)
4519 return TRUE;
4520
4521 BFD_ASSERT (is_ppc64_elf (abfd));
4522
4523 htab = ppc_hash_table (info);
4524 if (htab == NULL)
4525 return FALSE;
4526
4527 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4528 FALSE, FALSE, TRUE);
4529 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4530 FALSE, FALSE, TRUE);
4531 symtab_hdr = &elf_symtab_hdr (abfd);
4532 sym_hashes = elf_sym_hashes (abfd);
4533 sreloc = NULL;
4534 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4535 rel_end = relocs + sec->reloc_count;
4536 for (rel = relocs; rel < rel_end; rel++)
4537 {
4538 unsigned long r_symndx;
4539 struct elf_link_hash_entry *h;
4540 enum elf_ppc64_reloc_type r_type;
4541 int tls_type;
4542 struct _ppc64_elf_section_data *ppc64_sec;
4543 struct plt_entry **ifunc, **plt_list;
4544
4545 r_symndx = ELF64_R_SYM (rel->r_info);
4546 if (r_symndx < symtab_hdr->sh_info)
4547 h = NULL;
4548 else
4549 {
4550 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4551 h = elf_follow_link (h);
4552
4553 if (h == htab->elf.hgot)
4554 sec->has_toc_reloc = 1;
4555 }
4556
4557 r_type = ELF64_R_TYPE (rel->r_info);
4558 switch (r_type)
4559 {
4560 case R_PPC64_D34:
4561 case R_PPC64_D34_LO:
4562 case R_PPC64_D34_HI30:
4563 case R_PPC64_D34_HA30:
4564 case R_PPC64_D28:
4565 case R_PPC64_TPREL34:
4566 case R_PPC64_DTPREL34:
4567 case R_PPC64_PCREL34:
4568 case R_PPC64_GOT_PCREL34:
4569 case R_PPC64_GOT_TLSGD34:
4570 case R_PPC64_GOT_TLSLD34:
4571 case R_PPC64_GOT_TPREL34:
4572 case R_PPC64_GOT_DTPREL34:
4573 case R_PPC64_PLT_PCREL34:
4574 case R_PPC64_PLT_PCREL34_NOTOC:
4575 case R_PPC64_PCREL28:
4576 htab->powerxx_stubs = 1;
4577 break;
4578 default:
4579 break;
4580 }
4581
4582 switch (r_type)
4583 {
4584 case R_PPC64_PLT16_HA:
4585 case R_PPC64_GOT_TLSLD16_HA:
4586 case R_PPC64_GOT_TLSGD16_HA:
4587 case R_PPC64_GOT_TPREL16_HA:
4588 case R_PPC64_GOT_DTPREL16_HA:
4589 case R_PPC64_GOT16_HA:
4590 case R_PPC64_TOC16_HA:
4591 case R_PPC64_PLT16_LO:
4592 case R_PPC64_PLT16_LO_DS:
4593 case R_PPC64_GOT_TLSLD16_LO:
4594 case R_PPC64_GOT_TLSGD16_LO:
4595 case R_PPC64_GOT_TPREL16_LO_DS:
4596 case R_PPC64_GOT_DTPREL16_LO_DS:
4597 case R_PPC64_GOT16_LO:
4598 case R_PPC64_GOT16_LO_DS:
4599 case R_PPC64_TOC16_LO:
4600 case R_PPC64_TOC16_LO_DS:
4601 case R_PPC64_GOT_PCREL34:
4602 ppc64_elf_tdata (abfd)->has_optrel = 1;
4603 ppc64_elf_section_data (sec)->has_optrel = 1;
4604 break;
4605 default:
4606 break;
4607 }
4608
4609 ifunc = NULL;
4610 if (h != NULL)
4611 {
4612 if (h->type == STT_GNU_IFUNC)
4613 {
4614 h->needs_plt = 1;
4615 ifunc = &h->plt.plist;
4616 }
4617 }
4618 else
4619 {
4620 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4621 abfd, r_symndx);
4622 if (isym == NULL)
4623 return FALSE;
4624
4625 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4626 {
4627 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4628 rel->r_addend,
4629 NON_GOT | PLT_IFUNC);
4630 if (ifunc == NULL)
4631 return FALSE;
4632 }
4633 }
4634
4635 tls_type = 0;
4636 switch (r_type)
4637 {
4638 case R_PPC64_TLSGD:
4639 case R_PPC64_TLSLD:
4640 /* These special tls relocs tie a call to __tls_get_addr with
4641 its parameter symbol. */
4642 if (h != NULL)
4643 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4644 else
4645 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4646 rel->r_addend,
4647 NON_GOT | TLS_TLS | TLS_MARK))
4648 return FALSE;
4649 sec->has_tls_reloc = 1;
4650 break;
4651
4652 case R_PPC64_GOT_TLSLD16:
4653 case R_PPC64_GOT_TLSLD16_LO:
4654 case R_PPC64_GOT_TLSLD16_HI:
4655 case R_PPC64_GOT_TLSLD16_HA:
4656 case R_PPC64_GOT_TLSLD34:
4657 tls_type = TLS_TLS | TLS_LD;
4658 goto dogottls;
4659
4660 case R_PPC64_GOT_TLSGD16:
4661 case R_PPC64_GOT_TLSGD16_LO:
4662 case R_PPC64_GOT_TLSGD16_HI:
4663 case R_PPC64_GOT_TLSGD16_HA:
4664 case R_PPC64_GOT_TLSGD34:
4665 tls_type = TLS_TLS | TLS_GD;
4666 goto dogottls;
4667
4668 case R_PPC64_GOT_TPREL16_DS:
4669 case R_PPC64_GOT_TPREL16_LO_DS:
4670 case R_PPC64_GOT_TPREL16_HI:
4671 case R_PPC64_GOT_TPREL16_HA:
4672 case R_PPC64_GOT_TPREL34:
4673 if (bfd_link_dll (info))
4674 info->flags |= DF_STATIC_TLS;
4675 tls_type = TLS_TLS | TLS_TPREL;
4676 goto dogottls;
4677
4678 case R_PPC64_GOT_DTPREL16_DS:
4679 case R_PPC64_GOT_DTPREL16_LO_DS:
4680 case R_PPC64_GOT_DTPREL16_HI:
4681 case R_PPC64_GOT_DTPREL16_HA:
4682 case R_PPC64_GOT_DTPREL34:
4683 tls_type = TLS_TLS | TLS_DTPREL;
4684 dogottls:
4685 sec->has_tls_reloc = 1;
4686 goto dogot;
4687
4688 case R_PPC64_GOT16:
4689 case R_PPC64_GOT16_LO:
4690 case R_PPC64_GOT16_HI:
4691 case R_PPC64_GOT16_HA:
4692 case R_PPC64_GOT16_DS:
4693 case R_PPC64_GOT16_LO_DS:
4694 case R_PPC64_GOT_PCREL34:
4695 dogot:
4696 /* This symbol requires a global offset table entry. */
4697 sec->has_toc_reloc = 1;
4698 if (r_type == R_PPC64_GOT_TLSLD16
4699 || r_type == R_PPC64_GOT_TLSGD16
4700 || r_type == R_PPC64_GOT_TPREL16_DS
4701 || r_type == R_PPC64_GOT_DTPREL16_DS
4702 || r_type == R_PPC64_GOT16
4703 || r_type == R_PPC64_GOT16_DS)
4704 {
4705 htab->do_multi_toc = 1;
4706 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4707 }
4708
4709 if (ppc64_elf_tdata (abfd)->got == NULL
4710 && !create_got_section (abfd, info))
4711 return FALSE;
4712
4713 if (h != NULL)
4714 {
4715 struct ppc_link_hash_entry *eh;
4716 struct got_entry *ent;
4717
4718 eh = (struct ppc_link_hash_entry *) h;
4719 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4720 if (ent->addend == rel->r_addend
4721 && ent->owner == abfd
4722 && ent->tls_type == tls_type)
4723 break;
4724 if (ent == NULL)
4725 {
4726 bfd_size_type amt = sizeof (*ent);
4727 ent = bfd_alloc (abfd, amt);
4728 if (ent == NULL)
4729 return FALSE;
4730 ent->next = eh->elf.got.glist;
4731 ent->addend = rel->r_addend;
4732 ent->owner = abfd;
4733 ent->tls_type = tls_type;
4734 ent->is_indirect = FALSE;
4735 ent->got.refcount = 0;
4736 eh->elf.got.glist = ent;
4737 }
4738 ent->got.refcount += 1;
4739 eh->tls_mask |= tls_type;
4740 }
4741 else
4742 /* This is a global offset table entry for a local symbol. */
4743 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4744 rel->r_addend, tls_type))
4745 return FALSE;
4746
4747 /* We may also need a plt entry if the symbol turns out to be
4748 an ifunc. */
4749 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
4750 {
4751 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4752 return FALSE;
4753 }
4754 break;
4755
4756 case R_PPC64_PLT16_HA:
4757 case R_PPC64_PLT16_HI:
4758 case R_PPC64_PLT16_LO:
4759 case R_PPC64_PLT16_LO_DS:
4760 case R_PPC64_PLT_PCREL34:
4761 case R_PPC64_PLT_PCREL34_NOTOC:
4762 case R_PPC64_PLT32:
4763 case R_PPC64_PLT64:
4764 /* This symbol requires a procedure linkage table entry. */
4765 plt_list = ifunc;
4766 if (h != NULL)
4767 {
4768 h->needs_plt = 1;
4769 if (h->root.root.string[0] == '.'
4770 && h->root.root.string[1] != '\0')
4771 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4772 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
4773 plt_list = &h->plt.plist;
4774 }
4775 if (plt_list == NULL)
4776 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4777 rel->r_addend,
4778 NON_GOT | PLT_KEEP);
4779 if (!update_plt_info (abfd, plt_list, rel->r_addend))
4780 return FALSE;
4781 break;
4782
4783 /* The following relocations don't need to propagate the
4784 relocation if linking a shared object since they are
4785 section relative. */
4786 case R_PPC64_SECTOFF:
4787 case R_PPC64_SECTOFF_LO:
4788 case R_PPC64_SECTOFF_HI:
4789 case R_PPC64_SECTOFF_HA:
4790 case R_PPC64_SECTOFF_DS:
4791 case R_PPC64_SECTOFF_LO_DS:
4792 case R_PPC64_DTPREL16:
4793 case R_PPC64_DTPREL16_LO:
4794 case R_PPC64_DTPREL16_HI:
4795 case R_PPC64_DTPREL16_HA:
4796 case R_PPC64_DTPREL16_DS:
4797 case R_PPC64_DTPREL16_LO_DS:
4798 case R_PPC64_DTPREL16_HIGH:
4799 case R_PPC64_DTPREL16_HIGHA:
4800 case R_PPC64_DTPREL16_HIGHER:
4801 case R_PPC64_DTPREL16_HIGHERA:
4802 case R_PPC64_DTPREL16_HIGHEST:
4803 case R_PPC64_DTPREL16_HIGHESTA:
4804 break;
4805
4806 /* Nor do these. */
4807 case R_PPC64_REL16:
4808 case R_PPC64_REL16_LO:
4809 case R_PPC64_REL16_HI:
4810 case R_PPC64_REL16_HA:
4811 case R_PPC64_REL16_HIGH:
4812 case R_PPC64_REL16_HIGHA:
4813 case R_PPC64_REL16_HIGHER:
4814 case R_PPC64_REL16_HIGHERA:
4815 case R_PPC64_REL16_HIGHEST:
4816 case R_PPC64_REL16_HIGHESTA:
4817 case R_PPC64_REL16_HIGHER34:
4818 case R_PPC64_REL16_HIGHERA34:
4819 case R_PPC64_REL16_HIGHEST34:
4820 case R_PPC64_REL16_HIGHESTA34:
4821 case R_PPC64_REL16DX_HA:
4822 break;
4823
4824 /* Not supported as a dynamic relocation. */
4825 case R_PPC64_ADDR64_LOCAL:
4826 if (bfd_link_pic (info))
4827 {
4828 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4829 ppc_howto_init ();
4830 /* xgettext:c-format */
4831 info->callbacks->einfo (_("%H: %s reloc unsupported "
4832 "in shared libraries and PIEs\n"),
4833 abfd, sec, rel->r_offset,
4834 ppc64_elf_howto_table[r_type]->name);
4835 bfd_set_error (bfd_error_bad_value);
4836 return FALSE;
4837 }
4838 break;
4839
4840 case R_PPC64_TOC16:
4841 case R_PPC64_TOC16_DS:
4842 htab->do_multi_toc = 1;
4843 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4844 /* Fall through. */
4845 case R_PPC64_TOC16_LO:
4846 case R_PPC64_TOC16_HI:
4847 case R_PPC64_TOC16_HA:
4848 case R_PPC64_TOC16_LO_DS:
4849 sec->has_toc_reloc = 1;
4850 if (h != NULL && bfd_link_executable (info))
4851 {
4852 /* We may need a copy reloc. */
4853 h->non_got_ref = 1;
4854 /* Strongly prefer a copy reloc over a dynamic reloc.
4855 glibc ld.so as of 2019-08 will error out if one of
4856 these relocations is emitted. */
4857 h->needs_copy = 1;
4858 goto dodyn;
4859 }
4860 break;
4861
4862 /* Marker reloc. */
4863 case R_PPC64_ENTRY:
4864 break;
4865
4866 /* This relocation describes the C++ object vtable hierarchy.
4867 Reconstruct it for later use during GC. */
4868 case R_PPC64_GNU_VTINHERIT:
4869 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4870 return FALSE;
4871 break;
4872
4873 /* This relocation describes which C++ vtable entries are actually
4874 used. Record for later use during GC. */
4875 case R_PPC64_GNU_VTENTRY:
4876 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4877 return FALSE;
4878 break;
4879
4880 case R_PPC64_REL14:
4881 case R_PPC64_REL14_BRTAKEN:
4882 case R_PPC64_REL14_BRNTAKEN:
4883 {
4884 asection *dest = NULL;
4885
4886 /* Heuristic: If jumping outside our section, chances are
4887 we are going to need a stub. */
4888 if (h != NULL)
4889 {
4890 /* If the sym is weak it may be overridden later, so
4891 don't assume we know where a weak sym lives. */
4892 if (h->root.type == bfd_link_hash_defined)
4893 dest = h->root.u.def.section;
4894 }
4895 else
4896 {
4897 Elf_Internal_Sym *isym;
4898
4899 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4900 abfd, r_symndx);
4901 if (isym == NULL)
4902 return FALSE;
4903
4904 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4905 }
4906
4907 if (dest != sec)
4908 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4909 }
4910 goto rel24;
4911
4912 case R_PPC64_PLTCALL:
4913 case R_PPC64_PLTCALL_NOTOC:
4914 ppc64_elf_section_data (sec)->has_pltcall = 1;
4915 /* Fall through. */
4916
4917 case R_PPC64_REL24:
4918 case R_PPC64_REL24_NOTOC:
4919 rel24:
4920 plt_list = ifunc;
4921 if (h != NULL)
4922 {
4923 h->needs_plt = 1;
4924 if (h->root.root.string[0] == '.'
4925 && h->root.root.string[1] != '\0')
4926 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4927
4928 if (h == tga || h == dottga)
4929 {
4930 sec->has_tls_reloc = 1;
4931 if (rel != relocs
4932 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4933 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4934 /* We have a new-style __tls_get_addr call with
4935 a marker reloc. */
4936 ;
4937 else
4938 /* Mark this section as having an old-style call. */
4939 sec->nomark_tls_get_addr = 1;
4940 }
4941 plt_list = &h->plt.plist;
4942 }
4943
4944 /* We may need a .plt entry if the function this reloc
4945 refers to is in a shared lib. */
4946 if (plt_list
4947 && !update_plt_info (abfd, plt_list, rel->r_addend))
4948 return FALSE;
4949 break;
4950
4951 case R_PPC64_ADDR14:
4952 case R_PPC64_ADDR14_BRNTAKEN:
4953 case R_PPC64_ADDR14_BRTAKEN:
4954 case R_PPC64_ADDR24:
4955 goto dodyn;
4956
4957 case R_PPC64_TPREL64:
4958 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4959 if (bfd_link_dll (info))
4960 info->flags |= DF_STATIC_TLS;
4961 goto dotlstoc;
4962
4963 case R_PPC64_DTPMOD64:
4964 if (rel + 1 < rel_end
4965 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4966 && rel[1].r_offset == rel->r_offset + 8)
4967 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4968 else
4969 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4970 goto dotlstoc;
4971
4972 case R_PPC64_DTPREL64:
4973 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4974 if (rel != relocs
4975 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4976 && rel[-1].r_offset == rel->r_offset - 8)
4977 /* This is the second reloc of a dtpmod, dtprel pair.
4978 Don't mark with TLS_DTPREL. */
4979 goto dodyn;
4980
4981 dotlstoc:
4982 sec->has_tls_reloc = 1;
4983 if (h != NULL)
4984 {
4985 struct ppc_link_hash_entry *eh;
4986 eh = (struct ppc_link_hash_entry *) h;
4987 eh->tls_mask |= tls_type & 0xff;
4988 }
4989 else
4990 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4991 rel->r_addend, tls_type))
4992 return FALSE;
4993
4994 ppc64_sec = ppc64_elf_section_data (sec);
4995 if (ppc64_sec->sec_type != sec_toc)
4996 {
4997 bfd_size_type amt;
4998
4999 /* One extra to simplify get_tls_mask. */
5000 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5001 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5002 if (ppc64_sec->u.toc.symndx == NULL)
5003 return FALSE;
5004 amt = sec->size * sizeof (bfd_vma) / 8;
5005 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5006 if (ppc64_sec->u.toc.add == NULL)
5007 return FALSE;
5008 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5009 ppc64_sec->sec_type = sec_toc;
5010 }
5011 BFD_ASSERT (rel->r_offset % 8 == 0);
5012 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5013 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5014
5015 /* Mark the second slot of a GD or LD entry.
5016 -1 to indicate GD and -2 to indicate LD. */
5017 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5018 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5019 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5020 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5021 goto dodyn;
5022
5023 case R_PPC64_TPREL16:
5024 case R_PPC64_TPREL16_LO:
5025 case R_PPC64_TPREL16_HI:
5026 case R_PPC64_TPREL16_HA:
5027 case R_PPC64_TPREL16_DS:
5028 case R_PPC64_TPREL16_LO_DS:
5029 case R_PPC64_TPREL16_HIGH:
5030 case R_PPC64_TPREL16_HIGHA:
5031 case R_PPC64_TPREL16_HIGHER:
5032 case R_PPC64_TPREL16_HIGHERA:
5033 case R_PPC64_TPREL16_HIGHEST:
5034 case R_PPC64_TPREL16_HIGHESTA:
5035 case R_PPC64_TPREL34:
5036 if (bfd_link_dll (info))
5037 info->flags |= DF_STATIC_TLS;
5038 goto dodyn;
5039
5040 case R_PPC64_ADDR64:
5041 if (is_opd
5042 && rel + 1 < rel_end
5043 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5044 {
5045 if (h != NULL)
5046 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5047 }
5048 /* Fall through. */
5049
5050 case R_PPC64_ADDR16:
5051 case R_PPC64_ADDR16_DS:
5052 case R_PPC64_ADDR16_HA:
5053 case R_PPC64_ADDR16_HI:
5054 case R_PPC64_ADDR16_HIGH:
5055 case R_PPC64_ADDR16_HIGHA:
5056 case R_PPC64_ADDR16_HIGHER:
5057 case R_PPC64_ADDR16_HIGHERA:
5058 case R_PPC64_ADDR16_HIGHEST:
5059 case R_PPC64_ADDR16_HIGHESTA:
5060 case R_PPC64_ADDR16_LO:
5061 case R_PPC64_ADDR16_LO_DS:
5062 case R_PPC64_D34:
5063 case R_PPC64_D34_LO:
5064 case R_PPC64_D34_HI30:
5065 case R_PPC64_D34_HA30:
5066 case R_PPC64_ADDR16_HIGHER34:
5067 case R_PPC64_ADDR16_HIGHERA34:
5068 case R_PPC64_ADDR16_HIGHEST34:
5069 case R_PPC64_ADDR16_HIGHESTA34:
5070 case R_PPC64_D28:
5071 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5072 && rel->r_addend == 0)
5073 {
5074 /* We may need a .plt entry if this reloc refers to a
5075 function in a shared lib. */
5076 if (!update_plt_info (abfd, &h->plt.plist, 0))
5077 return FALSE;
5078 h->pointer_equality_needed = 1;
5079 }
5080 /* Fall through. */
5081
5082 case R_PPC64_REL30:
5083 case R_PPC64_REL32:
5084 case R_PPC64_REL64:
5085 case R_PPC64_ADDR32:
5086 case R_PPC64_UADDR16:
5087 case R_PPC64_UADDR32:
5088 case R_PPC64_UADDR64:
5089 case R_PPC64_TOC:
5090 if (h != NULL && bfd_link_executable (info))
5091 /* We may need a copy reloc. */
5092 h->non_got_ref = 1;
5093
5094 /* Don't propagate .opd relocs. */
5095 if (NO_OPD_RELOCS && is_opd)
5096 break;
5097
5098 /* If we are creating a shared library, and this is a reloc
5099 against a global symbol, or a non PC relative reloc
5100 against a local symbol, then we need to copy the reloc
5101 into the shared library. However, if we are linking with
5102 -Bsymbolic, we do not need to copy a reloc against a
5103 global symbol which is defined in an object we are
5104 including in the link (i.e., DEF_REGULAR is set). At
5105 this point we have not seen all the input files, so it is
5106 possible that DEF_REGULAR is not set now but will be set
5107 later (it is never cleared). In case of a weak definition,
5108 DEF_REGULAR may be cleared later by a strong definition in
5109 a shared library. We account for that possibility below by
5110 storing information in the dyn_relocs field of the hash
5111 table entry. A similar situation occurs when creating
5112 shared libraries and symbol visibility changes render the
5113 symbol local.
5114
5115 If on the other hand, we are creating an executable, we
5116 may need to keep relocations for symbols satisfied by a
5117 dynamic library if we manage to avoid copy relocs for the
5118 symbol. */
5119 dodyn:
5120 if ((h != NULL
5121 && (h->root.type == bfd_link_hash_defweak
5122 || !h->def_regular))
5123 || (h != NULL
5124 && !bfd_link_executable (info)
5125 && !SYMBOLIC_BIND (info, h))
5126 || (bfd_link_pic (info)
5127 && must_be_dyn_reloc (info, r_type))
5128 || (!bfd_link_pic (info)
5129 && ifunc != NULL))
5130 {
5131 /* We must copy these reloc types into the output file.
5132 Create a reloc section in dynobj and make room for
5133 this reloc. */
5134 if (sreloc == NULL)
5135 {
5136 sreloc = _bfd_elf_make_dynamic_reloc_section
5137 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5138
5139 if (sreloc == NULL)
5140 return FALSE;
5141 }
5142
5143 /* If this is a global symbol, we count the number of
5144 relocations we need for this symbol. */
5145 if (h != NULL)
5146 {
5147 struct elf_dyn_relocs *p;
5148 struct elf_dyn_relocs **head;
5149
5150 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5151 p = *head;
5152 if (p == NULL || p->sec != sec)
5153 {
5154 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5155 if (p == NULL)
5156 return FALSE;
5157 p->next = *head;
5158 *head = p;
5159 p->sec = sec;
5160 p->count = 0;
5161 p->pc_count = 0;
5162 }
5163 p->count += 1;
5164 if (!must_be_dyn_reloc (info, r_type))
5165 p->pc_count += 1;
5166 }
5167 else
5168 {
5169 /* Track dynamic relocs needed for local syms too.
5170 We really need local syms available to do this
5171 easily. Oh well. */
5172 struct ppc_dyn_relocs *p;
5173 struct ppc_dyn_relocs **head;
5174 bfd_boolean is_ifunc;
5175 asection *s;
5176 void *vpp;
5177 Elf_Internal_Sym *isym;
5178
5179 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5180 abfd, r_symndx);
5181 if (isym == NULL)
5182 return FALSE;
5183
5184 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5185 if (s == NULL)
5186 s = sec;
5187
5188 vpp = &elf_section_data (s)->local_dynrel;
5189 head = (struct ppc_dyn_relocs **) vpp;
5190 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5191 p = *head;
5192 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5193 p = p->next;
5194 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5195 {
5196 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5197 if (p == NULL)
5198 return FALSE;
5199 p->next = *head;
5200 *head = p;
5201 p->sec = sec;
5202 p->ifunc = is_ifunc;
5203 p->count = 0;
5204 }
5205 p->count += 1;
5206 }
5207 }
5208 break;
5209
5210 default:
5211 break;
5212 }
5213 }
5214
5215 return TRUE;
5216 }
5217
5218 /* Merge backend specific data from an object file to the output
5219 object file when linking. */
5220
5221 static bfd_boolean
5222 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5223 {
5224 bfd *obfd = info->output_bfd;
5225 unsigned long iflags, oflags;
5226
5227 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5228 return TRUE;
5229
5230 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5231 return TRUE;
5232
5233 if (!_bfd_generic_verify_endian_match (ibfd, info))
5234 return FALSE;
5235
5236 iflags = elf_elfheader (ibfd)->e_flags;
5237 oflags = elf_elfheader (obfd)->e_flags;
5238
5239 if (iflags & ~EF_PPC64_ABI)
5240 {
5241 _bfd_error_handler
5242 /* xgettext:c-format */
5243 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5244 bfd_set_error (bfd_error_bad_value);
5245 return FALSE;
5246 }
5247 else if (iflags != oflags && iflags != 0)
5248 {
5249 _bfd_error_handler
5250 /* xgettext:c-format */
5251 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5252 ibfd, iflags, oflags);
5253 bfd_set_error (bfd_error_bad_value);
5254 return FALSE;
5255 }
5256
5257 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5258 return FALSE;
5259
5260 /* Merge Tag_compatibility attributes and any common GNU ones. */
5261 return _bfd_elf_merge_object_attributes (ibfd, info);
5262 }
5263
5264 static bfd_boolean
5265 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5266 {
5267 /* Print normal ELF private data. */
5268 _bfd_elf_print_private_bfd_data (abfd, ptr);
5269
5270 if (elf_elfheader (abfd)->e_flags != 0)
5271 {
5272 FILE *file = ptr;
5273
5274 fprintf (file, _("private flags = 0x%lx:"),
5275 elf_elfheader (abfd)->e_flags);
5276
5277 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5278 fprintf (file, _(" [abiv%ld]"),
5279 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5280 fputc ('\n', file);
5281 }
5282
5283 return TRUE;
5284 }
5285
5286 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5287 of the code entry point, and its section, which must be in the same
5288 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5289
5290 static bfd_vma
5291 opd_entry_value (asection *opd_sec,
5292 bfd_vma offset,
5293 asection **code_sec,
5294 bfd_vma *code_off,
5295 bfd_boolean in_code_sec)
5296 {
5297 bfd *opd_bfd = opd_sec->owner;
5298 Elf_Internal_Rela *relocs;
5299 Elf_Internal_Rela *lo, *hi, *look;
5300 bfd_vma val;
5301
5302 /* No relocs implies we are linking a --just-symbols object, or looking
5303 at a final linked executable with addr2line or somesuch. */
5304 if (opd_sec->reloc_count == 0)
5305 {
5306 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5307
5308 if (contents == NULL)
5309 {
5310 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5311 return (bfd_vma) -1;
5312 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5313 }
5314
5315 /* PR 17512: file: 64b9dfbb. */
5316 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5317 return (bfd_vma) -1;
5318
5319 val = bfd_get_64 (opd_bfd, contents + offset);
5320 if (code_sec != NULL)
5321 {
5322 asection *sec, *likely = NULL;
5323
5324 if (in_code_sec)
5325 {
5326 sec = *code_sec;
5327 if (sec->vma <= val
5328 && val < sec->vma + sec->size)
5329 likely = sec;
5330 else
5331 val = -1;
5332 }
5333 else
5334 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5335 if (sec->vma <= val
5336 && (sec->flags & SEC_LOAD) != 0
5337 && (sec->flags & SEC_ALLOC) != 0)
5338 likely = sec;
5339 if (likely != NULL)
5340 {
5341 *code_sec = likely;
5342 if (code_off != NULL)
5343 *code_off = val - likely->vma;
5344 }
5345 }
5346 return val;
5347 }
5348
5349 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5350
5351 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5352 if (relocs == NULL)
5353 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5354 /* PR 17512: file: df8e1fd6. */
5355 if (relocs == NULL)
5356 return (bfd_vma) -1;
5357
5358 /* Go find the opd reloc at the sym address. */
5359 lo = relocs;
5360 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5361 val = (bfd_vma) -1;
5362 while (lo < hi)
5363 {
5364 look = lo + (hi - lo) / 2;
5365 if (look->r_offset < offset)
5366 lo = look + 1;
5367 else if (look->r_offset > offset)
5368 hi = look;
5369 else
5370 {
5371 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5372
5373 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5374 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5375 {
5376 unsigned long symndx = ELF64_R_SYM (look->r_info);
5377 asection *sec = NULL;
5378
5379 if (symndx >= symtab_hdr->sh_info
5380 && elf_sym_hashes (opd_bfd) != NULL)
5381 {
5382 struct elf_link_hash_entry **sym_hashes;
5383 struct elf_link_hash_entry *rh;
5384
5385 sym_hashes = elf_sym_hashes (opd_bfd);
5386 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5387 if (rh != NULL)
5388 {
5389 rh = elf_follow_link (rh);
5390 if (rh->root.type != bfd_link_hash_defined
5391 && rh->root.type != bfd_link_hash_defweak)
5392 break;
5393 if (rh->root.u.def.section->owner == opd_bfd)
5394 {
5395 val = rh->root.u.def.value;
5396 sec = rh->root.u.def.section;
5397 }
5398 }
5399 }
5400
5401 if (sec == NULL)
5402 {
5403 Elf_Internal_Sym *sym;
5404
5405 if (symndx < symtab_hdr->sh_info)
5406 {
5407 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5408 if (sym == NULL)
5409 {
5410 size_t symcnt = symtab_hdr->sh_info;
5411 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5412 symcnt, 0,
5413 NULL, NULL, NULL);
5414 if (sym == NULL)
5415 break;
5416 symtab_hdr->contents = (bfd_byte *) sym;
5417 }
5418 sym += symndx;
5419 }
5420 else
5421 {
5422 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5423 1, symndx,
5424 NULL, NULL, NULL);
5425 if (sym == NULL)
5426 break;
5427 }
5428 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5429 if (sec == NULL)
5430 break;
5431 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5432 val = sym->st_value;
5433 }
5434
5435 val += look->r_addend;
5436 if (code_off != NULL)
5437 *code_off = val;
5438 if (code_sec != NULL)
5439 {
5440 if (in_code_sec && *code_sec != sec)
5441 return -1;
5442 else
5443 *code_sec = sec;
5444 }
5445 if (sec->output_section != NULL)
5446 val += sec->output_section->vma + sec->output_offset;
5447 }
5448 break;
5449 }
5450 }
5451
5452 return val;
5453 }
5454
5455 /* If the ELF symbol SYM might be a function in SEC, return the
5456 function size and set *CODE_OFF to the function's entry point,
5457 otherwise return zero. */
5458
5459 static bfd_size_type
5460 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5461 bfd_vma *code_off)
5462 {
5463 bfd_size_type size;
5464
5465 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5466 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5467 return 0;
5468
5469 size = 0;
5470 if (!(sym->flags & BSF_SYNTHETIC))
5471 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5472
5473 if (strcmp (sym->section->name, ".opd") == 0)
5474 {
5475 struct _opd_sec_data *opd = get_opd_info (sym->section);
5476 bfd_vma symval = sym->value;
5477
5478 if (opd != NULL
5479 && opd->adjust != NULL
5480 && elf_section_data (sym->section)->relocs != NULL)
5481 {
5482 /* opd_entry_value will use cached relocs that have been
5483 adjusted, but with raw symbols. That means both local
5484 and global symbols need adjusting. */
5485 long adjust = opd->adjust[OPD_NDX (symval)];
5486 if (adjust == -1)
5487 return 0;
5488 symval += adjust;
5489 }
5490
5491 if (opd_entry_value (sym->section, symval,
5492 &sec, code_off, TRUE) == (bfd_vma) -1)
5493 return 0;
5494 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5495 symbol. This size has nothing to do with the code size of the
5496 function, which is what we're supposed to return, but the
5497 code size isn't available without looking up the dot-sym.
5498 However, doing that would be a waste of time particularly
5499 since elf_find_function will look at the dot-sym anyway.
5500 Now, elf_find_function will keep the largest size of any
5501 function sym found at the code address of interest, so return
5502 1 here to avoid it incorrectly caching a larger function size
5503 for a small function. This does mean we return the wrong
5504 size for a new-ABI function of size 24, but all that does is
5505 disable caching for such functions. */
5506 if (size == 24)
5507 size = 1;
5508 }
5509 else
5510 {
5511 if (sym->section != sec)
5512 return 0;
5513 *code_off = sym->value;
5514 }
5515 if (size == 0)
5516 size = 1;
5517 return size;
5518 }
5519
5520 /* Return true if symbol is a strong function defined in an ELFv2
5521 object with st_other localentry bits of zero, ie. its local entry
5522 point coincides with its global entry point. */
5523
5524 static bfd_boolean
5525 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5526 {
5527 return (h != NULL
5528 && h->type == STT_FUNC
5529 && h->root.type == bfd_link_hash_defined
5530 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5531 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
5532 && is_ppc64_elf (h->root.u.def.section->owner)
5533 && abiversion (h->root.u.def.section->owner) >= 2);
5534 }
5535
5536 /* Return true if symbol is defined in a regular object file. */
5537
5538 static bfd_boolean
5539 is_static_defined (struct elf_link_hash_entry *h)
5540 {
5541 return ((h->root.type == bfd_link_hash_defined
5542 || h->root.type == bfd_link_hash_defweak)
5543 && h->root.u.def.section != NULL
5544 && h->root.u.def.section->output_section != NULL);
5545 }
5546
5547 /* If FDH is a function descriptor symbol, return the associated code
5548 entry symbol if it is defined. Return NULL otherwise. */
5549
5550 static struct ppc_link_hash_entry *
5551 defined_code_entry (struct ppc_link_hash_entry *fdh)
5552 {
5553 if (fdh->is_func_descriptor)
5554 {
5555 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5556 if (fh->elf.root.type == bfd_link_hash_defined
5557 || fh->elf.root.type == bfd_link_hash_defweak)
5558 return fh;
5559 }
5560 return NULL;
5561 }
5562
5563 /* If FH is a function code entry symbol, return the associated
5564 function descriptor symbol if it is defined. Return NULL otherwise. */
5565
5566 static struct ppc_link_hash_entry *
5567 defined_func_desc (struct ppc_link_hash_entry *fh)
5568 {
5569 if (fh->oh != NULL
5570 && fh->oh->is_func_descriptor)
5571 {
5572 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5573 if (fdh->elf.root.type == bfd_link_hash_defined
5574 || fdh->elf.root.type == bfd_link_hash_defweak)
5575 return fdh;
5576 }
5577 return NULL;
5578 }
5579
5580 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5581
5582 /* Garbage collect sections, after first dealing with dot-symbols. */
5583
5584 static bfd_boolean
5585 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5586 {
5587 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5588
5589 if (htab != NULL && htab->need_func_desc_adj)
5590 {
5591 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5592 htab->need_func_desc_adj = 0;
5593 }
5594 return bfd_elf_gc_sections (abfd, info);
5595 }
5596
5597 /* Mark all our entry sym sections, both opd and code section. */
5598
5599 static void
5600 ppc64_elf_gc_keep (struct bfd_link_info *info)
5601 {
5602 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5603 struct bfd_sym_chain *sym;
5604
5605 if (htab == NULL)
5606 return;
5607
5608 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5609 {
5610 struct ppc_link_hash_entry *eh, *fh;
5611 asection *sec;
5612
5613 eh = (struct ppc_link_hash_entry *)
5614 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5615 if (eh == NULL)
5616 continue;
5617 if (eh->elf.root.type != bfd_link_hash_defined
5618 && eh->elf.root.type != bfd_link_hash_defweak)
5619 continue;
5620
5621 fh = defined_code_entry (eh);
5622 if (fh != NULL)
5623 {
5624 sec = fh->elf.root.u.def.section;
5625 sec->flags |= SEC_KEEP;
5626 }
5627 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5628 && opd_entry_value (eh->elf.root.u.def.section,
5629 eh->elf.root.u.def.value,
5630 &sec, NULL, FALSE) != (bfd_vma) -1)
5631 sec->flags |= SEC_KEEP;
5632
5633 sec = eh->elf.root.u.def.section;
5634 sec->flags |= SEC_KEEP;
5635 }
5636 }
5637
5638 /* Mark sections containing dynamically referenced symbols. When
5639 building shared libraries, we must assume that any visible symbol is
5640 referenced. */
5641
5642 static bfd_boolean
5643 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5644 {
5645 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5646 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5647 struct ppc_link_hash_entry *fdh;
5648 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5649
5650 /* Dynamic linking info is on the func descriptor sym. */
5651 fdh = defined_func_desc (eh);
5652 if (fdh != NULL)
5653 eh = fdh;
5654
5655 if ((eh->elf.root.type == bfd_link_hash_defined
5656 || eh->elf.root.type == bfd_link_hash_defweak)
5657 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5658 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5659 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5660 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5661 && (!bfd_link_executable (info)
5662 || info->gc_keep_exported
5663 || info->export_dynamic
5664 || (eh->elf.dynamic
5665 && d != NULL
5666 && (*d->match) (&d->head, NULL,
5667 eh->elf.root.root.string)))
5668 && (eh->elf.versioned >= versioned
5669 || !bfd_hide_sym_by_version (info->version_info,
5670 eh->elf.root.root.string)))))
5671 {
5672 asection *code_sec;
5673 struct ppc_link_hash_entry *fh;
5674
5675 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5676
5677 /* Function descriptor syms cause the associated
5678 function code sym section to be marked. */
5679 fh = defined_code_entry (eh);
5680 if (fh != NULL)
5681 {
5682 code_sec = fh->elf.root.u.def.section;
5683 code_sec->flags |= SEC_KEEP;
5684 }
5685 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5686 && opd_entry_value (eh->elf.root.u.def.section,
5687 eh->elf.root.u.def.value,
5688 &code_sec, NULL, FALSE) != (bfd_vma) -1)
5689 code_sec->flags |= SEC_KEEP;
5690 }
5691
5692 return TRUE;
5693 }
5694
5695 /* Return the section that should be marked against GC for a given
5696 relocation. */
5697
5698 static asection *
5699 ppc64_elf_gc_mark_hook (asection *sec,
5700 struct bfd_link_info *info,
5701 Elf_Internal_Rela *rel,
5702 struct elf_link_hash_entry *h,
5703 Elf_Internal_Sym *sym)
5704 {
5705 asection *rsec;
5706
5707 /* Syms return NULL if we're marking .opd, so we avoid marking all
5708 function sections, as all functions are referenced in .opd. */
5709 rsec = NULL;
5710 if (get_opd_info (sec) != NULL)
5711 return rsec;
5712
5713 if (h != NULL)
5714 {
5715 enum elf_ppc64_reloc_type r_type;
5716 struct ppc_link_hash_entry *eh, *fh, *fdh;
5717
5718 r_type = ELF64_R_TYPE (rel->r_info);
5719 switch (r_type)
5720 {
5721 case R_PPC64_GNU_VTINHERIT:
5722 case R_PPC64_GNU_VTENTRY:
5723 break;
5724
5725 default:
5726 switch (h->root.type)
5727 {
5728 case bfd_link_hash_defined:
5729 case bfd_link_hash_defweak:
5730 eh = (struct ppc_link_hash_entry *) h;
5731 fdh = defined_func_desc (eh);
5732 if (fdh != NULL)
5733 {
5734 /* -mcall-aixdesc code references the dot-symbol on
5735 a call reloc. Mark the function descriptor too
5736 against garbage collection. */
5737 fdh->elf.mark = 1;
5738 if (fdh->elf.is_weakalias)
5739 weakdef (&fdh->elf)->mark = 1;
5740 eh = fdh;
5741 }
5742
5743 /* Function descriptor syms cause the associated
5744 function code sym section to be marked. */
5745 fh = defined_code_entry (eh);
5746 if (fh != NULL)
5747 {
5748 /* They also mark their opd section. */
5749 eh->elf.root.u.def.section->gc_mark = 1;
5750
5751 rsec = fh->elf.root.u.def.section;
5752 }
5753 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5754 && opd_entry_value (eh->elf.root.u.def.section,
5755 eh->elf.root.u.def.value,
5756 &rsec, NULL, FALSE) != (bfd_vma) -1)
5757 eh->elf.root.u.def.section->gc_mark = 1;
5758 else
5759 rsec = h->root.u.def.section;
5760 break;
5761
5762 case bfd_link_hash_common:
5763 rsec = h->root.u.c.p->section;
5764 break;
5765
5766 default:
5767 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5768 }
5769 }
5770 }
5771 else
5772 {
5773 struct _opd_sec_data *opd;
5774
5775 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5776 opd = get_opd_info (rsec);
5777 if (opd != NULL && opd->func_sec != NULL)
5778 {
5779 rsec->gc_mark = 1;
5780
5781 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
5782 }
5783 }
5784
5785 return rsec;
5786 }
5787
5788 /* The maximum size of .sfpr. */
5789 #define SFPR_MAX (218*4)
5790
5791 struct sfpr_def_parms
5792 {
5793 const char name[12];
5794 unsigned char lo, hi;
5795 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5796 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
5797 };
5798
5799 /* Auto-generate _save*, _rest* functions in .sfpr.
5800 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5801 instead. */
5802
5803 static bfd_boolean
5804 sfpr_define (struct bfd_link_info *info,
5805 const struct sfpr_def_parms *parm,
5806 asection *stub_sec)
5807 {
5808 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5809 unsigned int i;
5810 size_t len = strlen (parm->name);
5811 bfd_boolean writing = FALSE;
5812 char sym[16];
5813
5814 if (htab == NULL)
5815 return FALSE;
5816
5817 memcpy (sym, parm->name, len);
5818 sym[len + 2] = 0;
5819
5820 for (i = parm->lo; i <= parm->hi; i++)
5821 {
5822 struct ppc_link_hash_entry *h;
5823
5824 sym[len + 0] = i / 10 + '0';
5825 sym[len + 1] = i % 10 + '0';
5826 h = (struct ppc_link_hash_entry *)
5827 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
5828 if (stub_sec != NULL)
5829 {
5830 if (h != NULL
5831 && h->elf.root.type == bfd_link_hash_defined
5832 && h->elf.root.u.def.section == htab->sfpr)
5833 {
5834 struct elf_link_hash_entry *s;
5835 char buf[32];
5836 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5837 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5838 if (s == NULL)
5839 return FALSE;
5840 if (s->root.type == bfd_link_hash_new)
5841 {
5842 s->root.type = bfd_link_hash_defined;
5843 s->root.u.def.section = stub_sec;
5844 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
5845 + h->elf.root.u.def.value);
5846 s->ref_regular = 1;
5847 s->def_regular = 1;
5848 s->ref_regular_nonweak = 1;
5849 s->forced_local = 1;
5850 s->non_elf = 0;
5851 s->root.linker_def = 1;
5852 }
5853 }
5854 continue;
5855 }
5856 if (h != NULL)
5857 {
5858 h->save_res = 1;
5859 if (!h->elf.def_regular)
5860 {
5861 h->elf.root.type = bfd_link_hash_defined;
5862 h->elf.root.u.def.section = htab->sfpr;
5863 h->elf.root.u.def.value = htab->sfpr->size;
5864 h->elf.type = STT_FUNC;
5865 h->elf.def_regular = 1;
5866 h->elf.non_elf = 0;
5867 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5868 writing = TRUE;
5869 if (htab->sfpr->contents == NULL)
5870 {
5871 htab->sfpr->contents
5872 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5873 if (htab->sfpr->contents == NULL)
5874 return FALSE;
5875 }
5876 }
5877 }
5878 if (writing)
5879 {
5880 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5881 if (i != parm->hi)
5882 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5883 else
5884 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5885 htab->sfpr->size = p - htab->sfpr->contents;
5886 }
5887 }
5888
5889 return TRUE;
5890 }
5891
5892 static bfd_byte *
5893 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5894 {
5895 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5896 return p + 4;
5897 }
5898
5899 static bfd_byte *
5900 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5901 {
5902 p = savegpr0 (abfd, p, r);
5903 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5904 p = p + 4;
5905 bfd_put_32 (abfd, BLR, p);
5906 return p + 4;
5907 }
5908
5909 static bfd_byte *
5910 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5911 {
5912 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5913 return p + 4;
5914 }
5915
5916 static bfd_byte *
5917 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5918 {
5919 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5920 p = p + 4;
5921 p = restgpr0 (abfd, p, r);
5922 bfd_put_32 (abfd, MTLR_R0, p);
5923 p = p + 4;
5924 if (r == 29)
5925 {
5926 p = restgpr0 (abfd, p, 30);
5927 p = restgpr0 (abfd, p, 31);
5928 }
5929 bfd_put_32 (abfd, BLR, p);
5930 return p + 4;
5931 }
5932
5933 static bfd_byte *
5934 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5935 {
5936 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5937 return p + 4;
5938 }
5939
5940 static bfd_byte *
5941 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5942 {
5943 p = savegpr1 (abfd, p, r);
5944 bfd_put_32 (abfd, BLR, p);
5945 return p + 4;
5946 }
5947
5948 static bfd_byte *
5949 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5950 {
5951 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5952 return p + 4;
5953 }
5954
5955 static bfd_byte *
5956 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5957 {
5958 p = restgpr1 (abfd, p, r);
5959 bfd_put_32 (abfd, BLR, p);
5960 return p + 4;
5961 }
5962
5963 static bfd_byte *
5964 savefpr (bfd *abfd, bfd_byte *p, int r)
5965 {
5966 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5967 return p + 4;
5968 }
5969
5970 static bfd_byte *
5971 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5972 {
5973 p = savefpr (abfd, p, r);
5974 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5975 p = p + 4;
5976 bfd_put_32 (abfd, BLR, p);
5977 return p + 4;
5978 }
5979
5980 static bfd_byte *
5981 restfpr (bfd *abfd, bfd_byte *p, int r)
5982 {
5983 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5984 return p + 4;
5985 }
5986
5987 static bfd_byte *
5988 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5989 {
5990 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5991 p = p + 4;
5992 p = restfpr (abfd, p, r);
5993 bfd_put_32 (abfd, MTLR_R0, p);
5994 p = p + 4;
5995 if (r == 29)
5996 {
5997 p = restfpr (abfd, p, 30);
5998 p = restfpr (abfd, p, 31);
5999 }
6000 bfd_put_32 (abfd, BLR, p);
6001 return p + 4;
6002 }
6003
6004 static bfd_byte *
6005 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6006 {
6007 p = savefpr (abfd, p, r);
6008 bfd_put_32 (abfd, BLR, p);
6009 return p + 4;
6010 }
6011
6012 static bfd_byte *
6013 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6014 {
6015 p = restfpr (abfd, p, r);
6016 bfd_put_32 (abfd, BLR, p);
6017 return p + 4;
6018 }
6019
6020 static bfd_byte *
6021 savevr (bfd *abfd, bfd_byte *p, int r)
6022 {
6023 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6024 p = p + 4;
6025 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6026 return p + 4;
6027 }
6028
6029 static bfd_byte *
6030 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6031 {
6032 p = savevr (abfd, p, r);
6033 bfd_put_32 (abfd, BLR, p);
6034 return p + 4;
6035 }
6036
6037 static bfd_byte *
6038 restvr (bfd *abfd, bfd_byte *p, int r)
6039 {
6040 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6041 p = p + 4;
6042 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6043 return p + 4;
6044 }
6045
6046 static bfd_byte *
6047 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6048 {
6049 p = restvr (abfd, p, r);
6050 bfd_put_32 (abfd, BLR, p);
6051 return p + 4;
6052 }
6053
6054 /* Called via elf_link_hash_traverse to transfer dynamic linking
6055 information on function code symbol entries to their corresponding
6056 function descriptor symbol entries. */
6057
6058 static bfd_boolean
6059 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6060 {
6061 struct bfd_link_info *info;
6062 struct ppc_link_hash_table *htab;
6063 struct ppc_link_hash_entry *fh;
6064 struct ppc_link_hash_entry *fdh;
6065 bfd_boolean force_local;
6066
6067 fh = (struct ppc_link_hash_entry *) h;
6068 if (fh->elf.root.type == bfd_link_hash_indirect)
6069 return TRUE;
6070
6071 if (!fh->is_func)
6072 return TRUE;
6073
6074 if (fh->elf.root.root.string[0] != '.'
6075 || fh->elf.root.root.string[1] == '\0')
6076 return TRUE;
6077
6078 info = inf;
6079 htab = ppc_hash_table (info);
6080 if (htab == NULL)
6081 return FALSE;
6082
6083 /* Find the corresponding function descriptor symbol. */
6084 fdh = lookup_fdh (fh, htab);
6085
6086 /* Resolve undefined references to dot-symbols as the value
6087 in the function descriptor, if we have one in a regular object.
6088 This is to satisfy cases like ".quad .foo". Calls to functions
6089 in dynamic objects are handled elsewhere. */
6090 if ((fh->elf.root.type == bfd_link_hash_undefined
6091 || fh->elf.root.type == bfd_link_hash_undefweak)
6092 && (fdh->elf.root.type == bfd_link_hash_defined
6093 || fdh->elf.root.type == bfd_link_hash_defweak)
6094 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6095 && opd_entry_value (fdh->elf.root.u.def.section,
6096 fdh->elf.root.u.def.value,
6097 &fh->elf.root.u.def.section,
6098 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6099 {
6100 fh->elf.root.type = fdh->elf.root.type;
6101 fh->elf.forced_local = 1;
6102 fh->elf.def_regular = fdh->elf.def_regular;
6103 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6104 }
6105
6106 if (!fh->elf.dynamic)
6107 {
6108 struct plt_entry *ent;
6109
6110 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6111 if (ent->plt.refcount > 0)
6112 break;
6113 if (ent == NULL)
6114 return TRUE;
6115 }
6116
6117 /* Create a descriptor as undefined if necessary. */
6118 if (fdh == NULL
6119 && !bfd_link_executable (info)
6120 && (fh->elf.root.type == bfd_link_hash_undefined
6121 || fh->elf.root.type == bfd_link_hash_undefweak))
6122 {
6123 fdh = make_fdh (info, fh);
6124 if (fdh == NULL)
6125 return FALSE;
6126 }
6127
6128 /* We can't support overriding of symbols on a fake descriptor. */
6129 if (fdh != NULL
6130 && fdh->fake
6131 && (fh->elf.root.type == bfd_link_hash_defined
6132 || fh->elf.root.type == bfd_link_hash_defweak))
6133 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6134
6135 /* Transfer dynamic linking information to the function descriptor. */
6136 if (fdh != NULL)
6137 {
6138 fdh->elf.ref_regular |= fh->elf.ref_regular;
6139 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6140 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6141 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6142 fdh->elf.dynamic |= fh->elf.dynamic;
6143 fdh->elf.needs_plt |= (fh->elf.needs_plt
6144 || fh->elf.type == STT_FUNC
6145 || fh->elf.type == STT_GNU_IFUNC);
6146 move_plt_plist (fh, fdh);
6147
6148 if (!fdh->elf.forced_local
6149 && fh->elf.dynindx != -1)
6150 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6151 return FALSE;
6152 }
6153
6154 /* Now that the info is on the function descriptor, clear the
6155 function code sym info. Any function code syms for which we
6156 don't have a definition in a regular file, we force local.
6157 This prevents a shared library from exporting syms that have
6158 been imported from another library. Function code syms that
6159 are really in the library we must leave global to prevent the
6160 linker dragging in a definition from a static library. */
6161 force_local = (!fh->elf.def_regular
6162 || fdh == NULL
6163 || !fdh->elf.def_regular
6164 || fdh->elf.forced_local);
6165 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6166
6167 return TRUE;
6168 }
6169
6170 static const struct sfpr_def_parms save_res_funcs[] =
6171 {
6172 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6173 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6174 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6175 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6176 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6177 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6178 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6179 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6180 { "._savef", 14, 31, savefpr, savefpr1_tail },
6181 { "._restf", 14, 31, restfpr, restfpr1_tail },
6182 { "_savevr_", 20, 31, savevr, savevr_tail },
6183 { "_restvr_", 20, 31, restvr, restvr_tail }
6184 };
6185
6186 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6187 this hook to a) provide some gcc support functions, and b) transfer
6188 dynamic linking information gathered so far on function code symbol
6189 entries, to their corresponding function descriptor symbol entries. */
6190
6191 static bfd_boolean
6192 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6193 struct bfd_link_info *info)
6194 {
6195 struct ppc_link_hash_table *htab;
6196
6197 htab = ppc_hash_table (info);
6198 if (htab == NULL)
6199 return FALSE;
6200
6201 /* Provide any missing _save* and _rest* functions. */
6202 if (htab->sfpr != NULL)
6203 {
6204 unsigned int i;
6205
6206 htab->sfpr->size = 0;
6207 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6208 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6209 return FALSE;
6210 if (htab->sfpr->size == 0)
6211 htab->sfpr->flags |= SEC_EXCLUDE;
6212 }
6213
6214 if (bfd_link_relocatable (info))
6215 return TRUE;
6216
6217 if (htab->elf.hgot != NULL)
6218 {
6219 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6220 /* Make .TOC. defined so as to prevent it being made dynamic.
6221 The wrong value here is fixed later in ppc64_elf_set_toc. */
6222 if (!htab->elf.hgot->def_regular
6223 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6224 {
6225 htab->elf.hgot->root.type = bfd_link_hash_defined;
6226 htab->elf.hgot->root.u.def.value = 0;
6227 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6228 htab->elf.hgot->def_regular = 1;
6229 htab->elf.hgot->root.linker_def = 1;
6230 }
6231 htab->elf.hgot->type = STT_OBJECT;
6232 htab->elf.hgot->other
6233 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6234 }
6235
6236 if (htab->need_func_desc_adj)
6237 {
6238 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6239 htab->need_func_desc_adj = 0;
6240 }
6241
6242 return TRUE;
6243 }
6244
6245 /* Find dynamic relocs for H that apply to read-only sections. */
6246
6247 static asection *
6248 readonly_dynrelocs (struct elf_link_hash_entry *h)
6249 {
6250 struct ppc_link_hash_entry *eh;
6251 struct elf_dyn_relocs *p;
6252
6253 eh = (struct ppc_link_hash_entry *) h;
6254 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6255 {
6256 asection *s = p->sec->output_section;
6257
6258 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6259 return p->sec;
6260 }
6261 return NULL;
6262 }
6263
6264 /* Return true if we have dynamic relocs against H or any of its weak
6265 aliases, that apply to read-only sections. Cannot be used after
6266 size_dynamic_sections. */
6267
6268 static bfd_boolean
6269 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6270 {
6271 struct ppc_link_hash_entry *eh;
6272
6273 eh = (struct ppc_link_hash_entry *) h;
6274 do
6275 {
6276 if (readonly_dynrelocs (&eh->elf))
6277 return TRUE;
6278 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
6279 }
6280 while (eh != NULL && &eh->elf != h);
6281
6282 return FALSE;
6283 }
6284
6285 /* Return whether EH has pc-relative dynamic relocs. */
6286
6287 static bfd_boolean
6288 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6289 {
6290 struct elf_dyn_relocs *p;
6291
6292 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6293 if (p->pc_count != 0)
6294 return TRUE;
6295 return FALSE;
6296 }
6297
6298 /* Return true if a global entry stub will be created for H. Valid
6299 for ELFv2 before plt entries have been allocated. */
6300
6301 static bfd_boolean
6302 global_entry_stub (struct elf_link_hash_entry *h)
6303 {
6304 struct plt_entry *pent;
6305
6306 if (!h->pointer_equality_needed
6307 || h->def_regular)
6308 return FALSE;
6309
6310 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6311 if (pent->plt.refcount > 0
6312 && pent->addend == 0)
6313 return TRUE;
6314
6315 return FALSE;
6316 }
6317
6318 /* Adjust a symbol defined by a dynamic object and referenced by a
6319 regular object. The current definition is in some section of the
6320 dynamic object, but we're not including those sections. We have to
6321 change the definition to something the rest of the link can
6322 understand. */
6323
6324 static bfd_boolean
6325 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6326 struct elf_link_hash_entry *h)
6327 {
6328 struct ppc_link_hash_table *htab;
6329 asection *s, *srel;
6330
6331 htab = ppc_hash_table (info);
6332 if (htab == NULL)
6333 return FALSE;
6334
6335 /* Deal with function syms. */
6336 if (h->type == STT_FUNC
6337 || h->type == STT_GNU_IFUNC
6338 || h->needs_plt)
6339 {
6340 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6341 || SYMBOL_CALLS_LOCAL (info, h)
6342 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6343 /* Discard dyn_relocs when non-pic if we've decided that a
6344 function symbol is local and not an ifunc. We keep dynamic
6345 relocs for ifuncs when local rather than always emitting a
6346 plt call stub for them and defining the symbol on the call
6347 stub. We can't do that for ELFv1 anyway (a function symbol
6348 is defined on a descriptor, not code) and it can be faster at
6349 run-time due to not needing to bounce through a stub. The
6350 dyn_relocs for ifuncs will be applied even in a static
6351 executable. */
6352 if (!bfd_link_pic (info)
6353 && h->type != STT_GNU_IFUNC
6354 && local)
6355 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6356
6357 /* Clear procedure linkage table information for any symbol that
6358 won't need a .plt entry. */
6359 struct plt_entry *ent;
6360 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6361 if (ent->plt.refcount > 0)
6362 break;
6363 if (ent == NULL
6364 || (h->type != STT_GNU_IFUNC
6365 && local
6366 && (htab->can_convert_all_inline_plt
6367 || (((struct ppc_link_hash_entry *) h)->tls_mask
6368 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6369 {
6370 h->plt.plist = NULL;
6371 h->needs_plt = 0;
6372 h->pointer_equality_needed = 0;
6373 }
6374 else if (abiversion (info->output_bfd) >= 2)
6375 {
6376 /* Taking a function's address in a read/write section
6377 doesn't require us to define the function symbol in the
6378 executable on a global entry stub. A dynamic reloc can
6379 be used instead. The reason we prefer a few more dynamic
6380 relocs is that calling via a global entry stub costs a
6381 few more instructions, and pointer_equality_needed causes
6382 extra work in ld.so when resolving these symbols. */
6383 if (global_entry_stub (h))
6384 {
6385 if (!readonly_dynrelocs (h))
6386 {
6387 h->pointer_equality_needed = 0;
6388 /* If we haven't seen a branch reloc and the symbol
6389 isn't an ifunc then we don't need a plt entry. */
6390 if (!h->needs_plt)
6391 h->plt.plist = NULL;
6392 }
6393 else if (!bfd_link_pic (info))
6394 /* We are going to be defining the function symbol on the
6395 plt stub, so no dyn_relocs needed when non-pic. */
6396 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6397 }
6398
6399 /* ELFv2 function symbols can't have copy relocs. */
6400 return TRUE;
6401 }
6402 else if (!h->needs_plt
6403 && !readonly_dynrelocs (h))
6404 {
6405 /* If we haven't seen a branch reloc and the symbol isn't an
6406 ifunc then we don't need a plt entry. */
6407 h->plt.plist = NULL;
6408 h->pointer_equality_needed = 0;
6409 return TRUE;
6410 }
6411 }
6412 else
6413 h->plt.plist = NULL;
6414
6415 /* If this is a weak symbol, and there is a real definition, the
6416 processor independent code will have arranged for us to see the
6417 real definition first, and we can just use the same value. */
6418 if (h->is_weakalias)
6419 {
6420 struct elf_link_hash_entry *def = weakdef (h);
6421 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6422 h->root.u.def.section = def->root.u.def.section;
6423 h->root.u.def.value = def->root.u.def.value;
6424 if (def->root.u.def.section == htab->elf.sdynbss
6425 || def->root.u.def.section == htab->elf.sdynrelro)
6426 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6427 return TRUE;
6428 }
6429
6430 /* If we are creating a shared library, we must presume that the
6431 only references to the symbol are via the global offset table.
6432 For such cases we need not do anything here; the relocations will
6433 be handled correctly by relocate_section. */
6434 if (!bfd_link_executable (info))
6435 return TRUE;
6436
6437 /* If there are no references to this symbol that do not use the
6438 GOT, we don't need to generate a copy reloc. */
6439 if (!h->non_got_ref)
6440 return TRUE;
6441
6442 /* Don't generate a copy reloc for symbols defined in the executable. */
6443 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6444
6445 /* If -z nocopyreloc was given, don't generate them either. */
6446 || info->nocopyreloc
6447
6448 /* If we don't find any dynamic relocs in read-only sections, then
6449 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6450 || (ELIMINATE_COPY_RELOCS
6451 && !h->needs_copy
6452 && !alias_readonly_dynrelocs (h))
6453
6454 /* Protected variables do not work with .dynbss. The copy in
6455 .dynbss won't be used by the shared library with the protected
6456 definition for the variable. Text relocations are preferable
6457 to an incorrect program. */
6458 || h->protected_def)
6459 return TRUE;
6460
6461 if (h->plt.plist != NULL)
6462 {
6463 /* We should never get here, but unfortunately there are versions
6464 of gcc out there that improperly (for this ABI) put initialized
6465 function pointers, vtable refs and suchlike in read-only
6466 sections. Allow them to proceed, but warn that this might
6467 break at runtime. */
6468 info->callbacks->einfo
6469 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6470 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6471 h->root.root.string);
6472 }
6473
6474 /* This is a reference to a symbol defined by a dynamic object which
6475 is not a function. */
6476
6477 /* We must allocate the symbol in our .dynbss section, which will
6478 become part of the .bss section of the executable. There will be
6479 an entry for this symbol in the .dynsym section. The dynamic
6480 object will contain position independent code, so all references
6481 from the dynamic object to this symbol will go through the global
6482 offset table. The dynamic linker will use the .dynsym entry to
6483 determine the address it must put in the global offset table, so
6484 both the dynamic object and the regular object will refer to the
6485 same memory location for the variable. */
6486 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6487 {
6488 s = htab->elf.sdynrelro;
6489 srel = htab->elf.sreldynrelro;
6490 }
6491 else
6492 {
6493 s = htab->elf.sdynbss;
6494 srel = htab->elf.srelbss;
6495 }
6496 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6497 {
6498 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6499 linker to copy the initial value out of the dynamic object
6500 and into the runtime process image. */
6501 srel->size += sizeof (Elf64_External_Rela);
6502 h->needs_copy = 1;
6503 }
6504
6505 /* We no longer want dyn_relocs. */
6506 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6507 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6508 }
6509
6510 /* If given a function descriptor symbol, hide both the function code
6511 sym and the descriptor. */
6512 static void
6513 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6514 struct elf_link_hash_entry *h,
6515 bfd_boolean force_local)
6516 {
6517 struct ppc_link_hash_entry *eh;
6518 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6519
6520 if (ppc_hash_table (info) == NULL)
6521 return;
6522
6523 eh = (struct ppc_link_hash_entry *) h;
6524 if (eh->is_func_descriptor)
6525 {
6526 struct ppc_link_hash_entry *fh = eh->oh;
6527
6528 if (fh == NULL)
6529 {
6530 const char *p, *q;
6531 struct elf_link_hash_table *htab = elf_hash_table (info);
6532 char save;
6533
6534 /* We aren't supposed to use alloca in BFD because on
6535 systems which do not have alloca the version in libiberty
6536 calls xmalloc, which might cause the program to crash
6537 when it runs out of memory. This function doesn't have a
6538 return status, so there's no way to gracefully return an
6539 error. So cheat. We know that string[-1] can be safely
6540 accessed; It's either a string in an ELF string table,
6541 or allocated in an objalloc structure. */
6542
6543 p = eh->elf.root.root.string - 1;
6544 save = *p;
6545 *(char *) p = '.';
6546 fh = (struct ppc_link_hash_entry *)
6547 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6548 *(char *) p = save;
6549
6550 /* Unfortunately, if it so happens that the string we were
6551 looking for was allocated immediately before this string,
6552 then we overwrote the string terminator. That's the only
6553 reason the lookup should fail. */
6554 if (fh == NULL)
6555 {
6556 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6557 while (q >= eh->elf.root.root.string && *q == *p)
6558 --q, --p;
6559 if (q < eh->elf.root.root.string && *p == '.')
6560 fh = (struct ppc_link_hash_entry *)
6561 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6562 }
6563 if (fh != NULL)
6564 {
6565 eh->oh = fh;
6566 fh->oh = eh;
6567 }
6568 }
6569 if (fh != NULL)
6570 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6571 }
6572 }
6573
6574 static bfd_boolean
6575 get_sym_h (struct elf_link_hash_entry **hp,
6576 Elf_Internal_Sym **symp,
6577 asection **symsecp,
6578 unsigned char **tls_maskp,
6579 Elf_Internal_Sym **locsymsp,
6580 unsigned long r_symndx,
6581 bfd *ibfd)
6582 {
6583 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6584
6585 if (r_symndx >= symtab_hdr->sh_info)
6586 {
6587 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6588 struct elf_link_hash_entry *h;
6589
6590 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6591 h = elf_follow_link (h);
6592
6593 if (hp != NULL)
6594 *hp = h;
6595
6596 if (symp != NULL)
6597 *symp = NULL;
6598
6599 if (symsecp != NULL)
6600 {
6601 asection *symsec = NULL;
6602 if (h->root.type == bfd_link_hash_defined
6603 || h->root.type == bfd_link_hash_defweak)
6604 symsec = h->root.u.def.section;
6605 *symsecp = symsec;
6606 }
6607
6608 if (tls_maskp != NULL)
6609 {
6610 struct ppc_link_hash_entry *eh;
6611
6612 eh = (struct ppc_link_hash_entry *) h;
6613 *tls_maskp = &eh->tls_mask;
6614 }
6615 }
6616 else
6617 {
6618 Elf_Internal_Sym *sym;
6619 Elf_Internal_Sym *locsyms = *locsymsp;
6620
6621 if (locsyms == NULL)
6622 {
6623 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6624 if (locsyms == NULL)
6625 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6626 symtab_hdr->sh_info,
6627 0, NULL, NULL, NULL);
6628 if (locsyms == NULL)
6629 return FALSE;
6630 *locsymsp = locsyms;
6631 }
6632 sym = locsyms + r_symndx;
6633
6634 if (hp != NULL)
6635 *hp = NULL;
6636
6637 if (symp != NULL)
6638 *symp = sym;
6639
6640 if (symsecp != NULL)
6641 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6642
6643 if (tls_maskp != NULL)
6644 {
6645 struct got_entry **lgot_ents;
6646 unsigned char *tls_mask;
6647
6648 tls_mask = NULL;
6649 lgot_ents = elf_local_got_ents (ibfd);
6650 if (lgot_ents != NULL)
6651 {
6652 struct plt_entry **local_plt = (struct plt_entry **)
6653 (lgot_ents + symtab_hdr->sh_info);
6654 unsigned char *lgot_masks = (unsigned char *)
6655 (local_plt + symtab_hdr->sh_info);
6656 tls_mask = &lgot_masks[r_symndx];
6657 }
6658 *tls_maskp = tls_mask;
6659 }
6660 }
6661 return TRUE;
6662 }
6663
6664 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6665 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6666 type suitable for optimization, and 1 otherwise. */
6667
6668 static int
6669 get_tls_mask (unsigned char **tls_maskp,
6670 unsigned long *toc_symndx,
6671 bfd_vma *toc_addend,
6672 Elf_Internal_Sym **locsymsp,
6673 const Elf_Internal_Rela *rel,
6674 bfd *ibfd)
6675 {
6676 unsigned long r_symndx;
6677 int next_r;
6678 struct elf_link_hash_entry *h;
6679 Elf_Internal_Sym *sym;
6680 asection *sec;
6681 bfd_vma off;
6682
6683 r_symndx = ELF64_R_SYM (rel->r_info);
6684 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6685 return 0;
6686
6687 if ((*tls_maskp != NULL
6688 && (**tls_maskp & TLS_TLS) != 0
6689 && **tls_maskp != (TLS_TLS | TLS_MARK))
6690 || sec == NULL
6691 || ppc64_elf_section_data (sec) == NULL
6692 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6693 return 1;
6694
6695 /* Look inside a TOC section too. */
6696 if (h != NULL)
6697 {
6698 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6699 off = h->root.u.def.value;
6700 }
6701 else
6702 off = sym->st_value;
6703 off += rel->r_addend;
6704 BFD_ASSERT (off % 8 == 0);
6705 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6706 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6707 if (toc_symndx != NULL)
6708 *toc_symndx = r_symndx;
6709 if (toc_addend != NULL)
6710 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6711 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6712 return 0;
6713 if ((h == NULL || is_static_defined (h))
6714 && (next_r == -1 || next_r == -2))
6715 return 1 - next_r;
6716 return 1;
6717 }
6718
6719 /* Find (or create) an entry in the tocsave hash table. */
6720
6721 static struct tocsave_entry *
6722 tocsave_find (struct ppc_link_hash_table *htab,
6723 enum insert_option insert,
6724 Elf_Internal_Sym **local_syms,
6725 const Elf_Internal_Rela *irela,
6726 bfd *ibfd)
6727 {
6728 unsigned long r_indx;
6729 struct elf_link_hash_entry *h;
6730 Elf_Internal_Sym *sym;
6731 struct tocsave_entry ent, *p;
6732 hashval_t hash;
6733 struct tocsave_entry **slot;
6734
6735 r_indx = ELF64_R_SYM (irela->r_info);
6736 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6737 return NULL;
6738 if (ent.sec == NULL || ent.sec->output_section == NULL)
6739 {
6740 _bfd_error_handler
6741 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
6742 return NULL;
6743 }
6744
6745 if (h != NULL)
6746 ent.offset = h->root.u.def.value;
6747 else
6748 ent.offset = sym->st_value;
6749 ent.offset += irela->r_addend;
6750
6751 hash = tocsave_htab_hash (&ent);
6752 slot = ((struct tocsave_entry **)
6753 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6754 if (slot == NULL)
6755 return NULL;
6756
6757 if (*slot == NULL)
6758 {
6759 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6760 if (p == NULL)
6761 return NULL;
6762 *p = ent;
6763 *slot = p;
6764 }
6765 return *slot;
6766 }
6767
6768 /* Adjust all global syms defined in opd sections. In gcc generated
6769 code for the old ABI, these will already have been done. */
6770
6771 static bfd_boolean
6772 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6773 {
6774 struct ppc_link_hash_entry *eh;
6775 asection *sym_sec;
6776 struct _opd_sec_data *opd;
6777
6778 if (h->root.type == bfd_link_hash_indirect)
6779 return TRUE;
6780
6781 if (h->root.type != bfd_link_hash_defined
6782 && h->root.type != bfd_link_hash_defweak)
6783 return TRUE;
6784
6785 eh = (struct ppc_link_hash_entry *) h;
6786 if (eh->adjust_done)
6787 return TRUE;
6788
6789 sym_sec = eh->elf.root.u.def.section;
6790 opd = get_opd_info (sym_sec);
6791 if (opd != NULL && opd->adjust != NULL)
6792 {
6793 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
6794 if (adjust == -1)
6795 {
6796 /* This entry has been deleted. */
6797 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6798 if (dsec == NULL)
6799 {
6800 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6801 if (discarded_section (dsec))
6802 {
6803 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6804 break;
6805 }
6806 }
6807 eh->elf.root.u.def.value = 0;
6808 eh->elf.root.u.def.section = dsec;
6809 }
6810 else
6811 eh->elf.root.u.def.value += adjust;
6812 eh->adjust_done = 1;
6813 }
6814 return TRUE;
6815 }
6816
6817 /* Handles decrementing dynamic reloc counts for the reloc specified by
6818 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
6819 have already been determined. */
6820
6821 static bfd_boolean
6822 dec_dynrel_count (bfd_vma r_info,
6823 asection *sec,
6824 struct bfd_link_info *info,
6825 Elf_Internal_Sym **local_syms,
6826 struct elf_link_hash_entry *h,
6827 Elf_Internal_Sym *sym)
6828 {
6829 enum elf_ppc64_reloc_type r_type;
6830 asection *sym_sec = NULL;
6831
6832 /* Can this reloc be dynamic? This switch, and later tests here
6833 should be kept in sync with the code in check_relocs. */
6834 r_type = ELF64_R_TYPE (r_info);
6835 switch (r_type)
6836 {
6837 default:
6838 return TRUE;
6839
6840 case R_PPC64_TOC16:
6841 case R_PPC64_TOC16_DS:
6842 case R_PPC64_TOC16_LO:
6843 case R_PPC64_TOC16_HI:
6844 case R_PPC64_TOC16_HA:
6845 case R_PPC64_TOC16_LO_DS:
6846 if (h == NULL)
6847 return TRUE;
6848 break;
6849
6850 case R_PPC64_TPREL16:
6851 case R_PPC64_TPREL16_LO:
6852 case R_PPC64_TPREL16_HI:
6853 case R_PPC64_TPREL16_HA:
6854 case R_PPC64_TPREL16_DS:
6855 case R_PPC64_TPREL16_LO_DS:
6856 case R_PPC64_TPREL16_HIGH:
6857 case R_PPC64_TPREL16_HIGHA:
6858 case R_PPC64_TPREL16_HIGHER:
6859 case R_PPC64_TPREL16_HIGHERA:
6860 case R_PPC64_TPREL16_HIGHEST:
6861 case R_PPC64_TPREL16_HIGHESTA:
6862 case R_PPC64_TPREL64:
6863 case R_PPC64_TPREL34:
6864 case R_PPC64_DTPMOD64:
6865 case R_PPC64_DTPREL64:
6866 case R_PPC64_ADDR64:
6867 case R_PPC64_REL30:
6868 case R_PPC64_REL32:
6869 case R_PPC64_REL64:
6870 case R_PPC64_ADDR14:
6871 case R_PPC64_ADDR14_BRNTAKEN:
6872 case R_PPC64_ADDR14_BRTAKEN:
6873 case R_PPC64_ADDR16:
6874 case R_PPC64_ADDR16_DS:
6875 case R_PPC64_ADDR16_HA:
6876 case R_PPC64_ADDR16_HI:
6877 case R_PPC64_ADDR16_HIGH:
6878 case R_PPC64_ADDR16_HIGHA:
6879 case R_PPC64_ADDR16_HIGHER:
6880 case R_PPC64_ADDR16_HIGHERA:
6881 case R_PPC64_ADDR16_HIGHEST:
6882 case R_PPC64_ADDR16_HIGHESTA:
6883 case R_PPC64_ADDR16_LO:
6884 case R_PPC64_ADDR16_LO_DS:
6885 case R_PPC64_ADDR24:
6886 case R_PPC64_ADDR32:
6887 case R_PPC64_UADDR16:
6888 case R_PPC64_UADDR32:
6889 case R_PPC64_UADDR64:
6890 case R_PPC64_TOC:
6891 case R_PPC64_D34:
6892 case R_PPC64_D34_LO:
6893 case R_PPC64_D34_HI30:
6894 case R_PPC64_D34_HA30:
6895 case R_PPC64_ADDR16_HIGHER34:
6896 case R_PPC64_ADDR16_HIGHERA34:
6897 case R_PPC64_ADDR16_HIGHEST34:
6898 case R_PPC64_ADDR16_HIGHESTA34:
6899 case R_PPC64_D28:
6900 break;
6901 }
6902
6903 if (local_syms != NULL)
6904 {
6905 unsigned long r_symndx;
6906 bfd *ibfd = sec->owner;
6907
6908 r_symndx = ELF64_R_SYM (r_info);
6909 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6910 return FALSE;
6911 }
6912
6913 if ((h != NULL
6914 && (h->root.type == bfd_link_hash_defweak
6915 || !h->def_regular))
6916 || (h != NULL
6917 && !bfd_link_executable (info)
6918 && !SYMBOLIC_BIND (info, h))
6919 || (bfd_link_pic (info)
6920 && must_be_dyn_reloc (info, r_type))
6921 || (!bfd_link_pic (info)
6922 && (h != NULL
6923 ? h->type == STT_GNU_IFUNC
6924 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
6925 ;
6926 else
6927 return TRUE;
6928
6929 if (h != NULL)
6930 {
6931 struct elf_dyn_relocs *p;
6932 struct elf_dyn_relocs **pp;
6933 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6934
6935 /* elf_gc_sweep may have already removed all dyn relocs associated
6936 with local syms for a given section. Also, symbol flags are
6937 changed by elf_gc_sweep_symbol, confusing the test above. Don't
6938 report a dynreloc miscount. */
6939 if (*pp == NULL && info->gc_sections)
6940 return TRUE;
6941
6942 while ((p = *pp) != NULL)
6943 {
6944 if (p->sec == sec)
6945 {
6946 if (!must_be_dyn_reloc (info, r_type))
6947 p->pc_count -= 1;
6948 p->count -= 1;
6949 if (p->count == 0)
6950 *pp = p->next;
6951 return TRUE;
6952 }
6953 pp = &p->next;
6954 }
6955 }
6956 else
6957 {
6958 struct ppc_dyn_relocs *p;
6959 struct ppc_dyn_relocs **pp;
6960 void *vpp;
6961 bfd_boolean is_ifunc;
6962
6963 if (local_syms == NULL)
6964 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6965 if (sym_sec == NULL)
6966 sym_sec = sec;
6967
6968 vpp = &elf_section_data (sym_sec)->local_dynrel;
6969 pp = (struct ppc_dyn_relocs **) vpp;
6970
6971 if (*pp == NULL && info->gc_sections)
6972 return TRUE;
6973
6974 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6975 while ((p = *pp) != NULL)
6976 {
6977 if (p->sec == sec && p->ifunc == is_ifunc)
6978 {
6979 p->count -= 1;
6980 if (p->count == 0)
6981 *pp = p->next;
6982 return TRUE;
6983 }
6984 pp = &p->next;
6985 }
6986 }
6987
6988 /* xgettext:c-format */
6989 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6990 sec->owner, sec);
6991 bfd_set_error (bfd_error_bad_value);
6992 return FALSE;
6993 }
6994
6995 /* Remove unused Official Procedure Descriptor entries. Currently we
6996 only remove those associated with functions in discarded link-once
6997 sections, or weakly defined functions that have been overridden. It
6998 would be possible to remove many more entries for statically linked
6999 applications. */
7000
7001 bfd_boolean
7002 ppc64_elf_edit_opd (struct bfd_link_info *info)
7003 {
7004 bfd *ibfd;
7005 bfd_boolean some_edited = FALSE;
7006 asection *need_pad = NULL;
7007 struct ppc_link_hash_table *htab;
7008
7009 htab = ppc_hash_table (info);
7010 if (htab == NULL)
7011 return FALSE;
7012
7013 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7014 {
7015 asection *sec;
7016 Elf_Internal_Rela *relstart, *rel, *relend;
7017 Elf_Internal_Shdr *symtab_hdr;
7018 Elf_Internal_Sym *local_syms;
7019 struct _opd_sec_data *opd;
7020 bfd_boolean need_edit, add_aux_fields, broken;
7021 bfd_size_type cnt_16b = 0;
7022
7023 if (!is_ppc64_elf (ibfd))
7024 continue;
7025
7026 sec = bfd_get_section_by_name (ibfd, ".opd");
7027 if (sec == NULL || sec->size == 0)
7028 continue;
7029
7030 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7031 continue;
7032
7033 if (sec->output_section == bfd_abs_section_ptr)
7034 continue;
7035
7036 /* Look through the section relocs. */
7037 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7038 continue;
7039
7040 local_syms = NULL;
7041 symtab_hdr = &elf_symtab_hdr (ibfd);
7042
7043 /* Read the relocations. */
7044 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7045 info->keep_memory);
7046 if (relstart == NULL)
7047 return FALSE;
7048
7049 /* First run through the relocs to check they are sane, and to
7050 determine whether we need to edit this opd section. */
7051 need_edit = FALSE;
7052 broken = FALSE;
7053 need_pad = sec;
7054 relend = relstart + sec->reloc_count;
7055 for (rel = relstart; rel < relend; )
7056 {
7057 enum elf_ppc64_reloc_type r_type;
7058 unsigned long r_symndx;
7059 asection *sym_sec;
7060 struct elf_link_hash_entry *h;
7061 Elf_Internal_Sym *sym;
7062 bfd_vma offset;
7063
7064 /* .opd contains an array of 16 or 24 byte entries. We're
7065 only interested in the reloc pointing to a function entry
7066 point. */
7067 offset = rel->r_offset;
7068 if (rel + 1 == relend
7069 || rel[1].r_offset != offset + 8)
7070 {
7071 /* If someone messes with .opd alignment then after a
7072 "ld -r" we might have padding in the middle of .opd.
7073 Also, there's nothing to prevent someone putting
7074 something silly in .opd with the assembler. No .opd
7075 optimization for them! */
7076 broken_opd:
7077 _bfd_error_handler
7078 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7079 broken = TRUE;
7080 break;
7081 }
7082
7083 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7084 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7085 {
7086 _bfd_error_handler
7087 /* xgettext:c-format */
7088 (_("%pB: unexpected reloc type %u in .opd section"),
7089 ibfd, r_type);
7090 broken = TRUE;
7091 break;
7092 }
7093
7094 r_symndx = ELF64_R_SYM (rel->r_info);
7095 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7096 r_symndx, ibfd))
7097 goto error_ret;
7098
7099 if (sym_sec == NULL || sym_sec->owner == NULL)
7100 {
7101 const char *sym_name;
7102 if (h != NULL)
7103 sym_name = h->root.root.string;
7104 else
7105 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7106 sym_sec);
7107
7108 _bfd_error_handler
7109 /* xgettext:c-format */
7110 (_("%pB: undefined sym `%s' in .opd section"),
7111 ibfd, sym_name);
7112 broken = TRUE;
7113 break;
7114 }
7115
7116 /* opd entries are always for functions defined in the
7117 current input bfd. If the symbol isn't defined in the
7118 input bfd, then we won't be using the function in this
7119 bfd; It must be defined in a linkonce section in another
7120 bfd, or is weak. It's also possible that we are
7121 discarding the function due to a linker script /DISCARD/,
7122 which we test for via the output_section. */
7123 if (sym_sec->owner != ibfd
7124 || sym_sec->output_section == bfd_abs_section_ptr)
7125 need_edit = TRUE;
7126
7127 rel += 2;
7128 if (rel + 1 == relend
7129 || (rel + 2 < relend
7130 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7131 ++rel;
7132
7133 if (rel == relend)
7134 {
7135 if (sec->size == offset + 24)
7136 {
7137 need_pad = NULL;
7138 break;
7139 }
7140 if (sec->size == offset + 16)
7141 {
7142 cnt_16b++;
7143 break;
7144 }
7145 goto broken_opd;
7146 }
7147 else if (rel + 1 < relend
7148 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7149 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7150 {
7151 if (rel[0].r_offset == offset + 16)
7152 cnt_16b++;
7153 else if (rel[0].r_offset != offset + 24)
7154 goto broken_opd;
7155 }
7156 else
7157 goto broken_opd;
7158 }
7159
7160 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7161
7162 if (!broken && (need_edit || add_aux_fields))
7163 {
7164 Elf_Internal_Rela *write_rel;
7165 Elf_Internal_Shdr *rel_hdr;
7166 bfd_byte *rptr, *wptr;
7167 bfd_byte *new_contents;
7168 bfd_size_type amt;
7169
7170 new_contents = NULL;
7171 amt = OPD_NDX (sec->size) * sizeof (long);
7172 opd = &ppc64_elf_section_data (sec)->u.opd;
7173 opd->adjust = bfd_zalloc (sec->owner, amt);
7174 if (opd->adjust == NULL)
7175 return FALSE;
7176
7177 /* This seems a waste of time as input .opd sections are all
7178 zeros as generated by gcc, but I suppose there's no reason
7179 this will always be so. We might start putting something in
7180 the third word of .opd entries. */
7181 if ((sec->flags & SEC_IN_MEMORY) == 0)
7182 {
7183 bfd_byte *loc;
7184 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7185 {
7186 if (loc != NULL)
7187 free (loc);
7188 error_ret:
7189 if (local_syms != NULL
7190 && symtab_hdr->contents != (unsigned char *) local_syms)
7191 free (local_syms);
7192 if (elf_section_data (sec)->relocs != relstart)
7193 free (relstart);
7194 return FALSE;
7195 }
7196 sec->contents = loc;
7197 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7198 }
7199
7200 elf_section_data (sec)->relocs = relstart;
7201
7202 new_contents = sec->contents;
7203 if (add_aux_fields)
7204 {
7205 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7206 if (new_contents == NULL)
7207 return FALSE;
7208 need_pad = NULL;
7209 }
7210 wptr = new_contents;
7211 rptr = sec->contents;
7212 write_rel = relstart;
7213 for (rel = relstart; rel < relend; )
7214 {
7215 unsigned long r_symndx;
7216 asection *sym_sec;
7217 struct elf_link_hash_entry *h;
7218 struct ppc_link_hash_entry *fdh = NULL;
7219 Elf_Internal_Sym *sym;
7220 long opd_ent_size;
7221 Elf_Internal_Rela *next_rel;
7222 bfd_boolean skip;
7223
7224 r_symndx = ELF64_R_SYM (rel->r_info);
7225 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7226 r_symndx, ibfd))
7227 goto error_ret;
7228
7229 next_rel = rel + 2;
7230 if (next_rel + 1 == relend
7231 || (next_rel + 2 < relend
7232 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7233 ++next_rel;
7234
7235 /* See if the .opd entry is full 24 byte or
7236 16 byte (with fd_aux entry overlapped with next
7237 fd_func). */
7238 opd_ent_size = 24;
7239 if (next_rel == relend)
7240 {
7241 if (sec->size == rel->r_offset + 16)
7242 opd_ent_size = 16;
7243 }
7244 else if (next_rel->r_offset == rel->r_offset + 16)
7245 opd_ent_size = 16;
7246
7247 if (h != NULL
7248 && h->root.root.string[0] == '.')
7249 {
7250 fdh = ((struct ppc_link_hash_entry *) h)->oh;
7251 if (fdh != NULL)
7252 {
7253 fdh = ppc_follow_link (fdh);
7254 if (fdh->elf.root.type != bfd_link_hash_defined
7255 && fdh->elf.root.type != bfd_link_hash_defweak)
7256 fdh = NULL;
7257 }
7258 }
7259
7260 skip = (sym_sec->owner != ibfd
7261 || sym_sec->output_section == bfd_abs_section_ptr);
7262 if (skip)
7263 {
7264 if (fdh != NULL && sym_sec->owner == ibfd)
7265 {
7266 /* Arrange for the function descriptor sym
7267 to be dropped. */
7268 fdh->elf.root.u.def.value = 0;
7269 fdh->elf.root.u.def.section = sym_sec;
7270 }
7271 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7272
7273 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7274 rel = next_rel;
7275 else
7276 while (1)
7277 {
7278 if (!dec_dynrel_count (rel->r_info, sec, info,
7279 NULL, h, sym))
7280 goto error_ret;
7281
7282 if (++rel == next_rel)
7283 break;
7284
7285 r_symndx = ELF64_R_SYM (rel->r_info);
7286 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7287 r_symndx, ibfd))
7288 goto error_ret;
7289 }
7290 }
7291 else
7292 {
7293 /* We'll be keeping this opd entry. */
7294 long adjust;
7295
7296 if (fdh != NULL)
7297 {
7298 /* Redefine the function descriptor symbol to
7299 this location in the opd section. It is
7300 necessary to update the value here rather
7301 than using an array of adjustments as we do
7302 for local symbols, because various places
7303 in the generic ELF code use the value
7304 stored in u.def.value. */
7305 fdh->elf.root.u.def.value = wptr - new_contents;
7306 fdh->adjust_done = 1;
7307 }
7308
7309 /* Local syms are a bit tricky. We could
7310 tweak them as they can be cached, but
7311 we'd need to look through the local syms
7312 for the function descriptor sym which we
7313 don't have at the moment. So keep an
7314 array of adjustments. */
7315 adjust = (wptr - new_contents) - (rptr - sec->contents);
7316 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7317
7318 if (wptr != rptr)
7319 memcpy (wptr, rptr, opd_ent_size);
7320 wptr += opd_ent_size;
7321 if (add_aux_fields && opd_ent_size == 16)
7322 {
7323 memset (wptr, '\0', 8);
7324 wptr += 8;
7325 }
7326
7327 /* We need to adjust any reloc offsets to point to the
7328 new opd entries. */
7329 for ( ; rel != next_rel; ++rel)
7330 {
7331 rel->r_offset += adjust;
7332 if (write_rel != rel)
7333 memcpy (write_rel, rel, sizeof (*rel));
7334 ++write_rel;
7335 }
7336 }
7337
7338 rptr += opd_ent_size;
7339 }
7340
7341 sec->size = wptr - new_contents;
7342 sec->reloc_count = write_rel - relstart;
7343 if (add_aux_fields)
7344 {
7345 free (sec->contents);
7346 sec->contents = new_contents;
7347 }
7348
7349 /* Fudge the header size too, as this is used later in
7350 elf_bfd_final_link if we are emitting relocs. */
7351 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7352 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7353 some_edited = TRUE;
7354 }
7355 else if (elf_section_data (sec)->relocs != relstart)
7356 free (relstart);
7357
7358 if (local_syms != NULL
7359 && symtab_hdr->contents != (unsigned char *) local_syms)
7360 {
7361 if (!info->keep_memory)
7362 free (local_syms);
7363 else
7364 symtab_hdr->contents = (unsigned char *) local_syms;
7365 }
7366 }
7367
7368 if (some_edited)
7369 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7370
7371 /* If we are doing a final link and the last .opd entry is just 16 byte
7372 long, add a 8 byte padding after it. */
7373 if (need_pad != NULL && !bfd_link_relocatable (info))
7374 {
7375 bfd_byte *p;
7376
7377 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7378 {
7379 BFD_ASSERT (need_pad->size > 0);
7380
7381 p = bfd_malloc (need_pad->size + 8);
7382 if (p == NULL)
7383 return FALSE;
7384
7385 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7386 p, 0, need_pad->size))
7387 return FALSE;
7388
7389 need_pad->contents = p;
7390 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7391 }
7392 else
7393 {
7394 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7395 if (p == NULL)
7396 return FALSE;
7397
7398 need_pad->contents = p;
7399 }
7400
7401 memset (need_pad->contents + need_pad->size, 0, 8);
7402 need_pad->size += 8;
7403 }
7404
7405 return TRUE;
7406 }
7407
7408 /* Analyze inline PLT call relocations to see whether calls to locally
7409 defined functions can be converted to direct calls. */
7410
7411 bfd_boolean
7412 ppc64_elf_inline_plt (struct bfd_link_info *info)
7413 {
7414 struct ppc_link_hash_table *htab;
7415 bfd *ibfd;
7416 asection *sec;
7417 bfd_vma low_vma, high_vma, limit;
7418
7419 htab = ppc_hash_table (info);
7420 if (htab == NULL)
7421 return FALSE;
7422
7423 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7424 reduced somewhat to cater for possible stubs that might be added
7425 between the call and its destination. */
7426 if (htab->params->group_size < 0)
7427 {
7428 limit = -htab->params->group_size;
7429 if (limit == 1)
7430 limit = 0x1e00000;
7431 }
7432 else
7433 {
7434 limit = htab->params->group_size;
7435 if (limit == 1)
7436 limit = 0x1c00000;
7437 }
7438
7439 low_vma = -1;
7440 high_vma = 0;
7441 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7442 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7443 {
7444 if (low_vma > sec->vma)
7445 low_vma = sec->vma;
7446 if (high_vma < sec->vma + sec->size)
7447 high_vma = sec->vma + sec->size;
7448 }
7449
7450 /* If a "bl" can reach anywhere in local code sections, then we can
7451 convert all inline PLT sequences to direct calls when the symbol
7452 is local. */
7453 if (high_vma - low_vma < limit)
7454 {
7455 htab->can_convert_all_inline_plt = 1;
7456 return TRUE;
7457 }
7458
7459 /* Otherwise, go looking through relocs for cases where a direct
7460 call won't reach. Mark the symbol on any such reloc to disable
7461 the optimization and keep the PLT entry as it seems likely that
7462 this will be better than creating trampolines. Note that this
7463 will disable the optimization for all inline PLT calls to a
7464 particular symbol, not just those that won't reach. The
7465 difficulty in doing a more precise optimization is that the
7466 linker needs to make a decision depending on whether a
7467 particular R_PPC64_PLTCALL insn can be turned into a direct
7468 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7469 the sequence, and there is nothing that ties those relocs
7470 together except their symbol. */
7471
7472 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7473 {
7474 Elf_Internal_Shdr *symtab_hdr;
7475 Elf_Internal_Sym *local_syms;
7476
7477 if (!is_ppc64_elf (ibfd))
7478 continue;
7479
7480 local_syms = NULL;
7481 symtab_hdr = &elf_symtab_hdr (ibfd);
7482
7483 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7484 if (ppc64_elf_section_data (sec)->has_pltcall
7485 && !bfd_is_abs_section (sec->output_section))
7486 {
7487 Elf_Internal_Rela *relstart, *rel, *relend;
7488
7489 /* Read the relocations. */
7490 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7491 info->keep_memory);
7492 if (relstart == NULL)
7493 return FALSE;
7494
7495 relend = relstart + sec->reloc_count;
7496 for (rel = relstart; rel < relend; )
7497 {
7498 enum elf_ppc64_reloc_type r_type;
7499 unsigned long r_symndx;
7500 asection *sym_sec;
7501 struct elf_link_hash_entry *h;
7502 Elf_Internal_Sym *sym;
7503 unsigned char *tls_maskp;
7504
7505 r_type = ELF64_R_TYPE (rel->r_info);
7506 if (r_type != R_PPC64_PLTCALL
7507 && r_type != R_PPC64_PLTCALL_NOTOC)
7508 continue;
7509
7510 r_symndx = ELF64_R_SYM (rel->r_info);
7511 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7512 r_symndx, ibfd))
7513 {
7514 if (elf_section_data (sec)->relocs != relstart)
7515 free (relstart);
7516 if (local_syms != NULL
7517 && symtab_hdr->contents != (bfd_byte *) local_syms)
7518 free (local_syms);
7519 return FALSE;
7520 }
7521
7522 if (sym_sec != NULL && sym_sec->output_section != NULL)
7523 {
7524 bfd_vma from, to;
7525 if (h != NULL)
7526 to = h->root.u.def.value;
7527 else
7528 to = sym->st_value;
7529 to += (rel->r_addend
7530 + sym_sec->output_offset
7531 + sym_sec->output_section->vma);
7532 from = (rel->r_offset
7533 + sec->output_offset
7534 + sec->output_section->vma);
7535 if (to - from + limit < 2 * limit
7536 && !(r_type == R_PPC64_PLTCALL_NOTOC
7537 && (((h ? h->other : sym->st_other)
7538 & STO_PPC64_LOCAL_MASK)
7539 > 1 << STO_PPC64_LOCAL_BIT)))
7540 *tls_maskp &= ~PLT_KEEP;
7541 }
7542 }
7543 if (elf_section_data (sec)->relocs != relstart)
7544 free (relstart);
7545 }
7546
7547 if (local_syms != NULL
7548 && symtab_hdr->contents != (unsigned char *) local_syms)
7549 {
7550 if (!info->keep_memory)
7551 free (local_syms);
7552 else
7553 symtab_hdr->contents = (unsigned char *) local_syms;
7554 }
7555 }
7556
7557 return TRUE;
7558 }
7559
7560 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7561
7562 asection *
7563 ppc64_elf_tls_setup (struct bfd_link_info *info)
7564 {
7565 struct ppc_link_hash_table *htab;
7566
7567 htab = ppc_hash_table (info);
7568 if (htab == NULL)
7569 return NULL;
7570
7571 if (abiversion (info->output_bfd) == 1)
7572 htab->opd_abi = 1;
7573
7574 if (htab->params->no_multi_toc)
7575 htab->do_multi_toc = 0;
7576 else if (!htab->do_multi_toc)
7577 htab->params->no_multi_toc = 1;
7578
7579 /* Default to --no-plt-localentry, as this option can cause problems
7580 with symbol interposition. For example, glibc libpthread.so and
7581 libc.so duplicate many pthread symbols, with a fallback
7582 implementation in libc.so. In some cases the fallback does more
7583 work than the pthread implementation. __pthread_condattr_destroy
7584 is one such symbol: the libpthread.so implementation is
7585 localentry:0 while the libc.so implementation is localentry:8.
7586 An app that "cleverly" uses dlopen to only load necessary
7587 libraries at runtime may omit loading libpthread.so when not
7588 running multi-threaded, which then results in the libc.so
7589 fallback symbols being used and ld.so complaining. Now there
7590 are workarounds in ld (see non_zero_localentry) to detect the
7591 pthread situation, but that may not be the only case where
7592 --plt-localentry can cause trouble. */
7593 if (htab->params->plt_localentry0 < 0)
7594 htab->params->plt_localentry0 = 0;
7595 if (htab->params->plt_localentry0
7596 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7597 FALSE, FALSE, FALSE) == NULL)
7598 _bfd_error_handler
7599 (_("warning: --plt-localentry is especially dangerous without "
7600 "ld.so support to detect ABI violations"));
7601
7602 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7603 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7604 FALSE, FALSE, TRUE));
7605 /* Move dynamic linking info to the function descriptor sym. */
7606 if (htab->tls_get_addr != NULL)
7607 func_desc_adjust (&htab->tls_get_addr->elf, info);
7608 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7609 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7610 FALSE, FALSE, TRUE));
7611 if (htab->params->tls_get_addr_opt)
7612 {
7613 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7614
7615 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7616 FALSE, FALSE, TRUE);
7617 if (opt != NULL)
7618 func_desc_adjust (opt, info);
7619 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7620 FALSE, FALSE, TRUE);
7621 if (opt_fd != NULL
7622 && (opt_fd->root.type == bfd_link_hash_defined
7623 || opt_fd->root.type == bfd_link_hash_defweak))
7624 {
7625 /* If glibc supports an optimized __tls_get_addr call stub,
7626 signalled by the presence of __tls_get_addr_opt, and we'll
7627 be calling __tls_get_addr via a plt call stub, then
7628 make __tls_get_addr point to __tls_get_addr_opt. */
7629 tga_fd = &htab->tls_get_addr_fd->elf;
7630 if (htab->elf.dynamic_sections_created
7631 && tga_fd != NULL
7632 && (tga_fd->type == STT_FUNC
7633 || tga_fd->needs_plt)
7634 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7635 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
7636 {
7637 struct plt_entry *ent;
7638
7639 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7640 if (ent->plt.refcount > 0)
7641 break;
7642 if (ent != NULL)
7643 {
7644 tga_fd->root.type = bfd_link_hash_indirect;
7645 tga_fd->root.u.i.link = &opt_fd->root;
7646 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7647 opt_fd->mark = 1;
7648 if (opt_fd->dynindx != -1)
7649 {
7650 /* Use __tls_get_addr_opt in dynamic relocations. */
7651 opt_fd->dynindx = -1;
7652 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7653 opt_fd->dynstr_index);
7654 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7655 return NULL;
7656 }
7657 htab->tls_get_addr_fd
7658 = (struct ppc_link_hash_entry *) opt_fd;
7659 tga = &htab->tls_get_addr->elf;
7660 if (opt != NULL && tga != NULL)
7661 {
7662 tga->root.type = bfd_link_hash_indirect;
7663 tga->root.u.i.link = &opt->root;
7664 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7665 opt->mark = 1;
7666 _bfd_elf_link_hash_hide_symbol (info, opt,
7667 tga->forced_local);
7668 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7669 }
7670 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7671 htab->tls_get_addr_fd->is_func_descriptor = 1;
7672 if (htab->tls_get_addr != NULL)
7673 {
7674 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7675 htab->tls_get_addr->is_func = 1;
7676 }
7677 }
7678 }
7679 }
7680 else if (htab->params->tls_get_addr_opt < 0)
7681 htab->params->tls_get_addr_opt = 0;
7682 }
7683 return _bfd_elf_tls_setup (info->output_bfd, info);
7684 }
7685
7686 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7687 HASH1 or HASH2. */
7688
7689 static bfd_boolean
7690 branch_reloc_hash_match (const bfd *ibfd,
7691 const Elf_Internal_Rela *rel,
7692 const struct ppc_link_hash_entry *hash1,
7693 const struct ppc_link_hash_entry *hash2)
7694 {
7695 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7696 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7697 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7698
7699 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7700 {
7701 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7702 struct elf_link_hash_entry *h;
7703
7704 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7705 h = elf_follow_link (h);
7706 if (h == &hash1->elf || h == &hash2->elf)
7707 return TRUE;
7708 }
7709 return FALSE;
7710 }
7711
7712 /* Run through all the TLS relocs looking for optimization
7713 opportunities. The linker has been hacked (see ppc64elf.em) to do
7714 a preliminary section layout so that we know the TLS segment
7715 offsets. We can't optimize earlier because some optimizations need
7716 to know the tp offset, and we need to optimize before allocating
7717 dynamic relocations. */
7718
7719 bfd_boolean
7720 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7721 {
7722 bfd *ibfd;
7723 asection *sec;
7724 struct ppc_link_hash_table *htab;
7725 unsigned char *toc_ref;
7726 int pass;
7727
7728 if (!bfd_link_executable (info))
7729 return TRUE;
7730
7731 htab = ppc_hash_table (info);
7732 if (htab == NULL)
7733 return FALSE;
7734
7735 /* Make two passes over the relocs. On the first pass, mark toc
7736 entries involved with tls relocs, and check that tls relocs
7737 involved in setting up a tls_get_addr call are indeed followed by
7738 such a call. If they are not, we can't do any tls optimization.
7739 On the second pass twiddle tls_mask flags to notify
7740 relocate_section that optimization can be done, and adjust got
7741 and plt refcounts. */
7742 toc_ref = NULL;
7743 for (pass = 0; pass < 2; ++pass)
7744 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7745 {
7746 Elf_Internal_Sym *locsyms = NULL;
7747 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7748
7749 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7750 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7751 {
7752 Elf_Internal_Rela *relstart, *rel, *relend;
7753 bfd_boolean found_tls_get_addr_arg = 0;
7754
7755 /* Read the relocations. */
7756 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7757 info->keep_memory);
7758 if (relstart == NULL)
7759 {
7760 free (toc_ref);
7761 return FALSE;
7762 }
7763
7764 relend = relstart + sec->reloc_count;
7765 for (rel = relstart; rel < relend; rel++)
7766 {
7767 enum elf_ppc64_reloc_type r_type;
7768 unsigned long r_symndx;
7769 struct elf_link_hash_entry *h;
7770 Elf_Internal_Sym *sym;
7771 asection *sym_sec;
7772 unsigned char *tls_mask;
7773 unsigned int tls_set, tls_clear, tls_type = 0;
7774 bfd_vma value;
7775 bfd_boolean ok_tprel, is_local;
7776 long toc_ref_index = 0;
7777 int expecting_tls_get_addr = 0;
7778 bfd_boolean ret = FALSE;
7779
7780 r_symndx = ELF64_R_SYM (rel->r_info);
7781 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7782 r_symndx, ibfd))
7783 {
7784 err_free_rel:
7785 if (elf_section_data (sec)->relocs != relstart)
7786 free (relstart);
7787 if (toc_ref != NULL)
7788 free (toc_ref);
7789 if (locsyms != NULL
7790 && (elf_symtab_hdr (ibfd).contents
7791 != (unsigned char *) locsyms))
7792 free (locsyms);
7793 return ret;
7794 }
7795
7796 if (h != NULL)
7797 {
7798 if (h->root.type == bfd_link_hash_defined
7799 || h->root.type == bfd_link_hash_defweak)
7800 value = h->root.u.def.value;
7801 else if (h->root.type == bfd_link_hash_undefweak)
7802 value = 0;
7803 else
7804 {
7805 found_tls_get_addr_arg = 0;
7806 continue;
7807 }
7808 }
7809 else
7810 /* Symbols referenced by TLS relocs must be of type
7811 STT_TLS. So no need for .opd local sym adjust. */
7812 value = sym->st_value;
7813
7814 ok_tprel = FALSE;
7815 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
7816 if (is_local)
7817 {
7818 if (h != NULL
7819 && h->root.type == bfd_link_hash_undefweak)
7820 ok_tprel = TRUE;
7821 else if (sym_sec != NULL
7822 && sym_sec->output_section != NULL)
7823 {
7824 value += sym_sec->output_offset;
7825 value += sym_sec->output_section->vma;
7826 value -= htab->elf.tls_sec->vma + TP_OFFSET;
7827 /* Note that even though the prefix insns
7828 allow a 1<<33 offset we use the same test
7829 as for addis;addi. There may be a mix of
7830 pcrel and non-pcrel code and the decision
7831 to optimise is per symbol, not per TLS
7832 sequence. */
7833 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
7834 }
7835 }
7836
7837 r_type = ELF64_R_TYPE (rel->r_info);
7838 /* If this section has old-style __tls_get_addr calls
7839 without marker relocs, then check that each
7840 __tls_get_addr call reloc is preceded by a reloc
7841 that conceivably belongs to the __tls_get_addr arg
7842 setup insn. If we don't find matching arg setup
7843 relocs, don't do any tls optimization. */
7844 if (pass == 0
7845 && sec->nomark_tls_get_addr
7846 && h != NULL
7847 && (h == &htab->tls_get_addr->elf
7848 || h == &htab->tls_get_addr_fd->elf)
7849 && !found_tls_get_addr_arg
7850 && is_branch_reloc (r_type))
7851 {
7852 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7853 "TLS optimization disabled\n"),
7854 ibfd, sec, rel->r_offset);
7855 ret = TRUE;
7856 goto err_free_rel;
7857 }
7858
7859 found_tls_get_addr_arg = 0;
7860 switch (r_type)
7861 {
7862 case R_PPC64_GOT_TLSLD16:
7863 case R_PPC64_GOT_TLSLD16_LO:
7864 case R_PPC64_GOT_TLSLD34:
7865 expecting_tls_get_addr = 1;
7866 found_tls_get_addr_arg = 1;
7867 /* Fall through. */
7868
7869 case R_PPC64_GOT_TLSLD16_HI:
7870 case R_PPC64_GOT_TLSLD16_HA:
7871 /* These relocs should never be against a symbol
7872 defined in a shared lib. Leave them alone if
7873 that turns out to be the case. */
7874 if (!is_local)
7875 continue;
7876
7877 /* LD -> LE */
7878 tls_set = 0;
7879 tls_clear = TLS_LD;
7880 tls_type = TLS_TLS | TLS_LD;
7881 break;
7882
7883 case R_PPC64_GOT_TLSGD16:
7884 case R_PPC64_GOT_TLSGD16_LO:
7885 case R_PPC64_GOT_TLSGD34:
7886 expecting_tls_get_addr = 1;
7887 found_tls_get_addr_arg = 1;
7888 /* Fall through. */
7889
7890 case R_PPC64_GOT_TLSGD16_HI:
7891 case R_PPC64_GOT_TLSGD16_HA:
7892 if (ok_tprel)
7893 /* GD -> LE */
7894 tls_set = 0;
7895 else
7896 /* GD -> IE */
7897 tls_set = TLS_TLS | TLS_GDIE;
7898 tls_clear = TLS_GD;
7899 tls_type = TLS_TLS | TLS_GD;
7900 break;
7901
7902 case R_PPC64_GOT_TPREL34:
7903 case R_PPC64_GOT_TPREL16_DS:
7904 case R_PPC64_GOT_TPREL16_LO_DS:
7905 case R_PPC64_GOT_TPREL16_HI:
7906 case R_PPC64_GOT_TPREL16_HA:
7907 if (ok_tprel)
7908 {
7909 /* IE -> LE */
7910 tls_set = 0;
7911 tls_clear = TLS_TPREL;
7912 tls_type = TLS_TLS | TLS_TPREL;
7913 break;
7914 }
7915 continue;
7916
7917 case R_PPC64_TLSLD:
7918 if (!is_local)
7919 continue;
7920 /* Fall through. */
7921 case R_PPC64_TLSGD:
7922 if (rel + 1 < relend
7923 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7924 {
7925 if (pass != 0
7926 && (ELF64_R_TYPE (rel[1].r_info)
7927 != R_PPC64_PLTSEQ)
7928 && (ELF64_R_TYPE (rel[1].r_info)
7929 != R_PPC64_PLTSEQ_NOTOC))
7930 {
7931 r_symndx = ELF64_R_SYM (rel[1].r_info);
7932 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
7933 r_symndx, ibfd))
7934 goto err_free_rel;
7935 if (h != NULL)
7936 {
7937 struct plt_entry *ent = NULL;
7938
7939 for (ent = h->plt.plist;
7940 ent != NULL;
7941 ent = ent->next)
7942 if (ent->addend == rel[1].r_addend)
7943 break;
7944
7945 if (ent != NULL
7946 && ent->plt.refcount > 0)
7947 ent->plt.refcount -= 1;
7948 }
7949 }
7950 continue;
7951 }
7952 found_tls_get_addr_arg = 1;
7953 /* Fall through. */
7954
7955 case R_PPC64_TLS:
7956 case R_PPC64_TOC16:
7957 case R_PPC64_TOC16_LO:
7958 if (sym_sec == NULL || sym_sec != toc)
7959 continue;
7960
7961 /* Mark this toc entry as referenced by a TLS
7962 code sequence. We can do that now in the
7963 case of R_PPC64_TLS, and after checking for
7964 tls_get_addr for the TOC16 relocs. */
7965 if (toc_ref == NULL)
7966 toc_ref
7967 = bfd_zmalloc (toc->output_section->rawsize / 8);
7968 if (toc_ref == NULL)
7969 goto err_free_rel;
7970
7971 if (h != NULL)
7972 value = h->root.u.def.value;
7973 else
7974 value = sym->st_value;
7975 value += rel->r_addend;
7976 if (value % 8 != 0)
7977 continue;
7978 BFD_ASSERT (value < toc->size
7979 && toc->output_offset % 8 == 0);
7980 toc_ref_index = (value + toc->output_offset) / 8;
7981 if (r_type == R_PPC64_TLS
7982 || r_type == R_PPC64_TLSGD
7983 || r_type == R_PPC64_TLSLD)
7984 {
7985 toc_ref[toc_ref_index] = 1;
7986 continue;
7987 }
7988
7989 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7990 continue;
7991
7992 tls_set = 0;
7993 tls_clear = 0;
7994 expecting_tls_get_addr = 2;
7995 break;
7996
7997 case R_PPC64_TPREL64:
7998 if (pass == 0
7999 || sec != toc
8000 || toc_ref == NULL
8001 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8002 continue;
8003 if (ok_tprel)
8004 {
8005 /* IE -> LE */
8006 tls_set = TLS_EXPLICIT;
8007 tls_clear = TLS_TPREL;
8008 break;
8009 }
8010 continue;
8011
8012 case R_PPC64_DTPMOD64:
8013 if (pass == 0
8014 || sec != toc
8015 || toc_ref == NULL
8016 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8017 continue;
8018 if (rel + 1 < relend
8019 && (rel[1].r_info
8020 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8021 && rel[1].r_offset == rel->r_offset + 8)
8022 {
8023 if (ok_tprel)
8024 /* GD -> LE */
8025 tls_set = TLS_EXPLICIT | TLS_GD;
8026 else
8027 /* GD -> IE */
8028 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8029 tls_clear = TLS_GD;
8030 }
8031 else
8032 {
8033 if (!is_local)
8034 continue;
8035
8036 /* LD -> LE */
8037 tls_set = TLS_EXPLICIT;
8038 tls_clear = TLS_LD;
8039 }
8040 break;
8041
8042 default:
8043 continue;
8044 }
8045
8046 if (pass == 0)
8047 {
8048 if (!expecting_tls_get_addr
8049 || !sec->nomark_tls_get_addr)
8050 continue;
8051
8052 if (rel + 1 < relend
8053 && branch_reloc_hash_match (ibfd, rel + 1,
8054 htab->tls_get_addr,
8055 htab->tls_get_addr_fd))
8056 {
8057 if (expecting_tls_get_addr == 2)
8058 {
8059 /* Check for toc tls entries. */
8060 unsigned char *toc_tls;
8061 int retval;
8062
8063 retval = get_tls_mask (&toc_tls, NULL, NULL,
8064 &locsyms,
8065 rel, ibfd);
8066 if (retval == 0)
8067 goto err_free_rel;
8068 if (toc_tls != NULL)
8069 {
8070 if ((*toc_tls & TLS_TLS) != 0
8071 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8072 found_tls_get_addr_arg = 1;
8073 if (retval > 1)
8074 toc_ref[toc_ref_index] = 1;
8075 }
8076 }
8077 continue;
8078 }
8079
8080 /* Uh oh, we didn't find the expected call. We
8081 could just mark this symbol to exclude it
8082 from tls optimization but it's safer to skip
8083 the entire optimization. */
8084 /* xgettext:c-format */
8085 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8086 "TLS optimization disabled\n"),
8087 ibfd, sec, rel->r_offset);
8088 ret = TRUE;
8089 goto err_free_rel;
8090 }
8091
8092 /* If we don't have old-style __tls_get_addr calls
8093 without TLSGD/TLSLD marker relocs, and we haven't
8094 found a new-style __tls_get_addr call with a
8095 marker for this symbol, then we either have a
8096 broken object file or an -mlongcall style
8097 indirect call to __tls_get_addr without a marker.
8098 Disable optimization in this case. */
8099 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8100 && (tls_set & TLS_EXPLICIT) == 0
8101 && !sec->nomark_tls_get_addr
8102 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8103 != (TLS_TLS | TLS_MARK)))
8104 continue;
8105
8106 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8107 {
8108 struct plt_entry *ent = NULL;
8109
8110 if (htab->tls_get_addr != NULL)
8111 for (ent = htab->tls_get_addr->elf.plt.plist;
8112 ent != NULL;
8113 ent = ent->next)
8114 if (ent->addend == 0)
8115 break;
8116
8117 if (ent == NULL && htab->tls_get_addr_fd != NULL)
8118 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8119 ent != NULL;
8120 ent = ent->next)
8121 if (ent->addend == 0)
8122 break;
8123
8124 if (ent != NULL
8125 && ent->plt.refcount > 0)
8126 ent->plt.refcount -= 1;
8127 }
8128
8129 if (tls_clear == 0)
8130 continue;
8131
8132 if ((tls_set & TLS_EXPLICIT) == 0)
8133 {
8134 struct got_entry *ent;
8135
8136 /* Adjust got entry for this reloc. */
8137 if (h != NULL)
8138 ent = h->got.glist;
8139 else
8140 ent = elf_local_got_ents (ibfd)[r_symndx];
8141
8142 for (; ent != NULL; ent = ent->next)
8143 if (ent->addend == rel->r_addend
8144 && ent->owner == ibfd
8145 && ent->tls_type == tls_type)
8146 break;
8147 if (ent == NULL)
8148 abort ();
8149
8150 if (tls_set == 0)
8151 {
8152 /* We managed to get rid of a got entry. */
8153 if (ent->got.refcount > 0)
8154 ent->got.refcount -= 1;
8155 }
8156 }
8157 else
8158 {
8159 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8160 we'll lose one or two dyn relocs. */
8161 if (!dec_dynrel_count (rel->r_info, sec, info,
8162 NULL, h, sym))
8163 return FALSE;
8164
8165 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8166 {
8167 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8168 NULL, h, sym))
8169 return FALSE;
8170 }
8171 }
8172
8173 *tls_mask |= tls_set & 0xff;
8174 *tls_mask &= ~tls_clear;
8175 }
8176
8177 if (elf_section_data (sec)->relocs != relstart)
8178 free (relstart);
8179 }
8180
8181 if (locsyms != NULL
8182 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8183 {
8184 if (!info->keep_memory)
8185 free (locsyms);
8186 else
8187 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8188 }
8189 }
8190
8191 if (toc_ref != NULL)
8192 free (toc_ref);
8193 htab->do_tls_opt = 1;
8194 return TRUE;
8195 }
8196
8197 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8198 the values of any global symbols in a toc section that has been
8199 edited. Globals in toc sections should be a rarity, so this function
8200 sets a flag if any are found in toc sections other than the one just
8201 edited, so that further hash table traversals can be avoided. */
8202
8203 struct adjust_toc_info
8204 {
8205 asection *toc;
8206 unsigned long *skip;
8207 bfd_boolean global_toc_syms;
8208 };
8209
8210 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8211
8212 static bfd_boolean
8213 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8214 {
8215 struct ppc_link_hash_entry *eh;
8216 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8217 unsigned long i;
8218
8219 if (h->root.type != bfd_link_hash_defined
8220 && h->root.type != bfd_link_hash_defweak)
8221 return TRUE;
8222
8223 eh = (struct ppc_link_hash_entry *) h;
8224 if (eh->adjust_done)
8225 return TRUE;
8226
8227 if (eh->elf.root.u.def.section == toc_inf->toc)
8228 {
8229 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8230 i = toc_inf->toc->rawsize >> 3;
8231 else
8232 i = eh->elf.root.u.def.value >> 3;
8233
8234 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8235 {
8236 _bfd_error_handler
8237 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8238 do
8239 ++i;
8240 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8241 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8242 }
8243
8244 eh->elf.root.u.def.value -= toc_inf->skip[i];
8245 eh->adjust_done = 1;
8246 }
8247 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8248 toc_inf->global_toc_syms = TRUE;
8249
8250 return TRUE;
8251 }
8252
8253 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8254 on a _LO variety toc/got reloc. */
8255
8256 static bfd_boolean
8257 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8258 {
8259 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8260 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8261 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8262 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8263 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8264 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8265 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8266 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8267 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8268 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8269 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8270 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8271 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8272 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8273 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8274 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8275 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8276 /* Exclude lfqu by testing reloc. If relocs are ever
8277 defined for the reduced D field in psq_lu then those
8278 will need testing too. */
8279 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8280 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8281 && (insn & 1) == 0)
8282 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8283 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8284 /* Exclude stfqu. psq_stu as above for psq_lu. */
8285 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8286 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8287 && (insn & 1) == 0));
8288 }
8289
8290 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8291 pld ra,symbol@got@pcrel
8292 load/store rt,off(ra)
8293 or
8294 pla ra,symbol@pcrel
8295 load/store rt,off(ra)
8296 may be translated to
8297 pload/pstore rt,symbol+off@pcrel
8298 nop.
8299 This function returns true if the optimization is possible, placing
8300 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8301
8302 On entry to this function, the linker has already determined that
8303 the pld can be replaced with pla: *PINSN1 is that pla insn,
8304 while *PINSN2 is the second instruction. */
8305
8306 static bfd_boolean
8307 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8308 {
8309 uint64_t insn1 = *pinsn1;
8310 uint64_t insn2 = *pinsn2;
8311 bfd_signed_vma off;
8312
8313 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8314 {
8315 /* Check that regs match. */
8316 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8317 return FALSE;
8318
8319 /* P8LS or PMLS form, non-pcrel. */
8320 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8321 return FALSE;
8322
8323 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8324 *pinsn2 = PNOP;
8325 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8326 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8327 return TRUE;
8328 }
8329
8330 insn2 >>= 32;
8331
8332 /* Check that regs match. */
8333 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8334 return FALSE;
8335
8336 switch ((insn2 >> 26) & 63)
8337 {
8338 default:
8339 return FALSE;
8340
8341 case 32: /* lwz */
8342 case 34: /* lbz */
8343 case 36: /* stw */
8344 case 38: /* stb */
8345 case 40: /* lhz */
8346 case 42: /* lha */
8347 case 44: /* sth */
8348 case 48: /* lfs */
8349 case 50: /* lfd */
8350 case 52: /* stfs */
8351 case 54: /* stfd */
8352 /* These are the PMLS cases, where we just need to tack a prefix
8353 on the insn. */
8354 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8355 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8356 off = insn2 & 0xffff;
8357 break;
8358
8359 case 58: /* lwa, ld */
8360 if ((insn2 & 1) != 0)
8361 return FALSE;
8362 insn1 = ((1ULL << 58) | (1ULL << 52)
8363 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8364 | (insn2 & (31ULL << 21)));
8365 off = insn2 & 0xfffc;
8366 break;
8367
8368 case 57: /* lxsd, lxssp */
8369 if ((insn2 & 3) < 2)
8370 return FALSE;
8371 insn1 = ((1ULL << 58) | (1ULL << 52)
8372 | ((40ULL | (insn2 & 3)) << 26)
8373 | (insn2 & (31ULL << 21)));
8374 off = insn2 & 0xfffc;
8375 break;
8376
8377 case 61: /* stxsd, stxssp, lxv, stxv */
8378 if ((insn2 & 3) == 0)
8379 return FALSE;
8380 else if ((insn2 & 3) >= 2)
8381 {
8382 insn1 = ((1ULL << 58) | (1ULL << 52)
8383 | ((44ULL | (insn2 & 3)) << 26)
8384 | (insn2 & (31ULL << 21)));
8385 off = insn2 & 0xfffc;
8386 }
8387 else
8388 {
8389 insn1 = ((1ULL << 58) | (1ULL << 52)
8390 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8391 | (insn2 & (31ULL << 21)));
8392 off = insn2 & 0xfff0;
8393 }
8394 break;
8395
8396 case 56: /* lq */
8397 insn1 = ((1ULL << 58) | (1ULL << 52)
8398 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8399 off = insn2 & 0xffff;
8400 break;
8401
8402 case 62: /* std, stq */
8403 if ((insn2 & 1) != 0)
8404 return FALSE;
8405 insn1 = ((1ULL << 58) | (1ULL << 52)
8406 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8407 | (insn2 & (31ULL << 21)));
8408 off = insn2 & 0xfffc;
8409 break;
8410 }
8411
8412 *pinsn1 = insn1;
8413 *pinsn2 = (uint64_t) NOP << 32;
8414 *poff = (off ^ 0x8000) - 0x8000;
8415 return TRUE;
8416 }
8417
8418 /* Examine all relocs referencing .toc sections in order to remove
8419 unused .toc entries. */
8420
8421 bfd_boolean
8422 ppc64_elf_edit_toc (struct bfd_link_info *info)
8423 {
8424 bfd *ibfd;
8425 struct adjust_toc_info toc_inf;
8426 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8427
8428 htab->do_toc_opt = 1;
8429 toc_inf.global_toc_syms = TRUE;
8430 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8431 {
8432 asection *toc, *sec;
8433 Elf_Internal_Shdr *symtab_hdr;
8434 Elf_Internal_Sym *local_syms;
8435 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8436 unsigned long *skip, *drop;
8437 unsigned char *used;
8438 unsigned char *keep, last, some_unused;
8439
8440 if (!is_ppc64_elf (ibfd))
8441 continue;
8442
8443 toc = bfd_get_section_by_name (ibfd, ".toc");
8444 if (toc == NULL
8445 || toc->size == 0
8446 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8447 || discarded_section (toc))
8448 continue;
8449
8450 toc_relocs = NULL;
8451 local_syms = NULL;
8452 symtab_hdr = &elf_symtab_hdr (ibfd);
8453
8454 /* Look at sections dropped from the final link. */
8455 skip = NULL;
8456 relstart = NULL;
8457 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8458 {
8459 if (sec->reloc_count == 0
8460 || !discarded_section (sec)
8461 || get_opd_info (sec)
8462 || (sec->flags & SEC_ALLOC) == 0
8463 || (sec->flags & SEC_DEBUGGING) != 0)
8464 continue;
8465
8466 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8467 if (relstart == NULL)
8468 goto error_ret;
8469
8470 /* Run through the relocs to see which toc entries might be
8471 unused. */
8472 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8473 {
8474 enum elf_ppc64_reloc_type r_type;
8475 unsigned long r_symndx;
8476 asection *sym_sec;
8477 struct elf_link_hash_entry *h;
8478 Elf_Internal_Sym *sym;
8479 bfd_vma val;
8480
8481 r_type = ELF64_R_TYPE (rel->r_info);
8482 switch (r_type)
8483 {
8484 default:
8485 continue;
8486
8487 case R_PPC64_TOC16:
8488 case R_PPC64_TOC16_LO:
8489 case R_PPC64_TOC16_HI:
8490 case R_PPC64_TOC16_HA:
8491 case R_PPC64_TOC16_DS:
8492 case R_PPC64_TOC16_LO_DS:
8493 break;
8494 }
8495
8496 r_symndx = ELF64_R_SYM (rel->r_info);
8497 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8498 r_symndx, ibfd))
8499 goto error_ret;
8500
8501 if (sym_sec != toc)
8502 continue;
8503
8504 if (h != NULL)
8505 val = h->root.u.def.value;
8506 else
8507 val = sym->st_value;
8508 val += rel->r_addend;
8509
8510 if (val >= toc->size)
8511 continue;
8512
8513 /* Anything in the toc ought to be aligned to 8 bytes.
8514 If not, don't mark as unused. */
8515 if (val & 7)
8516 continue;
8517
8518 if (skip == NULL)
8519 {
8520 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8521 if (skip == NULL)
8522 goto error_ret;
8523 }
8524
8525 skip[val >> 3] = ref_from_discarded;
8526 }
8527
8528 if (elf_section_data (sec)->relocs != relstart)
8529 free (relstart);
8530 }
8531
8532 /* For largetoc loads of address constants, we can convert
8533 . addis rx,2,addr@got@ha
8534 . ld ry,addr@got@l(rx)
8535 to
8536 . addis rx,2,addr@toc@ha
8537 . addi ry,rx,addr@toc@l
8538 when addr is within 2G of the toc pointer. This then means
8539 that the word storing "addr" in the toc is no longer needed. */
8540
8541 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8542 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8543 && toc->reloc_count != 0)
8544 {
8545 /* Read toc relocs. */
8546 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8547 info->keep_memory);
8548 if (toc_relocs == NULL)
8549 goto error_ret;
8550
8551 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8552 {
8553 enum elf_ppc64_reloc_type r_type;
8554 unsigned long r_symndx;
8555 asection *sym_sec;
8556 struct elf_link_hash_entry *h;
8557 Elf_Internal_Sym *sym;
8558 bfd_vma val, addr;
8559
8560 r_type = ELF64_R_TYPE (rel->r_info);
8561 if (r_type != R_PPC64_ADDR64)
8562 continue;
8563
8564 r_symndx = ELF64_R_SYM (rel->r_info);
8565 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8566 r_symndx, ibfd))
8567 goto error_ret;
8568
8569 if (sym_sec == NULL
8570 || sym_sec->output_section == NULL
8571 || discarded_section (sym_sec))
8572 continue;
8573
8574 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8575 continue;
8576
8577 if (h != NULL)
8578 {
8579 if (h->type == STT_GNU_IFUNC)
8580 continue;
8581 val = h->root.u.def.value;
8582 }
8583 else
8584 {
8585 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8586 continue;
8587 val = sym->st_value;
8588 }
8589 val += rel->r_addend;
8590 val += sym_sec->output_section->vma + sym_sec->output_offset;
8591
8592 /* We don't yet know the exact toc pointer value, but we
8593 know it will be somewhere in the toc section. Don't
8594 optimize if the difference from any possible toc
8595 pointer is outside [ff..f80008000, 7fff7fff]. */
8596 addr = toc->output_section->vma + TOC_BASE_OFF;
8597 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8598 continue;
8599
8600 addr = toc->output_section->vma + toc->output_section->rawsize;
8601 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8602 continue;
8603
8604 if (skip == NULL)
8605 {
8606 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8607 if (skip == NULL)
8608 goto error_ret;
8609 }
8610
8611 skip[rel->r_offset >> 3]
8612 |= can_optimize | ((rel - toc_relocs) << 2);
8613 }
8614 }
8615
8616 if (skip == NULL)
8617 continue;
8618
8619 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8620 if (used == NULL)
8621 {
8622 error_ret:
8623 if (local_syms != NULL
8624 && symtab_hdr->contents != (unsigned char *) local_syms)
8625 free (local_syms);
8626 if (sec != NULL
8627 && relstart != NULL
8628 && elf_section_data (sec)->relocs != relstart)
8629 free (relstart);
8630 if (toc_relocs != NULL
8631 && elf_section_data (toc)->relocs != toc_relocs)
8632 free (toc_relocs);
8633 if (skip != NULL)
8634 free (skip);
8635 return FALSE;
8636 }
8637
8638 /* Now check all kept sections that might reference the toc.
8639 Check the toc itself last. */
8640 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8641 : ibfd->sections);
8642 sec != NULL;
8643 sec = (sec == toc ? NULL
8644 : sec->next == NULL ? toc
8645 : sec->next == toc && toc->next ? toc->next
8646 : sec->next))
8647 {
8648 int repeat;
8649
8650 if (sec->reloc_count == 0
8651 || discarded_section (sec)
8652 || get_opd_info (sec)
8653 || (sec->flags & SEC_ALLOC) == 0
8654 || (sec->flags & SEC_DEBUGGING) != 0)
8655 continue;
8656
8657 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8658 info->keep_memory);
8659 if (relstart == NULL)
8660 {
8661 free (used);
8662 goto error_ret;
8663 }
8664
8665 /* Mark toc entries referenced as used. */
8666 do
8667 {
8668 repeat = 0;
8669 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8670 {
8671 enum elf_ppc64_reloc_type r_type;
8672 unsigned long r_symndx;
8673 asection *sym_sec;
8674 struct elf_link_hash_entry *h;
8675 Elf_Internal_Sym *sym;
8676 bfd_vma val;
8677
8678 r_type = ELF64_R_TYPE (rel->r_info);
8679 switch (r_type)
8680 {
8681 case R_PPC64_TOC16:
8682 case R_PPC64_TOC16_LO:
8683 case R_PPC64_TOC16_HI:
8684 case R_PPC64_TOC16_HA:
8685 case R_PPC64_TOC16_DS:
8686 case R_PPC64_TOC16_LO_DS:
8687 /* In case we're taking addresses of toc entries. */
8688 case R_PPC64_ADDR64:
8689 break;
8690
8691 default:
8692 continue;
8693 }
8694
8695 r_symndx = ELF64_R_SYM (rel->r_info);
8696 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8697 r_symndx, ibfd))
8698 {
8699 free (used);
8700 goto error_ret;
8701 }
8702
8703 if (sym_sec != toc)
8704 continue;
8705
8706 if (h != NULL)
8707 val = h->root.u.def.value;
8708 else
8709 val = sym->st_value;
8710 val += rel->r_addend;
8711
8712 if (val >= toc->size)
8713 continue;
8714
8715 if ((skip[val >> 3] & can_optimize) != 0)
8716 {
8717 bfd_vma off;
8718 unsigned char opc;
8719
8720 switch (r_type)
8721 {
8722 case R_PPC64_TOC16_HA:
8723 break;
8724
8725 case R_PPC64_TOC16_LO_DS:
8726 off = rel->r_offset;
8727 off += (bfd_big_endian (ibfd) ? -2 : 3);
8728 if (!bfd_get_section_contents (ibfd, sec, &opc,
8729 off, 1))
8730 {
8731 free (used);
8732 goto error_ret;
8733 }
8734 if ((opc & (0x3f << 2)) == (58u << 2))
8735 break;
8736 /* Fall through. */
8737
8738 default:
8739 /* Wrong sort of reloc, or not a ld. We may
8740 as well clear ref_from_discarded too. */
8741 skip[val >> 3] = 0;
8742 }
8743 }
8744
8745 if (sec != toc)
8746 used[val >> 3] = 1;
8747 /* For the toc section, we only mark as used if this
8748 entry itself isn't unused. */
8749 else if ((used[rel->r_offset >> 3]
8750 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8751 && !used[val >> 3])
8752 {
8753 /* Do all the relocs again, to catch reference
8754 chains. */
8755 repeat = 1;
8756 used[val >> 3] = 1;
8757 }
8758 }
8759 }
8760 while (repeat);
8761
8762 if (elf_section_data (sec)->relocs != relstart)
8763 free (relstart);
8764 }
8765
8766 /* Merge the used and skip arrays. Assume that TOC
8767 doublewords not appearing as either used or unused belong
8768 to an entry more than one doubleword in size. */
8769 for (drop = skip, keep = used, last = 0, some_unused = 0;
8770 drop < skip + (toc->size + 7) / 8;
8771 ++drop, ++keep)
8772 {
8773 if (*keep)
8774 {
8775 *drop &= ~ref_from_discarded;
8776 if ((*drop & can_optimize) != 0)
8777 some_unused = 1;
8778 last = 0;
8779 }
8780 else if ((*drop & ref_from_discarded) != 0)
8781 {
8782 some_unused = 1;
8783 last = ref_from_discarded;
8784 }
8785 else
8786 *drop = last;
8787 }
8788
8789 free (used);
8790
8791 if (some_unused)
8792 {
8793 bfd_byte *contents, *src;
8794 unsigned long off;
8795 Elf_Internal_Sym *sym;
8796 bfd_boolean local_toc_syms = FALSE;
8797
8798 /* Shuffle the toc contents, and at the same time convert the
8799 skip array from booleans into offsets. */
8800 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8801 goto error_ret;
8802
8803 elf_section_data (toc)->this_hdr.contents = contents;
8804
8805 for (src = contents, off = 0, drop = skip;
8806 src < contents + toc->size;
8807 src += 8, ++drop)
8808 {
8809 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8810 off += 8;
8811 else if (off != 0)
8812 {
8813 *drop = off;
8814 memcpy (src - off, src, 8);
8815 }
8816 }
8817 *drop = off;
8818 toc->rawsize = toc->size;
8819 toc->size = src - contents - off;
8820
8821 /* Adjust addends for relocs against the toc section sym,
8822 and optimize any accesses we can. */
8823 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8824 {
8825 if (sec->reloc_count == 0
8826 || discarded_section (sec))
8827 continue;
8828
8829 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8830 info->keep_memory);
8831 if (relstart == NULL)
8832 goto error_ret;
8833
8834 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8835 {
8836 enum elf_ppc64_reloc_type r_type;
8837 unsigned long r_symndx;
8838 asection *sym_sec;
8839 struct elf_link_hash_entry *h;
8840 bfd_vma val;
8841
8842 r_type = ELF64_R_TYPE (rel->r_info);
8843 switch (r_type)
8844 {
8845 default:
8846 continue;
8847
8848 case R_PPC64_TOC16:
8849 case R_PPC64_TOC16_LO:
8850 case R_PPC64_TOC16_HI:
8851 case R_PPC64_TOC16_HA:
8852 case R_PPC64_TOC16_DS:
8853 case R_PPC64_TOC16_LO_DS:
8854 case R_PPC64_ADDR64:
8855 break;
8856 }
8857
8858 r_symndx = ELF64_R_SYM (rel->r_info);
8859 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8860 r_symndx, ibfd))
8861 goto error_ret;
8862
8863 if (sym_sec != toc)
8864 continue;
8865
8866 if (h != NULL)
8867 val = h->root.u.def.value;
8868 else
8869 {
8870 val = sym->st_value;
8871 if (val != 0)
8872 local_toc_syms = TRUE;
8873 }
8874
8875 val += rel->r_addend;
8876
8877 if (val > toc->rawsize)
8878 val = toc->rawsize;
8879 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8880 continue;
8881 else if ((skip[val >> 3] & can_optimize) != 0)
8882 {
8883 Elf_Internal_Rela *tocrel
8884 = toc_relocs + (skip[val >> 3] >> 2);
8885 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8886
8887 switch (r_type)
8888 {
8889 case R_PPC64_TOC16_HA:
8890 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8891 break;
8892
8893 case R_PPC64_TOC16_LO_DS:
8894 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8895 break;
8896
8897 default:
8898 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8899 ppc_howto_init ();
8900 info->callbacks->einfo
8901 /* xgettext:c-format */
8902 (_("%H: %s references "
8903 "optimized away TOC entry\n"),
8904 ibfd, sec, rel->r_offset,
8905 ppc64_elf_howto_table[r_type]->name);
8906 bfd_set_error (bfd_error_bad_value);
8907 goto error_ret;
8908 }
8909 rel->r_addend = tocrel->r_addend;
8910 elf_section_data (sec)->relocs = relstart;
8911 continue;
8912 }
8913
8914 if (h != NULL || sym->st_value != 0)
8915 continue;
8916
8917 rel->r_addend -= skip[val >> 3];
8918 elf_section_data (sec)->relocs = relstart;
8919 }
8920
8921 if (elf_section_data (sec)->relocs != relstart)
8922 free (relstart);
8923 }
8924
8925 /* We shouldn't have local or global symbols defined in the TOC,
8926 but handle them anyway. */
8927 if (local_syms != NULL)
8928 for (sym = local_syms;
8929 sym < local_syms + symtab_hdr->sh_info;
8930 ++sym)
8931 if (sym->st_value != 0
8932 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8933 {
8934 unsigned long i;
8935
8936 if (sym->st_value > toc->rawsize)
8937 i = toc->rawsize >> 3;
8938 else
8939 i = sym->st_value >> 3;
8940
8941 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8942 {
8943 if (local_toc_syms)
8944 _bfd_error_handler
8945 (_("%s defined on removed toc entry"),
8946 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8947 do
8948 ++i;
8949 while ((skip[i] & (ref_from_discarded | can_optimize)));
8950 sym->st_value = (bfd_vma) i << 3;
8951 }
8952
8953 sym->st_value -= skip[i];
8954 symtab_hdr->contents = (unsigned char *) local_syms;
8955 }
8956
8957 /* Adjust any global syms defined in this toc input section. */
8958 if (toc_inf.global_toc_syms)
8959 {
8960 toc_inf.toc = toc;
8961 toc_inf.skip = skip;
8962 toc_inf.global_toc_syms = FALSE;
8963 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8964 &toc_inf);
8965 }
8966
8967 if (toc->reloc_count != 0)
8968 {
8969 Elf_Internal_Shdr *rel_hdr;
8970 Elf_Internal_Rela *wrel;
8971 bfd_size_type sz;
8972
8973 /* Remove unused toc relocs, and adjust those we keep. */
8974 if (toc_relocs == NULL)
8975 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8976 info->keep_memory);
8977 if (toc_relocs == NULL)
8978 goto error_ret;
8979
8980 wrel = toc_relocs;
8981 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8982 if ((skip[rel->r_offset >> 3]
8983 & (ref_from_discarded | can_optimize)) == 0)
8984 {
8985 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8986 wrel->r_info = rel->r_info;
8987 wrel->r_addend = rel->r_addend;
8988 ++wrel;
8989 }
8990 else if (!dec_dynrel_count (rel->r_info, toc, info,
8991 &local_syms, NULL, NULL))
8992 goto error_ret;
8993
8994 elf_section_data (toc)->relocs = toc_relocs;
8995 toc->reloc_count = wrel - toc_relocs;
8996 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8997 sz = rel_hdr->sh_entsize;
8998 rel_hdr->sh_size = toc->reloc_count * sz;
8999 }
9000 }
9001 else if (toc_relocs != NULL
9002 && elf_section_data (toc)->relocs != toc_relocs)
9003 free (toc_relocs);
9004
9005 if (local_syms != NULL
9006 && symtab_hdr->contents != (unsigned char *) local_syms)
9007 {
9008 if (!info->keep_memory)
9009 free (local_syms);
9010 else
9011 symtab_hdr->contents = (unsigned char *) local_syms;
9012 }
9013 free (skip);
9014 }
9015
9016 /* Look for cases where we can change an indirect GOT access to
9017 a GOT relative or PC relative access, possibly reducing the
9018 number of GOT entries. */
9019 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9020 {
9021 asection *sec;
9022 Elf_Internal_Shdr *symtab_hdr;
9023 Elf_Internal_Sym *local_syms;
9024 Elf_Internal_Rela *relstart, *rel;
9025 bfd_vma got;
9026
9027 if (!is_ppc64_elf (ibfd))
9028 continue;
9029
9030 if (!ppc64_elf_tdata (ibfd)->has_optrel)
9031 continue;
9032
9033 sec = ppc64_elf_tdata (ibfd)->got;
9034 got = 0;
9035 if (sec != NULL)
9036 got = sec->output_section->vma + sec->output_offset + 0x8000;
9037
9038 local_syms = NULL;
9039 symtab_hdr = &elf_symtab_hdr (ibfd);
9040
9041 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9042 {
9043 if (sec->reloc_count == 0
9044 || !ppc64_elf_section_data (sec)->has_optrel
9045 || discarded_section (sec))
9046 continue;
9047
9048 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9049 info->keep_memory);
9050 if (relstart == NULL)
9051 {
9052 got_error_ret:
9053 if (local_syms != NULL
9054 && symtab_hdr->contents != (unsigned char *) local_syms)
9055 free (local_syms);
9056 if (sec != NULL
9057 && relstart != NULL
9058 && elf_section_data (sec)->relocs != relstart)
9059 free (relstart);
9060 return FALSE;
9061 }
9062
9063 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9064 {
9065 enum elf_ppc64_reloc_type r_type;
9066 unsigned long r_symndx;
9067 Elf_Internal_Sym *sym;
9068 asection *sym_sec;
9069 struct elf_link_hash_entry *h;
9070 struct got_entry *ent;
9071 bfd_vma val, pc;
9072 unsigned char buf[8];
9073 unsigned int insn;
9074 enum {no_check, check_lo, check_ha} insn_check;
9075
9076 r_type = ELF64_R_TYPE (rel->r_info);
9077 switch (r_type)
9078 {
9079 default:
9080 insn_check = no_check;
9081 break;
9082
9083 case R_PPC64_PLT16_HA:
9084 case R_PPC64_GOT_TLSLD16_HA:
9085 case R_PPC64_GOT_TLSGD16_HA:
9086 case R_PPC64_GOT_TPREL16_HA:
9087 case R_PPC64_GOT_DTPREL16_HA:
9088 case R_PPC64_GOT16_HA:
9089 case R_PPC64_TOC16_HA:
9090 insn_check = check_ha;
9091 break;
9092
9093 case R_PPC64_PLT16_LO:
9094 case R_PPC64_PLT16_LO_DS:
9095 case R_PPC64_GOT_TLSLD16_LO:
9096 case R_PPC64_GOT_TLSGD16_LO:
9097 case R_PPC64_GOT_TPREL16_LO_DS:
9098 case R_PPC64_GOT_DTPREL16_LO_DS:
9099 case R_PPC64_GOT16_LO:
9100 case R_PPC64_GOT16_LO_DS:
9101 case R_PPC64_TOC16_LO:
9102 case R_PPC64_TOC16_LO_DS:
9103 insn_check = check_lo;
9104 break;
9105 }
9106
9107 if (insn_check != no_check)
9108 {
9109 bfd_vma off = rel->r_offset & ~3;
9110
9111 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9112 goto got_error_ret;
9113
9114 insn = bfd_get_32 (ibfd, buf);
9115 if (insn_check == check_lo
9116 ? !ok_lo_toc_insn (insn, r_type)
9117 : ((insn & ((0x3f << 26) | 0x1f << 16))
9118 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9119 {
9120 char str[12];
9121
9122 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9123 sprintf (str, "%#08x", insn);
9124 info->callbacks->einfo
9125 /* xgettext:c-format */
9126 (_("%H: got/toc optimization is not supported for"
9127 " %s instruction\n"),
9128 ibfd, sec, rel->r_offset & ~3, str);
9129 continue;
9130 }
9131 }
9132
9133 switch (r_type)
9134 {
9135 /* Note that we don't delete GOT entries for
9136 R_PPC64_GOT16_DS since we'd need a lot more
9137 analysis. For starters, the preliminary layout is
9138 before the GOT, PLT, dynamic sections and stubs are
9139 laid out. Then we'd need to allow for changes in
9140 distance between sections caused by alignment. */
9141 default:
9142 continue;
9143
9144 case R_PPC64_GOT16_HA:
9145 case R_PPC64_GOT16_LO_DS:
9146 case R_PPC64_GOT_PCREL34:
9147 break;
9148 }
9149
9150 r_symndx = ELF64_R_SYM (rel->r_info);
9151 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9152 r_symndx, ibfd))
9153 goto got_error_ret;
9154
9155 if (sym_sec == NULL
9156 || sym_sec->output_section == NULL
9157 || discarded_section (sym_sec))
9158 continue;
9159
9160 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9161 continue;
9162
9163 if (h != NULL)
9164 val = h->root.u.def.value;
9165 else
9166 val = sym->st_value;
9167 val += rel->r_addend;
9168 val += sym_sec->output_section->vma + sym_sec->output_offset;
9169
9170 /* Fudge factor to allow for the fact that the preliminary layout
9171 isn't exact. Reduce limits by this factor. */
9172 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9173
9174 switch (r_type)
9175 {
9176 default:
9177 continue;
9178
9179 case R_PPC64_GOT16_HA:
9180 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9181 >= LIMIT_ADJUST (0x100000000ULL))
9182 continue;
9183
9184 if (!bfd_get_section_contents (ibfd, sec, buf,
9185 rel->r_offset & ~3, 4))
9186 goto got_error_ret;
9187 insn = bfd_get_32 (ibfd, buf);
9188 if (((insn & ((0x3f << 26) | 0x1f << 16))
9189 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9190 continue;
9191 break;
9192
9193 case R_PPC64_GOT16_LO_DS:
9194 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9195 >= LIMIT_ADJUST (0x100000000ULL))
9196 continue;
9197 if (!bfd_get_section_contents (ibfd, sec, buf,
9198 rel->r_offset & ~3, 4))
9199 goto got_error_ret;
9200 insn = bfd_get_32 (ibfd, buf);
9201 if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9202 continue;
9203 break;
9204
9205 case R_PPC64_GOT_PCREL34:
9206 pc = rel->r_offset;
9207 pc += sec->output_section->vma + sec->output_offset;
9208 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9209 >= LIMIT_ADJUST (1ULL << 34))
9210 continue;
9211 if (!bfd_get_section_contents (ibfd, sec, buf,
9212 rel->r_offset & ~3, 8))
9213 goto got_error_ret;
9214 insn = bfd_get_32 (ibfd, buf);
9215 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9216 continue;
9217 insn = bfd_get_32 (ibfd, buf + 4);
9218 if ((insn & (0x3f << 26)) != 57u << 26)
9219 continue;
9220 break;
9221 }
9222 #undef LIMIT_ADJUST
9223
9224 if (h != NULL)
9225 ent = h->got.glist;
9226 else
9227 {
9228 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9229 ent = local_got_ents[r_symndx];
9230 }
9231 for (; ent != NULL; ent = ent->next)
9232 if (ent->addend == rel->r_addend
9233 && ent->owner == ibfd
9234 && ent->tls_type == 0)
9235 break;
9236 BFD_ASSERT (ent && ent->got.refcount > 0);
9237 ent->got.refcount -= 1;
9238 }
9239
9240 if (elf_section_data (sec)->relocs != relstart)
9241 free (relstart);
9242 }
9243
9244 if (local_syms != NULL
9245 && symtab_hdr->contents != (unsigned char *) local_syms)
9246 {
9247 if (!info->keep_memory)
9248 free (local_syms);
9249 else
9250 symtab_hdr->contents = (unsigned char *) local_syms;
9251 }
9252 }
9253
9254 return TRUE;
9255 }
9256
9257 /* Return true iff input section I references the TOC using
9258 instructions limited to +/-32k offsets. */
9259
9260 bfd_boolean
9261 ppc64_elf_has_small_toc_reloc (asection *i)
9262 {
9263 return (is_ppc64_elf (i->owner)
9264 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9265 }
9266
9267 /* Allocate space for one GOT entry. */
9268
9269 static void
9270 allocate_got (struct elf_link_hash_entry *h,
9271 struct bfd_link_info *info,
9272 struct got_entry *gent)
9273 {
9274 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9275 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9276 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9277 ? 16 : 8);
9278 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9279 ? 2 : 1) * sizeof (Elf64_External_Rela);
9280 asection *got = ppc64_elf_tdata (gent->owner)->got;
9281
9282 gent->got.offset = got->size;
9283 got->size += entsize;
9284
9285 if (h->type == STT_GNU_IFUNC)
9286 {
9287 htab->elf.irelplt->size += rentsize;
9288 htab->got_reli_size += rentsize;
9289 }
9290 else if (((bfd_link_pic (info)
9291 && !(gent->tls_type != 0
9292 && bfd_link_executable (info)
9293 && SYMBOL_REFERENCES_LOCAL (info, h)))
9294 || (htab->elf.dynamic_sections_created
9295 && h->dynindx != -1
9296 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9297 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9298 {
9299 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9300 relgot->size += rentsize;
9301 }
9302 }
9303
9304 /* This function merges got entries in the same toc group. */
9305
9306 static void
9307 merge_got_entries (struct got_entry **pent)
9308 {
9309 struct got_entry *ent, *ent2;
9310
9311 for (ent = *pent; ent != NULL; ent = ent->next)
9312 if (!ent->is_indirect)
9313 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9314 if (!ent2->is_indirect
9315 && ent2->addend == ent->addend
9316 && ent2->tls_type == ent->tls_type
9317 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9318 {
9319 ent2->is_indirect = TRUE;
9320 ent2->got.ent = ent;
9321 }
9322 }
9323
9324 /* If H is undefined, make it dynamic if that makes sense. */
9325
9326 static bfd_boolean
9327 ensure_undef_dynamic (struct bfd_link_info *info,
9328 struct elf_link_hash_entry *h)
9329 {
9330 struct elf_link_hash_table *htab = elf_hash_table (info);
9331
9332 if (htab->dynamic_sections_created
9333 && ((info->dynamic_undefined_weak != 0
9334 && h->root.type == bfd_link_hash_undefweak)
9335 || h->root.type == bfd_link_hash_undefined)
9336 && h->dynindx == -1
9337 && !h->forced_local
9338 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9339 return bfd_elf_link_record_dynamic_symbol (info, h);
9340 return TRUE;
9341 }
9342
9343 /* Allocate space in .plt, .got and associated reloc sections for
9344 dynamic relocs. */
9345
9346 static bfd_boolean
9347 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9348 {
9349 struct bfd_link_info *info;
9350 struct ppc_link_hash_table *htab;
9351 asection *s;
9352 struct ppc_link_hash_entry *eh;
9353 struct got_entry **pgent, *gent;
9354
9355 if (h->root.type == bfd_link_hash_indirect)
9356 return TRUE;
9357
9358 info = (struct bfd_link_info *) inf;
9359 htab = ppc_hash_table (info);
9360 if (htab == NULL)
9361 return FALSE;
9362
9363 eh = (struct ppc_link_hash_entry *) h;
9364 /* Run through the TLS GD got entries first if we're changing them
9365 to TPREL. */
9366 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9367 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9368 if (gent->got.refcount > 0
9369 && (gent->tls_type & TLS_GD) != 0)
9370 {
9371 /* This was a GD entry that has been converted to TPREL. If
9372 there happens to be a TPREL entry we can use that one. */
9373 struct got_entry *ent;
9374 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9375 if (ent->got.refcount > 0
9376 && (ent->tls_type & TLS_TPREL) != 0
9377 && ent->addend == gent->addend
9378 && ent->owner == gent->owner)
9379 {
9380 gent->got.refcount = 0;
9381 break;
9382 }
9383
9384 /* If not, then we'll be using our own TPREL entry. */
9385 if (gent->got.refcount != 0)
9386 gent->tls_type = TLS_TLS | TLS_TPREL;
9387 }
9388
9389 /* Remove any list entry that won't generate a word in the GOT before
9390 we call merge_got_entries. Otherwise we risk merging to empty
9391 entries. */
9392 pgent = &h->got.glist;
9393 while ((gent = *pgent) != NULL)
9394 if (gent->got.refcount > 0)
9395 {
9396 if ((gent->tls_type & TLS_LD) != 0
9397 && SYMBOL_REFERENCES_LOCAL (info, h))
9398 {
9399 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9400 *pgent = gent->next;
9401 }
9402 else
9403 pgent = &gent->next;
9404 }
9405 else
9406 *pgent = gent->next;
9407
9408 if (!htab->do_multi_toc)
9409 merge_got_entries (&h->got.glist);
9410
9411 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9412 if (!gent->is_indirect)
9413 {
9414 /* Ensure we catch all the cases where this symbol should
9415 be made dynamic. */
9416 if (!ensure_undef_dynamic (info, h))
9417 return FALSE;
9418
9419 if (!is_ppc64_elf (gent->owner))
9420 abort ();
9421
9422 allocate_got (h, info, gent);
9423 }
9424
9425 /* If no dynamic sections we can't have dynamic relocs, except for
9426 IFUNCs which are handled even in static executables. */
9427 if (!htab->elf.dynamic_sections_created
9428 && h->type != STT_GNU_IFUNC)
9429 eh->dyn_relocs = NULL;
9430
9431 /* Discard relocs on undefined symbols that must be local. */
9432 else if (h->root.type == bfd_link_hash_undefined
9433 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9434 eh->dyn_relocs = NULL;
9435
9436 /* Also discard relocs on undefined weak syms with non-default
9437 visibility, or when dynamic_undefined_weak says so. */
9438 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9439 eh->dyn_relocs = NULL;
9440
9441 if (eh->dyn_relocs != NULL)
9442 {
9443 struct elf_dyn_relocs *p, **pp;
9444
9445 /* In the shared -Bsymbolic case, discard space allocated for
9446 dynamic pc-relative relocs against symbols which turn out to
9447 be defined in regular objects. For the normal shared case,
9448 discard space for relocs that have become local due to symbol
9449 visibility changes. */
9450 if (bfd_link_pic (info))
9451 {
9452 /* Relocs that use pc_count are those that appear on a call
9453 insn, or certain REL relocs (see must_be_dyn_reloc) that
9454 can be generated via assembly. We want calls to
9455 protected symbols to resolve directly to the function
9456 rather than going via the plt. If people want function
9457 pointer comparisons to work as expected then they should
9458 avoid writing weird assembly. */
9459 if (SYMBOL_CALLS_LOCAL (info, h))
9460 {
9461 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9462 {
9463 p->count -= p->pc_count;
9464 p->pc_count = 0;
9465 if (p->count == 0)
9466 *pp = p->next;
9467 else
9468 pp = &p->next;
9469 }
9470 }
9471
9472 if (eh->dyn_relocs != NULL)
9473 {
9474 /* Ensure we catch all the cases where this symbol
9475 should be made dynamic. */
9476 if (!ensure_undef_dynamic (info, h))
9477 return FALSE;
9478 }
9479 }
9480
9481 /* For a fixed position executable, discard space for
9482 relocs against symbols which are not dynamic. */
9483 else if (h->type != STT_GNU_IFUNC)
9484 {
9485 if (h->dynamic_adjusted
9486 && !h->def_regular
9487 && !ELF_COMMON_DEF_P (h))
9488 {
9489 /* Ensure we catch all the cases where this symbol
9490 should be made dynamic. */
9491 if (!ensure_undef_dynamic (info, h))
9492 return FALSE;
9493
9494 /* But if that didn't work out, discard dynamic relocs. */
9495 if (h->dynindx == -1)
9496 eh->dyn_relocs = NULL;
9497 }
9498 else
9499 eh->dyn_relocs = NULL;
9500 }
9501
9502 /* Finally, allocate space. */
9503 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9504 {
9505 asection *sreloc = elf_section_data (p->sec)->sreloc;
9506 if (eh->elf.type == STT_GNU_IFUNC)
9507 sreloc = htab->elf.irelplt;
9508 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9509 }
9510 }
9511
9512 /* We might need a PLT entry when the symbol
9513 a) is dynamic, or
9514 b) is an ifunc, or
9515 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9516 d) has plt16 relocs and we are linking statically. */
9517 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9518 || h->type == STT_GNU_IFUNC
9519 || (h->needs_plt && h->dynamic_adjusted)
9520 || (h->needs_plt
9521 && h->def_regular
9522 && !htab->elf.dynamic_sections_created
9523 && !htab->can_convert_all_inline_plt
9524 && (((struct ppc_link_hash_entry *) h)->tls_mask
9525 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
9526 {
9527 struct plt_entry *pent;
9528 bfd_boolean doneone = FALSE;
9529 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9530 if (pent->plt.refcount > 0)
9531 {
9532 if (!htab->elf.dynamic_sections_created
9533 || h->dynindx == -1)
9534 {
9535 if (h->type == STT_GNU_IFUNC)
9536 {
9537 s = htab->elf.iplt;
9538 pent->plt.offset = s->size;
9539 s->size += PLT_ENTRY_SIZE (htab);
9540 s = htab->elf.irelplt;
9541 }
9542 else
9543 {
9544 s = htab->pltlocal;
9545 pent->plt.offset = s->size;
9546 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9547 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9548 }
9549 }
9550 else
9551 {
9552 /* If this is the first .plt entry, make room for the special
9553 first entry. */
9554 s = htab->elf.splt;
9555 if (s->size == 0)
9556 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9557
9558 pent->plt.offset = s->size;
9559
9560 /* Make room for this entry. */
9561 s->size += PLT_ENTRY_SIZE (htab);
9562
9563 /* Make room for the .glink code. */
9564 s = htab->glink;
9565 if (s->size == 0)
9566 s->size += GLINK_PLTRESOLVE_SIZE (htab);
9567 if (htab->opd_abi)
9568 {
9569 /* We need bigger stubs past index 32767. */
9570 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9571 s->size += 4;
9572 s->size += 2*4;
9573 }
9574 else
9575 s->size += 4;
9576
9577 /* We also need to make an entry in the .rela.plt section. */
9578 s = htab->elf.srelplt;
9579 }
9580 if (s != NULL)
9581 s->size += sizeof (Elf64_External_Rela);
9582 doneone = TRUE;
9583 }
9584 else
9585 pent->plt.offset = (bfd_vma) -1;
9586 if (!doneone)
9587 {
9588 h->plt.plist = NULL;
9589 h->needs_plt = 0;
9590 }
9591 }
9592 else
9593 {
9594 h->plt.plist = NULL;
9595 h->needs_plt = 0;
9596 }
9597
9598 return TRUE;
9599 }
9600
9601 #define PPC_LO(v) ((v) & 0xffff)
9602 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9603 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9604 #define D34(v) \
9605 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9606 #define HA34(v) ((v + (1ULL << 33)) >> 34)
9607
9608 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9609 to set up space for global entry stubs. These are put in glink,
9610 after the branch table. */
9611
9612 static bfd_boolean
9613 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9614 {
9615 struct bfd_link_info *info;
9616 struct ppc_link_hash_table *htab;
9617 struct plt_entry *pent;
9618 asection *s, *plt;
9619
9620 if (h->root.type == bfd_link_hash_indirect)
9621 return TRUE;
9622
9623 if (!h->pointer_equality_needed)
9624 return TRUE;
9625
9626 if (h->def_regular)
9627 return TRUE;
9628
9629 info = inf;
9630 htab = ppc_hash_table (info);
9631 if (htab == NULL)
9632 return FALSE;
9633
9634 s = htab->global_entry;
9635 plt = htab->elf.splt;
9636 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9637 if (pent->plt.offset != (bfd_vma) -1
9638 && pent->addend == 0)
9639 {
9640 /* For ELFv2, if this symbol is not defined in a regular file
9641 and we are not generating a shared library or pie, then we
9642 need to define the symbol in the executable on a call stub.
9643 This is to avoid text relocations. */
9644 bfd_vma off, stub_align, stub_off, stub_size;
9645 unsigned int align_power;
9646
9647 stub_size = 16;
9648 stub_off = s->size;
9649 if (htab->params->plt_stub_align >= 0)
9650 align_power = htab->params->plt_stub_align;
9651 else
9652 align_power = -htab->params->plt_stub_align;
9653 /* Setting section alignment is delayed until we know it is
9654 non-empty. Otherwise the .text output section will be
9655 aligned at least to plt_stub_align even when no global
9656 entry stubs are needed. */
9657 if (s->alignment_power < align_power)
9658 s->alignment_power = align_power;
9659 stub_align = (bfd_vma) 1 << align_power;
9660 if (htab->params->plt_stub_align >= 0
9661 || ((((stub_off + stub_size - 1) & -stub_align)
9662 - (stub_off & -stub_align))
9663 > ((stub_size - 1) & -stub_align)))
9664 stub_off = (stub_off + stub_align - 1) & -stub_align;
9665 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9666 off -= stub_off + s->output_offset + s->output_section->vma;
9667 /* Note that for --plt-stub-align negative we have a possible
9668 dependency between stub offset and size. Break that
9669 dependency by assuming the max stub size when calculating
9670 the stub offset. */
9671 if (PPC_HA (off) == 0)
9672 stub_size -= 4;
9673 h->root.type = bfd_link_hash_defined;
9674 h->root.u.def.section = s;
9675 h->root.u.def.value = stub_off;
9676 s->size = stub_off + stub_size;
9677 break;
9678 }
9679 return TRUE;
9680 }
9681
9682 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9683 read-only sections. */
9684
9685 static bfd_boolean
9686 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
9687 {
9688 asection *sec;
9689
9690 if (h->root.type == bfd_link_hash_indirect)
9691 return TRUE;
9692
9693 sec = readonly_dynrelocs (h);
9694 if (sec != NULL)
9695 {
9696 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9697
9698 info->flags |= DF_TEXTREL;
9699 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9700 " in read-only section `%pA'\n"),
9701 sec->owner, h->root.root.string, sec);
9702
9703 /* Not an error, just cut short the traversal. */
9704 return FALSE;
9705 }
9706 return TRUE;
9707 }
9708
9709 /* Set the sizes of the dynamic sections. */
9710
9711 static bfd_boolean
9712 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9713 struct bfd_link_info *info)
9714 {
9715 struct ppc_link_hash_table *htab;
9716 bfd *dynobj;
9717 asection *s;
9718 bfd_boolean relocs;
9719 bfd *ibfd;
9720 struct got_entry *first_tlsld;
9721
9722 htab = ppc_hash_table (info);
9723 if (htab == NULL)
9724 return FALSE;
9725
9726 dynobj = htab->elf.dynobj;
9727 if (dynobj == NULL)
9728 abort ();
9729
9730 if (htab->elf.dynamic_sections_created)
9731 {
9732 /* Set the contents of the .interp section to the interpreter. */
9733 if (bfd_link_executable (info) && !info->nointerp)
9734 {
9735 s = bfd_get_linker_section (dynobj, ".interp");
9736 if (s == NULL)
9737 abort ();
9738 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9739 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9740 }
9741 }
9742
9743 /* Set up .got offsets for local syms, and space for local dynamic
9744 relocs. */
9745 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9746 {
9747 struct got_entry **lgot_ents;
9748 struct got_entry **end_lgot_ents;
9749 struct plt_entry **local_plt;
9750 struct plt_entry **end_local_plt;
9751 unsigned char *lgot_masks;
9752 bfd_size_type locsymcount;
9753 Elf_Internal_Shdr *symtab_hdr;
9754
9755 if (!is_ppc64_elf (ibfd))
9756 continue;
9757
9758 for (s = ibfd->sections; s != NULL; s = s->next)
9759 {
9760 struct ppc_dyn_relocs *p;
9761
9762 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9763 {
9764 if (!bfd_is_abs_section (p->sec)
9765 && bfd_is_abs_section (p->sec->output_section))
9766 {
9767 /* Input section has been discarded, either because
9768 it is a copy of a linkonce section or due to
9769 linker script /DISCARD/, so we'll be discarding
9770 the relocs too. */
9771 }
9772 else if (p->count != 0)
9773 {
9774 asection *srel = elf_section_data (p->sec)->sreloc;
9775 if (p->ifunc)
9776 srel = htab->elf.irelplt;
9777 srel->size += p->count * sizeof (Elf64_External_Rela);
9778 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9779 info->flags |= DF_TEXTREL;
9780 }
9781 }
9782 }
9783
9784 lgot_ents = elf_local_got_ents (ibfd);
9785 if (!lgot_ents)
9786 continue;
9787
9788 symtab_hdr = &elf_symtab_hdr (ibfd);
9789 locsymcount = symtab_hdr->sh_info;
9790 end_lgot_ents = lgot_ents + locsymcount;
9791 local_plt = (struct plt_entry **) end_lgot_ents;
9792 end_local_plt = local_plt + locsymcount;
9793 lgot_masks = (unsigned char *) end_local_plt;
9794 s = ppc64_elf_tdata (ibfd)->got;
9795 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9796 {
9797 struct got_entry **pent, *ent;
9798
9799 pent = lgot_ents;
9800 while ((ent = *pent) != NULL)
9801 if (ent->got.refcount > 0)
9802 {
9803 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9804 {
9805 ppc64_tlsld_got (ibfd)->got.refcount += 1;
9806 *pent = ent->next;
9807 }
9808 else
9809 {
9810 unsigned int ent_size = 8;
9811 unsigned int rel_size = sizeof (Elf64_External_Rela);
9812
9813 ent->got.offset = s->size;
9814 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9815 {
9816 ent_size *= 2;
9817 rel_size *= 2;
9818 }
9819 s->size += ent_size;
9820 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9821 {
9822 htab->elf.irelplt->size += rel_size;
9823 htab->got_reli_size += rel_size;
9824 }
9825 else if (bfd_link_pic (info)
9826 && !(ent->tls_type != 0
9827 && bfd_link_executable (info)))
9828 {
9829 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9830 srel->size += rel_size;
9831 }
9832 pent = &ent->next;
9833 }
9834 }
9835 else
9836 *pent = ent->next;
9837 }
9838
9839 /* Allocate space for plt calls to local syms. */
9840 lgot_masks = (unsigned char *) end_local_plt;
9841 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
9842 {
9843 struct plt_entry *ent;
9844
9845 for (ent = *local_plt; ent != NULL; ent = ent->next)
9846 if (ent->plt.refcount > 0)
9847 {
9848 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9849 {
9850 s = htab->elf.iplt;
9851 ent->plt.offset = s->size;
9852 s->size += PLT_ENTRY_SIZE (htab);
9853 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9854 }
9855 else if (htab->can_convert_all_inline_plt
9856 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
9857 ent->plt.offset = (bfd_vma) -1;
9858 else
9859 {
9860 s = htab->pltlocal;
9861 ent->plt.offset = s->size;
9862 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9863 if (bfd_link_pic (info))
9864 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9865 }
9866 }
9867 else
9868 ent->plt.offset = (bfd_vma) -1;
9869 }
9870 }
9871
9872 /* Allocate global sym .plt and .got entries, and space for global
9873 sym dynamic relocs. */
9874 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9875
9876 if (!htab->opd_abi && !bfd_link_pic (info))
9877 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9878
9879 first_tlsld = NULL;
9880 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9881 {
9882 struct got_entry *ent;
9883
9884 if (!is_ppc64_elf (ibfd))
9885 continue;
9886
9887 ent = ppc64_tlsld_got (ibfd);
9888 if (ent->got.refcount > 0)
9889 {
9890 if (!htab->do_multi_toc && first_tlsld != NULL)
9891 {
9892 ent->is_indirect = TRUE;
9893 ent->got.ent = first_tlsld;
9894 }
9895 else
9896 {
9897 if (first_tlsld == NULL)
9898 first_tlsld = ent;
9899 s = ppc64_elf_tdata (ibfd)->got;
9900 ent->got.offset = s->size;
9901 ent->owner = ibfd;
9902 s->size += 16;
9903 if (bfd_link_dll (info))
9904 {
9905 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9906 srel->size += sizeof (Elf64_External_Rela);
9907 }
9908 }
9909 }
9910 else
9911 ent->got.offset = (bfd_vma) -1;
9912 }
9913
9914 /* We now have determined the sizes of the various dynamic sections.
9915 Allocate memory for them. */
9916 relocs = FALSE;
9917 for (s = dynobj->sections; s != NULL; s = s->next)
9918 {
9919 if ((s->flags & SEC_LINKER_CREATED) == 0)
9920 continue;
9921
9922 if (s == htab->brlt || s == htab->relbrlt)
9923 /* These haven't been allocated yet; don't strip. */
9924 continue;
9925 else if (s == htab->elf.sgot
9926 || s == htab->elf.splt
9927 || s == htab->elf.iplt
9928 || s == htab->pltlocal
9929 || s == htab->glink
9930 || s == htab->global_entry
9931 || s == htab->elf.sdynbss
9932 || s == htab->elf.sdynrelro)
9933 {
9934 /* Strip this section if we don't need it; see the
9935 comment below. */
9936 }
9937 else if (s == htab->glink_eh_frame)
9938 {
9939 if (!bfd_is_abs_section (s->output_section))
9940 /* Not sized yet. */
9941 continue;
9942 }
9943 else if (CONST_STRNEQ (s->name, ".rela"))
9944 {
9945 if (s->size != 0)
9946 {
9947 if (s != htab->elf.srelplt)
9948 relocs = TRUE;
9949
9950 /* We use the reloc_count field as a counter if we need
9951 to copy relocs into the output file. */
9952 s->reloc_count = 0;
9953 }
9954 }
9955 else
9956 {
9957 /* It's not one of our sections, so don't allocate space. */
9958 continue;
9959 }
9960
9961 if (s->size == 0)
9962 {
9963 /* If we don't need this section, strip it from the
9964 output file. This is mostly to handle .rela.bss and
9965 .rela.plt. We must create both sections in
9966 create_dynamic_sections, because they must be created
9967 before the linker maps input sections to output
9968 sections. The linker does that before
9969 adjust_dynamic_symbol is called, and it is that
9970 function which decides whether anything needs to go
9971 into these sections. */
9972 s->flags |= SEC_EXCLUDE;
9973 continue;
9974 }
9975
9976 if (bfd_is_abs_section (s->output_section))
9977 _bfd_error_handler (_("warning: discarding dynamic section %s"),
9978 s->name);
9979
9980 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9981 continue;
9982
9983 /* Allocate memory for the section contents. We use bfd_zalloc
9984 here in case unused entries are not reclaimed before the
9985 section's contents are written out. This should not happen,
9986 but this way if it does we get a R_PPC64_NONE reloc in .rela
9987 sections instead of garbage.
9988 We also rely on the section contents being zero when writing
9989 the GOT and .dynrelro. */
9990 s->contents = bfd_zalloc (dynobj, s->size);
9991 if (s->contents == NULL)
9992 return FALSE;
9993 }
9994
9995 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9996 {
9997 if (!is_ppc64_elf (ibfd))
9998 continue;
9999
10000 s = ppc64_elf_tdata (ibfd)->got;
10001 if (s != NULL && s != htab->elf.sgot)
10002 {
10003 if (s->size == 0)
10004 s->flags |= SEC_EXCLUDE;
10005 else
10006 {
10007 s->contents = bfd_zalloc (ibfd, s->size);
10008 if (s->contents == NULL)
10009 return FALSE;
10010 }
10011 }
10012 s = ppc64_elf_tdata (ibfd)->relgot;
10013 if (s != NULL)
10014 {
10015 if (s->size == 0)
10016 s->flags |= SEC_EXCLUDE;
10017 else
10018 {
10019 s->contents = bfd_zalloc (ibfd, s->size);
10020 if (s->contents == NULL)
10021 return FALSE;
10022 relocs = TRUE;
10023 s->reloc_count = 0;
10024 }
10025 }
10026 }
10027
10028 if (htab->elf.dynamic_sections_created)
10029 {
10030 bfd_boolean tls_opt;
10031
10032 /* Add some entries to the .dynamic section. We fill in the
10033 values later, in ppc64_elf_finish_dynamic_sections, but we
10034 must add the entries now so that we get the correct size for
10035 the .dynamic section. The DT_DEBUG entry is filled in by the
10036 dynamic linker and used by the debugger. */
10037 #define add_dynamic_entry(TAG, VAL) \
10038 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10039
10040 if (bfd_link_executable (info))
10041 {
10042 if (!add_dynamic_entry (DT_DEBUG, 0))
10043 return FALSE;
10044 }
10045
10046 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10047 {
10048 if (!add_dynamic_entry (DT_PLTGOT, 0)
10049 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10050 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10051 || !add_dynamic_entry (DT_JMPREL, 0)
10052 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10053 return FALSE;
10054 }
10055
10056 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10057 {
10058 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10059 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10060 return FALSE;
10061 }
10062
10063 tls_opt = (htab->params->tls_get_addr_opt
10064 && htab->tls_get_addr_fd != NULL
10065 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10066 if (tls_opt || !htab->opd_abi)
10067 {
10068 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10069 return FALSE;
10070 }
10071
10072 if (relocs)
10073 {
10074 if (!add_dynamic_entry (DT_RELA, 0)
10075 || !add_dynamic_entry (DT_RELASZ, 0)
10076 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10077 return FALSE;
10078
10079 /* If any dynamic relocs apply to a read-only section,
10080 then we need a DT_TEXTREL entry. */
10081 if ((info->flags & DF_TEXTREL) == 0)
10082 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10083
10084 if ((info->flags & DF_TEXTREL) != 0)
10085 {
10086 if (!add_dynamic_entry (DT_TEXTREL, 0))
10087 return FALSE;
10088 }
10089 }
10090 }
10091 #undef add_dynamic_entry
10092
10093 return TRUE;
10094 }
10095
10096 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10097
10098 static bfd_boolean
10099 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10100 {
10101 if (h->plt.plist != NULL
10102 && !h->def_regular
10103 && !h->pointer_equality_needed)
10104 return FALSE;
10105
10106 return _bfd_elf_hash_symbol (h);
10107 }
10108
10109 /* Determine the type of stub needed, if any, for a call. */
10110
10111 static inline enum ppc_stub_type
10112 ppc_type_of_stub (asection *input_sec,
10113 const Elf_Internal_Rela *rel,
10114 struct ppc_link_hash_entry **hash,
10115 struct plt_entry **plt_ent,
10116 bfd_vma destination,
10117 unsigned long local_off)
10118 {
10119 struct ppc_link_hash_entry *h = *hash;
10120 bfd_vma location;
10121 bfd_vma branch_offset;
10122 bfd_vma max_branch_offset;
10123 enum elf_ppc64_reloc_type r_type;
10124
10125 if (h != NULL)
10126 {
10127 struct plt_entry *ent;
10128 struct ppc_link_hash_entry *fdh = h;
10129 if (h->oh != NULL
10130 && h->oh->is_func_descriptor)
10131 {
10132 fdh = ppc_follow_link (h->oh);
10133 *hash = fdh;
10134 }
10135
10136 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10137 if (ent->addend == rel->r_addend
10138 && ent->plt.offset != (bfd_vma) -1)
10139 {
10140 *plt_ent = ent;
10141 return ppc_stub_plt_call;
10142 }
10143
10144 /* Here, we know we don't have a plt entry. If we don't have a
10145 either a defined function descriptor or a defined entry symbol
10146 in a regular object file, then it is pointless trying to make
10147 any other type of stub. */
10148 if (!is_static_defined (&fdh->elf)
10149 && !is_static_defined (&h->elf))
10150 return ppc_stub_none;
10151 }
10152 else if (elf_local_got_ents (input_sec->owner) != NULL)
10153 {
10154 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10155 struct plt_entry **local_plt = (struct plt_entry **)
10156 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10157 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10158
10159 if (local_plt[r_symndx] != NULL)
10160 {
10161 struct plt_entry *ent;
10162
10163 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10164 if (ent->addend == rel->r_addend
10165 && ent->plt.offset != (bfd_vma) -1)
10166 {
10167 *plt_ent = ent;
10168 return ppc_stub_plt_call;
10169 }
10170 }
10171 }
10172
10173 /* Determine where the call point is. */
10174 location = (input_sec->output_offset
10175 + input_sec->output_section->vma
10176 + rel->r_offset);
10177
10178 branch_offset = destination - location;
10179 r_type = ELF64_R_TYPE (rel->r_info);
10180
10181 /* Determine if a long branch stub is needed. */
10182 max_branch_offset = 1 << 25;
10183 if (r_type == R_PPC64_REL14
10184 || r_type == R_PPC64_REL14_BRTAKEN
10185 || r_type == R_PPC64_REL14_BRNTAKEN)
10186 max_branch_offset = 1 << 15;
10187
10188 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10189 /* We need a stub. Figure out whether a long_branch or plt_branch
10190 is needed later. */
10191 return ppc_stub_long_branch;
10192
10193 return ppc_stub_none;
10194 }
10195
10196 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10197 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10198 . mflr %r12
10199 . bcl 20,31,1f
10200 .1: mflr %r11
10201 . mtlr %r12
10202 . lis %r12,xxx-1b@highest
10203 . ori %r12,%r12,xxx-1b@higher
10204 . sldi %r12,%r12,32
10205 . oris %r12,%r12,xxx-1b@high
10206 . ori %r12,%r12,xxx-1b@l
10207 . add/ldx %r12,%r11,%r12 */
10208
10209 static bfd_byte *
10210 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10211 {
10212 bfd_put_32 (abfd, MFLR_R12, p);
10213 p += 4;
10214 bfd_put_32 (abfd, BCL_20_31, p);
10215 p += 4;
10216 bfd_put_32 (abfd, MFLR_R11, p);
10217 p += 4;
10218 bfd_put_32 (abfd, MTLR_R12, p);
10219 p += 4;
10220 if (off + 0x8000 < 0x10000)
10221 {
10222 if (load)
10223 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10224 else
10225 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10226 p += 4;
10227 }
10228 else if (off + 0x80008000ULL < 0x100000000ULL)
10229 {
10230 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10231 p += 4;
10232 if (load)
10233 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10234 else
10235 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10236 p += 4;
10237 }
10238 else
10239 {
10240 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10241 {
10242 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10243 p += 4;
10244 }
10245 else
10246 {
10247 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10248 p += 4;
10249 if (((off >> 32) & 0xffff) != 0)
10250 {
10251 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10252 p += 4;
10253 }
10254 }
10255 if (((off >> 32) & 0xffffffffULL) != 0)
10256 {
10257 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10258 p += 4;
10259 }
10260 if (PPC_HI (off) != 0)
10261 {
10262 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10263 p += 4;
10264 }
10265 if (PPC_LO (off) != 0)
10266 {
10267 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10268 p += 4;
10269 }
10270 if (load)
10271 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10272 else
10273 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10274 p += 4;
10275 }
10276 return p;
10277 }
10278
10279 static unsigned int
10280 size_offset (bfd_vma off)
10281 {
10282 unsigned int size;
10283 if (off + 0x8000 < 0x10000)
10284 size = 4;
10285 else if (off + 0x80008000ULL < 0x100000000ULL)
10286 size = 8;
10287 else
10288 {
10289 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10290 size = 4;
10291 else
10292 {
10293 size = 4;
10294 if (((off >> 32) & 0xffff) != 0)
10295 size += 4;
10296 }
10297 if (((off >> 32) & 0xffffffffULL) != 0)
10298 size += 4;
10299 if (PPC_HI (off) != 0)
10300 size += 4;
10301 if (PPC_LO (off) != 0)
10302 size += 4;
10303 size += 4;
10304 }
10305 return size + 16;
10306 }
10307
10308 static unsigned int
10309 num_relocs_for_offset (bfd_vma off)
10310 {
10311 unsigned int num_rel;
10312 if (off + 0x8000 < 0x10000)
10313 num_rel = 1;
10314 else if (off + 0x80008000ULL < 0x100000000ULL)
10315 num_rel = 2;
10316 else
10317 {
10318 num_rel = 1;
10319 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10320 && ((off >> 32) & 0xffff) != 0)
10321 num_rel += 1;
10322 if (PPC_HI (off) != 0)
10323 num_rel += 1;
10324 if (PPC_LO (off) != 0)
10325 num_rel += 1;
10326 }
10327 return num_rel;
10328 }
10329
10330 static Elf_Internal_Rela *
10331 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10332 bfd_vma roff, bfd_vma targ, bfd_vma off)
10333 {
10334 bfd_vma relative_targ = targ - (roff - 8);
10335 if (bfd_big_endian (info->output_bfd))
10336 roff += 2;
10337 r->r_offset = roff;
10338 r->r_addend = relative_targ + roff;
10339 if (off + 0x8000 < 0x10000)
10340 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10341 else if (off + 0x80008000ULL < 0x100000000ULL)
10342 {
10343 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10344 ++r;
10345 roff += 4;
10346 r->r_offset = roff;
10347 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10348 r->r_addend = relative_targ + roff;
10349 }
10350 else
10351 {
10352 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10353 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10354 else
10355 {
10356 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10357 if (((off >> 32) & 0xffff) != 0)
10358 {
10359 ++r;
10360 roff += 4;
10361 r->r_offset = roff;
10362 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10363 r->r_addend = relative_targ + roff;
10364 }
10365 }
10366 if (((off >> 32) & 0xffffffffULL) != 0)
10367 roff += 4;
10368 if (PPC_HI (off) != 0)
10369 {
10370 ++r;
10371 roff += 4;
10372 r->r_offset = roff;
10373 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10374 r->r_addend = relative_targ + roff;
10375 }
10376 if (PPC_LO (off) != 0)
10377 {
10378 ++r;
10379 roff += 4;
10380 r->r_offset = roff;
10381 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10382 r->r_addend = relative_targ + roff;
10383 }
10384 }
10385 return r;
10386 }
10387
10388 static bfd_byte *
10389 build_powerxx_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10390 bfd_boolean load)
10391 {
10392 uint64_t insn;
10393 if (off - odd + (1ULL << 33) < 1ULL << 34)
10394 {
10395 off -= odd;
10396 if (odd)
10397 {
10398 bfd_put_32 (abfd, NOP, p);
10399 p += 4;
10400 }
10401 if (load)
10402 insn = PLD_R12_PC;
10403 else
10404 insn = PADDI_R12_PC;
10405 insn |= D34 (off);
10406 bfd_put_32 (abfd, insn >> 32, p);
10407 p += 4;
10408 bfd_put_32 (abfd, insn, p);
10409 }
10410 /* The minimum value for paddi is -0x200000000. The minimum value
10411 for li is -0x8000, which when shifted by 34 and added gives a
10412 minimum value of -0x2000200000000. The maximum value is
10413 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10414 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10415 {
10416 off -= 8 - odd;
10417 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10418 p += 4;
10419 if (!odd)
10420 {
10421 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10422 p += 4;
10423 }
10424 insn = PADDI_R12_PC | D34 (off);
10425 bfd_put_32 (abfd, insn >> 32, p);
10426 p += 4;
10427 bfd_put_32 (abfd, insn, p);
10428 p += 4;
10429 if (odd)
10430 {
10431 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10432 p += 4;
10433 }
10434 if (load)
10435 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10436 else
10437 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10438 }
10439 else
10440 {
10441 off -= odd + 8;
10442 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10443 p += 4;
10444 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10445 p += 4;
10446 if (odd)
10447 {
10448 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10449 p += 4;
10450 }
10451 insn = PADDI_R12_PC | D34 (off);
10452 bfd_put_32 (abfd, insn >> 32, p);
10453 p += 4;
10454 bfd_put_32 (abfd, insn, p);
10455 p += 4;
10456 if (!odd)
10457 {
10458 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10459 p += 4;
10460 }
10461 if (load)
10462 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10463 else
10464 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10465 }
10466 p += 4;
10467 return p;
10468 }
10469
10470 static unsigned int
10471 size_powerxx_offset (bfd_vma off, int odd)
10472 {
10473 if (off - odd + (1ULL << 33) < 1ULL << 34)
10474 return odd + 8;
10475 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10476 return 20;
10477 else
10478 return 24;
10479 }
10480
10481 static unsigned int
10482 num_relocs_for_powerxx_offset (bfd_vma off, int odd)
10483 {
10484 if (off - odd + (1ULL << 33) < 1ULL << 34)
10485 return 1;
10486 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10487 return 2;
10488 else
10489 return 3;
10490 }
10491
10492 static Elf_Internal_Rela *
10493 emit_relocs_for_powerxx_offset (struct bfd_link_info *info,
10494 Elf_Internal_Rela *r, bfd_vma roff,
10495 bfd_vma targ, bfd_vma off, int odd)
10496 {
10497 if (off - odd + (1ULL << 33) < 1ULL << 34)
10498 roff += odd;
10499 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10500 {
10501 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10502 r->r_offset = roff + d_offset;
10503 r->r_addend = targ + 8 - odd - d_offset;
10504 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10505 ++r;
10506 roff += 8 - odd;
10507 }
10508 else
10509 {
10510 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10511 r->r_offset = roff + d_offset;
10512 r->r_addend = targ + 8 + odd - d_offset;
10513 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10514 ++r;
10515 roff += 4;
10516 r->r_offset = roff + d_offset;
10517 r->r_addend = targ + 4 + odd - d_offset;
10518 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10519 ++r;
10520 roff += 4 + odd;
10521 }
10522 r->r_offset = roff;
10523 r->r_addend = targ;
10524 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10525 return r;
10526 }
10527
10528 /* Emit .eh_frame opcode to advance pc by DELTA. */
10529
10530 static bfd_byte *
10531 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10532 {
10533 delta /= 4;
10534 if (delta < 64)
10535 *eh++ = DW_CFA_advance_loc + delta;
10536 else if (delta < 256)
10537 {
10538 *eh++ = DW_CFA_advance_loc1;
10539 *eh++ = delta;
10540 }
10541 else if (delta < 65536)
10542 {
10543 *eh++ = DW_CFA_advance_loc2;
10544 bfd_put_16 (abfd, delta, eh);
10545 eh += 2;
10546 }
10547 else
10548 {
10549 *eh++ = DW_CFA_advance_loc4;
10550 bfd_put_32 (abfd, delta, eh);
10551 eh += 4;
10552 }
10553 return eh;
10554 }
10555
10556 /* Size of required .eh_frame opcode to advance pc by DELTA. */
10557
10558 static unsigned int
10559 eh_advance_size (unsigned int delta)
10560 {
10561 if (delta < 64 * 4)
10562 /* DW_CFA_advance_loc+[1..63]. */
10563 return 1;
10564 if (delta < 256 * 4)
10565 /* DW_CFA_advance_loc1, byte. */
10566 return 2;
10567 if (delta < 65536 * 4)
10568 /* DW_CFA_advance_loc2, 2 bytes. */
10569 return 3;
10570 /* DW_CFA_advance_loc4, 4 bytes. */
10571 return 5;
10572 }
10573
10574 /* With power7 weakly ordered memory model, it is possible for ld.so
10575 to update a plt entry in one thread and have another thread see a
10576 stale zero toc entry. To avoid this we need some sort of acquire
10577 barrier in the call stub. One solution is to make the load of the
10578 toc word seem to appear to depend on the load of the function entry
10579 word. Another solution is to test for r2 being zero, and branch to
10580 the appropriate glink entry if so.
10581
10582 . fake dep barrier compare
10583 . ld 12,xxx(2) ld 12,xxx(2)
10584 . mtctr 12 mtctr 12
10585 . xor 11,12,12 ld 2,xxx+8(2)
10586 . add 2,2,11 cmpldi 2,0
10587 . ld 2,xxx+8(2) bnectr+
10588 . bctr b <glink_entry>
10589
10590 The solution involving the compare turns out to be faster, so
10591 that's what we use unless the branch won't reach. */
10592
10593 #define ALWAYS_USE_FAKE_DEP 0
10594 #define ALWAYS_EMIT_R2SAVE 0
10595
10596 static inline unsigned int
10597 plt_stub_size (struct ppc_link_hash_table *htab,
10598 struct ppc_stub_hash_entry *stub_entry,
10599 bfd_vma off)
10600 {
10601 unsigned size;
10602
10603 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10604 {
10605 if (htab->powerxx_stubs)
10606 {
10607 bfd_vma start = (stub_entry->stub_offset
10608 + stub_entry->group->stub_sec->output_offset
10609 + stub_entry->group->stub_sec->output_section->vma);
10610 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10611 start += 4;
10612 size = 8 + size_powerxx_offset (off, start & 4);
10613 }
10614 else
10615 size = 8 + size_offset (off - 8);
10616 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10617 size += 4;
10618 return size;
10619 }
10620
10621 size = 12;
10622 if (ALWAYS_EMIT_R2SAVE
10623 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10624 size += 4;
10625 if (PPC_HA (off) != 0)
10626 size += 4;
10627 if (htab->opd_abi)
10628 {
10629 size += 4;
10630 if (htab->params->plt_static_chain)
10631 size += 4;
10632 if (htab->params->plt_thread_safe
10633 && htab->elf.dynamic_sections_created
10634 && stub_entry->h != NULL
10635 && stub_entry->h->elf.dynindx != -1)
10636 size += 8;
10637 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10638 size += 4;
10639 }
10640 if (stub_entry->h != NULL
10641 && (stub_entry->h == htab->tls_get_addr_fd
10642 || stub_entry->h == htab->tls_get_addr)
10643 && htab->params->tls_get_addr_opt)
10644 {
10645 size += 7 * 4;
10646 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
10647 size += 6 * 4;
10648 }
10649 return size;
10650 }
10651
10652 /* Depending on the sign of plt_stub_align:
10653 If positive, return the padding to align to a 2**plt_stub_align
10654 boundary.
10655 If negative, if this stub would cross fewer 2**plt_stub_align
10656 boundaries if we align, then return the padding needed to do so. */
10657
10658 static inline unsigned int
10659 plt_stub_pad (struct ppc_link_hash_table *htab,
10660 struct ppc_stub_hash_entry *stub_entry,
10661 bfd_vma plt_off)
10662 {
10663 int stub_align;
10664 unsigned stub_size;
10665 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10666
10667 if (htab->params->plt_stub_align >= 0)
10668 {
10669 stub_align = 1 << htab->params->plt_stub_align;
10670 if ((stub_off & (stub_align - 1)) != 0)
10671 return stub_align - (stub_off & (stub_align - 1));
10672 return 0;
10673 }
10674
10675 stub_align = 1 << -htab->params->plt_stub_align;
10676 stub_size = plt_stub_size (htab, stub_entry, plt_off);
10677 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10678 > ((stub_size - 1) & -stub_align))
10679 return stub_align - (stub_off & (stub_align - 1));
10680 return 0;
10681 }
10682
10683 /* Build a .plt call stub. */
10684
10685 static inline bfd_byte *
10686 build_plt_stub (struct ppc_link_hash_table *htab,
10687 struct ppc_stub_hash_entry *stub_entry,
10688 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10689 {
10690 bfd *obfd = htab->params->stub_bfd;
10691 bfd_boolean plt_load_toc = htab->opd_abi;
10692 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10693 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10694 && htab->elf.dynamic_sections_created
10695 && stub_entry->h != NULL
10696 && stub_entry->h->elf.dynindx != -1);
10697 bfd_boolean use_fake_dep = plt_thread_safe;
10698 bfd_vma cmp_branch_off = 0;
10699
10700 if (!ALWAYS_USE_FAKE_DEP
10701 && plt_load_toc
10702 && plt_thread_safe
10703 && !((stub_entry->h == htab->tls_get_addr_fd
10704 || stub_entry->h == htab->tls_get_addr)
10705 && htab->params->tls_get_addr_opt))
10706 {
10707 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10708 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10709 / PLT_ENTRY_SIZE (htab));
10710 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10711 bfd_vma to, from;
10712
10713 if (pltindex > 32768)
10714 glinkoff += (pltindex - 32768) * 4;
10715 to = (glinkoff
10716 + htab->glink->output_offset
10717 + htab->glink->output_section->vma);
10718 from = (p - stub_entry->group->stub_sec->contents
10719 + 4 * (ALWAYS_EMIT_R2SAVE
10720 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10721 + 4 * (PPC_HA (offset) != 0)
10722 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10723 != PPC_HA (offset))
10724 + 4 * (plt_static_chain != 0)
10725 + 20
10726 + stub_entry->group->stub_sec->output_offset
10727 + stub_entry->group->stub_sec->output_section->vma);
10728 cmp_branch_off = to - from;
10729 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10730 }
10731
10732 if (PPC_HA (offset) != 0)
10733 {
10734 if (r != NULL)
10735 {
10736 if (ALWAYS_EMIT_R2SAVE
10737 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10738 r[0].r_offset += 4;
10739 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10740 r[1].r_offset = r[0].r_offset + 4;
10741 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10742 r[1].r_addend = r[0].r_addend;
10743 if (plt_load_toc)
10744 {
10745 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10746 {
10747 r[2].r_offset = r[1].r_offset + 4;
10748 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10749 r[2].r_addend = r[0].r_addend;
10750 }
10751 else
10752 {
10753 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10754 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10755 r[2].r_addend = r[0].r_addend + 8;
10756 if (plt_static_chain)
10757 {
10758 r[3].r_offset = r[2].r_offset + 4;
10759 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10760 r[3].r_addend = r[0].r_addend + 16;
10761 }
10762 }
10763 }
10764 }
10765 if (ALWAYS_EMIT_R2SAVE
10766 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10767 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10768 if (plt_load_toc)
10769 {
10770 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10771 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10772 }
10773 else
10774 {
10775 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10776 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10777 }
10778 if (plt_load_toc
10779 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10780 {
10781 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10782 offset = 0;
10783 }
10784 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10785 if (plt_load_toc)
10786 {
10787 if (use_fake_dep)
10788 {
10789 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10790 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10791 }
10792 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10793 if (plt_static_chain)
10794 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10795 }
10796 }
10797 else
10798 {
10799 if (r != NULL)
10800 {
10801 if (ALWAYS_EMIT_R2SAVE
10802 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10803 r[0].r_offset += 4;
10804 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10805 if (plt_load_toc)
10806 {
10807 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10808 {
10809 r[1].r_offset = r[0].r_offset + 4;
10810 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10811 r[1].r_addend = r[0].r_addend;
10812 }
10813 else
10814 {
10815 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10816 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10817 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10818 if (plt_static_chain)
10819 {
10820 r[2].r_offset = r[1].r_offset + 4;
10821 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10822 r[2].r_addend = r[0].r_addend + 8;
10823 }
10824 }
10825 }
10826 }
10827 if (ALWAYS_EMIT_R2SAVE
10828 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10829 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10830 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10831 if (plt_load_toc
10832 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10833 {
10834 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10835 offset = 0;
10836 }
10837 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10838 if (plt_load_toc)
10839 {
10840 if (use_fake_dep)
10841 {
10842 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10843 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10844 }
10845 if (plt_static_chain)
10846 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10847 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10848 }
10849 }
10850 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10851 {
10852 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10853 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10854 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10855 }
10856 else
10857 bfd_put_32 (obfd, BCTR, p), p += 4;
10858 return p;
10859 }
10860
10861 /* Build a special .plt call stub for __tls_get_addr. */
10862
10863 #define LD_R11_0R3 0xe9630000
10864 #define LD_R12_0R3 0xe9830000
10865 #define MR_R0_R3 0x7c601b78
10866 #define CMPDI_R11_0 0x2c2b0000
10867 #define ADD_R3_R12_R13 0x7c6c6a14
10868 #define BEQLR 0x4d820020
10869 #define MR_R3_R0 0x7c030378
10870 #define STD_R11_0R1 0xf9610000
10871 #define BCTRL 0x4e800421
10872 #define LD_R11_0R1 0xe9610000
10873 #define MTLR_R11 0x7d6803a6
10874
10875 static inline bfd_byte *
10876 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10877 struct ppc_stub_hash_entry *stub_entry,
10878 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10879 {
10880 bfd *obfd = htab->params->stub_bfd;
10881 bfd_byte *loc = p;
10882
10883 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10884 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10885 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10886 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10887 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10888 bfd_put_32 (obfd, BEQLR, p), p += 4;
10889 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10890 if (r != NULL)
10891 r[0].r_offset += 7 * 4;
10892 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
10893 return build_plt_stub (htab, stub_entry, p, offset, r);
10894
10895 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10896 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10897
10898 if (r != NULL)
10899 r[0].r_offset += 2 * 4;
10900 p = build_plt_stub (htab, stub_entry, p, offset, r);
10901 bfd_put_32 (obfd, BCTRL, p - 4);
10902
10903 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10904 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10905 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10906 bfd_put_32 (obfd, BLR, p), p += 4;
10907
10908 if (htab->glink_eh_frame != NULL
10909 && htab->glink_eh_frame->size != 0)
10910 {
10911 bfd_byte *base, *eh;
10912 unsigned int lr_used, delta;
10913
10914 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10915 eh = base + stub_entry->group->eh_size;
10916 lr_used = stub_entry->stub_offset + (p - 20 - loc);
10917 delta = lr_used - stub_entry->group->lr_restore;
10918 stub_entry->group->lr_restore = lr_used + 16;
10919 eh = eh_advance (htab->elf.dynobj, eh, delta);
10920 *eh++ = DW_CFA_offset_extended_sf;
10921 *eh++ = 65;
10922 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10923 *eh++ = DW_CFA_advance_loc + 4;
10924 *eh++ = DW_CFA_restore_extended;
10925 *eh++ = 65;
10926 stub_entry->group->eh_size = eh - base;
10927 }
10928 return p;
10929 }
10930
10931 static Elf_Internal_Rela *
10932 get_relocs (asection *sec, int count)
10933 {
10934 Elf_Internal_Rela *relocs;
10935 struct bfd_elf_section_data *elfsec_data;
10936
10937 elfsec_data = elf_section_data (sec);
10938 relocs = elfsec_data->relocs;
10939 if (relocs == NULL)
10940 {
10941 bfd_size_type relsize;
10942 relsize = sec->reloc_count * sizeof (*relocs);
10943 relocs = bfd_alloc (sec->owner, relsize);
10944 if (relocs == NULL)
10945 return NULL;
10946 elfsec_data->relocs = relocs;
10947 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10948 sizeof (Elf_Internal_Shdr));
10949 if (elfsec_data->rela.hdr == NULL)
10950 return NULL;
10951 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10952 * sizeof (Elf64_External_Rela));
10953 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10954 sec->reloc_count = 0;
10955 }
10956 relocs += sec->reloc_count;
10957 sec->reloc_count += count;
10958 return relocs;
10959 }
10960
10961 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10962 forms, to the equivalent relocs against the global symbol given by
10963 STUB_ENTRY->H. */
10964
10965 static bfd_boolean
10966 use_global_in_relocs (struct ppc_link_hash_table *htab,
10967 struct ppc_stub_hash_entry *stub_entry,
10968 Elf_Internal_Rela *r, unsigned int num_rel)
10969 {
10970 struct elf_link_hash_entry **hashes;
10971 unsigned long symndx;
10972 struct ppc_link_hash_entry *h;
10973 bfd_vma symval;
10974
10975 /* Relocs are always against symbols in their own object file. Fake
10976 up global sym hashes for the stub bfd (which has no symbols). */
10977 hashes = elf_sym_hashes (htab->params->stub_bfd);
10978 if (hashes == NULL)
10979 {
10980 bfd_size_type hsize;
10981
10982 /* When called the first time, stub_globals will contain the
10983 total number of symbols seen during stub sizing. After
10984 allocating, stub_globals is used as an index to fill the
10985 hashes array. */
10986 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10987 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10988 if (hashes == NULL)
10989 return FALSE;
10990 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10991 htab->stub_globals = 1;
10992 }
10993 symndx = htab->stub_globals++;
10994 h = stub_entry->h;
10995 hashes[symndx] = &h->elf;
10996 if (h->oh != NULL && h->oh->is_func)
10997 h = ppc_follow_link (h->oh);
10998 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10999 || h->elf.root.type == bfd_link_hash_defweak);
11000 symval = (h->elf.root.u.def.value
11001 + h->elf.root.u.def.section->output_offset
11002 + h->elf.root.u.def.section->output_section->vma);
11003 while (num_rel-- != 0)
11004 {
11005 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11006 if (h->elf.root.u.def.section != stub_entry->target_section)
11007 {
11008 /* H is an opd symbol. The addend must be zero, and the
11009 branch reloc is the only one we can convert. */
11010 r->r_addend = 0;
11011 break;
11012 }
11013 else
11014 r->r_addend -= symval;
11015 --r;
11016 }
11017 return TRUE;
11018 }
11019
11020 static bfd_vma
11021 get_r2off (struct bfd_link_info *info,
11022 struct ppc_stub_hash_entry *stub_entry)
11023 {
11024 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11025 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11026
11027 if (r2off == 0)
11028 {
11029 /* Support linking -R objects. Get the toc pointer from the
11030 opd entry. */
11031 char buf[8];
11032 if (!htab->opd_abi)
11033 return r2off;
11034 asection *opd = stub_entry->h->elf.root.u.def.section;
11035 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11036
11037 if (strcmp (opd->name, ".opd") != 0
11038 || opd->reloc_count != 0)
11039 {
11040 info->callbacks->einfo
11041 (_("%P: cannot find opd entry toc for `%pT'\n"),
11042 stub_entry->h->elf.root.root.string);
11043 bfd_set_error (bfd_error_bad_value);
11044 return (bfd_vma) -1;
11045 }
11046 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11047 return (bfd_vma) -1;
11048 r2off = bfd_get_64 (opd->owner, buf);
11049 r2off -= elf_gp (info->output_bfd);
11050 }
11051 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11052 return r2off;
11053 }
11054
11055 static bfd_boolean
11056 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11057 {
11058 struct ppc_stub_hash_entry *stub_entry;
11059 struct ppc_branch_hash_entry *br_entry;
11060 struct bfd_link_info *info;
11061 struct ppc_link_hash_table *htab;
11062 bfd_byte *loc;
11063 bfd_byte *p, *relp;
11064 bfd_vma targ, off;
11065 Elf_Internal_Rela *r;
11066 asection *plt;
11067 int num_rel;
11068 int odd;
11069
11070 /* Massage our args to the form they really have. */
11071 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11072 info = in_arg;
11073
11074 htab = ppc_hash_table (info);
11075 if (htab == NULL)
11076 return FALSE;
11077
11078 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
11079 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11080
11081 htab->stub_count[stub_entry->stub_type - 1] += 1;
11082 switch (stub_entry->stub_type)
11083 {
11084 case ppc_stub_long_branch:
11085 case ppc_stub_long_branch_r2off:
11086 /* Branches are relative. This is where we are going to. */
11087 targ = (stub_entry->target_value
11088 + stub_entry->target_section->output_offset
11089 + stub_entry->target_section->output_section->vma);
11090 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11091
11092 /* And this is where we are coming from. */
11093 off = (stub_entry->stub_offset
11094 + stub_entry->group->stub_sec->output_offset
11095 + stub_entry->group->stub_sec->output_section->vma);
11096 off = targ - off;
11097
11098 p = loc;
11099 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11100 {
11101 bfd_vma r2off = get_r2off (info, stub_entry);
11102
11103 if (r2off == (bfd_vma) -1)
11104 {
11105 htab->stub_error = TRUE;
11106 return FALSE;
11107 }
11108 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11109 p += 4;
11110 if (PPC_HA (r2off) != 0)
11111 {
11112 bfd_put_32 (htab->params->stub_bfd,
11113 ADDIS_R2_R2 | PPC_HA (r2off), p);
11114 p += 4;
11115 }
11116 if (PPC_LO (r2off) != 0)
11117 {
11118 bfd_put_32 (htab->params->stub_bfd,
11119 ADDI_R2_R2 | PPC_LO (r2off), p);
11120 p += 4;
11121 }
11122 off -= p - loc;
11123 }
11124 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11125 p += 4;
11126
11127 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11128 {
11129 _bfd_error_handler
11130 (_("long branch stub `%s' offset overflow"),
11131 stub_entry->root.string);
11132 htab->stub_error = TRUE;
11133 return FALSE;
11134 }
11135
11136 if (info->emitrelocations)
11137 {
11138 r = get_relocs (stub_entry->group->stub_sec, 1);
11139 if (r == NULL)
11140 return FALSE;
11141 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11142 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11143 r->r_addend = targ;
11144 if (stub_entry->h != NULL
11145 && !use_global_in_relocs (htab, stub_entry, r, 1))
11146 return FALSE;
11147 }
11148 break;
11149
11150 case ppc_stub_plt_branch:
11151 case ppc_stub_plt_branch_r2off:
11152 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11153 stub_entry->root.string + 9,
11154 FALSE, FALSE);
11155 if (br_entry == NULL)
11156 {
11157 _bfd_error_handler (_("can't find branch stub `%s'"),
11158 stub_entry->root.string);
11159 htab->stub_error = TRUE;
11160 return FALSE;
11161 }
11162
11163 targ = (stub_entry->target_value
11164 + stub_entry->target_section->output_offset
11165 + stub_entry->target_section->output_section->vma);
11166 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11167 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11168
11169 bfd_put_64 (htab->brlt->owner, targ,
11170 htab->brlt->contents + br_entry->offset);
11171
11172 if (br_entry->iter == htab->stub_iteration)
11173 {
11174 br_entry->iter = 0;
11175
11176 if (htab->relbrlt != NULL)
11177 {
11178 /* Create a reloc for the branch lookup table entry. */
11179 Elf_Internal_Rela rela;
11180 bfd_byte *rl;
11181
11182 rela.r_offset = (br_entry->offset
11183 + htab->brlt->output_offset
11184 + htab->brlt->output_section->vma);
11185 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11186 rela.r_addend = targ;
11187
11188 rl = htab->relbrlt->contents;
11189 rl += (htab->relbrlt->reloc_count++
11190 * sizeof (Elf64_External_Rela));
11191 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11192 }
11193 else if (info->emitrelocations)
11194 {
11195 r = get_relocs (htab->brlt, 1);
11196 if (r == NULL)
11197 return FALSE;
11198 /* brlt, being SEC_LINKER_CREATED does not go through the
11199 normal reloc processing. Symbols and offsets are not
11200 translated from input file to output file form, so
11201 set up the offset per the output file. */
11202 r->r_offset = (br_entry->offset
11203 + htab->brlt->output_offset
11204 + htab->brlt->output_section->vma);
11205 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11206 r->r_addend = targ;
11207 }
11208 }
11209
11210 targ = (br_entry->offset
11211 + htab->brlt->output_offset
11212 + htab->brlt->output_section->vma);
11213
11214 off = (elf_gp (info->output_bfd)
11215 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11216 off = targ - off;
11217
11218 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11219 {
11220 info->callbacks->einfo
11221 (_("%P: linkage table error against `%pT'\n"),
11222 stub_entry->root.string);
11223 bfd_set_error (bfd_error_bad_value);
11224 htab->stub_error = TRUE;
11225 return FALSE;
11226 }
11227
11228 if (info->emitrelocations)
11229 {
11230 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11231 if (r == NULL)
11232 return FALSE;
11233 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11234 if (bfd_big_endian (info->output_bfd))
11235 r[0].r_offset += 2;
11236 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11237 r[0].r_offset += 4;
11238 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11239 r[0].r_addend = targ;
11240 if (PPC_HA (off) != 0)
11241 {
11242 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11243 r[1].r_offset = r[0].r_offset + 4;
11244 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11245 r[1].r_addend = r[0].r_addend;
11246 }
11247 }
11248
11249 p = loc;
11250 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11251 {
11252 if (PPC_HA (off) != 0)
11253 {
11254 bfd_put_32 (htab->params->stub_bfd,
11255 ADDIS_R12_R2 | PPC_HA (off), p);
11256 p += 4;
11257 bfd_put_32 (htab->params->stub_bfd,
11258 LD_R12_0R12 | PPC_LO (off), p);
11259 }
11260 else
11261 bfd_put_32 (htab->params->stub_bfd,
11262 LD_R12_0R2 | PPC_LO (off), p);
11263 }
11264 else
11265 {
11266 bfd_vma r2off = get_r2off (info, stub_entry);
11267
11268 if (r2off == (bfd_vma) -1)
11269 {
11270 htab->stub_error = TRUE;
11271 return FALSE;
11272 }
11273
11274 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11275 p += 4;
11276 if (PPC_HA (off) != 0)
11277 {
11278 bfd_put_32 (htab->params->stub_bfd,
11279 ADDIS_R12_R2 | PPC_HA (off), p);
11280 p += 4;
11281 bfd_put_32 (htab->params->stub_bfd,
11282 LD_R12_0R12 | PPC_LO (off), p);
11283 }
11284 else
11285 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11286
11287 if (PPC_HA (r2off) != 0)
11288 {
11289 p += 4;
11290 bfd_put_32 (htab->params->stub_bfd,
11291 ADDIS_R2_R2 | PPC_HA (r2off), p);
11292 }
11293 if (PPC_LO (r2off) != 0)
11294 {
11295 p += 4;
11296 bfd_put_32 (htab->params->stub_bfd,
11297 ADDI_R2_R2 | PPC_LO (r2off), p);
11298 }
11299 }
11300 p += 4;
11301 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11302 p += 4;
11303 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11304 p += 4;
11305 break;
11306
11307 case ppc_stub_long_branch_notoc:
11308 case ppc_stub_long_branch_both:
11309 case ppc_stub_plt_branch_notoc:
11310 case ppc_stub_plt_branch_both:
11311 case ppc_stub_plt_call_notoc:
11312 case ppc_stub_plt_call_both:
11313 p = loc;
11314 off = (stub_entry->stub_offset
11315 + stub_entry->group->stub_sec->output_offset
11316 + stub_entry->group->stub_sec->output_section->vma);
11317 if (stub_entry->stub_type == ppc_stub_long_branch_both
11318 || stub_entry->stub_type == ppc_stub_plt_branch_both
11319 || stub_entry->stub_type == ppc_stub_plt_call_both)
11320 {
11321 off += 4;
11322 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11323 p += 4;
11324 }
11325 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11326 {
11327 targ = stub_entry->plt_ent->plt.offset & ~1;
11328 if (targ >= (bfd_vma) -2)
11329 abort ();
11330
11331 plt = htab->elf.splt;
11332 if (!htab->elf.dynamic_sections_created
11333 || stub_entry->h == NULL
11334 || stub_entry->h->elf.dynindx == -1)
11335 {
11336 if (stub_entry->symtype == STT_GNU_IFUNC)
11337 plt = htab->elf.iplt;
11338 else
11339 plt = htab->pltlocal;
11340 }
11341 targ += plt->output_offset + plt->output_section->vma;
11342 }
11343 else
11344 targ = (stub_entry->target_value
11345 + stub_entry->target_section->output_offset
11346 + stub_entry->target_section->output_section->vma);
11347 odd = off & 4;
11348 off = targ - off;
11349
11350 relp = p;
11351 num_rel = 0;
11352 if (htab->powerxx_stubs)
11353 {
11354 bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
11355 p = build_powerxx_offset (htab->params->stub_bfd, p, off, odd, load);
11356 }
11357 else
11358 {
11359 /* The notoc stubs calculate their target (either a PLT entry or
11360 the global entry point of a function) relative to the PC
11361 returned by the "bcl" two instructions past the start of the
11362 sequence emitted by build_offset. The offset is therefore 8
11363 less than calculated from the start of the sequence. */
11364 off -= 8;
11365 p = build_offset (htab->params->stub_bfd, p, off,
11366 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11367 }
11368
11369 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
11370 {
11371 bfd_vma from;
11372 num_rel = 1;
11373 from = (stub_entry->stub_offset
11374 + stub_entry->group->stub_sec->output_offset
11375 + stub_entry->group->stub_sec->output_section->vma
11376 + (p - loc));
11377 bfd_put_32 (htab->params->stub_bfd,
11378 B_DOT | ((targ - from) & 0x3fffffc), p);
11379 }
11380 else
11381 {
11382 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11383 p += 4;
11384 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11385 }
11386 p += 4;
11387
11388 if (info->emitrelocations)
11389 {
11390 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
11391 if (htab->powerxx_stubs)
11392 num_rel += num_relocs_for_powerxx_offset (off, odd);
11393 else
11394 {
11395 num_rel += num_relocs_for_offset (off);
11396 roff += 16;
11397 }
11398 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11399 if (r == NULL)
11400 return FALSE;
11401 if (htab->powerxx_stubs)
11402 r = emit_relocs_for_powerxx_offset (info, r, roff, targ, off, odd);
11403 else
11404 r = emit_relocs_for_offset (info, r, roff, targ, off);
11405 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11406 || stub_entry->stub_type == ppc_stub_long_branch_both)
11407 {
11408 ++r;
11409 roff = p - 4 - stub_entry->group->stub_sec->contents;
11410 r->r_offset = roff;
11411 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11412 r->r_addend = targ;
11413 if (stub_entry->h != NULL
11414 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11415 return FALSE;
11416 }
11417 }
11418
11419 if (!htab->powerxx_stubs
11420 && htab->glink_eh_frame != NULL
11421 && htab->glink_eh_frame->size != 0)
11422 {
11423 bfd_byte *base, *eh;
11424 unsigned int lr_used, delta;
11425
11426 base = (htab->glink_eh_frame->contents
11427 + stub_entry->group->eh_base + 17);
11428 eh = base + stub_entry->group->eh_size;
11429 lr_used = stub_entry->stub_offset + 8;
11430 if (stub_entry->stub_type == ppc_stub_long_branch_both
11431 || stub_entry->stub_type == ppc_stub_plt_branch_both
11432 || stub_entry->stub_type == ppc_stub_plt_call_both)
11433 lr_used += 4;
11434 delta = lr_used - stub_entry->group->lr_restore;
11435 stub_entry->group->lr_restore = lr_used + 8;
11436 eh = eh_advance (htab->elf.dynobj, eh, delta);
11437 *eh++ = DW_CFA_register;
11438 *eh++ = 65;
11439 *eh++ = 12;
11440 *eh++ = DW_CFA_advance_loc + 2;
11441 *eh++ = DW_CFA_restore_extended;
11442 *eh++ = 65;
11443 stub_entry->group->eh_size = eh - base;
11444 }
11445 break;
11446
11447 case ppc_stub_plt_call:
11448 case ppc_stub_plt_call_r2save:
11449 if (stub_entry->h != NULL
11450 && stub_entry->h->is_func_descriptor
11451 && stub_entry->h->oh != NULL)
11452 {
11453 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11454
11455 /* If the old-ABI "dot-symbol" is undefined make it weak so
11456 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11457 if (fh->elf.root.type == bfd_link_hash_undefined
11458 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11459 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11460 fh->elf.root.type = bfd_link_hash_undefweak;
11461 }
11462
11463 /* Now build the stub. */
11464 targ = stub_entry->plt_ent->plt.offset & ~1;
11465 if (targ >= (bfd_vma) -2)
11466 abort ();
11467
11468 plt = htab->elf.splt;
11469 if (!htab->elf.dynamic_sections_created
11470 || stub_entry->h == NULL
11471 || stub_entry->h->elf.dynindx == -1)
11472 {
11473 if (stub_entry->symtype == STT_GNU_IFUNC)
11474 plt = htab->elf.iplt;
11475 else
11476 plt = htab->pltlocal;
11477 }
11478 targ += plt->output_offset + plt->output_section->vma;
11479
11480 off = (elf_gp (info->output_bfd)
11481 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11482 off = targ - off;
11483
11484 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11485 {
11486 info->callbacks->einfo
11487 /* xgettext:c-format */
11488 (_("%P: linkage table error against `%pT'\n"),
11489 stub_entry->h != NULL
11490 ? stub_entry->h->elf.root.root.string
11491 : "<local sym>");
11492 bfd_set_error (bfd_error_bad_value);
11493 htab->stub_error = TRUE;
11494 return FALSE;
11495 }
11496
11497 r = NULL;
11498 if (info->emitrelocations)
11499 {
11500 r = get_relocs (stub_entry->group->stub_sec,
11501 ((PPC_HA (off) != 0)
11502 + (htab->opd_abi
11503 ? 2 + (htab->params->plt_static_chain
11504 && PPC_HA (off + 16) == PPC_HA (off))
11505 : 1)));
11506 if (r == NULL)
11507 return FALSE;
11508 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11509 if (bfd_big_endian (info->output_bfd))
11510 r[0].r_offset += 2;
11511 r[0].r_addend = targ;
11512 }
11513 if (stub_entry->h != NULL
11514 && (stub_entry->h == htab->tls_get_addr_fd
11515 || stub_entry->h == htab->tls_get_addr)
11516 && htab->params->tls_get_addr_opt)
11517 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11518 else
11519 p = build_plt_stub (htab, stub_entry, loc, off, r);
11520 break;
11521
11522 case ppc_stub_save_res:
11523 return TRUE;
11524
11525 default:
11526 BFD_FAIL ();
11527 return FALSE;
11528 }
11529
11530 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11531
11532 if (htab->params->emit_stub_syms)
11533 {
11534 struct elf_link_hash_entry *h;
11535 size_t len1, len2;
11536 char *name;
11537 const char *const stub_str[] = { "long_branch",
11538 "long_branch",
11539 "long_branch",
11540 "long_branch",
11541 "plt_branch",
11542 "plt_branch",
11543 "plt_branch",
11544 "plt_branch",
11545 "plt_call",
11546 "plt_call",
11547 "plt_call",
11548 "plt_call" };
11549
11550 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11551 len2 = strlen (stub_entry->root.string);
11552 name = bfd_malloc (len1 + len2 + 2);
11553 if (name == NULL)
11554 return FALSE;
11555 memcpy (name, stub_entry->root.string, 9);
11556 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11557 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11558 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11559 if (h == NULL)
11560 return FALSE;
11561 if (h->root.type == bfd_link_hash_new)
11562 {
11563 h->root.type = bfd_link_hash_defined;
11564 h->root.u.def.section = stub_entry->group->stub_sec;
11565 h->root.u.def.value = stub_entry->stub_offset;
11566 h->ref_regular = 1;
11567 h->def_regular = 1;
11568 h->ref_regular_nonweak = 1;
11569 h->forced_local = 1;
11570 h->non_elf = 0;
11571 h->root.linker_def = 1;
11572 }
11573 }
11574
11575 return TRUE;
11576 }
11577
11578 /* As above, but don't actually build the stub. Just bump offset so
11579 we know stub section sizes, and select plt_branch stubs where
11580 long_branch stubs won't do. */
11581
11582 static bfd_boolean
11583 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11584 {
11585 struct ppc_stub_hash_entry *stub_entry;
11586 struct bfd_link_info *info;
11587 struct ppc_link_hash_table *htab;
11588 asection *plt;
11589 bfd_vma targ, off, r2off;
11590 unsigned int size, extra, lr_used, delta, odd;
11591
11592 /* Massage our args to the form they really have. */
11593 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11594 info = in_arg;
11595
11596 htab = ppc_hash_table (info);
11597 if (htab == NULL)
11598 return FALSE;
11599
11600 /* Make a note of the offset within the stubs for this entry. */
11601 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11602
11603 if (stub_entry->h != NULL
11604 && stub_entry->h->save_res
11605 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11606 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11607 {
11608 /* Don't make stubs to out-of-line register save/restore
11609 functions. Instead, emit copies of the functions. */
11610 stub_entry->group->needs_save_res = 1;
11611 stub_entry->stub_type = ppc_stub_save_res;
11612 return TRUE;
11613 }
11614
11615 switch (stub_entry->stub_type)
11616 {
11617 case ppc_stub_plt_branch:
11618 case ppc_stub_plt_branch_r2off:
11619 /* Reset the stub type from the plt branch variant in case we now
11620 can reach with a shorter stub. */
11621 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11622 /* Fall through. */
11623 case ppc_stub_long_branch:
11624 case ppc_stub_long_branch_r2off:
11625 targ = (stub_entry->target_value
11626 + stub_entry->target_section->output_offset
11627 + stub_entry->target_section->output_section->vma);
11628 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11629 off = (stub_entry->stub_offset
11630 + stub_entry->group->stub_sec->output_offset
11631 + stub_entry->group->stub_sec->output_section->vma);
11632
11633 size = 4;
11634 r2off = 0;
11635 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11636 {
11637 r2off = get_r2off (info, stub_entry);
11638 if (r2off == (bfd_vma) -1)
11639 {
11640 htab->stub_error = TRUE;
11641 return FALSE;
11642 }
11643 size = 8;
11644 if (PPC_HA (r2off) != 0)
11645 size += 4;
11646 if (PPC_LO (r2off) != 0)
11647 size += 4;
11648 off += size - 4;
11649 }
11650 off = targ - off;
11651
11652 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11653 Do the same for -R objects without function descriptors. */
11654 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11655 && r2off == 0
11656 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11657 || off + (1 << 25) >= (bfd_vma) (1 << 26))
11658 {
11659 struct ppc_branch_hash_entry *br_entry;
11660
11661 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11662 stub_entry->root.string + 9,
11663 TRUE, FALSE);
11664 if (br_entry == NULL)
11665 {
11666 _bfd_error_handler (_("can't build branch stub `%s'"),
11667 stub_entry->root.string);
11668 htab->stub_error = TRUE;
11669 return FALSE;
11670 }
11671
11672 if (br_entry->iter != htab->stub_iteration)
11673 {
11674 br_entry->iter = htab->stub_iteration;
11675 br_entry->offset = htab->brlt->size;
11676 htab->brlt->size += 8;
11677
11678 if (htab->relbrlt != NULL)
11679 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11680 else if (info->emitrelocations)
11681 {
11682 htab->brlt->reloc_count += 1;
11683 htab->brlt->flags |= SEC_RELOC;
11684 }
11685 }
11686
11687 targ = (br_entry->offset
11688 + htab->brlt->output_offset
11689 + htab->brlt->output_section->vma);
11690 off = (elf_gp (info->output_bfd)
11691 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11692 off = targ - off;
11693
11694 if (info->emitrelocations)
11695 {
11696 stub_entry->group->stub_sec->reloc_count
11697 += 1 + (PPC_HA (off) != 0);
11698 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11699 }
11700
11701 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11702 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11703 {
11704 size = 12;
11705 if (PPC_HA (off) != 0)
11706 size = 16;
11707 }
11708 else
11709 {
11710 size = 16;
11711 if (PPC_HA (off) != 0)
11712 size += 4;
11713
11714 if (PPC_HA (r2off) != 0)
11715 size += 4;
11716 if (PPC_LO (r2off) != 0)
11717 size += 4;
11718 }
11719 }
11720 else if (info->emitrelocations)
11721 {
11722 stub_entry->group->stub_sec->reloc_count += 1;
11723 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11724 }
11725 break;
11726
11727 case ppc_stub_plt_branch_notoc:
11728 case ppc_stub_plt_branch_both:
11729 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11730 /* Fall through. */
11731 case ppc_stub_long_branch_notoc:
11732 case ppc_stub_long_branch_both:
11733 off = (stub_entry->stub_offset
11734 + stub_entry->group->stub_sec->output_offset
11735 + stub_entry->group->stub_sec->output_section->vma);
11736 size = 0;
11737 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11738 size = 4;
11739 off += size;
11740 targ = (stub_entry->target_value
11741 + stub_entry->target_section->output_offset
11742 + stub_entry->target_section->output_section->vma);
11743 odd = off & 4;
11744 off = targ - off;
11745
11746 if (info->emitrelocations)
11747 {
11748 unsigned int num_rel;
11749 if (htab->powerxx_stubs)
11750 num_rel = num_relocs_for_powerxx_offset (off, odd);
11751 else
11752 num_rel = num_relocs_for_offset (off - 8);
11753 stub_entry->group->stub_sec->reloc_count += num_rel;
11754 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11755 }
11756
11757 if (htab->powerxx_stubs)
11758 extra = size_powerxx_offset (off, odd);
11759 else
11760 extra = size_offset (off - 8);
11761 /* Include branch insn plus those in the offset sequence. */
11762 size += 4 + extra;
11763 /* The branch insn is at the end, or "extra" bytes along. So
11764 its offset will be "extra" bytes less that that already
11765 calculated. */
11766 off -= extra;
11767
11768 if (!htab->powerxx_stubs)
11769 {
11770 /* After the bcl, lr has been modified so we need to emit
11771 .eh_frame info saying the return address is in r12. */
11772 lr_used = stub_entry->stub_offset + 8;
11773 if (stub_entry->stub_type == ppc_stub_long_branch_both)
11774 lr_used += 4;
11775 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11776 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11777 DW_CFA_restore_extended 65. */
11778 delta = lr_used - stub_entry->group->lr_restore;
11779 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11780 stub_entry->group->lr_restore = lr_used + 8;
11781 }
11782
11783 /* If the branch can't reach, use a plt_branch. */
11784 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11785 {
11786 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11787 - ppc_stub_long_branch_notoc);
11788 size += 4;
11789 }
11790 else if (info->emitrelocations)
11791 stub_entry->group->stub_sec->reloc_count +=1;
11792 break;
11793
11794 case ppc_stub_plt_call_notoc:
11795 case ppc_stub_plt_call_both:
11796 off = (stub_entry->stub_offset
11797 + stub_entry->group->stub_sec->output_offset
11798 + stub_entry->group->stub_sec->output_section->vma);
11799 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11800 off += 4;
11801 targ = stub_entry->plt_ent->plt.offset & ~1;
11802 if (targ >= (bfd_vma) -2)
11803 abort ();
11804
11805 plt = htab->elf.splt;
11806 if (!htab->elf.dynamic_sections_created
11807 || stub_entry->h == NULL
11808 || stub_entry->h->elf.dynindx == -1)
11809 {
11810 if (stub_entry->symtype == STT_GNU_IFUNC)
11811 plt = htab->elf.iplt;
11812 else
11813 plt = htab->pltlocal;
11814 }
11815 targ += plt->output_offset + plt->output_section->vma;
11816 odd = off & 4;
11817 off = targ - off;
11818
11819 if (htab->params->plt_stub_align != 0)
11820 {
11821 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11822
11823 stub_entry->group->stub_sec->size += pad;
11824 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11825 off -= pad;
11826 }
11827
11828 if (info->emitrelocations)
11829 {
11830 unsigned int num_rel;
11831 if (htab->powerxx_stubs)
11832 num_rel = num_relocs_for_powerxx_offset (off, odd);
11833 else
11834 num_rel = num_relocs_for_offset (off - 8);
11835 stub_entry->group->stub_sec->reloc_count += num_rel;
11836 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11837 }
11838
11839 size = plt_stub_size (htab, stub_entry, off);
11840
11841 if (!htab->powerxx_stubs)
11842 {
11843 /* After the bcl, lr has been modified so we need to emit
11844 .eh_frame info saying the return address is in r12. */
11845 lr_used = stub_entry->stub_offset + 8;
11846 if (stub_entry->stub_type == ppc_stub_plt_call_both)
11847 lr_used += 4;
11848 /* The eh_frame info will consist of a DW_CFA_advance_loc or
11849 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11850 DW_CFA_restore_extended 65. */
11851 delta = lr_used - stub_entry->group->lr_restore;
11852 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11853 stub_entry->group->lr_restore = lr_used + 8;
11854 }
11855 break;
11856
11857 case ppc_stub_plt_call:
11858 case ppc_stub_plt_call_r2save:
11859 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11860 if (targ >= (bfd_vma) -2)
11861 abort ();
11862 plt = htab->elf.splt;
11863 if (!htab->elf.dynamic_sections_created
11864 || stub_entry->h == NULL
11865 || stub_entry->h->elf.dynindx == -1)
11866 {
11867 if (stub_entry->symtype == STT_GNU_IFUNC)
11868 plt = htab->elf.iplt;
11869 else
11870 plt = htab->pltlocal;
11871 }
11872 targ += plt->output_offset + plt->output_section->vma;
11873
11874 off = (elf_gp (info->output_bfd)
11875 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11876 off = targ - off;
11877
11878 if (htab->params->plt_stub_align != 0)
11879 {
11880 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11881
11882 stub_entry->group->stub_sec->size += pad;
11883 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11884 }
11885
11886 if (info->emitrelocations)
11887 {
11888 stub_entry->group->stub_sec->reloc_count
11889 += ((PPC_HA (off) != 0)
11890 + (htab->opd_abi
11891 ? 2 + (htab->params->plt_static_chain
11892 && PPC_HA (off + 16) == PPC_HA (off))
11893 : 1));
11894 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11895 }
11896
11897 size = plt_stub_size (htab, stub_entry, off);
11898
11899 if (stub_entry->h != NULL
11900 && (stub_entry->h == htab->tls_get_addr_fd
11901 || stub_entry->h == htab->tls_get_addr)
11902 && htab->params->tls_get_addr_opt
11903 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11904 {
11905 /* After the bctrl, lr has been modified so we need to
11906 emit .eh_frame info saying the return address is
11907 on the stack. In fact we put the EH info specifying
11908 that the return address is on the stack *at* the
11909 call rather than after it, because the EH info for a
11910 call needs to be specified by that point.
11911 See libgcc/unwind-dw2.c execute_cfa_program. */
11912 lr_used = stub_entry->stub_offset + size - 20;
11913 /* The eh_frame info will consist of a DW_CFA_advance_loc
11914 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11915 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
11916 delta = lr_used - stub_entry->group->lr_restore;
11917 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11918 stub_entry->group->lr_restore = size - 4;
11919 }
11920 break;
11921
11922 default:
11923 BFD_FAIL ();
11924 return FALSE;
11925 }
11926
11927 stub_entry->group->stub_sec->size += size;
11928 return TRUE;
11929 }
11930
11931 /* Set up various things so that we can make a list of input sections
11932 for each output section included in the link. Returns -1 on error,
11933 0 when no stubs will be needed, and 1 on success. */
11934
11935 int
11936 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11937 {
11938 unsigned int id;
11939 bfd_size_type amt;
11940 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11941
11942 if (htab == NULL)
11943 return -1;
11944
11945 htab->sec_info_arr_size = _bfd_section_id;
11946 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11947 htab->sec_info = bfd_zmalloc (amt);
11948 if (htab->sec_info == NULL)
11949 return -1;
11950
11951 /* Set toc_off for com, und, abs and ind sections. */
11952 for (id = 0; id < 3; id++)
11953 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11954
11955 return 1;
11956 }
11957
11958 /* Set up for first pass at multitoc partitioning. */
11959
11960 void
11961 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11962 {
11963 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11964
11965 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11966 htab->toc_bfd = NULL;
11967 htab->toc_first_sec = NULL;
11968 }
11969
11970 /* The linker repeatedly calls this function for each TOC input section
11971 and linker generated GOT section. Group input bfds such that the toc
11972 within a group is less than 64k in size. */
11973
11974 bfd_boolean
11975 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11976 {
11977 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11978 bfd_vma addr, off, limit;
11979
11980 if (htab == NULL)
11981 return FALSE;
11982
11983 if (!htab->second_toc_pass)
11984 {
11985 /* Keep track of the first .toc or .got section for this input bfd. */
11986 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11987
11988 if (new_bfd)
11989 {
11990 htab->toc_bfd = isec->owner;
11991 htab->toc_first_sec = isec;
11992 }
11993
11994 addr = isec->output_offset + isec->output_section->vma;
11995 off = addr - htab->toc_curr;
11996 limit = 0x80008000;
11997 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11998 limit = 0x10000;
11999 if (off + isec->size > limit)
12000 {
12001 addr = (htab->toc_first_sec->output_offset
12002 + htab->toc_first_sec->output_section->vma);
12003 htab->toc_curr = addr;
12004 htab->toc_curr &= -TOC_BASE_ALIGN;
12005 }
12006
12007 /* toc_curr is the base address of this toc group. Set elf_gp
12008 for the input section to be the offset relative to the
12009 output toc base plus 0x8000. Making the input elf_gp an
12010 offset allows us to move the toc as a whole without
12011 recalculating input elf_gp. */
12012 off = htab->toc_curr - elf_gp (info->output_bfd);
12013 off += TOC_BASE_OFF;
12014
12015 /* Die if someone uses a linker script that doesn't keep input
12016 file .toc and .got together. */
12017 if (new_bfd
12018 && elf_gp (isec->owner) != 0
12019 && elf_gp (isec->owner) != off)
12020 return FALSE;
12021
12022 elf_gp (isec->owner) = off;
12023 return TRUE;
12024 }
12025
12026 /* During the second pass toc_first_sec points to the start of
12027 a toc group, and toc_curr is used to track the old elf_gp.
12028 We use toc_bfd to ensure we only look at each bfd once. */
12029 if (htab->toc_bfd == isec->owner)
12030 return TRUE;
12031 htab->toc_bfd = isec->owner;
12032
12033 if (htab->toc_first_sec == NULL
12034 || htab->toc_curr != elf_gp (isec->owner))
12035 {
12036 htab->toc_curr = elf_gp (isec->owner);
12037 htab->toc_first_sec = isec;
12038 }
12039 addr = (htab->toc_first_sec->output_offset
12040 + htab->toc_first_sec->output_section->vma);
12041 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12042 elf_gp (isec->owner) = off;
12043
12044 return TRUE;
12045 }
12046
12047 /* Called via elf_link_hash_traverse to merge GOT entries for global
12048 symbol H. */
12049
12050 static bfd_boolean
12051 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12052 {
12053 if (h->root.type == bfd_link_hash_indirect)
12054 return TRUE;
12055
12056 merge_got_entries (&h->got.glist);
12057
12058 return TRUE;
12059 }
12060
12061 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12062 symbol H. */
12063
12064 static bfd_boolean
12065 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12066 {
12067 struct got_entry *gent;
12068
12069 if (h->root.type == bfd_link_hash_indirect)
12070 return TRUE;
12071
12072 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12073 if (!gent->is_indirect)
12074 allocate_got (h, (struct bfd_link_info *) inf, gent);
12075 return TRUE;
12076 }
12077
12078 /* Called on the first multitoc pass after the last call to
12079 ppc64_elf_next_toc_section. This function removes duplicate GOT
12080 entries. */
12081
12082 bfd_boolean
12083 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12084 {
12085 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12086 struct bfd *ibfd, *ibfd2;
12087 bfd_boolean done_something;
12088
12089 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12090
12091 if (!htab->do_multi_toc)
12092 return FALSE;
12093
12094 /* Merge global sym got entries within a toc group. */
12095 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12096
12097 /* And tlsld_got. */
12098 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12099 {
12100 struct got_entry *ent, *ent2;
12101
12102 if (!is_ppc64_elf (ibfd))
12103 continue;
12104
12105 ent = ppc64_tlsld_got (ibfd);
12106 if (!ent->is_indirect
12107 && ent->got.offset != (bfd_vma) -1)
12108 {
12109 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12110 {
12111 if (!is_ppc64_elf (ibfd2))
12112 continue;
12113
12114 ent2 = ppc64_tlsld_got (ibfd2);
12115 if (!ent2->is_indirect
12116 && ent2->got.offset != (bfd_vma) -1
12117 && elf_gp (ibfd2) == elf_gp (ibfd))
12118 {
12119 ent2->is_indirect = TRUE;
12120 ent2->got.ent = ent;
12121 }
12122 }
12123 }
12124 }
12125
12126 /* Zap sizes of got sections. */
12127 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12128 htab->elf.irelplt->size -= htab->got_reli_size;
12129 htab->got_reli_size = 0;
12130
12131 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12132 {
12133 asection *got, *relgot;
12134
12135 if (!is_ppc64_elf (ibfd))
12136 continue;
12137
12138 got = ppc64_elf_tdata (ibfd)->got;
12139 if (got != NULL)
12140 {
12141 got->rawsize = got->size;
12142 got->size = 0;
12143 relgot = ppc64_elf_tdata (ibfd)->relgot;
12144 relgot->rawsize = relgot->size;
12145 relgot->size = 0;
12146 }
12147 }
12148
12149 /* Now reallocate the got, local syms first. We don't need to
12150 allocate section contents again since we never increase size. */
12151 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12152 {
12153 struct got_entry **lgot_ents;
12154 struct got_entry **end_lgot_ents;
12155 struct plt_entry **local_plt;
12156 struct plt_entry **end_local_plt;
12157 unsigned char *lgot_masks;
12158 bfd_size_type locsymcount;
12159 Elf_Internal_Shdr *symtab_hdr;
12160 asection *s;
12161
12162 if (!is_ppc64_elf (ibfd))
12163 continue;
12164
12165 lgot_ents = elf_local_got_ents (ibfd);
12166 if (!lgot_ents)
12167 continue;
12168
12169 symtab_hdr = &elf_symtab_hdr (ibfd);
12170 locsymcount = symtab_hdr->sh_info;
12171 end_lgot_ents = lgot_ents + locsymcount;
12172 local_plt = (struct plt_entry **) end_lgot_ents;
12173 end_local_plt = local_plt + locsymcount;
12174 lgot_masks = (unsigned char *) end_local_plt;
12175 s = ppc64_elf_tdata (ibfd)->got;
12176 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12177 {
12178 struct got_entry *ent;
12179
12180 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12181 {
12182 unsigned int ent_size = 8;
12183 unsigned int rel_size = sizeof (Elf64_External_Rela);
12184
12185 ent->got.offset = s->size;
12186 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12187 {
12188 ent_size *= 2;
12189 rel_size *= 2;
12190 }
12191 s->size += ent_size;
12192 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12193 {
12194 htab->elf.irelplt->size += rel_size;
12195 htab->got_reli_size += rel_size;
12196 }
12197 else if (bfd_link_pic (info)
12198 && !(ent->tls_type != 0
12199 && bfd_link_executable (info)))
12200 {
12201 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12202 srel->size += rel_size;
12203 }
12204 }
12205 }
12206 }
12207
12208 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12209
12210 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12211 {
12212 struct got_entry *ent;
12213
12214 if (!is_ppc64_elf (ibfd))
12215 continue;
12216
12217 ent = ppc64_tlsld_got (ibfd);
12218 if (!ent->is_indirect
12219 && ent->got.offset != (bfd_vma) -1)
12220 {
12221 asection *s = ppc64_elf_tdata (ibfd)->got;
12222 ent->got.offset = s->size;
12223 s->size += 16;
12224 if (bfd_link_dll (info))
12225 {
12226 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12227 srel->size += sizeof (Elf64_External_Rela);
12228 }
12229 }
12230 }
12231
12232 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12233 if (!done_something)
12234 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12235 {
12236 asection *got;
12237
12238 if (!is_ppc64_elf (ibfd))
12239 continue;
12240
12241 got = ppc64_elf_tdata (ibfd)->got;
12242 if (got != NULL)
12243 {
12244 done_something = got->rawsize != got->size;
12245 if (done_something)
12246 break;
12247 }
12248 }
12249
12250 if (done_something)
12251 (*htab->params->layout_sections_again) ();
12252
12253 /* Set up for second pass over toc sections to recalculate elf_gp
12254 on input sections. */
12255 htab->toc_bfd = NULL;
12256 htab->toc_first_sec = NULL;
12257 htab->second_toc_pass = TRUE;
12258 return done_something;
12259 }
12260
12261 /* Called after second pass of multitoc partitioning. */
12262
12263 void
12264 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12265 {
12266 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12267
12268 /* After the second pass, toc_curr tracks the TOC offset used
12269 for code sections below in ppc64_elf_next_input_section. */
12270 htab->toc_curr = TOC_BASE_OFF;
12271 }
12272
12273 /* No toc references were found in ISEC. If the code in ISEC makes no
12274 calls, then there's no need to use toc adjusting stubs when branching
12275 into ISEC. Actually, indirect calls from ISEC are OK as they will
12276 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12277 needed, and 2 if a cyclical call-graph was found but no other reason
12278 for a stub was detected. If called from the top level, a return of
12279 2 means the same as a return of 0. */
12280
12281 static int
12282 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12283 {
12284 int ret;
12285
12286 /* Mark this section as checked. */
12287 isec->call_check_done = 1;
12288
12289 /* We know none of our code bearing sections will need toc stubs. */
12290 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12291 return 0;
12292
12293 if (isec->size == 0)
12294 return 0;
12295
12296 if (isec->output_section == NULL)
12297 return 0;
12298
12299 ret = 0;
12300 if (isec->reloc_count != 0)
12301 {
12302 Elf_Internal_Rela *relstart, *rel;
12303 Elf_Internal_Sym *local_syms;
12304 struct ppc_link_hash_table *htab;
12305
12306 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12307 info->keep_memory);
12308 if (relstart == NULL)
12309 return -1;
12310
12311 /* Look for branches to outside of this section. */
12312 local_syms = NULL;
12313 htab = ppc_hash_table (info);
12314 if (htab == NULL)
12315 return -1;
12316
12317 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12318 {
12319 enum elf_ppc64_reloc_type r_type;
12320 unsigned long r_symndx;
12321 struct elf_link_hash_entry *h;
12322 struct ppc_link_hash_entry *eh;
12323 Elf_Internal_Sym *sym;
12324 asection *sym_sec;
12325 struct _opd_sec_data *opd;
12326 bfd_vma sym_value;
12327 bfd_vma dest;
12328
12329 r_type = ELF64_R_TYPE (rel->r_info);
12330 if (r_type != R_PPC64_REL24
12331 && r_type != R_PPC64_REL24_NOTOC
12332 && r_type != R_PPC64_REL14
12333 && r_type != R_PPC64_REL14_BRTAKEN
12334 && r_type != R_PPC64_REL14_BRNTAKEN
12335 && r_type != R_PPC64_PLTCALL
12336 && r_type != R_PPC64_PLTCALL_NOTOC)
12337 continue;
12338
12339 r_symndx = ELF64_R_SYM (rel->r_info);
12340 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12341 isec->owner))
12342 {
12343 ret = -1;
12344 break;
12345 }
12346
12347 /* Calls to dynamic lib functions go through a plt call stub
12348 that uses r2. */
12349 eh = (struct ppc_link_hash_entry *) h;
12350 if (eh != NULL
12351 && (eh->elf.plt.plist != NULL
12352 || (eh->oh != NULL
12353 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12354 {
12355 ret = 1;
12356 break;
12357 }
12358
12359 if (sym_sec == NULL)
12360 /* Ignore other undefined symbols. */
12361 continue;
12362
12363 /* Assume branches to other sections not included in the
12364 link need stubs too, to cover -R and absolute syms. */
12365 if (sym_sec->output_section == NULL)
12366 {
12367 ret = 1;
12368 break;
12369 }
12370
12371 if (h == NULL)
12372 sym_value = sym->st_value;
12373 else
12374 {
12375 if (h->root.type != bfd_link_hash_defined
12376 && h->root.type != bfd_link_hash_defweak)
12377 abort ();
12378 sym_value = h->root.u.def.value;
12379 }
12380 sym_value += rel->r_addend;
12381
12382 /* If this branch reloc uses an opd sym, find the code section. */
12383 opd = get_opd_info (sym_sec);
12384 if (opd != NULL)
12385 {
12386 if (h == NULL && opd->adjust != NULL)
12387 {
12388 long adjust;
12389
12390 adjust = opd->adjust[OPD_NDX (sym_value)];
12391 if (adjust == -1)
12392 /* Assume deleted functions won't ever be called. */
12393 continue;
12394 sym_value += adjust;
12395 }
12396
12397 dest = opd_entry_value (sym_sec, sym_value,
12398 &sym_sec, NULL, FALSE);
12399 if (dest == (bfd_vma) -1)
12400 continue;
12401 }
12402 else
12403 dest = (sym_value
12404 + sym_sec->output_offset
12405 + sym_sec->output_section->vma);
12406
12407 /* Ignore branch to self. */
12408 if (sym_sec == isec)
12409 continue;
12410
12411 /* If the called function uses the toc, we need a stub. */
12412 if (sym_sec->has_toc_reloc
12413 || sym_sec->makes_toc_func_call)
12414 {
12415 ret = 1;
12416 break;
12417 }
12418
12419 /* Assume any branch that needs a long branch stub might in fact
12420 need a plt_branch stub. A plt_branch stub uses r2. */
12421 else if (dest - (isec->output_offset
12422 + isec->output_section->vma
12423 + rel->r_offset) + (1 << 25)
12424 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12425 ? h->other
12426 : sym->st_other))
12427 {
12428 ret = 1;
12429 break;
12430 }
12431
12432 /* If calling back to a section in the process of being
12433 tested, we can't say for sure that no toc adjusting stubs
12434 are needed, so don't return zero. */
12435 else if (sym_sec->call_check_in_progress)
12436 ret = 2;
12437
12438 /* Branches to another section that itself doesn't have any TOC
12439 references are OK. Recursively call ourselves to check. */
12440 else if (!sym_sec->call_check_done)
12441 {
12442 int recur;
12443
12444 /* Mark current section as indeterminate, so that other
12445 sections that call back to current won't be marked as
12446 known. */
12447 isec->call_check_in_progress = 1;
12448 recur = toc_adjusting_stub_needed (info, sym_sec);
12449 isec->call_check_in_progress = 0;
12450
12451 if (recur != 0)
12452 {
12453 ret = recur;
12454 if (recur != 2)
12455 break;
12456 }
12457 }
12458 }
12459
12460 if (local_syms != NULL
12461 && (elf_symtab_hdr (isec->owner).contents
12462 != (unsigned char *) local_syms))
12463 free (local_syms);
12464 if (elf_section_data (isec)->relocs != relstart)
12465 free (relstart);
12466 }
12467
12468 if ((ret & 1) == 0
12469 && isec->map_head.s != NULL
12470 && (strcmp (isec->output_section->name, ".init") == 0
12471 || strcmp (isec->output_section->name, ".fini") == 0))
12472 {
12473 if (isec->map_head.s->has_toc_reloc
12474 || isec->map_head.s->makes_toc_func_call)
12475 ret = 1;
12476 else if (!isec->map_head.s->call_check_done)
12477 {
12478 int recur;
12479 isec->call_check_in_progress = 1;
12480 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12481 isec->call_check_in_progress = 0;
12482 if (recur != 0)
12483 ret = recur;
12484 }
12485 }
12486
12487 if (ret == 1)
12488 isec->makes_toc_func_call = 1;
12489
12490 return ret;
12491 }
12492
12493 /* The linker repeatedly calls this function for each input section,
12494 in the order that input sections are linked into output sections.
12495 Build lists of input sections to determine groupings between which
12496 we may insert linker stubs. */
12497
12498 bfd_boolean
12499 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12500 {
12501 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12502
12503 if (htab == NULL)
12504 return FALSE;
12505
12506 if ((isec->output_section->flags & SEC_CODE) != 0
12507 && isec->output_section->id < htab->sec_info_arr_size)
12508 {
12509 /* This happens to make the list in reverse order,
12510 which is what we want. */
12511 htab->sec_info[isec->id].u.list
12512 = htab->sec_info[isec->output_section->id].u.list;
12513 htab->sec_info[isec->output_section->id].u.list = isec;
12514 }
12515
12516 if (htab->multi_toc_needed)
12517 {
12518 /* Analyse sections that aren't already flagged as needing a
12519 valid toc pointer. Exclude .fixup for the linux kernel.
12520 .fixup contains branches, but only back to the function that
12521 hit an exception. */
12522 if (!(isec->has_toc_reloc
12523 || (isec->flags & SEC_CODE) == 0
12524 || strcmp (isec->name, ".fixup") == 0
12525 || isec->call_check_done))
12526 {
12527 if (toc_adjusting_stub_needed (info, isec) < 0)
12528 return FALSE;
12529 }
12530 /* Make all sections use the TOC assigned for this object file.
12531 This will be wrong for pasted sections; We fix that in
12532 check_pasted_section(). */
12533 if (elf_gp (isec->owner) != 0)
12534 htab->toc_curr = elf_gp (isec->owner);
12535 }
12536
12537 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12538 return TRUE;
12539 }
12540
12541 /* Check that all .init and .fini sections use the same toc, if they
12542 have toc relocs. */
12543
12544 static bfd_boolean
12545 check_pasted_section (struct bfd_link_info *info, const char *name)
12546 {
12547 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12548
12549 if (o != NULL)
12550 {
12551 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12552 bfd_vma toc_off = 0;
12553 asection *i;
12554
12555 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12556 if (i->has_toc_reloc)
12557 {
12558 if (toc_off == 0)
12559 toc_off = htab->sec_info[i->id].toc_off;
12560 else if (toc_off != htab->sec_info[i->id].toc_off)
12561 return FALSE;
12562 }
12563
12564 if (toc_off == 0)
12565 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12566 if (i->makes_toc_func_call)
12567 {
12568 toc_off = htab->sec_info[i->id].toc_off;
12569 break;
12570 }
12571
12572 /* Make sure the whole pasted function uses the same toc offset. */
12573 if (toc_off != 0)
12574 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12575 htab->sec_info[i->id].toc_off = toc_off;
12576 }
12577 return TRUE;
12578 }
12579
12580 bfd_boolean
12581 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12582 {
12583 return (check_pasted_section (info, ".init")
12584 & check_pasted_section (info, ".fini"));
12585 }
12586
12587 /* See whether we can group stub sections together. Grouping stub
12588 sections may result in fewer stubs. More importantly, we need to
12589 put all .init* and .fini* stubs at the beginning of the .init or
12590 .fini output sections respectively, because glibc splits the
12591 _init and _fini functions into multiple parts. Putting a stub in
12592 the middle of a function is not a good idea. */
12593
12594 static bfd_boolean
12595 group_sections (struct bfd_link_info *info,
12596 bfd_size_type stub_group_size,
12597 bfd_boolean stubs_always_before_branch)
12598 {
12599 struct ppc_link_hash_table *htab;
12600 asection *osec;
12601 bfd_boolean suppress_size_errors;
12602
12603 htab = ppc_hash_table (info);
12604 if (htab == NULL)
12605 return FALSE;
12606
12607 suppress_size_errors = FALSE;
12608 if (stub_group_size == 1)
12609 {
12610 /* Default values. */
12611 if (stubs_always_before_branch)
12612 stub_group_size = 0x1e00000;
12613 else
12614 stub_group_size = 0x1c00000;
12615 suppress_size_errors = TRUE;
12616 }
12617
12618 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12619 {
12620 asection *tail;
12621
12622 if (osec->id >= htab->sec_info_arr_size)
12623 continue;
12624
12625 tail = htab->sec_info[osec->id].u.list;
12626 while (tail != NULL)
12627 {
12628 asection *curr;
12629 asection *prev;
12630 bfd_size_type total;
12631 bfd_boolean big_sec;
12632 bfd_vma curr_toc;
12633 struct map_stub *group;
12634 bfd_size_type group_size;
12635
12636 curr = tail;
12637 total = tail->size;
12638 group_size = (ppc64_elf_section_data (tail) != NULL
12639 && ppc64_elf_section_data (tail)->has_14bit_branch
12640 ? stub_group_size >> 10 : stub_group_size);
12641
12642 big_sec = total > group_size;
12643 if (big_sec && !suppress_size_errors)
12644 /* xgettext:c-format */
12645 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12646 tail->owner, tail);
12647 curr_toc = htab->sec_info[tail->id].toc_off;
12648
12649 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12650 && ((total += curr->output_offset - prev->output_offset)
12651 < (ppc64_elf_section_data (prev) != NULL
12652 && ppc64_elf_section_data (prev)->has_14bit_branch
12653 ? (group_size = stub_group_size >> 10) : group_size))
12654 && htab->sec_info[prev->id].toc_off == curr_toc)
12655 curr = prev;
12656
12657 /* OK, the size from the start of CURR to the end is less
12658 than group_size and thus can be handled by one stub
12659 section. (or the tail section is itself larger than
12660 group_size, in which case we may be toast.) We should
12661 really be keeping track of the total size of stubs added
12662 here, as stubs contribute to the final output section
12663 size. That's a little tricky, and this way will only
12664 break if stubs added make the total size more than 2^25,
12665 ie. for the default stub_group_size, if stubs total more
12666 than 2097152 bytes, or nearly 75000 plt call stubs. */
12667 group = bfd_alloc (curr->owner, sizeof (*group));
12668 if (group == NULL)
12669 return FALSE;
12670 group->link_sec = curr;
12671 group->stub_sec = NULL;
12672 group->needs_save_res = 0;
12673 group->lr_restore = 0;
12674 group->eh_size = 0;
12675 group->eh_base = 0;
12676 group->next = htab->group;
12677 htab->group = group;
12678 do
12679 {
12680 prev = htab->sec_info[tail->id].u.list;
12681 /* Set up this stub group. */
12682 htab->sec_info[tail->id].u.group = group;
12683 }
12684 while (tail != curr && (tail = prev) != NULL);
12685
12686 /* But wait, there's more! Input sections up to group_size
12687 bytes before the stub section can be handled by it too.
12688 Don't do this if we have a really large section after the
12689 stubs, as adding more stubs increases the chance that
12690 branches may not reach into the stub section. */
12691 if (!stubs_always_before_branch && !big_sec)
12692 {
12693 total = 0;
12694 while (prev != NULL
12695 && ((total += tail->output_offset - prev->output_offset)
12696 < (ppc64_elf_section_data (prev) != NULL
12697 && ppc64_elf_section_data (prev)->has_14bit_branch
12698 ? (group_size = stub_group_size >> 10)
12699 : group_size))
12700 && htab->sec_info[prev->id].toc_off == curr_toc)
12701 {
12702 tail = prev;
12703 prev = htab->sec_info[tail->id].u.list;
12704 htab->sec_info[tail->id].u.group = group;
12705 }
12706 }
12707 tail = prev;
12708 }
12709 }
12710 return TRUE;
12711 }
12712
12713 static const unsigned char glink_eh_frame_cie[] =
12714 {
12715 0, 0, 0, 16, /* length. */
12716 0, 0, 0, 0, /* id. */
12717 1, /* CIE version. */
12718 'z', 'R', 0, /* Augmentation string. */
12719 4, /* Code alignment. */
12720 0x78, /* Data alignment. */
12721 65, /* RA reg. */
12722 1, /* Augmentation size. */
12723 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12724 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12725 };
12726
12727 /* Stripping output sections is normally done before dynamic section
12728 symbols have been allocated. This function is called later, and
12729 handles cases like htab->brlt which is mapped to its own output
12730 section. */
12731
12732 static void
12733 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12734 {
12735 if (isec->size == 0
12736 && isec->output_section->size == 0
12737 && !(isec->output_section->flags & SEC_KEEP)
12738 && !bfd_section_removed_from_list (info->output_bfd,
12739 isec->output_section)
12740 && elf_section_data (isec->output_section)->dynindx == 0)
12741 {
12742 isec->output_section->flags |= SEC_EXCLUDE;
12743 bfd_section_list_remove (info->output_bfd, isec->output_section);
12744 info->output_bfd->section_count--;
12745 }
12746 }
12747
12748 /* Determine and set the size of the stub section for a final link.
12749
12750 The basic idea here is to examine all the relocations looking for
12751 PC-relative calls to a target that is unreachable with a "bl"
12752 instruction. */
12753
12754 bfd_boolean
12755 ppc64_elf_size_stubs (struct bfd_link_info *info)
12756 {
12757 bfd_size_type stub_group_size;
12758 bfd_boolean stubs_always_before_branch;
12759 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12760
12761 if (htab == NULL)
12762 return FALSE;
12763
12764 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12765 htab->params->plt_thread_safe = 1;
12766 if (!htab->opd_abi)
12767 htab->params->plt_thread_safe = 0;
12768 else if (htab->params->plt_thread_safe == -1)
12769 {
12770 static const char *const thread_starter[] =
12771 {
12772 "pthread_create",
12773 /* libstdc++ */
12774 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12775 /* librt */
12776 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12777 "mq_notify", "create_timer",
12778 /* libanl */
12779 "getaddrinfo_a",
12780 /* libgomp */
12781 "GOMP_parallel",
12782 "GOMP_parallel_start",
12783 "GOMP_parallel_loop_static",
12784 "GOMP_parallel_loop_static_start",
12785 "GOMP_parallel_loop_dynamic",
12786 "GOMP_parallel_loop_dynamic_start",
12787 "GOMP_parallel_loop_guided",
12788 "GOMP_parallel_loop_guided_start",
12789 "GOMP_parallel_loop_runtime",
12790 "GOMP_parallel_loop_runtime_start",
12791 "GOMP_parallel_sections",
12792 "GOMP_parallel_sections_start",
12793 /* libgo */
12794 "__go_go",
12795 };
12796 unsigned i;
12797
12798 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12799 {
12800 struct elf_link_hash_entry *h;
12801 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12802 FALSE, FALSE, TRUE);
12803 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12804 if (htab->params->plt_thread_safe)
12805 break;
12806 }
12807 }
12808 stubs_always_before_branch = htab->params->group_size < 0;
12809 if (htab->params->group_size < 0)
12810 stub_group_size = -htab->params->group_size;
12811 else
12812 stub_group_size = htab->params->group_size;
12813
12814 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12815 return FALSE;
12816
12817 #define STUB_SHRINK_ITER 20
12818 /* Loop until no stubs added. After iteration 20 of this loop we may
12819 exit on a stub section shrinking. This is to break out of a
12820 pathological case where adding stubs on one iteration decreases
12821 section gaps (perhaps due to alignment), which then requires
12822 fewer or smaller stubs on the next iteration. */
12823
12824 while (1)
12825 {
12826 bfd *input_bfd;
12827 unsigned int bfd_indx;
12828 struct map_stub *group;
12829
12830 htab->stub_iteration += 1;
12831
12832 for (input_bfd = info->input_bfds, bfd_indx = 0;
12833 input_bfd != NULL;
12834 input_bfd = input_bfd->link.next, bfd_indx++)
12835 {
12836 Elf_Internal_Shdr *symtab_hdr;
12837 asection *section;
12838 Elf_Internal_Sym *local_syms = NULL;
12839
12840 if (!is_ppc64_elf (input_bfd))
12841 continue;
12842
12843 /* We'll need the symbol table in a second. */
12844 symtab_hdr = &elf_symtab_hdr (input_bfd);
12845 if (symtab_hdr->sh_info == 0)
12846 continue;
12847
12848 /* Walk over each section attached to the input bfd. */
12849 for (section = input_bfd->sections;
12850 section != NULL;
12851 section = section->next)
12852 {
12853 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12854
12855 /* If there aren't any relocs, then there's nothing more
12856 to do. */
12857 if ((section->flags & SEC_RELOC) == 0
12858 || (section->flags & SEC_ALLOC) == 0
12859 || (section->flags & SEC_LOAD) == 0
12860 || (section->flags & SEC_CODE) == 0
12861 || section->reloc_count == 0)
12862 continue;
12863
12864 /* If this section is a link-once section that will be
12865 discarded, then don't create any stubs. */
12866 if (section->output_section == NULL
12867 || section->output_section->owner != info->output_bfd)
12868 continue;
12869
12870 /* Get the relocs. */
12871 internal_relocs
12872 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12873 info->keep_memory);
12874 if (internal_relocs == NULL)
12875 goto error_ret_free_local;
12876
12877 /* Now examine each relocation. */
12878 irela = internal_relocs;
12879 irelaend = irela + section->reloc_count;
12880 for (; irela < irelaend; irela++)
12881 {
12882 enum elf_ppc64_reloc_type r_type;
12883 unsigned int r_indx;
12884 enum ppc_stub_type stub_type;
12885 struct ppc_stub_hash_entry *stub_entry;
12886 asection *sym_sec, *code_sec;
12887 bfd_vma sym_value, code_value;
12888 bfd_vma destination;
12889 unsigned long local_off;
12890 bfd_boolean ok_dest;
12891 struct ppc_link_hash_entry *hash;
12892 struct ppc_link_hash_entry *fdh;
12893 struct elf_link_hash_entry *h;
12894 Elf_Internal_Sym *sym;
12895 char *stub_name;
12896 const asection *id_sec;
12897 struct _opd_sec_data *opd;
12898 struct plt_entry *plt_ent;
12899
12900 r_type = ELF64_R_TYPE (irela->r_info);
12901 r_indx = ELF64_R_SYM (irela->r_info);
12902
12903 if (r_type >= R_PPC64_max)
12904 {
12905 bfd_set_error (bfd_error_bad_value);
12906 goto error_ret_free_internal;
12907 }
12908
12909 /* Only look for stubs on branch instructions. */
12910 if (r_type != R_PPC64_REL24
12911 && r_type != R_PPC64_REL24_NOTOC
12912 && r_type != R_PPC64_REL14
12913 && r_type != R_PPC64_REL14_BRTAKEN
12914 && r_type != R_PPC64_REL14_BRNTAKEN)
12915 continue;
12916
12917 /* Now determine the call target, its name, value,
12918 section. */
12919 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12920 r_indx, input_bfd))
12921 goto error_ret_free_internal;
12922 hash = (struct ppc_link_hash_entry *) h;
12923
12924 ok_dest = FALSE;
12925 fdh = NULL;
12926 sym_value = 0;
12927 if (hash == NULL)
12928 {
12929 sym_value = sym->st_value;
12930 if (sym_sec != NULL
12931 && sym_sec->output_section != NULL)
12932 ok_dest = TRUE;
12933 }
12934 else if (hash->elf.root.type == bfd_link_hash_defined
12935 || hash->elf.root.type == bfd_link_hash_defweak)
12936 {
12937 sym_value = hash->elf.root.u.def.value;
12938 if (sym_sec->output_section != NULL)
12939 ok_dest = TRUE;
12940 }
12941 else if (hash->elf.root.type == bfd_link_hash_undefweak
12942 || hash->elf.root.type == bfd_link_hash_undefined)
12943 {
12944 /* Recognise an old ABI func code entry sym, and
12945 use the func descriptor sym instead if it is
12946 defined. */
12947 if (hash->elf.root.root.string[0] == '.'
12948 && hash->oh != NULL)
12949 {
12950 fdh = ppc_follow_link (hash->oh);
12951 if (fdh->elf.root.type == bfd_link_hash_defined
12952 || fdh->elf.root.type == bfd_link_hash_defweak)
12953 {
12954 sym_sec = fdh->elf.root.u.def.section;
12955 sym_value = fdh->elf.root.u.def.value;
12956 if (sym_sec->output_section != NULL)
12957 ok_dest = TRUE;
12958 }
12959 else
12960 fdh = NULL;
12961 }
12962 }
12963 else
12964 {
12965 bfd_set_error (bfd_error_bad_value);
12966 goto error_ret_free_internal;
12967 }
12968
12969 destination = 0;
12970 local_off = 0;
12971 if (ok_dest)
12972 {
12973 sym_value += irela->r_addend;
12974 destination = (sym_value
12975 + sym_sec->output_offset
12976 + sym_sec->output_section->vma);
12977 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12978 ? hash->elf.other
12979 : sym->st_other);
12980 }
12981
12982 code_sec = sym_sec;
12983 code_value = sym_value;
12984 opd = get_opd_info (sym_sec);
12985 if (opd != NULL)
12986 {
12987 bfd_vma dest;
12988
12989 if (hash == NULL && opd->adjust != NULL)
12990 {
12991 long adjust = opd->adjust[OPD_NDX (sym_value)];
12992 if (adjust == -1)
12993 continue;
12994 code_value += adjust;
12995 sym_value += adjust;
12996 }
12997 dest = opd_entry_value (sym_sec, sym_value,
12998 &code_sec, &code_value, FALSE);
12999 if (dest != (bfd_vma) -1)
13000 {
13001 destination = dest;
13002 if (fdh != NULL)
13003 {
13004 /* Fixup old ABI sym to point at code
13005 entry. */
13006 hash->elf.root.type = bfd_link_hash_defweak;
13007 hash->elf.root.u.def.section = code_sec;
13008 hash->elf.root.u.def.value = code_value;
13009 }
13010 }
13011 }
13012
13013 /* Determine what (if any) linker stub is needed. */
13014 plt_ent = NULL;
13015 stub_type = ppc_type_of_stub (section, irela, &hash,
13016 &plt_ent, destination,
13017 local_off);
13018
13019 if (r_type == R_PPC64_REL24_NOTOC)
13020 {
13021 if (stub_type == ppc_stub_plt_call)
13022 stub_type = ppc_stub_plt_call_notoc;
13023 else if (stub_type == ppc_stub_long_branch
13024 || (code_sec != NULL
13025 && code_sec->output_section != NULL
13026 && (((hash ? hash->elf.other : sym->st_other)
13027 & STO_PPC64_LOCAL_MASK)
13028 > 1 << STO_PPC64_LOCAL_BIT)))
13029 stub_type = ppc_stub_long_branch_notoc;
13030 }
13031 else if (stub_type != ppc_stub_plt_call)
13032 {
13033 /* Check whether we need a TOC adjusting stub.
13034 Since the linker pastes together pieces from
13035 different object files when creating the
13036 _init and _fini functions, it may be that a
13037 call to what looks like a local sym is in
13038 fact a call needing a TOC adjustment. */
13039 if ((code_sec != NULL
13040 && code_sec->output_section != NULL
13041 && (htab->sec_info[code_sec->id].toc_off
13042 != htab->sec_info[section->id].toc_off)
13043 && (code_sec->has_toc_reloc
13044 || code_sec->makes_toc_func_call))
13045 || (((hash ? hash->elf.other : sym->st_other)
13046 & STO_PPC64_LOCAL_MASK)
13047 == 1 << STO_PPC64_LOCAL_BIT))
13048 stub_type = ppc_stub_long_branch_r2off;
13049 }
13050
13051 if (stub_type == ppc_stub_none)
13052 continue;
13053
13054 /* __tls_get_addr calls might be eliminated. */
13055 if (stub_type != ppc_stub_plt_call
13056 && stub_type != ppc_stub_plt_call_notoc
13057 && hash != NULL
13058 && (hash == htab->tls_get_addr
13059 || hash == htab->tls_get_addr_fd)
13060 && section->has_tls_reloc
13061 && irela != internal_relocs)
13062 {
13063 /* Get tls info. */
13064 unsigned char *tls_mask;
13065
13066 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
13067 irela - 1, input_bfd))
13068 goto error_ret_free_internal;
13069 if ((*tls_mask & TLS_TLS) != 0)
13070 continue;
13071 }
13072
13073 if (stub_type == ppc_stub_plt_call)
13074 {
13075 if (!htab->opd_abi
13076 && htab->params->plt_localentry0 != 0
13077 && is_elfv2_localentry0 (&hash->elf))
13078 htab->has_plt_localentry0 = 1;
13079 else if (irela + 1 < irelaend
13080 && irela[1].r_offset == irela->r_offset + 4
13081 && (ELF64_R_TYPE (irela[1].r_info)
13082 == R_PPC64_TOCSAVE))
13083 {
13084 if (!tocsave_find (htab, INSERT,
13085 &local_syms, irela + 1, input_bfd))
13086 goto error_ret_free_internal;
13087 }
13088 else
13089 stub_type = ppc_stub_plt_call_r2save;
13090 }
13091
13092 /* Support for grouping stub sections. */
13093 id_sec = htab->sec_info[section->id].u.group->link_sec;
13094
13095 /* Get the name of this stub. */
13096 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13097 if (!stub_name)
13098 goto error_ret_free_internal;
13099
13100 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
13101 stub_name, FALSE, FALSE);
13102 if (stub_entry != NULL)
13103 {
13104 enum ppc_stub_type old_type;
13105 /* A stub has already been created, but it may
13106 not be the required type. We shouldn't be
13107 transitioning from plt_call to long_branch
13108 stubs or vice versa, but we might be
13109 upgrading from plt_call to plt_call_r2save or
13110 from long_branch to long_branch_r2off. */
13111 free (stub_name);
13112 old_type = stub_entry->stub_type;
13113 switch (old_type)
13114 {
13115 default:
13116 abort ();
13117
13118 case ppc_stub_save_res:
13119 continue;
13120
13121 case ppc_stub_plt_call:
13122 case ppc_stub_plt_call_r2save:
13123 case ppc_stub_plt_call_notoc:
13124 case ppc_stub_plt_call_both:
13125 if (stub_type == ppc_stub_plt_call)
13126 continue;
13127 else if (stub_type == ppc_stub_plt_call_r2save)
13128 {
13129 if (old_type == ppc_stub_plt_call_notoc)
13130 stub_type = ppc_stub_plt_call_both;
13131 }
13132 else if (stub_type == ppc_stub_plt_call_notoc)
13133 {
13134 if (old_type == ppc_stub_plt_call_r2save)
13135 stub_type = ppc_stub_plt_call_both;
13136 }
13137 else
13138 abort ();
13139 break;
13140
13141 case ppc_stub_plt_branch:
13142 case ppc_stub_plt_branch_r2off:
13143 case ppc_stub_plt_branch_notoc:
13144 case ppc_stub_plt_branch_both:
13145 old_type += (ppc_stub_long_branch
13146 - ppc_stub_plt_branch);
13147 /* Fall through. */
13148 case ppc_stub_long_branch:
13149 case ppc_stub_long_branch_r2off:
13150 case ppc_stub_long_branch_notoc:
13151 case ppc_stub_long_branch_both:
13152 if (stub_type == ppc_stub_long_branch)
13153 continue;
13154 else if (stub_type == ppc_stub_long_branch_r2off)
13155 {
13156 if (old_type == ppc_stub_long_branch_notoc)
13157 stub_type = ppc_stub_long_branch_both;
13158 }
13159 else if (stub_type == ppc_stub_long_branch_notoc)
13160 {
13161 if (old_type == ppc_stub_long_branch_r2off)
13162 stub_type = ppc_stub_long_branch_both;
13163 }
13164 else
13165 abort ();
13166 break;
13167 }
13168 if (old_type < stub_type)
13169 stub_entry->stub_type = stub_type;
13170 continue;
13171 }
13172
13173 stub_entry = ppc_add_stub (stub_name, section, info);
13174 if (stub_entry == NULL)
13175 {
13176 free (stub_name);
13177 error_ret_free_internal:
13178 if (elf_section_data (section)->relocs == NULL)
13179 free (internal_relocs);
13180 error_ret_free_local:
13181 if (local_syms != NULL
13182 && (symtab_hdr->contents
13183 != (unsigned char *) local_syms))
13184 free (local_syms);
13185 return FALSE;
13186 }
13187
13188 stub_entry->stub_type = stub_type;
13189 if (stub_type >= ppc_stub_plt_call
13190 && stub_type <= ppc_stub_plt_call_both)
13191 {
13192 stub_entry->target_value = sym_value;
13193 stub_entry->target_section = sym_sec;
13194 }
13195 else
13196 {
13197 stub_entry->target_value = code_value;
13198 stub_entry->target_section = code_sec;
13199 }
13200 stub_entry->h = hash;
13201 stub_entry->plt_ent = plt_ent;
13202 stub_entry->symtype
13203 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
13204 stub_entry->other = hash ? hash->elf.other : sym->st_other;
13205
13206 if (hash != NULL
13207 && (hash->elf.root.type == bfd_link_hash_defined
13208 || hash->elf.root.type == bfd_link_hash_defweak))
13209 htab->stub_globals += 1;
13210 }
13211
13212 /* We're done with the internal relocs, free them. */
13213 if (elf_section_data (section)->relocs != internal_relocs)
13214 free (internal_relocs);
13215 }
13216
13217 if (local_syms != NULL
13218 && symtab_hdr->contents != (unsigned char *) local_syms)
13219 {
13220 if (!info->keep_memory)
13221 free (local_syms);
13222 else
13223 symtab_hdr->contents = (unsigned char *) local_syms;
13224 }
13225 }
13226
13227 /* We may have added some stubs. Find out the new size of the
13228 stub sections. */
13229 for (group = htab->group; group != NULL; group = group->next)
13230 {
13231 group->lr_restore = 0;
13232 group->eh_size = 0;
13233 if (group->stub_sec != NULL)
13234 {
13235 asection *stub_sec = group->stub_sec;
13236
13237 if (htab->stub_iteration <= STUB_SHRINK_ITER
13238 || stub_sec->rawsize < stub_sec->size)
13239 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13240 stub_sec->rawsize = stub_sec->size;
13241 stub_sec->size = 0;
13242 stub_sec->reloc_count = 0;
13243 stub_sec->flags &= ~SEC_RELOC;
13244 }
13245 }
13246
13247 if (htab->stub_iteration <= STUB_SHRINK_ITER
13248 || htab->brlt->rawsize < htab->brlt->size)
13249 htab->brlt->rawsize = htab->brlt->size;
13250 htab->brlt->size = 0;
13251 htab->brlt->reloc_count = 0;
13252 htab->brlt->flags &= ~SEC_RELOC;
13253 if (htab->relbrlt != NULL)
13254 htab->relbrlt->size = 0;
13255
13256 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
13257
13258 for (group = htab->group; group != NULL; group = group->next)
13259 if (group->needs_save_res)
13260 group->stub_sec->size += htab->sfpr->size;
13261
13262 if (info->emitrelocations
13263 && htab->glink != NULL && htab->glink->size != 0)
13264 {
13265 htab->glink->reloc_count = 1;
13266 htab->glink->flags |= SEC_RELOC;
13267 }
13268
13269 if (htab->glink_eh_frame != NULL
13270 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13271 && htab->glink_eh_frame->output_section->size > 8)
13272 {
13273 size_t size = 0, align = 4;
13274
13275 for (group = htab->group; group != NULL; group = group->next)
13276 if (group->eh_size != 0)
13277 size += (group->eh_size + 17 + align - 1) & -align;
13278 if (htab->glink != NULL && htab->glink->size != 0)
13279 size += (24 + align - 1) & -align;
13280 if (size != 0)
13281 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13282 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13283 size = (size + align - 1) & -align;
13284 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13285 htab->glink_eh_frame->size = size;
13286 }
13287
13288 if (htab->params->plt_stub_align != 0)
13289 for (group = htab->group; group != NULL; group = group->next)
13290 if (group->stub_sec != NULL)
13291 {
13292 int align = abs (htab->params->plt_stub_align);
13293 group->stub_sec->size
13294 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13295 }
13296
13297 for (group = htab->group; group != NULL; group = group->next)
13298 if (group->stub_sec != NULL
13299 && group->stub_sec->rawsize != group->stub_sec->size
13300 && (htab->stub_iteration <= STUB_SHRINK_ITER
13301 || group->stub_sec->rawsize < group->stub_sec->size))
13302 break;
13303
13304 if (group == NULL
13305 && (htab->brlt->rawsize == htab->brlt->size
13306 || (htab->stub_iteration > STUB_SHRINK_ITER
13307 && htab->brlt->rawsize > htab->brlt->size))
13308 && (htab->glink_eh_frame == NULL
13309 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
13310 break;
13311
13312 /* Ask the linker to do its stuff. */
13313 (*htab->params->layout_sections_again) ();
13314 }
13315
13316 if (htab->glink_eh_frame != NULL
13317 && htab->glink_eh_frame->size != 0)
13318 {
13319 bfd_vma val;
13320 bfd_byte *p, *last_fde;
13321 size_t last_fde_len, size, align, pad;
13322 struct map_stub *group;
13323
13324 /* It is necessary to at least have a rough outline of the
13325 linker generated CIEs and FDEs written before
13326 bfd_elf_discard_info is run, in order for these FDEs to be
13327 indexed in .eh_frame_hdr. */
13328 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13329 if (p == NULL)
13330 return FALSE;
13331 htab->glink_eh_frame->contents = p;
13332 last_fde = p;
13333 align = 4;
13334
13335 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13336 /* CIE length (rewrite in case little-endian). */
13337 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13338 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13339 p += last_fde_len + 4;
13340
13341 for (group = htab->group; group != NULL; group = group->next)
13342 if (group->eh_size != 0)
13343 {
13344 group->eh_base = p - htab->glink_eh_frame->contents;
13345 last_fde = p;
13346 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
13347 /* FDE length. */
13348 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13349 p += 4;
13350 /* CIE pointer. */
13351 val = p - htab->glink_eh_frame->contents;
13352 bfd_put_32 (htab->elf.dynobj, val, p);
13353 p += 4;
13354 /* Offset to stub section, written later. */
13355 p += 4;
13356 /* stub section size. */
13357 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13358 p += 4;
13359 /* Augmentation. */
13360 p += 1;
13361 /* Make sure we don't have all nops. This is enough for
13362 elf-eh-frame.c to detect the last non-nop opcode. */
13363 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
13364 p = last_fde + last_fde_len + 4;
13365 }
13366 if (htab->glink != NULL && htab->glink->size != 0)
13367 {
13368 last_fde = p;
13369 last_fde_len = ((24 + align - 1) & -align) - 4;
13370 /* FDE length. */
13371 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13372 p += 4;
13373 /* CIE pointer. */
13374 val = p - htab->glink_eh_frame->contents;
13375 bfd_put_32 (htab->elf.dynobj, val, p);
13376 p += 4;
13377 /* Offset to .glink, written later. */
13378 p += 4;
13379 /* .glink size. */
13380 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13381 p += 4;
13382 /* Augmentation. */
13383 p += 1;
13384
13385 *p++ = DW_CFA_advance_loc + 1;
13386 *p++ = DW_CFA_register;
13387 *p++ = 65;
13388 *p++ = htab->opd_abi ? 12 : 0;
13389 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13390 *p++ = DW_CFA_restore_extended;
13391 *p++ = 65;
13392 p += ((24 + align - 1) & -align) - 24;
13393 }
13394 /* Subsume any padding into the last FDE if user .eh_frame
13395 sections are aligned more than glink_eh_frame. Otherwise any
13396 zero padding will be seen as a terminator. */
13397 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13398 size = p - htab->glink_eh_frame->contents;
13399 pad = ((size + align - 1) & -align) - size;
13400 htab->glink_eh_frame->size = size + pad;
13401 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13402 }
13403
13404 maybe_strip_output (info, htab->brlt);
13405 if (htab->glink_eh_frame != NULL)
13406 maybe_strip_output (info, htab->glink_eh_frame);
13407
13408 return TRUE;
13409 }
13410
13411 /* Called after we have determined section placement. If sections
13412 move, we'll be called again. Provide a value for TOCstart. */
13413
13414 bfd_vma
13415 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13416 {
13417 asection *s;
13418 bfd_vma TOCstart, adjust;
13419
13420 if (info != NULL)
13421 {
13422 struct elf_link_hash_entry *h;
13423 struct elf_link_hash_table *htab = elf_hash_table (info);
13424
13425 if (is_elf_hash_table (htab)
13426 && htab->hgot != NULL)
13427 h = htab->hgot;
13428 else
13429 {
13430 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13431 if (is_elf_hash_table (htab))
13432 htab->hgot = h;
13433 }
13434 if (h != NULL
13435 && h->root.type == bfd_link_hash_defined
13436 && !h->root.linker_def
13437 && (!is_elf_hash_table (htab)
13438 || h->def_regular))
13439 {
13440 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13441 + h->root.u.def.section->output_offset
13442 + h->root.u.def.section->output_section->vma);
13443 _bfd_set_gp_value (obfd, TOCstart);
13444 return TOCstart;
13445 }
13446 }
13447
13448 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13449 order. The TOC starts where the first of these sections starts. */
13450 s = bfd_get_section_by_name (obfd, ".got");
13451 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13452 s = bfd_get_section_by_name (obfd, ".toc");
13453 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13454 s = bfd_get_section_by_name (obfd, ".tocbss");
13455 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13456 s = bfd_get_section_by_name (obfd, ".plt");
13457 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13458 {
13459 /* This may happen for
13460 o references to TOC base (SYM@toc / TOC[tc0]) without a
13461 .toc directive
13462 o bad linker script
13463 o --gc-sections and empty TOC sections
13464
13465 FIXME: Warn user? */
13466
13467 /* Look for a likely section. We probably won't even be
13468 using TOCstart. */
13469 for (s = obfd->sections; s != NULL; s = s->next)
13470 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13471 | SEC_EXCLUDE))
13472 == (SEC_ALLOC | SEC_SMALL_DATA))
13473 break;
13474 if (s == NULL)
13475 for (s = obfd->sections; s != NULL; s = s->next)
13476 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13477 == (SEC_ALLOC | SEC_SMALL_DATA))
13478 break;
13479 if (s == NULL)
13480 for (s = obfd->sections; s != NULL; s = s->next)
13481 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13482 == SEC_ALLOC)
13483 break;
13484 if (s == NULL)
13485 for (s = obfd->sections; s != NULL; s = s->next)
13486 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13487 break;
13488 }
13489
13490 TOCstart = 0;
13491 if (s != NULL)
13492 TOCstart = s->output_section->vma + s->output_offset;
13493
13494 /* Force alignment. */
13495 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13496 TOCstart -= adjust;
13497 _bfd_set_gp_value (obfd, TOCstart);
13498
13499 if (info != NULL && s != NULL)
13500 {
13501 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13502
13503 if (htab != NULL)
13504 {
13505 if (htab->elf.hgot != NULL)
13506 {
13507 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13508 htab->elf.hgot->root.u.def.section = s;
13509 }
13510 }
13511 else
13512 {
13513 struct bfd_link_hash_entry *bh = NULL;
13514 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13515 s, TOC_BASE_OFF - adjust,
13516 NULL, FALSE, FALSE, &bh);
13517 }
13518 }
13519 return TOCstart;
13520 }
13521
13522 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13523 write out any global entry stubs, and PLT relocations. */
13524
13525 static bfd_boolean
13526 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13527 {
13528 struct bfd_link_info *info;
13529 struct ppc_link_hash_table *htab;
13530 struct plt_entry *ent;
13531 asection *s;
13532
13533 if (h->root.type == bfd_link_hash_indirect)
13534 return TRUE;
13535
13536 info = inf;
13537 htab = ppc_hash_table (info);
13538 if (htab == NULL)
13539 return FALSE;
13540
13541 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13542 if (ent->plt.offset != (bfd_vma) -1)
13543 {
13544 /* This symbol has an entry in the procedure linkage
13545 table. Set it up. */
13546 Elf_Internal_Rela rela;
13547 asection *plt, *relplt;
13548 bfd_byte *loc;
13549
13550 if (!htab->elf.dynamic_sections_created
13551 || h->dynindx == -1)
13552 {
13553 if (!(h->def_regular
13554 && (h->root.type == bfd_link_hash_defined
13555 || h->root.type == bfd_link_hash_defweak)))
13556 continue;
13557 if (h->type == STT_GNU_IFUNC)
13558 {
13559 plt = htab->elf.iplt;
13560 relplt = htab->elf.irelplt;
13561 htab->local_ifunc_resolver = 1;
13562 if (htab->opd_abi)
13563 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13564 else
13565 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13566 }
13567 else
13568 {
13569 plt = htab->pltlocal;
13570 if (bfd_link_pic (info))
13571 {
13572 relplt = htab->relpltlocal;
13573 if (htab->opd_abi)
13574 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13575 else
13576 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13577 }
13578 else
13579 relplt = NULL;
13580 }
13581 rela.r_addend = (h->root.u.def.value
13582 + h->root.u.def.section->output_offset
13583 + h->root.u.def.section->output_section->vma
13584 + ent->addend);
13585
13586 if (relplt == NULL)
13587 {
13588 loc = plt->contents + ent->plt.offset;
13589 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13590 if (htab->opd_abi)
13591 {
13592 bfd_vma toc = elf_gp (info->output_bfd);
13593 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13594 bfd_put_64 (info->output_bfd, toc, loc + 8);
13595 }
13596 }
13597 else
13598 {
13599 rela.r_offset = (plt->output_section->vma
13600 + plt->output_offset
13601 + ent->plt.offset);
13602 loc = relplt->contents + (relplt->reloc_count++
13603 * sizeof (Elf64_External_Rela));
13604 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13605 }
13606 }
13607 else
13608 {
13609 rela.r_offset = (htab->elf.splt->output_section->vma
13610 + htab->elf.splt->output_offset
13611 + ent->plt.offset);
13612 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13613 rela.r_addend = ent->addend;
13614 loc = (htab->elf.srelplt->contents
13615 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13616 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13617 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13618 htab->maybe_local_ifunc_resolver = 1;
13619 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13620 }
13621 }
13622
13623 if (!h->pointer_equality_needed)
13624 return TRUE;
13625
13626 if (h->def_regular)
13627 return TRUE;
13628
13629 s = htab->global_entry;
13630 if (s == NULL || s->size == 0)
13631 return TRUE;
13632
13633 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13634 if (ent->plt.offset != (bfd_vma) -1
13635 && ent->addend == 0)
13636 {
13637 bfd_byte *p;
13638 asection *plt;
13639 bfd_vma off;
13640
13641 p = s->contents + h->root.u.def.value;
13642 plt = htab->elf.splt;
13643 if (!htab->elf.dynamic_sections_created
13644 || h->dynindx == -1)
13645 {
13646 if (h->type == STT_GNU_IFUNC)
13647 plt = htab->elf.iplt;
13648 else
13649 plt = htab->pltlocal;
13650 }
13651 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13652 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13653
13654 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13655 {
13656 info->callbacks->einfo
13657 (_("%P: linkage table error against `%pT'\n"),
13658 h->root.root.string);
13659 bfd_set_error (bfd_error_bad_value);
13660 htab->stub_error = TRUE;
13661 }
13662
13663 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13664 if (htab->params->emit_stub_syms)
13665 {
13666 size_t len = strlen (h->root.root.string);
13667 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13668
13669 if (name == NULL)
13670 return FALSE;
13671
13672 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13673 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13674 if (h == NULL)
13675 return FALSE;
13676 if (h->root.type == bfd_link_hash_new)
13677 {
13678 h->root.type = bfd_link_hash_defined;
13679 h->root.u.def.section = s;
13680 h->root.u.def.value = p - s->contents;
13681 h->ref_regular = 1;
13682 h->def_regular = 1;
13683 h->ref_regular_nonweak = 1;
13684 h->forced_local = 1;
13685 h->non_elf = 0;
13686 h->root.linker_def = 1;
13687 }
13688 }
13689
13690 if (PPC_HA (off) != 0)
13691 {
13692 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13693 p += 4;
13694 }
13695 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13696 p += 4;
13697 bfd_put_32 (s->owner, MTCTR_R12, p);
13698 p += 4;
13699 bfd_put_32 (s->owner, BCTR, p);
13700 break;
13701 }
13702 return TRUE;
13703 }
13704
13705 /* Write PLT relocs for locals. */
13706
13707 static bfd_boolean
13708 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13709 {
13710 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13711 bfd *ibfd;
13712
13713 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13714 {
13715 struct got_entry **lgot_ents, **end_lgot_ents;
13716 struct plt_entry **local_plt, **lplt, **end_local_plt;
13717 Elf_Internal_Shdr *symtab_hdr;
13718 bfd_size_type locsymcount;
13719 Elf_Internal_Sym *local_syms = NULL;
13720 struct plt_entry *ent;
13721
13722 if (!is_ppc64_elf (ibfd))
13723 continue;
13724
13725 lgot_ents = elf_local_got_ents (ibfd);
13726 if (!lgot_ents)
13727 continue;
13728
13729 symtab_hdr = &elf_symtab_hdr (ibfd);
13730 locsymcount = symtab_hdr->sh_info;
13731 end_lgot_ents = lgot_ents + locsymcount;
13732 local_plt = (struct plt_entry **) end_lgot_ents;
13733 end_local_plt = local_plt + locsymcount;
13734 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13735 for (ent = *lplt; ent != NULL; ent = ent->next)
13736 if (ent->plt.offset != (bfd_vma) -1)
13737 {
13738 Elf_Internal_Sym *sym;
13739 asection *sym_sec;
13740 asection *plt, *relplt;
13741 bfd_byte *loc;
13742 bfd_vma val;
13743
13744 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13745 lplt - local_plt, ibfd))
13746 {
13747 if (local_syms != NULL
13748 && symtab_hdr->contents != (unsigned char *) local_syms)
13749 free (local_syms);
13750 return FALSE;
13751 }
13752
13753 val = sym->st_value + ent->addend;
13754 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13755 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13756 if (sym_sec != NULL && sym_sec->output_section != NULL)
13757 val += sym_sec->output_offset + sym_sec->output_section->vma;
13758
13759 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13760 {
13761 htab->local_ifunc_resolver = 1;
13762 plt = htab->elf.iplt;
13763 relplt = htab->elf.irelplt;
13764 }
13765 else
13766 {
13767 plt = htab->pltlocal;
13768 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13769 }
13770
13771 if (relplt == NULL)
13772 {
13773 loc = plt->contents + ent->plt.offset;
13774 bfd_put_64 (info->output_bfd, val, loc);
13775 if (htab->opd_abi)
13776 {
13777 bfd_vma toc = elf_gp (ibfd);
13778 bfd_put_64 (info->output_bfd, toc, loc + 8);
13779 }
13780 }
13781 else
13782 {
13783 Elf_Internal_Rela rela;
13784 rela.r_offset = (ent->plt.offset
13785 + plt->output_offset
13786 + plt->output_section->vma);
13787 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13788 {
13789 if (htab->opd_abi)
13790 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13791 else
13792 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13793 }
13794 else
13795 {
13796 if (htab->opd_abi)
13797 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13798 else
13799 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13800 }
13801 rela.r_addend = val;
13802 loc = relplt->contents + (relplt->reloc_count++
13803 * sizeof (Elf64_External_Rela));
13804 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13805 }
13806 }
13807
13808 if (local_syms != NULL
13809 && symtab_hdr->contents != (unsigned char *) local_syms)
13810 {
13811 if (!info->keep_memory)
13812 free (local_syms);
13813 else
13814 symtab_hdr->contents = (unsigned char *) local_syms;
13815 }
13816 }
13817 return TRUE;
13818 }
13819
13820 /* Build all the stubs associated with the current output file.
13821 The stubs are kept in a hash table attached to the main linker
13822 hash table. This function is called via gldelf64ppc_finish. */
13823
13824 bfd_boolean
13825 ppc64_elf_build_stubs (struct bfd_link_info *info,
13826 char **stats)
13827 {
13828 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13829 struct map_stub *group;
13830 asection *stub_sec;
13831 bfd_byte *p;
13832 int stub_sec_count = 0;
13833
13834 if (htab == NULL)
13835 return FALSE;
13836
13837 /* Allocate memory to hold the linker stubs. */
13838 for (group = htab->group; group != NULL; group = group->next)
13839 {
13840 group->eh_size = 0;
13841 group->lr_restore = 0;
13842 if ((stub_sec = group->stub_sec) != NULL
13843 && stub_sec->size != 0)
13844 {
13845 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13846 stub_sec->size);
13847 if (stub_sec->contents == NULL)
13848 return FALSE;
13849 stub_sec->size = 0;
13850 }
13851 }
13852
13853 if (htab->glink != NULL && htab->glink->size != 0)
13854 {
13855 unsigned int indx;
13856 bfd_vma plt0;
13857
13858 /* Build the .glink plt call stub. */
13859 if (htab->params->emit_stub_syms)
13860 {
13861 struct elf_link_hash_entry *h;
13862 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13863 TRUE, FALSE, FALSE);
13864 if (h == NULL)
13865 return FALSE;
13866 if (h->root.type == bfd_link_hash_new)
13867 {
13868 h->root.type = bfd_link_hash_defined;
13869 h->root.u.def.section = htab->glink;
13870 h->root.u.def.value = 8;
13871 h->ref_regular = 1;
13872 h->def_regular = 1;
13873 h->ref_regular_nonweak = 1;
13874 h->forced_local = 1;
13875 h->non_elf = 0;
13876 h->root.linker_def = 1;
13877 }
13878 }
13879 plt0 = (htab->elf.splt->output_section->vma
13880 + htab->elf.splt->output_offset
13881 - 16);
13882 if (info->emitrelocations)
13883 {
13884 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13885 if (r == NULL)
13886 return FALSE;
13887 r->r_offset = (htab->glink->output_offset
13888 + htab->glink->output_section->vma);
13889 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13890 r->r_addend = plt0;
13891 }
13892 p = htab->glink->contents;
13893 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13894 bfd_put_64 (htab->glink->owner, plt0, p);
13895 p += 8;
13896 if (htab->opd_abi)
13897 {
13898 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13899 p += 4;
13900 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13901 p += 4;
13902 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13903 p += 4;
13904 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13905 p += 4;
13906 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13907 p += 4;
13908 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13909 p += 4;
13910 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13911 p += 4;
13912 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13913 p += 4;
13914 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13915 p += 4;
13916 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13917 p += 4;
13918 }
13919 else
13920 {
13921 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13922 p += 4;
13923 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13924 p += 4;
13925 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13926 p += 4;
13927 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13928 p += 4;
13929 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13930 p += 4;
13931 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13932 p += 4;
13933 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13934 p += 4;
13935 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13936 p += 4;
13937 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13938 p += 4;
13939 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13940 p += 4;
13941 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13942 p += 4;
13943 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13944 p += 4;
13945 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13946 p += 4;
13947 }
13948 bfd_put_32 (htab->glink->owner, BCTR, p);
13949 p += 4;
13950 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13951
13952 /* Build the .glink lazy link call stubs. */
13953 indx = 0;
13954 while (p < htab->glink->contents + htab->glink->size)
13955 {
13956 if (htab->opd_abi)
13957 {
13958 if (indx < 0x8000)
13959 {
13960 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13961 p += 4;
13962 }
13963 else
13964 {
13965 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13966 p += 4;
13967 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13968 p);
13969 p += 4;
13970 }
13971 }
13972 bfd_put_32 (htab->glink->owner,
13973 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13974 indx++;
13975 p += 4;
13976 }
13977 }
13978
13979 /* Build .glink global entry stubs, and PLT relocs for globals. */
13980 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13981
13982 if (!write_plt_relocs_for_local_syms (info))
13983 return FALSE;
13984
13985 if (htab->brlt != NULL && htab->brlt->size != 0)
13986 {
13987 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13988 htab->brlt->size);
13989 if (htab->brlt->contents == NULL)
13990 return FALSE;
13991 }
13992 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13993 {
13994 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13995 htab->relbrlt->size);
13996 if (htab->relbrlt->contents == NULL)
13997 return FALSE;
13998 }
13999
14000 /* Build the stubs as directed by the stub hash table. */
14001 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
14002
14003 for (group = htab->group; group != NULL; group = group->next)
14004 if (group->needs_save_res)
14005 group->stub_sec->size += htab->sfpr->size;
14006
14007 if (htab->relbrlt != NULL)
14008 htab->relbrlt->reloc_count = 0;
14009
14010 if (htab->params->plt_stub_align != 0)
14011 for (group = htab->group; group != NULL; group = group->next)
14012 if ((stub_sec = group->stub_sec) != NULL)
14013 {
14014 int align = abs (htab->params->plt_stub_align);
14015 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14016 }
14017
14018 for (group = htab->group; group != NULL; group = group->next)
14019 if (group->needs_save_res)
14020 {
14021 stub_sec = group->stub_sec;
14022 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14023 htab->sfpr->contents, htab->sfpr->size);
14024 if (htab->params->emit_stub_syms)
14025 {
14026 unsigned int i;
14027
14028 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14029 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14030 return FALSE;
14031 }
14032 }
14033
14034 if (htab->glink_eh_frame != NULL
14035 && htab->glink_eh_frame->size != 0)
14036 {
14037 bfd_vma val;
14038 size_t align = 4;
14039
14040 p = htab->glink_eh_frame->contents;
14041 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14042
14043 for (group = htab->group; group != NULL; group = group->next)
14044 if (group->eh_size != 0)
14045 {
14046 /* Offset to stub section. */
14047 val = (group->stub_sec->output_section->vma
14048 + group->stub_sec->output_offset);
14049 val -= (htab->glink_eh_frame->output_section->vma
14050 + htab->glink_eh_frame->output_offset
14051 + (p + 8 - htab->glink_eh_frame->contents));
14052 if (val + 0x80000000 > 0xffffffff)
14053 {
14054 _bfd_error_handler
14055 (_("%s offset too large for .eh_frame sdata4 encoding"),
14056 group->stub_sec->name);
14057 return FALSE;
14058 }
14059 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14060 p += (group->eh_size + 17 + 3) & -4;
14061 }
14062 if (htab->glink != NULL && htab->glink->size != 0)
14063 {
14064 /* Offset to .glink. */
14065 val = (htab->glink->output_section->vma
14066 + htab->glink->output_offset
14067 + 8);
14068 val -= (htab->glink_eh_frame->output_section->vma
14069 + htab->glink_eh_frame->output_offset
14070 + (p + 8 - htab->glink_eh_frame->contents));
14071 if (val + 0x80000000 > 0xffffffff)
14072 {
14073 _bfd_error_handler
14074 (_("%s offset too large for .eh_frame sdata4 encoding"),
14075 htab->glink->name);
14076 return FALSE;
14077 }
14078 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14079 p += (24 + align - 1) & -align;
14080 }
14081 }
14082
14083 for (group = htab->group; group != NULL; group = group->next)
14084 if ((stub_sec = group->stub_sec) != NULL)
14085 {
14086 stub_sec_count += 1;
14087 if (stub_sec->rawsize != stub_sec->size
14088 && (htab->stub_iteration <= STUB_SHRINK_ITER
14089 || stub_sec->rawsize < stub_sec->size))
14090 break;
14091 }
14092
14093 if (group != NULL)
14094 {
14095 htab->stub_error = TRUE;
14096 _bfd_error_handler (_("stubs don't match calculated size"));
14097 }
14098
14099 if (htab->stub_error)
14100 return FALSE;
14101
14102 if (stats != NULL)
14103 {
14104 size_t len;
14105 *stats = bfd_malloc (500);
14106 if (*stats == NULL)
14107 return FALSE;
14108
14109 len = sprintf (*stats,
14110 ngettext ("linker stubs in %u group\n",
14111 "linker stubs in %u groups\n",
14112 stub_sec_count),
14113 stub_sec_count);
14114 sprintf (*stats + len, _(" branch %lu\n"
14115 " branch toc adj %lu\n"
14116 " branch notoc %lu\n"
14117 " branch both %lu\n"
14118 " long branch %lu\n"
14119 " long toc adj %lu\n"
14120 " long notoc %lu\n"
14121 " long both %lu\n"
14122 " plt call %lu\n"
14123 " plt call save %lu\n"
14124 " plt call notoc %lu\n"
14125 " plt call both %lu\n"
14126 " global entry %lu"),
14127 htab->stub_count[ppc_stub_long_branch - 1],
14128 htab->stub_count[ppc_stub_long_branch_r2off - 1],
14129 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14130 htab->stub_count[ppc_stub_long_branch_both - 1],
14131 htab->stub_count[ppc_stub_plt_branch - 1],
14132 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14133 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14134 htab->stub_count[ppc_stub_plt_branch_both - 1],
14135 htab->stub_count[ppc_stub_plt_call - 1],
14136 htab->stub_count[ppc_stub_plt_call_r2save - 1],
14137 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14138 htab->stub_count[ppc_stub_plt_call_both - 1],
14139 htab->stub_count[ppc_stub_global_entry - 1]);
14140 }
14141 return TRUE;
14142 }
14143
14144 /* What to do when ld finds relocations against symbols defined in
14145 discarded sections. */
14146
14147 static unsigned int
14148 ppc64_elf_action_discarded (asection *sec)
14149 {
14150 if (strcmp (".opd", sec->name) == 0)
14151 return 0;
14152
14153 if (strcmp (".toc", sec->name) == 0)
14154 return 0;
14155
14156 if (strcmp (".toc1", sec->name) == 0)
14157 return 0;
14158
14159 return _bfd_elf_default_action_discarded (sec);
14160 }
14161
14162 /* These are the dynamic relocations supported by glibc. */
14163
14164 static bfd_boolean
14165 ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
14166 {
14167 switch (r_type)
14168 {
14169 case R_PPC64_RELATIVE:
14170 case R_PPC64_NONE:
14171 case R_PPC64_ADDR64:
14172 case R_PPC64_GLOB_DAT:
14173 case R_PPC64_IRELATIVE:
14174 case R_PPC64_JMP_IREL:
14175 case R_PPC64_JMP_SLOT:
14176 case R_PPC64_DTPMOD64:
14177 case R_PPC64_DTPREL64:
14178 case R_PPC64_TPREL64:
14179 case R_PPC64_TPREL16_LO_DS:
14180 case R_PPC64_TPREL16_DS:
14181 case R_PPC64_TPREL16:
14182 case R_PPC64_TPREL16_LO:
14183 case R_PPC64_TPREL16_HI:
14184 case R_PPC64_TPREL16_HIGH:
14185 case R_PPC64_TPREL16_HA:
14186 case R_PPC64_TPREL16_HIGHA:
14187 case R_PPC64_TPREL16_HIGHER:
14188 case R_PPC64_TPREL16_HIGHEST:
14189 case R_PPC64_TPREL16_HIGHERA:
14190 case R_PPC64_TPREL16_HIGHESTA:
14191 case R_PPC64_ADDR16_LO_DS:
14192 case R_PPC64_ADDR16_LO:
14193 case R_PPC64_ADDR16_HI:
14194 case R_PPC64_ADDR16_HIGH:
14195 case R_PPC64_ADDR16_HA:
14196 case R_PPC64_ADDR16_HIGHA:
14197 case R_PPC64_REL30:
14198 case R_PPC64_COPY:
14199 case R_PPC64_UADDR64:
14200 case R_PPC64_UADDR32:
14201 case R_PPC64_ADDR32:
14202 case R_PPC64_ADDR24:
14203 case R_PPC64_ADDR16:
14204 case R_PPC64_UADDR16:
14205 case R_PPC64_ADDR16_DS:
14206 case R_PPC64_ADDR16_HIGHER:
14207 case R_PPC64_ADDR16_HIGHEST:
14208 case R_PPC64_ADDR16_HIGHERA:
14209 case R_PPC64_ADDR16_HIGHESTA:
14210 case R_PPC64_ADDR14:
14211 case R_PPC64_ADDR14_BRTAKEN:
14212 case R_PPC64_ADDR14_BRNTAKEN:
14213 case R_PPC64_REL32:
14214 case R_PPC64_REL64:
14215 return TRUE;
14216
14217 default:
14218 return FALSE;
14219 }
14220 }
14221
14222 /* The RELOCATE_SECTION function is called by the ELF backend linker
14223 to handle the relocations for a section.
14224
14225 The relocs are always passed as Rela structures; if the section
14226 actually uses Rel structures, the r_addend field will always be
14227 zero.
14228
14229 This function is responsible for adjust the section contents as
14230 necessary, and (if using Rela relocs and generating a
14231 relocatable output file) adjusting the reloc addend as
14232 necessary.
14233
14234 This function does not have to worry about setting the reloc
14235 address or the reloc symbol index.
14236
14237 LOCAL_SYMS is a pointer to the swapped in local symbols.
14238
14239 LOCAL_SECTIONS is an array giving the section in the input file
14240 corresponding to the st_shndx field of each local symbol.
14241
14242 The global hash table entry for the global symbols can be found
14243 via elf_sym_hashes (input_bfd).
14244
14245 When generating relocatable output, this function must handle
14246 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14247 going to be the section symbol corresponding to the output
14248 section, which means that the addend must be adjusted
14249 accordingly. */
14250
14251 static bfd_boolean
14252 ppc64_elf_relocate_section (bfd *output_bfd,
14253 struct bfd_link_info *info,
14254 bfd *input_bfd,
14255 asection *input_section,
14256 bfd_byte *contents,
14257 Elf_Internal_Rela *relocs,
14258 Elf_Internal_Sym *local_syms,
14259 asection **local_sections)
14260 {
14261 struct ppc_link_hash_table *htab;
14262 Elf_Internal_Shdr *symtab_hdr;
14263 struct elf_link_hash_entry **sym_hashes;
14264 Elf_Internal_Rela *rel;
14265 Elf_Internal_Rela *wrel;
14266 Elf_Internal_Rela *relend;
14267 Elf_Internal_Rela outrel;
14268 bfd_byte *loc;
14269 struct got_entry **local_got_ents;
14270 bfd_vma TOCstart;
14271 bfd_boolean ret = TRUE;
14272 bfd_boolean is_opd;
14273 /* Assume 'at' branch hints. */
14274 bfd_boolean is_isa_v2 = TRUE;
14275 bfd_boolean warned_dynamic = FALSE;
14276 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
14277
14278 /* Initialize howto table if needed. */
14279 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
14280 ppc_howto_init ();
14281
14282 htab = ppc_hash_table (info);
14283 if (htab == NULL)
14284 return FALSE;
14285
14286 /* Don't relocate stub sections. */
14287 if (input_section->owner == htab->params->stub_bfd)
14288 return TRUE;
14289
14290 if (!is_ppc64_elf (input_bfd))
14291 {
14292 bfd_set_error (bfd_error_wrong_format);
14293 return FALSE;
14294 }
14295
14296 local_got_ents = elf_local_got_ents (input_bfd);
14297 TOCstart = elf_gp (output_bfd);
14298 symtab_hdr = &elf_symtab_hdr (input_bfd);
14299 sym_hashes = elf_sym_hashes (input_bfd);
14300 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
14301
14302 rel = wrel = relocs;
14303 relend = relocs + input_section->reloc_count;
14304 for (; rel < relend; wrel++, rel++)
14305 {
14306 enum elf_ppc64_reloc_type r_type;
14307 bfd_vma addend;
14308 bfd_reloc_status_type r;
14309 Elf_Internal_Sym *sym;
14310 asection *sec;
14311 struct elf_link_hash_entry *h_elf;
14312 struct ppc_link_hash_entry *h;
14313 struct ppc_link_hash_entry *fdh;
14314 const char *sym_name;
14315 unsigned long r_symndx, toc_symndx;
14316 bfd_vma toc_addend;
14317 unsigned char tls_mask, tls_gd, tls_type;
14318 unsigned char sym_type;
14319 bfd_vma relocation;
14320 bfd_boolean unresolved_reloc, save_unresolved_reloc;
14321 bfd_boolean warned;
14322 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
14323 unsigned int insn;
14324 unsigned int mask;
14325 struct ppc_stub_hash_entry *stub_entry;
14326 bfd_vma max_br_offset;
14327 bfd_vma from;
14328 Elf_Internal_Rela orig_rel;
14329 reloc_howto_type *howto;
14330 struct reloc_howto_struct alt_howto;
14331 uint64_t pinsn;
14332 bfd_vma offset;
14333
14334 again:
14335 orig_rel = *rel;
14336
14337 r_type = ELF64_R_TYPE (rel->r_info);
14338 r_symndx = ELF64_R_SYM (rel->r_info);
14339
14340 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14341 symbol of the previous ADDR64 reloc. The symbol gives us the
14342 proper TOC base to use. */
14343 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
14344 && wrel != relocs
14345 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
14346 && is_opd)
14347 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
14348
14349 sym = NULL;
14350 sec = NULL;
14351 h_elf = NULL;
14352 sym_name = NULL;
14353 unresolved_reloc = FALSE;
14354 warned = FALSE;
14355
14356 if (r_symndx < symtab_hdr->sh_info)
14357 {
14358 /* It's a local symbol. */
14359 struct _opd_sec_data *opd;
14360
14361 sym = local_syms + r_symndx;
14362 sec = local_sections[r_symndx];
14363 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
14364 sym_type = ELF64_ST_TYPE (sym->st_info);
14365 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
14366 opd = get_opd_info (sec);
14367 if (opd != NULL && opd->adjust != NULL)
14368 {
14369 long adjust = opd->adjust[OPD_NDX (sym->st_value
14370 + rel->r_addend)];
14371 if (adjust == -1)
14372 relocation = 0;
14373 else
14374 {
14375 /* If this is a relocation against the opd section sym
14376 and we have edited .opd, adjust the reloc addend so
14377 that ld -r and ld --emit-relocs output is correct.
14378 If it is a reloc against some other .opd symbol,
14379 then the symbol value will be adjusted later. */
14380 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14381 rel->r_addend += adjust;
14382 else
14383 relocation += adjust;
14384 }
14385 }
14386 }
14387 else
14388 {
14389 bfd_boolean ignored;
14390
14391 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14392 r_symndx, symtab_hdr, sym_hashes,
14393 h_elf, sec, relocation,
14394 unresolved_reloc, warned, ignored);
14395 sym_name = h_elf->root.root.string;
14396 sym_type = h_elf->type;
14397 if (sec != NULL
14398 && sec->owner == output_bfd
14399 && strcmp (sec->name, ".opd") == 0)
14400 {
14401 /* This is a symbol defined in a linker script. All
14402 such are defined in output sections, even those
14403 defined by simple assignment from a symbol defined in
14404 an input section. Transfer the symbol to an
14405 appropriate input .opd section, so that a branch to
14406 this symbol will be mapped to the location specified
14407 by the opd entry. */
14408 struct bfd_link_order *lo;
14409 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14410 if (lo->type == bfd_indirect_link_order)
14411 {
14412 asection *isec = lo->u.indirect.section;
14413 if (h_elf->root.u.def.value >= isec->output_offset
14414 && h_elf->root.u.def.value < (isec->output_offset
14415 + isec->size))
14416 {
14417 h_elf->root.u.def.value -= isec->output_offset;
14418 h_elf->root.u.def.section = isec;
14419 sec = isec;
14420 break;
14421 }
14422 }
14423 }
14424 }
14425 h = (struct ppc_link_hash_entry *) h_elf;
14426
14427 if (sec != NULL && discarded_section (sec))
14428 {
14429 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14430 input_bfd, input_section,
14431 contents, rel->r_offset);
14432 wrel->r_offset = rel->r_offset;
14433 wrel->r_info = 0;
14434 wrel->r_addend = 0;
14435
14436 /* For ld -r, remove relocations in debug sections against
14437 symbols defined in discarded sections. Not done for
14438 non-debug to preserve relocs in .eh_frame which the
14439 eh_frame editing code expects to be present. */
14440 if (bfd_link_relocatable (info)
14441 && (input_section->flags & SEC_DEBUGGING))
14442 wrel--;
14443
14444 continue;
14445 }
14446
14447 if (bfd_link_relocatable (info))
14448 goto copy_reloc;
14449
14450 if (h != NULL && &h->elf == htab->elf.hgot)
14451 {
14452 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14453 sec = bfd_abs_section_ptr;
14454 unresolved_reloc = FALSE;
14455 }
14456
14457 /* TLS optimizations. Replace instruction sequences and relocs
14458 based on information we collected in tls_optimize. We edit
14459 RELOCS so that --emit-relocs will output something sensible
14460 for the final instruction stream. */
14461 tls_mask = 0;
14462 tls_gd = 0;
14463 toc_symndx = 0;
14464 if (h != NULL)
14465 tls_mask = h->tls_mask;
14466 else if (local_got_ents != NULL)
14467 {
14468 struct plt_entry **local_plt = (struct plt_entry **)
14469 (local_got_ents + symtab_hdr->sh_info);
14470 unsigned char *lgot_masks = (unsigned char *)
14471 (local_plt + symtab_hdr->sh_info);
14472 tls_mask = lgot_masks[r_symndx];
14473 }
14474 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14475 && (r_type == R_PPC64_TLS
14476 || r_type == R_PPC64_TLSGD
14477 || r_type == R_PPC64_TLSLD))
14478 {
14479 /* Check for toc tls entries. */
14480 unsigned char *toc_tls;
14481
14482 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14483 &local_syms, rel, input_bfd))
14484 return FALSE;
14485
14486 if (toc_tls)
14487 tls_mask = *toc_tls;
14488 }
14489
14490 /* Check that tls relocs are used with tls syms, and non-tls
14491 relocs are used with non-tls syms. */
14492 if (r_symndx != STN_UNDEF
14493 && r_type != R_PPC64_NONE
14494 && (h == NULL
14495 || h->elf.root.type == bfd_link_hash_defined
14496 || h->elf.root.type == bfd_link_hash_defweak)
14497 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
14498 {
14499 if ((tls_mask & TLS_TLS) != 0
14500 && (r_type == R_PPC64_TLS
14501 || r_type == R_PPC64_TLSGD
14502 || r_type == R_PPC64_TLSLD))
14503 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14504 ;
14505 else
14506 info->callbacks->einfo
14507 (!IS_PPC64_TLS_RELOC (r_type)
14508 /* xgettext:c-format */
14509 ? _("%H: %s used with TLS symbol `%pT'\n")
14510 /* xgettext:c-format */
14511 : _("%H: %s used with non-TLS symbol `%pT'\n"),
14512 input_bfd, input_section, rel->r_offset,
14513 ppc64_elf_howto_table[r_type]->name,
14514 sym_name);
14515 }
14516
14517 /* Ensure reloc mapping code below stays sane. */
14518 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14519 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14520 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14521 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14522 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14523 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14524 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14525 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14526 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14527 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14528 abort ();
14529
14530 switch (r_type)
14531 {
14532 default:
14533 break;
14534
14535 case R_PPC64_LO_DS_OPT:
14536 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
14537 if ((insn & (0x3f << 26)) != 58u << 26)
14538 abort ();
14539 insn += (14u << 26) - (58u << 26);
14540 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
14541 r_type = R_PPC64_TOC16_LO;
14542 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14543 break;
14544
14545 case R_PPC64_TOC16:
14546 case R_PPC64_TOC16_LO:
14547 case R_PPC64_TOC16_DS:
14548 case R_PPC64_TOC16_LO_DS:
14549 {
14550 /* Check for toc tls entries. */
14551 unsigned char *toc_tls;
14552 int retval;
14553
14554 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14555 &local_syms, rel, input_bfd);
14556 if (retval == 0)
14557 return FALSE;
14558
14559 if (toc_tls)
14560 {
14561 tls_mask = *toc_tls;
14562 if (r_type == R_PPC64_TOC16_DS
14563 || r_type == R_PPC64_TOC16_LO_DS)
14564 {
14565 if ((tls_mask & TLS_TLS) != 0
14566 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14567 goto toctprel;
14568 }
14569 else
14570 {
14571 /* If we found a GD reloc pair, then we might be
14572 doing a GD->IE transition. */
14573 if (retval == 2)
14574 {
14575 tls_gd = TLS_GDIE;
14576 if ((tls_mask & TLS_TLS) != 0
14577 && (tls_mask & TLS_GD) == 0)
14578 goto tls_ldgd_opt;
14579 }
14580 else if (retval == 3)
14581 {
14582 if ((tls_mask & TLS_TLS) != 0
14583 && (tls_mask & TLS_LD) == 0)
14584 goto tls_ldgd_opt;
14585 }
14586 }
14587 }
14588 }
14589 break;
14590
14591 case R_PPC64_GOT_TPREL16_HI:
14592 case R_PPC64_GOT_TPREL16_HA:
14593 if ((tls_mask & TLS_TLS) != 0
14594 && (tls_mask & TLS_TPREL) == 0)
14595 {
14596 rel->r_offset -= d_offset;
14597 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14598 r_type = R_PPC64_NONE;
14599 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14600 }
14601 break;
14602
14603 case R_PPC64_GOT_TPREL16_DS:
14604 case R_PPC64_GOT_TPREL16_LO_DS:
14605 if ((tls_mask & TLS_TLS) != 0
14606 && (tls_mask & TLS_TPREL) == 0)
14607 {
14608 toctprel:
14609 insn = bfd_get_32 (input_bfd,
14610 contents + rel->r_offset - d_offset);
14611 insn &= 31 << 21;
14612 insn |= 0x3c0d0000; /* addis 0,13,0 */
14613 bfd_put_32 (input_bfd, insn,
14614 contents + rel->r_offset - d_offset);
14615 r_type = R_PPC64_TPREL16_HA;
14616 if (toc_symndx != 0)
14617 {
14618 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14619 rel->r_addend = toc_addend;
14620 /* We changed the symbol. Start over in order to
14621 get h, sym, sec etc. right. */
14622 goto again;
14623 }
14624 else
14625 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14626 }
14627 break;
14628
14629 case R_PPC64_GOT_TPREL34:
14630 if ((tls_mask & TLS_TLS) != 0
14631 && (tls_mask & TLS_TPREL) == 0)
14632 {
14633 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
14634 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14635 pinsn <<= 32;
14636 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14637 pinsn += ((2ULL << 56) + (-1ULL << 52)
14638 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
14639 bfd_put_32 (input_bfd, pinsn >> 32,
14640 contents + rel->r_offset);
14641 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14642 contents + rel->r_offset + 4);
14643 r_type = R_PPC64_TPREL34;
14644 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14645 }
14646 break;
14647
14648 case R_PPC64_TLS:
14649 if ((tls_mask & TLS_TLS) != 0
14650 && (tls_mask & TLS_TPREL) == 0)
14651 {
14652 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14653 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14654 if (insn == 0)
14655 break;
14656 if ((rel->r_offset & 3) == 0)
14657 {
14658 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14659 /* Was PPC64_TLS which sits on insn boundary, now
14660 PPC64_TPREL16_LO which is at low-order half-word. */
14661 rel->r_offset += d_offset;
14662 r_type = R_PPC64_TPREL16_LO;
14663 if (toc_symndx != 0)
14664 {
14665 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14666 rel->r_addend = toc_addend;
14667 /* We changed the symbol. Start over in order to
14668 get h, sym, sec etc. right. */
14669 goto again;
14670 }
14671 else
14672 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14673 }
14674 else if ((rel->r_offset & 3) == 1)
14675 {
14676 /* For pcrel IE to LE we already have the full
14677 offset and thus don't need an addi here. A nop
14678 or mr will do. */
14679 if ((insn & (0x3f << 26)) == 14 << 26)
14680 {
14681 /* Extract regs from addi rt,ra,si. */
14682 unsigned int rt = (insn >> 21) & 0x1f;
14683 unsigned int ra = (insn >> 16) & 0x1f;
14684 if (rt == ra)
14685 insn = NOP;
14686 else
14687 {
14688 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
14689 insn = (rt << 16) | (ra << 21) | (ra << 11);
14690 insn |= (31u << 26) | (444u << 1);
14691 }
14692 }
14693 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
14694 }
14695 }
14696 break;
14697
14698 case R_PPC64_GOT_TLSGD16_HI:
14699 case R_PPC64_GOT_TLSGD16_HA:
14700 tls_gd = TLS_GDIE;
14701 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14702 goto tls_gdld_hi;
14703 break;
14704
14705 case R_PPC64_GOT_TLSLD16_HI:
14706 case R_PPC64_GOT_TLSLD16_HA:
14707 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14708 {
14709 tls_gdld_hi:
14710 if ((tls_mask & tls_gd) != 0)
14711 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14712 + R_PPC64_GOT_TPREL16_DS);
14713 else
14714 {
14715 rel->r_offset -= d_offset;
14716 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14717 r_type = R_PPC64_NONE;
14718 }
14719 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14720 }
14721 break;
14722
14723 case R_PPC64_GOT_TLSGD16:
14724 case R_PPC64_GOT_TLSGD16_LO:
14725 tls_gd = TLS_GDIE;
14726 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14727 goto tls_ldgd_opt;
14728 break;
14729
14730 case R_PPC64_GOT_TLSLD16:
14731 case R_PPC64_GOT_TLSLD16_LO:
14732 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14733 {
14734 unsigned int insn1, insn2;
14735
14736 tls_ldgd_opt:
14737 offset = (bfd_vma) -1;
14738 /* If not using the newer R_PPC64_TLSGD/LD to mark
14739 __tls_get_addr calls, we must trust that the call
14740 stays with its arg setup insns, ie. that the next
14741 reloc is the __tls_get_addr call associated with
14742 the current reloc. Edit both insns. */
14743 if (input_section->nomark_tls_get_addr
14744 && rel + 1 < relend
14745 && branch_reloc_hash_match (input_bfd, rel + 1,
14746 htab->tls_get_addr,
14747 htab->tls_get_addr_fd))
14748 offset = rel[1].r_offset;
14749 /* We read the low GOT_TLS (or TOC16) insn because we
14750 need to keep the destination reg. It may be
14751 something other than the usual r3, and moved to r3
14752 before the call by intervening code. */
14753 insn1 = bfd_get_32 (input_bfd,
14754 contents + rel->r_offset - d_offset);
14755 if ((tls_mask & tls_gd) != 0)
14756 {
14757 /* IE */
14758 insn1 &= (0x1f << 21) | (0x1f << 16);
14759 insn1 |= 58 << 26; /* ld */
14760 insn2 = 0x7c636a14; /* add 3,3,13 */
14761 if (offset != (bfd_vma) -1)
14762 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14763 if (r_type == R_PPC64_TOC16
14764 || r_type == R_PPC64_TOC16_LO)
14765 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14766 else
14767 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
14768 + R_PPC64_GOT_TPREL16_DS);
14769 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14770 }
14771 else
14772 {
14773 /* LE */
14774 insn1 &= 0x1f << 21;
14775 insn1 |= 0x3c0d0000; /* addis r,13,0 */
14776 insn2 = 0x38630000; /* addi 3,3,0 */
14777 if (tls_gd == 0)
14778 {
14779 /* Was an LD reloc. */
14780 r_symndx = STN_UNDEF;
14781 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14782 }
14783 else if (toc_symndx != 0)
14784 {
14785 r_symndx = toc_symndx;
14786 rel->r_addend = toc_addend;
14787 }
14788 r_type = R_PPC64_TPREL16_HA;
14789 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14790 if (offset != (bfd_vma) -1)
14791 {
14792 rel[1].r_info = ELF64_R_INFO (r_symndx,
14793 R_PPC64_TPREL16_LO);
14794 rel[1].r_offset = offset + d_offset;
14795 rel[1].r_addend = rel->r_addend;
14796 }
14797 }
14798 bfd_put_32 (input_bfd, insn1,
14799 contents + rel->r_offset - d_offset);
14800 if (offset != (bfd_vma) -1)
14801 {
14802 bfd_put_32 (input_bfd, insn2, contents + offset);
14803 if (offset + 8 <= input_section->size)
14804 {
14805 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14806 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14807 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14808 }
14809 }
14810 if ((tls_mask & tls_gd) == 0
14811 && (tls_gd == 0 || toc_symndx != 0))
14812 {
14813 /* We changed the symbol. Start over in order
14814 to get h, sym, sec etc. right. */
14815 goto again;
14816 }
14817 }
14818 break;
14819
14820 case R_PPC64_GOT_TLSGD34:
14821 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14822 {
14823 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14824 pinsn <<= 32;
14825 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14826 if ((tls_mask & TLS_GDIE) != 0)
14827 {
14828 /* IE, pla -> pld */
14829 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
14830 r_type = R_PPC64_GOT_TPREL34;
14831 }
14832 else
14833 {
14834 /* LE, pla pcrel -> paddi r13 */
14835 pinsn += (-1ULL << 52) + (13ULL << 16);
14836 r_type = R_PPC64_TPREL34;
14837 }
14838 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14839 bfd_put_32 (input_bfd, pinsn >> 32,
14840 contents + rel->r_offset);
14841 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14842 contents + rel->r_offset + 4);
14843 }
14844 break;
14845
14846 case R_PPC64_GOT_TLSLD34:
14847 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14848 {
14849 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14850 pinsn <<= 32;
14851 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14852 pinsn += (-1ULL << 52) + (13ULL << 16);
14853 bfd_put_32 (input_bfd, pinsn >> 32,
14854 contents + rel->r_offset);
14855 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14856 contents + rel->r_offset + 4);
14857 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14858 r_symndx = STN_UNDEF;
14859 r_type = R_PPC64_TPREL34;
14860 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14861 goto again;
14862 }
14863 break;
14864
14865 case R_PPC64_TLSGD:
14866 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14867 && rel + 1 < relend)
14868 {
14869 unsigned int insn2;
14870 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14871
14872 offset = rel->r_offset;
14873 if (is_plt_seq_reloc (r_type1))
14874 {
14875 bfd_put_32 (output_bfd, NOP, contents + offset);
14876 if (r_type1 == R_PPC64_PLT_PCREL34
14877 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14878 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14879 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14880 break;
14881 }
14882
14883 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14884 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14885
14886 if ((tls_mask & TLS_GDIE) != 0)
14887 {
14888 /* IE */
14889 r_type = R_PPC64_NONE;
14890 insn2 = 0x7c636a14; /* add 3,3,13 */
14891 }
14892 else
14893 {
14894 /* LE */
14895 if (toc_symndx != 0)
14896 {
14897 r_symndx = toc_symndx;
14898 rel->r_addend = toc_addend;
14899 }
14900 if (r_type1 == R_PPC64_REL24_NOTOC
14901 || r_type1 == R_PPC64_PLTCALL_NOTOC)
14902 {
14903 r_type = R_PPC64_NONE;
14904 insn2 = NOP;
14905 }
14906 else
14907 {
14908 rel->r_offset = offset + d_offset;
14909 r_type = R_PPC64_TPREL16_LO;
14910 insn2 = 0x38630000; /* addi 3,3,0 */
14911 }
14912 }
14913 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14914 /* Zap the reloc on the _tls_get_addr call too. */
14915 BFD_ASSERT (offset == rel[1].r_offset);
14916 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14917 bfd_put_32 (input_bfd, insn2, contents + offset);
14918 if ((tls_mask & TLS_GDIE) == 0
14919 && toc_symndx != 0
14920 && r_type != R_PPC64_NONE)
14921 goto again;
14922 }
14923 break;
14924
14925 case R_PPC64_TLSLD:
14926 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14927 && rel + 1 < relend)
14928 {
14929 unsigned int insn2;
14930 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14931
14932 offset = rel->r_offset;
14933 if (is_plt_seq_reloc (r_type1))
14934 {
14935 bfd_put_32 (output_bfd, NOP, contents + offset);
14936 if (r_type1 == R_PPC64_PLT_PCREL34
14937 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14938 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14939 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14940 break;
14941 }
14942
14943 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14944 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14945
14946 if (r_type1 == R_PPC64_REL24_NOTOC
14947 || r_type1 == R_PPC64_PLTCALL_NOTOC)
14948 {
14949 r_type = R_PPC64_NONE;
14950 insn2 = NOP;
14951 }
14952 else
14953 {
14954 rel->r_offset = offset + d_offset;
14955 r_symndx = STN_UNDEF;
14956 r_type = R_PPC64_TPREL16_LO;
14957 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14958 insn2 = 0x38630000; /* addi 3,3,0 */
14959 }
14960 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14961 /* Zap the reloc on the _tls_get_addr call too. */
14962 BFD_ASSERT (offset == rel[1].r_offset);
14963 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14964 bfd_put_32 (input_bfd, insn2, contents + offset);
14965 if (r_type != R_PPC64_NONE)
14966 goto again;
14967 }
14968 break;
14969
14970 case R_PPC64_DTPMOD64:
14971 if (rel + 1 < relend
14972 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14973 && rel[1].r_offset == rel->r_offset + 8)
14974 {
14975 if ((tls_mask & TLS_GD) == 0)
14976 {
14977 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14978 if ((tls_mask & TLS_GDIE) != 0)
14979 r_type = R_PPC64_TPREL64;
14980 else
14981 {
14982 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14983 r_type = R_PPC64_NONE;
14984 }
14985 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14986 }
14987 }
14988 else
14989 {
14990 if ((tls_mask & TLS_LD) == 0)
14991 {
14992 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14993 r_type = R_PPC64_NONE;
14994 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14995 }
14996 }
14997 break;
14998
14999 case R_PPC64_TPREL64:
15000 if ((tls_mask & TLS_TPREL) == 0)
15001 {
15002 r_type = R_PPC64_NONE;
15003 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15004 }
15005 break;
15006
15007 case R_PPC64_ENTRY:
15008 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15009 if (!bfd_link_pic (info)
15010 && !info->traditional_format
15011 && relocation + 0x80008000 <= 0xffffffff)
15012 {
15013 unsigned int insn1, insn2;
15014
15015 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15016 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15017 if ((insn1 & ~0xfffc) == LD_R2_0R12
15018 && insn2 == ADD_R2_R2_R12)
15019 {
15020 bfd_put_32 (input_bfd,
15021 LIS_R2 + PPC_HA (relocation),
15022 contents + rel->r_offset);
15023 bfd_put_32 (input_bfd,
15024 ADDI_R2_R2 + PPC_LO (relocation),
15025 contents + rel->r_offset + 4);
15026 }
15027 }
15028 else
15029 {
15030 relocation -= (rel->r_offset
15031 + input_section->output_offset
15032 + input_section->output_section->vma);
15033 if (relocation + 0x80008000 <= 0xffffffff)
15034 {
15035 unsigned int insn1, insn2;
15036
15037 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15038 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15039 if ((insn1 & ~0xfffc) == LD_R2_0R12
15040 && insn2 == ADD_R2_R2_R12)
15041 {
15042 bfd_put_32 (input_bfd,
15043 ADDIS_R2_R12 + PPC_HA (relocation),
15044 contents + rel->r_offset);
15045 bfd_put_32 (input_bfd,
15046 ADDI_R2_R2 + PPC_LO (relocation),
15047 contents + rel->r_offset + 4);
15048 }
15049 }
15050 }
15051 break;
15052
15053 case R_PPC64_REL16_HA:
15054 /* If we are generating a non-PIC executable, edit
15055 . 0: addis 2,12,.TOC.-0b@ha
15056 . addi 2,2,.TOC.-0b@l
15057 used by ELFv2 global entry points to set up r2, to
15058 . lis 2,.TOC.@ha
15059 . addi 2,2,.TOC.@l
15060 if .TOC. is in range. */
15061 if (!bfd_link_pic (info)
15062 && !info->traditional_format
15063 && !htab->opd_abi
15064 && rel->r_addend == d_offset
15065 && h != NULL && &h->elf == htab->elf.hgot
15066 && rel + 1 < relend
15067 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15068 && rel[1].r_offset == rel->r_offset + 4
15069 && rel[1].r_addend == rel->r_addend + 4
15070 && relocation + 0x80008000 <= 0xffffffff)
15071 {
15072 unsigned int insn1, insn2;
15073 offset = rel->r_offset - d_offset;
15074 insn1 = bfd_get_32 (input_bfd, contents + offset);
15075 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15076 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15077 && (insn2 & 0xffff0000) == ADDI_R2_R2)
15078 {
15079 r_type = R_PPC64_ADDR16_HA;
15080 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15081 rel->r_addend -= d_offset;
15082 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15083 rel[1].r_addend -= d_offset + 4;
15084 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
15085 }
15086 }
15087 break;
15088 }
15089
15090 /* Handle other relocations that tweak non-addend part of insn. */
15091 insn = 0;
15092 max_br_offset = 1 << 25;
15093 addend = rel->r_addend;
15094 reloc_dest = DEST_NORMAL;
15095 switch (r_type)
15096 {
15097 default:
15098 break;
15099
15100 case R_PPC64_TOCSAVE:
15101 if (relocation + addend == (rel->r_offset
15102 + input_section->output_offset
15103 + input_section->output_section->vma)
15104 && tocsave_find (htab, NO_INSERT,
15105 &local_syms, rel, input_bfd))
15106 {
15107 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15108 if (insn == NOP
15109 || insn == CROR_151515 || insn == CROR_313131)
15110 bfd_put_32 (input_bfd,
15111 STD_R2_0R1 + STK_TOC (htab),
15112 contents + rel->r_offset);
15113 }
15114 break;
15115
15116 /* Branch taken prediction relocations. */
15117 case R_PPC64_ADDR14_BRTAKEN:
15118 case R_PPC64_REL14_BRTAKEN:
15119 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
15120 /* Fall through. */
15121
15122 /* Branch not taken prediction relocations. */
15123 case R_PPC64_ADDR14_BRNTAKEN:
15124 case R_PPC64_REL14_BRNTAKEN:
15125 insn |= bfd_get_32 (input_bfd,
15126 contents + rel->r_offset) & ~(0x01 << 21);
15127 /* Fall through. */
15128
15129 case R_PPC64_REL14:
15130 max_br_offset = 1 << 15;
15131 /* Fall through. */
15132
15133 case R_PPC64_REL24:
15134 case R_PPC64_REL24_NOTOC:
15135 case R_PPC64_PLTCALL:
15136 case R_PPC64_PLTCALL_NOTOC:
15137 /* Calls to functions with a different TOC, such as calls to
15138 shared objects, need to alter the TOC pointer. This is
15139 done using a linkage stub. A REL24 branching to these
15140 linkage stubs needs to be followed by a nop, as the nop
15141 will be replaced with an instruction to restore the TOC
15142 base pointer. */
15143 fdh = h;
15144 if (h != NULL
15145 && h->oh != NULL
15146 && h->oh->is_func_descriptor)
15147 fdh = ppc_follow_link (h->oh);
15148 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15149 htab);
15150 if ((r_type == R_PPC64_PLTCALL
15151 || r_type == R_PPC64_PLTCALL_NOTOC)
15152 && stub_entry != NULL
15153 && stub_entry->stub_type >= ppc_stub_plt_call
15154 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15155 stub_entry = NULL;
15156
15157 if (stub_entry != NULL
15158 && ((stub_entry->stub_type >= ppc_stub_plt_call
15159 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15160 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15161 || stub_entry->stub_type == ppc_stub_plt_branch_both
15162 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15163 || stub_entry->stub_type == ppc_stub_long_branch_both))
15164 {
15165 bfd_boolean can_plt_call = FALSE;
15166
15167 if (stub_entry->stub_type == ppc_stub_plt_call
15168 && !htab->opd_abi
15169 && htab->params->plt_localentry0 != 0
15170 && is_elfv2_localentry0 (&h->elf))
15171 {
15172 /* The function doesn't use or change r2. */
15173 can_plt_call = TRUE;
15174 }
15175 else if (r_type == R_PPC64_REL24_NOTOC)
15176 {
15177 /* NOTOC calls don't need to restore r2. */
15178 can_plt_call = TRUE;
15179 }
15180
15181 /* All of these stubs may modify r2, so there must be a
15182 branch and link followed by a nop. The nop is
15183 replaced by an insn to restore r2. */
15184 else if (rel->r_offset + 8 <= input_section->size)
15185 {
15186 unsigned long br;
15187
15188 br = bfd_get_32 (input_bfd,
15189 contents + rel->r_offset);
15190 if ((br & 1) != 0)
15191 {
15192 unsigned long nop;
15193
15194 nop = bfd_get_32 (input_bfd,
15195 contents + rel->r_offset + 4);
15196 if (nop == LD_R2_0R1 + STK_TOC (htab))
15197 can_plt_call = TRUE;
15198 else if (nop == NOP
15199 || nop == CROR_151515
15200 || nop == CROR_313131)
15201 {
15202 if (h != NULL
15203 && (h == htab->tls_get_addr_fd
15204 || h == htab->tls_get_addr)
15205 && htab->params->tls_get_addr_opt)
15206 {
15207 /* Special stub used, leave nop alone. */
15208 }
15209 else
15210 bfd_put_32 (input_bfd,
15211 LD_R2_0R1 + STK_TOC (htab),
15212 contents + rel->r_offset + 4);
15213 can_plt_call = TRUE;
15214 }
15215 }
15216 }
15217
15218 if (!can_plt_call && h != NULL)
15219 {
15220 const char *name = h->elf.root.root.string;
15221
15222 if (*name == '.')
15223 ++name;
15224
15225 if (strncmp (name, "__libc_start_main", 17) == 0
15226 && (name[17] == 0 || name[17] == '@'))
15227 {
15228 /* Allow crt1 branch to go via a toc adjusting
15229 stub. Other calls that never return could do
15230 the same, if we could detect such. */
15231 can_plt_call = TRUE;
15232 }
15233 }
15234
15235 if (!can_plt_call)
15236 {
15237 /* g++ as of 20130507 emits self-calls without a
15238 following nop. This is arguably wrong since we
15239 have conflicting information. On the one hand a
15240 global symbol and on the other a local call
15241 sequence, but don't error for this special case.
15242 It isn't possible to cheaply verify we have
15243 exactly such a call. Allow all calls to the same
15244 section. */
15245 asection *code_sec = sec;
15246
15247 if (get_opd_info (sec) != NULL)
15248 {
15249 bfd_vma off = (relocation + addend
15250 - sec->output_section->vma
15251 - sec->output_offset);
15252
15253 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
15254 }
15255 if (code_sec == input_section)
15256 can_plt_call = TRUE;
15257 }
15258
15259 if (!can_plt_call)
15260 {
15261 if (stub_entry->stub_type >= ppc_stub_plt_call
15262 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15263 info->callbacks->einfo
15264 /* xgettext:c-format */
15265 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15266 "(plt call stub)\n"),
15267 input_bfd, input_section, rel->r_offset, sym_name);
15268 else
15269 info->callbacks->einfo
15270 /* xgettext:c-format */
15271 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15272 "(toc save/adjust stub)\n"),
15273 input_bfd, input_section, rel->r_offset, sym_name);
15274
15275 bfd_set_error (bfd_error_bad_value);
15276 ret = FALSE;
15277 }
15278
15279 if (can_plt_call
15280 && stub_entry->stub_type >= ppc_stub_plt_call
15281 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15282 unresolved_reloc = FALSE;
15283 }
15284
15285 if ((stub_entry == NULL
15286 || stub_entry->stub_type == ppc_stub_long_branch
15287 || stub_entry->stub_type == ppc_stub_plt_branch)
15288 && get_opd_info (sec) != NULL)
15289 {
15290 /* The branch destination is the value of the opd entry. */
15291 bfd_vma off = (relocation + addend
15292 - sec->output_section->vma
15293 - sec->output_offset);
15294 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
15295 if (dest != (bfd_vma) -1)
15296 {
15297 relocation = dest;
15298 addend = 0;
15299 reloc_dest = DEST_OPD;
15300 }
15301 }
15302
15303 /* If the branch is out of reach we ought to have a long
15304 branch stub. */
15305 from = (rel->r_offset
15306 + input_section->output_offset
15307 + input_section->output_section->vma);
15308
15309 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15310 ? fdh->elf.other
15311 : sym->st_other);
15312
15313 if (stub_entry != NULL
15314 && (stub_entry->stub_type == ppc_stub_long_branch
15315 || stub_entry->stub_type == ppc_stub_plt_branch)
15316 && (r_type == R_PPC64_ADDR14_BRTAKEN
15317 || r_type == R_PPC64_ADDR14_BRNTAKEN
15318 || (relocation + addend - from + max_br_offset
15319 < 2 * max_br_offset)))
15320 /* Don't use the stub if this branch is in range. */
15321 stub_entry = NULL;
15322
15323 if (stub_entry != NULL
15324 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15325 || stub_entry->stub_type == ppc_stub_long_branch_both
15326 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15327 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15328 && (r_type != R_PPC64_REL24_NOTOC
15329 || ((fdh ? fdh->elf.other : sym->st_other)
15330 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
15331 && (relocation + addend - from + max_br_offset
15332 < 2 * max_br_offset))
15333 stub_entry = NULL;
15334
15335 if (stub_entry != NULL
15336 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15337 || stub_entry->stub_type == ppc_stub_long_branch_both
15338 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15339 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15340 && r_type == R_PPC64_REL24_NOTOC
15341 && (relocation + addend - from + max_br_offset
15342 < 2 * max_br_offset))
15343 stub_entry = NULL;
15344
15345 if (stub_entry != NULL)
15346 {
15347 /* Munge up the value and addend so that we call the stub
15348 rather than the procedure directly. */
15349 asection *stub_sec = stub_entry->group->stub_sec;
15350
15351 if (stub_entry->stub_type == ppc_stub_save_res)
15352 relocation += (stub_sec->output_offset
15353 + stub_sec->output_section->vma
15354 + stub_sec->size - htab->sfpr->size
15355 - htab->sfpr->output_offset
15356 - htab->sfpr->output_section->vma);
15357 else
15358 relocation = (stub_entry->stub_offset
15359 + stub_sec->output_offset
15360 + stub_sec->output_section->vma);
15361 addend = 0;
15362 reloc_dest = DEST_STUB;
15363
15364 if (((stub_entry->stub_type == ppc_stub_plt_call
15365 && ALWAYS_EMIT_R2SAVE)
15366 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15367 || stub_entry->stub_type == ppc_stub_plt_call_both)
15368 && !(h != NULL
15369 && (h == htab->tls_get_addr_fd
15370 || h == htab->tls_get_addr)
15371 && htab->params->tls_get_addr_opt)
15372 && rel + 1 < relend
15373 && rel[1].r_offset == rel->r_offset + 4
15374 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15375 relocation += 4;
15376 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15377 || stub_entry->stub_type == ppc_stub_plt_branch_both
15378 || stub_entry->stub_type == ppc_stub_plt_call_both)
15379 && r_type == R_PPC64_REL24_NOTOC)
15380 relocation += 4;
15381
15382 if (r_type == R_PPC64_REL24_NOTOC
15383 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
15384 || stub_entry->stub_type == ppc_stub_plt_call_both))
15385 htab->notoc_plt = 1;
15386 }
15387
15388 if (insn != 0)
15389 {
15390 if (is_isa_v2)
15391 {
15392 /* Set 'a' bit. This is 0b00010 in BO field for branch
15393 on CR(BI) insns (BO == 001at or 011at), and 0b01000
15394 for branch on CTR insns (BO == 1a00t or 1a01t). */
15395 if ((insn & (0x14 << 21)) == (0x04 << 21))
15396 insn |= 0x02 << 21;
15397 else if ((insn & (0x14 << 21)) == (0x10 << 21))
15398 insn |= 0x08 << 21;
15399 else
15400 break;
15401 }
15402 else
15403 {
15404 /* Invert 'y' bit if not the default. */
15405 if ((bfd_signed_vma) (relocation + addend - from) < 0)
15406 insn ^= 0x01 << 21;
15407 }
15408
15409 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15410 }
15411
15412 /* NOP out calls to undefined weak functions.
15413 We can thus call a weak function without first
15414 checking whether the function is defined. */
15415 else if (h != NULL
15416 && h->elf.root.type == bfd_link_hash_undefweak
15417 && h->elf.dynindx == -1
15418 && (r_type == R_PPC64_REL24
15419 || r_type == R_PPC64_REL24_NOTOC)
15420 && relocation == 0
15421 && addend == 0)
15422 {
15423 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15424 goto copy_reloc;
15425 }
15426 break;
15427
15428 case R_PPC64_GOT16_DS:
15429 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15430 if (relocation + addend - from + 0x8000 < 0x10000
15431 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15432 {
15433 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15434 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15435 {
15436 insn += (14u << 26) - (58u << 26);
15437 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15438 r_type = R_PPC64_TOC16;
15439 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15440 }
15441 }
15442 break;
15443
15444 case R_PPC64_GOT16_LO_DS:
15445 case R_PPC64_GOT16_HA:
15446 from = TOCstart + htab->sec_info[input_section->id].toc_off;
15447 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
15448 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15449 {
15450 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15451 if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15452 {
15453 insn += (14u << 26) - (58u << 26);
15454 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15455 r_type = R_PPC64_TOC16_LO;
15456 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15457 }
15458 else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
15459 {
15460 r_type = R_PPC64_TOC16_HA;
15461 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15462 }
15463 }
15464 break;
15465
15466 case R_PPC64_GOT_PCREL34:
15467 from = (rel->r_offset
15468 + input_section->output_section->vma
15469 + input_section->output_offset);
15470 if (relocation - from + (1ULL << 33) < 1ULL << 34
15471 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15472 {
15473 offset = rel->r_offset;
15474 pinsn = bfd_get_32 (input_bfd, contents + offset);
15475 pinsn <<= 32;
15476 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15477 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15478 == ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
15479 {
15480 /* Replace with paddi. */
15481 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
15482 r_type = R_PPC64_PCREL34;
15483 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15484 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
15485 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
15486 goto pcrelopt;
15487 }
15488 }
15489 break;
15490
15491 case R_PPC64_PCREL34:
15492 if (SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15493 {
15494 offset = rel->r_offset;
15495 pinsn = bfd_get_32 (input_bfd, contents + offset);
15496 pinsn <<= 32;
15497 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15498 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15499 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
15500 | (14ULL << 26) /* paddi */))
15501 {
15502 pcrelopt:
15503 if (rel + 1 < relend
15504 && rel[1].r_offset == offset
15505 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT))
15506 {
15507 bfd_vma off2 = rel[1].r_addend;
15508 if (off2 == 0)
15509 /* zero means next insn. */
15510 off2 = 8;
15511 off2 += offset;
15512 if (off2 + 4 <= input_section->size)
15513 {
15514 uint64_t pinsn2;
15515 bfd_signed_vma addend_off;
15516 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
15517 pinsn2 <<= 32;
15518 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15519 {
15520 if (off2 + 8 > input_section->size)
15521 break;
15522 pinsn2 |= bfd_get_32 (input_bfd,
15523 contents + off2 + 4);
15524 }
15525 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
15526 {
15527 addend += addend_off;
15528 rel->r_addend = addend;
15529 bfd_put_32 (input_bfd, pinsn >> 32,
15530 contents + offset);
15531 bfd_put_32 (input_bfd, pinsn,
15532 contents + offset + 4);
15533 bfd_put_32 (input_bfd, pinsn2 >> 32,
15534 contents + off2);
15535 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15536 bfd_put_32 (input_bfd, pinsn2,
15537 contents + off2 + 4);
15538 }
15539 }
15540 }
15541 }
15542 }
15543 break;
15544 }
15545
15546 tls_type = 0;
15547 save_unresolved_reloc = unresolved_reloc;
15548 switch (r_type)
15549 {
15550 default:
15551 /* xgettext:c-format */
15552 _bfd_error_handler (_("%pB: %s unsupported"),
15553 input_bfd, ppc64_elf_howto_table[r_type]->name);
15554
15555 bfd_set_error (bfd_error_bad_value);
15556 ret = FALSE;
15557 goto copy_reloc;
15558
15559 case R_PPC64_NONE:
15560 case R_PPC64_TLS:
15561 case R_PPC64_TLSGD:
15562 case R_PPC64_TLSLD:
15563 case R_PPC64_TOCSAVE:
15564 case R_PPC64_GNU_VTINHERIT:
15565 case R_PPC64_GNU_VTENTRY:
15566 case R_PPC64_ENTRY:
15567 case R_PPC64_PCREL_OPT:
15568 goto copy_reloc;
15569
15570 /* GOT16 relocations. Like an ADDR16 using the symbol's
15571 address in the GOT as relocation value instead of the
15572 symbol's value itself. Also, create a GOT entry for the
15573 symbol and put the symbol value there. */
15574 case R_PPC64_GOT_TLSGD16:
15575 case R_PPC64_GOT_TLSGD16_LO:
15576 case R_PPC64_GOT_TLSGD16_HI:
15577 case R_PPC64_GOT_TLSGD16_HA:
15578 case R_PPC64_GOT_TLSGD34:
15579 tls_type = TLS_TLS | TLS_GD;
15580 goto dogot;
15581
15582 case R_PPC64_GOT_TLSLD16:
15583 case R_PPC64_GOT_TLSLD16_LO:
15584 case R_PPC64_GOT_TLSLD16_HI:
15585 case R_PPC64_GOT_TLSLD16_HA:
15586 case R_PPC64_GOT_TLSLD34:
15587 tls_type = TLS_TLS | TLS_LD;
15588 goto dogot;
15589
15590 case R_PPC64_GOT_TPREL16_DS:
15591 case R_PPC64_GOT_TPREL16_LO_DS:
15592 case R_PPC64_GOT_TPREL16_HI:
15593 case R_PPC64_GOT_TPREL16_HA:
15594 case R_PPC64_GOT_TPREL34:
15595 tls_type = TLS_TLS | TLS_TPREL;
15596 goto dogot;
15597
15598 case R_PPC64_GOT_DTPREL16_DS:
15599 case R_PPC64_GOT_DTPREL16_LO_DS:
15600 case R_PPC64_GOT_DTPREL16_HI:
15601 case R_PPC64_GOT_DTPREL16_HA:
15602 case R_PPC64_GOT_DTPREL34:
15603 tls_type = TLS_TLS | TLS_DTPREL;
15604 goto dogot;
15605
15606 case R_PPC64_GOT16:
15607 case R_PPC64_GOT16_LO:
15608 case R_PPC64_GOT16_HI:
15609 case R_PPC64_GOT16_HA:
15610 case R_PPC64_GOT16_DS:
15611 case R_PPC64_GOT16_LO_DS:
15612 case R_PPC64_GOT_PCREL34:
15613 dogot:
15614 {
15615 /* Relocation is to the entry for this symbol in the global
15616 offset table. */
15617 asection *got;
15618 bfd_vma *offp;
15619 bfd_vma off;
15620 unsigned long indx = 0;
15621 struct got_entry *ent;
15622
15623 if (tls_type == (TLS_TLS | TLS_LD)
15624 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15625 ent = ppc64_tlsld_got (input_bfd);
15626 else
15627 {
15628 if (h != NULL)
15629 {
15630 if (!htab->elf.dynamic_sections_created
15631 || h->elf.dynindx == -1
15632 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15633 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15634 /* This is actually a static link, or it is a
15635 -Bsymbolic link and the symbol is defined
15636 locally, or the symbol was forced to be local
15637 because of a version file. */
15638 ;
15639 else
15640 {
15641 indx = h->elf.dynindx;
15642 unresolved_reloc = FALSE;
15643 }
15644 ent = h->elf.got.glist;
15645 }
15646 else
15647 {
15648 if (local_got_ents == NULL)
15649 abort ();
15650 ent = local_got_ents[r_symndx];
15651 }
15652
15653 for (; ent != NULL; ent = ent->next)
15654 if (ent->addend == orig_rel.r_addend
15655 && ent->owner == input_bfd
15656 && ent->tls_type == tls_type)
15657 break;
15658 }
15659
15660 if (ent == NULL)
15661 abort ();
15662 if (ent->is_indirect)
15663 ent = ent->got.ent;
15664 offp = &ent->got.offset;
15665 got = ppc64_elf_tdata (ent->owner)->got;
15666 if (got == NULL)
15667 abort ();
15668
15669 /* The offset must always be a multiple of 8. We use the
15670 least significant bit to record whether we have already
15671 processed this entry. */
15672 off = *offp;
15673 if ((off & 1) != 0)
15674 off &= ~1;
15675 else
15676 {
15677 /* Generate relocs for the dynamic linker, except in
15678 the case of TLSLD where we'll use one entry per
15679 module. */
15680 asection *relgot;
15681 bfd_boolean ifunc;
15682
15683 *offp = off | 1;
15684 relgot = NULL;
15685 ifunc = (h != NULL
15686 ? h->elf.type == STT_GNU_IFUNC
15687 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
15688 if (ifunc)
15689 {
15690 relgot = htab->elf.irelplt;
15691 if (indx == 0)
15692 htab->local_ifunc_resolver = 1;
15693 else if (is_static_defined (&h->elf))
15694 htab->maybe_local_ifunc_resolver = 1;
15695 }
15696 else if (indx != 0
15697 || (bfd_link_pic (info)
15698 && (h == NULL
15699 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15700 && !(tls_type != 0
15701 && bfd_link_executable (info)
15702 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
15703 relgot = ppc64_elf_tdata (ent->owner)->relgot;
15704 if (relgot != NULL)
15705 {
15706 outrel.r_offset = (got->output_section->vma
15707 + got->output_offset
15708 + off);
15709 outrel.r_addend = orig_rel.r_addend;
15710 if (tls_type & (TLS_LD | TLS_GD))
15711 {
15712 outrel.r_addend = 0;
15713 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
15714 if (tls_type == (TLS_TLS | TLS_GD))
15715 {
15716 loc = relgot->contents;
15717 loc += (relgot->reloc_count++
15718 * sizeof (Elf64_External_Rela));
15719 bfd_elf64_swap_reloca_out (output_bfd,
15720 &outrel, loc);
15721 outrel.r_offset += 8;
15722 outrel.r_addend = orig_rel.r_addend;
15723 outrel.r_info
15724 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15725 }
15726 }
15727 else if (tls_type == (TLS_TLS | TLS_DTPREL))
15728 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15729 else if (tls_type == (TLS_TLS | TLS_TPREL))
15730 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15731 else if (indx != 0)
15732 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15733 else
15734 {
15735 if (ifunc)
15736 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15737 else
15738 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15739
15740 /* Write the .got section contents for the sake
15741 of prelink. */
15742 loc = got->contents + off;
15743 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15744 loc);
15745 }
15746
15747 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15748 {
15749 outrel.r_addend += relocation;
15750 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15751 {
15752 if (htab->elf.tls_sec == NULL)
15753 outrel.r_addend = 0;
15754 else
15755 outrel.r_addend -= htab->elf.tls_sec->vma;
15756 }
15757 }
15758 loc = relgot->contents;
15759 loc += (relgot->reloc_count++
15760 * sizeof (Elf64_External_Rela));
15761 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15762 }
15763
15764 /* Init the .got section contents here if we're not
15765 emitting a reloc. */
15766 else
15767 {
15768 relocation += orig_rel.r_addend;
15769 if (tls_type != 0)
15770 {
15771 if (htab->elf.tls_sec == NULL)
15772 relocation = 0;
15773 else
15774 {
15775 if (tls_type & TLS_LD)
15776 relocation = 0;
15777 else
15778 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15779 if (tls_type & TLS_TPREL)
15780 relocation += DTP_OFFSET - TP_OFFSET;
15781 }
15782
15783 if (tls_type & (TLS_GD | TLS_LD))
15784 {
15785 bfd_put_64 (output_bfd, relocation,
15786 got->contents + off + 8);
15787 relocation = 1;
15788 }
15789 }
15790 bfd_put_64 (output_bfd, relocation,
15791 got->contents + off);
15792 }
15793 }
15794
15795 if (off >= (bfd_vma) -2)
15796 abort ();
15797
15798 relocation = got->output_section->vma + got->output_offset + off;
15799 addend = 0;
15800 if (!(r_type == R_PPC64_GOT_PCREL34
15801 || r_type == R_PPC64_GOT_TLSGD34
15802 || r_type == R_PPC64_GOT_TLSLD34
15803 || r_type == R_PPC64_GOT_TPREL34
15804 || r_type == R_PPC64_GOT_DTPREL34))
15805 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15806 }
15807 break;
15808
15809 case R_PPC64_PLT16_HA:
15810 case R_PPC64_PLT16_HI:
15811 case R_PPC64_PLT16_LO:
15812 case R_PPC64_PLT16_LO_DS:
15813 case R_PPC64_PLT_PCREL34:
15814 case R_PPC64_PLT_PCREL34_NOTOC:
15815 case R_PPC64_PLT32:
15816 case R_PPC64_PLT64:
15817 case R_PPC64_PLTSEQ:
15818 case R_PPC64_PLTSEQ_NOTOC:
15819 case R_PPC64_PLTCALL:
15820 case R_PPC64_PLTCALL_NOTOC:
15821 /* Relocation is to the entry for this symbol in the
15822 procedure linkage table. */
15823 unresolved_reloc = TRUE;
15824 {
15825 struct plt_entry **plt_list = NULL;
15826 if (h != NULL)
15827 plt_list = &h->elf.plt.plist;
15828 else if (local_got_ents != NULL)
15829 {
15830 struct plt_entry **local_plt = (struct plt_entry **)
15831 (local_got_ents + symtab_hdr->sh_info);
15832 plt_list = local_plt + r_symndx;
15833 }
15834 if (plt_list)
15835 {
15836 struct plt_entry *ent;
15837
15838 for (ent = *plt_list; ent != NULL; ent = ent->next)
15839 if (ent->plt.offset != (bfd_vma) -1
15840 && ent->addend == orig_rel.r_addend)
15841 {
15842 asection *plt;
15843 bfd_vma got;
15844
15845 plt = htab->elf.splt;
15846 if (!htab->elf.dynamic_sections_created
15847 || h == NULL
15848 || h->elf.dynindx == -1)
15849 {
15850 if (h != NULL
15851 ? h->elf.type == STT_GNU_IFUNC
15852 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15853 plt = htab->elf.iplt;
15854 else
15855 plt = htab->pltlocal;
15856 }
15857 relocation = (plt->output_section->vma
15858 + plt->output_offset
15859 + ent->plt.offset);
15860 if (r_type == R_PPC64_PLT16_HA
15861 || r_type == R_PPC64_PLT16_HI
15862 || r_type == R_PPC64_PLT16_LO
15863 || r_type == R_PPC64_PLT16_LO_DS)
15864 {
15865 got = (elf_gp (output_bfd)
15866 + htab->sec_info[input_section->id].toc_off);
15867 relocation -= got;
15868 }
15869 addend = 0;
15870 unresolved_reloc = FALSE;
15871 break;
15872 }
15873 }
15874 }
15875 break;
15876
15877 case R_PPC64_TOC:
15878 /* Relocation value is TOC base. */
15879 relocation = TOCstart;
15880 if (r_symndx == STN_UNDEF)
15881 relocation += htab->sec_info[input_section->id].toc_off;
15882 else if (unresolved_reloc)
15883 ;
15884 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15885 relocation += htab->sec_info[sec->id].toc_off;
15886 else
15887 unresolved_reloc = TRUE;
15888 goto dodyn;
15889
15890 /* TOC16 relocs. We want the offset relative to the TOC base,
15891 which is the address of the start of the TOC plus 0x8000.
15892 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15893 in this order. */
15894 case R_PPC64_TOC16:
15895 case R_PPC64_TOC16_LO:
15896 case R_PPC64_TOC16_HI:
15897 case R_PPC64_TOC16_DS:
15898 case R_PPC64_TOC16_LO_DS:
15899 case R_PPC64_TOC16_HA:
15900 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15901 if (h != NULL)
15902 goto dodyn;
15903 break;
15904
15905 /* Relocate against the beginning of the section. */
15906 case R_PPC64_SECTOFF:
15907 case R_PPC64_SECTOFF_LO:
15908 case R_PPC64_SECTOFF_HI:
15909 case R_PPC64_SECTOFF_DS:
15910 case R_PPC64_SECTOFF_LO_DS:
15911 case R_PPC64_SECTOFF_HA:
15912 if (sec != NULL)
15913 addend -= sec->output_section->vma;
15914 break;
15915
15916 case R_PPC64_REL16:
15917 case R_PPC64_REL16_LO:
15918 case R_PPC64_REL16_HI:
15919 case R_PPC64_REL16_HA:
15920 case R_PPC64_REL16_HIGH:
15921 case R_PPC64_REL16_HIGHA:
15922 case R_PPC64_REL16_HIGHER:
15923 case R_PPC64_REL16_HIGHERA:
15924 case R_PPC64_REL16_HIGHEST:
15925 case R_PPC64_REL16_HIGHESTA:
15926 case R_PPC64_REL16_HIGHER34:
15927 case R_PPC64_REL16_HIGHERA34:
15928 case R_PPC64_REL16_HIGHEST34:
15929 case R_PPC64_REL16_HIGHESTA34:
15930 case R_PPC64_REL16DX_HA:
15931 case R_PPC64_REL14:
15932 case R_PPC64_REL14_BRNTAKEN:
15933 case R_PPC64_REL14_BRTAKEN:
15934 case R_PPC64_REL24:
15935 case R_PPC64_REL24_NOTOC:
15936 case R_PPC64_PCREL34:
15937 case R_PPC64_PCREL28:
15938 break;
15939
15940 case R_PPC64_TPREL16:
15941 case R_PPC64_TPREL16_LO:
15942 case R_PPC64_TPREL16_HI:
15943 case R_PPC64_TPREL16_HA:
15944 case R_PPC64_TPREL16_DS:
15945 case R_PPC64_TPREL16_LO_DS:
15946 case R_PPC64_TPREL16_HIGH:
15947 case R_PPC64_TPREL16_HIGHA:
15948 case R_PPC64_TPREL16_HIGHER:
15949 case R_PPC64_TPREL16_HIGHERA:
15950 case R_PPC64_TPREL16_HIGHEST:
15951 case R_PPC64_TPREL16_HIGHESTA:
15952 case R_PPC64_TPREL34:
15953 if (h != NULL
15954 && h->elf.root.type == bfd_link_hash_undefweak
15955 && h->elf.dynindx == -1)
15956 {
15957 /* Make this relocation against an undefined weak symbol
15958 resolve to zero. This is really just a tweak, since
15959 code using weak externs ought to check that they are
15960 defined before using them. */
15961 bfd_byte *p = contents + rel->r_offset - d_offset;
15962
15963 insn = bfd_get_32 (input_bfd, p);
15964 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15965 if (insn != 0)
15966 bfd_put_32 (input_bfd, insn, p);
15967 break;
15968 }
15969 if (htab->elf.tls_sec != NULL)
15970 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15971 /* The TPREL16 relocs shouldn't really be used in shared
15972 libs or with non-local symbols as that will result in
15973 DT_TEXTREL being set, but support them anyway. */
15974 goto dodyn;
15975
15976 case R_PPC64_DTPREL16:
15977 case R_PPC64_DTPREL16_LO:
15978 case R_PPC64_DTPREL16_HI:
15979 case R_PPC64_DTPREL16_HA:
15980 case R_PPC64_DTPREL16_DS:
15981 case R_PPC64_DTPREL16_LO_DS:
15982 case R_PPC64_DTPREL16_HIGH:
15983 case R_PPC64_DTPREL16_HIGHA:
15984 case R_PPC64_DTPREL16_HIGHER:
15985 case R_PPC64_DTPREL16_HIGHERA:
15986 case R_PPC64_DTPREL16_HIGHEST:
15987 case R_PPC64_DTPREL16_HIGHESTA:
15988 case R_PPC64_DTPREL34:
15989 if (htab->elf.tls_sec != NULL)
15990 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15991 break;
15992
15993 case R_PPC64_ADDR64_LOCAL:
15994 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15995 ? h->elf.other
15996 : sym->st_other);
15997 break;
15998
15999 case R_PPC64_DTPMOD64:
16000 relocation = 1;
16001 addend = 0;
16002 goto dodyn;
16003
16004 case R_PPC64_TPREL64:
16005 if (htab->elf.tls_sec != NULL)
16006 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
16007 goto dodyn;
16008
16009 case R_PPC64_DTPREL64:
16010 if (htab->elf.tls_sec != NULL)
16011 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
16012 /* Fall through. */
16013
16014 /* Relocations that may need to be propagated if this is a
16015 dynamic object. */
16016 case R_PPC64_REL30:
16017 case R_PPC64_REL32:
16018 case R_PPC64_REL64:
16019 case R_PPC64_ADDR14:
16020 case R_PPC64_ADDR14_BRNTAKEN:
16021 case R_PPC64_ADDR14_BRTAKEN:
16022 case R_PPC64_ADDR16:
16023 case R_PPC64_ADDR16_DS:
16024 case R_PPC64_ADDR16_HA:
16025 case R_PPC64_ADDR16_HI:
16026 case R_PPC64_ADDR16_HIGH:
16027 case R_PPC64_ADDR16_HIGHA:
16028 case R_PPC64_ADDR16_HIGHER:
16029 case R_PPC64_ADDR16_HIGHERA:
16030 case R_PPC64_ADDR16_HIGHEST:
16031 case R_PPC64_ADDR16_HIGHESTA:
16032 case R_PPC64_ADDR16_LO:
16033 case R_PPC64_ADDR16_LO_DS:
16034 case R_PPC64_ADDR16_HIGHER34:
16035 case R_PPC64_ADDR16_HIGHERA34:
16036 case R_PPC64_ADDR16_HIGHEST34:
16037 case R_PPC64_ADDR16_HIGHESTA34:
16038 case R_PPC64_ADDR24:
16039 case R_PPC64_ADDR32:
16040 case R_PPC64_ADDR64:
16041 case R_PPC64_UADDR16:
16042 case R_PPC64_UADDR32:
16043 case R_PPC64_UADDR64:
16044 case R_PPC64_D34:
16045 case R_PPC64_D34_LO:
16046 case R_PPC64_D34_HI30:
16047 case R_PPC64_D34_HA30:
16048 case R_PPC64_D28:
16049 dodyn:
16050 if ((input_section->flags & SEC_ALLOC) == 0)
16051 break;
16052
16053 if (NO_OPD_RELOCS && is_opd)
16054 break;
16055
16056 if (bfd_link_pic (info)
16057 ? ((h == NULL
16058 || h->dyn_relocs != NULL)
16059 && ((h != NULL && pc_dynrelocs (h))
16060 || must_be_dyn_reloc (info, r_type)))
16061 : (h != NULL
16062 ? h->dyn_relocs != NULL
16063 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16064 {
16065 bfd_boolean skip, relocate;
16066 asection *sreloc;
16067 bfd_vma out_off;
16068 long indx = 0;
16069
16070 /* When generating a dynamic object, these relocations
16071 are copied into the output file to be resolved at run
16072 time. */
16073
16074 skip = FALSE;
16075 relocate = FALSE;
16076
16077 out_off = _bfd_elf_section_offset (output_bfd, info,
16078 input_section, rel->r_offset);
16079 if (out_off == (bfd_vma) -1)
16080 skip = TRUE;
16081 else if (out_off == (bfd_vma) -2)
16082 skip = TRUE, relocate = TRUE;
16083 out_off += (input_section->output_section->vma
16084 + input_section->output_offset);
16085 outrel.r_offset = out_off;
16086 outrel.r_addend = rel->r_addend;
16087
16088 /* Optimize unaligned reloc use. */
16089 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
16090 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
16091 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
16092 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
16093 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
16094 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
16095 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16096 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16097 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16098
16099 if (skip)
16100 memset (&outrel, 0, sizeof outrel);
16101 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16102 && !is_opd
16103 && r_type != R_PPC64_TOC)
16104 {
16105 indx = h->elf.dynindx;
16106 BFD_ASSERT (indx != -1);
16107 outrel.r_info = ELF64_R_INFO (indx, r_type);
16108 }
16109 else
16110 {
16111 /* This symbol is local, or marked to become local,
16112 or this is an opd section reloc which must point
16113 at a local function. */
16114 outrel.r_addend += relocation;
16115 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
16116 {
16117 if (is_opd && h != NULL)
16118 {
16119 /* Lie about opd entries. This case occurs
16120 when building shared libraries and we
16121 reference a function in another shared
16122 lib. The same thing happens for a weak
16123 definition in an application that's
16124 overridden by a strong definition in a
16125 shared lib. (I believe this is a generic
16126 bug in binutils handling of weak syms.)
16127 In these cases we won't use the opd
16128 entry in this lib. */
16129 unresolved_reloc = FALSE;
16130 }
16131 if (!is_opd
16132 && r_type == R_PPC64_ADDR64
16133 && (h != NULL
16134 ? h->elf.type == STT_GNU_IFUNC
16135 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16136 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16137 else
16138 {
16139 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16140
16141 /* We need to relocate .opd contents for ld.so.
16142 Prelink also wants simple and consistent rules
16143 for relocs. This make all RELATIVE relocs have
16144 *r_offset equal to r_addend. */
16145 relocate = TRUE;
16146 }
16147 }
16148 else
16149 {
16150 if (h != NULL
16151 ? h->elf.type == STT_GNU_IFUNC
16152 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16153 {
16154 info->callbacks->einfo
16155 /* xgettext:c-format */
16156 (_("%H: %s for indirect "
16157 "function `%pT' unsupported\n"),
16158 input_bfd, input_section, rel->r_offset,
16159 ppc64_elf_howto_table[r_type]->name,
16160 sym_name);
16161 ret = FALSE;
16162 }
16163 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
16164 ;
16165 else if (sec == NULL || sec->owner == NULL)
16166 {
16167 bfd_set_error (bfd_error_bad_value);
16168 return FALSE;
16169 }
16170 else
16171 {
16172 asection *osec = sec->output_section;
16173
16174 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16175 {
16176 /* TLS symbol values are relative to the
16177 TLS segment. Dynamic relocations for
16178 local TLS symbols therefore can't be
16179 reduced to a relocation against their
16180 section symbol because it holds the
16181 address of the section, not a value
16182 relative to the TLS segment. We could
16183 change the .tdata dynamic section symbol
16184 to be zero value but STN_UNDEF works
16185 and is used elsewhere, eg. for TPREL64
16186 GOT relocs against local TLS symbols. */
16187 osec = htab->elf.tls_sec;
16188 indx = 0;
16189 }
16190 else
16191 {
16192 indx = elf_section_data (osec)->dynindx;
16193 if (indx == 0)
16194 {
16195 if ((osec->flags & SEC_READONLY) == 0
16196 && htab->elf.data_index_section != NULL)
16197 osec = htab->elf.data_index_section;
16198 else
16199 osec = htab->elf.text_index_section;
16200 indx = elf_section_data (osec)->dynindx;
16201 }
16202 BFD_ASSERT (indx != 0);
16203 }
16204
16205 /* We are turning this relocation into one
16206 against a section symbol, so subtract out
16207 the output section's address but not the
16208 offset of the input section in the output
16209 section. */
16210 outrel.r_addend -= osec->vma;
16211 }
16212
16213 outrel.r_info = ELF64_R_INFO (indx, r_type);
16214 }
16215 }
16216
16217 sreloc = elf_section_data (input_section)->sreloc;
16218 if (h != NULL
16219 ? h->elf.type == STT_GNU_IFUNC
16220 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16221 {
16222 sreloc = htab->elf.irelplt;
16223 if (indx == 0)
16224 htab->local_ifunc_resolver = 1;
16225 else if (is_static_defined (&h->elf))
16226 htab->maybe_local_ifunc_resolver = 1;
16227 }
16228 if (sreloc == NULL)
16229 abort ();
16230
16231 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16232 >= sreloc->size)
16233 abort ();
16234 loc = sreloc->contents;
16235 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
16236 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16237
16238 if (!warned_dynamic
16239 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
16240 {
16241 info->callbacks->einfo
16242 /* xgettext:c-format */
16243 (_("%X%P: %pB: %s against %pT "
16244 "is not supported by glibc as a dynamic relocation\n"),
16245 input_bfd,
16246 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
16247 sym_name);
16248 warned_dynamic = TRUE;
16249 }
16250
16251 /* If this reloc is against an external symbol, it will
16252 be computed at runtime, so there's no need to do
16253 anything now. However, for the sake of prelink ensure
16254 that the section contents are a known value. */
16255 if (!relocate)
16256 {
16257 unresolved_reloc = FALSE;
16258 /* The value chosen here is quite arbitrary as ld.so
16259 ignores section contents except for the special
16260 case of .opd where the contents might be accessed
16261 before relocation. Choose zero, as that won't
16262 cause reloc overflow. */
16263 relocation = 0;
16264 addend = 0;
16265 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16266 to improve backward compatibility with older
16267 versions of ld. */
16268 if (r_type == R_PPC64_ADDR64)
16269 addend = outrel.r_addend;
16270 /* Adjust pc_relative relocs to have zero in *r_offset. */
16271 else if (ppc64_elf_howto_table[r_type]->pc_relative)
16272 addend = outrel.r_offset;
16273 }
16274 }
16275 break;
16276
16277 case R_PPC64_COPY:
16278 case R_PPC64_GLOB_DAT:
16279 case R_PPC64_JMP_SLOT:
16280 case R_PPC64_JMP_IREL:
16281 case R_PPC64_RELATIVE:
16282 /* We shouldn't ever see these dynamic relocs in relocatable
16283 files. */
16284 /* Fall through. */
16285
16286 case R_PPC64_PLTGOT16:
16287 case R_PPC64_PLTGOT16_DS:
16288 case R_PPC64_PLTGOT16_HA:
16289 case R_PPC64_PLTGOT16_HI:
16290 case R_PPC64_PLTGOT16_LO:
16291 case R_PPC64_PLTGOT16_LO_DS:
16292 case R_PPC64_PLTREL32:
16293 case R_PPC64_PLTREL64:
16294 /* These ones haven't been implemented yet. */
16295
16296 info->callbacks->einfo
16297 /* xgettext:c-format */
16298 (_("%P: %pB: %s is not supported for `%pT'\n"),
16299 input_bfd,
16300 ppc64_elf_howto_table[r_type]->name, sym_name);
16301
16302 bfd_set_error (bfd_error_invalid_operation);
16303 ret = FALSE;
16304 goto copy_reloc;
16305 }
16306
16307 /* Multi-instruction sequences that access the TOC can be
16308 optimized, eg. addis ra,r2,0; addi rb,ra,x;
16309 to nop; addi rb,r2,x; */
16310 switch (r_type)
16311 {
16312 default:
16313 break;
16314
16315 case R_PPC64_GOT_TLSLD16_HI:
16316 case R_PPC64_GOT_TLSGD16_HI:
16317 case R_PPC64_GOT_TPREL16_HI:
16318 case R_PPC64_GOT_DTPREL16_HI:
16319 case R_PPC64_GOT16_HI:
16320 case R_PPC64_TOC16_HI:
16321 /* These relocs would only be useful if building up an
16322 offset to later add to r2, perhaps in an indexed
16323 addressing mode instruction. Don't try to optimize.
16324 Unfortunately, the possibility of someone building up an
16325 offset like this or even with the HA relocs, means that
16326 we need to check the high insn when optimizing the low
16327 insn. */
16328 break;
16329
16330 case R_PPC64_PLTCALL_NOTOC:
16331 if (!unresolved_reloc)
16332 htab->notoc_plt = 1;
16333 /* Fall through. */
16334 case R_PPC64_PLTCALL:
16335 if (unresolved_reloc)
16336 {
16337 /* No plt entry. Make this into a direct call. */
16338 bfd_byte *p = contents + rel->r_offset;
16339 insn = bfd_get_32 (input_bfd, p);
16340 insn &= 1;
16341 bfd_put_32 (input_bfd, B_DOT | insn, p);
16342 if (r_type == R_PPC64_PLTCALL)
16343 bfd_put_32 (input_bfd, NOP, p + 4);
16344 unresolved_reloc = save_unresolved_reloc;
16345 r_type = R_PPC64_REL24;
16346 }
16347 break;
16348
16349 case R_PPC64_PLTSEQ_NOTOC:
16350 case R_PPC64_PLTSEQ:
16351 if (unresolved_reloc)
16352 {
16353 unresolved_reloc = FALSE;
16354 goto nop_it;
16355 }
16356 break;
16357
16358 case R_PPC64_PLT_PCREL34_NOTOC:
16359 if (!unresolved_reloc)
16360 htab->notoc_plt = 1;
16361 /* Fall through. */
16362 case R_PPC64_PLT_PCREL34:
16363 if (unresolved_reloc)
16364 {
16365 bfd_byte *p = contents + rel->r_offset;
16366 bfd_put_32 (input_bfd, PNOP >> 32, p);
16367 bfd_put_32 (input_bfd, PNOP, p + 4);
16368 unresolved_reloc = FALSE;
16369 goto copy_reloc;
16370 }
16371 break;
16372
16373 case R_PPC64_PLT16_HA:
16374 if (unresolved_reloc)
16375 {
16376 unresolved_reloc = FALSE;
16377 goto nop_it;
16378 }
16379 /* Fall through. */
16380 case R_PPC64_GOT_TLSLD16_HA:
16381 case R_PPC64_GOT_TLSGD16_HA:
16382 case R_PPC64_GOT_TPREL16_HA:
16383 case R_PPC64_GOT_DTPREL16_HA:
16384 case R_PPC64_GOT16_HA:
16385 case R_PPC64_TOC16_HA:
16386 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16387 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16388 {
16389 bfd_byte *p;
16390 nop_it:
16391 p = contents + (rel->r_offset & ~3);
16392 bfd_put_32 (input_bfd, NOP, p);
16393 goto copy_reloc;
16394 }
16395 break;
16396
16397 case R_PPC64_PLT16_LO:
16398 case R_PPC64_PLT16_LO_DS:
16399 if (unresolved_reloc)
16400 {
16401 unresolved_reloc = FALSE;
16402 goto nop_it;
16403 }
16404 /* Fall through. */
16405 case R_PPC64_GOT_TLSLD16_LO:
16406 case R_PPC64_GOT_TLSGD16_LO:
16407 case R_PPC64_GOT_TPREL16_LO_DS:
16408 case R_PPC64_GOT_DTPREL16_LO_DS:
16409 case R_PPC64_GOT16_LO:
16410 case R_PPC64_GOT16_LO_DS:
16411 case R_PPC64_TOC16_LO:
16412 case R_PPC64_TOC16_LO_DS:
16413 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16414 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16415 {
16416 bfd_byte *p = contents + (rel->r_offset & ~3);
16417 insn = bfd_get_32 (input_bfd, p);
16418 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16419 {
16420 /* Transform addic to addi when we change reg. */
16421 insn &= ~((0x3f << 26) | (0x1f << 16));
16422 insn |= (14u << 26) | (2 << 16);
16423 }
16424 else
16425 {
16426 insn &= ~(0x1f << 16);
16427 insn |= 2 << 16;
16428 }
16429 bfd_put_32 (input_bfd, insn, p);
16430 }
16431 break;
16432
16433 case R_PPC64_TPREL16_HA:
16434 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16435 {
16436 bfd_byte *p = contents + (rel->r_offset & ~3);
16437 insn = bfd_get_32 (input_bfd, p);
16438 if ((insn & ((0x3f << 26) | 0x1f << 16))
16439 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16440 /* xgettext:c-format */
16441 info->callbacks->minfo
16442 (_("%H: warning: %s unexpected insn %#x.\n"),
16443 input_bfd, input_section, rel->r_offset,
16444 ppc64_elf_howto_table[r_type]->name, insn);
16445 else
16446 {
16447 bfd_put_32 (input_bfd, NOP, p);
16448 goto copy_reloc;
16449 }
16450 }
16451 break;
16452
16453 case R_PPC64_TPREL16_LO:
16454 case R_PPC64_TPREL16_LO_DS:
16455 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16456 {
16457 bfd_byte *p = contents + (rel->r_offset & ~3);
16458 insn = bfd_get_32 (input_bfd, p);
16459 insn &= ~(0x1f << 16);
16460 insn |= 13 << 16;
16461 bfd_put_32 (input_bfd, insn, p);
16462 }
16463 break;
16464 }
16465
16466 /* Do any further special processing. */
16467 switch (r_type)
16468 {
16469 default:
16470 break;
16471
16472 case R_PPC64_REL16_HA:
16473 case R_PPC64_REL16_HIGHA:
16474 case R_PPC64_REL16_HIGHERA:
16475 case R_PPC64_REL16_HIGHESTA:
16476 case R_PPC64_REL16DX_HA:
16477 case R_PPC64_ADDR16_HA:
16478 case R_PPC64_ADDR16_HIGHA:
16479 case R_PPC64_ADDR16_HIGHERA:
16480 case R_PPC64_ADDR16_HIGHESTA:
16481 case R_PPC64_TOC16_HA:
16482 case R_PPC64_SECTOFF_HA:
16483 case R_PPC64_TPREL16_HA:
16484 case R_PPC64_TPREL16_HIGHA:
16485 case R_PPC64_TPREL16_HIGHERA:
16486 case R_PPC64_TPREL16_HIGHESTA:
16487 case R_PPC64_DTPREL16_HA:
16488 case R_PPC64_DTPREL16_HIGHA:
16489 case R_PPC64_DTPREL16_HIGHERA:
16490 case R_PPC64_DTPREL16_HIGHESTA:
16491 /* It's just possible that this symbol is a weak symbol
16492 that's not actually defined anywhere. In that case,
16493 'sec' would be NULL, and we should leave the symbol
16494 alone (it will be set to zero elsewhere in the link). */
16495 if (sec == NULL)
16496 break;
16497 /* Fall through. */
16498
16499 case R_PPC64_GOT16_HA:
16500 case R_PPC64_PLTGOT16_HA:
16501 case R_PPC64_PLT16_HA:
16502 case R_PPC64_GOT_TLSGD16_HA:
16503 case R_PPC64_GOT_TLSLD16_HA:
16504 case R_PPC64_GOT_TPREL16_HA:
16505 case R_PPC64_GOT_DTPREL16_HA:
16506 /* Add 0x10000 if sign bit in 0:15 is set.
16507 Bits 0:15 are not used. */
16508 addend += 0x8000;
16509 break;
16510
16511 case R_PPC64_D34_HA30:
16512 case R_PPC64_ADDR16_HIGHERA34:
16513 case R_PPC64_ADDR16_HIGHESTA34:
16514 case R_PPC64_REL16_HIGHERA34:
16515 case R_PPC64_REL16_HIGHESTA34:
16516 if (sec != NULL)
16517 addend += 1ULL << 33;
16518 break;
16519
16520 case R_PPC64_ADDR16_DS:
16521 case R_PPC64_ADDR16_LO_DS:
16522 case R_PPC64_GOT16_DS:
16523 case R_PPC64_GOT16_LO_DS:
16524 case R_PPC64_PLT16_LO_DS:
16525 case R_PPC64_SECTOFF_DS:
16526 case R_PPC64_SECTOFF_LO_DS:
16527 case R_PPC64_TOC16_DS:
16528 case R_PPC64_TOC16_LO_DS:
16529 case R_PPC64_PLTGOT16_DS:
16530 case R_PPC64_PLTGOT16_LO_DS:
16531 case R_PPC64_GOT_TPREL16_DS:
16532 case R_PPC64_GOT_TPREL16_LO_DS:
16533 case R_PPC64_GOT_DTPREL16_DS:
16534 case R_PPC64_GOT_DTPREL16_LO_DS:
16535 case R_PPC64_TPREL16_DS:
16536 case R_PPC64_TPREL16_LO_DS:
16537 case R_PPC64_DTPREL16_DS:
16538 case R_PPC64_DTPREL16_LO_DS:
16539 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16540 mask = 3;
16541 /* If this reloc is against an lq, lxv, or stxv insn, then
16542 the value must be a multiple of 16. This is somewhat of
16543 a hack, but the "correct" way to do this by defining _DQ
16544 forms of all the _DS relocs bloats all reloc switches in
16545 this file. It doesn't make much sense to use these
16546 relocs in data, so testing the insn should be safe. */
16547 if ((insn & (0x3f << 26)) == (56u << 26)
16548 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
16549 mask = 15;
16550 relocation += addend;
16551 addend = insn & (mask ^ 3);
16552 if ((relocation & mask) != 0)
16553 {
16554 relocation ^= relocation & mask;
16555 info->callbacks->einfo
16556 /* xgettext:c-format */
16557 (_("%H: error: %s not a multiple of %u\n"),
16558 input_bfd, input_section, rel->r_offset,
16559 ppc64_elf_howto_table[r_type]->name,
16560 mask + 1);
16561 bfd_set_error (bfd_error_bad_value);
16562 ret = FALSE;
16563 goto copy_reloc;
16564 }
16565 break;
16566 }
16567
16568 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16569 because such sections are not SEC_ALLOC and thus ld.so will
16570 not process them. */
16571 howto = ppc64_elf_howto_table[(int) r_type];
16572 if (unresolved_reloc
16573 && !((input_section->flags & SEC_DEBUGGING) != 0
16574 && h->elf.def_dynamic)
16575 && _bfd_elf_section_offset (output_bfd, info, input_section,
16576 rel->r_offset) != (bfd_vma) -1)
16577 {
16578 info->callbacks->einfo
16579 /* xgettext:c-format */
16580 (_("%H: unresolvable %s against `%pT'\n"),
16581 input_bfd, input_section, rel->r_offset,
16582 howto->name,
16583 h->elf.root.root.string);
16584 ret = FALSE;
16585 }
16586
16587 /* 16-bit fields in insns mostly have signed values, but a
16588 few insns have 16-bit unsigned values. Really, we should
16589 have different reloc types. */
16590 if (howto->complain_on_overflow != complain_overflow_dont
16591 && howto->dst_mask == 0xffff
16592 && (input_section->flags & SEC_CODE) != 0)
16593 {
16594 enum complain_overflow complain = complain_overflow_signed;
16595
16596 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16597 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16598 complain = complain_overflow_bitfield;
16599 else if (howto->rightshift == 0
16600 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16601 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16602 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16603 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16604 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16605 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
16606 complain = complain_overflow_unsigned;
16607 if (howto->complain_on_overflow != complain)
16608 {
16609 alt_howto = *howto;
16610 alt_howto.complain_on_overflow = complain;
16611 howto = &alt_howto;
16612 }
16613 }
16614
16615 switch (r_type)
16616 {
16617 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
16618 case R_PPC64_D34:
16619 case R_PPC64_D34_LO:
16620 case R_PPC64_D34_HI30:
16621 case R_PPC64_D34_HA30:
16622 case R_PPC64_PCREL34:
16623 case R_PPC64_GOT_PCREL34:
16624 case R_PPC64_TPREL34:
16625 case R_PPC64_DTPREL34:
16626 case R_PPC64_GOT_TLSGD34:
16627 case R_PPC64_GOT_TLSLD34:
16628 case R_PPC64_GOT_TPREL34:
16629 case R_PPC64_GOT_DTPREL34:
16630 case R_PPC64_PLT_PCREL34:
16631 case R_PPC64_PLT_PCREL34_NOTOC:
16632 case R_PPC64_D28:
16633 case R_PPC64_PCREL28:
16634 if (rel->r_offset + 8 > input_section->size)
16635 r = bfd_reloc_outofrange;
16636 else
16637 {
16638 relocation += addend;
16639 if (howto->pc_relative)
16640 relocation -= (rel->r_offset
16641 + input_section->output_offset
16642 + input_section->output_section->vma);
16643 relocation >>= howto->rightshift;
16644
16645 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16646 pinsn <<= 32;
16647 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16648
16649 pinsn &= ~howto->dst_mask;
16650 pinsn |= (((relocation << 16) | (relocation & 0xffff))
16651 & howto->dst_mask);
16652 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
16653 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
16654 r = bfd_reloc_ok;
16655 if (howto->complain_on_overflow == complain_overflow_signed
16656 && (relocation + (1ULL << (howto->bitsize - 1))
16657 >= 1ULL << howto->bitsize))
16658 r = bfd_reloc_overflow;
16659 }
16660 break;
16661
16662 case R_PPC64_REL16DX_HA:
16663 if (rel->r_offset + 4 > input_section->size)
16664 r = bfd_reloc_outofrange;
16665 else
16666 {
16667 relocation += addend;
16668 relocation -= (rel->r_offset
16669 + input_section->output_offset
16670 + input_section->output_section->vma);
16671 relocation = (bfd_signed_vma) relocation >> 16;
16672 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16673 insn &= ~0x1fffc1;
16674 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
16675 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16676 r = bfd_reloc_ok;
16677 if (relocation + 0x8000 > 0xffff)
16678 r = bfd_reloc_overflow;
16679 }
16680 break;
16681
16682 default:
16683 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
16684 contents, rel->r_offset,
16685 relocation, addend);
16686 }
16687
16688 if (r != bfd_reloc_ok)
16689 {
16690 char *more_info = NULL;
16691 const char *reloc_name = howto->name;
16692
16693 if (reloc_dest != DEST_NORMAL)
16694 {
16695 more_info = bfd_malloc (strlen (reloc_name) + 8);
16696 if (more_info != NULL)
16697 {
16698 strcpy (more_info, reloc_name);
16699 strcat (more_info, (reloc_dest == DEST_OPD
16700 ? " (OPD)" : " (stub)"));
16701 reloc_name = more_info;
16702 }
16703 }
16704
16705 if (r == bfd_reloc_overflow)
16706 {
16707 /* On code like "if (foo) foo();" don't report overflow
16708 on a branch to zero when foo is undefined. */
16709 if (!warned
16710 && (reloc_dest == DEST_STUB
16711 || !(h != NULL
16712 && (h->elf.root.type == bfd_link_hash_undefweak
16713 || h->elf.root.type == bfd_link_hash_undefined)
16714 && is_branch_reloc (r_type))))
16715 info->callbacks->reloc_overflow (info, &h->elf.root,
16716 sym_name, reloc_name,
16717 orig_rel.r_addend,
16718 input_bfd, input_section,
16719 rel->r_offset);
16720 }
16721 else
16722 {
16723 info->callbacks->einfo
16724 /* xgettext:c-format */
16725 (_("%H: %s against `%pT': error %d\n"),
16726 input_bfd, input_section, rel->r_offset,
16727 reloc_name, sym_name, (int) r);
16728 ret = FALSE;
16729 }
16730 if (more_info != NULL)
16731 free (more_info);
16732 }
16733 copy_reloc:
16734 if (wrel != rel)
16735 *wrel = *rel;
16736 }
16737
16738 if (wrel != rel)
16739 {
16740 Elf_Internal_Shdr *rel_hdr;
16741 size_t deleted = rel - wrel;
16742
16743 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16744 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16745 if (rel_hdr->sh_size == 0)
16746 {
16747 /* It is too late to remove an empty reloc section. Leave
16748 one NONE reloc.
16749 ??? What is wrong with an empty section??? */
16750 rel_hdr->sh_size = rel_hdr->sh_entsize;
16751 deleted -= 1;
16752 }
16753 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16754 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16755 input_section->reloc_count -= deleted;
16756 }
16757
16758 /* If we're emitting relocations, then shortly after this function
16759 returns, reloc offsets and addends for this section will be
16760 adjusted. Worse, reloc symbol indices will be for the output
16761 file rather than the input. Save a copy of the relocs for
16762 opd_entry_value. */
16763 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
16764 {
16765 bfd_size_type amt;
16766 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16767 rel = bfd_alloc (input_bfd, amt);
16768 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16769 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
16770 if (rel == NULL)
16771 return FALSE;
16772 memcpy (rel, relocs, amt);
16773 }
16774 return ret;
16775 }
16776
16777 /* Adjust the value of any local symbols in opd sections. */
16778
16779 static int
16780 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16781 const char *name ATTRIBUTE_UNUSED,
16782 Elf_Internal_Sym *elfsym,
16783 asection *input_sec,
16784 struct elf_link_hash_entry *h)
16785 {
16786 struct _opd_sec_data *opd;
16787 long adjust;
16788 bfd_vma value;
16789
16790 if (h != NULL)
16791 return 1;
16792
16793 opd = get_opd_info (input_sec);
16794 if (opd == NULL || opd->adjust == NULL)
16795 return 1;
16796
16797 value = elfsym->st_value - input_sec->output_offset;
16798 if (!bfd_link_relocatable (info))
16799 value -= input_sec->output_section->vma;
16800
16801 adjust = opd->adjust[OPD_NDX (value)];
16802 if (adjust == -1)
16803 return 2;
16804
16805 elfsym->st_value += adjust;
16806 return 1;
16807 }
16808
16809 /* Finish up dynamic symbol handling. We set the contents of various
16810 dynamic sections here. */
16811
16812 static bfd_boolean
16813 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16814 struct bfd_link_info *info,
16815 struct elf_link_hash_entry *h,
16816 Elf_Internal_Sym *sym)
16817 {
16818 struct ppc_link_hash_table *htab;
16819 struct plt_entry *ent;
16820
16821 htab = ppc_hash_table (info);
16822 if (htab == NULL)
16823 return FALSE;
16824
16825 if (!htab->opd_abi && !h->def_regular)
16826 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16827 if (ent->plt.offset != (bfd_vma) -1)
16828 {
16829 /* Mark the symbol as undefined, rather than as
16830 defined in glink. Leave the value if there were
16831 any relocations where pointer equality matters
16832 (this is a clue for the dynamic linker, to make
16833 function pointer comparisons work between an
16834 application and shared library), otherwise set it
16835 to zero. */
16836 sym->st_shndx = SHN_UNDEF;
16837 if (!h->pointer_equality_needed)
16838 sym->st_value = 0;
16839 else if (!h->ref_regular_nonweak)
16840 {
16841 /* This breaks function pointer comparisons, but
16842 that is better than breaking tests for a NULL
16843 function pointer. */
16844 sym->st_value = 0;
16845 }
16846 break;
16847 }
16848
16849 if (h->needs_copy
16850 && (h->root.type == bfd_link_hash_defined
16851 || h->root.type == bfd_link_hash_defweak)
16852 && (h->root.u.def.section == htab->elf.sdynbss
16853 || h->root.u.def.section == htab->elf.sdynrelro))
16854 {
16855 /* This symbol needs a copy reloc. Set it up. */
16856 Elf_Internal_Rela rela;
16857 asection *srel;
16858 bfd_byte *loc;
16859
16860 if (h->dynindx == -1)
16861 abort ();
16862
16863 rela.r_offset = (h->root.u.def.value
16864 + h->root.u.def.section->output_section->vma
16865 + h->root.u.def.section->output_offset);
16866 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16867 rela.r_addend = 0;
16868 if (h->root.u.def.section == htab->elf.sdynrelro)
16869 srel = htab->elf.sreldynrelro;
16870 else
16871 srel = htab->elf.srelbss;
16872 loc = srel->contents;
16873 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16874 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16875 }
16876
16877 return TRUE;
16878 }
16879
16880 /* Used to decide how to sort relocs in an optimal manner for the
16881 dynamic linker, before writing them out. */
16882
16883 static enum elf_reloc_type_class
16884 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16885 const asection *rel_sec,
16886 const Elf_Internal_Rela *rela)
16887 {
16888 enum elf_ppc64_reloc_type r_type;
16889 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16890
16891 if (rel_sec == htab->elf.irelplt)
16892 return reloc_class_ifunc;
16893
16894 r_type = ELF64_R_TYPE (rela->r_info);
16895 switch (r_type)
16896 {
16897 case R_PPC64_RELATIVE:
16898 return reloc_class_relative;
16899 case R_PPC64_JMP_SLOT:
16900 return reloc_class_plt;
16901 case R_PPC64_COPY:
16902 return reloc_class_copy;
16903 default:
16904 return reloc_class_normal;
16905 }
16906 }
16907
16908 /* Finish up the dynamic sections. */
16909
16910 static bfd_boolean
16911 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16912 struct bfd_link_info *info)
16913 {
16914 struct ppc_link_hash_table *htab;
16915 bfd *dynobj;
16916 asection *sdyn;
16917
16918 htab = ppc_hash_table (info);
16919 if (htab == NULL)
16920 return FALSE;
16921
16922 dynobj = htab->elf.dynobj;
16923 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16924
16925 if (htab->elf.dynamic_sections_created)
16926 {
16927 Elf64_External_Dyn *dyncon, *dynconend;
16928
16929 if (sdyn == NULL || htab->elf.sgot == NULL)
16930 abort ();
16931
16932 dyncon = (Elf64_External_Dyn *) sdyn->contents;
16933 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16934 for (; dyncon < dynconend; dyncon++)
16935 {
16936 Elf_Internal_Dyn dyn;
16937 asection *s;
16938
16939 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16940
16941 switch (dyn.d_tag)
16942 {
16943 default:
16944 continue;
16945
16946 case DT_PPC64_GLINK:
16947 s = htab->glink;
16948 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16949 /* We stupidly defined DT_PPC64_GLINK to be the start
16950 of glink rather than the first entry point, which is
16951 what ld.so needs, and now have a bigger stub to
16952 support automatic multiple TOCs. */
16953 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16954 break;
16955
16956 case DT_PPC64_OPD:
16957 s = bfd_get_section_by_name (output_bfd, ".opd");
16958 if (s == NULL)
16959 continue;
16960 dyn.d_un.d_ptr = s->vma;
16961 break;
16962
16963 case DT_PPC64_OPT:
16964 if ((htab->do_multi_toc && htab->multi_toc_needed)
16965 || htab->notoc_plt)
16966 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16967 if (htab->has_plt_localentry0)
16968 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16969 break;
16970
16971 case DT_PPC64_OPDSZ:
16972 s = bfd_get_section_by_name (output_bfd, ".opd");
16973 if (s == NULL)
16974 continue;
16975 dyn.d_un.d_val = s->size;
16976 break;
16977
16978 case DT_PLTGOT:
16979 s = htab->elf.splt;
16980 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16981 break;
16982
16983 case DT_JMPREL:
16984 s = htab->elf.srelplt;
16985 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16986 break;
16987
16988 case DT_PLTRELSZ:
16989 dyn.d_un.d_val = htab->elf.srelplt->size;
16990 break;
16991
16992 case DT_TEXTREL:
16993 if (htab->local_ifunc_resolver)
16994 info->callbacks->einfo
16995 (_("%X%P: text relocations and GNU indirect "
16996 "functions will result in a segfault at runtime\n"));
16997 else if (htab->maybe_local_ifunc_resolver)
16998 info->callbacks->einfo
16999 (_("%P: warning: text relocations and GNU indirect "
17000 "functions may result in a segfault at runtime\n"));
17001 continue;
17002 }
17003
17004 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
17005 }
17006 }
17007
17008 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
17009 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
17010 {
17011 /* Fill in the first entry in the global offset table.
17012 We use it to hold the link-time TOCbase. */
17013 bfd_put_64 (output_bfd,
17014 elf_gp (output_bfd) + TOC_BASE_OFF,
17015 htab->elf.sgot->contents);
17016
17017 /* Set .got entry size. */
17018 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
17019 = 8;
17020 }
17021
17022 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
17023 && htab->elf.splt->output_section != bfd_abs_section_ptr)
17024 {
17025 /* Set .plt entry size. */
17026 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
17027 = PLT_ENTRY_SIZE (htab);
17028 }
17029
17030 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
17031 brlt ourselves if emitrelocations. */
17032 if (htab->brlt != NULL
17033 && htab->brlt->reloc_count != 0
17034 && !_bfd_elf_link_output_relocs (output_bfd,
17035 htab->brlt,
17036 elf_section_data (htab->brlt)->rela.hdr,
17037 elf_section_data (htab->brlt)->relocs,
17038 NULL))
17039 return FALSE;
17040
17041 if (htab->glink != NULL
17042 && htab->glink->reloc_count != 0
17043 && !_bfd_elf_link_output_relocs (output_bfd,
17044 htab->glink,
17045 elf_section_data (htab->glink)->rela.hdr,
17046 elf_section_data (htab->glink)->relocs,
17047 NULL))
17048 return FALSE;
17049
17050
17051 if (htab->glink_eh_frame != NULL
17052 && htab->glink_eh_frame->size != 0
17053 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
17054 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
17055 htab->glink_eh_frame,
17056 htab->glink_eh_frame->contents))
17057 return FALSE;
17058
17059 /* We need to handle writing out multiple GOT sections ourselves,
17060 since we didn't add them to DYNOBJ. We know dynobj is the first
17061 bfd. */
17062 while ((dynobj = dynobj->link.next) != NULL)
17063 {
17064 asection *s;
17065
17066 if (!is_ppc64_elf (dynobj))
17067 continue;
17068
17069 s = ppc64_elf_tdata (dynobj)->got;
17070 if (s != NULL
17071 && s->size != 0
17072 && s->output_section != bfd_abs_section_ptr
17073 && !bfd_set_section_contents (output_bfd, s->output_section,
17074 s->contents, s->output_offset,
17075 s->size))
17076 return FALSE;
17077 s = ppc64_elf_tdata (dynobj)->relgot;
17078 if (s != NULL
17079 && s->size != 0
17080 && s->output_section != bfd_abs_section_ptr
17081 && !bfd_set_section_contents (output_bfd, s->output_section,
17082 s->contents, s->output_offset,
17083 s->size))
17084 return FALSE;
17085 }
17086
17087 return TRUE;
17088 }
17089
17090 #include "elf64-target.h"
17091
17092 /* FreeBSD support */
17093
17094 #undef TARGET_LITTLE_SYM
17095 #undef TARGET_LITTLE_NAME
17096
17097 #undef TARGET_BIG_SYM
17098 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
17099 #undef TARGET_BIG_NAME
17100 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
17101
17102 #undef ELF_OSABI
17103 #define ELF_OSABI ELFOSABI_FREEBSD
17104
17105 #undef elf64_bed
17106 #define elf64_bed elf64_powerpc_fbsd_bed
17107
17108 #include "elf64-target.h"
This page took 0.599189 seconds and 4 git commands to generate.