Support PLT16 relocs against local symbols
[binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2018 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5 Largely rewritten by Alan Modra.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22
23
24 /* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
27
28 #include "sysdep.h"
29 #include <stdarg.h>
30 #include "bfd.h"
31 #include "bfdlink.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elf/ppc64.h"
35 #include "elf64-ppc.h"
36 #include "dwarf2.h"
37
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_vma opd_entry_value
57 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
58
59 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
60 #define TARGET_LITTLE_NAME "elf64-powerpcle"
61 #define TARGET_BIG_SYM powerpc_elf64_vec
62 #define TARGET_BIG_NAME "elf64-powerpc"
63 #define ELF_ARCH bfd_arch_powerpc
64 #define ELF_TARGET_ID PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE EM_PPC64
66 #define ELF_MAXPAGESIZE 0x10000
67 #define ELF_COMMONPAGESIZE 0x10000
68 #define elf_info_to_howto ppc64_elf_info_to_howto
69
70 #define elf_backend_want_got_sym 0
71 #define elf_backend_want_plt_sym 0
72 #define elf_backend_plt_alignment 3
73 #define elf_backend_plt_not_loaded 1
74 #define elf_backend_got_header_size 8
75 #define elf_backend_want_dynrelro 1
76 #define elf_backend_can_gc_sections 1
77 #define elf_backend_can_refcount 1
78 #define elf_backend_rela_normal 1
79 #define elf_backend_dtrel_excludes_plt 1
80 #define elf_backend_default_execstack 0
81
82 #define bfd_elf64_mkobject ppc64_elf_mkobject
83 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
84 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
85 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
86 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
87 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
88 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
89 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
90 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
91 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
92
93 #define elf_backend_object_p ppc64_elf_object_p
94 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
95 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
96 #define elf_backend_write_core_note ppc64_elf_write_core_note
97 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
98 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
99 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
100 #define elf_backend_check_directives ppc64_elf_before_check_relocs
101 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
102 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
103 #define elf_backend_check_relocs ppc64_elf_check_relocs
104 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
105 #define elf_backend_gc_keep ppc64_elf_gc_keep
106 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
107 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
108 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
109 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
110 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
111 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
112 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
113 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
114 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
115 #define elf_backend_action_discarded ppc64_elf_action_discarded
116 #define elf_backend_relocate_section ppc64_elf_relocate_section
117 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
118 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
119 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
120 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
121 #define elf_backend_special_sections ppc64_elf_special_sections
122 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
123 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
124 #define elf_backend_get_reloc_section bfd_get_section_by_name
125
126 /* The name of the dynamic interpreter. This is put in the .interp
127 section. */
128 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
129
130 /* The size in bytes of an entry in the procedure linkage table. */
131 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
132 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
133
134 /* The initial size of the plt reserved for the dynamic linker. */
135 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
136
137 /* Offsets to some stack save slots. */
138 #define STK_LR 16
139 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
140 /* This one is dodgy. ELFv2 does not have a linker word, so use the
141 CR save slot. Used only by optimised __tls_get_addr call stub,
142 relying on __tls_get_addr_opt not saving CR.. */
143 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
144
145 /* TOC base pointers offset from start of TOC. */
146 #define TOC_BASE_OFF 0x8000
147 /* TOC base alignment. */
148 #define TOC_BASE_ALIGN 256
149
150 /* Offset of tp and dtp pointers from start of TLS block. */
151 #define TP_OFFSET 0x7000
152 #define DTP_OFFSET 0x8000
153
154 /* .plt call stub instructions. The normal stub is like this, but
155 sometimes the .plt entry crosses a 64k boundary and we need to
156 insert an addi to adjust r11. */
157 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
158 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
159 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
160 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
161 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
162 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
163 #define BCTR 0x4e800420 /* bctr */
164
165 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
166 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
167 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
168
169 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
170 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
171 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
172 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
173 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
174 #define BNECTR 0x4ca20420 /* bnectr+ */
175 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
176
177 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
178 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
179 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
180
181 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
182 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
183 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
184
185 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
186 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
187 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
188 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
189 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
190
191 /* __glink_PLTresolve stub instructions. We enter with the index in R0. */
192 #define GLINK_PLTRESOLVE_SIZE(htab) \
193 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
194 /* 0: */
195 /* .quad plt0-1f */
196 /* __glink: */
197 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
198 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
199 /* 1: */
200 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
201 /* ld %2,(0b-1b)(%11) */
202 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
203 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
204 /* ld %12,0(%11) */
205 /* ld %2,8(%11) */
206 /* mtctr %12 */
207 /* ld %11,16(%11) */
208 /* bctr */
209 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
210 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
211 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
212 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
213 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
214
215 /* Pad with this. */
216 #define NOP 0x60000000
217
218 /* Some other nops. */
219 #define CROR_151515 0x4def7b82
220 #define CROR_313131 0x4ffffb82
221
222 /* .glink entries for the first 32k functions are two instructions. */
223 #define LI_R0_0 0x38000000 /* li %r0,0 */
224 #define B_DOT 0x48000000 /* b . */
225
226 /* After that, we need two instructions to load the index, followed by
227 a branch. */
228 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
229 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
230
231 /* Instructions used by the save and restore reg functions. */
232 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
233 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
234 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
235 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
236 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
237 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
238 #define LI_R12_0 0x39800000 /* li %r12,0 */
239 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
240 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
241 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
242 #define BLR 0x4e800020 /* blr */
243
244 /* Since .opd is an array of descriptors and each entry will end up
245 with identical R_PPC64_RELATIVE relocs, there is really no need to
246 propagate .opd relocs; The dynamic linker should be taught to
247 relocate .opd without reloc entries. */
248 #ifndef NO_OPD_RELOCS
249 #define NO_OPD_RELOCS 0
250 #endif
251
252 #ifndef ARRAY_SIZE
253 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
254 #endif
255
256 static inline int
257 abiversion (bfd *abfd)
258 {
259 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
260 }
261
262 static inline void
263 set_abiversion (bfd *abfd, int ver)
264 {
265 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
266 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
267 }
268 \f
269 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
270
271 /* Relocation HOWTO's. */
272 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
273
274 static reloc_howto_type ppc64_elf_howto_raw[] =
275 {
276 /* This reloc does nothing. */
277 HOWTO (R_PPC64_NONE, /* type */
278 0, /* rightshift */
279 3, /* size (0 = byte, 1 = short, 2 = long) */
280 0, /* bitsize */
281 FALSE, /* pc_relative */
282 0, /* bitpos */
283 complain_overflow_dont, /* complain_on_overflow */
284 bfd_elf_generic_reloc, /* special_function */
285 "R_PPC64_NONE", /* name */
286 FALSE, /* partial_inplace */
287 0, /* src_mask */
288 0, /* dst_mask */
289 FALSE), /* pcrel_offset */
290
291 /* A standard 32 bit relocation. */
292 HOWTO (R_PPC64_ADDR32, /* type */
293 0, /* rightshift */
294 2, /* size (0 = byte, 1 = short, 2 = long) */
295 32, /* bitsize */
296 FALSE, /* pc_relative */
297 0, /* bitpos */
298 complain_overflow_bitfield, /* complain_on_overflow */
299 bfd_elf_generic_reloc, /* special_function */
300 "R_PPC64_ADDR32", /* name */
301 FALSE, /* partial_inplace */
302 0, /* src_mask */
303 0xffffffff, /* dst_mask */
304 FALSE), /* pcrel_offset */
305
306 /* An absolute 26 bit branch; the lower two bits must be zero.
307 FIXME: we don't check that, we just clear them. */
308 HOWTO (R_PPC64_ADDR24, /* type */
309 0, /* rightshift */
310 2, /* size (0 = byte, 1 = short, 2 = long) */
311 26, /* bitsize */
312 FALSE, /* pc_relative */
313 0, /* bitpos */
314 complain_overflow_bitfield, /* complain_on_overflow */
315 bfd_elf_generic_reloc, /* special_function */
316 "R_PPC64_ADDR24", /* name */
317 FALSE, /* partial_inplace */
318 0, /* src_mask */
319 0x03fffffc, /* dst_mask */
320 FALSE), /* pcrel_offset */
321
322 /* A standard 16 bit relocation. */
323 HOWTO (R_PPC64_ADDR16, /* type */
324 0, /* rightshift */
325 1, /* size (0 = byte, 1 = short, 2 = long) */
326 16, /* bitsize */
327 FALSE, /* pc_relative */
328 0, /* bitpos */
329 complain_overflow_bitfield, /* complain_on_overflow */
330 bfd_elf_generic_reloc, /* special_function */
331 "R_PPC64_ADDR16", /* name */
332 FALSE, /* partial_inplace */
333 0, /* src_mask */
334 0xffff, /* dst_mask */
335 FALSE), /* pcrel_offset */
336
337 /* A 16 bit relocation without overflow. */
338 HOWTO (R_PPC64_ADDR16_LO, /* type */
339 0, /* rightshift */
340 1, /* size (0 = byte, 1 = short, 2 = long) */
341 16, /* bitsize */
342 FALSE, /* pc_relative */
343 0, /* bitpos */
344 complain_overflow_dont,/* complain_on_overflow */
345 bfd_elf_generic_reloc, /* special_function */
346 "R_PPC64_ADDR16_LO", /* name */
347 FALSE, /* partial_inplace */
348 0, /* src_mask */
349 0xffff, /* dst_mask */
350 FALSE), /* pcrel_offset */
351
352 /* Bits 16-31 of an address. */
353 HOWTO (R_PPC64_ADDR16_HI, /* type */
354 16, /* rightshift */
355 1, /* size (0 = byte, 1 = short, 2 = long) */
356 16, /* bitsize */
357 FALSE, /* pc_relative */
358 0, /* bitpos */
359 complain_overflow_signed, /* complain_on_overflow */
360 bfd_elf_generic_reloc, /* special_function */
361 "R_PPC64_ADDR16_HI", /* name */
362 FALSE, /* partial_inplace */
363 0, /* src_mask */
364 0xffff, /* dst_mask */
365 FALSE), /* pcrel_offset */
366
367 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
368 bits, treated as a signed number, is negative. */
369 HOWTO (R_PPC64_ADDR16_HA, /* type */
370 16, /* rightshift */
371 1, /* size (0 = byte, 1 = short, 2 = long) */
372 16, /* bitsize */
373 FALSE, /* pc_relative */
374 0, /* bitpos */
375 complain_overflow_signed, /* complain_on_overflow */
376 ppc64_elf_ha_reloc, /* special_function */
377 "R_PPC64_ADDR16_HA", /* name */
378 FALSE, /* partial_inplace */
379 0, /* src_mask */
380 0xffff, /* dst_mask */
381 FALSE), /* pcrel_offset */
382
383 /* An absolute 16 bit branch; the lower two bits must be zero.
384 FIXME: we don't check that, we just clear them. */
385 HOWTO (R_PPC64_ADDR14, /* type */
386 0, /* rightshift */
387 2, /* size (0 = byte, 1 = short, 2 = long) */
388 16, /* bitsize */
389 FALSE, /* pc_relative */
390 0, /* bitpos */
391 complain_overflow_signed, /* complain_on_overflow */
392 ppc64_elf_branch_reloc, /* special_function */
393 "R_PPC64_ADDR14", /* name */
394 FALSE, /* partial_inplace */
395 0, /* src_mask */
396 0x0000fffc, /* dst_mask */
397 FALSE), /* pcrel_offset */
398
399 /* An absolute 16 bit branch, for which bit 10 should be set to
400 indicate that the branch is expected to be taken. The lower two
401 bits must be zero. */
402 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
403 0, /* rightshift */
404 2, /* size (0 = byte, 1 = short, 2 = long) */
405 16, /* bitsize */
406 FALSE, /* pc_relative */
407 0, /* bitpos */
408 complain_overflow_signed, /* complain_on_overflow */
409 ppc64_elf_brtaken_reloc, /* special_function */
410 "R_PPC64_ADDR14_BRTAKEN",/* name */
411 FALSE, /* partial_inplace */
412 0, /* src_mask */
413 0x0000fffc, /* dst_mask */
414 FALSE), /* pcrel_offset */
415
416 /* An absolute 16 bit branch, for which bit 10 should be set to
417 indicate that the branch is not expected to be taken. The lower
418 two bits must be zero. */
419 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
420 0, /* rightshift */
421 2, /* size (0 = byte, 1 = short, 2 = long) */
422 16, /* bitsize */
423 FALSE, /* pc_relative */
424 0, /* bitpos */
425 complain_overflow_signed, /* complain_on_overflow */
426 ppc64_elf_brtaken_reloc, /* special_function */
427 "R_PPC64_ADDR14_BRNTAKEN",/* name */
428 FALSE, /* partial_inplace */
429 0, /* src_mask */
430 0x0000fffc, /* dst_mask */
431 FALSE), /* pcrel_offset */
432
433 /* A relative 26 bit branch; the lower two bits must be zero. */
434 HOWTO (R_PPC64_REL24, /* type */
435 0, /* rightshift */
436 2, /* size (0 = byte, 1 = short, 2 = long) */
437 26, /* bitsize */
438 TRUE, /* pc_relative */
439 0, /* bitpos */
440 complain_overflow_signed, /* complain_on_overflow */
441 ppc64_elf_branch_reloc, /* special_function */
442 "R_PPC64_REL24", /* name */
443 FALSE, /* partial_inplace */
444 0, /* src_mask */
445 0x03fffffc, /* dst_mask */
446 TRUE), /* pcrel_offset */
447
448 /* A relative 16 bit branch; the lower two bits must be zero. */
449 HOWTO (R_PPC64_REL14, /* type */
450 0, /* rightshift */
451 2, /* size (0 = byte, 1 = short, 2 = long) */
452 16, /* bitsize */
453 TRUE, /* pc_relative */
454 0, /* bitpos */
455 complain_overflow_signed, /* complain_on_overflow */
456 ppc64_elf_branch_reloc, /* special_function */
457 "R_PPC64_REL14", /* name */
458 FALSE, /* partial_inplace */
459 0, /* src_mask */
460 0x0000fffc, /* dst_mask */
461 TRUE), /* pcrel_offset */
462
463 /* A relative 16 bit branch. Bit 10 should be set to indicate that
464 the branch is expected to be taken. The lower two bits must be
465 zero. */
466 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
467 0, /* rightshift */
468 2, /* size (0 = byte, 1 = short, 2 = long) */
469 16, /* bitsize */
470 TRUE, /* pc_relative */
471 0, /* bitpos */
472 complain_overflow_signed, /* complain_on_overflow */
473 ppc64_elf_brtaken_reloc, /* special_function */
474 "R_PPC64_REL14_BRTAKEN", /* name */
475 FALSE, /* partial_inplace */
476 0, /* src_mask */
477 0x0000fffc, /* dst_mask */
478 TRUE), /* pcrel_offset */
479
480 /* A relative 16 bit branch. Bit 10 should be set to indicate that
481 the branch is not expected to be taken. The lower two bits must
482 be zero. */
483 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
484 0, /* rightshift */
485 2, /* size (0 = byte, 1 = short, 2 = long) */
486 16, /* bitsize */
487 TRUE, /* pc_relative */
488 0, /* bitpos */
489 complain_overflow_signed, /* complain_on_overflow */
490 ppc64_elf_brtaken_reloc, /* special_function */
491 "R_PPC64_REL14_BRNTAKEN",/* name */
492 FALSE, /* partial_inplace */
493 0, /* src_mask */
494 0x0000fffc, /* dst_mask */
495 TRUE), /* pcrel_offset */
496
497 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
498 symbol. */
499 HOWTO (R_PPC64_GOT16, /* type */
500 0, /* rightshift */
501 1, /* size (0 = byte, 1 = short, 2 = long) */
502 16, /* bitsize */
503 FALSE, /* pc_relative */
504 0, /* bitpos */
505 complain_overflow_signed, /* complain_on_overflow */
506 ppc64_elf_unhandled_reloc, /* special_function */
507 "R_PPC64_GOT16", /* name */
508 FALSE, /* partial_inplace */
509 0, /* src_mask */
510 0xffff, /* dst_mask */
511 FALSE), /* pcrel_offset */
512
513 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
514 the symbol. */
515 HOWTO (R_PPC64_GOT16_LO, /* type */
516 0, /* rightshift */
517 1, /* size (0 = byte, 1 = short, 2 = long) */
518 16, /* bitsize */
519 FALSE, /* pc_relative */
520 0, /* bitpos */
521 complain_overflow_dont, /* complain_on_overflow */
522 ppc64_elf_unhandled_reloc, /* special_function */
523 "R_PPC64_GOT16_LO", /* name */
524 FALSE, /* partial_inplace */
525 0, /* src_mask */
526 0xffff, /* dst_mask */
527 FALSE), /* pcrel_offset */
528
529 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
530 the symbol. */
531 HOWTO (R_PPC64_GOT16_HI, /* type */
532 16, /* rightshift */
533 1, /* size (0 = byte, 1 = short, 2 = long) */
534 16, /* bitsize */
535 FALSE, /* pc_relative */
536 0, /* bitpos */
537 complain_overflow_signed,/* complain_on_overflow */
538 ppc64_elf_unhandled_reloc, /* special_function */
539 "R_PPC64_GOT16_HI", /* name */
540 FALSE, /* partial_inplace */
541 0, /* src_mask */
542 0xffff, /* dst_mask */
543 FALSE), /* pcrel_offset */
544
545 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
546 the symbol. */
547 HOWTO (R_PPC64_GOT16_HA, /* type */
548 16, /* rightshift */
549 1, /* size (0 = byte, 1 = short, 2 = long) */
550 16, /* bitsize */
551 FALSE, /* pc_relative */
552 0, /* bitpos */
553 complain_overflow_signed,/* complain_on_overflow */
554 ppc64_elf_unhandled_reloc, /* special_function */
555 "R_PPC64_GOT16_HA", /* name */
556 FALSE, /* partial_inplace */
557 0, /* src_mask */
558 0xffff, /* dst_mask */
559 FALSE), /* pcrel_offset */
560
561 /* This is used only by the dynamic linker. The symbol should exist
562 both in the object being run and in some shared library. The
563 dynamic linker copies the data addressed by the symbol from the
564 shared library into the object, because the object being
565 run has to have the data at some particular address. */
566 HOWTO (R_PPC64_COPY, /* type */
567 0, /* rightshift */
568 0, /* this one is variable size */
569 0, /* bitsize */
570 FALSE, /* pc_relative */
571 0, /* bitpos */
572 complain_overflow_dont, /* complain_on_overflow */
573 ppc64_elf_unhandled_reloc, /* special_function */
574 "R_PPC64_COPY", /* name */
575 FALSE, /* partial_inplace */
576 0, /* src_mask */
577 0, /* dst_mask */
578 FALSE), /* pcrel_offset */
579
580 /* Like R_PPC64_ADDR64, but used when setting global offset table
581 entries. */
582 HOWTO (R_PPC64_GLOB_DAT, /* type */
583 0, /* rightshift */
584 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
585 64, /* bitsize */
586 FALSE, /* pc_relative */
587 0, /* bitpos */
588 complain_overflow_dont, /* complain_on_overflow */
589 ppc64_elf_unhandled_reloc, /* special_function */
590 "R_PPC64_GLOB_DAT", /* name */
591 FALSE, /* partial_inplace */
592 0, /* src_mask */
593 ONES (64), /* dst_mask */
594 FALSE), /* pcrel_offset */
595
596 /* Created by the link editor. Marks a procedure linkage table
597 entry for a symbol. */
598 HOWTO (R_PPC64_JMP_SLOT, /* type */
599 0, /* rightshift */
600 0, /* size (0 = byte, 1 = short, 2 = long) */
601 0, /* bitsize */
602 FALSE, /* pc_relative */
603 0, /* bitpos */
604 complain_overflow_dont, /* complain_on_overflow */
605 ppc64_elf_unhandled_reloc, /* special_function */
606 "R_PPC64_JMP_SLOT", /* name */
607 FALSE, /* partial_inplace */
608 0, /* src_mask */
609 0, /* dst_mask */
610 FALSE), /* pcrel_offset */
611
612 /* Used only by the dynamic linker. When the object is run, this
613 doubleword64 is set to the load address of the object, plus the
614 addend. */
615 HOWTO (R_PPC64_RELATIVE, /* type */
616 0, /* rightshift */
617 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
618 64, /* bitsize */
619 FALSE, /* pc_relative */
620 0, /* bitpos */
621 complain_overflow_dont, /* complain_on_overflow */
622 bfd_elf_generic_reloc, /* special_function */
623 "R_PPC64_RELATIVE", /* name */
624 FALSE, /* partial_inplace */
625 0, /* src_mask */
626 ONES (64), /* dst_mask */
627 FALSE), /* pcrel_offset */
628
629 /* Like R_PPC64_ADDR32, but may be unaligned. */
630 HOWTO (R_PPC64_UADDR32, /* type */
631 0, /* rightshift */
632 2, /* size (0 = byte, 1 = short, 2 = long) */
633 32, /* bitsize */
634 FALSE, /* pc_relative */
635 0, /* bitpos */
636 complain_overflow_bitfield, /* complain_on_overflow */
637 bfd_elf_generic_reloc, /* special_function */
638 "R_PPC64_UADDR32", /* name */
639 FALSE, /* partial_inplace */
640 0, /* src_mask */
641 0xffffffff, /* dst_mask */
642 FALSE), /* pcrel_offset */
643
644 /* Like R_PPC64_ADDR16, but may be unaligned. */
645 HOWTO (R_PPC64_UADDR16, /* type */
646 0, /* rightshift */
647 1, /* size (0 = byte, 1 = short, 2 = long) */
648 16, /* bitsize */
649 FALSE, /* pc_relative */
650 0, /* bitpos */
651 complain_overflow_bitfield, /* complain_on_overflow */
652 bfd_elf_generic_reloc, /* special_function */
653 "R_PPC64_UADDR16", /* name */
654 FALSE, /* partial_inplace */
655 0, /* src_mask */
656 0xffff, /* dst_mask */
657 FALSE), /* pcrel_offset */
658
659 /* 32-bit PC relative. */
660 HOWTO (R_PPC64_REL32, /* type */
661 0, /* rightshift */
662 2, /* size (0 = byte, 1 = short, 2 = long) */
663 32, /* bitsize */
664 TRUE, /* pc_relative */
665 0, /* bitpos */
666 complain_overflow_signed, /* complain_on_overflow */
667 bfd_elf_generic_reloc, /* special_function */
668 "R_PPC64_REL32", /* name */
669 FALSE, /* partial_inplace */
670 0, /* src_mask */
671 0xffffffff, /* dst_mask */
672 TRUE), /* pcrel_offset */
673
674 /* 32-bit relocation to the symbol's procedure linkage table. */
675 HOWTO (R_PPC64_PLT32, /* type */
676 0, /* rightshift */
677 2, /* size (0 = byte, 1 = short, 2 = long) */
678 32, /* bitsize */
679 FALSE, /* pc_relative */
680 0, /* bitpos */
681 complain_overflow_bitfield, /* complain_on_overflow */
682 ppc64_elf_unhandled_reloc, /* special_function */
683 "R_PPC64_PLT32", /* name */
684 FALSE, /* partial_inplace */
685 0, /* src_mask */
686 0xffffffff, /* dst_mask */
687 FALSE), /* pcrel_offset */
688
689 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
690 FIXME: R_PPC64_PLTREL32 not supported. */
691 HOWTO (R_PPC64_PLTREL32, /* type */
692 0, /* rightshift */
693 2, /* size (0 = byte, 1 = short, 2 = long) */
694 32, /* bitsize */
695 TRUE, /* pc_relative */
696 0, /* bitpos */
697 complain_overflow_signed, /* complain_on_overflow */
698 ppc64_elf_unhandled_reloc, /* special_function */
699 "R_PPC64_PLTREL32", /* name */
700 FALSE, /* partial_inplace */
701 0, /* src_mask */
702 0xffffffff, /* dst_mask */
703 TRUE), /* pcrel_offset */
704
705 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
706 the symbol. */
707 HOWTO (R_PPC64_PLT16_LO, /* type */
708 0, /* rightshift */
709 1, /* size (0 = byte, 1 = short, 2 = long) */
710 16, /* bitsize */
711 FALSE, /* pc_relative */
712 0, /* bitpos */
713 complain_overflow_dont, /* complain_on_overflow */
714 ppc64_elf_unhandled_reloc, /* special_function */
715 "R_PPC64_PLT16_LO", /* name */
716 FALSE, /* partial_inplace */
717 0, /* src_mask */
718 0xffff, /* dst_mask */
719 FALSE), /* pcrel_offset */
720
721 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
722 the symbol. */
723 HOWTO (R_PPC64_PLT16_HI, /* type */
724 16, /* rightshift */
725 1, /* size (0 = byte, 1 = short, 2 = long) */
726 16, /* bitsize */
727 FALSE, /* pc_relative */
728 0, /* bitpos */
729 complain_overflow_signed, /* complain_on_overflow */
730 ppc64_elf_unhandled_reloc, /* special_function */
731 "R_PPC64_PLT16_HI", /* name */
732 FALSE, /* partial_inplace */
733 0, /* src_mask */
734 0xffff, /* dst_mask */
735 FALSE), /* pcrel_offset */
736
737 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
738 the symbol. */
739 HOWTO (R_PPC64_PLT16_HA, /* type */
740 16, /* rightshift */
741 1, /* size (0 = byte, 1 = short, 2 = long) */
742 16, /* bitsize */
743 FALSE, /* pc_relative */
744 0, /* bitpos */
745 complain_overflow_signed, /* complain_on_overflow */
746 ppc64_elf_unhandled_reloc, /* special_function */
747 "R_PPC64_PLT16_HA", /* name */
748 FALSE, /* partial_inplace */
749 0, /* src_mask */
750 0xffff, /* dst_mask */
751 FALSE), /* pcrel_offset */
752
753 /* 16-bit section relative relocation. */
754 HOWTO (R_PPC64_SECTOFF, /* type */
755 0, /* rightshift */
756 1, /* size (0 = byte, 1 = short, 2 = long) */
757 16, /* bitsize */
758 FALSE, /* pc_relative */
759 0, /* bitpos */
760 complain_overflow_signed, /* complain_on_overflow */
761 ppc64_elf_sectoff_reloc, /* special_function */
762 "R_PPC64_SECTOFF", /* name */
763 FALSE, /* partial_inplace */
764 0, /* src_mask */
765 0xffff, /* dst_mask */
766 FALSE), /* pcrel_offset */
767
768 /* Like R_PPC64_SECTOFF, but no overflow warning. */
769 HOWTO (R_PPC64_SECTOFF_LO, /* type */
770 0, /* rightshift */
771 1, /* size (0 = byte, 1 = short, 2 = long) */
772 16, /* bitsize */
773 FALSE, /* pc_relative */
774 0, /* bitpos */
775 complain_overflow_dont, /* complain_on_overflow */
776 ppc64_elf_sectoff_reloc, /* special_function */
777 "R_PPC64_SECTOFF_LO", /* name */
778 FALSE, /* partial_inplace */
779 0, /* src_mask */
780 0xffff, /* dst_mask */
781 FALSE), /* pcrel_offset */
782
783 /* 16-bit upper half section relative relocation. */
784 HOWTO (R_PPC64_SECTOFF_HI, /* type */
785 16, /* rightshift */
786 1, /* size (0 = byte, 1 = short, 2 = long) */
787 16, /* bitsize */
788 FALSE, /* pc_relative */
789 0, /* bitpos */
790 complain_overflow_signed, /* complain_on_overflow */
791 ppc64_elf_sectoff_reloc, /* special_function */
792 "R_PPC64_SECTOFF_HI", /* name */
793 FALSE, /* partial_inplace */
794 0, /* src_mask */
795 0xffff, /* dst_mask */
796 FALSE), /* pcrel_offset */
797
798 /* 16-bit upper half adjusted section relative relocation. */
799 HOWTO (R_PPC64_SECTOFF_HA, /* type */
800 16, /* rightshift */
801 1, /* size (0 = byte, 1 = short, 2 = long) */
802 16, /* bitsize */
803 FALSE, /* pc_relative */
804 0, /* bitpos */
805 complain_overflow_signed, /* complain_on_overflow */
806 ppc64_elf_sectoff_ha_reloc, /* special_function */
807 "R_PPC64_SECTOFF_HA", /* name */
808 FALSE, /* partial_inplace */
809 0, /* src_mask */
810 0xffff, /* dst_mask */
811 FALSE), /* pcrel_offset */
812
813 /* Like R_PPC64_REL24 without touching the two least significant bits. */
814 HOWTO (R_PPC64_REL30, /* type */
815 2, /* rightshift */
816 2, /* size (0 = byte, 1 = short, 2 = long) */
817 30, /* bitsize */
818 TRUE, /* pc_relative */
819 0, /* bitpos */
820 complain_overflow_dont, /* complain_on_overflow */
821 bfd_elf_generic_reloc, /* special_function */
822 "R_PPC64_REL30", /* name */
823 FALSE, /* partial_inplace */
824 0, /* src_mask */
825 0xfffffffc, /* dst_mask */
826 TRUE), /* pcrel_offset */
827
828 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
829
830 /* A standard 64-bit relocation. */
831 HOWTO (R_PPC64_ADDR64, /* type */
832 0, /* rightshift */
833 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
834 64, /* bitsize */
835 FALSE, /* pc_relative */
836 0, /* bitpos */
837 complain_overflow_dont, /* complain_on_overflow */
838 bfd_elf_generic_reloc, /* special_function */
839 "R_PPC64_ADDR64", /* name */
840 FALSE, /* partial_inplace */
841 0, /* src_mask */
842 ONES (64), /* dst_mask */
843 FALSE), /* pcrel_offset */
844
845 /* The bits 32-47 of an address. */
846 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
847 32, /* rightshift */
848 1, /* size (0 = byte, 1 = short, 2 = long) */
849 16, /* bitsize */
850 FALSE, /* pc_relative */
851 0, /* bitpos */
852 complain_overflow_dont, /* complain_on_overflow */
853 bfd_elf_generic_reloc, /* special_function */
854 "R_PPC64_ADDR16_HIGHER", /* name */
855 FALSE, /* partial_inplace */
856 0, /* src_mask */
857 0xffff, /* dst_mask */
858 FALSE), /* pcrel_offset */
859
860 /* The bits 32-47 of an address, plus 1 if the contents of the low
861 16 bits, treated as a signed number, is negative. */
862 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
863 32, /* rightshift */
864 1, /* size (0 = byte, 1 = short, 2 = long) */
865 16, /* bitsize */
866 FALSE, /* pc_relative */
867 0, /* bitpos */
868 complain_overflow_dont, /* complain_on_overflow */
869 ppc64_elf_ha_reloc, /* special_function */
870 "R_PPC64_ADDR16_HIGHERA", /* name */
871 FALSE, /* partial_inplace */
872 0, /* src_mask */
873 0xffff, /* dst_mask */
874 FALSE), /* pcrel_offset */
875
876 /* The bits 48-63 of an address. */
877 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
878 48, /* rightshift */
879 1, /* size (0 = byte, 1 = short, 2 = long) */
880 16, /* bitsize */
881 FALSE, /* pc_relative */
882 0, /* bitpos */
883 complain_overflow_dont, /* complain_on_overflow */
884 bfd_elf_generic_reloc, /* special_function */
885 "R_PPC64_ADDR16_HIGHEST", /* name */
886 FALSE, /* partial_inplace */
887 0, /* src_mask */
888 0xffff, /* dst_mask */
889 FALSE), /* pcrel_offset */
890
891 /* The bits 48-63 of an address, plus 1 if the contents of the low
892 16 bits, treated as a signed number, is negative. */
893 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
894 48, /* rightshift */
895 1, /* size (0 = byte, 1 = short, 2 = long) */
896 16, /* bitsize */
897 FALSE, /* pc_relative */
898 0, /* bitpos */
899 complain_overflow_dont, /* complain_on_overflow */
900 ppc64_elf_ha_reloc, /* special_function */
901 "R_PPC64_ADDR16_HIGHESTA", /* name */
902 FALSE, /* partial_inplace */
903 0, /* src_mask */
904 0xffff, /* dst_mask */
905 FALSE), /* pcrel_offset */
906
907 /* Like ADDR64, but may be unaligned. */
908 HOWTO (R_PPC64_UADDR64, /* type */
909 0, /* rightshift */
910 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
911 64, /* bitsize */
912 FALSE, /* pc_relative */
913 0, /* bitpos */
914 complain_overflow_dont, /* complain_on_overflow */
915 bfd_elf_generic_reloc, /* special_function */
916 "R_PPC64_UADDR64", /* name */
917 FALSE, /* partial_inplace */
918 0, /* src_mask */
919 ONES (64), /* dst_mask */
920 FALSE), /* pcrel_offset */
921
922 /* 64-bit relative relocation. */
923 HOWTO (R_PPC64_REL64, /* type */
924 0, /* rightshift */
925 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
926 64, /* bitsize */
927 TRUE, /* pc_relative */
928 0, /* bitpos */
929 complain_overflow_dont, /* complain_on_overflow */
930 bfd_elf_generic_reloc, /* special_function */
931 "R_PPC64_REL64", /* name */
932 FALSE, /* partial_inplace */
933 0, /* src_mask */
934 ONES (64), /* dst_mask */
935 TRUE), /* pcrel_offset */
936
937 /* 64-bit relocation to the symbol's procedure linkage table. */
938 HOWTO (R_PPC64_PLT64, /* type */
939 0, /* rightshift */
940 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
941 64, /* bitsize */
942 FALSE, /* pc_relative */
943 0, /* bitpos */
944 complain_overflow_dont, /* complain_on_overflow */
945 ppc64_elf_unhandled_reloc, /* special_function */
946 "R_PPC64_PLT64", /* name */
947 FALSE, /* partial_inplace */
948 0, /* src_mask */
949 ONES (64), /* dst_mask */
950 FALSE), /* pcrel_offset */
951
952 /* 64-bit PC relative relocation to the symbol's procedure linkage
953 table. */
954 /* FIXME: R_PPC64_PLTREL64 not supported. */
955 HOWTO (R_PPC64_PLTREL64, /* type */
956 0, /* rightshift */
957 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
958 64, /* bitsize */
959 TRUE, /* pc_relative */
960 0, /* bitpos */
961 complain_overflow_dont, /* complain_on_overflow */
962 ppc64_elf_unhandled_reloc, /* special_function */
963 "R_PPC64_PLTREL64", /* name */
964 FALSE, /* partial_inplace */
965 0, /* src_mask */
966 ONES (64), /* dst_mask */
967 TRUE), /* pcrel_offset */
968
969 /* 16 bit TOC-relative relocation. */
970
971 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
972 HOWTO (R_PPC64_TOC16, /* type */
973 0, /* rightshift */
974 1, /* size (0 = byte, 1 = short, 2 = long) */
975 16, /* bitsize */
976 FALSE, /* pc_relative */
977 0, /* bitpos */
978 complain_overflow_signed, /* complain_on_overflow */
979 ppc64_elf_toc_reloc, /* special_function */
980 "R_PPC64_TOC16", /* name */
981 FALSE, /* partial_inplace */
982 0, /* src_mask */
983 0xffff, /* dst_mask */
984 FALSE), /* pcrel_offset */
985
986 /* 16 bit TOC-relative relocation without overflow. */
987
988 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
989 HOWTO (R_PPC64_TOC16_LO, /* type */
990 0, /* rightshift */
991 1, /* size (0 = byte, 1 = short, 2 = long) */
992 16, /* bitsize */
993 FALSE, /* pc_relative */
994 0, /* bitpos */
995 complain_overflow_dont, /* complain_on_overflow */
996 ppc64_elf_toc_reloc, /* special_function */
997 "R_PPC64_TOC16_LO", /* name */
998 FALSE, /* partial_inplace */
999 0, /* src_mask */
1000 0xffff, /* dst_mask */
1001 FALSE), /* pcrel_offset */
1002
1003 /* 16 bit TOC-relative relocation, high 16 bits. */
1004
1005 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
1006 HOWTO (R_PPC64_TOC16_HI, /* type */
1007 16, /* rightshift */
1008 1, /* size (0 = byte, 1 = short, 2 = long) */
1009 16, /* bitsize */
1010 FALSE, /* pc_relative */
1011 0, /* bitpos */
1012 complain_overflow_signed, /* complain_on_overflow */
1013 ppc64_elf_toc_reloc, /* special_function */
1014 "R_PPC64_TOC16_HI", /* name */
1015 FALSE, /* partial_inplace */
1016 0, /* src_mask */
1017 0xffff, /* dst_mask */
1018 FALSE), /* pcrel_offset */
1019
1020 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1021 contents of the low 16 bits, treated as a signed number, is
1022 negative. */
1023
1024 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1025 HOWTO (R_PPC64_TOC16_HA, /* type */
1026 16, /* rightshift */
1027 1, /* size (0 = byte, 1 = short, 2 = long) */
1028 16, /* bitsize */
1029 FALSE, /* pc_relative */
1030 0, /* bitpos */
1031 complain_overflow_signed, /* complain_on_overflow */
1032 ppc64_elf_toc_ha_reloc, /* special_function */
1033 "R_PPC64_TOC16_HA", /* name */
1034 FALSE, /* partial_inplace */
1035 0, /* src_mask */
1036 0xffff, /* dst_mask */
1037 FALSE), /* pcrel_offset */
1038
1039 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1040
1041 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1042 HOWTO (R_PPC64_TOC, /* type */
1043 0, /* rightshift */
1044 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1045 64, /* bitsize */
1046 FALSE, /* pc_relative */
1047 0, /* bitpos */
1048 complain_overflow_dont, /* complain_on_overflow */
1049 ppc64_elf_toc64_reloc, /* special_function */
1050 "R_PPC64_TOC", /* name */
1051 FALSE, /* partial_inplace */
1052 0, /* src_mask */
1053 ONES (64), /* dst_mask */
1054 FALSE), /* pcrel_offset */
1055
1056 /* Like R_PPC64_GOT16, but also informs the link editor that the
1057 value to relocate may (!) refer to a PLT entry which the link
1058 editor (a) may replace with the symbol value. If the link editor
1059 is unable to fully resolve the symbol, it may (b) create a PLT
1060 entry and store the address to the new PLT entry in the GOT.
1061 This permits lazy resolution of function symbols at run time.
1062 The link editor may also skip all of this and just (c) emit a
1063 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1064 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1065 HOWTO (R_PPC64_PLTGOT16, /* type */
1066 0, /* rightshift */
1067 1, /* size (0 = byte, 1 = short, 2 = long) */
1068 16, /* bitsize */
1069 FALSE, /* pc_relative */
1070 0, /* bitpos */
1071 complain_overflow_signed, /* complain_on_overflow */
1072 ppc64_elf_unhandled_reloc, /* special_function */
1073 "R_PPC64_PLTGOT16", /* name */
1074 FALSE, /* partial_inplace */
1075 0, /* src_mask */
1076 0xffff, /* dst_mask */
1077 FALSE), /* pcrel_offset */
1078
1079 /* Like R_PPC64_PLTGOT16, but without overflow. */
1080 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1081 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1082 0, /* rightshift */
1083 1, /* size (0 = byte, 1 = short, 2 = long) */
1084 16, /* bitsize */
1085 FALSE, /* pc_relative */
1086 0, /* bitpos */
1087 complain_overflow_dont, /* complain_on_overflow */
1088 ppc64_elf_unhandled_reloc, /* special_function */
1089 "R_PPC64_PLTGOT16_LO", /* name */
1090 FALSE, /* partial_inplace */
1091 0, /* src_mask */
1092 0xffff, /* dst_mask */
1093 FALSE), /* pcrel_offset */
1094
1095 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1096 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1097 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1098 16, /* rightshift */
1099 1, /* size (0 = byte, 1 = short, 2 = long) */
1100 16, /* bitsize */
1101 FALSE, /* pc_relative */
1102 0, /* bitpos */
1103 complain_overflow_signed, /* complain_on_overflow */
1104 ppc64_elf_unhandled_reloc, /* special_function */
1105 "R_PPC64_PLTGOT16_HI", /* name */
1106 FALSE, /* partial_inplace */
1107 0, /* src_mask */
1108 0xffff, /* dst_mask */
1109 FALSE), /* pcrel_offset */
1110
1111 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1112 1 if the contents of the low 16 bits, treated as a signed number,
1113 is negative. */
1114 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1115 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1116 16, /* rightshift */
1117 1, /* size (0 = byte, 1 = short, 2 = long) */
1118 16, /* bitsize */
1119 FALSE, /* pc_relative */
1120 0, /* bitpos */
1121 complain_overflow_signed, /* complain_on_overflow */
1122 ppc64_elf_unhandled_reloc, /* special_function */
1123 "R_PPC64_PLTGOT16_HA", /* name */
1124 FALSE, /* partial_inplace */
1125 0, /* src_mask */
1126 0xffff, /* dst_mask */
1127 FALSE), /* pcrel_offset */
1128
1129 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1130 HOWTO (R_PPC64_ADDR16_DS, /* type */
1131 0, /* rightshift */
1132 1, /* size (0 = byte, 1 = short, 2 = long) */
1133 16, /* bitsize */
1134 FALSE, /* pc_relative */
1135 0, /* bitpos */
1136 complain_overflow_signed, /* complain_on_overflow */
1137 bfd_elf_generic_reloc, /* special_function */
1138 "R_PPC64_ADDR16_DS", /* name */
1139 FALSE, /* partial_inplace */
1140 0, /* src_mask */
1141 0xfffc, /* dst_mask */
1142 FALSE), /* pcrel_offset */
1143
1144 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1145 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1146 0, /* rightshift */
1147 1, /* size (0 = byte, 1 = short, 2 = long) */
1148 16, /* bitsize */
1149 FALSE, /* pc_relative */
1150 0, /* bitpos */
1151 complain_overflow_dont,/* complain_on_overflow */
1152 bfd_elf_generic_reloc, /* special_function */
1153 "R_PPC64_ADDR16_LO_DS",/* name */
1154 FALSE, /* partial_inplace */
1155 0, /* src_mask */
1156 0xfffc, /* dst_mask */
1157 FALSE), /* pcrel_offset */
1158
1159 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1160 HOWTO (R_PPC64_GOT16_DS, /* type */
1161 0, /* rightshift */
1162 1, /* size (0 = byte, 1 = short, 2 = long) */
1163 16, /* bitsize */
1164 FALSE, /* pc_relative */
1165 0, /* bitpos */
1166 complain_overflow_signed, /* complain_on_overflow */
1167 ppc64_elf_unhandled_reloc, /* special_function */
1168 "R_PPC64_GOT16_DS", /* name */
1169 FALSE, /* partial_inplace */
1170 0, /* src_mask */
1171 0xfffc, /* dst_mask */
1172 FALSE), /* pcrel_offset */
1173
1174 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1175 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1176 0, /* rightshift */
1177 1, /* size (0 = byte, 1 = short, 2 = long) */
1178 16, /* bitsize */
1179 FALSE, /* pc_relative */
1180 0, /* bitpos */
1181 complain_overflow_dont, /* complain_on_overflow */
1182 ppc64_elf_unhandled_reloc, /* special_function */
1183 "R_PPC64_GOT16_LO_DS", /* name */
1184 FALSE, /* partial_inplace */
1185 0, /* src_mask */
1186 0xfffc, /* dst_mask */
1187 FALSE), /* pcrel_offset */
1188
1189 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1190 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1191 0, /* rightshift */
1192 1, /* size (0 = byte, 1 = short, 2 = long) */
1193 16, /* bitsize */
1194 FALSE, /* pc_relative */
1195 0, /* bitpos */
1196 complain_overflow_dont, /* complain_on_overflow */
1197 ppc64_elf_unhandled_reloc, /* special_function */
1198 "R_PPC64_PLT16_LO_DS", /* name */
1199 FALSE, /* partial_inplace */
1200 0, /* src_mask */
1201 0xfffc, /* dst_mask */
1202 FALSE), /* pcrel_offset */
1203
1204 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1205 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1206 0, /* rightshift */
1207 1, /* size (0 = byte, 1 = short, 2 = long) */
1208 16, /* bitsize */
1209 FALSE, /* pc_relative */
1210 0, /* bitpos */
1211 complain_overflow_signed, /* complain_on_overflow */
1212 ppc64_elf_sectoff_reloc, /* special_function */
1213 "R_PPC64_SECTOFF_DS", /* name */
1214 FALSE, /* partial_inplace */
1215 0, /* src_mask */
1216 0xfffc, /* dst_mask */
1217 FALSE), /* pcrel_offset */
1218
1219 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1220 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1221 0, /* rightshift */
1222 1, /* size (0 = byte, 1 = short, 2 = long) */
1223 16, /* bitsize */
1224 FALSE, /* pc_relative */
1225 0, /* bitpos */
1226 complain_overflow_dont, /* complain_on_overflow */
1227 ppc64_elf_sectoff_reloc, /* special_function */
1228 "R_PPC64_SECTOFF_LO_DS",/* name */
1229 FALSE, /* partial_inplace */
1230 0, /* src_mask */
1231 0xfffc, /* dst_mask */
1232 FALSE), /* pcrel_offset */
1233
1234 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1235 HOWTO (R_PPC64_TOC16_DS, /* type */
1236 0, /* rightshift */
1237 1, /* size (0 = byte, 1 = short, 2 = long) */
1238 16, /* bitsize */
1239 FALSE, /* pc_relative */
1240 0, /* bitpos */
1241 complain_overflow_signed, /* complain_on_overflow */
1242 ppc64_elf_toc_reloc, /* special_function */
1243 "R_PPC64_TOC16_DS", /* name */
1244 FALSE, /* partial_inplace */
1245 0, /* src_mask */
1246 0xfffc, /* dst_mask */
1247 FALSE), /* pcrel_offset */
1248
1249 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1250 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1251 0, /* rightshift */
1252 1, /* size (0 = byte, 1 = short, 2 = long) */
1253 16, /* bitsize */
1254 FALSE, /* pc_relative */
1255 0, /* bitpos */
1256 complain_overflow_dont, /* complain_on_overflow */
1257 ppc64_elf_toc_reloc, /* special_function */
1258 "R_PPC64_TOC16_LO_DS", /* name */
1259 FALSE, /* partial_inplace */
1260 0, /* src_mask */
1261 0xfffc, /* dst_mask */
1262 FALSE), /* pcrel_offset */
1263
1264 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1265 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1266 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1267 0, /* rightshift */
1268 1, /* size (0 = byte, 1 = short, 2 = long) */
1269 16, /* bitsize */
1270 FALSE, /* pc_relative */
1271 0, /* bitpos */
1272 complain_overflow_signed, /* complain_on_overflow */
1273 ppc64_elf_unhandled_reloc, /* special_function */
1274 "R_PPC64_PLTGOT16_DS", /* name */
1275 FALSE, /* partial_inplace */
1276 0, /* src_mask */
1277 0xfffc, /* dst_mask */
1278 FALSE), /* pcrel_offset */
1279
1280 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1281 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1282 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1283 0, /* rightshift */
1284 1, /* size (0 = byte, 1 = short, 2 = long) */
1285 16, /* bitsize */
1286 FALSE, /* pc_relative */
1287 0, /* bitpos */
1288 complain_overflow_dont, /* complain_on_overflow */
1289 ppc64_elf_unhandled_reloc, /* special_function */
1290 "R_PPC64_PLTGOT16_LO_DS",/* name */
1291 FALSE, /* partial_inplace */
1292 0, /* src_mask */
1293 0xfffc, /* dst_mask */
1294 FALSE), /* pcrel_offset */
1295
1296 /* Marker relocs for TLS. */
1297 HOWTO (R_PPC64_TLS,
1298 0, /* rightshift */
1299 2, /* size (0 = byte, 1 = short, 2 = long) */
1300 32, /* bitsize */
1301 FALSE, /* pc_relative */
1302 0, /* bitpos */
1303 complain_overflow_dont, /* complain_on_overflow */
1304 bfd_elf_generic_reloc, /* special_function */
1305 "R_PPC64_TLS", /* name */
1306 FALSE, /* partial_inplace */
1307 0, /* src_mask */
1308 0, /* dst_mask */
1309 FALSE), /* pcrel_offset */
1310
1311 HOWTO (R_PPC64_TLSGD,
1312 0, /* rightshift */
1313 2, /* size (0 = byte, 1 = short, 2 = long) */
1314 32, /* bitsize */
1315 FALSE, /* pc_relative */
1316 0, /* bitpos */
1317 complain_overflow_dont, /* complain_on_overflow */
1318 bfd_elf_generic_reloc, /* special_function */
1319 "R_PPC64_TLSGD", /* name */
1320 FALSE, /* partial_inplace */
1321 0, /* src_mask */
1322 0, /* dst_mask */
1323 FALSE), /* pcrel_offset */
1324
1325 HOWTO (R_PPC64_TLSLD,
1326 0, /* rightshift */
1327 2, /* size (0 = byte, 1 = short, 2 = long) */
1328 32, /* bitsize */
1329 FALSE, /* pc_relative */
1330 0, /* bitpos */
1331 complain_overflow_dont, /* complain_on_overflow */
1332 bfd_elf_generic_reloc, /* special_function */
1333 "R_PPC64_TLSLD", /* name */
1334 FALSE, /* partial_inplace */
1335 0, /* src_mask */
1336 0, /* dst_mask */
1337 FALSE), /* pcrel_offset */
1338
1339 HOWTO (R_PPC64_TOCSAVE,
1340 0, /* rightshift */
1341 2, /* size (0 = byte, 1 = short, 2 = long) */
1342 32, /* bitsize */
1343 FALSE, /* pc_relative */
1344 0, /* bitpos */
1345 complain_overflow_dont, /* complain_on_overflow */
1346 bfd_elf_generic_reloc, /* special_function */
1347 "R_PPC64_TOCSAVE", /* name */
1348 FALSE, /* partial_inplace */
1349 0, /* src_mask */
1350 0, /* dst_mask */
1351 FALSE), /* pcrel_offset */
1352
1353 /* Computes the load module index of the load module that contains the
1354 definition of its TLS sym. */
1355 HOWTO (R_PPC64_DTPMOD64,
1356 0, /* rightshift */
1357 4, /* size (0 = byte, 1 = short, 2 = long) */
1358 64, /* bitsize */
1359 FALSE, /* pc_relative */
1360 0, /* bitpos */
1361 complain_overflow_dont, /* complain_on_overflow */
1362 ppc64_elf_unhandled_reloc, /* special_function */
1363 "R_PPC64_DTPMOD64", /* name */
1364 FALSE, /* partial_inplace */
1365 0, /* src_mask */
1366 ONES (64), /* dst_mask */
1367 FALSE), /* pcrel_offset */
1368
1369 /* Computes a dtv-relative displacement, the difference between the value
1370 of sym+add and the base address of the thread-local storage block that
1371 contains the definition of sym, minus 0x8000. */
1372 HOWTO (R_PPC64_DTPREL64,
1373 0, /* rightshift */
1374 4, /* size (0 = byte, 1 = short, 2 = long) */
1375 64, /* bitsize */
1376 FALSE, /* pc_relative */
1377 0, /* bitpos */
1378 complain_overflow_dont, /* complain_on_overflow */
1379 ppc64_elf_unhandled_reloc, /* special_function */
1380 "R_PPC64_DTPREL64", /* name */
1381 FALSE, /* partial_inplace */
1382 0, /* src_mask */
1383 ONES (64), /* dst_mask */
1384 FALSE), /* pcrel_offset */
1385
1386 /* A 16 bit dtprel reloc. */
1387 HOWTO (R_PPC64_DTPREL16,
1388 0, /* rightshift */
1389 1, /* size (0 = byte, 1 = short, 2 = long) */
1390 16, /* bitsize */
1391 FALSE, /* pc_relative */
1392 0, /* bitpos */
1393 complain_overflow_signed, /* complain_on_overflow */
1394 ppc64_elf_unhandled_reloc, /* special_function */
1395 "R_PPC64_DTPREL16", /* name */
1396 FALSE, /* partial_inplace */
1397 0, /* src_mask */
1398 0xffff, /* dst_mask */
1399 FALSE), /* pcrel_offset */
1400
1401 /* Like DTPREL16, but no overflow. */
1402 HOWTO (R_PPC64_DTPREL16_LO,
1403 0, /* rightshift */
1404 1, /* size (0 = byte, 1 = short, 2 = long) */
1405 16, /* bitsize */
1406 FALSE, /* pc_relative */
1407 0, /* bitpos */
1408 complain_overflow_dont, /* complain_on_overflow */
1409 ppc64_elf_unhandled_reloc, /* special_function */
1410 "R_PPC64_DTPREL16_LO", /* name */
1411 FALSE, /* partial_inplace */
1412 0, /* src_mask */
1413 0xffff, /* dst_mask */
1414 FALSE), /* pcrel_offset */
1415
1416 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1417 HOWTO (R_PPC64_DTPREL16_HI,
1418 16, /* rightshift */
1419 1, /* size (0 = byte, 1 = short, 2 = long) */
1420 16, /* bitsize */
1421 FALSE, /* pc_relative */
1422 0, /* bitpos */
1423 complain_overflow_signed, /* complain_on_overflow */
1424 ppc64_elf_unhandled_reloc, /* special_function */
1425 "R_PPC64_DTPREL16_HI", /* name */
1426 FALSE, /* partial_inplace */
1427 0, /* src_mask */
1428 0xffff, /* dst_mask */
1429 FALSE), /* pcrel_offset */
1430
1431 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1432 HOWTO (R_PPC64_DTPREL16_HA,
1433 16, /* rightshift */
1434 1, /* size (0 = byte, 1 = short, 2 = long) */
1435 16, /* bitsize */
1436 FALSE, /* pc_relative */
1437 0, /* bitpos */
1438 complain_overflow_signed, /* complain_on_overflow */
1439 ppc64_elf_unhandled_reloc, /* special_function */
1440 "R_PPC64_DTPREL16_HA", /* name */
1441 FALSE, /* partial_inplace */
1442 0, /* src_mask */
1443 0xffff, /* dst_mask */
1444 FALSE), /* pcrel_offset */
1445
1446 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1447 HOWTO (R_PPC64_DTPREL16_HIGHER,
1448 32, /* rightshift */
1449 1, /* size (0 = byte, 1 = short, 2 = long) */
1450 16, /* bitsize */
1451 FALSE, /* pc_relative */
1452 0, /* bitpos */
1453 complain_overflow_dont, /* complain_on_overflow */
1454 ppc64_elf_unhandled_reloc, /* special_function */
1455 "R_PPC64_DTPREL16_HIGHER", /* name */
1456 FALSE, /* partial_inplace */
1457 0, /* src_mask */
1458 0xffff, /* dst_mask */
1459 FALSE), /* pcrel_offset */
1460
1461 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1462 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1463 32, /* rightshift */
1464 1, /* size (0 = byte, 1 = short, 2 = long) */
1465 16, /* bitsize */
1466 FALSE, /* pc_relative */
1467 0, /* bitpos */
1468 complain_overflow_dont, /* complain_on_overflow */
1469 ppc64_elf_unhandled_reloc, /* special_function */
1470 "R_PPC64_DTPREL16_HIGHERA", /* name */
1471 FALSE, /* partial_inplace */
1472 0, /* src_mask */
1473 0xffff, /* dst_mask */
1474 FALSE), /* pcrel_offset */
1475
1476 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1477 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1478 48, /* rightshift */
1479 1, /* size (0 = byte, 1 = short, 2 = long) */
1480 16, /* bitsize */
1481 FALSE, /* pc_relative */
1482 0, /* bitpos */
1483 complain_overflow_dont, /* complain_on_overflow */
1484 ppc64_elf_unhandled_reloc, /* special_function */
1485 "R_PPC64_DTPREL16_HIGHEST", /* name */
1486 FALSE, /* partial_inplace */
1487 0, /* src_mask */
1488 0xffff, /* dst_mask */
1489 FALSE), /* pcrel_offset */
1490
1491 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1492 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1493 48, /* rightshift */
1494 1, /* size (0 = byte, 1 = short, 2 = long) */
1495 16, /* bitsize */
1496 FALSE, /* pc_relative */
1497 0, /* bitpos */
1498 complain_overflow_dont, /* complain_on_overflow */
1499 ppc64_elf_unhandled_reloc, /* special_function */
1500 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1501 FALSE, /* partial_inplace */
1502 0, /* src_mask */
1503 0xffff, /* dst_mask */
1504 FALSE), /* pcrel_offset */
1505
1506 /* Like DTPREL16, but for insns with a DS field. */
1507 HOWTO (R_PPC64_DTPREL16_DS,
1508 0, /* rightshift */
1509 1, /* size (0 = byte, 1 = short, 2 = long) */
1510 16, /* bitsize */
1511 FALSE, /* pc_relative */
1512 0, /* bitpos */
1513 complain_overflow_signed, /* complain_on_overflow */
1514 ppc64_elf_unhandled_reloc, /* special_function */
1515 "R_PPC64_DTPREL16_DS", /* name */
1516 FALSE, /* partial_inplace */
1517 0, /* src_mask */
1518 0xfffc, /* dst_mask */
1519 FALSE), /* pcrel_offset */
1520
1521 /* Like DTPREL16_DS, but no overflow. */
1522 HOWTO (R_PPC64_DTPREL16_LO_DS,
1523 0, /* rightshift */
1524 1, /* size (0 = byte, 1 = short, 2 = long) */
1525 16, /* bitsize */
1526 FALSE, /* pc_relative */
1527 0, /* bitpos */
1528 complain_overflow_dont, /* complain_on_overflow */
1529 ppc64_elf_unhandled_reloc, /* special_function */
1530 "R_PPC64_DTPREL16_LO_DS", /* name */
1531 FALSE, /* partial_inplace */
1532 0, /* src_mask */
1533 0xfffc, /* dst_mask */
1534 FALSE), /* pcrel_offset */
1535
1536 /* Computes a tp-relative displacement, the difference between the value of
1537 sym+add and the value of the thread pointer (r13). */
1538 HOWTO (R_PPC64_TPREL64,
1539 0, /* rightshift */
1540 4, /* size (0 = byte, 1 = short, 2 = long) */
1541 64, /* bitsize */
1542 FALSE, /* pc_relative */
1543 0, /* bitpos */
1544 complain_overflow_dont, /* complain_on_overflow */
1545 ppc64_elf_unhandled_reloc, /* special_function */
1546 "R_PPC64_TPREL64", /* name */
1547 FALSE, /* partial_inplace */
1548 0, /* src_mask */
1549 ONES (64), /* dst_mask */
1550 FALSE), /* pcrel_offset */
1551
1552 /* A 16 bit tprel reloc. */
1553 HOWTO (R_PPC64_TPREL16,
1554 0, /* rightshift */
1555 1, /* size (0 = byte, 1 = short, 2 = long) */
1556 16, /* bitsize */
1557 FALSE, /* pc_relative */
1558 0, /* bitpos */
1559 complain_overflow_signed, /* complain_on_overflow */
1560 ppc64_elf_unhandled_reloc, /* special_function */
1561 "R_PPC64_TPREL16", /* name */
1562 FALSE, /* partial_inplace */
1563 0, /* src_mask */
1564 0xffff, /* dst_mask */
1565 FALSE), /* pcrel_offset */
1566
1567 /* Like TPREL16, but no overflow. */
1568 HOWTO (R_PPC64_TPREL16_LO,
1569 0, /* rightshift */
1570 1, /* size (0 = byte, 1 = short, 2 = long) */
1571 16, /* bitsize */
1572 FALSE, /* pc_relative */
1573 0, /* bitpos */
1574 complain_overflow_dont, /* complain_on_overflow */
1575 ppc64_elf_unhandled_reloc, /* special_function */
1576 "R_PPC64_TPREL16_LO", /* name */
1577 FALSE, /* partial_inplace */
1578 0, /* src_mask */
1579 0xffff, /* dst_mask */
1580 FALSE), /* pcrel_offset */
1581
1582 /* Like TPREL16_LO, but next higher group of 16 bits. */
1583 HOWTO (R_PPC64_TPREL16_HI,
1584 16, /* rightshift */
1585 1, /* size (0 = byte, 1 = short, 2 = long) */
1586 16, /* bitsize */
1587 FALSE, /* pc_relative */
1588 0, /* bitpos */
1589 complain_overflow_signed, /* complain_on_overflow */
1590 ppc64_elf_unhandled_reloc, /* special_function */
1591 "R_PPC64_TPREL16_HI", /* name */
1592 FALSE, /* partial_inplace */
1593 0, /* src_mask */
1594 0xffff, /* dst_mask */
1595 FALSE), /* pcrel_offset */
1596
1597 /* Like TPREL16_HI, but adjust for low 16 bits. */
1598 HOWTO (R_PPC64_TPREL16_HA,
1599 16, /* rightshift */
1600 1, /* size (0 = byte, 1 = short, 2 = long) */
1601 16, /* bitsize */
1602 FALSE, /* pc_relative */
1603 0, /* bitpos */
1604 complain_overflow_signed, /* complain_on_overflow */
1605 ppc64_elf_unhandled_reloc, /* special_function */
1606 "R_PPC64_TPREL16_HA", /* name */
1607 FALSE, /* partial_inplace */
1608 0, /* src_mask */
1609 0xffff, /* dst_mask */
1610 FALSE), /* pcrel_offset */
1611
1612 /* Like TPREL16_HI, but next higher group of 16 bits. */
1613 HOWTO (R_PPC64_TPREL16_HIGHER,
1614 32, /* rightshift */
1615 1, /* size (0 = byte, 1 = short, 2 = long) */
1616 16, /* bitsize */
1617 FALSE, /* pc_relative */
1618 0, /* bitpos */
1619 complain_overflow_dont, /* complain_on_overflow */
1620 ppc64_elf_unhandled_reloc, /* special_function */
1621 "R_PPC64_TPREL16_HIGHER", /* name */
1622 FALSE, /* partial_inplace */
1623 0, /* src_mask */
1624 0xffff, /* dst_mask */
1625 FALSE), /* pcrel_offset */
1626
1627 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1628 HOWTO (R_PPC64_TPREL16_HIGHERA,
1629 32, /* rightshift */
1630 1, /* size (0 = byte, 1 = short, 2 = long) */
1631 16, /* bitsize */
1632 FALSE, /* pc_relative */
1633 0, /* bitpos */
1634 complain_overflow_dont, /* complain_on_overflow */
1635 ppc64_elf_unhandled_reloc, /* special_function */
1636 "R_PPC64_TPREL16_HIGHERA", /* name */
1637 FALSE, /* partial_inplace */
1638 0, /* src_mask */
1639 0xffff, /* dst_mask */
1640 FALSE), /* pcrel_offset */
1641
1642 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1643 HOWTO (R_PPC64_TPREL16_HIGHEST,
1644 48, /* rightshift */
1645 1, /* size (0 = byte, 1 = short, 2 = long) */
1646 16, /* bitsize */
1647 FALSE, /* pc_relative */
1648 0, /* bitpos */
1649 complain_overflow_dont, /* complain_on_overflow */
1650 ppc64_elf_unhandled_reloc, /* special_function */
1651 "R_PPC64_TPREL16_HIGHEST", /* name */
1652 FALSE, /* partial_inplace */
1653 0, /* src_mask */
1654 0xffff, /* dst_mask */
1655 FALSE), /* pcrel_offset */
1656
1657 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1658 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1659 48, /* rightshift */
1660 1, /* size (0 = byte, 1 = short, 2 = long) */
1661 16, /* bitsize */
1662 FALSE, /* pc_relative */
1663 0, /* bitpos */
1664 complain_overflow_dont, /* complain_on_overflow */
1665 ppc64_elf_unhandled_reloc, /* special_function */
1666 "R_PPC64_TPREL16_HIGHESTA", /* name */
1667 FALSE, /* partial_inplace */
1668 0, /* src_mask */
1669 0xffff, /* dst_mask */
1670 FALSE), /* pcrel_offset */
1671
1672 /* Like TPREL16, but for insns with a DS field. */
1673 HOWTO (R_PPC64_TPREL16_DS,
1674 0, /* rightshift */
1675 1, /* size (0 = byte, 1 = short, 2 = long) */
1676 16, /* bitsize */
1677 FALSE, /* pc_relative */
1678 0, /* bitpos */
1679 complain_overflow_signed, /* complain_on_overflow */
1680 ppc64_elf_unhandled_reloc, /* special_function */
1681 "R_PPC64_TPREL16_DS", /* name */
1682 FALSE, /* partial_inplace */
1683 0, /* src_mask */
1684 0xfffc, /* dst_mask */
1685 FALSE), /* pcrel_offset */
1686
1687 /* Like TPREL16_DS, but no overflow. */
1688 HOWTO (R_PPC64_TPREL16_LO_DS,
1689 0, /* rightshift */
1690 1, /* size (0 = byte, 1 = short, 2 = long) */
1691 16, /* bitsize */
1692 FALSE, /* pc_relative */
1693 0, /* bitpos */
1694 complain_overflow_dont, /* complain_on_overflow */
1695 ppc64_elf_unhandled_reloc, /* special_function */
1696 "R_PPC64_TPREL16_LO_DS", /* name */
1697 FALSE, /* partial_inplace */
1698 0, /* src_mask */
1699 0xfffc, /* dst_mask */
1700 FALSE), /* pcrel_offset */
1701
1702 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1703 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1704 to the first entry relative to the TOC base (r2). */
1705 HOWTO (R_PPC64_GOT_TLSGD16,
1706 0, /* rightshift */
1707 1, /* size (0 = byte, 1 = short, 2 = long) */
1708 16, /* bitsize */
1709 FALSE, /* pc_relative */
1710 0, /* bitpos */
1711 complain_overflow_signed, /* complain_on_overflow */
1712 ppc64_elf_unhandled_reloc, /* special_function */
1713 "R_PPC64_GOT_TLSGD16", /* name */
1714 FALSE, /* partial_inplace */
1715 0, /* src_mask */
1716 0xffff, /* dst_mask */
1717 FALSE), /* pcrel_offset */
1718
1719 /* Like GOT_TLSGD16, but no overflow. */
1720 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1721 0, /* rightshift */
1722 1, /* size (0 = byte, 1 = short, 2 = long) */
1723 16, /* bitsize */
1724 FALSE, /* pc_relative */
1725 0, /* bitpos */
1726 complain_overflow_dont, /* complain_on_overflow */
1727 ppc64_elf_unhandled_reloc, /* special_function */
1728 "R_PPC64_GOT_TLSGD16_LO", /* name */
1729 FALSE, /* partial_inplace */
1730 0, /* src_mask */
1731 0xffff, /* dst_mask */
1732 FALSE), /* pcrel_offset */
1733
1734 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1735 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1736 16, /* rightshift */
1737 1, /* size (0 = byte, 1 = short, 2 = long) */
1738 16, /* bitsize */
1739 FALSE, /* pc_relative */
1740 0, /* bitpos */
1741 complain_overflow_signed, /* complain_on_overflow */
1742 ppc64_elf_unhandled_reloc, /* special_function */
1743 "R_PPC64_GOT_TLSGD16_HI", /* name */
1744 FALSE, /* partial_inplace */
1745 0, /* src_mask */
1746 0xffff, /* dst_mask */
1747 FALSE), /* pcrel_offset */
1748
1749 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1750 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1751 16, /* rightshift */
1752 1, /* size (0 = byte, 1 = short, 2 = long) */
1753 16, /* bitsize */
1754 FALSE, /* pc_relative */
1755 0, /* bitpos */
1756 complain_overflow_signed, /* complain_on_overflow */
1757 ppc64_elf_unhandled_reloc, /* special_function */
1758 "R_PPC64_GOT_TLSGD16_HA", /* name */
1759 FALSE, /* partial_inplace */
1760 0, /* src_mask */
1761 0xffff, /* dst_mask */
1762 FALSE), /* pcrel_offset */
1763
1764 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1765 with values (sym+add)@dtpmod and zero, and computes the offset to the
1766 first entry relative to the TOC base (r2). */
1767 HOWTO (R_PPC64_GOT_TLSLD16,
1768 0, /* rightshift */
1769 1, /* size (0 = byte, 1 = short, 2 = long) */
1770 16, /* bitsize */
1771 FALSE, /* pc_relative */
1772 0, /* bitpos */
1773 complain_overflow_signed, /* complain_on_overflow */
1774 ppc64_elf_unhandled_reloc, /* special_function */
1775 "R_PPC64_GOT_TLSLD16", /* name */
1776 FALSE, /* partial_inplace */
1777 0, /* src_mask */
1778 0xffff, /* dst_mask */
1779 FALSE), /* pcrel_offset */
1780
1781 /* Like GOT_TLSLD16, but no overflow. */
1782 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1783 0, /* rightshift */
1784 1, /* size (0 = byte, 1 = short, 2 = long) */
1785 16, /* bitsize */
1786 FALSE, /* pc_relative */
1787 0, /* bitpos */
1788 complain_overflow_dont, /* complain_on_overflow */
1789 ppc64_elf_unhandled_reloc, /* special_function */
1790 "R_PPC64_GOT_TLSLD16_LO", /* name */
1791 FALSE, /* partial_inplace */
1792 0, /* src_mask */
1793 0xffff, /* dst_mask */
1794 FALSE), /* pcrel_offset */
1795
1796 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1797 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1798 16, /* rightshift */
1799 1, /* size (0 = byte, 1 = short, 2 = long) */
1800 16, /* bitsize */
1801 FALSE, /* pc_relative */
1802 0, /* bitpos */
1803 complain_overflow_signed, /* complain_on_overflow */
1804 ppc64_elf_unhandled_reloc, /* special_function */
1805 "R_PPC64_GOT_TLSLD16_HI", /* name */
1806 FALSE, /* partial_inplace */
1807 0, /* src_mask */
1808 0xffff, /* dst_mask */
1809 FALSE), /* pcrel_offset */
1810
1811 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1812 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1813 16, /* rightshift */
1814 1, /* size (0 = byte, 1 = short, 2 = long) */
1815 16, /* bitsize */
1816 FALSE, /* pc_relative */
1817 0, /* bitpos */
1818 complain_overflow_signed, /* complain_on_overflow */
1819 ppc64_elf_unhandled_reloc, /* special_function */
1820 "R_PPC64_GOT_TLSLD16_HA", /* name */
1821 FALSE, /* partial_inplace */
1822 0, /* src_mask */
1823 0xffff, /* dst_mask */
1824 FALSE), /* pcrel_offset */
1825
1826 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1827 the offset to the entry relative to the TOC base (r2). */
1828 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1829 0, /* rightshift */
1830 1, /* size (0 = byte, 1 = short, 2 = long) */
1831 16, /* bitsize */
1832 FALSE, /* pc_relative */
1833 0, /* bitpos */
1834 complain_overflow_signed, /* complain_on_overflow */
1835 ppc64_elf_unhandled_reloc, /* special_function */
1836 "R_PPC64_GOT_DTPREL16_DS", /* name */
1837 FALSE, /* partial_inplace */
1838 0, /* src_mask */
1839 0xfffc, /* dst_mask */
1840 FALSE), /* pcrel_offset */
1841
1842 /* Like GOT_DTPREL16_DS, but no overflow. */
1843 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1844 0, /* rightshift */
1845 1, /* size (0 = byte, 1 = short, 2 = long) */
1846 16, /* bitsize */
1847 FALSE, /* pc_relative */
1848 0, /* bitpos */
1849 complain_overflow_dont, /* complain_on_overflow */
1850 ppc64_elf_unhandled_reloc, /* special_function */
1851 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1852 FALSE, /* partial_inplace */
1853 0, /* src_mask */
1854 0xfffc, /* dst_mask */
1855 FALSE), /* pcrel_offset */
1856
1857 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1858 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1859 16, /* rightshift */
1860 1, /* size (0 = byte, 1 = short, 2 = long) */
1861 16, /* bitsize */
1862 FALSE, /* pc_relative */
1863 0, /* bitpos */
1864 complain_overflow_signed, /* complain_on_overflow */
1865 ppc64_elf_unhandled_reloc, /* special_function */
1866 "R_PPC64_GOT_DTPREL16_HI", /* name */
1867 FALSE, /* partial_inplace */
1868 0, /* src_mask */
1869 0xffff, /* dst_mask */
1870 FALSE), /* pcrel_offset */
1871
1872 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1873 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1874 16, /* rightshift */
1875 1, /* size (0 = byte, 1 = short, 2 = long) */
1876 16, /* bitsize */
1877 FALSE, /* pc_relative */
1878 0, /* bitpos */
1879 complain_overflow_signed, /* complain_on_overflow */
1880 ppc64_elf_unhandled_reloc, /* special_function */
1881 "R_PPC64_GOT_DTPREL16_HA", /* name */
1882 FALSE, /* partial_inplace */
1883 0, /* src_mask */
1884 0xffff, /* dst_mask */
1885 FALSE), /* pcrel_offset */
1886
1887 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1888 offset to the entry relative to the TOC base (r2). */
1889 HOWTO (R_PPC64_GOT_TPREL16_DS,
1890 0, /* rightshift */
1891 1, /* size (0 = byte, 1 = short, 2 = long) */
1892 16, /* bitsize */
1893 FALSE, /* pc_relative */
1894 0, /* bitpos */
1895 complain_overflow_signed, /* complain_on_overflow */
1896 ppc64_elf_unhandled_reloc, /* special_function */
1897 "R_PPC64_GOT_TPREL16_DS", /* name */
1898 FALSE, /* partial_inplace */
1899 0, /* src_mask */
1900 0xfffc, /* dst_mask */
1901 FALSE), /* pcrel_offset */
1902
1903 /* Like GOT_TPREL16_DS, but no overflow. */
1904 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1905 0, /* rightshift */
1906 1, /* size (0 = byte, 1 = short, 2 = long) */
1907 16, /* bitsize */
1908 FALSE, /* pc_relative */
1909 0, /* bitpos */
1910 complain_overflow_dont, /* complain_on_overflow */
1911 ppc64_elf_unhandled_reloc, /* special_function */
1912 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1913 FALSE, /* partial_inplace */
1914 0, /* src_mask */
1915 0xfffc, /* dst_mask */
1916 FALSE), /* pcrel_offset */
1917
1918 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1919 HOWTO (R_PPC64_GOT_TPREL16_HI,
1920 16, /* rightshift */
1921 1, /* size (0 = byte, 1 = short, 2 = long) */
1922 16, /* bitsize */
1923 FALSE, /* pc_relative */
1924 0, /* bitpos */
1925 complain_overflow_signed, /* complain_on_overflow */
1926 ppc64_elf_unhandled_reloc, /* special_function */
1927 "R_PPC64_GOT_TPREL16_HI", /* name */
1928 FALSE, /* partial_inplace */
1929 0, /* src_mask */
1930 0xffff, /* dst_mask */
1931 FALSE), /* pcrel_offset */
1932
1933 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1934 HOWTO (R_PPC64_GOT_TPREL16_HA,
1935 16, /* rightshift */
1936 1, /* size (0 = byte, 1 = short, 2 = long) */
1937 16, /* bitsize */
1938 FALSE, /* pc_relative */
1939 0, /* bitpos */
1940 complain_overflow_signed, /* complain_on_overflow */
1941 ppc64_elf_unhandled_reloc, /* special_function */
1942 "R_PPC64_GOT_TPREL16_HA", /* name */
1943 FALSE, /* partial_inplace */
1944 0, /* src_mask */
1945 0xffff, /* dst_mask */
1946 FALSE), /* pcrel_offset */
1947
1948 HOWTO (R_PPC64_JMP_IREL, /* type */
1949 0, /* rightshift */
1950 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1951 0, /* bitsize */
1952 FALSE, /* pc_relative */
1953 0, /* bitpos */
1954 complain_overflow_dont, /* complain_on_overflow */
1955 ppc64_elf_unhandled_reloc, /* special_function */
1956 "R_PPC64_JMP_IREL", /* name */
1957 FALSE, /* partial_inplace */
1958 0, /* src_mask */
1959 0, /* dst_mask */
1960 FALSE), /* pcrel_offset */
1961
1962 HOWTO (R_PPC64_IRELATIVE, /* type */
1963 0, /* rightshift */
1964 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1965 64, /* bitsize */
1966 FALSE, /* pc_relative */
1967 0, /* bitpos */
1968 complain_overflow_dont, /* complain_on_overflow */
1969 bfd_elf_generic_reloc, /* special_function */
1970 "R_PPC64_IRELATIVE", /* name */
1971 FALSE, /* partial_inplace */
1972 0, /* src_mask */
1973 ONES (64), /* dst_mask */
1974 FALSE), /* pcrel_offset */
1975
1976 /* A 16 bit relative relocation. */
1977 HOWTO (R_PPC64_REL16, /* type */
1978 0, /* rightshift */
1979 1, /* size (0 = byte, 1 = short, 2 = long) */
1980 16, /* bitsize */
1981 TRUE, /* pc_relative */
1982 0, /* bitpos */
1983 complain_overflow_signed, /* complain_on_overflow */
1984 bfd_elf_generic_reloc, /* special_function */
1985 "R_PPC64_REL16", /* name */
1986 FALSE, /* partial_inplace */
1987 0, /* src_mask */
1988 0xffff, /* dst_mask */
1989 TRUE), /* pcrel_offset */
1990
1991 /* A 16 bit relative relocation without overflow. */
1992 HOWTO (R_PPC64_REL16_LO, /* type */
1993 0, /* rightshift */
1994 1, /* size (0 = byte, 1 = short, 2 = long) */
1995 16, /* bitsize */
1996 TRUE, /* pc_relative */
1997 0, /* bitpos */
1998 complain_overflow_dont,/* complain_on_overflow */
1999 bfd_elf_generic_reloc, /* special_function */
2000 "R_PPC64_REL16_LO", /* name */
2001 FALSE, /* partial_inplace */
2002 0, /* src_mask */
2003 0xffff, /* dst_mask */
2004 TRUE), /* pcrel_offset */
2005
2006 /* The high order 16 bits of a relative address. */
2007 HOWTO (R_PPC64_REL16_HI, /* type */
2008 16, /* rightshift */
2009 1, /* size (0 = byte, 1 = short, 2 = long) */
2010 16, /* bitsize */
2011 TRUE, /* pc_relative */
2012 0, /* bitpos */
2013 complain_overflow_signed, /* complain_on_overflow */
2014 bfd_elf_generic_reloc, /* special_function */
2015 "R_PPC64_REL16_HI", /* name */
2016 FALSE, /* partial_inplace */
2017 0, /* src_mask */
2018 0xffff, /* dst_mask */
2019 TRUE), /* pcrel_offset */
2020
2021 /* The high order 16 bits of a relative address, plus 1 if the contents of
2022 the low 16 bits, treated as a signed number, is negative. */
2023 HOWTO (R_PPC64_REL16_HA, /* type */
2024 16, /* rightshift */
2025 1, /* size (0 = byte, 1 = short, 2 = long) */
2026 16, /* bitsize */
2027 TRUE, /* pc_relative */
2028 0, /* bitpos */
2029 complain_overflow_signed, /* complain_on_overflow */
2030 ppc64_elf_ha_reloc, /* special_function */
2031 "R_PPC64_REL16_HA", /* name */
2032 FALSE, /* partial_inplace */
2033 0, /* src_mask */
2034 0xffff, /* dst_mask */
2035 TRUE), /* pcrel_offset */
2036
2037 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2038 HOWTO (R_PPC64_REL16DX_HA, /* type */
2039 16, /* rightshift */
2040 2, /* size (0 = byte, 1 = short, 2 = long) */
2041 16, /* bitsize */
2042 TRUE, /* pc_relative */
2043 0, /* bitpos */
2044 complain_overflow_signed, /* complain_on_overflow */
2045 ppc64_elf_ha_reloc, /* special_function */
2046 "R_PPC64_REL16DX_HA", /* name */
2047 FALSE, /* partial_inplace */
2048 0, /* src_mask */
2049 0x1fffc1, /* dst_mask */
2050 TRUE), /* pcrel_offset */
2051
2052 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
2053 HOWTO (R_PPC64_16DX_HA, /* type */
2054 16, /* rightshift */
2055 2, /* size (0 = byte, 1 = short, 2 = long) */
2056 16, /* bitsize */
2057 FALSE, /* pc_relative */
2058 0, /* bitpos */
2059 complain_overflow_signed, /* complain_on_overflow */
2060 ppc64_elf_ha_reloc, /* special_function */
2061 "R_PPC64_16DX_HA", /* name */
2062 FALSE, /* partial_inplace */
2063 0, /* src_mask */
2064 0x1fffc1, /* dst_mask */
2065 FALSE), /* pcrel_offset */
2066
2067 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2068 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2069 16, /* rightshift */
2070 1, /* size (0 = byte, 1 = short, 2 = long) */
2071 16, /* bitsize */
2072 FALSE, /* pc_relative */
2073 0, /* bitpos */
2074 complain_overflow_dont, /* complain_on_overflow */
2075 bfd_elf_generic_reloc, /* special_function */
2076 "R_PPC64_ADDR16_HIGH", /* name */
2077 FALSE, /* partial_inplace */
2078 0, /* src_mask */
2079 0xffff, /* dst_mask */
2080 FALSE), /* pcrel_offset */
2081
2082 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2083 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2084 16, /* rightshift */
2085 1, /* size (0 = byte, 1 = short, 2 = long) */
2086 16, /* bitsize */
2087 FALSE, /* pc_relative */
2088 0, /* bitpos */
2089 complain_overflow_dont, /* complain_on_overflow */
2090 ppc64_elf_ha_reloc, /* special_function */
2091 "R_PPC64_ADDR16_HIGHA", /* name */
2092 FALSE, /* partial_inplace */
2093 0, /* src_mask */
2094 0xffff, /* dst_mask */
2095 FALSE), /* pcrel_offset */
2096
2097 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2098 HOWTO (R_PPC64_DTPREL16_HIGH,
2099 16, /* rightshift */
2100 1, /* size (0 = byte, 1 = short, 2 = long) */
2101 16, /* bitsize */
2102 FALSE, /* pc_relative */
2103 0, /* bitpos */
2104 complain_overflow_dont, /* complain_on_overflow */
2105 ppc64_elf_unhandled_reloc, /* special_function */
2106 "R_PPC64_DTPREL16_HIGH", /* name */
2107 FALSE, /* partial_inplace */
2108 0, /* src_mask */
2109 0xffff, /* dst_mask */
2110 FALSE), /* pcrel_offset */
2111
2112 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2113 HOWTO (R_PPC64_DTPREL16_HIGHA,
2114 16, /* rightshift */
2115 1, /* size (0 = byte, 1 = short, 2 = long) */
2116 16, /* bitsize */
2117 FALSE, /* pc_relative */
2118 0, /* bitpos */
2119 complain_overflow_dont, /* complain_on_overflow */
2120 ppc64_elf_unhandled_reloc, /* special_function */
2121 "R_PPC64_DTPREL16_HIGHA", /* name */
2122 FALSE, /* partial_inplace */
2123 0, /* src_mask */
2124 0xffff, /* dst_mask */
2125 FALSE), /* pcrel_offset */
2126
2127 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2128 HOWTO (R_PPC64_TPREL16_HIGH,
2129 16, /* rightshift */
2130 1, /* size (0 = byte, 1 = short, 2 = long) */
2131 16, /* bitsize */
2132 FALSE, /* pc_relative */
2133 0, /* bitpos */
2134 complain_overflow_dont, /* complain_on_overflow */
2135 ppc64_elf_unhandled_reloc, /* special_function */
2136 "R_PPC64_TPREL16_HIGH", /* name */
2137 FALSE, /* partial_inplace */
2138 0, /* src_mask */
2139 0xffff, /* dst_mask */
2140 FALSE), /* pcrel_offset */
2141
2142 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2143 HOWTO (R_PPC64_TPREL16_HIGHA,
2144 16, /* rightshift */
2145 1, /* size (0 = byte, 1 = short, 2 = long) */
2146 16, /* bitsize */
2147 FALSE, /* pc_relative */
2148 0, /* bitpos */
2149 complain_overflow_dont, /* complain_on_overflow */
2150 ppc64_elf_unhandled_reloc, /* special_function */
2151 "R_PPC64_TPREL16_HIGHA", /* name */
2152 FALSE, /* partial_inplace */
2153 0, /* src_mask */
2154 0xffff, /* dst_mask */
2155 FALSE), /* pcrel_offset */
2156
2157 /* Marker reloc on ELFv2 large-model function entry. */
2158 HOWTO (R_PPC64_ENTRY,
2159 0, /* rightshift */
2160 2, /* size (0 = byte, 1 = short, 2 = long) */
2161 32, /* bitsize */
2162 FALSE, /* pc_relative */
2163 0, /* bitpos */
2164 complain_overflow_dont, /* complain_on_overflow */
2165 bfd_elf_generic_reloc, /* special_function */
2166 "R_PPC64_ENTRY", /* name */
2167 FALSE, /* partial_inplace */
2168 0, /* src_mask */
2169 0, /* dst_mask */
2170 FALSE), /* pcrel_offset */
2171
2172 /* Like ADDR64, but use local entry point of function. */
2173 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2174 0, /* rightshift */
2175 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2176 64, /* bitsize */
2177 FALSE, /* pc_relative */
2178 0, /* bitpos */
2179 complain_overflow_dont, /* complain_on_overflow */
2180 bfd_elf_generic_reloc, /* special_function */
2181 "R_PPC64_ADDR64_LOCAL", /* name */
2182 FALSE, /* partial_inplace */
2183 0, /* src_mask */
2184 ONES (64), /* dst_mask */
2185 FALSE), /* pcrel_offset */
2186
2187 /* GNU extension to record C++ vtable hierarchy. */
2188 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2189 0, /* rightshift */
2190 0, /* size (0 = byte, 1 = short, 2 = long) */
2191 0, /* bitsize */
2192 FALSE, /* pc_relative */
2193 0, /* bitpos */
2194 complain_overflow_dont, /* complain_on_overflow */
2195 NULL, /* special_function */
2196 "R_PPC64_GNU_VTINHERIT", /* name */
2197 FALSE, /* partial_inplace */
2198 0, /* src_mask */
2199 0, /* dst_mask */
2200 FALSE), /* pcrel_offset */
2201
2202 /* GNU extension to record C++ vtable member usage. */
2203 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2204 0, /* rightshift */
2205 0, /* size (0 = byte, 1 = short, 2 = long) */
2206 0, /* bitsize */
2207 FALSE, /* pc_relative */
2208 0, /* bitpos */
2209 complain_overflow_dont, /* complain_on_overflow */
2210 NULL, /* special_function */
2211 "R_PPC64_GNU_VTENTRY", /* name */
2212 FALSE, /* partial_inplace */
2213 0, /* src_mask */
2214 0, /* dst_mask */
2215 FALSE), /* pcrel_offset */
2216 };
2217
2218 \f
2219 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2220 be done. */
2221
2222 static void
2223 ppc_howto_init (void)
2224 {
2225 unsigned int i, type;
2226
2227 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2228 {
2229 type = ppc64_elf_howto_raw[i].type;
2230 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2231 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2232 }
2233 }
2234
2235 static reloc_howto_type *
2236 ppc64_elf_reloc_type_lookup (bfd *abfd,
2237 bfd_reloc_code_real_type code)
2238 {
2239 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2240
2241 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2242 /* Initialize howto table if needed. */
2243 ppc_howto_init ();
2244
2245 switch (code)
2246 {
2247 default:
2248 /* xgettext:c-format */
2249 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, (int) code);
2250 bfd_set_error (bfd_error_bad_value);
2251 return NULL;
2252
2253 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2254 break;
2255 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2256 break;
2257 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2258 break;
2259 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2260 break;
2261 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2262 break;
2263 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2264 break;
2265 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2266 break;
2267 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2268 break;
2269 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2270 break;
2271 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2272 break;
2273 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2274 break;
2275 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2276 break;
2277 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2278 break;
2279 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2280 break;
2281 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2282 break;
2283 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2284 break;
2285 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2286 break;
2287 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2288 break;
2289 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2290 break;
2291 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2292 break;
2293 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2294 break;
2295 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2296 break;
2297 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2298 break;
2299 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2300 break;
2301 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2302 break;
2303 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2304 break;
2305 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2306 break;
2307 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2308 break;
2309 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2310 break;
2311 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2312 break;
2313 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2314 break;
2315 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2316 break;
2317 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2318 break;
2319 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2320 break;
2321 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2322 break;
2323 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2324 break;
2325 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2326 break;
2327 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2328 break;
2329 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2330 break;
2331 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2332 break;
2333 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2334 break;
2335 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2336 break;
2337 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2338 break;
2339 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2340 break;
2341 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2342 break;
2343 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2344 break;
2345 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2346 break;
2347 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2348 break;
2349 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2350 break;
2351 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2352 break;
2353 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2354 break;
2355 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2356 break;
2357 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2358 break;
2359 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2360 break;
2361 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2362 break;
2363 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2364 break;
2365 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2366 break;
2367 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2368 break;
2369 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2370 break;
2371 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2372 break;
2373 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2374 break;
2375 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2376 break;
2377 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2378 break;
2379 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2380 break;
2381 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2382 break;
2383 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2384 break;
2385 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2386 break;
2387 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2388 break;
2389 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2390 break;
2391 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2392 break;
2393 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2394 break;
2395 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2396 break;
2397 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2398 break;
2399 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2400 break;
2401 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2402 break;
2403 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2404 break;
2405 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2406 break;
2407 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2408 break;
2409 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2410 break;
2411 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2412 break;
2413 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2414 break;
2415 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2416 break;
2417 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2418 break;
2419 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2420 break;
2421 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2422 break;
2423 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2424 break;
2425 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2426 break;
2427 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2428 break;
2429 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2430 break;
2431 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2432 break;
2433 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2434 break;
2435 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2436 break;
2437 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2438 break;
2439 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2440 break;
2441 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2442 break;
2443 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2444 break;
2445 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2446 break;
2447 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2448 break;
2449 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2450 break;
2451 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2452 break;
2453 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2454 break;
2455 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2456 break;
2457 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2458 break;
2459 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2460 break;
2461 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2462 break;
2463 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2464 break;
2465 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2466 break;
2467 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2468 break;
2469 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2470 break;
2471 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2472 break;
2473 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2474 break;
2475 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2476 break;
2477 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2478 break;
2479 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2480 break;
2481 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2482 break;
2483 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2484 break;
2485 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2486 break;
2487 }
2488
2489 return ppc64_elf_howto_table[r];
2490 };
2491
2492 static reloc_howto_type *
2493 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2494 const char *r_name)
2495 {
2496 unsigned int i;
2497
2498 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2499 if (ppc64_elf_howto_raw[i].name != NULL
2500 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2501 return &ppc64_elf_howto_raw[i];
2502
2503
2504 return NULL;
2505 }
2506
2507 /* Set the howto pointer for a PowerPC ELF reloc. */
2508
2509 static bfd_boolean
2510 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
2511 Elf_Internal_Rela *dst)
2512 {
2513 unsigned int type;
2514
2515 /* Initialize howto table if needed. */
2516 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2517 ppc_howto_init ();
2518
2519 type = ELF64_R_TYPE (dst->r_info);
2520 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2521 {
2522 /* xgettext:c-format */
2523 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2524 abfd, type);
2525 bfd_set_error (bfd_error_bad_value);
2526 return FALSE;
2527 }
2528 cache_ptr->howto = ppc64_elf_howto_table[type];
2529 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
2530 {
2531 /* xgettext:c-format */
2532 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2533 abfd, type);
2534 bfd_set_error (bfd_error_bad_value);
2535 return FALSE;
2536 }
2537
2538 return TRUE;
2539 }
2540
2541 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2542
2543 static bfd_reloc_status_type
2544 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2545 void *data, asection *input_section,
2546 bfd *output_bfd, char **error_message)
2547 {
2548 enum elf_ppc64_reloc_type r_type;
2549 long insn;
2550 bfd_size_type octets;
2551 bfd_vma value;
2552
2553 /* If this is a relocatable link (output_bfd test tells us), just
2554 call the generic function. Any adjustment will be done at final
2555 link time. */
2556 if (output_bfd != NULL)
2557 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2558 input_section, output_bfd, error_message);
2559
2560 /* Adjust the addend for sign extension of the low 16 bits.
2561 We won't actually be using the low 16 bits, so trashing them
2562 doesn't matter. */
2563 reloc_entry->addend += 0x8000;
2564 r_type = reloc_entry->howto->type;
2565 if (r_type != R_PPC64_REL16DX_HA)
2566 return bfd_reloc_continue;
2567
2568 value = 0;
2569 if (!bfd_is_com_section (symbol->section))
2570 value = symbol->value;
2571 value += (reloc_entry->addend
2572 + symbol->section->output_offset
2573 + symbol->section->output_section->vma);
2574 value -= (reloc_entry->address
2575 + input_section->output_offset
2576 + input_section->output_section->vma);
2577 value = (bfd_signed_vma) value >> 16;
2578
2579 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2580 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2581 insn &= ~0x1fffc1;
2582 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2583 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2584 if (value + 0x8000 > 0xffff)
2585 return bfd_reloc_overflow;
2586 return bfd_reloc_ok;
2587 }
2588
2589 static bfd_reloc_status_type
2590 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2591 void *data, asection *input_section,
2592 bfd *output_bfd, char **error_message)
2593 {
2594 if (output_bfd != NULL)
2595 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2596 input_section, output_bfd, error_message);
2597
2598 if (strcmp (symbol->section->name, ".opd") == 0
2599 && (symbol->section->owner->flags & DYNAMIC) == 0)
2600 {
2601 bfd_vma dest = opd_entry_value (symbol->section,
2602 symbol->value + reloc_entry->addend,
2603 NULL, NULL, FALSE);
2604 if (dest != (bfd_vma) -1)
2605 reloc_entry->addend = dest - (symbol->value
2606 + symbol->section->output_section->vma
2607 + symbol->section->output_offset);
2608 }
2609 else
2610 {
2611 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2612
2613 if (symbol->section->owner != abfd
2614 && symbol->section->owner != NULL
2615 && abiversion (symbol->section->owner) >= 2)
2616 {
2617 unsigned int i;
2618
2619 for (i = 0; i < symbol->section->owner->symcount; ++i)
2620 {
2621 asymbol *symdef = symbol->section->owner->outsymbols[i];
2622
2623 if (strcmp (symdef->name, symbol->name) == 0)
2624 {
2625 elfsym = (elf_symbol_type *) symdef;
2626 break;
2627 }
2628 }
2629 }
2630 reloc_entry->addend
2631 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2632 }
2633 return bfd_reloc_continue;
2634 }
2635
2636 static bfd_reloc_status_type
2637 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2638 void *data, asection *input_section,
2639 bfd *output_bfd, char **error_message)
2640 {
2641 long insn;
2642 enum elf_ppc64_reloc_type r_type;
2643 bfd_size_type octets;
2644 /* Assume 'at' branch hints. */
2645 bfd_boolean is_isa_v2 = TRUE;
2646
2647 /* If this is a relocatable link (output_bfd test tells us), just
2648 call the generic function. Any adjustment will be done at final
2649 link time. */
2650 if (output_bfd != NULL)
2651 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2652 input_section, output_bfd, error_message);
2653
2654 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2655 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2656 insn &= ~(0x01 << 21);
2657 r_type = reloc_entry->howto->type;
2658 if (r_type == R_PPC64_ADDR14_BRTAKEN
2659 || r_type == R_PPC64_REL14_BRTAKEN)
2660 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2661
2662 if (is_isa_v2)
2663 {
2664 /* Set 'a' bit. This is 0b00010 in BO field for branch
2665 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2666 for branch on CTR insns (BO == 1a00t or 1a01t). */
2667 if ((insn & (0x14 << 21)) == (0x04 << 21))
2668 insn |= 0x02 << 21;
2669 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2670 insn |= 0x08 << 21;
2671 else
2672 goto out;
2673 }
2674 else
2675 {
2676 bfd_vma target = 0;
2677 bfd_vma from;
2678
2679 if (!bfd_is_com_section (symbol->section))
2680 target = symbol->value;
2681 target += symbol->section->output_section->vma;
2682 target += symbol->section->output_offset;
2683 target += reloc_entry->addend;
2684
2685 from = (reloc_entry->address
2686 + input_section->output_offset
2687 + input_section->output_section->vma);
2688
2689 /* Invert 'y' bit if not the default. */
2690 if ((bfd_signed_vma) (target - from) < 0)
2691 insn ^= 0x01 << 21;
2692 }
2693 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2694 out:
2695 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2696 input_section, output_bfd, error_message);
2697 }
2698
2699 static bfd_reloc_status_type
2700 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2701 void *data, asection *input_section,
2702 bfd *output_bfd, char **error_message)
2703 {
2704 /* If this is a relocatable link (output_bfd test tells us), just
2705 call the generic function. Any adjustment will be done at final
2706 link time. */
2707 if (output_bfd != NULL)
2708 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2709 input_section, output_bfd, error_message);
2710
2711 /* Subtract the symbol section base address. */
2712 reloc_entry->addend -= symbol->section->output_section->vma;
2713 return bfd_reloc_continue;
2714 }
2715
2716 static bfd_reloc_status_type
2717 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2718 void *data, asection *input_section,
2719 bfd *output_bfd, char **error_message)
2720 {
2721 /* If this is a relocatable link (output_bfd test tells us), just
2722 call the generic function. Any adjustment will be done at final
2723 link time. */
2724 if (output_bfd != NULL)
2725 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2726 input_section, output_bfd, error_message);
2727
2728 /* Subtract the symbol section base address. */
2729 reloc_entry->addend -= symbol->section->output_section->vma;
2730
2731 /* Adjust the addend for sign extension of the low 16 bits. */
2732 reloc_entry->addend += 0x8000;
2733 return bfd_reloc_continue;
2734 }
2735
2736 static bfd_reloc_status_type
2737 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2738 void *data, asection *input_section,
2739 bfd *output_bfd, char **error_message)
2740 {
2741 bfd_vma TOCstart;
2742
2743 /* If this is a relocatable link (output_bfd test tells us), just
2744 call the generic function. Any adjustment will be done at final
2745 link time. */
2746 if (output_bfd != NULL)
2747 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2748 input_section, output_bfd, error_message);
2749
2750 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2751 if (TOCstart == 0)
2752 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2753
2754 /* Subtract the TOC base address. */
2755 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2756 return bfd_reloc_continue;
2757 }
2758
2759 static bfd_reloc_status_type
2760 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2761 void *data, asection *input_section,
2762 bfd *output_bfd, char **error_message)
2763 {
2764 bfd_vma TOCstart;
2765
2766 /* If this is a relocatable link (output_bfd test tells us), just
2767 call the generic function. Any adjustment will be done at final
2768 link time. */
2769 if (output_bfd != NULL)
2770 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2771 input_section, output_bfd, error_message);
2772
2773 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2774 if (TOCstart == 0)
2775 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2776
2777 /* Subtract the TOC base address. */
2778 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2779
2780 /* Adjust the addend for sign extension of the low 16 bits. */
2781 reloc_entry->addend += 0x8000;
2782 return bfd_reloc_continue;
2783 }
2784
2785 static bfd_reloc_status_type
2786 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2787 void *data, asection *input_section,
2788 bfd *output_bfd, char **error_message)
2789 {
2790 bfd_vma TOCstart;
2791 bfd_size_type octets;
2792
2793 /* If this is a relocatable link (output_bfd test tells us), just
2794 call the generic function. Any adjustment will be done at final
2795 link time. */
2796 if (output_bfd != NULL)
2797 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2798 input_section, output_bfd, error_message);
2799
2800 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2801 if (TOCstart == 0)
2802 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2803
2804 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2805 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2806 return bfd_reloc_ok;
2807 }
2808
2809 static bfd_reloc_status_type
2810 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2811 void *data, asection *input_section,
2812 bfd *output_bfd, char **error_message)
2813 {
2814 /* If this is a relocatable link (output_bfd test tells us), just
2815 call the generic function. Any adjustment will be done at final
2816 link time. */
2817 if (output_bfd != NULL)
2818 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2819 input_section, output_bfd, error_message);
2820
2821 if (error_message != NULL)
2822 {
2823 static char buf[60];
2824 sprintf (buf, "generic linker can't handle %s",
2825 reloc_entry->howto->name);
2826 *error_message = buf;
2827 }
2828 return bfd_reloc_dangerous;
2829 }
2830
2831 /* Track GOT entries needed for a given symbol. We might need more
2832 than one got entry per symbol. */
2833 struct got_entry
2834 {
2835 struct got_entry *next;
2836
2837 /* The symbol addend that we'll be placing in the GOT. */
2838 bfd_vma addend;
2839
2840 /* Unlike other ELF targets, we use separate GOT entries for the same
2841 symbol referenced from different input files. This is to support
2842 automatic multiple TOC/GOT sections, where the TOC base can vary
2843 from one input file to another. After partitioning into TOC groups
2844 we merge entries within the group.
2845
2846 Point to the BFD owning this GOT entry. */
2847 bfd *owner;
2848
2849 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2850 TLS_TPREL or TLS_DTPREL for tls entries. */
2851 unsigned char tls_type;
2852
2853 /* Non-zero if got.ent points to real entry. */
2854 unsigned char is_indirect;
2855
2856 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2857 union
2858 {
2859 bfd_signed_vma refcount;
2860 bfd_vma offset;
2861 struct got_entry *ent;
2862 } got;
2863 };
2864
2865 /* The same for PLT. */
2866 struct plt_entry
2867 {
2868 struct plt_entry *next;
2869
2870 bfd_vma addend;
2871
2872 union
2873 {
2874 bfd_signed_vma refcount;
2875 bfd_vma offset;
2876 } plt;
2877 };
2878
2879 struct ppc64_elf_obj_tdata
2880 {
2881 struct elf_obj_tdata elf;
2882
2883 /* Shortcuts to dynamic linker sections. */
2884 asection *got;
2885 asection *relgot;
2886
2887 /* Used during garbage collection. We attach global symbols defined
2888 on removed .opd entries to this section so that the sym is removed. */
2889 asection *deleted_section;
2890
2891 /* TLS local dynamic got entry handling. Support for multiple GOT
2892 sections means we potentially need one of these for each input bfd. */
2893 struct got_entry tlsld_got;
2894
2895 union {
2896 /* A copy of relocs before they are modified for --emit-relocs. */
2897 Elf_Internal_Rela *relocs;
2898
2899 /* Section contents. */
2900 bfd_byte *contents;
2901 } opd;
2902
2903 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2904 the reloc to be in the range -32768 to 32767. */
2905 unsigned int has_small_toc_reloc : 1;
2906
2907 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2908 instruction not one we handle. */
2909 unsigned int unexpected_toc_insn : 1;
2910 };
2911
2912 #define ppc64_elf_tdata(bfd) \
2913 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2914
2915 #define ppc64_tlsld_got(bfd) \
2916 (&ppc64_elf_tdata (bfd)->tlsld_got)
2917
2918 #define is_ppc64_elf(bfd) \
2919 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2920 && elf_object_id (bfd) == PPC64_ELF_DATA)
2921
2922 /* Override the generic function because we store some extras. */
2923
2924 static bfd_boolean
2925 ppc64_elf_mkobject (bfd *abfd)
2926 {
2927 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2928 PPC64_ELF_DATA);
2929 }
2930
2931 /* Fix bad default arch selected for a 64 bit input bfd when the
2932 default is 32 bit. Also select arch based on apuinfo. */
2933
2934 static bfd_boolean
2935 ppc64_elf_object_p (bfd *abfd)
2936 {
2937 if (!abfd->arch_info->the_default)
2938 return TRUE;
2939
2940 if (abfd->arch_info->bits_per_word == 32)
2941 {
2942 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2943
2944 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2945 {
2946 /* Relies on arch after 32 bit default being 64 bit default. */
2947 abfd->arch_info = abfd->arch_info->next;
2948 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2949 }
2950 }
2951 return _bfd_elf_ppc_set_arch (abfd);
2952 }
2953
2954 /* Support for core dump NOTE sections. */
2955
2956 static bfd_boolean
2957 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2958 {
2959 size_t offset, size;
2960
2961 if (note->descsz != 504)
2962 return FALSE;
2963
2964 /* pr_cursig */
2965 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2966
2967 /* pr_pid */
2968 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2969
2970 /* pr_reg */
2971 offset = 112;
2972 size = 384;
2973
2974 /* Make a ".reg/999" section. */
2975 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2976 size, note->descpos + offset);
2977 }
2978
2979 static bfd_boolean
2980 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2981 {
2982 if (note->descsz != 136)
2983 return FALSE;
2984
2985 elf_tdata (abfd)->core->pid
2986 = bfd_get_32 (abfd, note->descdata + 24);
2987 elf_tdata (abfd)->core->program
2988 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2989 elf_tdata (abfd)->core->command
2990 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2991
2992 return TRUE;
2993 }
2994
2995 static char *
2996 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2997 ...)
2998 {
2999 switch (note_type)
3000 {
3001 default:
3002 return NULL;
3003
3004 case NT_PRPSINFO:
3005 {
3006 char data[136];
3007 va_list ap;
3008
3009 va_start (ap, note_type);
3010 memset (data, 0, sizeof (data));
3011 strncpy (data + 40, va_arg (ap, const char *), 16);
3012 strncpy (data + 56, va_arg (ap, const char *), 80);
3013 va_end (ap);
3014 return elfcore_write_note (abfd, buf, bufsiz,
3015 "CORE", note_type, data, sizeof (data));
3016 }
3017
3018 case NT_PRSTATUS:
3019 {
3020 char data[504];
3021 va_list ap;
3022 long pid;
3023 int cursig;
3024 const void *greg;
3025
3026 va_start (ap, note_type);
3027 memset (data, 0, 112);
3028 pid = va_arg (ap, long);
3029 bfd_put_32 (abfd, pid, data + 32);
3030 cursig = va_arg (ap, int);
3031 bfd_put_16 (abfd, cursig, data + 12);
3032 greg = va_arg (ap, const void *);
3033 memcpy (data + 112, greg, 384);
3034 memset (data + 496, 0, 8);
3035 va_end (ap);
3036 return elfcore_write_note (abfd, buf, bufsiz,
3037 "CORE", note_type, data, sizeof (data));
3038 }
3039 }
3040 }
3041
3042 /* Add extra PPC sections. */
3043
3044 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3045 {
3046 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3047 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3048 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3049 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3050 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3051 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3052 { NULL, 0, 0, 0, 0 }
3053 };
3054
3055 enum _ppc64_sec_type {
3056 sec_normal = 0,
3057 sec_opd = 1,
3058 sec_toc = 2
3059 };
3060
3061 struct _ppc64_elf_section_data
3062 {
3063 struct bfd_elf_section_data elf;
3064
3065 union
3066 {
3067 /* An array with one entry for each opd function descriptor,
3068 and some spares since opd entries may be either 16 or 24 bytes. */
3069 #define OPD_NDX(OFF) ((OFF) >> 4)
3070 struct _opd_sec_data
3071 {
3072 /* Points to the function code section for local opd entries. */
3073 asection **func_sec;
3074
3075 /* After editing .opd, adjust references to opd local syms. */
3076 long *adjust;
3077 } opd;
3078
3079 /* An array for toc sections, indexed by offset/8. */
3080 struct _toc_sec_data
3081 {
3082 /* Specifies the relocation symbol index used at a given toc offset. */
3083 unsigned *symndx;
3084
3085 /* And the relocation addend. */
3086 bfd_vma *add;
3087 } toc;
3088 } u;
3089
3090 enum _ppc64_sec_type sec_type:2;
3091
3092 /* Flag set when small branches are detected. Used to
3093 select suitable defaults for the stub group size. */
3094 unsigned int has_14bit_branch:1;
3095 };
3096
3097 #define ppc64_elf_section_data(sec) \
3098 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3099
3100 static bfd_boolean
3101 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3102 {
3103 if (!sec->used_by_bfd)
3104 {
3105 struct _ppc64_elf_section_data *sdata;
3106 bfd_size_type amt = sizeof (*sdata);
3107
3108 sdata = bfd_zalloc (abfd, amt);
3109 if (sdata == NULL)
3110 return FALSE;
3111 sec->used_by_bfd = sdata;
3112 }
3113
3114 return _bfd_elf_new_section_hook (abfd, sec);
3115 }
3116
3117 static struct _opd_sec_data *
3118 get_opd_info (asection * sec)
3119 {
3120 if (sec != NULL
3121 && ppc64_elf_section_data (sec) != NULL
3122 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3123 return &ppc64_elf_section_data (sec)->u.opd;
3124 return NULL;
3125 }
3126 \f
3127 /* Parameters for the qsort hook. */
3128 static bfd_boolean synthetic_relocatable;
3129 static asection *synthetic_opd;
3130
3131 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
3132
3133 static int
3134 compare_symbols (const void *ap, const void *bp)
3135 {
3136 const asymbol *a = * (const asymbol **) ap;
3137 const asymbol *b = * (const asymbol **) bp;
3138
3139 /* Section symbols first. */
3140 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3141 return -1;
3142 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3143 return 1;
3144
3145 /* then .opd symbols. */
3146 if (synthetic_opd != NULL)
3147 {
3148 if (strcmp (a->section->name, ".opd") == 0
3149 && strcmp (b->section->name, ".opd") != 0)
3150 return -1;
3151 if (strcmp (a->section->name, ".opd") != 0
3152 && strcmp (b->section->name, ".opd") == 0)
3153 return 1;
3154 }
3155
3156 /* then other code symbols. */
3157 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3158 == (SEC_CODE | SEC_ALLOC)
3159 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3160 != (SEC_CODE | SEC_ALLOC))
3161 return -1;
3162
3163 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3164 != (SEC_CODE | SEC_ALLOC)
3165 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3166 == (SEC_CODE | SEC_ALLOC))
3167 return 1;
3168
3169 if (synthetic_relocatable)
3170 {
3171 if (a->section->id < b->section->id)
3172 return -1;
3173
3174 if (a->section->id > b->section->id)
3175 return 1;
3176 }
3177
3178 if (a->value + a->section->vma < b->value + b->section->vma)
3179 return -1;
3180
3181 if (a->value + a->section->vma > b->value + b->section->vma)
3182 return 1;
3183
3184 /* For syms with the same value, prefer strong dynamic global function
3185 syms over other syms. */
3186 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3187 return -1;
3188
3189 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3190 return 1;
3191
3192 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3193 return -1;
3194
3195 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3196 return 1;
3197
3198 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3199 return -1;
3200
3201 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3202 return 1;
3203
3204 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3205 return -1;
3206
3207 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3208 return 1;
3209
3210 return a > b;
3211 }
3212
3213 /* Search SYMS for a symbol of the given VALUE. */
3214
3215 static asymbol *
3216 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3217 {
3218 long mid;
3219
3220 if (id == (unsigned) -1)
3221 {
3222 while (lo < hi)
3223 {
3224 mid = (lo + hi) >> 1;
3225 if (syms[mid]->value + syms[mid]->section->vma < value)
3226 lo = mid + 1;
3227 else if (syms[mid]->value + syms[mid]->section->vma > value)
3228 hi = mid;
3229 else
3230 return syms[mid];
3231 }
3232 }
3233 else
3234 {
3235 while (lo < hi)
3236 {
3237 mid = (lo + hi) >> 1;
3238 if (syms[mid]->section->id < id)
3239 lo = mid + 1;
3240 else if (syms[mid]->section->id > id)
3241 hi = mid;
3242 else if (syms[mid]->value < value)
3243 lo = mid + 1;
3244 else if (syms[mid]->value > value)
3245 hi = mid;
3246 else
3247 return syms[mid];
3248 }
3249 }
3250 return NULL;
3251 }
3252
3253 static bfd_boolean
3254 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3255 {
3256 bfd_vma vma = *(bfd_vma *) ptr;
3257 return ((section->flags & SEC_ALLOC) != 0
3258 && section->vma <= vma
3259 && vma < section->vma + section->size);
3260 }
3261
3262 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3263 entry syms. Also generate @plt symbols for the glink branch table.
3264 Returns count of synthetic symbols in RET or -1 on error. */
3265
3266 static long
3267 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3268 long static_count, asymbol **static_syms,
3269 long dyn_count, asymbol **dyn_syms,
3270 asymbol **ret)
3271 {
3272 asymbol *s;
3273 size_t i, j, count;
3274 char *names;
3275 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3276 asection *opd = NULL;
3277 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3278 asymbol **syms;
3279 int abi = abiversion (abfd);
3280
3281 *ret = NULL;
3282
3283 if (abi < 2)
3284 {
3285 opd = bfd_get_section_by_name (abfd, ".opd");
3286 if (opd == NULL && abi == 1)
3287 return 0;
3288 }
3289
3290 syms = NULL;
3291 codesecsym = 0;
3292 codesecsymend = 0;
3293 secsymend = 0;
3294 opdsymend = 0;
3295 symcount = 0;
3296 if (opd != NULL)
3297 {
3298 symcount = static_count;
3299 if (!relocatable)
3300 symcount += dyn_count;
3301 if (symcount == 0)
3302 return 0;
3303
3304 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3305 if (syms == NULL)
3306 return -1;
3307
3308 if (!relocatable && static_count != 0 && dyn_count != 0)
3309 {
3310 /* Use both symbol tables. */
3311 memcpy (syms, static_syms, static_count * sizeof (*syms));
3312 memcpy (syms + static_count, dyn_syms,
3313 (dyn_count + 1) * sizeof (*syms));
3314 }
3315 else if (!relocatable && static_count == 0)
3316 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3317 else
3318 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3319
3320 /* Trim uninteresting symbols. Interesting symbols are section,
3321 function, and notype symbols. */
3322 for (i = 0, j = 0; i < symcount; ++i)
3323 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
3324 | BSF_RELC | BSF_SRELC)) == 0)
3325 syms[j++] = syms[i];
3326 symcount = j;
3327
3328 synthetic_relocatable = relocatable;
3329 synthetic_opd = opd;
3330 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3331
3332 if (!relocatable && symcount > 1)
3333 {
3334 /* Trim duplicate syms, since we may have merged the normal and
3335 dynamic symbols. Actually, we only care about syms that have
3336 different values, so trim any with the same value. */
3337 for (i = 1, j = 1; i < symcount; ++i)
3338 if (syms[i - 1]->value + syms[i - 1]->section->vma
3339 != syms[i]->value + syms[i]->section->vma)
3340 syms[j++] = syms[i];
3341 symcount = j;
3342 }
3343
3344 i = 0;
3345 /* Note that here and in compare_symbols we can't compare opd and
3346 sym->section directly. With separate debug info files, the
3347 symbols will be extracted from the debug file while abfd passed
3348 to this function is the real binary. */
3349 if (strcmp (syms[i]->section->name, ".opd") == 0)
3350 ++i;
3351 codesecsym = i;
3352
3353 for (; i < symcount; ++i)
3354 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3355 | SEC_THREAD_LOCAL))
3356 != (SEC_CODE | SEC_ALLOC))
3357 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3358 break;
3359 codesecsymend = i;
3360
3361 for (; i < symcount; ++i)
3362 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3363 break;
3364 secsymend = i;
3365
3366 for (; i < symcount; ++i)
3367 if (strcmp (syms[i]->section->name, ".opd") != 0)
3368 break;
3369 opdsymend = i;
3370
3371 for (; i < symcount; ++i)
3372 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3373 != (SEC_CODE | SEC_ALLOC))
3374 break;
3375 symcount = i;
3376 }
3377 count = 0;
3378
3379 if (relocatable)
3380 {
3381 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3382 arelent *r;
3383 size_t size;
3384 size_t relcount;
3385
3386 if (opdsymend == secsymend)
3387 goto done;
3388
3389 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3390 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3391 if (relcount == 0)
3392 goto done;
3393
3394 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3395 {
3396 count = -1;
3397 goto done;
3398 }
3399
3400 size = 0;
3401 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3402 {
3403 asymbol *sym;
3404
3405 while (r < opd->relocation + relcount
3406 && r->address < syms[i]->value + opd->vma)
3407 ++r;
3408
3409 if (r == opd->relocation + relcount)
3410 break;
3411
3412 if (r->address != syms[i]->value + opd->vma)
3413 continue;
3414
3415 if (r->howto->type != R_PPC64_ADDR64)
3416 continue;
3417
3418 sym = *r->sym_ptr_ptr;
3419 if (!sym_exists_at (syms, opdsymend, symcount,
3420 sym->section->id, sym->value + r->addend))
3421 {
3422 ++count;
3423 size += sizeof (asymbol);
3424 size += strlen (syms[i]->name) + 2;
3425 }
3426 }
3427
3428 if (size == 0)
3429 goto done;
3430 s = *ret = bfd_malloc (size);
3431 if (s == NULL)
3432 {
3433 count = -1;
3434 goto done;
3435 }
3436
3437 names = (char *) (s + count);
3438
3439 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3440 {
3441 asymbol *sym;
3442
3443 while (r < opd->relocation + relcount
3444 && r->address < syms[i]->value + opd->vma)
3445 ++r;
3446
3447 if (r == opd->relocation + relcount)
3448 break;
3449
3450 if (r->address != syms[i]->value + opd->vma)
3451 continue;
3452
3453 if (r->howto->type != R_PPC64_ADDR64)
3454 continue;
3455
3456 sym = *r->sym_ptr_ptr;
3457 if (!sym_exists_at (syms, opdsymend, symcount,
3458 sym->section->id, sym->value + r->addend))
3459 {
3460 size_t len;
3461
3462 *s = *syms[i];
3463 s->flags |= BSF_SYNTHETIC;
3464 s->section = sym->section;
3465 s->value = sym->value + r->addend;
3466 s->name = names;
3467 *names++ = '.';
3468 len = strlen (syms[i]->name);
3469 memcpy (names, syms[i]->name, len + 1);
3470 names += len + 1;
3471 /* Have udata.p point back to the original symbol this
3472 synthetic symbol was derived from. */
3473 s->udata.p = syms[i];
3474 s++;
3475 }
3476 }
3477 }
3478 else
3479 {
3480 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3481 bfd_byte *contents = NULL;
3482 size_t size;
3483 size_t plt_count = 0;
3484 bfd_vma glink_vma = 0, resolv_vma = 0;
3485 asection *dynamic, *glink = NULL, *relplt = NULL;
3486 arelent *p;
3487
3488 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3489 {
3490 free_contents_and_exit_err:
3491 count = -1;
3492 free_contents_and_exit:
3493 if (contents)
3494 free (contents);
3495 goto done;
3496 }
3497
3498 size = 0;
3499 for (i = secsymend; i < opdsymend; ++i)
3500 {
3501 bfd_vma ent;
3502
3503 /* Ignore bogus symbols. */
3504 if (syms[i]->value > opd->size - 8)
3505 continue;
3506
3507 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3508 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3509 {
3510 ++count;
3511 size += sizeof (asymbol);
3512 size += strlen (syms[i]->name) + 2;
3513 }
3514 }
3515
3516 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3517 if (dyn_count != 0
3518 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3519 {
3520 bfd_byte *dynbuf, *extdyn, *extdynend;
3521 size_t extdynsize;
3522 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3523
3524 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3525 goto free_contents_and_exit_err;
3526
3527 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3528 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3529
3530 extdyn = dynbuf;
3531 extdynend = extdyn + dynamic->size;
3532 for (; extdyn < extdynend; extdyn += extdynsize)
3533 {
3534 Elf_Internal_Dyn dyn;
3535 (*swap_dyn_in) (abfd, extdyn, &dyn);
3536
3537 if (dyn.d_tag == DT_NULL)
3538 break;
3539
3540 if (dyn.d_tag == DT_PPC64_GLINK)
3541 {
3542 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3543 See comment in ppc64_elf_finish_dynamic_sections. */
3544 glink_vma = dyn.d_un.d_val + 8 * 4;
3545 /* The .glink section usually does not survive the final
3546 link; search for the section (usually .text) where the
3547 glink stubs now reside. */
3548 glink = bfd_sections_find_if (abfd, section_covers_vma,
3549 &glink_vma);
3550 break;
3551 }
3552 }
3553
3554 free (dynbuf);
3555 }
3556
3557 if (glink != NULL)
3558 {
3559 /* Determine __glink trampoline by reading the relative branch
3560 from the first glink stub. */
3561 bfd_byte buf[4];
3562 unsigned int off = 0;
3563
3564 while (bfd_get_section_contents (abfd, glink, buf,
3565 glink_vma + off - glink->vma, 4))
3566 {
3567 unsigned int insn = bfd_get_32 (abfd, buf);
3568 insn ^= B_DOT;
3569 if ((insn & ~0x3fffffc) == 0)
3570 {
3571 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3572 break;
3573 }
3574 off += 4;
3575 if (off > 4)
3576 break;
3577 }
3578
3579 if (resolv_vma)
3580 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3581
3582 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3583 if (relplt != NULL)
3584 {
3585 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3586 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3587 goto free_contents_and_exit_err;
3588
3589 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3590 size += plt_count * sizeof (asymbol);
3591
3592 p = relplt->relocation;
3593 for (i = 0; i < plt_count; i++, p++)
3594 {
3595 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3596 if (p->addend != 0)
3597 size += sizeof ("+0x") - 1 + 16;
3598 }
3599 }
3600 }
3601
3602 if (size == 0)
3603 goto free_contents_and_exit;
3604 s = *ret = bfd_malloc (size);
3605 if (s == NULL)
3606 goto free_contents_and_exit_err;
3607
3608 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3609
3610 for (i = secsymend; i < opdsymend; ++i)
3611 {
3612 bfd_vma ent;
3613
3614 if (syms[i]->value > opd->size - 8)
3615 continue;
3616
3617 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3618 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3619 {
3620 size_t lo, hi;
3621 size_t len;
3622 asection *sec = abfd->sections;
3623
3624 *s = *syms[i];
3625 lo = codesecsym;
3626 hi = codesecsymend;
3627 while (lo < hi)
3628 {
3629 size_t mid = (lo + hi) >> 1;
3630 if (syms[mid]->section->vma < ent)
3631 lo = mid + 1;
3632 else if (syms[mid]->section->vma > ent)
3633 hi = mid;
3634 else
3635 {
3636 sec = syms[mid]->section;
3637 break;
3638 }
3639 }
3640
3641 if (lo >= hi && lo > codesecsym)
3642 sec = syms[lo - 1]->section;
3643
3644 for (; sec != NULL; sec = sec->next)
3645 {
3646 if (sec->vma > ent)
3647 break;
3648 /* SEC_LOAD may not be set if SEC is from a separate debug
3649 info file. */
3650 if ((sec->flags & SEC_ALLOC) == 0)
3651 break;
3652 if ((sec->flags & SEC_CODE) != 0)
3653 s->section = sec;
3654 }
3655 s->flags |= BSF_SYNTHETIC;
3656 s->value = ent - s->section->vma;
3657 s->name = names;
3658 *names++ = '.';
3659 len = strlen (syms[i]->name);
3660 memcpy (names, syms[i]->name, len + 1);
3661 names += len + 1;
3662 /* Have udata.p point back to the original symbol this
3663 synthetic symbol was derived from. */
3664 s->udata.p = syms[i];
3665 s++;
3666 }
3667 }
3668 free (contents);
3669
3670 if (glink != NULL && relplt != NULL)
3671 {
3672 if (resolv_vma)
3673 {
3674 /* Add a symbol for the main glink trampoline. */
3675 memset (s, 0, sizeof *s);
3676 s->the_bfd = abfd;
3677 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3678 s->section = glink;
3679 s->value = resolv_vma - glink->vma;
3680 s->name = names;
3681 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3682 names += sizeof ("__glink_PLTresolve");
3683 s++;
3684 count++;
3685 }
3686
3687 /* FIXME: It would be very much nicer to put sym@plt on the
3688 stub rather than on the glink branch table entry. The
3689 objdump disassembler would then use a sensible symbol
3690 name on plt calls. The difficulty in doing so is
3691 a) finding the stubs, and,
3692 b) matching stubs against plt entries, and,
3693 c) there can be multiple stubs for a given plt entry.
3694
3695 Solving (a) could be done by code scanning, but older
3696 ppc64 binaries used different stubs to current code.
3697 (b) is the tricky one since you need to known the toc
3698 pointer for at least one function that uses a pic stub to
3699 be able to calculate the plt address referenced.
3700 (c) means gdb would need to set multiple breakpoints (or
3701 find the glink branch itself) when setting breakpoints
3702 for pending shared library loads. */
3703 p = relplt->relocation;
3704 for (i = 0; i < plt_count; i++, p++)
3705 {
3706 size_t len;
3707
3708 *s = **p->sym_ptr_ptr;
3709 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3710 we are defining a symbol, ensure one of them is set. */
3711 if ((s->flags & BSF_LOCAL) == 0)
3712 s->flags |= BSF_GLOBAL;
3713 s->flags |= BSF_SYNTHETIC;
3714 s->section = glink;
3715 s->value = glink_vma - glink->vma;
3716 s->name = names;
3717 s->udata.p = NULL;
3718 len = strlen ((*p->sym_ptr_ptr)->name);
3719 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3720 names += len;
3721 if (p->addend != 0)
3722 {
3723 memcpy (names, "+0x", sizeof ("+0x") - 1);
3724 names += sizeof ("+0x") - 1;
3725 bfd_sprintf_vma (abfd, names, p->addend);
3726 names += strlen (names);
3727 }
3728 memcpy (names, "@plt", sizeof ("@plt"));
3729 names += sizeof ("@plt");
3730 s++;
3731 if (abi < 2)
3732 {
3733 glink_vma += 8;
3734 if (i >= 0x8000)
3735 glink_vma += 4;
3736 }
3737 else
3738 glink_vma += 4;
3739 }
3740 count += plt_count;
3741 }
3742 }
3743
3744 done:
3745 free (syms);
3746 return count;
3747 }
3748 \f
3749 /* The following functions are specific to the ELF linker, while
3750 functions above are used generally. Those named ppc64_elf_* are
3751 called by the main ELF linker code. They appear in this file more
3752 or less in the order in which they are called. eg.
3753 ppc64_elf_check_relocs is called early in the link process,
3754 ppc64_elf_finish_dynamic_sections is one of the last functions
3755 called.
3756
3757 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3758 functions have both a function code symbol and a function descriptor
3759 symbol. A call to foo in a relocatable object file looks like:
3760
3761 . .text
3762 . x:
3763 . bl .foo
3764 . nop
3765
3766 The function definition in another object file might be:
3767
3768 . .section .opd
3769 . foo: .quad .foo
3770 . .quad .TOC.@tocbase
3771 . .quad 0
3772 .
3773 . .text
3774 . .foo: blr
3775
3776 When the linker resolves the call during a static link, the branch
3777 unsurprisingly just goes to .foo and the .opd information is unused.
3778 If the function definition is in a shared library, things are a little
3779 different: The call goes via a plt call stub, the opd information gets
3780 copied to the plt, and the linker patches the nop.
3781
3782 . x:
3783 . bl .foo_stub
3784 . ld 2,40(1)
3785 .
3786 .
3787 . .foo_stub:
3788 . std 2,40(1) # in practice, the call stub
3789 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3790 . addi 11,11,Lfoo@toc@l # this is the general idea
3791 . ld 12,0(11)
3792 . ld 2,8(11)
3793 . mtctr 12
3794 . ld 11,16(11)
3795 . bctr
3796 .
3797 . .section .plt
3798 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3799
3800 The "reloc ()" notation is supposed to indicate that the linker emits
3801 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3802 copying.
3803
3804 What are the difficulties here? Well, firstly, the relocations
3805 examined by the linker in check_relocs are against the function code
3806 sym .foo, while the dynamic relocation in the plt is emitted against
3807 the function descriptor symbol, foo. Somewhere along the line, we need
3808 to carefully copy dynamic link information from one symbol to the other.
3809 Secondly, the generic part of the elf linker will make .foo a dynamic
3810 symbol as is normal for most other backends. We need foo dynamic
3811 instead, at least for an application final link. However, when
3812 creating a shared library containing foo, we need to have both symbols
3813 dynamic so that references to .foo are satisfied during the early
3814 stages of linking. Otherwise the linker might decide to pull in a
3815 definition from some other object, eg. a static library.
3816
3817 Update: As of August 2004, we support a new convention. Function
3818 calls may use the function descriptor symbol, ie. "bl foo". This
3819 behaves exactly as "bl .foo". */
3820
3821 /* Of those relocs that might be copied as dynamic relocs, this
3822 function selects those that must be copied when linking a shared
3823 library or PIE, even when the symbol is local. */
3824
3825 static int
3826 must_be_dyn_reloc (struct bfd_link_info *info,
3827 enum elf_ppc64_reloc_type r_type)
3828 {
3829 switch (r_type)
3830 {
3831 default:
3832 /* Only relative relocs can be resolved when the object load
3833 address isn't fixed. DTPREL64 is excluded because the
3834 dynamic linker needs to differentiate global dynamic from
3835 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
3836 return 1;
3837
3838 case R_PPC64_REL32:
3839 case R_PPC64_REL64:
3840 case R_PPC64_REL30:
3841 return 0;
3842
3843 case R_PPC64_TPREL16:
3844 case R_PPC64_TPREL16_LO:
3845 case R_PPC64_TPREL16_HI:
3846 case R_PPC64_TPREL16_HA:
3847 case R_PPC64_TPREL16_DS:
3848 case R_PPC64_TPREL16_LO_DS:
3849 case R_PPC64_TPREL16_HIGH:
3850 case R_PPC64_TPREL16_HIGHA:
3851 case R_PPC64_TPREL16_HIGHER:
3852 case R_PPC64_TPREL16_HIGHERA:
3853 case R_PPC64_TPREL16_HIGHEST:
3854 case R_PPC64_TPREL16_HIGHESTA:
3855 case R_PPC64_TPREL64:
3856 /* These relocations are relative but in a shared library the
3857 linker doesn't know the thread pointer base. */
3858 return bfd_link_dll (info);
3859 }
3860 }
3861
3862 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3863 copying dynamic variables from a shared lib into an app's dynbss
3864 section, and instead use a dynamic relocation to point into the
3865 shared lib. With code that gcc generates, it's vital that this be
3866 enabled; In the PowerPC64 ABI, the address of a function is actually
3867 the address of a function descriptor, which resides in the .opd
3868 section. gcc uses the descriptor directly rather than going via the
3869 GOT as some other ABI's do, which means that initialized function
3870 pointers must reference the descriptor. Thus, a function pointer
3871 initialized to the address of a function in a shared library will
3872 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3873 redefines the function descriptor symbol to point to the copy. This
3874 presents a problem as a plt entry for that function is also
3875 initialized from the function descriptor symbol and the copy reloc
3876 may not be initialized first. */
3877 #define ELIMINATE_COPY_RELOCS 1
3878
3879 /* Section name for stubs is the associated section name plus this
3880 string. */
3881 #define STUB_SUFFIX ".stub"
3882
3883 /* Linker stubs.
3884 ppc_stub_long_branch:
3885 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3886 destination, but a 24 bit branch in a stub section will reach.
3887 . b dest
3888
3889 ppc_stub_plt_branch:
3890 Similar to the above, but a 24 bit branch in the stub section won't
3891 reach its destination.
3892 . addis %r11,%r2,xxx@toc@ha
3893 . ld %r12,xxx@toc@l(%r11)
3894 . mtctr %r12
3895 . bctr
3896
3897 ppc_stub_plt_call:
3898 Used to call a function in a shared library. If it so happens that
3899 the plt entry referenced crosses a 64k boundary, then an extra
3900 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3901 . std %r2,40(%r1)
3902 . addis %r11,%r2,xxx@toc@ha
3903 . ld %r12,xxx+0@toc@l(%r11)
3904 . mtctr %r12
3905 . ld %r2,xxx+8@toc@l(%r11)
3906 . ld %r11,xxx+16@toc@l(%r11)
3907 . bctr
3908
3909 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3910 code to adjust the value and save r2 to support multiple toc sections.
3911 A ppc_stub_long_branch with an r2 offset looks like:
3912 . std %r2,40(%r1)
3913 . addis %r2,%r2,off@ha
3914 . addi %r2,%r2,off@l
3915 . b dest
3916
3917 A ppc_stub_plt_branch with an r2 offset looks like:
3918 . std %r2,40(%r1)
3919 . addis %r11,%r2,xxx@toc@ha
3920 . ld %r12,xxx@toc@l(%r11)
3921 . addis %r2,%r2,off@ha
3922 . addi %r2,%r2,off@l
3923 . mtctr %r12
3924 . bctr
3925
3926 In cases where the "addis" instruction would add zero, the "addis" is
3927 omitted and following instructions modified slightly in some cases.
3928 */
3929
3930 enum ppc_stub_type {
3931 ppc_stub_none,
3932 ppc_stub_long_branch,
3933 ppc_stub_long_branch_r2off,
3934 ppc_stub_plt_branch,
3935 ppc_stub_plt_branch_r2off,
3936 ppc_stub_plt_call,
3937 ppc_stub_plt_call_r2save,
3938 ppc_stub_global_entry,
3939 ppc_stub_save_res
3940 };
3941
3942 /* Information on stub grouping. */
3943 struct map_stub
3944 {
3945 /* The stub section. */
3946 asection *stub_sec;
3947 /* This is the section to which stubs in the group will be attached. */
3948 asection *link_sec;
3949 /* Next group. */
3950 struct map_stub *next;
3951 /* Whether to emit a copy of register save/restore functions in this
3952 group. */
3953 int needs_save_res;
3954 /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
3955 or -1u if no such stub with bctrl exists. */
3956 unsigned int tls_get_addr_opt_bctrl;
3957 };
3958
3959 struct ppc_stub_hash_entry {
3960
3961 /* Base hash table entry structure. */
3962 struct bfd_hash_entry root;
3963
3964 enum ppc_stub_type stub_type;
3965
3966 /* Group information. */
3967 struct map_stub *group;
3968
3969 /* Offset within stub_sec of the beginning of this stub. */
3970 bfd_vma stub_offset;
3971
3972 /* Given the symbol's value and its section we can determine its final
3973 value when building the stubs (so the stub knows where to jump. */
3974 bfd_vma target_value;
3975 asection *target_section;
3976
3977 /* The symbol table entry, if any, that this was derived from. */
3978 struct ppc_link_hash_entry *h;
3979 struct plt_entry *plt_ent;
3980
3981 /* Symbol type. */
3982 unsigned char symtype;
3983
3984 /* Symbol st_other. */
3985 unsigned char other;
3986 };
3987
3988 struct ppc_branch_hash_entry {
3989
3990 /* Base hash table entry structure. */
3991 struct bfd_hash_entry root;
3992
3993 /* Offset within branch lookup table. */
3994 unsigned int offset;
3995
3996 /* Generation marker. */
3997 unsigned int iter;
3998 };
3999
4000 /* Used to track dynamic relocations for local symbols. */
4001 struct ppc_dyn_relocs
4002 {
4003 struct ppc_dyn_relocs *next;
4004
4005 /* The input section of the reloc. */
4006 asection *sec;
4007
4008 /* Total number of relocs copied for the input section. */
4009 unsigned int count : 31;
4010
4011 /* Whether this entry is for STT_GNU_IFUNC symbols. */
4012 unsigned int ifunc : 1;
4013 };
4014
4015 struct ppc_link_hash_entry
4016 {
4017 struct elf_link_hash_entry elf;
4018
4019 union {
4020 /* A pointer to the most recently used stub hash entry against this
4021 symbol. */
4022 struct ppc_stub_hash_entry *stub_cache;
4023
4024 /* A pointer to the next symbol starting with a '.' */
4025 struct ppc_link_hash_entry *next_dot_sym;
4026 } u;
4027
4028 /* Track dynamic relocs copied for this symbol. */
4029 struct elf_dyn_relocs *dyn_relocs;
4030
4031 /* Link between function code and descriptor symbols. */
4032 struct ppc_link_hash_entry *oh;
4033
4034 /* Flag function code and descriptor symbols. */
4035 unsigned int is_func:1;
4036 unsigned int is_func_descriptor:1;
4037 unsigned int fake:1;
4038
4039 /* Whether global opd/toc sym has been adjusted or not.
4040 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4041 should be set for all globals defined in any opd/toc section. */
4042 unsigned int adjust_done:1;
4043
4044 /* Set if this is an out-of-line register save/restore function,
4045 with non-standard calling convention. */
4046 unsigned int save_res:1;
4047
4048 /* Set if a duplicate symbol with non-zero localentry is detected,
4049 even when the duplicate symbol does not provide a definition. */
4050 unsigned int non_zero_localentry:1;
4051
4052 /* Contexts in which symbol is used in the GOT (or TOC).
4053 Bits are or'd into the mask as the corresponding relocs are
4054 encountered during check_relocs, with TLS_TLS being set when any
4055 of the other TLS bits are set. tls_optimize clears bits when
4056 optimizing to indicate the corresponding GOT entry type is not
4057 needed. If set, TLS_TLS is never cleared. tls_optimize may also
4058 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
4059 separate flag rather than setting TPREL just for convenience in
4060 distinguishing the two cases.
4061 These flags are also kept for local symbols. */
4062 #define TLS_TLS 1 /* Any TLS reloc. */
4063 #define TLS_GD 2 /* GD reloc. */
4064 #define TLS_LD 4 /* LD reloc. */
4065 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
4066 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
4067 #define TLS_MARK 32 /* __tls_get_addr call marked. */
4068 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
4069 #define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
4070 unsigned char tls_mask;
4071
4072 /* The above field is also used to mark function symbols. In which
4073 case TLS_TLS will be 0. */
4074 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
4075 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
4076 #define NON_GOT 256 /* local symbol plt, not stored. */
4077 };
4078
4079 /* ppc64 ELF linker hash table. */
4080
4081 struct ppc_link_hash_table
4082 {
4083 struct elf_link_hash_table elf;
4084
4085 /* The stub hash table. */
4086 struct bfd_hash_table stub_hash_table;
4087
4088 /* Another hash table for plt_branch stubs. */
4089 struct bfd_hash_table branch_hash_table;
4090
4091 /* Hash table for function prologue tocsave. */
4092 htab_t tocsave_htab;
4093
4094 /* Various options and other info passed from the linker. */
4095 struct ppc64_elf_params *params;
4096
4097 /* The size of sec_info below. */
4098 unsigned int sec_info_arr_size;
4099
4100 /* Per-section array of extra section info. Done this way rather
4101 than as part of ppc64_elf_section_data so we have the info for
4102 non-ppc64 sections. */
4103 struct
4104 {
4105 /* Along with elf_gp, specifies the TOC pointer used by this section. */
4106 bfd_vma toc_off;
4107
4108 union
4109 {
4110 /* The section group that this section belongs to. */
4111 struct map_stub *group;
4112 /* A temp section list pointer. */
4113 asection *list;
4114 } u;
4115 } *sec_info;
4116
4117 /* Linked list of groups. */
4118 struct map_stub *group;
4119
4120 /* Temp used when calculating TOC pointers. */
4121 bfd_vma toc_curr;
4122 bfd *toc_bfd;
4123 asection *toc_first_sec;
4124
4125 /* Used when adding symbols. */
4126 struct ppc_link_hash_entry *dot_syms;
4127
4128 /* Shortcuts to get to dynamic linker sections. */
4129 asection *glink;
4130 asection *global_entry;
4131 asection *sfpr;
4132 asection *pltlocal;
4133 asection *relpltlocal;
4134 asection *brlt;
4135 asection *relbrlt;
4136 asection *glink_eh_frame;
4137
4138 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4139 struct ppc_link_hash_entry *tls_get_addr;
4140 struct ppc_link_hash_entry *tls_get_addr_fd;
4141
4142 /* The size of reliplt used by got entry relocs. */
4143 bfd_size_type got_reli_size;
4144
4145 /* Statistics. */
4146 unsigned long stub_count[ppc_stub_global_entry];
4147
4148 /* Number of stubs against global syms. */
4149 unsigned long stub_globals;
4150
4151 /* Set if we're linking code with function descriptors. */
4152 unsigned int opd_abi:1;
4153
4154 /* Support for multiple toc sections. */
4155 unsigned int do_multi_toc:1;
4156 unsigned int multi_toc_needed:1;
4157 unsigned int second_toc_pass:1;
4158 unsigned int do_toc_opt:1;
4159
4160 /* Set if tls optimization is enabled. */
4161 unsigned int do_tls_opt:1;
4162
4163 /* Set on error. */
4164 unsigned int stub_error:1;
4165
4166 /* Whether func_desc_adjust needs to be run over symbols. */
4167 unsigned int need_func_desc_adj:1;
4168
4169 /* Whether there exist local gnu indirect function resolvers,
4170 referenced by dynamic relocations. */
4171 unsigned int local_ifunc_resolver:1;
4172 unsigned int maybe_local_ifunc_resolver:1;
4173
4174 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4175 unsigned int has_plt_localentry0:1;
4176
4177 /* Incremented every time we size stubs. */
4178 unsigned int stub_iteration;
4179
4180 /* Small local sym cache. */
4181 struct sym_cache sym_cache;
4182 };
4183
4184 /* Rename some of the generic section flags to better document how they
4185 are used here. */
4186
4187 /* Nonzero if this section has TLS related relocations. */
4188 #define has_tls_reloc sec_flg0
4189
4190 /* Nonzero if this section has a call to __tls_get_addr. */
4191 #define has_tls_get_addr_call sec_flg1
4192
4193 /* Nonzero if this section has any toc or got relocs. */
4194 #define has_toc_reloc sec_flg2
4195
4196 /* Nonzero if this section has a call to another section that uses
4197 the toc or got. */
4198 #define makes_toc_func_call sec_flg3
4199
4200 /* Recursion protection when determining above flag. */
4201 #define call_check_in_progress sec_flg4
4202 #define call_check_done sec_flg5
4203
4204 /* Get the ppc64 ELF linker hash table from a link_info structure. */
4205
4206 #define ppc_hash_table(p) \
4207 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4208 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4209
4210 #define ppc_stub_hash_lookup(table, string, create, copy) \
4211 ((struct ppc_stub_hash_entry *) \
4212 bfd_hash_lookup ((table), (string), (create), (copy)))
4213
4214 #define ppc_branch_hash_lookup(table, string, create, copy) \
4215 ((struct ppc_branch_hash_entry *) \
4216 bfd_hash_lookup ((table), (string), (create), (copy)))
4217
4218 /* Create an entry in the stub hash table. */
4219
4220 static struct bfd_hash_entry *
4221 stub_hash_newfunc (struct bfd_hash_entry *entry,
4222 struct bfd_hash_table *table,
4223 const char *string)
4224 {
4225 /* Allocate the structure if it has not already been allocated by a
4226 subclass. */
4227 if (entry == NULL)
4228 {
4229 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4230 if (entry == NULL)
4231 return entry;
4232 }
4233
4234 /* Call the allocation method of the superclass. */
4235 entry = bfd_hash_newfunc (entry, table, string);
4236 if (entry != NULL)
4237 {
4238 struct ppc_stub_hash_entry *eh;
4239
4240 /* Initialize the local fields. */
4241 eh = (struct ppc_stub_hash_entry *) entry;
4242 eh->stub_type = ppc_stub_none;
4243 eh->group = NULL;
4244 eh->stub_offset = 0;
4245 eh->target_value = 0;
4246 eh->target_section = NULL;
4247 eh->h = NULL;
4248 eh->plt_ent = NULL;
4249 eh->other = 0;
4250 }
4251
4252 return entry;
4253 }
4254
4255 /* Create an entry in the branch hash table. */
4256
4257 static struct bfd_hash_entry *
4258 branch_hash_newfunc (struct bfd_hash_entry *entry,
4259 struct bfd_hash_table *table,
4260 const char *string)
4261 {
4262 /* Allocate the structure if it has not already been allocated by a
4263 subclass. */
4264 if (entry == NULL)
4265 {
4266 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4267 if (entry == NULL)
4268 return entry;
4269 }
4270
4271 /* Call the allocation method of the superclass. */
4272 entry = bfd_hash_newfunc (entry, table, string);
4273 if (entry != NULL)
4274 {
4275 struct ppc_branch_hash_entry *eh;
4276
4277 /* Initialize the local fields. */
4278 eh = (struct ppc_branch_hash_entry *) entry;
4279 eh->offset = 0;
4280 eh->iter = 0;
4281 }
4282
4283 return entry;
4284 }
4285
4286 /* Create an entry in a ppc64 ELF linker hash table. */
4287
4288 static struct bfd_hash_entry *
4289 link_hash_newfunc (struct bfd_hash_entry *entry,
4290 struct bfd_hash_table *table,
4291 const char *string)
4292 {
4293 /* Allocate the structure if it has not already been allocated by a
4294 subclass. */
4295 if (entry == NULL)
4296 {
4297 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4298 if (entry == NULL)
4299 return entry;
4300 }
4301
4302 /* Call the allocation method of the superclass. */
4303 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4304 if (entry != NULL)
4305 {
4306 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4307
4308 memset (&eh->u.stub_cache, 0,
4309 (sizeof (struct ppc_link_hash_entry)
4310 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4311
4312 /* When making function calls, old ABI code references function entry
4313 points (dot symbols), while new ABI code references the function
4314 descriptor symbol. We need to make any combination of reference and
4315 definition work together, without breaking archive linking.
4316
4317 For a defined function "foo" and an undefined call to "bar":
4318 An old object defines "foo" and ".foo", references ".bar" (possibly
4319 "bar" too).
4320 A new object defines "foo" and references "bar".
4321
4322 A new object thus has no problem with its undefined symbols being
4323 satisfied by definitions in an old object. On the other hand, the
4324 old object won't have ".bar" satisfied by a new object.
4325
4326 Keep a list of newly added dot-symbols. */
4327
4328 if (string[0] == '.')
4329 {
4330 struct ppc_link_hash_table *htab;
4331
4332 htab = (struct ppc_link_hash_table *) table;
4333 eh->u.next_dot_sym = htab->dot_syms;
4334 htab->dot_syms = eh;
4335 }
4336 }
4337
4338 return entry;
4339 }
4340
4341 struct tocsave_entry {
4342 asection *sec;
4343 bfd_vma offset;
4344 };
4345
4346 static hashval_t
4347 tocsave_htab_hash (const void *p)
4348 {
4349 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4350 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4351 }
4352
4353 static int
4354 tocsave_htab_eq (const void *p1, const void *p2)
4355 {
4356 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4357 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4358 return e1->sec == e2->sec && e1->offset == e2->offset;
4359 }
4360
4361 /* Destroy a ppc64 ELF linker hash table. */
4362
4363 static void
4364 ppc64_elf_link_hash_table_free (bfd *obfd)
4365 {
4366 struct ppc_link_hash_table *htab;
4367
4368 htab = (struct ppc_link_hash_table *) obfd->link.hash;
4369 if (htab->tocsave_htab)
4370 htab_delete (htab->tocsave_htab);
4371 bfd_hash_table_free (&htab->branch_hash_table);
4372 bfd_hash_table_free (&htab->stub_hash_table);
4373 _bfd_elf_link_hash_table_free (obfd);
4374 }
4375
4376 /* Create a ppc64 ELF linker hash table. */
4377
4378 static struct bfd_link_hash_table *
4379 ppc64_elf_link_hash_table_create (bfd *abfd)
4380 {
4381 struct ppc_link_hash_table *htab;
4382 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4383
4384 htab = bfd_zmalloc (amt);
4385 if (htab == NULL)
4386 return NULL;
4387
4388 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4389 sizeof (struct ppc_link_hash_entry),
4390 PPC64_ELF_DATA))
4391 {
4392 free (htab);
4393 return NULL;
4394 }
4395
4396 /* Init the stub hash table too. */
4397 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4398 sizeof (struct ppc_stub_hash_entry)))
4399 {
4400 _bfd_elf_link_hash_table_free (abfd);
4401 return NULL;
4402 }
4403
4404 /* And the branch hash table. */
4405 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4406 sizeof (struct ppc_branch_hash_entry)))
4407 {
4408 bfd_hash_table_free (&htab->stub_hash_table);
4409 _bfd_elf_link_hash_table_free (abfd);
4410 return NULL;
4411 }
4412
4413 htab->tocsave_htab = htab_try_create (1024,
4414 tocsave_htab_hash,
4415 tocsave_htab_eq,
4416 NULL);
4417 if (htab->tocsave_htab == NULL)
4418 {
4419 ppc64_elf_link_hash_table_free (abfd);
4420 return NULL;
4421 }
4422 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4423
4424 /* Initializing two fields of the union is just cosmetic. We really
4425 only care about glist, but when compiled on a 32-bit host the
4426 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4427 debugger inspection of these fields look nicer. */
4428 htab->elf.init_got_refcount.refcount = 0;
4429 htab->elf.init_got_refcount.glist = NULL;
4430 htab->elf.init_plt_refcount.refcount = 0;
4431 htab->elf.init_plt_refcount.glist = NULL;
4432 htab->elf.init_got_offset.offset = 0;
4433 htab->elf.init_got_offset.glist = NULL;
4434 htab->elf.init_plt_offset.offset = 0;
4435 htab->elf.init_plt_offset.glist = NULL;
4436
4437 return &htab->elf.root;
4438 }
4439
4440 /* Create sections for linker generated code. */
4441
4442 static bfd_boolean
4443 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4444 {
4445 struct ppc_link_hash_table *htab;
4446 flagword flags;
4447
4448 htab = ppc_hash_table (info);
4449
4450 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4451 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4452 if (htab->params->save_restore_funcs)
4453 {
4454 /* Create .sfpr for code to save and restore fp regs. */
4455 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4456 flags);
4457 if (htab->sfpr == NULL
4458 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4459 return FALSE;
4460 }
4461
4462 if (bfd_link_relocatable (info))
4463 return TRUE;
4464
4465 /* Create .glink for lazy dynamic linking support. */
4466 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4467 flags);
4468 if (htab->glink == NULL
4469 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4470 return FALSE;
4471
4472 /* The part of .glink used by global entry stubs, separate so that
4473 it can be aligned appropriately without affecting htab->glink. */
4474 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4475 flags);
4476 if (htab->global_entry == NULL
4477 || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4478 return FALSE;
4479
4480 if (!info->no_ld_generated_unwind_info)
4481 {
4482 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4483 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4484 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4485 ".eh_frame",
4486 flags);
4487 if (htab->glink_eh_frame == NULL
4488 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4489 return FALSE;
4490 }
4491
4492 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4493 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4494 if (htab->elf.iplt == NULL
4495 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4496 return FALSE;
4497
4498 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4499 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4500 htab->elf.irelplt
4501 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4502 if (htab->elf.irelplt == NULL
4503 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4504 return FALSE;
4505
4506 /* Create branch lookup table for plt_branch stubs. */
4507 flags = (SEC_ALLOC | SEC_LOAD
4508 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4509 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4510 flags);
4511 if (htab->brlt == NULL
4512 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4513 return FALSE;
4514
4515 /* Local plt entries, put in .branch_lt but a separate section for
4516 convenience. */
4517 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4518 flags);
4519 if (htab->pltlocal == NULL
4520 || ! bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
4521 return FALSE;
4522
4523 if (!bfd_link_pic (info))
4524 return TRUE;
4525
4526 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4527 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4528 htab->relbrlt
4529 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4530 if (htab->relbrlt == NULL
4531 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4532 return FALSE;
4533
4534 htab->relpltlocal
4535 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4536 if (htab->relpltlocal == NULL
4537 || ! bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
4538 return FALSE;
4539
4540 return TRUE;
4541 }
4542
4543 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4544
4545 bfd_boolean
4546 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4547 struct ppc64_elf_params *params)
4548 {
4549 struct ppc_link_hash_table *htab;
4550
4551 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4552
4553 /* Always hook our dynamic sections into the first bfd, which is the
4554 linker created stub bfd. This ensures that the GOT header is at
4555 the start of the output TOC section. */
4556 htab = ppc_hash_table (info);
4557 htab->elf.dynobj = params->stub_bfd;
4558 htab->params = params;
4559
4560 return create_linkage_sections (htab->elf.dynobj, info);
4561 }
4562
4563 /* Build a name for an entry in the stub hash table. */
4564
4565 static char *
4566 ppc_stub_name (const asection *input_section,
4567 const asection *sym_sec,
4568 const struct ppc_link_hash_entry *h,
4569 const Elf_Internal_Rela *rel)
4570 {
4571 char *stub_name;
4572 ssize_t len;
4573
4574 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4575 offsets from a sym as a branch target? In fact, we could
4576 probably assume the addend is always zero. */
4577 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4578
4579 if (h)
4580 {
4581 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4582 stub_name = bfd_malloc (len);
4583 if (stub_name == NULL)
4584 return stub_name;
4585
4586 len = sprintf (stub_name, "%08x.%s+%x",
4587 input_section->id & 0xffffffff,
4588 h->elf.root.root.string,
4589 (int) rel->r_addend & 0xffffffff);
4590 }
4591 else
4592 {
4593 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4594 stub_name = bfd_malloc (len);
4595 if (stub_name == NULL)
4596 return stub_name;
4597
4598 len = sprintf (stub_name, "%08x.%x:%x+%x",
4599 input_section->id & 0xffffffff,
4600 sym_sec->id & 0xffffffff,
4601 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4602 (int) rel->r_addend & 0xffffffff);
4603 }
4604 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4605 stub_name[len - 2] = 0;
4606 return stub_name;
4607 }
4608
4609 /* Look up an entry in the stub hash. Stub entries are cached because
4610 creating the stub name takes a bit of time. */
4611
4612 static struct ppc_stub_hash_entry *
4613 ppc_get_stub_entry (const asection *input_section,
4614 const asection *sym_sec,
4615 struct ppc_link_hash_entry *h,
4616 const Elf_Internal_Rela *rel,
4617 struct ppc_link_hash_table *htab)
4618 {
4619 struct ppc_stub_hash_entry *stub_entry;
4620 struct map_stub *group;
4621
4622 /* If this input section is part of a group of sections sharing one
4623 stub section, then use the id of the first section in the group.
4624 Stub names need to include a section id, as there may well be
4625 more than one stub used to reach say, printf, and we need to
4626 distinguish between them. */
4627 group = htab->sec_info[input_section->id].u.group;
4628 if (group == NULL)
4629 return NULL;
4630
4631 if (h != NULL && h->u.stub_cache != NULL
4632 && h->u.stub_cache->h == h
4633 && h->u.stub_cache->group == group)
4634 {
4635 stub_entry = h->u.stub_cache;
4636 }
4637 else
4638 {
4639 char *stub_name;
4640
4641 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4642 if (stub_name == NULL)
4643 return NULL;
4644
4645 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4646 stub_name, FALSE, FALSE);
4647 if (h != NULL)
4648 h->u.stub_cache = stub_entry;
4649
4650 free (stub_name);
4651 }
4652
4653 return stub_entry;
4654 }
4655
4656 /* Add a new stub entry to the stub hash. Not all fields of the new
4657 stub entry are initialised. */
4658
4659 static struct ppc_stub_hash_entry *
4660 ppc_add_stub (const char *stub_name,
4661 asection *section,
4662 struct bfd_link_info *info)
4663 {
4664 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4665 struct map_stub *group;
4666 asection *link_sec;
4667 asection *stub_sec;
4668 struct ppc_stub_hash_entry *stub_entry;
4669
4670 group = htab->sec_info[section->id].u.group;
4671 link_sec = group->link_sec;
4672 stub_sec = group->stub_sec;
4673 if (stub_sec == NULL)
4674 {
4675 size_t namelen;
4676 bfd_size_type len;
4677 char *s_name;
4678
4679 namelen = strlen (link_sec->name);
4680 len = namelen + sizeof (STUB_SUFFIX);
4681 s_name = bfd_alloc (htab->params->stub_bfd, len);
4682 if (s_name == NULL)
4683 return NULL;
4684
4685 memcpy (s_name, link_sec->name, namelen);
4686 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4687 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4688 if (stub_sec == NULL)
4689 return NULL;
4690 group->stub_sec = stub_sec;
4691 }
4692
4693 /* Enter this entry into the linker stub hash table. */
4694 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4695 TRUE, FALSE);
4696 if (stub_entry == NULL)
4697 {
4698 /* xgettext:c-format */
4699 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4700 section->owner, stub_name);
4701 return NULL;
4702 }
4703
4704 stub_entry->group = group;
4705 stub_entry->stub_offset = 0;
4706 return stub_entry;
4707 }
4708
4709 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4710 not already done. */
4711
4712 static bfd_boolean
4713 create_got_section (bfd *abfd, struct bfd_link_info *info)
4714 {
4715 asection *got, *relgot;
4716 flagword flags;
4717 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4718
4719 if (!is_ppc64_elf (abfd))
4720 return FALSE;
4721 if (htab == NULL)
4722 return FALSE;
4723
4724 if (!htab->elf.sgot
4725 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4726 return FALSE;
4727
4728 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4729 | SEC_LINKER_CREATED);
4730
4731 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4732 if (!got
4733 || !bfd_set_section_alignment (abfd, got, 3))
4734 return FALSE;
4735
4736 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4737 flags | SEC_READONLY);
4738 if (!relgot
4739 || ! bfd_set_section_alignment (abfd, relgot, 3))
4740 return FALSE;
4741
4742 ppc64_elf_tdata (abfd)->got = got;
4743 ppc64_elf_tdata (abfd)->relgot = relgot;
4744 return TRUE;
4745 }
4746
4747 /* Follow indirect and warning symbol links. */
4748
4749 static inline struct bfd_link_hash_entry *
4750 follow_link (struct bfd_link_hash_entry *h)
4751 {
4752 while (h->type == bfd_link_hash_indirect
4753 || h->type == bfd_link_hash_warning)
4754 h = h->u.i.link;
4755 return h;
4756 }
4757
4758 static inline struct elf_link_hash_entry *
4759 elf_follow_link (struct elf_link_hash_entry *h)
4760 {
4761 return (struct elf_link_hash_entry *) follow_link (&h->root);
4762 }
4763
4764 static inline struct ppc_link_hash_entry *
4765 ppc_follow_link (struct ppc_link_hash_entry *h)
4766 {
4767 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4768 }
4769
4770 /* Merge PLT info on FROM with that on TO. */
4771
4772 static void
4773 move_plt_plist (struct ppc_link_hash_entry *from,
4774 struct ppc_link_hash_entry *to)
4775 {
4776 if (from->elf.plt.plist != NULL)
4777 {
4778 if (to->elf.plt.plist != NULL)
4779 {
4780 struct plt_entry **entp;
4781 struct plt_entry *ent;
4782
4783 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4784 {
4785 struct plt_entry *dent;
4786
4787 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4788 if (dent->addend == ent->addend)
4789 {
4790 dent->plt.refcount += ent->plt.refcount;
4791 *entp = ent->next;
4792 break;
4793 }
4794 if (dent == NULL)
4795 entp = &ent->next;
4796 }
4797 *entp = to->elf.plt.plist;
4798 }
4799
4800 to->elf.plt.plist = from->elf.plt.plist;
4801 from->elf.plt.plist = NULL;
4802 }
4803 }
4804
4805 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4806
4807 static void
4808 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4809 struct elf_link_hash_entry *dir,
4810 struct elf_link_hash_entry *ind)
4811 {
4812 struct ppc_link_hash_entry *edir, *eind;
4813
4814 edir = (struct ppc_link_hash_entry *) dir;
4815 eind = (struct ppc_link_hash_entry *) ind;
4816
4817 edir->is_func |= eind->is_func;
4818 edir->is_func_descriptor |= eind->is_func_descriptor;
4819 edir->tls_mask |= eind->tls_mask;
4820 if (eind->oh != NULL)
4821 edir->oh = ppc_follow_link (eind->oh);
4822
4823 if (edir->elf.versioned != versioned_hidden)
4824 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4825 edir->elf.ref_regular |= eind->elf.ref_regular;
4826 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4827 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4828 edir->elf.needs_plt |= eind->elf.needs_plt;
4829 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4830
4831 /* If we were called to copy over info for a weak sym, don't copy
4832 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4833 in order to simplify readonly_dynrelocs and save a field in the
4834 symbol hash entry, but that means dyn_relocs can't be used in any
4835 tests about a specific symbol, or affect other symbol flags which
4836 are then tested. */
4837 if (eind->elf.root.type != bfd_link_hash_indirect)
4838 return;
4839
4840 /* Copy over any dynamic relocs we may have on the indirect sym. */
4841 if (eind->dyn_relocs != NULL)
4842 {
4843 if (edir->dyn_relocs != NULL)
4844 {
4845 struct elf_dyn_relocs **pp;
4846 struct elf_dyn_relocs *p;
4847
4848 /* Add reloc counts against the indirect sym to the direct sym
4849 list. Merge any entries against the same section. */
4850 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4851 {
4852 struct elf_dyn_relocs *q;
4853
4854 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4855 if (q->sec == p->sec)
4856 {
4857 q->pc_count += p->pc_count;
4858 q->count += p->count;
4859 *pp = p->next;
4860 break;
4861 }
4862 if (q == NULL)
4863 pp = &p->next;
4864 }
4865 *pp = edir->dyn_relocs;
4866 }
4867
4868 edir->dyn_relocs = eind->dyn_relocs;
4869 eind->dyn_relocs = NULL;
4870 }
4871
4872 /* Copy over got entries that we may have already seen to the
4873 symbol which just became indirect. */
4874 if (eind->elf.got.glist != NULL)
4875 {
4876 if (edir->elf.got.glist != NULL)
4877 {
4878 struct got_entry **entp;
4879 struct got_entry *ent;
4880
4881 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4882 {
4883 struct got_entry *dent;
4884
4885 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4886 if (dent->addend == ent->addend
4887 && dent->owner == ent->owner
4888 && dent->tls_type == ent->tls_type)
4889 {
4890 dent->got.refcount += ent->got.refcount;
4891 *entp = ent->next;
4892 break;
4893 }
4894 if (dent == NULL)
4895 entp = &ent->next;
4896 }
4897 *entp = edir->elf.got.glist;
4898 }
4899
4900 edir->elf.got.glist = eind->elf.got.glist;
4901 eind->elf.got.glist = NULL;
4902 }
4903
4904 /* And plt entries. */
4905 move_plt_plist (eind, edir);
4906
4907 if (eind->elf.dynindx != -1)
4908 {
4909 if (edir->elf.dynindx != -1)
4910 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4911 edir->elf.dynstr_index);
4912 edir->elf.dynindx = eind->elf.dynindx;
4913 edir->elf.dynstr_index = eind->elf.dynstr_index;
4914 eind->elf.dynindx = -1;
4915 eind->elf.dynstr_index = 0;
4916 }
4917 }
4918
4919 /* Find the function descriptor hash entry from the given function code
4920 hash entry FH. Link the entries via their OH fields. */
4921
4922 static struct ppc_link_hash_entry *
4923 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4924 {
4925 struct ppc_link_hash_entry *fdh = fh->oh;
4926
4927 if (fdh == NULL)
4928 {
4929 const char *fd_name = fh->elf.root.root.string + 1;
4930
4931 fdh = (struct ppc_link_hash_entry *)
4932 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4933 if (fdh == NULL)
4934 return fdh;
4935
4936 fdh->is_func_descriptor = 1;
4937 fdh->oh = fh;
4938 fh->is_func = 1;
4939 fh->oh = fdh;
4940 }
4941
4942 fdh = ppc_follow_link (fdh);
4943 fdh->is_func_descriptor = 1;
4944 fdh->oh = fh;
4945 return fdh;
4946 }
4947
4948 /* Make a fake function descriptor sym for the undefined code sym FH. */
4949
4950 static struct ppc_link_hash_entry *
4951 make_fdh (struct bfd_link_info *info,
4952 struct ppc_link_hash_entry *fh)
4953 {
4954 bfd *abfd = fh->elf.root.u.undef.abfd;
4955 struct bfd_link_hash_entry *bh = NULL;
4956 struct ppc_link_hash_entry *fdh;
4957 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4958 ? BSF_WEAK
4959 : BSF_GLOBAL);
4960
4961 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4962 fh->elf.root.root.string + 1,
4963 flags, bfd_und_section_ptr, 0,
4964 NULL, FALSE, FALSE, &bh))
4965 return NULL;
4966
4967 fdh = (struct ppc_link_hash_entry *) bh;
4968 fdh->elf.non_elf = 0;
4969 fdh->fake = 1;
4970 fdh->is_func_descriptor = 1;
4971 fdh->oh = fh;
4972 fh->is_func = 1;
4973 fh->oh = fdh;
4974 return fdh;
4975 }
4976
4977 /* Fix function descriptor symbols defined in .opd sections to be
4978 function type. */
4979
4980 static bfd_boolean
4981 ppc64_elf_add_symbol_hook (bfd *ibfd,
4982 struct bfd_link_info *info,
4983 Elf_Internal_Sym *isym,
4984 const char **name,
4985 flagword *flags ATTRIBUTE_UNUSED,
4986 asection **sec,
4987 bfd_vma *value)
4988 {
4989 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4990 && (ibfd->flags & DYNAMIC) == 0
4991 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4992 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
4993
4994 if (*sec != NULL
4995 && strcmp ((*sec)->name, ".opd") == 0)
4996 {
4997 asection *code_sec;
4998
4999 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5000 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
5001 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
5002
5003 /* If the symbol is a function defined in .opd, and the function
5004 code is in a discarded group, let it appear to be undefined. */
5005 if (!bfd_link_relocatable (info)
5006 && (*sec)->reloc_count != 0
5007 && opd_entry_value (*sec, *value, &code_sec, NULL,
5008 FALSE) != (bfd_vma) -1
5009 && discarded_section (code_sec))
5010 {
5011 *sec = bfd_und_section_ptr;
5012 isym->st_shndx = SHN_UNDEF;
5013 }
5014 }
5015 else if (*sec != NULL
5016 && strcmp ((*sec)->name, ".toc") == 0
5017 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
5018 {
5019 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5020 if (htab != NULL)
5021 htab->params->object_in_toc = 1;
5022 }
5023
5024 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5025 {
5026 if (abiversion (ibfd) == 0)
5027 set_abiversion (ibfd, 2);
5028 else if (abiversion (ibfd) == 1)
5029 {
5030 _bfd_error_handler (_("symbol '%s' has invalid st_other"
5031 " for ABI version 1"), *name);
5032 bfd_set_error (bfd_error_bad_value);
5033 return FALSE;
5034 }
5035 }
5036
5037 return TRUE;
5038 }
5039
5040 /* Merge non-visibility st_other attributes: local entry point. */
5041
5042 static void
5043 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5044 const Elf_Internal_Sym *isym,
5045 bfd_boolean definition,
5046 bfd_boolean dynamic)
5047 {
5048 if (definition && (!dynamic || !h->def_regular))
5049 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5050 | ELF_ST_VISIBILITY (h->other));
5051 }
5052
5053 /* Hook called on merging a symbol. We use this to clear "fake" since
5054 we now have a real symbol. */
5055
5056 static bfd_boolean
5057 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5058 const Elf_Internal_Sym *isym,
5059 asection **psec ATTRIBUTE_UNUSED,
5060 bfd_boolean newdef ATTRIBUTE_UNUSED,
5061 bfd_boolean olddef ATTRIBUTE_UNUSED,
5062 bfd *oldbfd ATTRIBUTE_UNUSED,
5063 const asection *oldsec ATTRIBUTE_UNUSED)
5064 {
5065 ((struct ppc_link_hash_entry *) h)->fake = 0;
5066 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5067 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
5068 return TRUE;
5069 }
5070
5071 /* This function makes an old ABI object reference to ".bar" cause the
5072 inclusion of a new ABI object archive that defines "bar".
5073 NAME is a symbol defined in an archive. Return a symbol in the hash
5074 table that might be satisfied by the archive symbols. */
5075
5076 static struct elf_link_hash_entry *
5077 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5078 struct bfd_link_info *info,
5079 const char *name)
5080 {
5081 struct elf_link_hash_entry *h;
5082 char *dot_name;
5083 size_t len;
5084
5085 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5086 if (h != NULL
5087 /* Don't return this sym if it is a fake function descriptor
5088 created by add_symbol_adjust. */
5089 && !((struct ppc_link_hash_entry *) h)->fake)
5090 return h;
5091
5092 if (name[0] == '.')
5093 return h;
5094
5095 len = strlen (name);
5096 dot_name = bfd_alloc (abfd, len + 2);
5097 if (dot_name == NULL)
5098 return (struct elf_link_hash_entry *) -1;
5099 dot_name[0] = '.';
5100 memcpy (dot_name + 1, name, len + 1);
5101 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5102 bfd_release (abfd, dot_name);
5103 return h;
5104 }
5105
5106 /* This function satisfies all old ABI object references to ".bar" if a
5107 new ABI object defines "bar". Well, at least, undefined dot symbols
5108 are made weak. This stops later archive searches from including an
5109 object if we already have a function descriptor definition. It also
5110 prevents the linker complaining about undefined symbols.
5111 We also check and correct mismatched symbol visibility here. The
5112 most restrictive visibility of the function descriptor and the
5113 function entry symbol is used. */
5114
5115 static bfd_boolean
5116 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5117 {
5118 struct ppc_link_hash_table *htab;
5119 struct ppc_link_hash_entry *fdh;
5120
5121 if (eh->elf.root.type == bfd_link_hash_warning)
5122 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5123
5124 if (eh->elf.root.type == bfd_link_hash_indirect)
5125 return TRUE;
5126
5127 if (eh->elf.root.root.string[0] != '.')
5128 abort ();
5129
5130 htab = ppc_hash_table (info);
5131 if (htab == NULL)
5132 return FALSE;
5133
5134 fdh = lookup_fdh (eh, htab);
5135 if (fdh == NULL
5136 && !bfd_link_relocatable (info)
5137 && (eh->elf.root.type == bfd_link_hash_undefined
5138 || eh->elf.root.type == bfd_link_hash_undefweak)
5139 && eh->elf.ref_regular)
5140 {
5141 /* Make an undefined function descriptor sym, in order to
5142 pull in an --as-needed shared lib. Archives are handled
5143 elsewhere. */
5144 fdh = make_fdh (info, eh);
5145 if (fdh == NULL)
5146 return FALSE;
5147 }
5148
5149 if (fdh != NULL)
5150 {
5151 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5152 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5153
5154 /* Make both descriptor and entry symbol have the most
5155 constraining visibility of either symbol. */
5156 if (entry_vis < descr_vis)
5157 fdh->elf.other += entry_vis - descr_vis;
5158 else if (entry_vis > descr_vis)
5159 eh->elf.other += descr_vis - entry_vis;
5160
5161 /* Propagate reference flags from entry symbol to function
5162 descriptor symbol. */
5163 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
5164 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
5165 fdh->elf.ref_regular |= eh->elf.ref_regular;
5166 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5167
5168 if (!fdh->elf.forced_local
5169 && fdh->elf.dynindx == -1
5170 && fdh->elf.versioned != versioned_hidden
5171 && (bfd_link_dll (info)
5172 || fdh->elf.def_dynamic
5173 || fdh->elf.ref_dynamic)
5174 && (eh->elf.ref_regular
5175 || eh->elf.def_regular))
5176 {
5177 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5178 return FALSE;
5179 }
5180 }
5181
5182 return TRUE;
5183 }
5184
5185 /* Set up opd section info and abiversion for IBFD, and process list
5186 of dot-symbols we made in link_hash_newfunc. */
5187
5188 static bfd_boolean
5189 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5190 {
5191 struct ppc_link_hash_table *htab;
5192 struct ppc_link_hash_entry **p, *eh;
5193 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5194
5195 if (opd != NULL && opd->size != 0)
5196 {
5197 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5198 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5199
5200 if (abiversion (ibfd) == 0)
5201 set_abiversion (ibfd, 1);
5202 else if (abiversion (ibfd) >= 2)
5203 {
5204 /* xgettext:c-format */
5205 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5206 ibfd, abiversion (ibfd));
5207 bfd_set_error (bfd_error_bad_value);
5208 return FALSE;
5209 }
5210 }
5211
5212 if (is_ppc64_elf (info->output_bfd))
5213 {
5214 /* For input files without an explicit abiversion in e_flags
5215 we should have flagged any with symbol st_other bits set
5216 as ELFv1 and above flagged those with .opd as ELFv2.
5217 Set the output abiversion if not yet set, and for any input
5218 still ambiguous, take its abiversion from the output.
5219 Differences in ABI are reported later. */
5220 if (abiversion (info->output_bfd) == 0)
5221 set_abiversion (info->output_bfd, abiversion (ibfd));
5222 else if (abiversion (ibfd) == 0)
5223 set_abiversion (ibfd, abiversion (info->output_bfd));
5224 }
5225
5226 htab = ppc_hash_table (info);
5227 if (htab == NULL)
5228 return TRUE;
5229
5230 if (opd != NULL && opd->size != 0
5231 && (ibfd->flags & DYNAMIC) == 0
5232 && (opd->flags & SEC_RELOC) != 0
5233 && opd->reloc_count != 0
5234 && !bfd_is_abs_section (opd->output_section)
5235 && info->gc_sections)
5236 {
5237 /* Garbage collection needs some extra help with .opd sections.
5238 We don't want to necessarily keep everything referenced by
5239 relocs in .opd, as that would keep all functions. Instead,
5240 if we reference an .opd symbol (a function descriptor), we
5241 want to keep the function code symbol's section. This is
5242 easy for global symbols, but for local syms we need to keep
5243 information about the associated function section. */
5244 bfd_size_type amt;
5245 asection **opd_sym_map;
5246 Elf_Internal_Shdr *symtab_hdr;
5247 Elf_Internal_Rela *relocs, *rel_end, *rel;
5248
5249 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5250 opd_sym_map = bfd_zalloc (ibfd, amt);
5251 if (opd_sym_map == NULL)
5252 return FALSE;
5253 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5254 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5255 info->keep_memory);
5256 if (relocs == NULL)
5257 return FALSE;
5258 symtab_hdr = &elf_symtab_hdr (ibfd);
5259 rel_end = relocs + opd->reloc_count - 1;
5260 for (rel = relocs; rel < rel_end; rel++)
5261 {
5262 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5263 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5264
5265 if (r_type == R_PPC64_ADDR64
5266 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5267 && r_symndx < symtab_hdr->sh_info)
5268 {
5269 Elf_Internal_Sym *isym;
5270 asection *s;
5271
5272 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5273 if (isym == NULL)
5274 {
5275 if (elf_section_data (opd)->relocs != relocs)
5276 free (relocs);
5277 return FALSE;
5278 }
5279
5280 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5281 if (s != NULL && s != opd)
5282 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5283 }
5284 }
5285 if (elf_section_data (opd)->relocs != relocs)
5286 free (relocs);
5287 }
5288
5289 p = &htab->dot_syms;
5290 while ((eh = *p) != NULL)
5291 {
5292 *p = NULL;
5293 if (&eh->elf == htab->elf.hgot)
5294 ;
5295 else if (htab->elf.hgot == NULL
5296 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5297 htab->elf.hgot = &eh->elf;
5298 else if (abiversion (ibfd) <= 1)
5299 {
5300 htab->need_func_desc_adj = 1;
5301 if (!add_symbol_adjust (eh, info))
5302 return FALSE;
5303 }
5304 p = &eh->u.next_dot_sym;
5305 }
5306 return TRUE;
5307 }
5308
5309 /* Undo hash table changes when an --as-needed input file is determined
5310 not to be needed. */
5311
5312 static bfd_boolean
5313 ppc64_elf_notice_as_needed (bfd *ibfd,
5314 struct bfd_link_info *info,
5315 enum notice_asneeded_action act)
5316 {
5317 if (act == notice_not_needed)
5318 {
5319 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5320
5321 if (htab == NULL)
5322 return FALSE;
5323
5324 htab->dot_syms = NULL;
5325 }
5326 return _bfd_elf_notice_as_needed (ibfd, info, act);
5327 }
5328
5329 /* If --just-symbols against a final linked binary, then assume we need
5330 toc adjusting stubs when calling functions defined there. */
5331
5332 static void
5333 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5334 {
5335 if ((sec->flags & SEC_CODE) != 0
5336 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5337 && is_ppc64_elf (sec->owner))
5338 {
5339 if (abiversion (sec->owner) >= 2
5340 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5341 sec->has_toc_reloc = 1;
5342 }
5343 _bfd_elf_link_just_syms (sec, info);
5344 }
5345
5346 static struct plt_entry **
5347 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5348 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5349 {
5350 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5351 struct plt_entry **local_plt;
5352 unsigned char *local_got_tls_masks;
5353
5354 if (local_got_ents == NULL)
5355 {
5356 bfd_size_type size = symtab_hdr->sh_info;
5357
5358 size *= (sizeof (*local_got_ents)
5359 + sizeof (*local_plt)
5360 + sizeof (*local_got_tls_masks));
5361 local_got_ents = bfd_zalloc (abfd, size);
5362 if (local_got_ents == NULL)
5363 return NULL;
5364 elf_local_got_ents (abfd) = local_got_ents;
5365 }
5366
5367 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
5368 {
5369 struct got_entry *ent;
5370
5371 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5372 if (ent->addend == r_addend
5373 && ent->owner == abfd
5374 && ent->tls_type == tls_type)
5375 break;
5376 if (ent == NULL)
5377 {
5378 bfd_size_type amt = sizeof (*ent);
5379 ent = bfd_alloc (abfd, amt);
5380 if (ent == NULL)
5381 return FALSE;
5382 ent->next = local_got_ents[r_symndx];
5383 ent->addend = r_addend;
5384 ent->owner = abfd;
5385 ent->tls_type = tls_type;
5386 ent->is_indirect = FALSE;
5387 ent->got.refcount = 0;
5388 local_got_ents[r_symndx] = ent;
5389 }
5390 ent->got.refcount += 1;
5391 }
5392
5393 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5394 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5395 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
5396
5397 return local_plt + r_symndx;
5398 }
5399
5400 static bfd_boolean
5401 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5402 {
5403 struct plt_entry *ent;
5404
5405 for (ent = *plist; ent != NULL; ent = ent->next)
5406 if (ent->addend == addend)
5407 break;
5408 if (ent == NULL)
5409 {
5410 bfd_size_type amt = sizeof (*ent);
5411 ent = bfd_alloc (abfd, amt);
5412 if (ent == NULL)
5413 return FALSE;
5414 ent->next = *plist;
5415 ent->addend = addend;
5416 ent->plt.refcount = 0;
5417 *plist = ent;
5418 }
5419 ent->plt.refcount += 1;
5420 return TRUE;
5421 }
5422
5423 static bfd_boolean
5424 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5425 {
5426 return (r_type == R_PPC64_REL24
5427 || r_type == R_PPC64_REL14
5428 || r_type == R_PPC64_REL14_BRTAKEN
5429 || r_type == R_PPC64_REL14_BRNTAKEN
5430 || r_type == R_PPC64_ADDR24
5431 || r_type == R_PPC64_ADDR14
5432 || r_type == R_PPC64_ADDR14_BRTAKEN
5433 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5434 }
5435
5436 /* Look through the relocs for a section during the first phase, and
5437 calculate needed space in the global offset table, procedure
5438 linkage table, and dynamic reloc sections. */
5439
5440 static bfd_boolean
5441 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5442 asection *sec, const Elf_Internal_Rela *relocs)
5443 {
5444 struct ppc_link_hash_table *htab;
5445 Elf_Internal_Shdr *symtab_hdr;
5446 struct elf_link_hash_entry **sym_hashes;
5447 const Elf_Internal_Rela *rel;
5448 const Elf_Internal_Rela *rel_end;
5449 asection *sreloc;
5450 struct elf_link_hash_entry *tga, *dottga;
5451 bfd_boolean is_opd;
5452
5453 if (bfd_link_relocatable (info))
5454 return TRUE;
5455
5456 /* Don't do anything special with non-loaded, non-alloced sections.
5457 In particular, any relocs in such sections should not affect GOT
5458 and PLT reference counting (ie. we don't allow them to create GOT
5459 or PLT entries), there's no possibility or desire to optimize TLS
5460 relocs, and there's not much point in propagating relocs to shared
5461 libs that the dynamic linker won't relocate. */
5462 if ((sec->flags & SEC_ALLOC) == 0)
5463 return TRUE;
5464
5465 BFD_ASSERT (is_ppc64_elf (abfd));
5466
5467 htab = ppc_hash_table (info);
5468 if (htab == NULL)
5469 return FALSE;
5470
5471 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5472 FALSE, FALSE, TRUE);
5473 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5474 FALSE, FALSE, TRUE);
5475 symtab_hdr = &elf_symtab_hdr (abfd);
5476 sym_hashes = elf_sym_hashes (abfd);
5477 sreloc = NULL;
5478 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5479 rel_end = relocs + sec->reloc_count;
5480 for (rel = relocs; rel < rel_end; rel++)
5481 {
5482 unsigned long r_symndx;
5483 struct elf_link_hash_entry *h;
5484 enum elf_ppc64_reloc_type r_type;
5485 int tls_type;
5486 struct _ppc64_elf_section_data *ppc64_sec;
5487 struct plt_entry **ifunc, **plt_list;
5488
5489 r_symndx = ELF64_R_SYM (rel->r_info);
5490 if (r_symndx < symtab_hdr->sh_info)
5491 h = NULL;
5492 else
5493 {
5494 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5495 h = elf_follow_link (h);
5496
5497 if (h == htab->elf.hgot)
5498 sec->has_toc_reloc = 1;
5499 }
5500
5501 tls_type = 0;
5502 ifunc = NULL;
5503 if (h != NULL)
5504 {
5505 if (h->type == STT_GNU_IFUNC)
5506 {
5507 h->needs_plt = 1;
5508 ifunc = &h->plt.plist;
5509 }
5510 }
5511 else
5512 {
5513 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5514 abfd, r_symndx);
5515 if (isym == NULL)
5516 return FALSE;
5517
5518 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5519 {
5520 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5521 rel->r_addend,
5522 NON_GOT | PLT_IFUNC);
5523 if (ifunc == NULL)
5524 return FALSE;
5525 }
5526 }
5527
5528 r_type = ELF64_R_TYPE (rel->r_info);
5529 switch (r_type)
5530 {
5531 case R_PPC64_TLSGD:
5532 case R_PPC64_TLSLD:
5533 /* These special tls relocs tie a call to __tls_get_addr with
5534 its parameter symbol. */
5535 if (h != NULL)
5536 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
5537 else
5538 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5539 rel->r_addend,
5540 NON_GOT | TLS_TLS | TLS_MARK))
5541 return FALSE;
5542 sec->has_tls_reloc = 1;
5543 break;
5544
5545 case R_PPC64_GOT_TLSLD16:
5546 case R_PPC64_GOT_TLSLD16_LO:
5547 case R_PPC64_GOT_TLSLD16_HI:
5548 case R_PPC64_GOT_TLSLD16_HA:
5549 tls_type = TLS_TLS | TLS_LD;
5550 goto dogottls;
5551
5552 case R_PPC64_GOT_TLSGD16:
5553 case R_PPC64_GOT_TLSGD16_LO:
5554 case R_PPC64_GOT_TLSGD16_HI:
5555 case R_PPC64_GOT_TLSGD16_HA:
5556 tls_type = TLS_TLS | TLS_GD;
5557 goto dogottls;
5558
5559 case R_PPC64_GOT_TPREL16_DS:
5560 case R_PPC64_GOT_TPREL16_LO_DS:
5561 case R_PPC64_GOT_TPREL16_HI:
5562 case R_PPC64_GOT_TPREL16_HA:
5563 if (bfd_link_dll (info))
5564 info->flags |= DF_STATIC_TLS;
5565 tls_type = TLS_TLS | TLS_TPREL;
5566 goto dogottls;
5567
5568 case R_PPC64_GOT_DTPREL16_DS:
5569 case R_PPC64_GOT_DTPREL16_LO_DS:
5570 case R_PPC64_GOT_DTPREL16_HI:
5571 case R_PPC64_GOT_DTPREL16_HA:
5572 tls_type = TLS_TLS | TLS_DTPREL;
5573 dogottls:
5574 sec->has_tls_reloc = 1;
5575 /* Fall through */
5576
5577 case R_PPC64_GOT16:
5578 case R_PPC64_GOT16_DS:
5579 case R_PPC64_GOT16_HA:
5580 case R_PPC64_GOT16_HI:
5581 case R_PPC64_GOT16_LO:
5582 case R_PPC64_GOT16_LO_DS:
5583 /* This symbol requires a global offset table entry. */
5584 sec->has_toc_reloc = 1;
5585 if (r_type == R_PPC64_GOT_TLSLD16
5586 || r_type == R_PPC64_GOT_TLSGD16
5587 || r_type == R_PPC64_GOT_TPREL16_DS
5588 || r_type == R_PPC64_GOT_DTPREL16_DS
5589 || r_type == R_PPC64_GOT16
5590 || r_type == R_PPC64_GOT16_DS)
5591 {
5592 htab->do_multi_toc = 1;
5593 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5594 }
5595
5596 if (ppc64_elf_tdata (abfd)->got == NULL
5597 && !create_got_section (abfd, info))
5598 return FALSE;
5599
5600 if (h != NULL)
5601 {
5602 struct ppc_link_hash_entry *eh;
5603 struct got_entry *ent;
5604
5605 eh = (struct ppc_link_hash_entry *) h;
5606 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5607 if (ent->addend == rel->r_addend
5608 && ent->owner == abfd
5609 && ent->tls_type == tls_type)
5610 break;
5611 if (ent == NULL)
5612 {
5613 bfd_size_type amt = sizeof (*ent);
5614 ent = bfd_alloc (abfd, amt);
5615 if (ent == NULL)
5616 return FALSE;
5617 ent->next = eh->elf.got.glist;
5618 ent->addend = rel->r_addend;
5619 ent->owner = abfd;
5620 ent->tls_type = tls_type;
5621 ent->is_indirect = FALSE;
5622 ent->got.refcount = 0;
5623 eh->elf.got.glist = ent;
5624 }
5625 ent->got.refcount += 1;
5626 eh->tls_mask |= tls_type;
5627 }
5628 else
5629 /* This is a global offset table entry for a local symbol. */
5630 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5631 rel->r_addend, tls_type))
5632 return FALSE;
5633
5634 /* We may also need a plt entry if the symbol turns out to be
5635 an ifunc. */
5636 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5637 {
5638 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5639 return FALSE;
5640 }
5641 break;
5642
5643 case R_PPC64_PLT16_HA:
5644 case R_PPC64_PLT16_HI:
5645 case R_PPC64_PLT16_LO:
5646 case R_PPC64_PLT16_LO_DS:
5647 case R_PPC64_PLT32:
5648 case R_PPC64_PLT64:
5649 /* This symbol requires a procedure linkage table entry. */
5650 plt_list = ifunc;
5651 if (h != NULL)
5652 {
5653 h->needs_plt = 1;
5654 if (h->root.root.string[0] == '.'
5655 && h->root.root.string[1] != '\0')
5656 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5657 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
5658 plt_list = &h->plt.plist;
5659 }
5660 if (plt_list == NULL)
5661 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5662 rel->r_addend,
5663 NON_GOT | PLT_KEEP);
5664 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5665 return FALSE;
5666 break;
5667
5668 /* The following relocations don't need to propagate the
5669 relocation if linking a shared object since they are
5670 section relative. */
5671 case R_PPC64_SECTOFF:
5672 case R_PPC64_SECTOFF_LO:
5673 case R_PPC64_SECTOFF_HI:
5674 case R_PPC64_SECTOFF_HA:
5675 case R_PPC64_SECTOFF_DS:
5676 case R_PPC64_SECTOFF_LO_DS:
5677 case R_PPC64_DTPREL16:
5678 case R_PPC64_DTPREL16_LO:
5679 case R_PPC64_DTPREL16_HI:
5680 case R_PPC64_DTPREL16_HA:
5681 case R_PPC64_DTPREL16_DS:
5682 case R_PPC64_DTPREL16_LO_DS:
5683 case R_PPC64_DTPREL16_HIGH:
5684 case R_PPC64_DTPREL16_HIGHA:
5685 case R_PPC64_DTPREL16_HIGHER:
5686 case R_PPC64_DTPREL16_HIGHERA:
5687 case R_PPC64_DTPREL16_HIGHEST:
5688 case R_PPC64_DTPREL16_HIGHESTA:
5689 break;
5690
5691 /* Nor do these. */
5692 case R_PPC64_REL16:
5693 case R_PPC64_REL16_LO:
5694 case R_PPC64_REL16_HI:
5695 case R_PPC64_REL16_HA:
5696 case R_PPC64_REL16DX_HA:
5697 break;
5698
5699 /* Not supported as a dynamic relocation. */
5700 case R_PPC64_ADDR64_LOCAL:
5701 if (bfd_link_pic (info))
5702 {
5703 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5704 ppc_howto_init ();
5705 /* xgettext:c-format */
5706 info->callbacks->einfo (_("%H: %s reloc unsupported "
5707 "in shared libraries and PIEs\n"),
5708 abfd, sec, rel->r_offset,
5709 ppc64_elf_howto_table[r_type]->name);
5710 bfd_set_error (bfd_error_bad_value);
5711 return FALSE;
5712 }
5713 break;
5714
5715 case R_PPC64_TOC16:
5716 case R_PPC64_TOC16_DS:
5717 htab->do_multi_toc = 1;
5718 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5719 /* Fall through. */
5720 case R_PPC64_TOC16_LO:
5721 case R_PPC64_TOC16_HI:
5722 case R_PPC64_TOC16_HA:
5723 case R_PPC64_TOC16_LO_DS:
5724 sec->has_toc_reloc = 1;
5725 break;
5726
5727 /* Marker reloc. */
5728 case R_PPC64_ENTRY:
5729 break;
5730
5731 /* This relocation describes the C++ object vtable hierarchy.
5732 Reconstruct it for later use during GC. */
5733 case R_PPC64_GNU_VTINHERIT:
5734 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5735 return FALSE;
5736 break;
5737
5738 /* This relocation describes which C++ vtable entries are actually
5739 used. Record for later use during GC. */
5740 case R_PPC64_GNU_VTENTRY:
5741 BFD_ASSERT (h != NULL);
5742 if (h != NULL
5743 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5744 return FALSE;
5745 break;
5746
5747 case R_PPC64_REL14:
5748 case R_PPC64_REL14_BRTAKEN:
5749 case R_PPC64_REL14_BRNTAKEN:
5750 {
5751 asection *dest = NULL;
5752
5753 /* Heuristic: If jumping outside our section, chances are
5754 we are going to need a stub. */
5755 if (h != NULL)
5756 {
5757 /* If the sym is weak it may be overridden later, so
5758 don't assume we know where a weak sym lives. */
5759 if (h->root.type == bfd_link_hash_defined)
5760 dest = h->root.u.def.section;
5761 }
5762 else
5763 {
5764 Elf_Internal_Sym *isym;
5765
5766 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5767 abfd, r_symndx);
5768 if (isym == NULL)
5769 return FALSE;
5770
5771 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5772 }
5773
5774 if (dest != sec)
5775 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5776 }
5777 /* Fall through. */
5778
5779 case R_PPC64_REL24:
5780 plt_list = ifunc;
5781 if (h != NULL)
5782 {
5783 h->needs_plt = 1;
5784 if (h->root.root.string[0] == '.'
5785 && h->root.root.string[1] != '\0')
5786 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5787
5788 if (h == tga || h == dottga)
5789 {
5790 sec->has_tls_reloc = 1;
5791 if (rel != relocs
5792 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5793 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5794 /* We have a new-style __tls_get_addr call with
5795 a marker reloc. */
5796 ;
5797 else
5798 /* Mark this section as having an old-style call. */
5799 sec->has_tls_get_addr_call = 1;
5800 }
5801 plt_list = &h->plt.plist;
5802 }
5803
5804 /* We may need a .plt entry if the function this reloc
5805 refers to is in a shared lib. */
5806 if (plt_list
5807 && !update_plt_info (abfd, plt_list, rel->r_addend))
5808 return FALSE;
5809 break;
5810
5811 case R_PPC64_ADDR14:
5812 case R_PPC64_ADDR14_BRNTAKEN:
5813 case R_PPC64_ADDR14_BRTAKEN:
5814 case R_PPC64_ADDR24:
5815 goto dodyn;
5816
5817 case R_PPC64_TPREL64:
5818 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5819 if (bfd_link_dll (info))
5820 info->flags |= DF_STATIC_TLS;
5821 goto dotlstoc;
5822
5823 case R_PPC64_DTPMOD64:
5824 if (rel + 1 < rel_end
5825 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5826 && rel[1].r_offset == rel->r_offset + 8)
5827 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5828 else
5829 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5830 goto dotlstoc;
5831
5832 case R_PPC64_DTPREL64:
5833 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5834 if (rel != relocs
5835 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5836 && rel[-1].r_offset == rel->r_offset - 8)
5837 /* This is the second reloc of a dtpmod, dtprel pair.
5838 Don't mark with TLS_DTPREL. */
5839 goto dodyn;
5840
5841 dotlstoc:
5842 sec->has_tls_reloc = 1;
5843 if (h != NULL)
5844 {
5845 struct ppc_link_hash_entry *eh;
5846 eh = (struct ppc_link_hash_entry *) h;
5847 eh->tls_mask |= tls_type;
5848 }
5849 else
5850 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5851 rel->r_addend, tls_type))
5852 return FALSE;
5853
5854 ppc64_sec = ppc64_elf_section_data (sec);
5855 if (ppc64_sec->sec_type != sec_toc)
5856 {
5857 bfd_size_type amt;
5858
5859 /* One extra to simplify get_tls_mask. */
5860 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5861 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5862 if (ppc64_sec->u.toc.symndx == NULL)
5863 return FALSE;
5864 amt = sec->size * sizeof (bfd_vma) / 8;
5865 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5866 if (ppc64_sec->u.toc.add == NULL)
5867 return FALSE;
5868 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5869 ppc64_sec->sec_type = sec_toc;
5870 }
5871 BFD_ASSERT (rel->r_offset % 8 == 0);
5872 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5873 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5874
5875 /* Mark the second slot of a GD or LD entry.
5876 -1 to indicate GD and -2 to indicate LD. */
5877 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5878 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5879 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5880 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5881 goto dodyn;
5882
5883 case R_PPC64_TPREL16:
5884 case R_PPC64_TPREL16_LO:
5885 case R_PPC64_TPREL16_HI:
5886 case R_PPC64_TPREL16_HA:
5887 case R_PPC64_TPREL16_DS:
5888 case R_PPC64_TPREL16_LO_DS:
5889 case R_PPC64_TPREL16_HIGH:
5890 case R_PPC64_TPREL16_HIGHA:
5891 case R_PPC64_TPREL16_HIGHER:
5892 case R_PPC64_TPREL16_HIGHERA:
5893 case R_PPC64_TPREL16_HIGHEST:
5894 case R_PPC64_TPREL16_HIGHESTA:
5895 if (bfd_link_dll (info))
5896 info->flags |= DF_STATIC_TLS;
5897 goto dodyn;
5898
5899 case R_PPC64_ADDR64:
5900 if (is_opd
5901 && rel + 1 < rel_end
5902 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5903 {
5904 if (h != NULL)
5905 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5906 }
5907 /* Fall through. */
5908
5909 case R_PPC64_ADDR16:
5910 case R_PPC64_ADDR16_DS:
5911 case R_PPC64_ADDR16_HA:
5912 case R_PPC64_ADDR16_HI:
5913 case R_PPC64_ADDR16_HIGH:
5914 case R_PPC64_ADDR16_HIGHA:
5915 case R_PPC64_ADDR16_HIGHER:
5916 case R_PPC64_ADDR16_HIGHERA:
5917 case R_PPC64_ADDR16_HIGHEST:
5918 case R_PPC64_ADDR16_HIGHESTA:
5919 case R_PPC64_ADDR16_LO:
5920 case R_PPC64_ADDR16_LO_DS:
5921 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5922 && rel->r_addend == 0)
5923 {
5924 /* We may need a .plt entry if this reloc refers to a
5925 function in a shared lib. */
5926 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5927 return FALSE;
5928 h->pointer_equality_needed = 1;
5929 }
5930 /* Fall through. */
5931
5932 case R_PPC64_REL30:
5933 case R_PPC64_REL32:
5934 case R_PPC64_REL64:
5935 case R_PPC64_ADDR32:
5936 case R_PPC64_UADDR16:
5937 case R_PPC64_UADDR32:
5938 case R_PPC64_UADDR64:
5939 case R_PPC64_TOC:
5940 if (h != NULL && !bfd_link_pic (info))
5941 /* We may need a copy reloc. */
5942 h->non_got_ref = 1;
5943
5944 /* Don't propagate .opd relocs. */
5945 if (NO_OPD_RELOCS && is_opd)
5946 break;
5947
5948 /* If we are creating a shared library, and this is a reloc
5949 against a global symbol, or a non PC relative reloc
5950 against a local symbol, then we need to copy the reloc
5951 into the shared library. However, if we are linking with
5952 -Bsymbolic, we do not need to copy a reloc against a
5953 global symbol which is defined in an object we are
5954 including in the link (i.e., DEF_REGULAR is set). At
5955 this point we have not seen all the input files, so it is
5956 possible that DEF_REGULAR is not set now but will be set
5957 later (it is never cleared). In case of a weak definition,
5958 DEF_REGULAR may be cleared later by a strong definition in
5959 a shared library. We account for that possibility below by
5960 storing information in the dyn_relocs field of the hash
5961 table entry. A similar situation occurs when creating
5962 shared libraries and symbol visibility changes render the
5963 symbol local.
5964
5965 If on the other hand, we are creating an executable, we
5966 may need to keep relocations for symbols satisfied by a
5967 dynamic library if we manage to avoid copy relocs for the
5968 symbol. */
5969 dodyn:
5970 if ((bfd_link_pic (info)
5971 && (must_be_dyn_reloc (info, r_type)
5972 || (h != NULL
5973 && (!SYMBOLIC_BIND (info, h)
5974 || h->root.type == bfd_link_hash_defweak
5975 || !h->def_regular))))
5976 || (ELIMINATE_COPY_RELOCS
5977 && !bfd_link_pic (info)
5978 && h != NULL
5979 && (h->root.type == bfd_link_hash_defweak
5980 || !h->def_regular))
5981 || (!bfd_link_pic (info)
5982 && ifunc != NULL))
5983 {
5984 /* We must copy these reloc types into the output file.
5985 Create a reloc section in dynobj and make room for
5986 this reloc. */
5987 if (sreloc == NULL)
5988 {
5989 sreloc = _bfd_elf_make_dynamic_reloc_section
5990 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5991
5992 if (sreloc == NULL)
5993 return FALSE;
5994 }
5995
5996 /* If this is a global symbol, we count the number of
5997 relocations we need for this symbol. */
5998 if (h != NULL)
5999 {
6000 struct elf_dyn_relocs *p;
6001 struct elf_dyn_relocs **head;
6002
6003 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6004 p = *head;
6005 if (p == NULL || p->sec != sec)
6006 {
6007 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6008 if (p == NULL)
6009 return FALSE;
6010 p->next = *head;
6011 *head = p;
6012 p->sec = sec;
6013 p->count = 0;
6014 p->pc_count = 0;
6015 }
6016 p->count += 1;
6017 if (!must_be_dyn_reloc (info, r_type))
6018 p->pc_count += 1;
6019 }
6020 else
6021 {
6022 /* Track dynamic relocs needed for local syms too.
6023 We really need local syms available to do this
6024 easily. Oh well. */
6025 struct ppc_dyn_relocs *p;
6026 struct ppc_dyn_relocs **head;
6027 bfd_boolean is_ifunc;
6028 asection *s;
6029 void *vpp;
6030 Elf_Internal_Sym *isym;
6031
6032 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6033 abfd, r_symndx);
6034 if (isym == NULL)
6035 return FALSE;
6036
6037 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6038 if (s == NULL)
6039 s = sec;
6040
6041 vpp = &elf_section_data (s)->local_dynrel;
6042 head = (struct ppc_dyn_relocs **) vpp;
6043 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6044 p = *head;
6045 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6046 p = p->next;
6047 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6048 {
6049 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6050 if (p == NULL)
6051 return FALSE;
6052 p->next = *head;
6053 *head = p;
6054 p->sec = sec;
6055 p->ifunc = is_ifunc;
6056 p->count = 0;
6057 }
6058 p->count += 1;
6059 }
6060 }
6061 break;
6062
6063 default:
6064 break;
6065 }
6066 }
6067
6068 return TRUE;
6069 }
6070
6071 /* Merge backend specific data from an object file to the output
6072 object file when linking. */
6073
6074 static bfd_boolean
6075 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6076 {
6077 bfd *obfd = info->output_bfd;
6078 unsigned long iflags, oflags;
6079
6080 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6081 return TRUE;
6082
6083 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6084 return TRUE;
6085
6086 if (!_bfd_generic_verify_endian_match (ibfd, info))
6087 return FALSE;
6088
6089 iflags = elf_elfheader (ibfd)->e_flags;
6090 oflags = elf_elfheader (obfd)->e_flags;
6091
6092 if (iflags & ~EF_PPC64_ABI)
6093 {
6094 _bfd_error_handler
6095 /* xgettext:c-format */
6096 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
6097 bfd_set_error (bfd_error_bad_value);
6098 return FALSE;
6099 }
6100 else if (iflags != oflags && iflags != 0)
6101 {
6102 _bfd_error_handler
6103 /* xgettext:c-format */
6104 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
6105 ibfd, iflags, oflags);
6106 bfd_set_error (bfd_error_bad_value);
6107 return FALSE;
6108 }
6109
6110 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
6111
6112 /* Merge Tag_compatibility attributes and any common GNU ones. */
6113 _bfd_elf_merge_object_attributes (ibfd, info);
6114
6115 return TRUE;
6116 }
6117
6118 static bfd_boolean
6119 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6120 {
6121 /* Print normal ELF private data. */
6122 _bfd_elf_print_private_bfd_data (abfd, ptr);
6123
6124 if (elf_elfheader (abfd)->e_flags != 0)
6125 {
6126 FILE *file = ptr;
6127
6128 fprintf (file, _("private flags = 0x%lx:"),
6129 elf_elfheader (abfd)->e_flags);
6130
6131 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6132 fprintf (file, _(" [abiv%ld]"),
6133 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6134 fputc ('\n', file);
6135 }
6136
6137 return TRUE;
6138 }
6139
6140 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
6141 of the code entry point, and its section, which must be in the same
6142 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
6143
6144 static bfd_vma
6145 opd_entry_value (asection *opd_sec,
6146 bfd_vma offset,
6147 asection **code_sec,
6148 bfd_vma *code_off,
6149 bfd_boolean in_code_sec)
6150 {
6151 bfd *opd_bfd = opd_sec->owner;
6152 Elf_Internal_Rela *relocs;
6153 Elf_Internal_Rela *lo, *hi, *look;
6154 bfd_vma val;
6155
6156 /* No relocs implies we are linking a --just-symbols object, or looking
6157 at a final linked executable with addr2line or somesuch. */
6158 if (opd_sec->reloc_count == 0)
6159 {
6160 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6161
6162 if (contents == NULL)
6163 {
6164 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6165 return (bfd_vma) -1;
6166 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6167 }
6168
6169 /* PR 17512: file: 64b9dfbb. */
6170 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6171 return (bfd_vma) -1;
6172
6173 val = bfd_get_64 (opd_bfd, contents + offset);
6174 if (code_sec != NULL)
6175 {
6176 asection *sec, *likely = NULL;
6177
6178 if (in_code_sec)
6179 {
6180 sec = *code_sec;
6181 if (sec->vma <= val
6182 && val < sec->vma + sec->size)
6183 likely = sec;
6184 else
6185 val = -1;
6186 }
6187 else
6188 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6189 if (sec->vma <= val
6190 && (sec->flags & SEC_LOAD) != 0
6191 && (sec->flags & SEC_ALLOC) != 0)
6192 likely = sec;
6193 if (likely != NULL)
6194 {
6195 *code_sec = likely;
6196 if (code_off != NULL)
6197 *code_off = val - likely->vma;
6198 }
6199 }
6200 return val;
6201 }
6202
6203 BFD_ASSERT (is_ppc64_elf (opd_bfd));
6204
6205 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6206 if (relocs == NULL)
6207 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6208 /* PR 17512: file: df8e1fd6. */
6209 if (relocs == NULL)
6210 return (bfd_vma) -1;
6211
6212 /* Go find the opd reloc at the sym address. */
6213 lo = relocs;
6214 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6215 val = (bfd_vma) -1;
6216 while (lo < hi)
6217 {
6218 look = lo + (hi - lo) / 2;
6219 if (look->r_offset < offset)
6220 lo = look + 1;
6221 else if (look->r_offset > offset)
6222 hi = look;
6223 else
6224 {
6225 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6226
6227 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6228 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6229 {
6230 unsigned long symndx = ELF64_R_SYM (look->r_info);
6231 asection *sec = NULL;
6232
6233 if (symndx >= symtab_hdr->sh_info
6234 && elf_sym_hashes (opd_bfd) != NULL)
6235 {
6236 struct elf_link_hash_entry **sym_hashes;
6237 struct elf_link_hash_entry *rh;
6238
6239 sym_hashes = elf_sym_hashes (opd_bfd);
6240 rh = sym_hashes[symndx - symtab_hdr->sh_info];
6241 if (rh != NULL)
6242 {
6243 rh = elf_follow_link (rh);
6244 if (rh->root.type != bfd_link_hash_defined
6245 && rh->root.type != bfd_link_hash_defweak)
6246 break;
6247 if (rh->root.u.def.section->owner == opd_bfd)
6248 {
6249 val = rh->root.u.def.value;
6250 sec = rh->root.u.def.section;
6251 }
6252 }
6253 }
6254
6255 if (sec == NULL)
6256 {
6257 Elf_Internal_Sym *sym;
6258
6259 if (symndx < symtab_hdr->sh_info)
6260 {
6261 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6262 if (sym == NULL)
6263 {
6264 size_t symcnt = symtab_hdr->sh_info;
6265 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6266 symcnt, 0,
6267 NULL, NULL, NULL);
6268 if (sym == NULL)
6269 break;
6270 symtab_hdr->contents = (bfd_byte *) sym;
6271 }
6272 sym += symndx;
6273 }
6274 else
6275 {
6276 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6277 1, symndx,
6278 NULL, NULL, NULL);
6279 if (sym == NULL)
6280 break;
6281 }
6282 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6283 if (sec == NULL)
6284 break;
6285 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6286 val = sym->st_value;
6287 }
6288
6289 val += look->r_addend;
6290 if (code_off != NULL)
6291 *code_off = val;
6292 if (code_sec != NULL)
6293 {
6294 if (in_code_sec && *code_sec != sec)
6295 return -1;
6296 else
6297 *code_sec = sec;
6298 }
6299 if (sec->output_section != NULL)
6300 val += sec->output_section->vma + sec->output_offset;
6301 }
6302 break;
6303 }
6304 }
6305
6306 return val;
6307 }
6308
6309 /* If the ELF symbol SYM might be a function in SEC, return the
6310 function size and set *CODE_OFF to the function's entry point,
6311 otherwise return zero. */
6312
6313 static bfd_size_type
6314 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6315 bfd_vma *code_off)
6316 {
6317 bfd_size_type size;
6318
6319 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6320 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6321 return 0;
6322
6323 size = 0;
6324 if (!(sym->flags & BSF_SYNTHETIC))
6325 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6326
6327 if (strcmp (sym->section->name, ".opd") == 0)
6328 {
6329 struct _opd_sec_data *opd = get_opd_info (sym->section);
6330 bfd_vma symval = sym->value;
6331
6332 if (opd != NULL
6333 && opd->adjust != NULL
6334 && elf_section_data (sym->section)->relocs != NULL)
6335 {
6336 /* opd_entry_value will use cached relocs that have been
6337 adjusted, but with raw symbols. That means both local
6338 and global symbols need adjusting. */
6339 long adjust = opd->adjust[OPD_NDX (symval)];
6340 if (adjust == -1)
6341 return 0;
6342 symval += adjust;
6343 }
6344
6345 if (opd_entry_value (sym->section, symval,
6346 &sec, code_off, TRUE) == (bfd_vma) -1)
6347 return 0;
6348 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6349 symbol. This size has nothing to do with the code size of the
6350 function, which is what we're supposed to return, but the
6351 code size isn't available without looking up the dot-sym.
6352 However, doing that would be a waste of time particularly
6353 since elf_find_function will look at the dot-sym anyway.
6354 Now, elf_find_function will keep the largest size of any
6355 function sym found at the code address of interest, so return
6356 1 here to avoid it incorrectly caching a larger function size
6357 for a small function. This does mean we return the wrong
6358 size for a new-ABI function of size 24, but all that does is
6359 disable caching for such functions. */
6360 if (size == 24)
6361 size = 1;
6362 }
6363 else
6364 {
6365 if (sym->section != sec)
6366 return 0;
6367 *code_off = sym->value;
6368 }
6369 if (size == 0)
6370 size = 1;
6371 return size;
6372 }
6373
6374 /* Return true if symbol is a strong function defined in an ELFv2
6375 object with st_other localentry bits of zero, ie. its local entry
6376 point coincides with its global entry point. */
6377
6378 static bfd_boolean
6379 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6380 {
6381 return (h != NULL
6382 && h->type == STT_FUNC
6383 && h->root.type == bfd_link_hash_defined
6384 && (STO_PPC64_LOCAL_MASK & h->other) == 0
6385 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
6386 && is_ppc64_elf (h->root.u.def.section->owner)
6387 && abiversion (h->root.u.def.section->owner) >= 2);
6388 }
6389
6390 /* Return true if symbol is defined in a regular object file. */
6391
6392 static bfd_boolean
6393 is_static_defined (struct elf_link_hash_entry *h)
6394 {
6395 return ((h->root.type == bfd_link_hash_defined
6396 || h->root.type == bfd_link_hash_defweak)
6397 && h->root.u.def.section != NULL
6398 && h->root.u.def.section->output_section != NULL);
6399 }
6400
6401 /* If FDH is a function descriptor symbol, return the associated code
6402 entry symbol if it is defined. Return NULL otherwise. */
6403
6404 static struct ppc_link_hash_entry *
6405 defined_code_entry (struct ppc_link_hash_entry *fdh)
6406 {
6407 if (fdh->is_func_descriptor)
6408 {
6409 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6410 if (fh->elf.root.type == bfd_link_hash_defined
6411 || fh->elf.root.type == bfd_link_hash_defweak)
6412 return fh;
6413 }
6414 return NULL;
6415 }
6416
6417 /* If FH is a function code entry symbol, return the associated
6418 function descriptor symbol if it is defined. Return NULL otherwise. */
6419
6420 static struct ppc_link_hash_entry *
6421 defined_func_desc (struct ppc_link_hash_entry *fh)
6422 {
6423 if (fh->oh != NULL
6424 && fh->oh->is_func_descriptor)
6425 {
6426 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6427 if (fdh->elf.root.type == bfd_link_hash_defined
6428 || fdh->elf.root.type == bfd_link_hash_defweak)
6429 return fdh;
6430 }
6431 return NULL;
6432 }
6433
6434 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6435
6436 /* Garbage collect sections, after first dealing with dot-symbols. */
6437
6438 static bfd_boolean
6439 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6440 {
6441 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6442
6443 if (htab != NULL && htab->need_func_desc_adj)
6444 {
6445 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6446 htab->need_func_desc_adj = 0;
6447 }
6448 return bfd_elf_gc_sections (abfd, info);
6449 }
6450
6451 /* Mark all our entry sym sections, both opd and code section. */
6452
6453 static void
6454 ppc64_elf_gc_keep (struct bfd_link_info *info)
6455 {
6456 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6457 struct bfd_sym_chain *sym;
6458
6459 if (htab == NULL)
6460 return;
6461
6462 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6463 {
6464 struct ppc_link_hash_entry *eh, *fh;
6465 asection *sec;
6466
6467 eh = (struct ppc_link_hash_entry *)
6468 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6469 if (eh == NULL)
6470 continue;
6471 if (eh->elf.root.type != bfd_link_hash_defined
6472 && eh->elf.root.type != bfd_link_hash_defweak)
6473 continue;
6474
6475 fh = defined_code_entry (eh);
6476 if (fh != NULL)
6477 {
6478 sec = fh->elf.root.u.def.section;
6479 sec->flags |= SEC_KEEP;
6480 }
6481 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6482 && opd_entry_value (eh->elf.root.u.def.section,
6483 eh->elf.root.u.def.value,
6484 &sec, NULL, FALSE) != (bfd_vma) -1)
6485 sec->flags |= SEC_KEEP;
6486
6487 sec = eh->elf.root.u.def.section;
6488 sec->flags |= SEC_KEEP;
6489 }
6490 }
6491
6492 /* Mark sections containing dynamically referenced symbols. When
6493 building shared libraries, we must assume that any visible symbol is
6494 referenced. */
6495
6496 static bfd_boolean
6497 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6498 {
6499 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6500 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6501 struct ppc_link_hash_entry *fdh;
6502 struct bfd_elf_dynamic_list *d = info->dynamic_list;
6503
6504 /* Dynamic linking info is on the func descriptor sym. */
6505 fdh = defined_func_desc (eh);
6506 if (fdh != NULL)
6507 eh = fdh;
6508
6509 if ((eh->elf.root.type == bfd_link_hash_defined
6510 || eh->elf.root.type == bfd_link_hash_defweak)
6511 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
6512 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6513 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6514 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6515 && (!bfd_link_executable (info)
6516 || info->gc_keep_exported
6517 || info->export_dynamic
6518 || (eh->elf.dynamic
6519 && d != NULL
6520 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6521 && (eh->elf.versioned >= versioned
6522 || !bfd_hide_sym_by_version (info->version_info,
6523 eh->elf.root.root.string)))))
6524 {
6525 asection *code_sec;
6526 struct ppc_link_hash_entry *fh;
6527
6528 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6529
6530 /* Function descriptor syms cause the associated
6531 function code sym section to be marked. */
6532 fh = defined_code_entry (eh);
6533 if (fh != NULL)
6534 {
6535 code_sec = fh->elf.root.u.def.section;
6536 code_sec->flags |= SEC_KEEP;
6537 }
6538 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6539 && opd_entry_value (eh->elf.root.u.def.section,
6540 eh->elf.root.u.def.value,
6541 &code_sec, NULL, FALSE) != (bfd_vma) -1)
6542 code_sec->flags |= SEC_KEEP;
6543 }
6544
6545 return TRUE;
6546 }
6547
6548 /* Return the section that should be marked against GC for a given
6549 relocation. */
6550
6551 static asection *
6552 ppc64_elf_gc_mark_hook (asection *sec,
6553 struct bfd_link_info *info,
6554 Elf_Internal_Rela *rel,
6555 struct elf_link_hash_entry *h,
6556 Elf_Internal_Sym *sym)
6557 {
6558 asection *rsec;
6559
6560 /* Syms return NULL if we're marking .opd, so we avoid marking all
6561 function sections, as all functions are referenced in .opd. */
6562 rsec = NULL;
6563 if (get_opd_info (sec) != NULL)
6564 return rsec;
6565
6566 if (h != NULL)
6567 {
6568 enum elf_ppc64_reloc_type r_type;
6569 struct ppc_link_hash_entry *eh, *fh, *fdh;
6570
6571 r_type = ELF64_R_TYPE (rel->r_info);
6572 switch (r_type)
6573 {
6574 case R_PPC64_GNU_VTINHERIT:
6575 case R_PPC64_GNU_VTENTRY:
6576 break;
6577
6578 default:
6579 switch (h->root.type)
6580 {
6581 case bfd_link_hash_defined:
6582 case bfd_link_hash_defweak:
6583 eh = (struct ppc_link_hash_entry *) h;
6584 fdh = defined_func_desc (eh);
6585 if (fdh != NULL)
6586 {
6587 /* -mcall-aixdesc code references the dot-symbol on
6588 a call reloc. Mark the function descriptor too
6589 against garbage collection. */
6590 fdh->elf.mark = 1;
6591 if (fdh->elf.is_weakalias)
6592 weakdef (&fdh->elf)->mark = 1;
6593 eh = fdh;
6594 }
6595
6596 /* Function descriptor syms cause the associated
6597 function code sym section to be marked. */
6598 fh = defined_code_entry (eh);
6599 if (fh != NULL)
6600 {
6601 /* They also mark their opd section. */
6602 eh->elf.root.u.def.section->gc_mark = 1;
6603
6604 rsec = fh->elf.root.u.def.section;
6605 }
6606 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6607 && opd_entry_value (eh->elf.root.u.def.section,
6608 eh->elf.root.u.def.value,
6609 &rsec, NULL, FALSE) != (bfd_vma) -1)
6610 eh->elf.root.u.def.section->gc_mark = 1;
6611 else
6612 rsec = h->root.u.def.section;
6613 break;
6614
6615 case bfd_link_hash_common:
6616 rsec = h->root.u.c.p->section;
6617 break;
6618
6619 default:
6620 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6621 }
6622 }
6623 }
6624 else
6625 {
6626 struct _opd_sec_data *opd;
6627
6628 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6629 opd = get_opd_info (rsec);
6630 if (opd != NULL && opd->func_sec != NULL)
6631 {
6632 rsec->gc_mark = 1;
6633
6634 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6635 }
6636 }
6637
6638 return rsec;
6639 }
6640
6641 /* The maximum size of .sfpr. */
6642 #define SFPR_MAX (218*4)
6643
6644 struct sfpr_def_parms
6645 {
6646 const char name[12];
6647 unsigned char lo, hi;
6648 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6649 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6650 };
6651
6652 /* Auto-generate _save*, _rest* functions in .sfpr.
6653 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6654 instead. */
6655
6656 static bfd_boolean
6657 sfpr_define (struct bfd_link_info *info,
6658 const struct sfpr_def_parms *parm,
6659 asection *stub_sec)
6660 {
6661 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6662 unsigned int i;
6663 size_t len = strlen (parm->name);
6664 bfd_boolean writing = FALSE;
6665 char sym[16];
6666
6667 if (htab == NULL)
6668 return FALSE;
6669
6670 memcpy (sym, parm->name, len);
6671 sym[len + 2] = 0;
6672
6673 for (i = parm->lo; i <= parm->hi; i++)
6674 {
6675 struct ppc_link_hash_entry *h;
6676
6677 sym[len + 0] = i / 10 + '0';
6678 sym[len + 1] = i % 10 + '0';
6679 h = (struct ppc_link_hash_entry *)
6680 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6681 if (stub_sec != NULL)
6682 {
6683 if (h != NULL
6684 && h->elf.root.type == bfd_link_hash_defined
6685 && h->elf.root.u.def.section == htab->sfpr)
6686 {
6687 struct elf_link_hash_entry *s;
6688 char buf[32];
6689 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6690 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6691 if (s == NULL)
6692 return FALSE;
6693 if (s->root.type == bfd_link_hash_new
6694 || (s->root.type = bfd_link_hash_defined
6695 && s->root.u.def.section == stub_sec))
6696 {
6697 s->root.type = bfd_link_hash_defined;
6698 s->root.u.def.section = stub_sec;
6699 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6700 + h->elf.root.u.def.value);
6701 s->ref_regular = 1;
6702 s->def_regular = 1;
6703 s->ref_regular_nonweak = 1;
6704 s->forced_local = 1;
6705 s->non_elf = 0;
6706 s->root.linker_def = 1;
6707 }
6708 }
6709 continue;
6710 }
6711 if (h != NULL)
6712 {
6713 h->save_res = 1;
6714 if (!h->elf.def_regular)
6715 {
6716 h->elf.root.type = bfd_link_hash_defined;
6717 h->elf.root.u.def.section = htab->sfpr;
6718 h->elf.root.u.def.value = htab->sfpr->size;
6719 h->elf.type = STT_FUNC;
6720 h->elf.def_regular = 1;
6721 h->elf.non_elf = 0;
6722 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6723 writing = TRUE;
6724 if (htab->sfpr->contents == NULL)
6725 {
6726 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6727 if (htab->sfpr->contents == NULL)
6728 return FALSE;
6729 }
6730 }
6731 }
6732 if (writing)
6733 {
6734 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6735 if (i != parm->hi)
6736 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6737 else
6738 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6739 htab->sfpr->size = p - htab->sfpr->contents;
6740 }
6741 }
6742
6743 return TRUE;
6744 }
6745
6746 static bfd_byte *
6747 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6748 {
6749 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6750 return p + 4;
6751 }
6752
6753 static bfd_byte *
6754 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6755 {
6756 p = savegpr0 (abfd, p, r);
6757 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6758 p = p + 4;
6759 bfd_put_32 (abfd, BLR, p);
6760 return p + 4;
6761 }
6762
6763 static bfd_byte *
6764 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6765 {
6766 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6767 return p + 4;
6768 }
6769
6770 static bfd_byte *
6771 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6772 {
6773 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6774 p = p + 4;
6775 p = restgpr0 (abfd, p, r);
6776 bfd_put_32 (abfd, MTLR_R0, p);
6777 p = p + 4;
6778 if (r == 29)
6779 {
6780 p = restgpr0 (abfd, p, 30);
6781 p = restgpr0 (abfd, p, 31);
6782 }
6783 bfd_put_32 (abfd, BLR, p);
6784 return p + 4;
6785 }
6786
6787 static bfd_byte *
6788 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6789 {
6790 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6791 return p + 4;
6792 }
6793
6794 static bfd_byte *
6795 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6796 {
6797 p = savegpr1 (abfd, p, r);
6798 bfd_put_32 (abfd, BLR, p);
6799 return p + 4;
6800 }
6801
6802 static bfd_byte *
6803 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6804 {
6805 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6806 return p + 4;
6807 }
6808
6809 static bfd_byte *
6810 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6811 {
6812 p = restgpr1 (abfd, p, r);
6813 bfd_put_32 (abfd, BLR, p);
6814 return p + 4;
6815 }
6816
6817 static bfd_byte *
6818 savefpr (bfd *abfd, bfd_byte *p, int r)
6819 {
6820 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6821 return p + 4;
6822 }
6823
6824 static bfd_byte *
6825 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6826 {
6827 p = savefpr (abfd, p, r);
6828 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6829 p = p + 4;
6830 bfd_put_32 (abfd, BLR, p);
6831 return p + 4;
6832 }
6833
6834 static bfd_byte *
6835 restfpr (bfd *abfd, bfd_byte *p, int r)
6836 {
6837 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6838 return p + 4;
6839 }
6840
6841 static bfd_byte *
6842 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6843 {
6844 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6845 p = p + 4;
6846 p = restfpr (abfd, p, r);
6847 bfd_put_32 (abfd, MTLR_R0, p);
6848 p = p + 4;
6849 if (r == 29)
6850 {
6851 p = restfpr (abfd, p, 30);
6852 p = restfpr (abfd, p, 31);
6853 }
6854 bfd_put_32 (abfd, BLR, p);
6855 return p + 4;
6856 }
6857
6858 static bfd_byte *
6859 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6860 {
6861 p = savefpr (abfd, p, r);
6862 bfd_put_32 (abfd, BLR, p);
6863 return p + 4;
6864 }
6865
6866 static bfd_byte *
6867 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6868 {
6869 p = restfpr (abfd, p, r);
6870 bfd_put_32 (abfd, BLR, p);
6871 return p + 4;
6872 }
6873
6874 static bfd_byte *
6875 savevr (bfd *abfd, bfd_byte *p, int r)
6876 {
6877 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6878 p = p + 4;
6879 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6880 return p + 4;
6881 }
6882
6883 static bfd_byte *
6884 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6885 {
6886 p = savevr (abfd, p, r);
6887 bfd_put_32 (abfd, BLR, p);
6888 return p + 4;
6889 }
6890
6891 static bfd_byte *
6892 restvr (bfd *abfd, bfd_byte *p, int r)
6893 {
6894 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6895 p = p + 4;
6896 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6897 return p + 4;
6898 }
6899
6900 static bfd_byte *
6901 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6902 {
6903 p = restvr (abfd, p, r);
6904 bfd_put_32 (abfd, BLR, p);
6905 return p + 4;
6906 }
6907
6908 /* Called via elf_link_hash_traverse to transfer dynamic linking
6909 information on function code symbol entries to their corresponding
6910 function descriptor symbol entries. */
6911
6912 static bfd_boolean
6913 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6914 {
6915 struct bfd_link_info *info;
6916 struct ppc_link_hash_table *htab;
6917 struct ppc_link_hash_entry *fh;
6918 struct ppc_link_hash_entry *fdh;
6919 bfd_boolean force_local;
6920
6921 fh = (struct ppc_link_hash_entry *) h;
6922 if (fh->elf.root.type == bfd_link_hash_indirect)
6923 return TRUE;
6924
6925 if (!fh->is_func)
6926 return TRUE;
6927
6928 if (fh->elf.root.root.string[0] != '.'
6929 || fh->elf.root.root.string[1] == '\0')
6930 return TRUE;
6931
6932 info = inf;
6933 htab = ppc_hash_table (info);
6934 if (htab == NULL)
6935 return FALSE;
6936
6937 /* Find the corresponding function descriptor symbol. */
6938 fdh = lookup_fdh (fh, htab);
6939
6940 /* Resolve undefined references to dot-symbols as the value
6941 in the function descriptor, if we have one in a regular object.
6942 This is to satisfy cases like ".quad .foo". Calls to functions
6943 in dynamic objects are handled elsewhere. */
6944 if ((fh->elf.root.type == bfd_link_hash_undefined
6945 || fh->elf.root.type == bfd_link_hash_undefweak)
6946 && (fdh->elf.root.type == bfd_link_hash_defined
6947 || fdh->elf.root.type == bfd_link_hash_defweak)
6948 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6949 && opd_entry_value (fdh->elf.root.u.def.section,
6950 fdh->elf.root.u.def.value,
6951 &fh->elf.root.u.def.section,
6952 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6953 {
6954 fh->elf.root.type = fdh->elf.root.type;
6955 fh->elf.forced_local = 1;
6956 fh->elf.def_regular = fdh->elf.def_regular;
6957 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6958 }
6959
6960 if (!fh->elf.dynamic)
6961 {
6962 struct plt_entry *ent;
6963
6964 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6965 if (ent->plt.refcount > 0)
6966 break;
6967 if (ent == NULL)
6968 return TRUE;
6969 }
6970
6971 /* Create a descriptor as undefined if necessary. */
6972 if (fdh == NULL
6973 && !bfd_link_executable (info)
6974 && (fh->elf.root.type == bfd_link_hash_undefined
6975 || fh->elf.root.type == bfd_link_hash_undefweak))
6976 {
6977 fdh = make_fdh (info, fh);
6978 if (fdh == NULL)
6979 return FALSE;
6980 }
6981
6982 /* We can't support overriding of symbols on a fake descriptor. */
6983 if (fdh != NULL
6984 && fdh->fake
6985 && (fh->elf.root.type == bfd_link_hash_defined
6986 || fh->elf.root.type == bfd_link_hash_defweak))
6987 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6988
6989 /* Transfer dynamic linking information to the function descriptor. */
6990 if (fdh != NULL)
6991 {
6992 fdh->elf.ref_regular |= fh->elf.ref_regular;
6993 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6994 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6995 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6996 fdh->elf.dynamic |= fh->elf.dynamic;
6997 fdh->elf.needs_plt |= (fh->elf.needs_plt
6998 || fh->elf.type == STT_FUNC
6999 || fh->elf.type == STT_GNU_IFUNC);
7000 move_plt_plist (fh, fdh);
7001
7002 if (!fdh->elf.forced_local
7003 && fh->elf.dynindx != -1)
7004 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7005 return FALSE;
7006 }
7007
7008 /* Now that the info is on the function descriptor, clear the
7009 function code sym info. Any function code syms for which we
7010 don't have a definition in a regular file, we force local.
7011 This prevents a shared library from exporting syms that have
7012 been imported from another library. Function code syms that
7013 are really in the library we must leave global to prevent the
7014 linker dragging in a definition from a static library. */
7015 force_local = (!fh->elf.def_regular
7016 || fdh == NULL
7017 || !fdh->elf.def_regular
7018 || fdh->elf.forced_local);
7019 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7020
7021 return TRUE;
7022 }
7023
7024 static const struct sfpr_def_parms save_res_funcs[] =
7025 {
7026 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7027 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7028 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7029 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7030 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7031 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7032 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7033 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7034 { "._savef", 14, 31, savefpr, savefpr1_tail },
7035 { "._restf", 14, 31, restfpr, restfpr1_tail },
7036 { "_savevr_", 20, 31, savevr, savevr_tail },
7037 { "_restvr_", 20, 31, restvr, restvr_tail }
7038 };
7039
7040 /* Called near the start of bfd_elf_size_dynamic_sections. We use
7041 this hook to a) provide some gcc support functions, and b) transfer
7042 dynamic linking information gathered so far on function code symbol
7043 entries, to their corresponding function descriptor symbol entries. */
7044
7045 static bfd_boolean
7046 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7047 struct bfd_link_info *info)
7048 {
7049 struct ppc_link_hash_table *htab;
7050
7051 htab = ppc_hash_table (info);
7052 if (htab == NULL)
7053 return FALSE;
7054
7055 /* Provide any missing _save* and _rest* functions. */
7056 if (htab->sfpr != NULL)
7057 {
7058 unsigned int i;
7059
7060 htab->sfpr->size = 0;
7061 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7062 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7063 return FALSE;
7064 if (htab->sfpr->size == 0)
7065 htab->sfpr->flags |= SEC_EXCLUDE;
7066 }
7067
7068 if (bfd_link_relocatable (info))
7069 return TRUE;
7070
7071 if (htab->elf.hgot != NULL)
7072 {
7073 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7074 /* Make .TOC. defined so as to prevent it being made dynamic.
7075 The wrong value here is fixed later in ppc64_elf_set_toc. */
7076 if (!htab->elf.hgot->def_regular
7077 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7078 {
7079 htab->elf.hgot->root.type = bfd_link_hash_defined;
7080 htab->elf.hgot->root.u.def.value = 0;
7081 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7082 htab->elf.hgot->def_regular = 1;
7083 htab->elf.hgot->root.linker_def = 1;
7084 }
7085 htab->elf.hgot->type = STT_OBJECT;
7086 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7087 | STV_HIDDEN);
7088 }
7089
7090 if (htab->need_func_desc_adj)
7091 {
7092 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7093 htab->need_func_desc_adj = 0;
7094 }
7095
7096 return TRUE;
7097 }
7098
7099 /* Find dynamic relocs for H that apply to read-only sections. */
7100
7101 static asection *
7102 readonly_dynrelocs (struct elf_link_hash_entry *h)
7103 {
7104 struct ppc_link_hash_entry *eh;
7105 struct elf_dyn_relocs *p;
7106
7107 eh = (struct ppc_link_hash_entry *) h;
7108 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7109 {
7110 asection *s = p->sec->output_section;
7111
7112 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7113 return p->sec;
7114 }
7115 return NULL;
7116 }
7117
7118 /* Return true if we have dynamic relocs against H or any of its weak
7119 aliases, that apply to read-only sections. Cannot be used after
7120 size_dynamic_sections. */
7121
7122 static bfd_boolean
7123 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7124 {
7125 struct ppc_link_hash_entry *eh;
7126
7127 eh = (struct ppc_link_hash_entry *) h;
7128 do
7129 {
7130 if (readonly_dynrelocs (&eh->elf))
7131 return TRUE;
7132 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
7133 } while (eh != NULL && &eh->elf != h);
7134
7135 return FALSE;
7136 }
7137
7138 /* Return whether EH has pc-relative dynamic relocs. */
7139
7140 static bfd_boolean
7141 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7142 {
7143 struct elf_dyn_relocs *p;
7144
7145 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7146 if (p->pc_count != 0)
7147 return TRUE;
7148 return FALSE;
7149 }
7150
7151 /* Return true if a global entry stub will be created for H. Valid
7152 for ELFv2 before plt entries have been allocated. */
7153
7154 static bfd_boolean
7155 global_entry_stub (struct elf_link_hash_entry *h)
7156 {
7157 struct plt_entry *pent;
7158
7159 if (!h->pointer_equality_needed
7160 || h->def_regular)
7161 return FALSE;
7162
7163 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7164 if (pent->plt.refcount > 0
7165 && pent->addend == 0)
7166 return TRUE;
7167
7168 return FALSE;
7169 }
7170
7171 /* Adjust a symbol defined by a dynamic object and referenced by a
7172 regular object. The current definition is in some section of the
7173 dynamic object, but we're not including those sections. We have to
7174 change the definition to something the rest of the link can
7175 understand. */
7176
7177 static bfd_boolean
7178 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7179 struct elf_link_hash_entry *h)
7180 {
7181 struct ppc_link_hash_table *htab;
7182 asection *s, *srel;
7183
7184 htab = ppc_hash_table (info);
7185 if (htab == NULL)
7186 return FALSE;
7187
7188 /* Deal with function syms. */
7189 if (h->type == STT_FUNC
7190 || h->type == STT_GNU_IFUNC
7191 || h->needs_plt)
7192 {
7193 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7194 || SYMBOL_CALLS_LOCAL (info, h)
7195 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7196 /* Discard dyn_relocs when non-pic if we've decided that a
7197 function symbol is local and not an ifunc. We keep dynamic
7198 relocs for ifuncs when local rather than always emitting a
7199 plt call stub for them and defining the symbol on the call
7200 stub. We can't do that for ELFv1 anyway (a function symbol
7201 is defined on a descriptor, not code) and it can be faster at
7202 run-time due to not needing to bounce through a stub. The
7203 dyn_relocs for ifuncs will be applied even in a static
7204 executable. */
7205 if (!bfd_link_pic (info)
7206 && h->type != STT_GNU_IFUNC
7207 && local)
7208 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7209
7210 /* Clear procedure linkage table information for any symbol that
7211 won't need a .plt entry. */
7212 struct plt_entry *ent;
7213 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7214 if (ent->plt.refcount > 0)
7215 break;
7216 if (ent == NULL
7217 || (h->type != STT_GNU_IFUNC
7218 && local
7219 && (((struct ppc_link_hash_entry *) h)->tls_mask
7220 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP))
7221 {
7222 h->plt.plist = NULL;
7223 h->needs_plt = 0;
7224 h->pointer_equality_needed = 0;
7225 }
7226 else if (abiversion (info->output_bfd) >= 2)
7227 {
7228 /* Taking a function's address in a read/write section
7229 doesn't require us to define the function symbol in the
7230 executable on a global entry stub. A dynamic reloc can
7231 be used instead. The reason we prefer a few more dynamic
7232 relocs is that calling via a global entry stub costs a
7233 few more instructions, and pointer_equality_needed causes
7234 extra work in ld.so when resolving these symbols. */
7235 if (global_entry_stub (h))
7236 {
7237 if (!readonly_dynrelocs (h))
7238 {
7239 h->pointer_equality_needed = 0;
7240 /* If we haven't seen a branch reloc then we don't need
7241 a plt entry. */
7242 if (!h->needs_plt)
7243 h->plt.plist = NULL;
7244 }
7245 else if (!bfd_link_pic (info))
7246 /* We are going to be defining the function symbol on the
7247 plt stub, so no dyn_relocs needed when non-pic. */
7248 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7249 }
7250
7251 /* ELFv2 function symbols can't have copy relocs. */
7252 return TRUE;
7253 }
7254 else if (!h->needs_plt
7255 && !readonly_dynrelocs (h))
7256 {
7257 /* If we haven't seen a branch reloc then we don't need a
7258 plt entry. */
7259 h->plt.plist = NULL;
7260 h->pointer_equality_needed = 0;
7261 return TRUE;
7262 }
7263 }
7264 else
7265 h->plt.plist = NULL;
7266
7267 /* If this is a weak symbol, and there is a real definition, the
7268 processor independent code will have arranged for us to see the
7269 real definition first, and we can just use the same value. */
7270 if (h->is_weakalias)
7271 {
7272 struct elf_link_hash_entry *def = weakdef (h);
7273 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7274 h->root.u.def.section = def->root.u.def.section;
7275 h->root.u.def.value = def->root.u.def.value;
7276 if (def->root.u.def.section == htab->elf.sdynbss
7277 || def->root.u.def.section == htab->elf.sdynrelro)
7278 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7279 return TRUE;
7280 }
7281
7282 /* If we are creating a shared library, we must presume that the
7283 only references to the symbol are via the global offset table.
7284 For such cases we need not do anything here; the relocations will
7285 be handled correctly by relocate_section. */
7286 if (bfd_link_pic (info))
7287 return TRUE;
7288
7289 /* If there are no references to this symbol that do not use the
7290 GOT, we don't need to generate a copy reloc. */
7291 if (!h->non_got_ref)
7292 return TRUE;
7293
7294 /* Don't generate a copy reloc for symbols defined in the executable. */
7295 if (!h->def_dynamic || !h->ref_regular || h->def_regular
7296
7297 /* If -z nocopyreloc was given, don't generate them either. */
7298 || info->nocopyreloc
7299
7300 /* If we don't find any dynamic relocs in read-only sections, then
7301 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7302 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7303
7304 /* Protected variables do not work with .dynbss. The copy in
7305 .dynbss won't be used by the shared library with the protected
7306 definition for the variable. Text relocations are preferable
7307 to an incorrect program. */
7308 || h->protected_def)
7309 return TRUE;
7310
7311 if (h->plt.plist != NULL)
7312 {
7313 /* We should never get here, but unfortunately there are versions
7314 of gcc out there that improperly (for this ABI) put initialized
7315 function pointers, vtable refs and suchlike in read-only
7316 sections. Allow them to proceed, but warn that this might
7317 break at runtime. */
7318 info->callbacks->einfo
7319 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
7320 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7321 h->root.root.string);
7322 }
7323
7324 /* This is a reference to a symbol defined by a dynamic object which
7325 is not a function. */
7326
7327 /* We must allocate the symbol in our .dynbss section, which will
7328 become part of the .bss section of the executable. There will be
7329 an entry for this symbol in the .dynsym section. The dynamic
7330 object will contain position independent code, so all references
7331 from the dynamic object to this symbol will go through the global
7332 offset table. The dynamic linker will use the .dynsym entry to
7333 determine the address it must put in the global offset table, so
7334 both the dynamic object and the regular object will refer to the
7335 same memory location for the variable. */
7336 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7337 {
7338 s = htab->elf.sdynrelro;
7339 srel = htab->elf.sreldynrelro;
7340 }
7341 else
7342 {
7343 s = htab->elf.sdynbss;
7344 srel = htab->elf.srelbss;
7345 }
7346 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7347 {
7348 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7349 linker to copy the initial value out of the dynamic object
7350 and into the runtime process image. */
7351 srel->size += sizeof (Elf64_External_Rela);
7352 h->needs_copy = 1;
7353 }
7354
7355 /* We no longer want dyn_relocs. */
7356 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7357 return _bfd_elf_adjust_dynamic_copy (info, h, s);
7358 }
7359
7360 /* If given a function descriptor symbol, hide both the function code
7361 sym and the descriptor. */
7362 static void
7363 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7364 struct elf_link_hash_entry *h,
7365 bfd_boolean force_local)
7366 {
7367 struct ppc_link_hash_entry *eh;
7368 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7369
7370 eh = (struct ppc_link_hash_entry *) h;
7371 if (eh->is_func_descriptor)
7372 {
7373 struct ppc_link_hash_entry *fh = eh->oh;
7374
7375 if (fh == NULL)
7376 {
7377 const char *p, *q;
7378 struct elf_link_hash_table *htab = elf_hash_table (info);
7379 char save;
7380
7381 /* We aren't supposed to use alloca in BFD because on
7382 systems which do not have alloca the version in libiberty
7383 calls xmalloc, which might cause the program to crash
7384 when it runs out of memory. This function doesn't have a
7385 return status, so there's no way to gracefully return an
7386 error. So cheat. We know that string[-1] can be safely
7387 accessed; It's either a string in an ELF string table,
7388 or allocated in an objalloc structure. */
7389
7390 p = eh->elf.root.root.string - 1;
7391 save = *p;
7392 *(char *) p = '.';
7393 fh = (struct ppc_link_hash_entry *)
7394 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7395 *(char *) p = save;
7396
7397 /* Unfortunately, if it so happens that the string we were
7398 looking for was allocated immediately before this string,
7399 then we overwrote the string terminator. That's the only
7400 reason the lookup should fail. */
7401 if (fh == NULL)
7402 {
7403 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7404 while (q >= eh->elf.root.root.string && *q == *p)
7405 --q, --p;
7406 if (q < eh->elf.root.root.string && *p == '.')
7407 fh = (struct ppc_link_hash_entry *)
7408 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7409 }
7410 if (fh != NULL)
7411 {
7412 eh->oh = fh;
7413 fh->oh = eh;
7414 }
7415 }
7416 if (fh != NULL)
7417 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7418 }
7419 }
7420
7421 static bfd_boolean
7422 get_sym_h (struct elf_link_hash_entry **hp,
7423 Elf_Internal_Sym **symp,
7424 asection **symsecp,
7425 unsigned char **tls_maskp,
7426 Elf_Internal_Sym **locsymsp,
7427 unsigned long r_symndx,
7428 bfd *ibfd)
7429 {
7430 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7431
7432 if (r_symndx >= symtab_hdr->sh_info)
7433 {
7434 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7435 struct elf_link_hash_entry *h;
7436
7437 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7438 h = elf_follow_link (h);
7439
7440 if (hp != NULL)
7441 *hp = h;
7442
7443 if (symp != NULL)
7444 *symp = NULL;
7445
7446 if (symsecp != NULL)
7447 {
7448 asection *symsec = NULL;
7449 if (h->root.type == bfd_link_hash_defined
7450 || h->root.type == bfd_link_hash_defweak)
7451 symsec = h->root.u.def.section;
7452 *symsecp = symsec;
7453 }
7454
7455 if (tls_maskp != NULL)
7456 {
7457 struct ppc_link_hash_entry *eh;
7458
7459 eh = (struct ppc_link_hash_entry *) h;
7460 *tls_maskp = &eh->tls_mask;
7461 }
7462 }
7463 else
7464 {
7465 Elf_Internal_Sym *sym;
7466 Elf_Internal_Sym *locsyms = *locsymsp;
7467
7468 if (locsyms == NULL)
7469 {
7470 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7471 if (locsyms == NULL)
7472 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7473 symtab_hdr->sh_info,
7474 0, NULL, NULL, NULL);
7475 if (locsyms == NULL)
7476 return FALSE;
7477 *locsymsp = locsyms;
7478 }
7479 sym = locsyms + r_symndx;
7480
7481 if (hp != NULL)
7482 *hp = NULL;
7483
7484 if (symp != NULL)
7485 *symp = sym;
7486
7487 if (symsecp != NULL)
7488 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7489
7490 if (tls_maskp != NULL)
7491 {
7492 struct got_entry **lgot_ents;
7493 unsigned char *tls_mask;
7494
7495 tls_mask = NULL;
7496 lgot_ents = elf_local_got_ents (ibfd);
7497 if (lgot_ents != NULL)
7498 {
7499 struct plt_entry **local_plt = (struct plt_entry **)
7500 (lgot_ents + symtab_hdr->sh_info);
7501 unsigned char *lgot_masks = (unsigned char *)
7502 (local_plt + symtab_hdr->sh_info);
7503 tls_mask = &lgot_masks[r_symndx];
7504 }
7505 *tls_maskp = tls_mask;
7506 }
7507 }
7508 return TRUE;
7509 }
7510
7511 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7512 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7513 type suitable for optimization, and 1 otherwise. */
7514
7515 static int
7516 get_tls_mask (unsigned char **tls_maskp,
7517 unsigned long *toc_symndx,
7518 bfd_vma *toc_addend,
7519 Elf_Internal_Sym **locsymsp,
7520 const Elf_Internal_Rela *rel,
7521 bfd *ibfd)
7522 {
7523 unsigned long r_symndx;
7524 int next_r;
7525 struct elf_link_hash_entry *h;
7526 Elf_Internal_Sym *sym;
7527 asection *sec;
7528 bfd_vma off;
7529
7530 r_symndx = ELF64_R_SYM (rel->r_info);
7531 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7532 return 0;
7533
7534 if ((*tls_maskp != NULL
7535 && (**tls_maskp & TLS_TLS) != 0
7536 && **tls_maskp != (TLS_TLS | TLS_MARK))
7537 || sec == NULL
7538 || ppc64_elf_section_data (sec) == NULL
7539 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7540 return 1;
7541
7542 /* Look inside a TOC section too. */
7543 if (h != NULL)
7544 {
7545 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7546 off = h->root.u.def.value;
7547 }
7548 else
7549 off = sym->st_value;
7550 off += rel->r_addend;
7551 BFD_ASSERT (off % 8 == 0);
7552 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7553 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7554 if (toc_symndx != NULL)
7555 *toc_symndx = r_symndx;
7556 if (toc_addend != NULL)
7557 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7558 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7559 return 0;
7560 if ((h == NULL || is_static_defined (h))
7561 && (next_r == -1 || next_r == -2))
7562 return 1 - next_r;
7563 return 1;
7564 }
7565
7566 /* Find (or create) an entry in the tocsave hash table. */
7567
7568 static struct tocsave_entry *
7569 tocsave_find (struct ppc_link_hash_table *htab,
7570 enum insert_option insert,
7571 Elf_Internal_Sym **local_syms,
7572 const Elf_Internal_Rela *irela,
7573 bfd *ibfd)
7574 {
7575 unsigned long r_indx;
7576 struct elf_link_hash_entry *h;
7577 Elf_Internal_Sym *sym;
7578 struct tocsave_entry ent, *p;
7579 hashval_t hash;
7580 struct tocsave_entry **slot;
7581
7582 r_indx = ELF64_R_SYM (irela->r_info);
7583 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7584 return NULL;
7585 if (ent.sec == NULL || ent.sec->output_section == NULL)
7586 {
7587 _bfd_error_handler
7588 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7589 return NULL;
7590 }
7591
7592 if (h != NULL)
7593 ent.offset = h->root.u.def.value;
7594 else
7595 ent.offset = sym->st_value;
7596 ent.offset += irela->r_addend;
7597
7598 hash = tocsave_htab_hash (&ent);
7599 slot = ((struct tocsave_entry **)
7600 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7601 if (slot == NULL)
7602 return NULL;
7603
7604 if (*slot == NULL)
7605 {
7606 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7607 if (p == NULL)
7608 return NULL;
7609 *p = ent;
7610 *slot = p;
7611 }
7612 return *slot;
7613 }
7614
7615 /* Adjust all global syms defined in opd sections. In gcc generated
7616 code for the old ABI, these will already have been done. */
7617
7618 static bfd_boolean
7619 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7620 {
7621 struct ppc_link_hash_entry *eh;
7622 asection *sym_sec;
7623 struct _opd_sec_data *opd;
7624
7625 if (h->root.type == bfd_link_hash_indirect)
7626 return TRUE;
7627
7628 if (h->root.type != bfd_link_hash_defined
7629 && h->root.type != bfd_link_hash_defweak)
7630 return TRUE;
7631
7632 eh = (struct ppc_link_hash_entry *) h;
7633 if (eh->adjust_done)
7634 return TRUE;
7635
7636 sym_sec = eh->elf.root.u.def.section;
7637 opd = get_opd_info (sym_sec);
7638 if (opd != NULL && opd->adjust != NULL)
7639 {
7640 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7641 if (adjust == -1)
7642 {
7643 /* This entry has been deleted. */
7644 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7645 if (dsec == NULL)
7646 {
7647 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7648 if (discarded_section (dsec))
7649 {
7650 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7651 break;
7652 }
7653 }
7654 eh->elf.root.u.def.value = 0;
7655 eh->elf.root.u.def.section = dsec;
7656 }
7657 else
7658 eh->elf.root.u.def.value += adjust;
7659 eh->adjust_done = 1;
7660 }
7661 return TRUE;
7662 }
7663
7664 /* Handles decrementing dynamic reloc counts for the reloc specified by
7665 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7666 have already been determined. */
7667
7668 static bfd_boolean
7669 dec_dynrel_count (bfd_vma r_info,
7670 asection *sec,
7671 struct bfd_link_info *info,
7672 Elf_Internal_Sym **local_syms,
7673 struct elf_link_hash_entry *h,
7674 Elf_Internal_Sym *sym)
7675 {
7676 enum elf_ppc64_reloc_type r_type;
7677 asection *sym_sec = NULL;
7678
7679 /* Can this reloc be dynamic? This switch, and later tests here
7680 should be kept in sync with the code in check_relocs. */
7681 r_type = ELF64_R_TYPE (r_info);
7682 switch (r_type)
7683 {
7684 default:
7685 return TRUE;
7686
7687 case R_PPC64_TPREL16:
7688 case R_PPC64_TPREL16_LO:
7689 case R_PPC64_TPREL16_HI:
7690 case R_PPC64_TPREL16_HA:
7691 case R_PPC64_TPREL16_DS:
7692 case R_PPC64_TPREL16_LO_DS:
7693 case R_PPC64_TPREL16_HIGH:
7694 case R_PPC64_TPREL16_HIGHA:
7695 case R_PPC64_TPREL16_HIGHER:
7696 case R_PPC64_TPREL16_HIGHERA:
7697 case R_PPC64_TPREL16_HIGHEST:
7698 case R_PPC64_TPREL16_HIGHESTA:
7699 case R_PPC64_TPREL64:
7700 case R_PPC64_DTPMOD64:
7701 case R_PPC64_DTPREL64:
7702 case R_PPC64_ADDR64:
7703 case R_PPC64_REL30:
7704 case R_PPC64_REL32:
7705 case R_PPC64_REL64:
7706 case R_PPC64_ADDR14:
7707 case R_PPC64_ADDR14_BRNTAKEN:
7708 case R_PPC64_ADDR14_BRTAKEN:
7709 case R_PPC64_ADDR16:
7710 case R_PPC64_ADDR16_DS:
7711 case R_PPC64_ADDR16_HA:
7712 case R_PPC64_ADDR16_HI:
7713 case R_PPC64_ADDR16_HIGH:
7714 case R_PPC64_ADDR16_HIGHA:
7715 case R_PPC64_ADDR16_HIGHER:
7716 case R_PPC64_ADDR16_HIGHERA:
7717 case R_PPC64_ADDR16_HIGHEST:
7718 case R_PPC64_ADDR16_HIGHESTA:
7719 case R_PPC64_ADDR16_LO:
7720 case R_PPC64_ADDR16_LO_DS:
7721 case R_PPC64_ADDR24:
7722 case R_PPC64_ADDR32:
7723 case R_PPC64_UADDR16:
7724 case R_PPC64_UADDR32:
7725 case R_PPC64_UADDR64:
7726 case R_PPC64_TOC:
7727 break;
7728 }
7729
7730 if (local_syms != NULL)
7731 {
7732 unsigned long r_symndx;
7733 bfd *ibfd = sec->owner;
7734
7735 r_symndx = ELF64_R_SYM (r_info);
7736 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7737 return FALSE;
7738 }
7739
7740 if ((bfd_link_pic (info)
7741 && (must_be_dyn_reloc (info, r_type)
7742 || (h != NULL
7743 && (!SYMBOLIC_BIND (info, h)
7744 || h->root.type == bfd_link_hash_defweak
7745 || !h->def_regular))))
7746 || (ELIMINATE_COPY_RELOCS
7747 && !bfd_link_pic (info)
7748 && h != NULL
7749 && (h->root.type == bfd_link_hash_defweak
7750 || !h->def_regular)))
7751 ;
7752 else
7753 return TRUE;
7754
7755 if (h != NULL)
7756 {
7757 struct elf_dyn_relocs *p;
7758 struct elf_dyn_relocs **pp;
7759 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7760
7761 /* elf_gc_sweep may have already removed all dyn relocs associated
7762 with local syms for a given section. Also, symbol flags are
7763 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7764 report a dynreloc miscount. */
7765 if (*pp == NULL && info->gc_sections)
7766 return TRUE;
7767
7768 while ((p = *pp) != NULL)
7769 {
7770 if (p->sec == sec)
7771 {
7772 if (!must_be_dyn_reloc (info, r_type))
7773 p->pc_count -= 1;
7774 p->count -= 1;
7775 if (p->count == 0)
7776 *pp = p->next;
7777 return TRUE;
7778 }
7779 pp = &p->next;
7780 }
7781 }
7782 else
7783 {
7784 struct ppc_dyn_relocs *p;
7785 struct ppc_dyn_relocs **pp;
7786 void *vpp;
7787 bfd_boolean is_ifunc;
7788
7789 if (local_syms == NULL)
7790 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7791 if (sym_sec == NULL)
7792 sym_sec = sec;
7793
7794 vpp = &elf_section_data (sym_sec)->local_dynrel;
7795 pp = (struct ppc_dyn_relocs **) vpp;
7796
7797 if (*pp == NULL && info->gc_sections)
7798 return TRUE;
7799
7800 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7801 while ((p = *pp) != NULL)
7802 {
7803 if (p->sec == sec && p->ifunc == is_ifunc)
7804 {
7805 p->count -= 1;
7806 if (p->count == 0)
7807 *pp = p->next;
7808 return TRUE;
7809 }
7810 pp = &p->next;
7811 }
7812 }
7813
7814 /* xgettext:c-format */
7815 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7816 sec->owner, sec);
7817 bfd_set_error (bfd_error_bad_value);
7818 return FALSE;
7819 }
7820
7821 /* Remove unused Official Procedure Descriptor entries. Currently we
7822 only remove those associated with functions in discarded link-once
7823 sections, or weakly defined functions that have been overridden. It
7824 would be possible to remove many more entries for statically linked
7825 applications. */
7826
7827 bfd_boolean
7828 ppc64_elf_edit_opd (struct bfd_link_info *info)
7829 {
7830 bfd *ibfd;
7831 bfd_boolean some_edited = FALSE;
7832 asection *need_pad = NULL;
7833 struct ppc_link_hash_table *htab;
7834
7835 htab = ppc_hash_table (info);
7836 if (htab == NULL)
7837 return FALSE;
7838
7839 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7840 {
7841 asection *sec;
7842 Elf_Internal_Rela *relstart, *rel, *relend;
7843 Elf_Internal_Shdr *symtab_hdr;
7844 Elf_Internal_Sym *local_syms;
7845 struct _opd_sec_data *opd;
7846 bfd_boolean need_edit, add_aux_fields, broken;
7847 bfd_size_type cnt_16b = 0;
7848
7849 if (!is_ppc64_elf (ibfd))
7850 continue;
7851
7852 sec = bfd_get_section_by_name (ibfd, ".opd");
7853 if (sec == NULL || sec->size == 0)
7854 continue;
7855
7856 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7857 continue;
7858
7859 if (sec->output_section == bfd_abs_section_ptr)
7860 continue;
7861
7862 /* Look through the section relocs. */
7863 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7864 continue;
7865
7866 local_syms = NULL;
7867 symtab_hdr = &elf_symtab_hdr (ibfd);
7868
7869 /* Read the relocations. */
7870 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7871 info->keep_memory);
7872 if (relstart == NULL)
7873 return FALSE;
7874
7875 /* First run through the relocs to check they are sane, and to
7876 determine whether we need to edit this opd section. */
7877 need_edit = FALSE;
7878 broken = FALSE;
7879 need_pad = sec;
7880 relend = relstart + sec->reloc_count;
7881 for (rel = relstart; rel < relend; )
7882 {
7883 enum elf_ppc64_reloc_type r_type;
7884 unsigned long r_symndx;
7885 asection *sym_sec;
7886 struct elf_link_hash_entry *h;
7887 Elf_Internal_Sym *sym;
7888 bfd_vma offset;
7889
7890 /* .opd contains an array of 16 or 24 byte entries. We're
7891 only interested in the reloc pointing to a function entry
7892 point. */
7893 offset = rel->r_offset;
7894 if (rel + 1 == relend
7895 || rel[1].r_offset != offset + 8)
7896 {
7897 /* If someone messes with .opd alignment then after a
7898 "ld -r" we might have padding in the middle of .opd.
7899 Also, there's nothing to prevent someone putting
7900 something silly in .opd with the assembler. No .opd
7901 optimization for them! */
7902 broken_opd:
7903 _bfd_error_handler
7904 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7905 broken = TRUE;
7906 break;
7907 }
7908
7909 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7910 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7911 {
7912 _bfd_error_handler
7913 /* xgettext:c-format */
7914 (_("%pB: unexpected reloc type %u in .opd section"),
7915 ibfd, r_type);
7916 broken = TRUE;
7917 break;
7918 }
7919
7920 r_symndx = ELF64_R_SYM (rel->r_info);
7921 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7922 r_symndx, ibfd))
7923 goto error_ret;
7924
7925 if (sym_sec == NULL || sym_sec->owner == NULL)
7926 {
7927 const char *sym_name;
7928 if (h != NULL)
7929 sym_name = h->root.root.string;
7930 else
7931 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7932 sym_sec);
7933
7934 _bfd_error_handler
7935 /* xgettext:c-format */
7936 (_("%pB: undefined sym `%s' in .opd section"),
7937 ibfd, sym_name);
7938 broken = TRUE;
7939 break;
7940 }
7941
7942 /* opd entries are always for functions defined in the
7943 current input bfd. If the symbol isn't defined in the
7944 input bfd, then we won't be using the function in this
7945 bfd; It must be defined in a linkonce section in another
7946 bfd, or is weak. It's also possible that we are
7947 discarding the function due to a linker script /DISCARD/,
7948 which we test for via the output_section. */
7949 if (sym_sec->owner != ibfd
7950 || sym_sec->output_section == bfd_abs_section_ptr)
7951 need_edit = TRUE;
7952
7953 rel += 2;
7954 if (rel + 1 == relend
7955 || (rel + 2 < relend
7956 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7957 ++rel;
7958
7959 if (rel == relend)
7960 {
7961 if (sec->size == offset + 24)
7962 {
7963 need_pad = NULL;
7964 break;
7965 }
7966 if (sec->size == offset + 16)
7967 {
7968 cnt_16b++;
7969 break;
7970 }
7971 goto broken_opd;
7972 }
7973 else if (rel + 1 < relend
7974 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7975 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7976 {
7977 if (rel[0].r_offset == offset + 16)
7978 cnt_16b++;
7979 else if (rel[0].r_offset != offset + 24)
7980 goto broken_opd;
7981 }
7982 else
7983 goto broken_opd;
7984 }
7985
7986 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7987
7988 if (!broken && (need_edit || add_aux_fields))
7989 {
7990 Elf_Internal_Rela *write_rel;
7991 Elf_Internal_Shdr *rel_hdr;
7992 bfd_byte *rptr, *wptr;
7993 bfd_byte *new_contents;
7994 bfd_size_type amt;
7995
7996 new_contents = NULL;
7997 amt = OPD_NDX (sec->size) * sizeof (long);
7998 opd = &ppc64_elf_section_data (sec)->u.opd;
7999 opd->adjust = bfd_zalloc (sec->owner, amt);
8000 if (opd->adjust == NULL)
8001 return FALSE;
8002
8003 /* This seems a waste of time as input .opd sections are all
8004 zeros as generated by gcc, but I suppose there's no reason
8005 this will always be so. We might start putting something in
8006 the third word of .opd entries. */
8007 if ((sec->flags & SEC_IN_MEMORY) == 0)
8008 {
8009 bfd_byte *loc;
8010 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
8011 {
8012 if (loc != NULL)
8013 free (loc);
8014 error_ret:
8015 if (local_syms != NULL
8016 && symtab_hdr->contents != (unsigned char *) local_syms)
8017 free (local_syms);
8018 if (elf_section_data (sec)->relocs != relstart)
8019 free (relstart);
8020 return FALSE;
8021 }
8022 sec->contents = loc;
8023 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8024 }
8025
8026 elf_section_data (sec)->relocs = relstart;
8027
8028 new_contents = sec->contents;
8029 if (add_aux_fields)
8030 {
8031 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8032 if (new_contents == NULL)
8033 return FALSE;
8034 need_pad = NULL;
8035 }
8036 wptr = new_contents;
8037 rptr = sec->contents;
8038 write_rel = relstart;
8039 for (rel = relstart; rel < relend; )
8040 {
8041 unsigned long r_symndx;
8042 asection *sym_sec;
8043 struct elf_link_hash_entry *h;
8044 struct ppc_link_hash_entry *fdh = NULL;
8045 Elf_Internal_Sym *sym;
8046 long opd_ent_size;
8047 Elf_Internal_Rela *next_rel;
8048 bfd_boolean skip;
8049
8050 r_symndx = ELF64_R_SYM (rel->r_info);
8051 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8052 r_symndx, ibfd))
8053 goto error_ret;
8054
8055 next_rel = rel + 2;
8056 if (next_rel + 1 == relend
8057 || (next_rel + 2 < relend
8058 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8059 ++next_rel;
8060
8061 /* See if the .opd entry is full 24 byte or
8062 16 byte (with fd_aux entry overlapped with next
8063 fd_func). */
8064 opd_ent_size = 24;
8065 if (next_rel == relend)
8066 {
8067 if (sec->size == rel->r_offset + 16)
8068 opd_ent_size = 16;
8069 }
8070 else if (next_rel->r_offset == rel->r_offset + 16)
8071 opd_ent_size = 16;
8072
8073 if (h != NULL
8074 && h->root.root.string[0] == '.')
8075 {
8076 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8077 if (fdh != NULL)
8078 {
8079 fdh = ppc_follow_link (fdh);
8080 if (fdh->elf.root.type != bfd_link_hash_defined
8081 && fdh->elf.root.type != bfd_link_hash_defweak)
8082 fdh = NULL;
8083 }
8084 }
8085
8086 skip = (sym_sec->owner != ibfd
8087 || sym_sec->output_section == bfd_abs_section_ptr);
8088 if (skip)
8089 {
8090 if (fdh != NULL && sym_sec->owner == ibfd)
8091 {
8092 /* Arrange for the function descriptor sym
8093 to be dropped. */
8094 fdh->elf.root.u.def.value = 0;
8095 fdh->elf.root.u.def.section = sym_sec;
8096 }
8097 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8098
8099 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8100 rel = next_rel;
8101 else
8102 while (1)
8103 {
8104 if (!dec_dynrel_count (rel->r_info, sec, info,
8105 NULL, h, sym))
8106 goto error_ret;
8107
8108 if (++rel == next_rel)
8109 break;
8110
8111 r_symndx = ELF64_R_SYM (rel->r_info);
8112 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8113 r_symndx, ibfd))
8114 goto error_ret;
8115 }
8116 }
8117 else
8118 {
8119 /* We'll be keeping this opd entry. */
8120 long adjust;
8121
8122 if (fdh != NULL)
8123 {
8124 /* Redefine the function descriptor symbol to
8125 this location in the opd section. It is
8126 necessary to update the value here rather
8127 than using an array of adjustments as we do
8128 for local symbols, because various places
8129 in the generic ELF code use the value
8130 stored in u.def.value. */
8131 fdh->elf.root.u.def.value = wptr - new_contents;
8132 fdh->adjust_done = 1;
8133 }
8134
8135 /* Local syms are a bit tricky. We could
8136 tweak them as they can be cached, but
8137 we'd need to look through the local syms
8138 for the function descriptor sym which we
8139 don't have at the moment. So keep an
8140 array of adjustments. */
8141 adjust = (wptr - new_contents) - (rptr - sec->contents);
8142 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8143
8144 if (wptr != rptr)
8145 memcpy (wptr, rptr, opd_ent_size);
8146 wptr += opd_ent_size;
8147 if (add_aux_fields && opd_ent_size == 16)
8148 {
8149 memset (wptr, '\0', 8);
8150 wptr += 8;
8151 }
8152
8153 /* We need to adjust any reloc offsets to point to the
8154 new opd entries. */
8155 for ( ; rel != next_rel; ++rel)
8156 {
8157 rel->r_offset += adjust;
8158 if (write_rel != rel)
8159 memcpy (write_rel, rel, sizeof (*rel));
8160 ++write_rel;
8161 }
8162 }
8163
8164 rptr += opd_ent_size;
8165 }
8166
8167 sec->size = wptr - new_contents;
8168 sec->reloc_count = write_rel - relstart;
8169 if (add_aux_fields)
8170 {
8171 free (sec->contents);
8172 sec->contents = new_contents;
8173 }
8174
8175 /* Fudge the header size too, as this is used later in
8176 elf_bfd_final_link if we are emitting relocs. */
8177 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8178 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8179 some_edited = TRUE;
8180 }
8181 else if (elf_section_data (sec)->relocs != relstart)
8182 free (relstart);
8183
8184 if (local_syms != NULL
8185 && symtab_hdr->contents != (unsigned char *) local_syms)
8186 {
8187 if (!info->keep_memory)
8188 free (local_syms);
8189 else
8190 symtab_hdr->contents = (unsigned char *) local_syms;
8191 }
8192 }
8193
8194 if (some_edited)
8195 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8196
8197 /* If we are doing a final link and the last .opd entry is just 16 byte
8198 long, add a 8 byte padding after it. */
8199 if (need_pad != NULL && !bfd_link_relocatable (info))
8200 {
8201 bfd_byte *p;
8202
8203 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8204 {
8205 BFD_ASSERT (need_pad->size > 0);
8206
8207 p = bfd_malloc (need_pad->size + 8);
8208 if (p == NULL)
8209 return FALSE;
8210
8211 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8212 p, 0, need_pad->size))
8213 return FALSE;
8214
8215 need_pad->contents = p;
8216 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8217 }
8218 else
8219 {
8220 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8221 if (p == NULL)
8222 return FALSE;
8223
8224 need_pad->contents = p;
8225 }
8226
8227 memset (need_pad->contents + need_pad->size, 0, 8);
8228 need_pad->size += 8;
8229 }
8230
8231 return TRUE;
8232 }
8233
8234 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
8235
8236 asection *
8237 ppc64_elf_tls_setup (struct bfd_link_info *info)
8238 {
8239 struct ppc_link_hash_table *htab;
8240
8241 htab = ppc_hash_table (info);
8242 if (htab == NULL)
8243 return NULL;
8244
8245 if (abiversion (info->output_bfd) == 1)
8246 htab->opd_abi = 1;
8247
8248 if (htab->params->no_multi_toc)
8249 htab->do_multi_toc = 0;
8250 else if (!htab->do_multi_toc)
8251 htab->params->no_multi_toc = 1;
8252
8253 /* Default to --no-plt-localentry, as this option can cause problems
8254 with symbol interposition. For example, glibc libpthread.so and
8255 libc.so duplicate many pthread symbols, with a fallback
8256 implementation in libc.so. In some cases the fallback does more
8257 work than the pthread implementation. __pthread_condattr_destroy
8258 is one such symbol: the libpthread.so implementation is
8259 localentry:0 while the libc.so implementation is localentry:8.
8260 An app that "cleverly" uses dlopen to only load necessary
8261 libraries at runtime may omit loading libpthread.so when not
8262 running multi-threaded, which then results in the libc.so
8263 fallback symbols being used and ld.so complaining. Now there
8264 are workarounds in ld (see non_zero_localentry) to detect the
8265 pthread situation, but that may not be the only case where
8266 --plt-localentry can cause trouble. */
8267 if (htab->params->plt_localentry0 < 0)
8268 htab->params->plt_localentry0 = 0;
8269 if (htab->params->plt_localentry0
8270 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8271 FALSE, FALSE, FALSE) == NULL)
8272 _bfd_error_handler
8273 (_("warning: --plt-localentry is especially dangerous without "
8274 "ld.so support to detect ABI violations"));
8275
8276 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8277 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8278 FALSE, FALSE, TRUE));
8279 /* Move dynamic linking info to the function descriptor sym. */
8280 if (htab->tls_get_addr != NULL)
8281 func_desc_adjust (&htab->tls_get_addr->elf, info);
8282 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8283 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8284 FALSE, FALSE, TRUE));
8285 if (htab->params->tls_get_addr_opt)
8286 {
8287 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8288
8289 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8290 FALSE, FALSE, TRUE);
8291 if (opt != NULL)
8292 func_desc_adjust (opt, info);
8293 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8294 FALSE, FALSE, TRUE);
8295 if (opt_fd != NULL
8296 && (opt_fd->root.type == bfd_link_hash_defined
8297 || opt_fd->root.type == bfd_link_hash_defweak))
8298 {
8299 /* If glibc supports an optimized __tls_get_addr call stub,
8300 signalled by the presence of __tls_get_addr_opt, and we'll
8301 be calling __tls_get_addr via a plt call stub, then
8302 make __tls_get_addr point to __tls_get_addr_opt. */
8303 tga_fd = &htab->tls_get_addr_fd->elf;
8304 if (htab->elf.dynamic_sections_created
8305 && tga_fd != NULL
8306 && (tga_fd->type == STT_FUNC
8307 || tga_fd->needs_plt)
8308 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8309 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
8310 {
8311 struct plt_entry *ent;
8312
8313 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8314 if (ent->plt.refcount > 0)
8315 break;
8316 if (ent != NULL)
8317 {
8318 tga_fd->root.type = bfd_link_hash_indirect;
8319 tga_fd->root.u.i.link = &opt_fd->root;
8320 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8321 opt_fd->mark = 1;
8322 if (opt_fd->dynindx != -1)
8323 {
8324 /* Use __tls_get_addr_opt in dynamic relocations. */
8325 opt_fd->dynindx = -1;
8326 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8327 opt_fd->dynstr_index);
8328 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8329 return NULL;
8330 }
8331 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8332 tga = &htab->tls_get_addr->elf;
8333 if (opt != NULL && tga != NULL)
8334 {
8335 tga->root.type = bfd_link_hash_indirect;
8336 tga->root.u.i.link = &opt->root;
8337 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8338 opt->mark = 1;
8339 _bfd_elf_link_hash_hide_symbol (info, opt,
8340 tga->forced_local);
8341 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8342 }
8343 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8344 htab->tls_get_addr_fd->is_func_descriptor = 1;
8345 if (htab->tls_get_addr != NULL)
8346 {
8347 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8348 htab->tls_get_addr->is_func = 1;
8349 }
8350 }
8351 }
8352 }
8353 else if (htab->params->tls_get_addr_opt < 0)
8354 htab->params->tls_get_addr_opt = 0;
8355 }
8356 return _bfd_elf_tls_setup (info->output_bfd, info);
8357 }
8358
8359 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8360 HASH1 or HASH2. */
8361
8362 static bfd_boolean
8363 branch_reloc_hash_match (const bfd *ibfd,
8364 const Elf_Internal_Rela *rel,
8365 const struct ppc_link_hash_entry *hash1,
8366 const struct ppc_link_hash_entry *hash2)
8367 {
8368 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8369 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8370 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8371
8372 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8373 {
8374 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8375 struct elf_link_hash_entry *h;
8376
8377 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8378 h = elf_follow_link (h);
8379 if (h == &hash1->elf || h == &hash2->elf)
8380 return TRUE;
8381 }
8382 return FALSE;
8383 }
8384
8385 /* Run through all the TLS relocs looking for optimization
8386 opportunities. The linker has been hacked (see ppc64elf.em) to do
8387 a preliminary section layout so that we know the TLS segment
8388 offsets. We can't optimize earlier because some optimizations need
8389 to know the tp offset, and we need to optimize before allocating
8390 dynamic relocations. */
8391
8392 bfd_boolean
8393 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8394 {
8395 bfd *ibfd;
8396 asection *sec;
8397 struct ppc_link_hash_table *htab;
8398 unsigned char *toc_ref;
8399 int pass;
8400
8401 if (!bfd_link_executable (info))
8402 return TRUE;
8403
8404 htab = ppc_hash_table (info);
8405 if (htab == NULL)
8406 return FALSE;
8407
8408 /* Make two passes over the relocs. On the first pass, mark toc
8409 entries involved with tls relocs, and check that tls relocs
8410 involved in setting up a tls_get_addr call are indeed followed by
8411 such a call. If they are not, we can't do any tls optimization.
8412 On the second pass twiddle tls_mask flags to notify
8413 relocate_section that optimization can be done, and adjust got
8414 and plt refcounts. */
8415 toc_ref = NULL;
8416 for (pass = 0; pass < 2; ++pass)
8417 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8418 {
8419 Elf_Internal_Sym *locsyms = NULL;
8420 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8421
8422 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8423 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8424 {
8425 Elf_Internal_Rela *relstart, *rel, *relend;
8426 bfd_boolean found_tls_get_addr_arg = 0;
8427
8428 /* Read the relocations. */
8429 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8430 info->keep_memory);
8431 if (relstart == NULL)
8432 {
8433 free (toc_ref);
8434 return FALSE;
8435 }
8436
8437 relend = relstart + sec->reloc_count;
8438 for (rel = relstart; rel < relend; rel++)
8439 {
8440 enum elf_ppc64_reloc_type r_type;
8441 unsigned long r_symndx;
8442 struct elf_link_hash_entry *h;
8443 Elf_Internal_Sym *sym;
8444 asection *sym_sec;
8445 unsigned char *tls_mask;
8446 unsigned char tls_set, tls_clear, tls_type = 0;
8447 bfd_vma value;
8448 bfd_boolean ok_tprel, is_local;
8449 long toc_ref_index = 0;
8450 int expecting_tls_get_addr = 0;
8451 bfd_boolean ret = FALSE;
8452
8453 r_symndx = ELF64_R_SYM (rel->r_info);
8454 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8455 r_symndx, ibfd))
8456 {
8457 err_free_rel:
8458 if (elf_section_data (sec)->relocs != relstart)
8459 free (relstart);
8460 if (toc_ref != NULL)
8461 free (toc_ref);
8462 if (locsyms != NULL
8463 && (elf_symtab_hdr (ibfd).contents
8464 != (unsigned char *) locsyms))
8465 free (locsyms);
8466 return ret;
8467 }
8468
8469 if (h != NULL)
8470 {
8471 if (h->root.type == bfd_link_hash_defined
8472 || h->root.type == bfd_link_hash_defweak)
8473 value = h->root.u.def.value;
8474 else if (h->root.type == bfd_link_hash_undefweak)
8475 value = 0;
8476 else
8477 {
8478 found_tls_get_addr_arg = 0;
8479 continue;
8480 }
8481 }
8482 else
8483 /* Symbols referenced by TLS relocs must be of type
8484 STT_TLS. So no need for .opd local sym adjust. */
8485 value = sym->st_value;
8486
8487 ok_tprel = FALSE;
8488 is_local = FALSE;
8489 if (h == NULL
8490 || !h->def_dynamic)
8491 {
8492 is_local = TRUE;
8493 if (h != NULL
8494 && h->root.type == bfd_link_hash_undefweak)
8495 ok_tprel = TRUE;
8496 else if (sym_sec != NULL
8497 && sym_sec->output_section != NULL)
8498 {
8499 value += sym_sec->output_offset;
8500 value += sym_sec->output_section->vma;
8501 value -= htab->elf.tls_sec->vma;
8502 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8503 < (bfd_vma) 1 << 32);
8504 }
8505 }
8506
8507 r_type = ELF64_R_TYPE (rel->r_info);
8508 /* If this section has old-style __tls_get_addr calls
8509 without marker relocs, then check that each
8510 __tls_get_addr call reloc is preceded by a reloc
8511 that conceivably belongs to the __tls_get_addr arg
8512 setup insn. If we don't find matching arg setup
8513 relocs, don't do any tls optimization. */
8514 if (pass == 0
8515 && sec->has_tls_get_addr_call
8516 && h != NULL
8517 && (h == &htab->tls_get_addr->elf
8518 || h == &htab->tls_get_addr_fd->elf)
8519 && !found_tls_get_addr_arg
8520 && is_branch_reloc (r_type))
8521 {
8522 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8523 "TLS optimization disabled\n"),
8524 ibfd, sec, rel->r_offset);
8525 ret = TRUE;
8526 goto err_free_rel;
8527 }
8528
8529 found_tls_get_addr_arg = 0;
8530 switch (r_type)
8531 {
8532 case R_PPC64_GOT_TLSLD16:
8533 case R_PPC64_GOT_TLSLD16_LO:
8534 expecting_tls_get_addr = 1;
8535 found_tls_get_addr_arg = 1;
8536 /* Fall through. */
8537
8538 case R_PPC64_GOT_TLSLD16_HI:
8539 case R_PPC64_GOT_TLSLD16_HA:
8540 /* These relocs should never be against a symbol
8541 defined in a shared lib. Leave them alone if
8542 that turns out to be the case. */
8543 if (!is_local)
8544 continue;
8545
8546 /* LD -> LE */
8547 tls_set = 0;
8548 tls_clear = TLS_LD;
8549 tls_type = TLS_TLS | TLS_LD;
8550 break;
8551
8552 case R_PPC64_GOT_TLSGD16:
8553 case R_PPC64_GOT_TLSGD16_LO:
8554 expecting_tls_get_addr = 1;
8555 found_tls_get_addr_arg = 1;
8556 /* Fall through. */
8557
8558 case R_PPC64_GOT_TLSGD16_HI:
8559 case R_PPC64_GOT_TLSGD16_HA:
8560 if (ok_tprel)
8561 /* GD -> LE */
8562 tls_set = 0;
8563 else
8564 /* GD -> IE */
8565 tls_set = TLS_TLS | TLS_TPRELGD;
8566 tls_clear = TLS_GD;
8567 tls_type = TLS_TLS | TLS_GD;
8568 break;
8569
8570 case R_PPC64_GOT_TPREL16_DS:
8571 case R_PPC64_GOT_TPREL16_LO_DS:
8572 case R_PPC64_GOT_TPREL16_HI:
8573 case R_PPC64_GOT_TPREL16_HA:
8574 if (ok_tprel)
8575 {
8576 /* IE -> LE */
8577 tls_set = 0;
8578 tls_clear = TLS_TPREL;
8579 tls_type = TLS_TLS | TLS_TPREL;
8580 break;
8581 }
8582 continue;
8583
8584 case R_PPC64_TLSGD:
8585 case R_PPC64_TLSLD:
8586 found_tls_get_addr_arg = 1;
8587 /* Fall through. */
8588
8589 case R_PPC64_TLS:
8590 case R_PPC64_TOC16:
8591 case R_PPC64_TOC16_LO:
8592 if (sym_sec == NULL || sym_sec != toc)
8593 continue;
8594
8595 /* Mark this toc entry as referenced by a TLS
8596 code sequence. We can do that now in the
8597 case of R_PPC64_TLS, and after checking for
8598 tls_get_addr for the TOC16 relocs. */
8599 if (toc_ref == NULL)
8600 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8601 if (toc_ref == NULL)
8602 goto err_free_rel;
8603
8604 if (h != NULL)
8605 value = h->root.u.def.value;
8606 else
8607 value = sym->st_value;
8608 value += rel->r_addend;
8609 if (value % 8 != 0)
8610 continue;
8611 BFD_ASSERT (value < toc->size
8612 && toc->output_offset % 8 == 0);
8613 toc_ref_index = (value + toc->output_offset) / 8;
8614 if (r_type == R_PPC64_TLS
8615 || r_type == R_PPC64_TLSGD
8616 || r_type == R_PPC64_TLSLD)
8617 {
8618 toc_ref[toc_ref_index] = 1;
8619 continue;
8620 }
8621
8622 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8623 continue;
8624
8625 tls_set = 0;
8626 tls_clear = 0;
8627 expecting_tls_get_addr = 2;
8628 break;
8629
8630 case R_PPC64_TPREL64:
8631 if (pass == 0
8632 || sec != toc
8633 || toc_ref == NULL
8634 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8635 continue;
8636 if (ok_tprel)
8637 {
8638 /* IE -> LE */
8639 tls_set = TLS_EXPLICIT;
8640 tls_clear = TLS_TPREL;
8641 break;
8642 }
8643 continue;
8644
8645 case R_PPC64_DTPMOD64:
8646 if (pass == 0
8647 || sec != toc
8648 || toc_ref == NULL
8649 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8650 continue;
8651 if (rel + 1 < relend
8652 && (rel[1].r_info
8653 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8654 && rel[1].r_offset == rel->r_offset + 8)
8655 {
8656 if (ok_tprel)
8657 /* GD -> LE */
8658 tls_set = TLS_EXPLICIT | TLS_GD;
8659 else
8660 /* GD -> IE */
8661 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8662 tls_clear = TLS_GD;
8663 }
8664 else
8665 {
8666 if (!is_local)
8667 continue;
8668
8669 /* LD -> LE */
8670 tls_set = TLS_EXPLICIT;
8671 tls_clear = TLS_LD;
8672 }
8673 break;
8674
8675 default:
8676 continue;
8677 }
8678
8679 if (pass == 0)
8680 {
8681 if (!expecting_tls_get_addr
8682 || !sec->has_tls_get_addr_call)
8683 continue;
8684
8685 if (rel + 1 < relend
8686 && branch_reloc_hash_match (ibfd, rel + 1,
8687 htab->tls_get_addr,
8688 htab->tls_get_addr_fd))
8689 {
8690 if (expecting_tls_get_addr == 2)
8691 {
8692 /* Check for toc tls entries. */
8693 unsigned char *toc_tls;
8694 int retval;
8695
8696 retval = get_tls_mask (&toc_tls, NULL, NULL,
8697 &locsyms,
8698 rel, ibfd);
8699 if (retval == 0)
8700 goto err_free_rel;
8701 if (toc_tls != NULL)
8702 {
8703 if ((*toc_tls & TLS_TLS) != 0
8704 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8705 found_tls_get_addr_arg = 1;
8706 if (retval > 1)
8707 toc_ref[toc_ref_index] = 1;
8708 }
8709 }
8710 continue;
8711 }
8712
8713 /* Uh oh, we didn't find the expected call. We
8714 could just mark this symbol to exclude it
8715 from tls optimization but it's safer to skip
8716 the entire optimization. */
8717 /* xgettext:c-format */
8718 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8719 "TLS optimization disabled\n"),
8720 ibfd, sec, rel->r_offset);
8721 ret = TRUE;
8722 goto err_free_rel;
8723 }
8724
8725 /* If we don't have old-style __tls_get_addr calls
8726 without TLSGD/TLSLD marker relocs, and we haven't
8727 found a new-style __tls_get_addr call with a
8728 marker for this symbol, then we either have a
8729 broken object file or an -mlongcall style
8730 indirect call to __tls_get_addr without a marker.
8731 Disable optimization in this case. */
8732 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8733 && (tls_set & TLS_EXPLICIT) == 0
8734 && !sec->has_tls_get_addr_call
8735 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8736 != (TLS_TLS | TLS_MARK)))
8737 continue;
8738
8739 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8740 {
8741 struct plt_entry *ent;
8742 for (ent = htab->tls_get_addr->elf.plt.plist;
8743 ent != NULL;
8744 ent = ent->next)
8745 if (ent->addend == 0)
8746 {
8747 if (ent->plt.refcount > 0)
8748 {
8749 ent->plt.refcount -= 1;
8750 expecting_tls_get_addr = 0;
8751 }
8752 break;
8753 }
8754 }
8755
8756 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8757 {
8758 struct plt_entry *ent;
8759 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8760 ent != NULL;
8761 ent = ent->next)
8762 if (ent->addend == 0)
8763 {
8764 if (ent->plt.refcount > 0)
8765 ent->plt.refcount -= 1;
8766 break;
8767 }
8768 }
8769
8770 if (tls_clear == 0)
8771 continue;
8772
8773 if ((tls_set & TLS_EXPLICIT) == 0)
8774 {
8775 struct got_entry *ent;
8776
8777 /* Adjust got entry for this reloc. */
8778 if (h != NULL)
8779 ent = h->got.glist;
8780 else
8781 ent = elf_local_got_ents (ibfd)[r_symndx];
8782
8783 for (; ent != NULL; ent = ent->next)
8784 if (ent->addend == rel->r_addend
8785 && ent->owner == ibfd
8786 && ent->tls_type == tls_type)
8787 break;
8788 if (ent == NULL)
8789 abort ();
8790
8791 if (tls_set == 0)
8792 {
8793 /* We managed to get rid of a got entry. */
8794 if (ent->got.refcount > 0)
8795 ent->got.refcount -= 1;
8796 }
8797 }
8798 else
8799 {
8800 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8801 we'll lose one or two dyn relocs. */
8802 if (!dec_dynrel_count (rel->r_info, sec, info,
8803 NULL, h, sym))
8804 return FALSE;
8805
8806 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8807 {
8808 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8809 NULL, h, sym))
8810 return FALSE;
8811 }
8812 }
8813
8814 *tls_mask |= tls_set;
8815 *tls_mask &= ~tls_clear;
8816 }
8817
8818 if (elf_section_data (sec)->relocs != relstart)
8819 free (relstart);
8820 }
8821
8822 if (locsyms != NULL
8823 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8824 {
8825 if (!info->keep_memory)
8826 free (locsyms);
8827 else
8828 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8829 }
8830 }
8831
8832 if (toc_ref != NULL)
8833 free (toc_ref);
8834 htab->do_tls_opt = 1;
8835 return TRUE;
8836 }
8837
8838 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8839 the values of any global symbols in a toc section that has been
8840 edited. Globals in toc sections should be a rarity, so this function
8841 sets a flag if any are found in toc sections other than the one just
8842 edited, so that further hash table traversals can be avoided. */
8843
8844 struct adjust_toc_info
8845 {
8846 asection *toc;
8847 unsigned long *skip;
8848 bfd_boolean global_toc_syms;
8849 };
8850
8851 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8852
8853 static bfd_boolean
8854 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8855 {
8856 struct ppc_link_hash_entry *eh;
8857 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8858 unsigned long i;
8859
8860 if (h->root.type != bfd_link_hash_defined
8861 && h->root.type != bfd_link_hash_defweak)
8862 return TRUE;
8863
8864 eh = (struct ppc_link_hash_entry *) h;
8865 if (eh->adjust_done)
8866 return TRUE;
8867
8868 if (eh->elf.root.u.def.section == toc_inf->toc)
8869 {
8870 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8871 i = toc_inf->toc->rawsize >> 3;
8872 else
8873 i = eh->elf.root.u.def.value >> 3;
8874
8875 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8876 {
8877 _bfd_error_handler
8878 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8879 do
8880 ++i;
8881 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8882 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8883 }
8884
8885 eh->elf.root.u.def.value -= toc_inf->skip[i];
8886 eh->adjust_done = 1;
8887 }
8888 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8889 toc_inf->global_toc_syms = TRUE;
8890
8891 return TRUE;
8892 }
8893
8894 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8895 on a _LO variety toc/got reloc. */
8896
8897 static bfd_boolean
8898 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8899 {
8900 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8901 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8902 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8903 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8904 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8905 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8906 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8907 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8908 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8909 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8910 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8911 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8912 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8913 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8914 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8915 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8916 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8917 /* Exclude lfqu by testing reloc. If relocs are ever
8918 defined for the reduced D field in psq_lu then those
8919 will need testing too. */
8920 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8921 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8922 && (insn & 1) == 0)
8923 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8924 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8925 /* Exclude stfqu. psq_stu as above for psq_lu. */
8926 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8927 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8928 && (insn & 1) == 0));
8929 }
8930
8931 /* Examine all relocs referencing .toc sections in order to remove
8932 unused .toc entries. */
8933
8934 bfd_boolean
8935 ppc64_elf_edit_toc (struct bfd_link_info *info)
8936 {
8937 bfd *ibfd;
8938 struct adjust_toc_info toc_inf;
8939 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8940
8941 htab->do_toc_opt = 1;
8942 toc_inf.global_toc_syms = TRUE;
8943 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8944 {
8945 asection *toc, *sec;
8946 Elf_Internal_Shdr *symtab_hdr;
8947 Elf_Internal_Sym *local_syms;
8948 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8949 unsigned long *skip, *drop;
8950 unsigned char *used;
8951 unsigned char *keep, last, some_unused;
8952
8953 if (!is_ppc64_elf (ibfd))
8954 continue;
8955
8956 toc = bfd_get_section_by_name (ibfd, ".toc");
8957 if (toc == NULL
8958 || toc->size == 0
8959 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8960 || discarded_section (toc))
8961 continue;
8962
8963 toc_relocs = NULL;
8964 local_syms = NULL;
8965 symtab_hdr = &elf_symtab_hdr (ibfd);
8966
8967 /* Look at sections dropped from the final link. */
8968 skip = NULL;
8969 relstart = NULL;
8970 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8971 {
8972 if (sec->reloc_count == 0
8973 || !discarded_section (sec)
8974 || get_opd_info (sec)
8975 || (sec->flags & SEC_ALLOC) == 0
8976 || (sec->flags & SEC_DEBUGGING) != 0)
8977 continue;
8978
8979 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8980 if (relstart == NULL)
8981 goto error_ret;
8982
8983 /* Run through the relocs to see which toc entries might be
8984 unused. */
8985 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8986 {
8987 enum elf_ppc64_reloc_type r_type;
8988 unsigned long r_symndx;
8989 asection *sym_sec;
8990 struct elf_link_hash_entry *h;
8991 Elf_Internal_Sym *sym;
8992 bfd_vma val;
8993
8994 r_type = ELF64_R_TYPE (rel->r_info);
8995 switch (r_type)
8996 {
8997 default:
8998 continue;
8999
9000 case R_PPC64_TOC16:
9001 case R_PPC64_TOC16_LO:
9002 case R_PPC64_TOC16_HI:
9003 case R_PPC64_TOC16_HA:
9004 case R_PPC64_TOC16_DS:
9005 case R_PPC64_TOC16_LO_DS:
9006 break;
9007 }
9008
9009 r_symndx = ELF64_R_SYM (rel->r_info);
9010 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9011 r_symndx, ibfd))
9012 goto error_ret;
9013
9014 if (sym_sec != toc)
9015 continue;
9016
9017 if (h != NULL)
9018 val = h->root.u.def.value;
9019 else
9020 val = sym->st_value;
9021 val += rel->r_addend;
9022
9023 if (val >= toc->size)
9024 continue;
9025
9026 /* Anything in the toc ought to be aligned to 8 bytes.
9027 If not, don't mark as unused. */
9028 if (val & 7)
9029 continue;
9030
9031 if (skip == NULL)
9032 {
9033 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9034 if (skip == NULL)
9035 goto error_ret;
9036 }
9037
9038 skip[val >> 3] = ref_from_discarded;
9039 }
9040
9041 if (elf_section_data (sec)->relocs != relstart)
9042 free (relstart);
9043 }
9044
9045 /* For largetoc loads of address constants, we can convert
9046 . addis rx,2,addr@got@ha
9047 . ld ry,addr@got@l(rx)
9048 to
9049 . addis rx,2,addr@toc@ha
9050 . addi ry,rx,addr@toc@l
9051 when addr is within 2G of the toc pointer. This then means
9052 that the word storing "addr" in the toc is no longer needed. */
9053
9054 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9055 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9056 && toc->reloc_count != 0)
9057 {
9058 /* Read toc relocs. */
9059 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9060 info->keep_memory);
9061 if (toc_relocs == NULL)
9062 goto error_ret;
9063
9064 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9065 {
9066 enum elf_ppc64_reloc_type r_type;
9067 unsigned long r_symndx;
9068 asection *sym_sec;
9069 struct elf_link_hash_entry *h;
9070 Elf_Internal_Sym *sym;
9071 bfd_vma val, addr;
9072
9073 r_type = ELF64_R_TYPE (rel->r_info);
9074 if (r_type != R_PPC64_ADDR64)
9075 continue;
9076
9077 r_symndx = ELF64_R_SYM (rel->r_info);
9078 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9079 r_symndx, ibfd))
9080 goto error_ret;
9081
9082 if (sym_sec == NULL
9083 || sym_sec->output_section == NULL
9084 || discarded_section (sym_sec))
9085 continue;
9086
9087 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9088 continue;
9089
9090 if (h != NULL)
9091 {
9092 if (h->type == STT_GNU_IFUNC)
9093 continue;
9094 val = h->root.u.def.value;
9095 }
9096 else
9097 {
9098 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9099 continue;
9100 val = sym->st_value;
9101 }
9102 val += rel->r_addend;
9103 val += sym_sec->output_section->vma + sym_sec->output_offset;
9104
9105 /* We don't yet know the exact toc pointer value, but we
9106 know it will be somewhere in the toc section. Don't
9107 optimize if the difference from any possible toc
9108 pointer is outside [ff..f80008000, 7fff7fff]. */
9109 addr = toc->output_section->vma + TOC_BASE_OFF;
9110 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9111 continue;
9112
9113 addr = toc->output_section->vma + toc->output_section->rawsize;
9114 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9115 continue;
9116
9117 if (skip == NULL)
9118 {
9119 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9120 if (skip == NULL)
9121 goto error_ret;
9122 }
9123
9124 skip[rel->r_offset >> 3]
9125 |= can_optimize | ((rel - toc_relocs) << 2);
9126 }
9127 }
9128
9129 if (skip == NULL)
9130 continue;
9131
9132 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9133 if (used == NULL)
9134 {
9135 error_ret:
9136 if (local_syms != NULL
9137 && symtab_hdr->contents != (unsigned char *) local_syms)
9138 free (local_syms);
9139 if (sec != NULL
9140 && relstart != NULL
9141 && elf_section_data (sec)->relocs != relstart)
9142 free (relstart);
9143 if (toc_relocs != NULL
9144 && elf_section_data (toc)->relocs != toc_relocs)
9145 free (toc_relocs);
9146 if (skip != NULL)
9147 free (skip);
9148 return FALSE;
9149 }
9150
9151 /* Now check all kept sections that might reference the toc.
9152 Check the toc itself last. */
9153 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9154 : ibfd->sections);
9155 sec != NULL;
9156 sec = (sec == toc ? NULL
9157 : sec->next == NULL ? toc
9158 : sec->next == toc && toc->next ? toc->next
9159 : sec->next))
9160 {
9161 int repeat;
9162
9163 if (sec->reloc_count == 0
9164 || discarded_section (sec)
9165 || get_opd_info (sec)
9166 || (sec->flags & SEC_ALLOC) == 0
9167 || (sec->flags & SEC_DEBUGGING) != 0)
9168 continue;
9169
9170 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9171 info->keep_memory);
9172 if (relstart == NULL)
9173 {
9174 free (used);
9175 goto error_ret;
9176 }
9177
9178 /* Mark toc entries referenced as used. */
9179 do
9180 {
9181 repeat = 0;
9182 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9183 {
9184 enum elf_ppc64_reloc_type r_type;
9185 unsigned long r_symndx;
9186 asection *sym_sec;
9187 struct elf_link_hash_entry *h;
9188 Elf_Internal_Sym *sym;
9189 bfd_vma val;
9190 enum {no_check, check_lo, check_ha} insn_check;
9191
9192 r_type = ELF64_R_TYPE (rel->r_info);
9193 switch (r_type)
9194 {
9195 default:
9196 insn_check = no_check;
9197 break;
9198
9199 case R_PPC64_GOT_TLSLD16_HA:
9200 case R_PPC64_GOT_TLSGD16_HA:
9201 case R_PPC64_GOT_TPREL16_HA:
9202 case R_PPC64_GOT_DTPREL16_HA:
9203 case R_PPC64_GOT16_HA:
9204 case R_PPC64_TOC16_HA:
9205 insn_check = check_ha;
9206 break;
9207
9208 case R_PPC64_GOT_TLSLD16_LO:
9209 case R_PPC64_GOT_TLSGD16_LO:
9210 case R_PPC64_GOT_TPREL16_LO_DS:
9211 case R_PPC64_GOT_DTPREL16_LO_DS:
9212 case R_PPC64_GOT16_LO:
9213 case R_PPC64_GOT16_LO_DS:
9214 case R_PPC64_TOC16_LO:
9215 case R_PPC64_TOC16_LO_DS:
9216 insn_check = check_lo;
9217 break;
9218 }
9219
9220 if (insn_check != no_check)
9221 {
9222 bfd_vma off = rel->r_offset & ~3;
9223 unsigned char buf[4];
9224 unsigned int insn;
9225
9226 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9227 {
9228 free (used);
9229 goto error_ret;
9230 }
9231 insn = bfd_get_32 (ibfd, buf);
9232 if (insn_check == check_lo
9233 ? !ok_lo_toc_insn (insn, r_type)
9234 : ((insn & ((0x3f << 26) | 0x1f << 16))
9235 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9236 {
9237 char str[12];
9238
9239 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9240 sprintf (str, "%#08x", insn);
9241 info->callbacks->einfo
9242 /* xgettext:c-format */
9243 (_("%H: toc optimization is not supported for"
9244 " %s instruction\n"),
9245 ibfd, sec, rel->r_offset & ~3, str);
9246 }
9247 }
9248
9249 switch (r_type)
9250 {
9251 case R_PPC64_TOC16:
9252 case R_PPC64_TOC16_LO:
9253 case R_PPC64_TOC16_HI:
9254 case R_PPC64_TOC16_HA:
9255 case R_PPC64_TOC16_DS:
9256 case R_PPC64_TOC16_LO_DS:
9257 /* In case we're taking addresses of toc entries. */
9258 case R_PPC64_ADDR64:
9259 break;
9260
9261 default:
9262 continue;
9263 }
9264
9265 r_symndx = ELF64_R_SYM (rel->r_info);
9266 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9267 r_symndx, ibfd))
9268 {
9269 free (used);
9270 goto error_ret;
9271 }
9272
9273 if (sym_sec != toc)
9274 continue;
9275
9276 if (h != NULL)
9277 val = h->root.u.def.value;
9278 else
9279 val = sym->st_value;
9280 val += rel->r_addend;
9281
9282 if (val >= toc->size)
9283 continue;
9284
9285 if ((skip[val >> 3] & can_optimize) != 0)
9286 {
9287 bfd_vma off;
9288 unsigned char opc;
9289
9290 switch (r_type)
9291 {
9292 case R_PPC64_TOC16_HA:
9293 break;
9294
9295 case R_PPC64_TOC16_LO_DS:
9296 off = rel->r_offset;
9297 off += (bfd_big_endian (ibfd) ? -2 : 3);
9298 if (!bfd_get_section_contents (ibfd, sec, &opc,
9299 off, 1))
9300 {
9301 free (used);
9302 goto error_ret;
9303 }
9304 if ((opc & (0x3f << 2)) == (58u << 2))
9305 break;
9306 /* Fall through. */
9307
9308 default:
9309 /* Wrong sort of reloc, or not a ld. We may
9310 as well clear ref_from_discarded too. */
9311 skip[val >> 3] = 0;
9312 }
9313 }
9314
9315 if (sec != toc)
9316 used[val >> 3] = 1;
9317 /* For the toc section, we only mark as used if this
9318 entry itself isn't unused. */
9319 else if ((used[rel->r_offset >> 3]
9320 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9321 && !used[val >> 3])
9322 {
9323 /* Do all the relocs again, to catch reference
9324 chains. */
9325 repeat = 1;
9326 used[val >> 3] = 1;
9327 }
9328 }
9329 }
9330 while (repeat);
9331
9332 if (elf_section_data (sec)->relocs != relstart)
9333 free (relstart);
9334 }
9335
9336 /* Merge the used and skip arrays. Assume that TOC
9337 doublewords not appearing as either used or unused belong
9338 to an entry more than one doubleword in size. */
9339 for (drop = skip, keep = used, last = 0, some_unused = 0;
9340 drop < skip + (toc->size + 7) / 8;
9341 ++drop, ++keep)
9342 {
9343 if (*keep)
9344 {
9345 *drop &= ~ref_from_discarded;
9346 if ((*drop & can_optimize) != 0)
9347 some_unused = 1;
9348 last = 0;
9349 }
9350 else if ((*drop & ref_from_discarded) != 0)
9351 {
9352 some_unused = 1;
9353 last = ref_from_discarded;
9354 }
9355 else
9356 *drop = last;
9357 }
9358
9359 free (used);
9360
9361 if (some_unused)
9362 {
9363 bfd_byte *contents, *src;
9364 unsigned long off;
9365 Elf_Internal_Sym *sym;
9366 bfd_boolean local_toc_syms = FALSE;
9367
9368 /* Shuffle the toc contents, and at the same time convert the
9369 skip array from booleans into offsets. */
9370 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9371 goto error_ret;
9372
9373 elf_section_data (toc)->this_hdr.contents = contents;
9374
9375 for (src = contents, off = 0, drop = skip;
9376 src < contents + toc->size;
9377 src += 8, ++drop)
9378 {
9379 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9380 off += 8;
9381 else if (off != 0)
9382 {
9383 *drop = off;
9384 memcpy (src - off, src, 8);
9385 }
9386 }
9387 *drop = off;
9388 toc->rawsize = toc->size;
9389 toc->size = src - contents - off;
9390
9391 /* Adjust addends for relocs against the toc section sym,
9392 and optimize any accesses we can. */
9393 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9394 {
9395 if (sec->reloc_count == 0
9396 || discarded_section (sec))
9397 continue;
9398
9399 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9400 info->keep_memory);
9401 if (relstart == NULL)
9402 goto error_ret;
9403
9404 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9405 {
9406 enum elf_ppc64_reloc_type r_type;
9407 unsigned long r_symndx;
9408 asection *sym_sec;
9409 struct elf_link_hash_entry *h;
9410 bfd_vma val;
9411
9412 r_type = ELF64_R_TYPE (rel->r_info);
9413 switch (r_type)
9414 {
9415 default:
9416 continue;
9417
9418 case R_PPC64_TOC16:
9419 case R_PPC64_TOC16_LO:
9420 case R_PPC64_TOC16_HI:
9421 case R_PPC64_TOC16_HA:
9422 case R_PPC64_TOC16_DS:
9423 case R_PPC64_TOC16_LO_DS:
9424 case R_PPC64_ADDR64:
9425 break;
9426 }
9427
9428 r_symndx = ELF64_R_SYM (rel->r_info);
9429 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9430 r_symndx, ibfd))
9431 goto error_ret;
9432
9433 if (sym_sec != toc)
9434 continue;
9435
9436 if (h != NULL)
9437 val = h->root.u.def.value;
9438 else
9439 {
9440 val = sym->st_value;
9441 if (val != 0)
9442 local_toc_syms = TRUE;
9443 }
9444
9445 val += rel->r_addend;
9446
9447 if (val > toc->rawsize)
9448 val = toc->rawsize;
9449 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9450 continue;
9451 else if ((skip[val >> 3] & can_optimize) != 0)
9452 {
9453 Elf_Internal_Rela *tocrel
9454 = toc_relocs + (skip[val >> 3] >> 2);
9455 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9456
9457 switch (r_type)
9458 {
9459 case R_PPC64_TOC16_HA:
9460 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9461 break;
9462
9463 case R_PPC64_TOC16_LO_DS:
9464 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9465 break;
9466
9467 default:
9468 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9469 ppc_howto_init ();
9470 info->callbacks->einfo
9471 /* xgettext:c-format */
9472 (_("%H: %s references "
9473 "optimized away TOC entry\n"),
9474 ibfd, sec, rel->r_offset,
9475 ppc64_elf_howto_table[r_type]->name);
9476 bfd_set_error (bfd_error_bad_value);
9477 goto error_ret;
9478 }
9479 rel->r_addend = tocrel->r_addend;
9480 elf_section_data (sec)->relocs = relstart;
9481 continue;
9482 }
9483
9484 if (h != NULL || sym->st_value != 0)
9485 continue;
9486
9487 rel->r_addend -= skip[val >> 3];
9488 elf_section_data (sec)->relocs = relstart;
9489 }
9490
9491 if (elf_section_data (sec)->relocs != relstart)
9492 free (relstart);
9493 }
9494
9495 /* We shouldn't have local or global symbols defined in the TOC,
9496 but handle them anyway. */
9497 if (local_syms != NULL)
9498 for (sym = local_syms;
9499 sym < local_syms + symtab_hdr->sh_info;
9500 ++sym)
9501 if (sym->st_value != 0
9502 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9503 {
9504 unsigned long i;
9505
9506 if (sym->st_value > toc->rawsize)
9507 i = toc->rawsize >> 3;
9508 else
9509 i = sym->st_value >> 3;
9510
9511 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9512 {
9513 if (local_toc_syms)
9514 _bfd_error_handler
9515 (_("%s defined on removed toc entry"),
9516 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9517 do
9518 ++i;
9519 while ((skip[i] & (ref_from_discarded | can_optimize)));
9520 sym->st_value = (bfd_vma) i << 3;
9521 }
9522
9523 sym->st_value -= skip[i];
9524 symtab_hdr->contents = (unsigned char *) local_syms;
9525 }
9526
9527 /* Adjust any global syms defined in this toc input section. */
9528 if (toc_inf.global_toc_syms)
9529 {
9530 toc_inf.toc = toc;
9531 toc_inf.skip = skip;
9532 toc_inf.global_toc_syms = FALSE;
9533 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9534 &toc_inf);
9535 }
9536
9537 if (toc->reloc_count != 0)
9538 {
9539 Elf_Internal_Shdr *rel_hdr;
9540 Elf_Internal_Rela *wrel;
9541 bfd_size_type sz;
9542
9543 /* Remove unused toc relocs, and adjust those we keep. */
9544 if (toc_relocs == NULL)
9545 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9546 info->keep_memory);
9547 if (toc_relocs == NULL)
9548 goto error_ret;
9549
9550 wrel = toc_relocs;
9551 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9552 if ((skip[rel->r_offset >> 3]
9553 & (ref_from_discarded | can_optimize)) == 0)
9554 {
9555 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9556 wrel->r_info = rel->r_info;
9557 wrel->r_addend = rel->r_addend;
9558 ++wrel;
9559 }
9560 else if (!dec_dynrel_count (rel->r_info, toc, info,
9561 &local_syms, NULL, NULL))
9562 goto error_ret;
9563
9564 elf_section_data (toc)->relocs = toc_relocs;
9565 toc->reloc_count = wrel - toc_relocs;
9566 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9567 sz = rel_hdr->sh_entsize;
9568 rel_hdr->sh_size = toc->reloc_count * sz;
9569 }
9570 }
9571 else if (toc_relocs != NULL
9572 && elf_section_data (toc)->relocs != toc_relocs)
9573 free (toc_relocs);
9574
9575 if (local_syms != NULL
9576 && symtab_hdr->contents != (unsigned char *) local_syms)
9577 {
9578 if (!info->keep_memory)
9579 free (local_syms);
9580 else
9581 symtab_hdr->contents = (unsigned char *) local_syms;
9582 }
9583 free (skip);
9584 }
9585
9586 return TRUE;
9587 }
9588
9589 /* Return true iff input section I references the TOC using
9590 instructions limited to +/-32k offsets. */
9591
9592 bfd_boolean
9593 ppc64_elf_has_small_toc_reloc (asection *i)
9594 {
9595 return (is_ppc64_elf (i->owner)
9596 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9597 }
9598
9599 /* Allocate space for one GOT entry. */
9600
9601 static void
9602 allocate_got (struct elf_link_hash_entry *h,
9603 struct bfd_link_info *info,
9604 struct got_entry *gent)
9605 {
9606 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9607 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9608 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9609 ? 16 : 8);
9610 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9611 ? 2 : 1) * sizeof (Elf64_External_Rela);
9612 asection *got = ppc64_elf_tdata (gent->owner)->got;
9613
9614 gent->got.offset = got->size;
9615 got->size += entsize;
9616
9617 if (h->type == STT_GNU_IFUNC)
9618 {
9619 htab->elf.irelplt->size += rentsize;
9620 htab->got_reli_size += rentsize;
9621 }
9622 else if (((bfd_link_pic (info)
9623 && !((gent->tls_type & TLS_TPREL) != 0
9624 && bfd_link_executable (info)
9625 && SYMBOL_REFERENCES_LOCAL (info, h)))
9626 || (htab->elf.dynamic_sections_created
9627 && h->dynindx != -1
9628 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9629 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9630 {
9631 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9632 relgot->size += rentsize;
9633 }
9634 }
9635
9636 /* This function merges got entries in the same toc group. */
9637
9638 static void
9639 merge_got_entries (struct got_entry **pent)
9640 {
9641 struct got_entry *ent, *ent2;
9642
9643 for (ent = *pent; ent != NULL; ent = ent->next)
9644 if (!ent->is_indirect)
9645 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9646 if (!ent2->is_indirect
9647 && ent2->addend == ent->addend
9648 && ent2->tls_type == ent->tls_type
9649 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9650 {
9651 ent2->is_indirect = TRUE;
9652 ent2->got.ent = ent;
9653 }
9654 }
9655
9656 /* If H is undefined, make it dynamic if that makes sense. */
9657
9658 static bfd_boolean
9659 ensure_undef_dynamic (struct bfd_link_info *info,
9660 struct elf_link_hash_entry *h)
9661 {
9662 struct elf_link_hash_table *htab = elf_hash_table (info);
9663
9664 if (htab->dynamic_sections_created
9665 && ((info->dynamic_undefined_weak != 0
9666 && h->root.type == bfd_link_hash_undefweak)
9667 || h->root.type == bfd_link_hash_undefined)
9668 && h->dynindx == -1
9669 && !h->forced_local
9670 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9671 return bfd_elf_link_record_dynamic_symbol (info, h);
9672 return TRUE;
9673 }
9674
9675 /* Allocate space in .plt, .got and associated reloc sections for
9676 dynamic relocs. */
9677
9678 static bfd_boolean
9679 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9680 {
9681 struct bfd_link_info *info;
9682 struct ppc_link_hash_table *htab;
9683 asection *s;
9684 struct ppc_link_hash_entry *eh;
9685 struct got_entry **pgent, *gent;
9686
9687 if (h->root.type == bfd_link_hash_indirect)
9688 return TRUE;
9689
9690 info = (struct bfd_link_info *) inf;
9691 htab = ppc_hash_table (info);
9692 if (htab == NULL)
9693 return FALSE;
9694
9695 eh = (struct ppc_link_hash_entry *) h;
9696 /* Run through the TLS GD got entries first if we're changing them
9697 to TPREL. */
9698 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
9699 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9700 if (gent->got.refcount > 0
9701 && (gent->tls_type & TLS_GD) != 0)
9702 {
9703 /* This was a GD entry that has been converted to TPREL. If
9704 there happens to be a TPREL entry we can use that one. */
9705 struct got_entry *ent;
9706 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9707 if (ent->got.refcount > 0
9708 && (ent->tls_type & TLS_TPREL) != 0
9709 && ent->addend == gent->addend
9710 && ent->owner == gent->owner)
9711 {
9712 gent->got.refcount = 0;
9713 break;
9714 }
9715
9716 /* If not, then we'll be using our own TPREL entry. */
9717 if (gent->got.refcount != 0)
9718 gent->tls_type = TLS_TLS | TLS_TPREL;
9719 }
9720
9721 /* Remove any list entry that won't generate a word in the GOT before
9722 we call merge_got_entries. Otherwise we risk merging to empty
9723 entries. */
9724 pgent = &h->got.glist;
9725 while ((gent = *pgent) != NULL)
9726 if (gent->got.refcount > 0)
9727 {
9728 if ((gent->tls_type & TLS_LD) != 0
9729 && !h->def_dynamic)
9730 {
9731 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9732 *pgent = gent->next;
9733 }
9734 else
9735 pgent = &gent->next;
9736 }
9737 else
9738 *pgent = gent->next;
9739
9740 if (!htab->do_multi_toc)
9741 merge_got_entries (&h->got.glist);
9742
9743 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9744 if (!gent->is_indirect)
9745 {
9746 /* Make sure this symbol is output as a dynamic symbol. */
9747 if (!ensure_undef_dynamic (info, h))
9748 return FALSE;
9749
9750 if (!is_ppc64_elf (gent->owner))
9751 abort ();
9752
9753 allocate_got (h, info, gent);
9754 }
9755
9756 /* If no dynamic sections we can't have dynamic relocs, except for
9757 IFUNCs which are handled even in static executables. */
9758 if (!htab->elf.dynamic_sections_created
9759 && h->type != STT_GNU_IFUNC)
9760 eh->dyn_relocs = NULL;
9761
9762 /* Discard relocs on undefined symbols that must be local. */
9763 else if (h->root.type == bfd_link_hash_undefined
9764 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9765 eh->dyn_relocs = NULL;
9766
9767 /* Also discard relocs on undefined weak syms with non-default
9768 visibility, or when dynamic_undefined_weak says so. */
9769 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9770 eh->dyn_relocs = NULL;
9771
9772 if (eh->dyn_relocs != NULL)
9773 {
9774 struct elf_dyn_relocs *p, **pp;
9775
9776 /* In the shared -Bsymbolic case, discard space allocated for
9777 dynamic pc-relative relocs against symbols which turn out to
9778 be defined in regular objects. For the normal shared case,
9779 discard space for relocs that have become local due to symbol
9780 visibility changes. */
9781
9782 if (bfd_link_pic (info))
9783 {
9784 /* Relocs that use pc_count are those that appear on a call
9785 insn, or certain REL relocs (see must_be_dyn_reloc) that
9786 can be generated via assembly. We want calls to
9787 protected symbols to resolve directly to the function
9788 rather than going via the plt. If people want function
9789 pointer comparisons to work as expected then they should
9790 avoid writing weird assembly. */
9791 if (SYMBOL_CALLS_LOCAL (info, h))
9792 {
9793 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9794 {
9795 p->count -= p->pc_count;
9796 p->pc_count = 0;
9797 if (p->count == 0)
9798 *pp = p->next;
9799 else
9800 pp = &p->next;
9801 }
9802 }
9803
9804 if (eh->dyn_relocs != NULL)
9805 {
9806 /* Make sure this symbol is output as a dynamic symbol. */
9807 if (!ensure_undef_dynamic (info, h))
9808 return FALSE;
9809 }
9810 }
9811 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
9812 {
9813 /* For the non-pic case, discard space for relocs against
9814 symbols which turn out to need copy relocs or are not
9815 dynamic. */
9816 if (h->dynamic_adjusted
9817 && !h->def_regular
9818 && !ELF_COMMON_DEF_P (h))
9819 {
9820 /* Make sure this symbol is output as a dynamic symbol. */
9821 if (!ensure_undef_dynamic (info, h))
9822 return FALSE;
9823
9824 if (h->dynindx == -1)
9825 eh->dyn_relocs = NULL;
9826 }
9827 else
9828 eh->dyn_relocs = NULL;
9829 }
9830
9831 /* Finally, allocate space. */
9832 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9833 {
9834 asection *sreloc = elf_section_data (p->sec)->sreloc;
9835 if (eh->elf.type == STT_GNU_IFUNC)
9836 sreloc = htab->elf.irelplt;
9837 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9838 }
9839 }
9840
9841 /* We might need a PLT entry when the symbol
9842 a) is dynamic, or
9843 b) is an ifunc, or
9844 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9845 d) has plt16 relocs and we are linking statically. */
9846 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9847 || h->type == STT_GNU_IFUNC
9848 || (h->needs_plt && h->dynamic_adjusted)
9849 || (h->needs_plt
9850 && h->def_regular
9851 && !htab->elf.dynamic_sections_created
9852 && (((struct ppc_link_hash_entry *) h)->tls_mask
9853 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
9854 {
9855 struct plt_entry *pent;
9856 bfd_boolean doneone = FALSE;
9857 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9858 if (pent->plt.refcount > 0)
9859 {
9860 if (!htab->elf.dynamic_sections_created
9861 || h->dynindx == -1)
9862 {
9863 if (h->type == STT_GNU_IFUNC)
9864 {
9865 s = htab->elf.iplt;
9866 pent->plt.offset = s->size;
9867 s->size += PLT_ENTRY_SIZE (htab);
9868 s = htab->elf.irelplt;
9869 }
9870 else
9871 {
9872 s = htab->pltlocal;
9873 pent->plt.offset = s->size;
9874 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9875 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9876 }
9877 }
9878 else
9879 {
9880 /* If this is the first .plt entry, make room for the special
9881 first entry. */
9882 s = htab->elf.splt;
9883 if (s->size == 0)
9884 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9885
9886 pent->plt.offset = s->size;
9887
9888 /* Make room for this entry. */
9889 s->size += PLT_ENTRY_SIZE (htab);
9890
9891 /* Make room for the .glink code. */
9892 s = htab->glink;
9893 if (s->size == 0)
9894 s->size += GLINK_PLTRESOLVE_SIZE (htab);
9895 if (htab->opd_abi)
9896 {
9897 /* We need bigger stubs past index 32767. */
9898 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9899 s->size += 4;
9900 s->size += 2*4;
9901 }
9902 else
9903 s->size += 4;
9904
9905 /* We also need to make an entry in the .rela.plt section. */
9906 s = htab->elf.srelplt;
9907 }
9908 if (s != NULL)
9909 s->size += sizeof (Elf64_External_Rela);
9910 doneone = TRUE;
9911 }
9912 else
9913 pent->plt.offset = (bfd_vma) -1;
9914 if (!doneone)
9915 {
9916 h->plt.plist = NULL;
9917 h->needs_plt = 0;
9918 }
9919 }
9920 else
9921 {
9922 h->plt.plist = NULL;
9923 h->needs_plt = 0;
9924 }
9925
9926 return TRUE;
9927 }
9928
9929 #define PPC_LO(v) ((v) & 0xffff)
9930 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9931 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9932
9933 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9934 to set up space for global entry stubs. These are put in glink,
9935 after the branch table. */
9936
9937 static bfd_boolean
9938 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9939 {
9940 struct bfd_link_info *info;
9941 struct ppc_link_hash_table *htab;
9942 struct plt_entry *pent;
9943 asection *s, *plt;
9944
9945 if (h->root.type == bfd_link_hash_indirect)
9946 return TRUE;
9947
9948 if (!h->pointer_equality_needed)
9949 return TRUE;
9950
9951 if (h->def_regular)
9952 return TRUE;
9953
9954 info = inf;
9955 htab = ppc_hash_table (info);
9956 if (htab == NULL)
9957 return FALSE;
9958
9959 s = htab->global_entry;
9960 plt = htab->elf.splt;
9961 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9962 if (pent->plt.offset != (bfd_vma) -1
9963 && pent->addend == 0)
9964 {
9965 /* For ELFv2, if this symbol is not defined in a regular file
9966 and we are not generating a shared library or pie, then we
9967 need to define the symbol in the executable on a call stub.
9968 This is to avoid text relocations. */
9969 bfd_vma off, stub_align, stub_off, stub_size;
9970 unsigned int align_power;
9971
9972 stub_size = 16;
9973 stub_off = s->size;
9974 if (htab->params->plt_stub_align >= 0)
9975 align_power = htab->params->plt_stub_align;
9976 else
9977 align_power = -htab->params->plt_stub_align;
9978 /* Setting section alignment is delayed until we know it is
9979 non-empty. Otherwise the .text output section will be
9980 aligned at least to plt_stub_align even when no global
9981 entry stubs are needed. */
9982 if (s->alignment_power < align_power)
9983 s->alignment_power = align_power;
9984 stub_align = (bfd_vma) 1 << align_power;
9985 if (htab->params->plt_stub_align >= 0
9986 || ((((stub_off + stub_size - 1) & -stub_align)
9987 - (stub_off & -stub_align))
9988 > ((stub_size - 1) & -stub_align)))
9989 stub_off = (stub_off + stub_align - 1) & -stub_align;
9990 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9991 off -= stub_off + s->output_offset + s->output_section->vma;
9992 /* Note that for --plt-stub-align negative we have a possible
9993 dependency between stub offset and size. Break that
9994 dependency by assuming the max stub size when calculating
9995 the stub offset. */
9996 if (PPC_HA (off) == 0)
9997 stub_size -= 4;
9998 h->root.type = bfd_link_hash_defined;
9999 h->root.u.def.section = s;
10000 h->root.u.def.value = stub_off;
10001 s->size = stub_off + stub_size;
10002 break;
10003 }
10004 return TRUE;
10005 }
10006
10007 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
10008 read-only sections. */
10009
10010 static bfd_boolean
10011 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
10012 {
10013 asection *sec;
10014
10015 if (h->root.type == bfd_link_hash_indirect)
10016 return TRUE;
10017
10018 sec = readonly_dynrelocs (h);
10019 if (sec != NULL)
10020 {
10021 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10022
10023 info->flags |= DF_TEXTREL;
10024 info->callbacks->minfo
10025 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
10026 sec->owner, h->root.root.string, sec);
10027
10028 /* Not an error, just cut short the traversal. */
10029 return FALSE;
10030 }
10031 return TRUE;
10032 }
10033
10034 /* Set the sizes of the dynamic sections. */
10035
10036 static bfd_boolean
10037 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10038 struct bfd_link_info *info)
10039 {
10040 struct ppc_link_hash_table *htab;
10041 bfd *dynobj;
10042 asection *s;
10043 bfd_boolean relocs;
10044 bfd *ibfd;
10045 struct got_entry *first_tlsld;
10046
10047 htab = ppc_hash_table (info);
10048 if (htab == NULL)
10049 return FALSE;
10050
10051 dynobj = htab->elf.dynobj;
10052 if (dynobj == NULL)
10053 abort ();
10054
10055 if (htab->elf.dynamic_sections_created)
10056 {
10057 /* Set the contents of the .interp section to the interpreter. */
10058 if (bfd_link_executable (info) && !info->nointerp)
10059 {
10060 s = bfd_get_linker_section (dynobj, ".interp");
10061 if (s == NULL)
10062 abort ();
10063 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10064 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10065 }
10066 }
10067
10068 /* Set up .got offsets for local syms, and space for local dynamic
10069 relocs. */
10070 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10071 {
10072 struct got_entry **lgot_ents;
10073 struct got_entry **end_lgot_ents;
10074 struct plt_entry **local_plt;
10075 struct plt_entry **end_local_plt;
10076 unsigned char *lgot_masks;
10077 bfd_size_type locsymcount;
10078 Elf_Internal_Shdr *symtab_hdr;
10079
10080 if (!is_ppc64_elf (ibfd))
10081 continue;
10082
10083 for (s = ibfd->sections; s != NULL; s = s->next)
10084 {
10085 struct ppc_dyn_relocs *p;
10086
10087 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10088 {
10089 if (!bfd_is_abs_section (p->sec)
10090 && bfd_is_abs_section (p->sec->output_section))
10091 {
10092 /* Input section has been discarded, either because
10093 it is a copy of a linkonce section or due to
10094 linker script /DISCARD/, so we'll be discarding
10095 the relocs too. */
10096 }
10097 else if (p->count != 0)
10098 {
10099 asection *srel = elf_section_data (p->sec)->sreloc;
10100 if (p->ifunc)
10101 srel = htab->elf.irelplt;
10102 srel->size += p->count * sizeof (Elf64_External_Rela);
10103 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10104 info->flags |= DF_TEXTREL;
10105 }
10106 }
10107 }
10108
10109 lgot_ents = elf_local_got_ents (ibfd);
10110 if (!lgot_ents)
10111 continue;
10112
10113 symtab_hdr = &elf_symtab_hdr (ibfd);
10114 locsymcount = symtab_hdr->sh_info;
10115 end_lgot_ents = lgot_ents + locsymcount;
10116 local_plt = (struct plt_entry **) end_lgot_ents;
10117 end_local_plt = local_plt + locsymcount;
10118 lgot_masks = (unsigned char *) end_local_plt;
10119 s = ppc64_elf_tdata (ibfd)->got;
10120 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10121 {
10122 struct got_entry **pent, *ent;
10123
10124 pent = lgot_ents;
10125 while ((ent = *pent) != NULL)
10126 if (ent->got.refcount > 0)
10127 {
10128 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10129 {
10130 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10131 *pent = ent->next;
10132 }
10133 else
10134 {
10135 unsigned int ent_size = 8;
10136 unsigned int rel_size = sizeof (Elf64_External_Rela);
10137
10138 ent->got.offset = s->size;
10139 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10140 {
10141 ent_size *= 2;
10142 rel_size *= 2;
10143 }
10144 s->size += ent_size;
10145 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10146 {
10147 htab->elf.irelplt->size += rel_size;
10148 htab->got_reli_size += rel_size;
10149 }
10150 else if (bfd_link_pic (info)
10151 && !((ent->tls_type & TLS_TPREL) != 0
10152 && bfd_link_executable (info)))
10153 {
10154 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10155 srel->size += rel_size;
10156 }
10157 pent = &ent->next;
10158 }
10159 }
10160 else
10161 *pent = ent->next;
10162 }
10163
10164 /* Allocate space for plt calls to local syms. */
10165 lgot_masks = (unsigned char *) end_local_plt;
10166 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10167 {
10168 struct plt_entry *ent;
10169
10170 for (ent = *local_plt; ent != NULL; ent = ent->next)
10171 if (ent->plt.refcount > 0)
10172 {
10173 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10174 {
10175 s = htab->elf.iplt;
10176 ent->plt.offset = s->size;
10177 s->size += PLT_ENTRY_SIZE (htab);
10178 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10179 }
10180 else if ((*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10181 ent->plt.offset = (bfd_vma) -1;
10182 else
10183 {
10184 s = htab->pltlocal;
10185 ent->plt.offset = s->size;
10186 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10187 if (bfd_link_pic (info))
10188 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10189 }
10190 }
10191 else
10192 ent->plt.offset = (bfd_vma) -1;
10193 }
10194 }
10195
10196 /* Allocate global sym .plt and .got entries, and space for global
10197 sym dynamic relocs. */
10198 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10199
10200 if (!htab->opd_abi && !bfd_link_pic (info))
10201 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10202
10203 first_tlsld = NULL;
10204 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10205 {
10206 struct got_entry *ent;
10207
10208 if (!is_ppc64_elf (ibfd))
10209 continue;
10210
10211 ent = ppc64_tlsld_got (ibfd);
10212 if (ent->got.refcount > 0)
10213 {
10214 if (!htab->do_multi_toc && first_tlsld != NULL)
10215 {
10216 ent->is_indirect = TRUE;
10217 ent->got.ent = first_tlsld;
10218 }
10219 else
10220 {
10221 if (first_tlsld == NULL)
10222 first_tlsld = ent;
10223 s = ppc64_elf_tdata (ibfd)->got;
10224 ent->got.offset = s->size;
10225 ent->owner = ibfd;
10226 s->size += 16;
10227 if (bfd_link_pic (info))
10228 {
10229 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10230 srel->size += sizeof (Elf64_External_Rela);
10231 }
10232 }
10233 }
10234 else
10235 ent->got.offset = (bfd_vma) -1;
10236 }
10237
10238 /* We now have determined the sizes of the various dynamic sections.
10239 Allocate memory for them. */
10240 relocs = FALSE;
10241 for (s = dynobj->sections; s != NULL; s = s->next)
10242 {
10243 if ((s->flags & SEC_LINKER_CREATED) == 0)
10244 continue;
10245
10246 if (s == htab->brlt || s == htab->relbrlt)
10247 /* These haven't been allocated yet; don't strip. */
10248 continue;
10249 else if (s == htab->elf.sgot
10250 || s == htab->elf.splt
10251 || s == htab->elf.iplt
10252 || s == htab->pltlocal
10253 || s == htab->glink
10254 || s == htab->global_entry
10255 || s == htab->elf.sdynbss
10256 || s == htab->elf.sdynrelro)
10257 {
10258 /* Strip this section if we don't need it; see the
10259 comment below. */
10260 }
10261 else if (s == htab->glink_eh_frame)
10262 {
10263 if (!bfd_is_abs_section (s->output_section))
10264 /* Not sized yet. */
10265 continue;
10266 }
10267 else if (CONST_STRNEQ (s->name, ".rela"))
10268 {
10269 if (s->size != 0)
10270 {
10271 if (s != htab->elf.srelplt)
10272 relocs = TRUE;
10273
10274 /* We use the reloc_count field as a counter if we need
10275 to copy relocs into the output file. */
10276 s->reloc_count = 0;
10277 }
10278 }
10279 else
10280 {
10281 /* It's not one of our sections, so don't allocate space. */
10282 continue;
10283 }
10284
10285 if (s->size == 0)
10286 {
10287 /* If we don't need this section, strip it from the
10288 output file. This is mostly to handle .rela.bss and
10289 .rela.plt. We must create both sections in
10290 create_dynamic_sections, because they must be created
10291 before the linker maps input sections to output
10292 sections. The linker does that before
10293 adjust_dynamic_symbol is called, and it is that
10294 function which decides whether anything needs to go
10295 into these sections. */
10296 s->flags |= SEC_EXCLUDE;
10297 continue;
10298 }
10299
10300 if (bfd_is_abs_section (s->output_section))
10301 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10302 s->name);
10303
10304 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10305 continue;
10306
10307 /* Allocate memory for the section contents. We use bfd_zalloc
10308 here in case unused entries are not reclaimed before the
10309 section's contents are written out. This should not happen,
10310 but this way if it does we get a R_PPC64_NONE reloc in .rela
10311 sections instead of garbage.
10312 We also rely on the section contents being zero when writing
10313 the GOT and .dynrelro. */
10314 s->contents = bfd_zalloc (dynobj, s->size);
10315 if (s->contents == NULL)
10316 return FALSE;
10317 }
10318
10319 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10320 {
10321 if (!is_ppc64_elf (ibfd))
10322 continue;
10323
10324 s = ppc64_elf_tdata (ibfd)->got;
10325 if (s != NULL && s != htab->elf.sgot)
10326 {
10327 if (s->size == 0)
10328 s->flags |= SEC_EXCLUDE;
10329 else
10330 {
10331 s->contents = bfd_zalloc (ibfd, s->size);
10332 if (s->contents == NULL)
10333 return FALSE;
10334 }
10335 }
10336 s = ppc64_elf_tdata (ibfd)->relgot;
10337 if (s != NULL)
10338 {
10339 if (s->size == 0)
10340 s->flags |= SEC_EXCLUDE;
10341 else
10342 {
10343 s->contents = bfd_zalloc (ibfd, s->size);
10344 if (s->contents == NULL)
10345 return FALSE;
10346 relocs = TRUE;
10347 s->reloc_count = 0;
10348 }
10349 }
10350 }
10351
10352 if (htab->elf.dynamic_sections_created)
10353 {
10354 bfd_boolean tls_opt;
10355
10356 /* Add some entries to the .dynamic section. We fill in the
10357 values later, in ppc64_elf_finish_dynamic_sections, but we
10358 must add the entries now so that we get the correct size for
10359 the .dynamic section. The DT_DEBUG entry is filled in by the
10360 dynamic linker and used by the debugger. */
10361 #define add_dynamic_entry(TAG, VAL) \
10362 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10363
10364 if (bfd_link_executable (info))
10365 {
10366 if (!add_dynamic_entry (DT_DEBUG, 0))
10367 return FALSE;
10368 }
10369
10370 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10371 {
10372 if (!add_dynamic_entry (DT_PLTGOT, 0)
10373 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10374 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10375 || !add_dynamic_entry (DT_JMPREL, 0)
10376 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10377 return FALSE;
10378 }
10379
10380 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10381 {
10382 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10383 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10384 return FALSE;
10385 }
10386
10387 tls_opt = (htab->params->tls_get_addr_opt
10388 && htab->tls_get_addr_fd != NULL
10389 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10390 if (tls_opt || !htab->opd_abi)
10391 {
10392 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10393 return FALSE;
10394 }
10395
10396 if (relocs)
10397 {
10398 if (!add_dynamic_entry (DT_RELA, 0)
10399 || !add_dynamic_entry (DT_RELASZ, 0)
10400 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10401 return FALSE;
10402
10403 /* If any dynamic relocs apply to a read-only section,
10404 then we need a DT_TEXTREL entry. */
10405 if ((info->flags & DF_TEXTREL) == 0)
10406 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10407
10408 if ((info->flags & DF_TEXTREL) != 0)
10409 {
10410 if (!add_dynamic_entry (DT_TEXTREL, 0))
10411 return FALSE;
10412 }
10413 }
10414 }
10415 #undef add_dynamic_entry
10416
10417 return TRUE;
10418 }
10419
10420 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10421
10422 static bfd_boolean
10423 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10424 {
10425 if (h->plt.plist != NULL
10426 && !h->def_regular
10427 && !h->pointer_equality_needed)
10428 return FALSE;
10429
10430 return _bfd_elf_hash_symbol (h);
10431 }
10432
10433 /* Determine the type of stub needed, if any, for a call. */
10434
10435 static inline enum ppc_stub_type
10436 ppc_type_of_stub (asection *input_sec,
10437 const Elf_Internal_Rela *rel,
10438 struct ppc_link_hash_entry **hash,
10439 struct plt_entry **plt_ent,
10440 bfd_vma destination,
10441 unsigned long local_off)
10442 {
10443 struct ppc_link_hash_entry *h = *hash;
10444 bfd_vma location;
10445 bfd_vma branch_offset;
10446 bfd_vma max_branch_offset;
10447 enum elf_ppc64_reloc_type r_type;
10448
10449 if (h != NULL)
10450 {
10451 struct plt_entry *ent;
10452 struct ppc_link_hash_entry *fdh = h;
10453 if (h->oh != NULL
10454 && h->oh->is_func_descriptor)
10455 {
10456 fdh = ppc_follow_link (h->oh);
10457 *hash = fdh;
10458 }
10459
10460 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10461 if (ent->addend == rel->r_addend
10462 && ent->plt.offset != (bfd_vma) -1)
10463 {
10464 *plt_ent = ent;
10465 return ppc_stub_plt_call;
10466 }
10467
10468 /* Here, we know we don't have a plt entry. If we don't have a
10469 either a defined function descriptor or a defined entry symbol
10470 in a regular object file, then it is pointless trying to make
10471 any other type of stub. */
10472 if (!is_static_defined (&fdh->elf)
10473 && !is_static_defined (&h->elf))
10474 return ppc_stub_none;
10475 }
10476 else if (elf_local_got_ents (input_sec->owner) != NULL)
10477 {
10478 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10479 struct plt_entry **local_plt = (struct plt_entry **)
10480 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10481 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10482
10483 if (local_plt[r_symndx] != NULL)
10484 {
10485 struct plt_entry *ent;
10486
10487 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10488 if (ent->addend == rel->r_addend
10489 && ent->plt.offset != (bfd_vma) -1)
10490 {
10491 *plt_ent = ent;
10492 return ppc_stub_plt_call;
10493 }
10494 }
10495 }
10496
10497 /* Determine where the call point is. */
10498 location = (input_sec->output_offset
10499 + input_sec->output_section->vma
10500 + rel->r_offset);
10501
10502 branch_offset = destination - location;
10503 r_type = ELF64_R_TYPE (rel->r_info);
10504
10505 /* Determine if a long branch stub is needed. */
10506 max_branch_offset = 1 << 25;
10507 if (r_type != R_PPC64_REL24)
10508 max_branch_offset = 1 << 15;
10509
10510 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10511 /* We need a stub. Figure out whether a long_branch or plt_branch
10512 is needed later. */
10513 return ppc_stub_long_branch;
10514
10515 return ppc_stub_none;
10516 }
10517
10518 /* With power7 weakly ordered memory model, it is possible for ld.so
10519 to update a plt entry in one thread and have another thread see a
10520 stale zero toc entry. To avoid this we need some sort of acquire
10521 barrier in the call stub. One solution is to make the load of the
10522 toc word seem to appear to depend on the load of the function entry
10523 word. Another solution is to test for r2 being zero, and branch to
10524 the appropriate glink entry if so.
10525
10526 . fake dep barrier compare
10527 . ld 12,xxx(2) ld 12,xxx(2)
10528 . mtctr 12 mtctr 12
10529 . xor 11,12,12 ld 2,xxx+8(2)
10530 . add 2,2,11 cmpldi 2,0
10531 . ld 2,xxx+8(2) bnectr+
10532 . bctr b <glink_entry>
10533
10534 The solution involving the compare turns out to be faster, so
10535 that's what we use unless the branch won't reach. */
10536
10537 #define ALWAYS_USE_FAKE_DEP 0
10538 #define ALWAYS_EMIT_R2SAVE 0
10539
10540 static inline unsigned int
10541 plt_stub_size (struct ppc_link_hash_table *htab,
10542 struct ppc_stub_hash_entry *stub_entry,
10543 bfd_vma off)
10544 {
10545 unsigned size = 12;
10546
10547 if (ALWAYS_EMIT_R2SAVE
10548 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10549 size += 4;
10550 if (PPC_HA (off) != 0)
10551 size += 4;
10552 if (htab->opd_abi)
10553 {
10554 size += 4;
10555 if (htab->params->plt_static_chain)
10556 size += 4;
10557 if (htab->params->plt_thread_safe
10558 && htab->elf.dynamic_sections_created
10559 && stub_entry->h != NULL
10560 && stub_entry->h->elf.dynindx != -1)
10561 size += 8;
10562 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10563 size += 4;
10564 }
10565 if (stub_entry->h != NULL
10566 && (stub_entry->h == htab->tls_get_addr_fd
10567 || stub_entry->h == htab->tls_get_addr)
10568 && htab->params->tls_get_addr_opt)
10569 {
10570 size += 7 * 4;
10571 if (ALWAYS_EMIT_R2SAVE
10572 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10573 size += 6 * 4;
10574 }
10575 return size;
10576 }
10577
10578 /* Depending on the sign of plt_stub_align:
10579 If positive, return the padding to align to a 2**plt_stub_align
10580 boundary.
10581 If negative, if this stub would cross fewer 2**plt_stub_align
10582 boundaries if we align, then return the padding needed to do so. */
10583
10584 static inline unsigned int
10585 plt_stub_pad (struct ppc_link_hash_table *htab,
10586 struct ppc_stub_hash_entry *stub_entry,
10587 bfd_vma plt_off)
10588 {
10589 int stub_align;
10590 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10591 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10592
10593 if (htab->params->plt_stub_align >= 0)
10594 {
10595 stub_align = 1 << htab->params->plt_stub_align;
10596 if ((stub_off & (stub_align - 1)) != 0)
10597 return stub_align - (stub_off & (stub_align - 1));
10598 return 0;
10599 }
10600
10601 stub_align = 1 << -htab->params->plt_stub_align;
10602 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10603 > ((stub_size - 1) & -stub_align))
10604 return stub_align - (stub_off & (stub_align - 1));
10605 return 0;
10606 }
10607
10608 /* Build a .plt call stub. */
10609
10610 static inline bfd_byte *
10611 build_plt_stub (struct ppc_link_hash_table *htab,
10612 struct ppc_stub_hash_entry *stub_entry,
10613 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10614 {
10615 bfd *obfd = htab->params->stub_bfd;
10616 bfd_boolean plt_load_toc = htab->opd_abi;
10617 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10618 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10619 && htab->elf.dynamic_sections_created
10620 && stub_entry->h != NULL
10621 && stub_entry->h->elf.dynindx != -1);
10622 bfd_boolean use_fake_dep = plt_thread_safe;
10623 bfd_vma cmp_branch_off = 0;
10624
10625 if (!ALWAYS_USE_FAKE_DEP
10626 && plt_load_toc
10627 && plt_thread_safe
10628 && !((stub_entry->h == htab->tls_get_addr_fd
10629 || stub_entry->h == htab->tls_get_addr)
10630 && htab->params->tls_get_addr_opt))
10631 {
10632 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10633 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10634 / PLT_ENTRY_SIZE (htab));
10635 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10636 bfd_vma to, from;
10637
10638 if (pltindex > 32768)
10639 glinkoff += (pltindex - 32768) * 4;
10640 to = (glinkoff
10641 + htab->glink->output_offset
10642 + htab->glink->output_section->vma);
10643 from = (p - stub_entry->group->stub_sec->contents
10644 + 4 * (ALWAYS_EMIT_R2SAVE
10645 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10646 + 4 * (PPC_HA (offset) != 0)
10647 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10648 != PPC_HA (offset))
10649 + 4 * (plt_static_chain != 0)
10650 + 20
10651 + stub_entry->group->stub_sec->output_offset
10652 + stub_entry->group->stub_sec->output_section->vma);
10653 cmp_branch_off = to - from;
10654 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10655 }
10656
10657 if (PPC_HA (offset) != 0)
10658 {
10659 if (r != NULL)
10660 {
10661 if (ALWAYS_EMIT_R2SAVE
10662 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10663 r[0].r_offset += 4;
10664 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10665 r[1].r_offset = r[0].r_offset + 4;
10666 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10667 r[1].r_addend = r[0].r_addend;
10668 if (plt_load_toc)
10669 {
10670 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10671 {
10672 r[2].r_offset = r[1].r_offset + 4;
10673 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10674 r[2].r_addend = r[0].r_addend;
10675 }
10676 else
10677 {
10678 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10679 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10680 r[2].r_addend = r[0].r_addend + 8;
10681 if (plt_static_chain)
10682 {
10683 r[3].r_offset = r[2].r_offset + 4;
10684 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10685 r[3].r_addend = r[0].r_addend + 16;
10686 }
10687 }
10688 }
10689 }
10690 if (ALWAYS_EMIT_R2SAVE
10691 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10692 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10693 if (plt_load_toc)
10694 {
10695 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10696 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10697 }
10698 else
10699 {
10700 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10701 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10702 }
10703 if (plt_load_toc
10704 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10705 {
10706 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10707 offset = 0;
10708 }
10709 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10710 if (plt_load_toc)
10711 {
10712 if (use_fake_dep)
10713 {
10714 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10715 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10716 }
10717 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10718 if (plt_static_chain)
10719 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10720 }
10721 }
10722 else
10723 {
10724 if (r != NULL)
10725 {
10726 if (ALWAYS_EMIT_R2SAVE
10727 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10728 r[0].r_offset += 4;
10729 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10730 if (plt_load_toc)
10731 {
10732 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10733 {
10734 r[1].r_offset = r[0].r_offset + 4;
10735 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10736 r[1].r_addend = r[0].r_addend;
10737 }
10738 else
10739 {
10740 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10741 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10742 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10743 if (plt_static_chain)
10744 {
10745 r[2].r_offset = r[1].r_offset + 4;
10746 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10747 r[2].r_addend = r[0].r_addend + 8;
10748 }
10749 }
10750 }
10751 }
10752 if (ALWAYS_EMIT_R2SAVE
10753 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10754 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10755 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10756 if (plt_load_toc
10757 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10758 {
10759 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10760 offset = 0;
10761 }
10762 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10763 if (plt_load_toc)
10764 {
10765 if (use_fake_dep)
10766 {
10767 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10768 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10769 }
10770 if (plt_static_chain)
10771 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10772 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10773 }
10774 }
10775 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10776 {
10777 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10778 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10779 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10780 }
10781 else
10782 bfd_put_32 (obfd, BCTR, p), p += 4;
10783 return p;
10784 }
10785
10786 /* Build a special .plt call stub for __tls_get_addr. */
10787
10788 #define LD_R11_0R3 0xe9630000
10789 #define LD_R12_0R3 0xe9830000
10790 #define MR_R0_R3 0x7c601b78
10791 #define CMPDI_R11_0 0x2c2b0000
10792 #define ADD_R3_R12_R13 0x7c6c6a14
10793 #define BEQLR 0x4d820020
10794 #define MR_R3_R0 0x7c030378
10795 #define STD_R11_0R1 0xf9610000
10796 #define BCTRL 0x4e800421
10797 #define LD_R11_0R1 0xe9610000
10798 #define MTLR_R11 0x7d6803a6
10799
10800 static inline bfd_byte *
10801 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10802 struct ppc_stub_hash_entry *stub_entry,
10803 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10804 {
10805 bfd *obfd = htab->params->stub_bfd;
10806
10807 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10808 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10809 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10810 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10811 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10812 bfd_put_32 (obfd, BEQLR, p), p += 4;
10813 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10814 if (r != NULL)
10815 r[0].r_offset += 7 * 4;
10816 if (!ALWAYS_EMIT_R2SAVE
10817 && stub_entry->stub_type != ppc_stub_plt_call_r2save)
10818 return build_plt_stub (htab, stub_entry, p, offset, r);
10819
10820 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10821 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10822
10823 if (r != NULL)
10824 r[0].r_offset += 2 * 4;
10825 p = build_plt_stub (htab, stub_entry, p, offset, r);
10826 bfd_put_32 (obfd, BCTRL, p - 4);
10827
10828 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10829 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10830 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10831 bfd_put_32 (obfd, BLR, p), p += 4;
10832
10833 return p;
10834 }
10835
10836 static Elf_Internal_Rela *
10837 get_relocs (asection *sec, int count)
10838 {
10839 Elf_Internal_Rela *relocs;
10840 struct bfd_elf_section_data *elfsec_data;
10841
10842 elfsec_data = elf_section_data (sec);
10843 relocs = elfsec_data->relocs;
10844 if (relocs == NULL)
10845 {
10846 bfd_size_type relsize;
10847 relsize = sec->reloc_count * sizeof (*relocs);
10848 relocs = bfd_alloc (sec->owner, relsize);
10849 if (relocs == NULL)
10850 return NULL;
10851 elfsec_data->relocs = relocs;
10852 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10853 sizeof (Elf_Internal_Shdr));
10854 if (elfsec_data->rela.hdr == NULL)
10855 return NULL;
10856 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10857 * sizeof (Elf64_External_Rela));
10858 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10859 sec->reloc_count = 0;
10860 }
10861 relocs += sec->reloc_count;
10862 sec->reloc_count += count;
10863 return relocs;
10864 }
10865
10866 static bfd_vma
10867 get_r2off (struct bfd_link_info *info,
10868 struct ppc_stub_hash_entry *stub_entry)
10869 {
10870 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10871 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10872
10873 if (r2off == 0)
10874 {
10875 /* Support linking -R objects. Get the toc pointer from the
10876 opd entry. */
10877 char buf[8];
10878 if (!htab->opd_abi)
10879 return r2off;
10880 asection *opd = stub_entry->h->elf.root.u.def.section;
10881 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10882
10883 if (strcmp (opd->name, ".opd") != 0
10884 || opd->reloc_count != 0)
10885 {
10886 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%pT'\n"),
10887 stub_entry->h->elf.root.root.string);
10888 bfd_set_error (bfd_error_bad_value);
10889 return (bfd_vma) -1;
10890 }
10891 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10892 return (bfd_vma) -1;
10893 r2off = bfd_get_64 (opd->owner, buf);
10894 r2off -= elf_gp (info->output_bfd);
10895 }
10896 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10897 return r2off;
10898 }
10899
10900 static bfd_boolean
10901 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10902 {
10903 struct ppc_stub_hash_entry *stub_entry;
10904 struct ppc_branch_hash_entry *br_entry;
10905 struct bfd_link_info *info;
10906 struct ppc_link_hash_table *htab;
10907 bfd_byte *loc;
10908 bfd_byte *p;
10909 bfd_vma dest, off;
10910 Elf_Internal_Rela *r;
10911 asection *plt;
10912
10913 /* Massage our args to the form they really have. */
10914 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10915 info = in_arg;
10916
10917 htab = ppc_hash_table (info);
10918 if (htab == NULL)
10919 return FALSE;
10920
10921 /* Make a note of the offset within the stubs for this entry. */
10922 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10923 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10924
10925 htab->stub_count[stub_entry->stub_type - 1] += 1;
10926 switch (stub_entry->stub_type)
10927 {
10928 case ppc_stub_long_branch:
10929 case ppc_stub_long_branch_r2off:
10930 /* Branches are relative. This is where we are going to. */
10931 dest = (stub_entry->target_value
10932 + stub_entry->target_section->output_offset
10933 + stub_entry->target_section->output_section->vma);
10934 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10935 off = dest;
10936
10937 /* And this is where we are coming from. */
10938 off -= (stub_entry->stub_offset
10939 + stub_entry->group->stub_sec->output_offset
10940 + stub_entry->group->stub_sec->output_section->vma);
10941
10942 p = loc;
10943 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10944 {
10945 bfd_vma r2off = get_r2off (info, stub_entry);
10946
10947 if (r2off == (bfd_vma) -1)
10948 {
10949 htab->stub_error = TRUE;
10950 return FALSE;
10951 }
10952 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10953 p += 4;
10954 if (PPC_HA (r2off) != 0)
10955 {
10956 bfd_put_32 (htab->params->stub_bfd,
10957 ADDIS_R2_R2 | PPC_HA (r2off), p);
10958 p += 4;
10959 }
10960 if (PPC_LO (r2off) != 0)
10961 {
10962 bfd_put_32 (htab->params->stub_bfd,
10963 ADDI_R2_R2 | PPC_LO (r2off), p);
10964 p += 4;
10965 }
10966 off -= p - loc;
10967 }
10968 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10969 p += 4;
10970
10971 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10972 {
10973 _bfd_error_handler
10974 (_("long branch stub `%s' offset overflow"),
10975 stub_entry->root.string);
10976 htab->stub_error = TRUE;
10977 return FALSE;
10978 }
10979
10980 if (info->emitrelocations)
10981 {
10982 r = get_relocs (stub_entry->group->stub_sec, 1);
10983 if (r == NULL)
10984 return FALSE;
10985 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
10986 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10987 r->r_addend = dest;
10988 if (stub_entry->h != NULL)
10989 {
10990 struct elf_link_hash_entry **hashes;
10991 unsigned long symndx;
10992 struct ppc_link_hash_entry *h;
10993
10994 hashes = elf_sym_hashes (htab->params->stub_bfd);
10995 if (hashes == NULL)
10996 {
10997 bfd_size_type hsize;
10998
10999 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11000 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11001 if (hashes == NULL)
11002 return FALSE;
11003 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11004 htab->stub_globals = 1;
11005 }
11006 symndx = htab->stub_globals++;
11007 h = stub_entry->h;
11008 hashes[symndx] = &h->elf;
11009 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
11010 if (h->oh != NULL && h->oh->is_func)
11011 h = ppc_follow_link (h->oh);
11012 if (h->elf.root.u.def.section != stub_entry->target_section)
11013 /* H is an opd symbol. The addend must be zero. */
11014 r->r_addend = 0;
11015 else
11016 {
11017 off = (h->elf.root.u.def.value
11018 + h->elf.root.u.def.section->output_offset
11019 + h->elf.root.u.def.section->output_section->vma);
11020 r->r_addend -= off;
11021 }
11022 }
11023 }
11024 break;
11025
11026 case ppc_stub_plt_branch:
11027 case ppc_stub_plt_branch_r2off:
11028 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11029 stub_entry->root.string + 9,
11030 FALSE, FALSE);
11031 if (br_entry == NULL)
11032 {
11033 _bfd_error_handler (_("can't find branch stub `%s'"),
11034 stub_entry->root.string);
11035 htab->stub_error = TRUE;
11036 return FALSE;
11037 }
11038
11039 dest = (stub_entry->target_value
11040 + stub_entry->target_section->output_offset
11041 + stub_entry->target_section->output_section->vma);
11042 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11043 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11044
11045 bfd_put_64 (htab->brlt->owner, dest,
11046 htab->brlt->contents + br_entry->offset);
11047
11048 if (br_entry->iter == htab->stub_iteration)
11049 {
11050 br_entry->iter = 0;
11051
11052 if (htab->relbrlt != NULL)
11053 {
11054 /* Create a reloc for the branch lookup table entry. */
11055 Elf_Internal_Rela rela;
11056 bfd_byte *rl;
11057
11058 rela.r_offset = (br_entry->offset
11059 + htab->brlt->output_offset
11060 + htab->brlt->output_section->vma);
11061 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11062 rela.r_addend = dest;
11063
11064 rl = htab->relbrlt->contents;
11065 rl += (htab->relbrlt->reloc_count++
11066 * sizeof (Elf64_External_Rela));
11067 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11068 }
11069 else if (info->emitrelocations)
11070 {
11071 r = get_relocs (htab->brlt, 1);
11072 if (r == NULL)
11073 return FALSE;
11074 /* brlt, being SEC_LINKER_CREATED does not go through the
11075 normal reloc processing. Symbols and offsets are not
11076 translated from input file to output file form, so
11077 set up the offset per the output file. */
11078 r->r_offset = (br_entry->offset
11079 + htab->brlt->output_offset
11080 + htab->brlt->output_section->vma);
11081 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11082 r->r_addend = dest;
11083 }
11084 }
11085
11086 dest = (br_entry->offset
11087 + htab->brlt->output_offset
11088 + htab->brlt->output_section->vma);
11089
11090 off = (dest
11091 - elf_gp (info->output_bfd)
11092 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11093
11094 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11095 {
11096 info->callbacks->einfo
11097 (_("%P: linkage table error against `%pT'\n"),
11098 stub_entry->root.string);
11099 bfd_set_error (bfd_error_bad_value);
11100 htab->stub_error = TRUE;
11101 return FALSE;
11102 }
11103
11104 if (info->emitrelocations)
11105 {
11106 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11107 if (r == NULL)
11108 return FALSE;
11109 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11110 if (bfd_big_endian (info->output_bfd))
11111 r[0].r_offset += 2;
11112 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11113 r[0].r_offset += 4;
11114 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11115 r[0].r_addend = dest;
11116 if (PPC_HA (off) != 0)
11117 {
11118 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11119 r[1].r_offset = r[0].r_offset + 4;
11120 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11121 r[1].r_addend = r[0].r_addend;
11122 }
11123 }
11124
11125 p = loc;
11126 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11127 {
11128 if (PPC_HA (off) != 0)
11129 {
11130 bfd_put_32 (htab->params->stub_bfd,
11131 ADDIS_R12_R2 | PPC_HA (off), p);
11132 p += 4;
11133 bfd_put_32 (htab->params->stub_bfd,
11134 LD_R12_0R12 | PPC_LO (off), p);
11135 }
11136 else
11137 bfd_put_32 (htab->params->stub_bfd,
11138 LD_R12_0R2 | PPC_LO (off), p);
11139 }
11140 else
11141 {
11142 bfd_vma r2off = get_r2off (info, stub_entry);
11143
11144 if (r2off == (bfd_vma) -1)
11145 {
11146 htab->stub_error = TRUE;
11147 return FALSE;
11148 }
11149
11150 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11151 p += 4;
11152 if (PPC_HA (off) != 0)
11153 {
11154 bfd_put_32 (htab->params->stub_bfd,
11155 ADDIS_R12_R2 | PPC_HA (off), p);
11156 p += 4;
11157 bfd_put_32 (htab->params->stub_bfd,
11158 LD_R12_0R12 | PPC_LO (off), p);
11159 }
11160 else
11161 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11162
11163 if (PPC_HA (r2off) != 0)
11164 {
11165 p += 4;
11166 bfd_put_32 (htab->params->stub_bfd,
11167 ADDIS_R2_R2 | PPC_HA (r2off), p);
11168 }
11169 if (PPC_LO (r2off) != 0)
11170 {
11171 p += 4;
11172 bfd_put_32 (htab->params->stub_bfd,
11173 ADDI_R2_R2 | PPC_LO (r2off), p);
11174 }
11175 }
11176 p += 4;
11177 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11178 p += 4;
11179 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11180 p += 4;
11181 break;
11182
11183 case ppc_stub_plt_call:
11184 case ppc_stub_plt_call_r2save:
11185 if (stub_entry->h != NULL
11186 && stub_entry->h->is_func_descriptor
11187 && stub_entry->h->oh != NULL)
11188 {
11189 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11190
11191 /* If the old-ABI "dot-symbol" is undefined make it weak so
11192 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11193 if (fh->elf.root.type == bfd_link_hash_undefined
11194 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11195 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11196 fh->elf.root.type = bfd_link_hash_undefweak;
11197 }
11198
11199 /* Now build the stub. */
11200 dest = stub_entry->plt_ent->plt.offset & ~1;
11201 if (dest >= (bfd_vma) -2)
11202 abort ();
11203
11204 plt = htab->elf.splt;
11205 if (!htab->elf.dynamic_sections_created
11206 || stub_entry->h == NULL
11207 || stub_entry->h->elf.dynindx == -1)
11208 {
11209 if (stub_entry->symtype == STT_GNU_IFUNC)
11210 plt = htab->elf.iplt;
11211 else
11212 plt = htab->pltlocal;
11213 }
11214
11215 dest += plt->output_offset + plt->output_section->vma;
11216
11217 off = (dest
11218 - elf_gp (info->output_bfd)
11219 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11220
11221 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11222 {
11223 info->callbacks->einfo
11224 /* xgettext:c-format */
11225 (_("%P: linkage table error against `%pT'\n"),
11226 stub_entry->h != NULL
11227 ? stub_entry->h->elf.root.root.string
11228 : "<local sym>");
11229 bfd_set_error (bfd_error_bad_value);
11230 htab->stub_error = TRUE;
11231 return FALSE;
11232 }
11233
11234 if (htab->params->plt_stub_align != 0)
11235 {
11236 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11237
11238 stub_entry->group->stub_sec->size += pad;
11239 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11240 loc += pad;
11241 }
11242
11243 r = NULL;
11244 if (info->emitrelocations)
11245 {
11246 r = get_relocs (stub_entry->group->stub_sec,
11247 ((PPC_HA (off) != 0)
11248 + (htab->opd_abi
11249 ? 2 + (htab->params->plt_static_chain
11250 && PPC_HA (off + 16) == PPC_HA (off))
11251 : 1)));
11252 if (r == NULL)
11253 return FALSE;
11254 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11255 if (bfd_big_endian (info->output_bfd))
11256 r[0].r_offset += 2;
11257 r[0].r_addend = dest;
11258 }
11259 if (stub_entry->h != NULL
11260 && (stub_entry->h == htab->tls_get_addr_fd
11261 || stub_entry->h == htab->tls_get_addr)
11262 && htab->params->tls_get_addr_opt)
11263 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11264 else
11265 p = build_plt_stub (htab, stub_entry, loc, off, r);
11266 break;
11267
11268 case ppc_stub_save_res:
11269 return TRUE;
11270
11271 default:
11272 BFD_FAIL ();
11273 return FALSE;
11274 }
11275
11276 stub_entry->group->stub_sec->size += p - loc;
11277
11278 if (htab->params->emit_stub_syms)
11279 {
11280 struct elf_link_hash_entry *h;
11281 size_t len1, len2;
11282 char *name;
11283 const char *const stub_str[] = { "long_branch",
11284 "long_branch_r2off",
11285 "plt_branch",
11286 "plt_branch_r2off",
11287 "plt_call",
11288 "plt_call" };
11289
11290 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11291 len2 = strlen (stub_entry->root.string);
11292 name = bfd_malloc (len1 + len2 + 2);
11293 if (name == NULL)
11294 return FALSE;
11295 memcpy (name, stub_entry->root.string, 9);
11296 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11297 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11298 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11299 if (h == NULL)
11300 return FALSE;
11301 if (h->root.type == bfd_link_hash_new)
11302 {
11303 h->root.type = bfd_link_hash_defined;
11304 h->root.u.def.section = stub_entry->group->stub_sec;
11305 h->root.u.def.value = stub_entry->stub_offset;
11306 h->ref_regular = 1;
11307 h->def_regular = 1;
11308 h->ref_regular_nonweak = 1;
11309 h->forced_local = 1;
11310 h->non_elf = 0;
11311 h->root.linker_def = 1;
11312 }
11313 }
11314
11315 return TRUE;
11316 }
11317
11318 /* As above, but don't actually build the stub. Just bump offset so
11319 we know stub section sizes, and select plt_branch stubs where
11320 long_branch stubs won't do. */
11321
11322 static bfd_boolean
11323 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11324 {
11325 struct ppc_stub_hash_entry *stub_entry;
11326 struct bfd_link_info *info;
11327 struct ppc_link_hash_table *htab;
11328 bfd_vma off;
11329 int size;
11330
11331 /* Massage our args to the form they really have. */
11332 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11333 info = in_arg;
11334
11335 htab = ppc_hash_table (info);
11336 if (htab == NULL)
11337 return FALSE;
11338
11339 if (stub_entry->h != NULL
11340 && stub_entry->h->save_res
11341 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11342 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11343 {
11344 /* Don't make stubs to out-of-line register save/restore
11345 functions. Instead, emit copies of the functions. */
11346 stub_entry->group->needs_save_res = 1;
11347 stub_entry->stub_type = ppc_stub_save_res;
11348 return TRUE;
11349 }
11350
11351 if (stub_entry->stub_type == ppc_stub_plt_call
11352 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11353 {
11354 asection *plt;
11355 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11356 if (off >= (bfd_vma) -2)
11357 abort ();
11358 plt = htab->elf.splt;
11359 if (!htab->elf.dynamic_sections_created
11360 || stub_entry->h == NULL
11361 || stub_entry->h->elf.dynindx == -1)
11362 {
11363 if (stub_entry->symtype == STT_GNU_IFUNC)
11364 plt = htab->elf.iplt;
11365 else
11366 plt = htab->pltlocal;
11367 }
11368 off += (plt->output_offset
11369 + plt->output_section->vma
11370 - elf_gp (info->output_bfd)
11371 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11372
11373 size = plt_stub_size (htab, stub_entry, off);
11374 if (stub_entry->h != NULL
11375 && (stub_entry->h == htab->tls_get_addr_fd
11376 || stub_entry->h == htab->tls_get_addr)
11377 && htab->params->tls_get_addr_opt
11378 && (ALWAYS_EMIT_R2SAVE
11379 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11380 stub_entry->group->tls_get_addr_opt_bctrl
11381 = stub_entry->group->stub_sec->size + size - 5 * 4;
11382
11383 if (htab->params->plt_stub_align)
11384 size += plt_stub_pad (htab, stub_entry, off);
11385 if (info->emitrelocations)
11386 {
11387 stub_entry->group->stub_sec->reloc_count
11388 += ((PPC_HA (off) != 0)
11389 + (htab->opd_abi
11390 ? 2 + (htab->params->plt_static_chain
11391 && PPC_HA (off + 16) == PPC_HA (off))
11392 : 1));
11393 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11394 }
11395 }
11396 else
11397 {
11398 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11399 variants. */
11400 bfd_vma r2off = 0;
11401 bfd_vma local_off = 0;
11402
11403 off = (stub_entry->target_value
11404 + stub_entry->target_section->output_offset
11405 + stub_entry->target_section->output_section->vma);
11406 off -= (stub_entry->group->stub_sec->size
11407 + stub_entry->group->stub_sec->output_offset
11408 + stub_entry->group->stub_sec->output_section->vma);
11409
11410 /* Reset the stub type from the plt variant in case we now
11411 can reach with a shorter stub. */
11412 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11413 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11414
11415 size = 4;
11416 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11417 {
11418 r2off = get_r2off (info, stub_entry);
11419 if (r2off == (bfd_vma) -1)
11420 {
11421 htab->stub_error = TRUE;
11422 return FALSE;
11423 }
11424 size = 8;
11425 if (PPC_HA (r2off) != 0)
11426 size += 4;
11427 if (PPC_LO (r2off) != 0)
11428 size += 4;
11429 off -= size - 4;
11430 }
11431
11432 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11433
11434 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11435 Do the same for -R objects without function descriptors. */
11436 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11437 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11438 && r2off == 0
11439 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11440 {
11441 struct ppc_branch_hash_entry *br_entry;
11442
11443 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11444 stub_entry->root.string + 9,
11445 TRUE, FALSE);
11446 if (br_entry == NULL)
11447 {
11448 _bfd_error_handler (_("can't build branch stub `%s'"),
11449 stub_entry->root.string);
11450 htab->stub_error = TRUE;
11451 return FALSE;
11452 }
11453
11454 if (br_entry->iter != htab->stub_iteration)
11455 {
11456 br_entry->iter = htab->stub_iteration;
11457 br_entry->offset = htab->brlt->size;
11458 htab->brlt->size += 8;
11459
11460 if (htab->relbrlt != NULL)
11461 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11462 else if (info->emitrelocations)
11463 {
11464 htab->brlt->reloc_count += 1;
11465 htab->brlt->flags |= SEC_RELOC;
11466 }
11467 }
11468
11469 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11470 off = (br_entry->offset
11471 + htab->brlt->output_offset
11472 + htab->brlt->output_section->vma
11473 - elf_gp (info->output_bfd)
11474 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11475
11476 if (info->emitrelocations)
11477 {
11478 stub_entry->group->stub_sec->reloc_count
11479 += 1 + (PPC_HA (off) != 0);
11480 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11481 }
11482
11483 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11484 {
11485 size = 12;
11486 if (PPC_HA (off) != 0)
11487 size = 16;
11488 }
11489 else
11490 {
11491 size = 16;
11492 if (PPC_HA (off) != 0)
11493 size += 4;
11494
11495 if (PPC_HA (r2off) != 0)
11496 size += 4;
11497 if (PPC_LO (r2off) != 0)
11498 size += 4;
11499 }
11500 }
11501 else if (info->emitrelocations)
11502 {
11503 stub_entry->group->stub_sec->reloc_count += 1;
11504 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11505 }
11506 }
11507
11508 stub_entry->group->stub_sec->size += size;
11509 return TRUE;
11510 }
11511
11512 /* Set up various things so that we can make a list of input sections
11513 for each output section included in the link. Returns -1 on error,
11514 0 when no stubs will be needed, and 1 on success. */
11515
11516 int
11517 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11518 {
11519 unsigned int id;
11520 bfd_size_type amt;
11521 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11522
11523 if (htab == NULL)
11524 return -1;
11525
11526 htab->sec_info_arr_size = bfd_get_next_section_id ();
11527 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11528 htab->sec_info = bfd_zmalloc (amt);
11529 if (htab->sec_info == NULL)
11530 return -1;
11531
11532 /* Set toc_off for com, und, abs and ind sections. */
11533 for (id = 0; id < 3; id++)
11534 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11535
11536 return 1;
11537 }
11538
11539 /* Set up for first pass at multitoc partitioning. */
11540
11541 void
11542 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11543 {
11544 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11545
11546 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11547 htab->toc_bfd = NULL;
11548 htab->toc_first_sec = NULL;
11549 }
11550
11551 /* The linker repeatedly calls this function for each TOC input section
11552 and linker generated GOT section. Group input bfds such that the toc
11553 within a group is less than 64k in size. */
11554
11555 bfd_boolean
11556 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11557 {
11558 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11559 bfd_vma addr, off, limit;
11560
11561 if (htab == NULL)
11562 return FALSE;
11563
11564 if (!htab->second_toc_pass)
11565 {
11566 /* Keep track of the first .toc or .got section for this input bfd. */
11567 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11568
11569 if (new_bfd)
11570 {
11571 htab->toc_bfd = isec->owner;
11572 htab->toc_first_sec = isec;
11573 }
11574
11575 addr = isec->output_offset + isec->output_section->vma;
11576 off = addr - htab->toc_curr;
11577 limit = 0x80008000;
11578 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11579 limit = 0x10000;
11580 if (off + isec->size > limit)
11581 {
11582 addr = (htab->toc_first_sec->output_offset
11583 + htab->toc_first_sec->output_section->vma);
11584 htab->toc_curr = addr;
11585 htab->toc_curr &= -TOC_BASE_ALIGN;
11586 }
11587
11588 /* toc_curr is the base address of this toc group. Set elf_gp
11589 for the input section to be the offset relative to the
11590 output toc base plus 0x8000. Making the input elf_gp an
11591 offset allows us to move the toc as a whole without
11592 recalculating input elf_gp. */
11593 off = htab->toc_curr - elf_gp (info->output_bfd);
11594 off += TOC_BASE_OFF;
11595
11596 /* Die if someone uses a linker script that doesn't keep input
11597 file .toc and .got together. */
11598 if (new_bfd
11599 && elf_gp (isec->owner) != 0
11600 && elf_gp (isec->owner) != off)
11601 return FALSE;
11602
11603 elf_gp (isec->owner) = off;
11604 return TRUE;
11605 }
11606
11607 /* During the second pass toc_first_sec points to the start of
11608 a toc group, and toc_curr is used to track the old elf_gp.
11609 We use toc_bfd to ensure we only look at each bfd once. */
11610 if (htab->toc_bfd == isec->owner)
11611 return TRUE;
11612 htab->toc_bfd = isec->owner;
11613
11614 if (htab->toc_first_sec == NULL
11615 || htab->toc_curr != elf_gp (isec->owner))
11616 {
11617 htab->toc_curr = elf_gp (isec->owner);
11618 htab->toc_first_sec = isec;
11619 }
11620 addr = (htab->toc_first_sec->output_offset
11621 + htab->toc_first_sec->output_section->vma);
11622 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11623 elf_gp (isec->owner) = off;
11624
11625 return TRUE;
11626 }
11627
11628 /* Called via elf_link_hash_traverse to merge GOT entries for global
11629 symbol H. */
11630
11631 static bfd_boolean
11632 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11633 {
11634 if (h->root.type == bfd_link_hash_indirect)
11635 return TRUE;
11636
11637 merge_got_entries (&h->got.glist);
11638
11639 return TRUE;
11640 }
11641
11642 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11643 symbol H. */
11644
11645 static bfd_boolean
11646 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11647 {
11648 struct got_entry *gent;
11649
11650 if (h->root.type == bfd_link_hash_indirect)
11651 return TRUE;
11652
11653 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11654 if (!gent->is_indirect)
11655 allocate_got (h, (struct bfd_link_info *) inf, gent);
11656 return TRUE;
11657 }
11658
11659 /* Called on the first multitoc pass after the last call to
11660 ppc64_elf_next_toc_section. This function removes duplicate GOT
11661 entries. */
11662
11663 bfd_boolean
11664 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11665 {
11666 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11667 struct bfd *ibfd, *ibfd2;
11668 bfd_boolean done_something;
11669
11670 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11671
11672 if (!htab->do_multi_toc)
11673 return FALSE;
11674
11675 /* Merge global sym got entries within a toc group. */
11676 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11677
11678 /* And tlsld_got. */
11679 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11680 {
11681 struct got_entry *ent, *ent2;
11682
11683 if (!is_ppc64_elf (ibfd))
11684 continue;
11685
11686 ent = ppc64_tlsld_got (ibfd);
11687 if (!ent->is_indirect
11688 && ent->got.offset != (bfd_vma) -1)
11689 {
11690 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11691 {
11692 if (!is_ppc64_elf (ibfd2))
11693 continue;
11694
11695 ent2 = ppc64_tlsld_got (ibfd2);
11696 if (!ent2->is_indirect
11697 && ent2->got.offset != (bfd_vma) -1
11698 && elf_gp (ibfd2) == elf_gp (ibfd))
11699 {
11700 ent2->is_indirect = TRUE;
11701 ent2->got.ent = ent;
11702 }
11703 }
11704 }
11705 }
11706
11707 /* Zap sizes of got sections. */
11708 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11709 htab->elf.irelplt->size -= htab->got_reli_size;
11710 htab->got_reli_size = 0;
11711
11712 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11713 {
11714 asection *got, *relgot;
11715
11716 if (!is_ppc64_elf (ibfd))
11717 continue;
11718
11719 got = ppc64_elf_tdata (ibfd)->got;
11720 if (got != NULL)
11721 {
11722 got->rawsize = got->size;
11723 got->size = 0;
11724 relgot = ppc64_elf_tdata (ibfd)->relgot;
11725 relgot->rawsize = relgot->size;
11726 relgot->size = 0;
11727 }
11728 }
11729
11730 /* Now reallocate the got, local syms first. We don't need to
11731 allocate section contents again since we never increase size. */
11732 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11733 {
11734 struct got_entry **lgot_ents;
11735 struct got_entry **end_lgot_ents;
11736 struct plt_entry **local_plt;
11737 struct plt_entry **end_local_plt;
11738 unsigned char *lgot_masks;
11739 bfd_size_type locsymcount;
11740 Elf_Internal_Shdr *symtab_hdr;
11741 asection *s;
11742
11743 if (!is_ppc64_elf (ibfd))
11744 continue;
11745
11746 lgot_ents = elf_local_got_ents (ibfd);
11747 if (!lgot_ents)
11748 continue;
11749
11750 symtab_hdr = &elf_symtab_hdr (ibfd);
11751 locsymcount = symtab_hdr->sh_info;
11752 end_lgot_ents = lgot_ents + locsymcount;
11753 local_plt = (struct plt_entry **) end_lgot_ents;
11754 end_local_plt = local_plt + locsymcount;
11755 lgot_masks = (unsigned char *) end_local_plt;
11756 s = ppc64_elf_tdata (ibfd)->got;
11757 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11758 {
11759 struct got_entry *ent;
11760
11761 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11762 {
11763 unsigned int ent_size = 8;
11764 unsigned int rel_size = sizeof (Elf64_External_Rela);
11765
11766 ent->got.offset = s->size;
11767 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11768 {
11769 ent_size *= 2;
11770 rel_size *= 2;
11771 }
11772 s->size += ent_size;
11773 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
11774 {
11775 htab->elf.irelplt->size += rel_size;
11776 htab->got_reli_size += rel_size;
11777 }
11778 else if (bfd_link_pic (info)
11779 && !((ent->tls_type & TLS_TPREL) != 0
11780 && bfd_link_executable (info)))
11781 {
11782 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11783 srel->size += rel_size;
11784 }
11785 }
11786 }
11787 }
11788
11789 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11790
11791 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11792 {
11793 struct got_entry *ent;
11794
11795 if (!is_ppc64_elf (ibfd))
11796 continue;
11797
11798 ent = ppc64_tlsld_got (ibfd);
11799 if (!ent->is_indirect
11800 && ent->got.offset != (bfd_vma) -1)
11801 {
11802 asection *s = ppc64_elf_tdata (ibfd)->got;
11803 ent->got.offset = s->size;
11804 s->size += 16;
11805 if (bfd_link_pic (info))
11806 {
11807 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11808 srel->size += sizeof (Elf64_External_Rela);
11809 }
11810 }
11811 }
11812
11813 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11814 if (!done_something)
11815 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11816 {
11817 asection *got;
11818
11819 if (!is_ppc64_elf (ibfd))
11820 continue;
11821
11822 got = ppc64_elf_tdata (ibfd)->got;
11823 if (got != NULL)
11824 {
11825 done_something = got->rawsize != got->size;
11826 if (done_something)
11827 break;
11828 }
11829 }
11830
11831 if (done_something)
11832 (*htab->params->layout_sections_again) ();
11833
11834 /* Set up for second pass over toc sections to recalculate elf_gp
11835 on input sections. */
11836 htab->toc_bfd = NULL;
11837 htab->toc_first_sec = NULL;
11838 htab->second_toc_pass = TRUE;
11839 return done_something;
11840 }
11841
11842 /* Called after second pass of multitoc partitioning. */
11843
11844 void
11845 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11846 {
11847 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11848
11849 /* After the second pass, toc_curr tracks the TOC offset used
11850 for code sections below in ppc64_elf_next_input_section. */
11851 htab->toc_curr = TOC_BASE_OFF;
11852 }
11853
11854 /* No toc references were found in ISEC. If the code in ISEC makes no
11855 calls, then there's no need to use toc adjusting stubs when branching
11856 into ISEC. Actually, indirect calls from ISEC are OK as they will
11857 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11858 needed, and 2 if a cyclical call-graph was found but no other reason
11859 for a stub was detected. If called from the top level, a return of
11860 2 means the same as a return of 0. */
11861
11862 static int
11863 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11864 {
11865 int ret;
11866
11867 /* Mark this section as checked. */
11868 isec->call_check_done = 1;
11869
11870 /* We know none of our code bearing sections will need toc stubs. */
11871 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11872 return 0;
11873
11874 if (isec->size == 0)
11875 return 0;
11876
11877 if (isec->output_section == NULL)
11878 return 0;
11879
11880 ret = 0;
11881 if (isec->reloc_count != 0)
11882 {
11883 Elf_Internal_Rela *relstart, *rel;
11884 Elf_Internal_Sym *local_syms;
11885 struct ppc_link_hash_table *htab;
11886
11887 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11888 info->keep_memory);
11889 if (relstart == NULL)
11890 return -1;
11891
11892 /* Look for branches to outside of this section. */
11893 local_syms = NULL;
11894 htab = ppc_hash_table (info);
11895 if (htab == NULL)
11896 return -1;
11897
11898 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11899 {
11900 enum elf_ppc64_reloc_type r_type;
11901 unsigned long r_symndx;
11902 struct elf_link_hash_entry *h;
11903 struct ppc_link_hash_entry *eh;
11904 Elf_Internal_Sym *sym;
11905 asection *sym_sec;
11906 struct _opd_sec_data *opd;
11907 bfd_vma sym_value;
11908 bfd_vma dest;
11909
11910 r_type = ELF64_R_TYPE (rel->r_info);
11911 if (r_type != R_PPC64_REL24
11912 && r_type != R_PPC64_REL14
11913 && r_type != R_PPC64_REL14_BRTAKEN
11914 && r_type != R_PPC64_REL14_BRNTAKEN)
11915 continue;
11916
11917 r_symndx = ELF64_R_SYM (rel->r_info);
11918 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11919 isec->owner))
11920 {
11921 ret = -1;
11922 break;
11923 }
11924
11925 /* Calls to dynamic lib functions go through a plt call stub
11926 that uses r2. */
11927 eh = (struct ppc_link_hash_entry *) h;
11928 if (eh != NULL
11929 && (eh->elf.plt.plist != NULL
11930 || (eh->oh != NULL
11931 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11932 {
11933 ret = 1;
11934 break;
11935 }
11936
11937 if (sym_sec == NULL)
11938 /* Ignore other undefined symbols. */
11939 continue;
11940
11941 /* Assume branches to other sections not included in the
11942 link need stubs too, to cover -R and absolute syms. */
11943 if (sym_sec->output_section == NULL)
11944 {
11945 ret = 1;
11946 break;
11947 }
11948
11949 if (h == NULL)
11950 sym_value = sym->st_value;
11951 else
11952 {
11953 if (h->root.type != bfd_link_hash_defined
11954 && h->root.type != bfd_link_hash_defweak)
11955 abort ();
11956 sym_value = h->root.u.def.value;
11957 }
11958 sym_value += rel->r_addend;
11959
11960 /* If this branch reloc uses an opd sym, find the code section. */
11961 opd = get_opd_info (sym_sec);
11962 if (opd != NULL)
11963 {
11964 if (h == NULL && opd->adjust != NULL)
11965 {
11966 long adjust;
11967
11968 adjust = opd->adjust[OPD_NDX (sym_value)];
11969 if (adjust == -1)
11970 /* Assume deleted functions won't ever be called. */
11971 continue;
11972 sym_value += adjust;
11973 }
11974
11975 dest = opd_entry_value (sym_sec, sym_value,
11976 &sym_sec, NULL, FALSE);
11977 if (dest == (bfd_vma) -1)
11978 continue;
11979 }
11980 else
11981 dest = (sym_value
11982 + sym_sec->output_offset
11983 + sym_sec->output_section->vma);
11984
11985 /* Ignore branch to self. */
11986 if (sym_sec == isec)
11987 continue;
11988
11989 /* If the called function uses the toc, we need a stub. */
11990 if (sym_sec->has_toc_reloc
11991 || sym_sec->makes_toc_func_call)
11992 {
11993 ret = 1;
11994 break;
11995 }
11996
11997 /* Assume any branch that needs a long branch stub might in fact
11998 need a plt_branch stub. A plt_branch stub uses r2. */
11999 else if (dest - (isec->output_offset
12000 + isec->output_section->vma
12001 + rel->r_offset) + (1 << 25)
12002 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12003 ? h->other
12004 : sym->st_other))
12005 {
12006 ret = 1;
12007 break;
12008 }
12009
12010 /* If calling back to a section in the process of being
12011 tested, we can't say for sure that no toc adjusting stubs
12012 are needed, so don't return zero. */
12013 else if (sym_sec->call_check_in_progress)
12014 ret = 2;
12015
12016 /* Branches to another section that itself doesn't have any TOC
12017 references are OK. Recursively call ourselves to check. */
12018 else if (!sym_sec->call_check_done)
12019 {
12020 int recur;
12021
12022 /* Mark current section as indeterminate, so that other
12023 sections that call back to current won't be marked as
12024 known. */
12025 isec->call_check_in_progress = 1;
12026 recur = toc_adjusting_stub_needed (info, sym_sec);
12027 isec->call_check_in_progress = 0;
12028
12029 if (recur != 0)
12030 {
12031 ret = recur;
12032 if (recur != 2)
12033 break;
12034 }
12035 }
12036 }
12037
12038 if (local_syms != NULL
12039 && (elf_symtab_hdr (isec->owner).contents
12040 != (unsigned char *) local_syms))
12041 free (local_syms);
12042 if (elf_section_data (isec)->relocs != relstart)
12043 free (relstart);
12044 }
12045
12046 if ((ret & 1) == 0
12047 && isec->map_head.s != NULL
12048 && (strcmp (isec->output_section->name, ".init") == 0
12049 || strcmp (isec->output_section->name, ".fini") == 0))
12050 {
12051 if (isec->map_head.s->has_toc_reloc
12052 || isec->map_head.s->makes_toc_func_call)
12053 ret = 1;
12054 else if (!isec->map_head.s->call_check_done)
12055 {
12056 int recur;
12057 isec->call_check_in_progress = 1;
12058 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12059 isec->call_check_in_progress = 0;
12060 if (recur != 0)
12061 ret = recur;
12062 }
12063 }
12064
12065 if (ret == 1)
12066 isec->makes_toc_func_call = 1;
12067
12068 return ret;
12069 }
12070
12071 /* The linker repeatedly calls this function for each input section,
12072 in the order that input sections are linked into output sections.
12073 Build lists of input sections to determine groupings between which
12074 we may insert linker stubs. */
12075
12076 bfd_boolean
12077 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12078 {
12079 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12080
12081 if (htab == NULL)
12082 return FALSE;
12083
12084 if ((isec->output_section->flags & SEC_CODE) != 0
12085 && isec->output_section->id < htab->sec_info_arr_size)
12086 {
12087 /* This happens to make the list in reverse order,
12088 which is what we want. */
12089 htab->sec_info[isec->id].u.list
12090 = htab->sec_info[isec->output_section->id].u.list;
12091 htab->sec_info[isec->output_section->id].u.list = isec;
12092 }
12093
12094 if (htab->multi_toc_needed)
12095 {
12096 /* Analyse sections that aren't already flagged as needing a
12097 valid toc pointer. Exclude .fixup for the linux kernel.
12098 .fixup contains branches, but only back to the function that
12099 hit an exception. */
12100 if (!(isec->has_toc_reloc
12101 || (isec->flags & SEC_CODE) == 0
12102 || strcmp (isec->name, ".fixup") == 0
12103 || isec->call_check_done))
12104 {
12105 if (toc_adjusting_stub_needed (info, isec) < 0)
12106 return FALSE;
12107 }
12108 /* Make all sections use the TOC assigned for this object file.
12109 This will be wrong for pasted sections; We fix that in
12110 check_pasted_section(). */
12111 if (elf_gp (isec->owner) != 0)
12112 htab->toc_curr = elf_gp (isec->owner);
12113 }
12114
12115 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12116 return TRUE;
12117 }
12118
12119 /* Check that all .init and .fini sections use the same toc, if they
12120 have toc relocs. */
12121
12122 static bfd_boolean
12123 check_pasted_section (struct bfd_link_info *info, const char *name)
12124 {
12125 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12126
12127 if (o != NULL)
12128 {
12129 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12130 bfd_vma toc_off = 0;
12131 asection *i;
12132
12133 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12134 if (i->has_toc_reloc)
12135 {
12136 if (toc_off == 0)
12137 toc_off = htab->sec_info[i->id].toc_off;
12138 else if (toc_off != htab->sec_info[i->id].toc_off)
12139 return FALSE;
12140 }
12141
12142 if (toc_off == 0)
12143 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12144 if (i->makes_toc_func_call)
12145 {
12146 toc_off = htab->sec_info[i->id].toc_off;
12147 break;
12148 }
12149
12150 /* Make sure the whole pasted function uses the same toc offset. */
12151 if (toc_off != 0)
12152 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12153 htab->sec_info[i->id].toc_off = toc_off;
12154 }
12155 return TRUE;
12156 }
12157
12158 bfd_boolean
12159 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12160 {
12161 return (check_pasted_section (info, ".init")
12162 & check_pasted_section (info, ".fini"));
12163 }
12164
12165 /* See whether we can group stub sections together. Grouping stub
12166 sections may result in fewer stubs. More importantly, we need to
12167 put all .init* and .fini* stubs at the beginning of the .init or
12168 .fini output sections respectively, because glibc splits the
12169 _init and _fini functions into multiple parts. Putting a stub in
12170 the middle of a function is not a good idea. */
12171
12172 static bfd_boolean
12173 group_sections (struct bfd_link_info *info,
12174 bfd_size_type stub_group_size,
12175 bfd_boolean stubs_always_before_branch)
12176 {
12177 struct ppc_link_hash_table *htab;
12178 asection *osec;
12179 bfd_boolean suppress_size_errors;
12180
12181 htab = ppc_hash_table (info);
12182 if (htab == NULL)
12183 return FALSE;
12184
12185 suppress_size_errors = FALSE;
12186 if (stub_group_size == 1)
12187 {
12188 /* Default values. */
12189 if (stubs_always_before_branch)
12190 stub_group_size = 0x1e00000;
12191 else
12192 stub_group_size = 0x1c00000;
12193 suppress_size_errors = TRUE;
12194 }
12195
12196 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12197 {
12198 asection *tail;
12199
12200 if (osec->id >= htab->sec_info_arr_size)
12201 continue;
12202
12203 tail = htab->sec_info[osec->id].u.list;
12204 while (tail != NULL)
12205 {
12206 asection *curr;
12207 asection *prev;
12208 bfd_size_type total;
12209 bfd_boolean big_sec;
12210 bfd_vma curr_toc;
12211 struct map_stub *group;
12212 bfd_size_type group_size;
12213
12214 curr = tail;
12215 total = tail->size;
12216 group_size = (ppc64_elf_section_data (tail) != NULL
12217 && ppc64_elf_section_data (tail)->has_14bit_branch
12218 ? stub_group_size >> 10 : stub_group_size);
12219
12220 big_sec = total > group_size;
12221 if (big_sec && !suppress_size_errors)
12222 /* xgettext:c-format */
12223 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12224 tail->owner, tail);
12225 curr_toc = htab->sec_info[tail->id].toc_off;
12226
12227 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12228 && ((total += curr->output_offset - prev->output_offset)
12229 < (ppc64_elf_section_data (prev) != NULL
12230 && ppc64_elf_section_data (prev)->has_14bit_branch
12231 ? (group_size = stub_group_size >> 10) : group_size))
12232 && htab->sec_info[prev->id].toc_off == curr_toc)
12233 curr = prev;
12234
12235 /* OK, the size from the start of CURR to the end is less
12236 than group_size and thus can be handled by one stub
12237 section. (or the tail section is itself larger than
12238 group_size, in which case we may be toast.) We should
12239 really be keeping track of the total size of stubs added
12240 here, as stubs contribute to the final output section
12241 size. That's a little tricky, and this way will only
12242 break if stubs added make the total size more than 2^25,
12243 ie. for the default stub_group_size, if stubs total more
12244 than 2097152 bytes, or nearly 75000 plt call stubs. */
12245 group = bfd_alloc (curr->owner, sizeof (*group));
12246 if (group == NULL)
12247 return FALSE;
12248 group->link_sec = curr;
12249 group->stub_sec = NULL;
12250 group->needs_save_res = 0;
12251 group->tls_get_addr_opt_bctrl = -1u;
12252 group->next = htab->group;
12253 htab->group = group;
12254 do
12255 {
12256 prev = htab->sec_info[tail->id].u.list;
12257 /* Set up this stub group. */
12258 htab->sec_info[tail->id].u.group = group;
12259 }
12260 while (tail != curr && (tail = prev) != NULL);
12261
12262 /* But wait, there's more! Input sections up to group_size
12263 bytes before the stub section can be handled by it too.
12264 Don't do this if we have a really large section after the
12265 stubs, as adding more stubs increases the chance that
12266 branches may not reach into the stub section. */
12267 if (!stubs_always_before_branch && !big_sec)
12268 {
12269 total = 0;
12270 while (prev != NULL
12271 && ((total += tail->output_offset - prev->output_offset)
12272 < (ppc64_elf_section_data (prev) != NULL
12273 && ppc64_elf_section_data (prev)->has_14bit_branch
12274 ? (group_size = stub_group_size >> 10) : group_size))
12275 && htab->sec_info[prev->id].toc_off == curr_toc)
12276 {
12277 tail = prev;
12278 prev = htab->sec_info[tail->id].u.list;
12279 htab->sec_info[tail->id].u.group = group;
12280 }
12281 }
12282 tail = prev;
12283 }
12284 }
12285 return TRUE;
12286 }
12287
12288 static const unsigned char glink_eh_frame_cie[] =
12289 {
12290 0, 0, 0, 16, /* length. */
12291 0, 0, 0, 0, /* id. */
12292 1, /* CIE version. */
12293 'z', 'R', 0, /* Augmentation string. */
12294 4, /* Code alignment. */
12295 0x78, /* Data alignment. */
12296 65, /* RA reg. */
12297 1, /* Augmentation size. */
12298 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12299 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12300 };
12301
12302 static size_t
12303 stub_eh_frame_size (struct map_stub *group, size_t align)
12304 {
12305 size_t this_size = 17;
12306 if (group->tls_get_addr_opt_bctrl != -1u)
12307 {
12308 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12309 if (to_bctrl < 64)
12310 this_size += 1;
12311 else if (to_bctrl < 256)
12312 this_size += 2;
12313 else if (to_bctrl < 65536)
12314 this_size += 3;
12315 else
12316 this_size += 5;
12317 this_size += 6;
12318 }
12319 this_size = (this_size + align - 1) & -align;
12320 return this_size;
12321 }
12322
12323 /* Stripping output sections is normally done before dynamic section
12324 symbols have been allocated. This function is called later, and
12325 handles cases like htab->brlt which is mapped to its own output
12326 section. */
12327
12328 static void
12329 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12330 {
12331 if (isec->size == 0
12332 && isec->output_section->size == 0
12333 && !(isec->output_section->flags & SEC_KEEP)
12334 && !bfd_section_removed_from_list (info->output_bfd,
12335 isec->output_section)
12336 && elf_section_data (isec->output_section)->dynindx == 0)
12337 {
12338 isec->output_section->flags |= SEC_EXCLUDE;
12339 bfd_section_list_remove (info->output_bfd, isec->output_section);
12340 info->output_bfd->section_count--;
12341 }
12342 }
12343
12344 /* Determine and set the size of the stub section for a final link.
12345
12346 The basic idea here is to examine all the relocations looking for
12347 PC-relative calls to a target that is unreachable with a "bl"
12348 instruction. */
12349
12350 bfd_boolean
12351 ppc64_elf_size_stubs (struct bfd_link_info *info)
12352 {
12353 bfd_size_type stub_group_size;
12354 bfd_boolean stubs_always_before_branch;
12355 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12356
12357 if (htab == NULL)
12358 return FALSE;
12359
12360 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12361 htab->params->plt_thread_safe = 1;
12362 if (!htab->opd_abi)
12363 htab->params->plt_thread_safe = 0;
12364 else if (htab->params->plt_thread_safe == -1)
12365 {
12366 static const char *const thread_starter[] =
12367 {
12368 "pthread_create",
12369 /* libstdc++ */
12370 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12371 /* librt */
12372 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12373 "mq_notify", "create_timer",
12374 /* libanl */
12375 "getaddrinfo_a",
12376 /* libgomp */
12377 "GOMP_parallel",
12378 "GOMP_parallel_start",
12379 "GOMP_parallel_loop_static",
12380 "GOMP_parallel_loop_static_start",
12381 "GOMP_parallel_loop_dynamic",
12382 "GOMP_parallel_loop_dynamic_start",
12383 "GOMP_parallel_loop_guided",
12384 "GOMP_parallel_loop_guided_start",
12385 "GOMP_parallel_loop_runtime",
12386 "GOMP_parallel_loop_runtime_start",
12387 "GOMP_parallel_sections",
12388 "GOMP_parallel_sections_start",
12389 /* libgo */
12390 "__go_go",
12391 };
12392 unsigned i;
12393
12394 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12395 {
12396 struct elf_link_hash_entry *h;
12397 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12398 FALSE, FALSE, TRUE);
12399 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12400 if (htab->params->plt_thread_safe)
12401 break;
12402 }
12403 }
12404 stubs_always_before_branch = htab->params->group_size < 0;
12405 if (htab->params->group_size < 0)
12406 stub_group_size = -htab->params->group_size;
12407 else
12408 stub_group_size = htab->params->group_size;
12409
12410 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12411 return FALSE;
12412
12413 #define STUB_SHRINK_ITER 20
12414 /* Loop until no stubs added. After iteration 20 of this loop we may
12415 exit on a stub section shrinking. This is to break out of a
12416 pathological case where adding stubs on one iteration decreases
12417 section gaps (perhaps due to alignment), which then requires
12418 fewer or smaller stubs on the next iteration. */
12419
12420 while (1)
12421 {
12422 bfd *input_bfd;
12423 unsigned int bfd_indx;
12424 struct map_stub *group;
12425
12426 htab->stub_iteration += 1;
12427
12428 for (input_bfd = info->input_bfds, bfd_indx = 0;
12429 input_bfd != NULL;
12430 input_bfd = input_bfd->link.next, bfd_indx++)
12431 {
12432 Elf_Internal_Shdr *symtab_hdr;
12433 asection *section;
12434 Elf_Internal_Sym *local_syms = NULL;
12435
12436 if (!is_ppc64_elf (input_bfd))
12437 continue;
12438
12439 /* We'll need the symbol table in a second. */
12440 symtab_hdr = &elf_symtab_hdr (input_bfd);
12441 if (symtab_hdr->sh_info == 0)
12442 continue;
12443
12444 /* Walk over each section attached to the input bfd. */
12445 for (section = input_bfd->sections;
12446 section != NULL;
12447 section = section->next)
12448 {
12449 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12450
12451 /* If there aren't any relocs, then there's nothing more
12452 to do. */
12453 if ((section->flags & SEC_RELOC) == 0
12454 || (section->flags & SEC_ALLOC) == 0
12455 || (section->flags & SEC_LOAD) == 0
12456 || (section->flags & SEC_CODE) == 0
12457 || section->reloc_count == 0)
12458 continue;
12459
12460 /* If this section is a link-once section that will be
12461 discarded, then don't create any stubs. */
12462 if (section->output_section == NULL
12463 || section->output_section->owner != info->output_bfd)
12464 continue;
12465
12466 /* Get the relocs. */
12467 internal_relocs
12468 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12469 info->keep_memory);
12470 if (internal_relocs == NULL)
12471 goto error_ret_free_local;
12472
12473 /* Now examine each relocation. */
12474 irela = internal_relocs;
12475 irelaend = irela + section->reloc_count;
12476 for (; irela < irelaend; irela++)
12477 {
12478 enum elf_ppc64_reloc_type r_type;
12479 unsigned int r_indx;
12480 enum ppc_stub_type stub_type;
12481 struct ppc_stub_hash_entry *stub_entry;
12482 asection *sym_sec, *code_sec;
12483 bfd_vma sym_value, code_value;
12484 bfd_vma destination;
12485 unsigned long local_off;
12486 bfd_boolean ok_dest;
12487 struct ppc_link_hash_entry *hash;
12488 struct ppc_link_hash_entry *fdh;
12489 struct elf_link_hash_entry *h;
12490 Elf_Internal_Sym *sym;
12491 char *stub_name;
12492 const asection *id_sec;
12493 struct _opd_sec_data *opd;
12494 struct plt_entry *plt_ent;
12495
12496 r_type = ELF64_R_TYPE (irela->r_info);
12497 r_indx = ELF64_R_SYM (irela->r_info);
12498
12499 if (r_type >= R_PPC64_max)
12500 {
12501 bfd_set_error (bfd_error_bad_value);
12502 goto error_ret_free_internal;
12503 }
12504
12505 /* Only look for stubs on branch instructions. */
12506 if (r_type != R_PPC64_REL24
12507 && r_type != R_PPC64_REL14
12508 && r_type != R_PPC64_REL14_BRTAKEN
12509 && r_type != R_PPC64_REL14_BRNTAKEN)
12510 continue;
12511
12512 /* Now determine the call target, its name, value,
12513 section. */
12514 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12515 r_indx, input_bfd))
12516 goto error_ret_free_internal;
12517 hash = (struct ppc_link_hash_entry *) h;
12518
12519 ok_dest = FALSE;
12520 fdh = NULL;
12521 sym_value = 0;
12522 if (hash == NULL)
12523 {
12524 sym_value = sym->st_value;
12525 if (sym_sec != NULL
12526 && sym_sec->output_section != NULL)
12527 ok_dest = TRUE;
12528 }
12529 else if (hash->elf.root.type == bfd_link_hash_defined
12530 || hash->elf.root.type == bfd_link_hash_defweak)
12531 {
12532 sym_value = hash->elf.root.u.def.value;
12533 if (sym_sec->output_section != NULL)
12534 ok_dest = TRUE;
12535 }
12536 else if (hash->elf.root.type == bfd_link_hash_undefweak
12537 || hash->elf.root.type == bfd_link_hash_undefined)
12538 {
12539 /* Recognise an old ABI func code entry sym, and
12540 use the func descriptor sym instead if it is
12541 defined. */
12542 if (hash->elf.root.root.string[0] == '.'
12543 && hash->oh != NULL)
12544 {
12545 fdh = ppc_follow_link (hash->oh);
12546 if (fdh->elf.root.type == bfd_link_hash_defined
12547 || fdh->elf.root.type == bfd_link_hash_defweak)
12548 {
12549 sym_sec = fdh->elf.root.u.def.section;
12550 sym_value = fdh->elf.root.u.def.value;
12551 if (sym_sec->output_section != NULL)
12552 ok_dest = TRUE;
12553 }
12554 else
12555 fdh = NULL;
12556 }
12557 }
12558 else
12559 {
12560 bfd_set_error (bfd_error_bad_value);
12561 goto error_ret_free_internal;
12562 }
12563
12564 destination = 0;
12565 local_off = 0;
12566 if (ok_dest)
12567 {
12568 sym_value += irela->r_addend;
12569 destination = (sym_value
12570 + sym_sec->output_offset
12571 + sym_sec->output_section->vma);
12572 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12573 ? hash->elf.other
12574 : sym->st_other);
12575 }
12576
12577 code_sec = sym_sec;
12578 code_value = sym_value;
12579 opd = get_opd_info (sym_sec);
12580 if (opd != NULL)
12581 {
12582 bfd_vma dest;
12583
12584 if (hash == NULL && opd->adjust != NULL)
12585 {
12586 long adjust = opd->adjust[OPD_NDX (sym_value)];
12587 if (adjust == -1)
12588 continue;
12589 code_value += adjust;
12590 sym_value += adjust;
12591 }
12592 dest = opd_entry_value (sym_sec, sym_value,
12593 &code_sec, &code_value, FALSE);
12594 if (dest != (bfd_vma) -1)
12595 {
12596 destination = dest;
12597 if (fdh != NULL)
12598 {
12599 /* Fixup old ABI sym to point at code
12600 entry. */
12601 hash->elf.root.type = bfd_link_hash_defweak;
12602 hash->elf.root.u.def.section = code_sec;
12603 hash->elf.root.u.def.value = code_value;
12604 }
12605 }
12606 }
12607
12608 /* Determine what (if any) linker stub is needed. */
12609 plt_ent = NULL;
12610 stub_type = ppc_type_of_stub (section, irela, &hash,
12611 &plt_ent, destination,
12612 local_off);
12613
12614 if (stub_type != ppc_stub_plt_call)
12615 {
12616 /* Check whether we need a TOC adjusting stub.
12617 Since the linker pastes together pieces from
12618 different object files when creating the
12619 _init and _fini functions, it may be that a
12620 call to what looks like a local sym is in
12621 fact a call needing a TOC adjustment. */
12622 if (code_sec != NULL
12623 && code_sec->output_section != NULL
12624 && (htab->sec_info[code_sec->id].toc_off
12625 != htab->sec_info[section->id].toc_off)
12626 && (code_sec->has_toc_reloc
12627 || code_sec->makes_toc_func_call))
12628 stub_type = ppc_stub_long_branch_r2off;
12629 }
12630
12631 if (stub_type == ppc_stub_none)
12632 continue;
12633
12634 /* __tls_get_addr calls might be eliminated. */
12635 if (stub_type != ppc_stub_plt_call
12636 && hash != NULL
12637 && (hash == htab->tls_get_addr
12638 || hash == htab->tls_get_addr_fd)
12639 && section->has_tls_reloc
12640 && irela != internal_relocs)
12641 {
12642 /* Get tls info. */
12643 unsigned char *tls_mask;
12644
12645 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12646 irela - 1, input_bfd))
12647 goto error_ret_free_internal;
12648 if ((*tls_mask & TLS_TLS) != 0)
12649 continue;
12650 }
12651
12652 if (stub_type == ppc_stub_plt_call)
12653 {
12654 if (!htab->opd_abi
12655 && htab->params->plt_localentry0 != 0
12656 && is_elfv2_localentry0 (&hash->elf))
12657 htab->has_plt_localentry0 = 1;
12658 else if (irela + 1 < irelaend
12659 && irela[1].r_offset == irela->r_offset + 4
12660 && (ELF64_R_TYPE (irela[1].r_info)
12661 == R_PPC64_TOCSAVE))
12662 {
12663 if (!tocsave_find (htab, INSERT,
12664 &local_syms, irela + 1, input_bfd))
12665 goto error_ret_free_internal;
12666 }
12667 else
12668 stub_type = ppc_stub_plt_call_r2save;
12669 }
12670
12671 /* Support for grouping stub sections. */
12672 id_sec = htab->sec_info[section->id].u.group->link_sec;
12673
12674 /* Get the name of this stub. */
12675 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12676 if (!stub_name)
12677 goto error_ret_free_internal;
12678
12679 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12680 stub_name, FALSE, FALSE);
12681 if (stub_entry != NULL)
12682 {
12683 /* The proper stub has already been created. */
12684 free (stub_name);
12685 if (stub_type == ppc_stub_plt_call_r2save)
12686 stub_entry->stub_type = stub_type;
12687 continue;
12688 }
12689
12690 stub_entry = ppc_add_stub (stub_name, section, info);
12691 if (stub_entry == NULL)
12692 {
12693 free (stub_name);
12694 error_ret_free_internal:
12695 if (elf_section_data (section)->relocs == NULL)
12696 free (internal_relocs);
12697 error_ret_free_local:
12698 if (local_syms != NULL
12699 && (symtab_hdr->contents
12700 != (unsigned char *) local_syms))
12701 free (local_syms);
12702 return FALSE;
12703 }
12704
12705 stub_entry->stub_type = stub_type;
12706 if (stub_type != ppc_stub_plt_call
12707 && stub_type != ppc_stub_plt_call_r2save)
12708 {
12709 stub_entry->target_value = code_value;
12710 stub_entry->target_section = code_sec;
12711 }
12712 else
12713 {
12714 stub_entry->target_value = sym_value;
12715 stub_entry->target_section = sym_sec;
12716 }
12717 stub_entry->h = hash;
12718 stub_entry->plt_ent = plt_ent;
12719 stub_entry->symtype
12720 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
12721 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12722
12723 if (stub_entry->h != NULL)
12724 htab->stub_globals += 1;
12725 }
12726
12727 /* We're done with the internal relocs, free them. */
12728 if (elf_section_data (section)->relocs != internal_relocs)
12729 free (internal_relocs);
12730 }
12731
12732 if (local_syms != NULL
12733 && symtab_hdr->contents != (unsigned char *) local_syms)
12734 {
12735 if (!info->keep_memory)
12736 free (local_syms);
12737 else
12738 symtab_hdr->contents = (unsigned char *) local_syms;
12739 }
12740 }
12741
12742 /* We may have added some stubs. Find out the new size of the
12743 stub sections. */
12744 for (group = htab->group; group != NULL; group = group->next)
12745 if (group->stub_sec != NULL)
12746 {
12747 asection *stub_sec = group->stub_sec;
12748
12749 if (htab->stub_iteration <= STUB_SHRINK_ITER
12750 || stub_sec->rawsize < stub_sec->size)
12751 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12752 stub_sec->rawsize = stub_sec->size;
12753 stub_sec->size = 0;
12754 stub_sec->reloc_count = 0;
12755 stub_sec->flags &= ~SEC_RELOC;
12756 }
12757
12758 if (htab->stub_iteration <= STUB_SHRINK_ITER
12759 || htab->brlt->rawsize < htab->brlt->size)
12760 htab->brlt->rawsize = htab->brlt->size;
12761 htab->brlt->size = 0;
12762 htab->brlt->reloc_count = 0;
12763 htab->brlt->flags &= ~SEC_RELOC;
12764 if (htab->relbrlt != NULL)
12765 htab->relbrlt->size = 0;
12766
12767 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12768
12769 for (group = htab->group; group != NULL; group = group->next)
12770 if (group->needs_save_res)
12771 group->stub_sec->size += htab->sfpr->size;
12772
12773 if (info->emitrelocations
12774 && htab->glink != NULL && htab->glink->size != 0)
12775 {
12776 htab->glink->reloc_count = 1;
12777 htab->glink->flags |= SEC_RELOC;
12778 }
12779
12780 if (htab->glink_eh_frame != NULL
12781 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12782 && htab->glink_eh_frame->output_section->size > 8)
12783 {
12784 size_t size = 0, align = 4;
12785
12786 for (group = htab->group; group != NULL; group = group->next)
12787 if (group->stub_sec != NULL)
12788 size += stub_eh_frame_size (group, align);
12789 if (htab->glink != NULL && htab->glink->size != 0)
12790 size += (24 + align - 1) & -align;
12791 if (size != 0)
12792 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12793 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12794 size = (size + align - 1) & -align;
12795 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12796 htab->glink_eh_frame->size = size;
12797 }
12798
12799 if (htab->params->plt_stub_align != 0)
12800 for (group = htab->group; group != NULL; group = group->next)
12801 if (group->stub_sec != NULL)
12802 {
12803 int align = abs (htab->params->plt_stub_align);
12804 group->stub_sec->size
12805 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12806 }
12807
12808 for (group = htab->group; group != NULL; group = group->next)
12809 if (group->stub_sec != NULL
12810 && group->stub_sec->rawsize != group->stub_sec->size
12811 && (htab->stub_iteration <= STUB_SHRINK_ITER
12812 || group->stub_sec->rawsize < group->stub_sec->size))
12813 break;
12814
12815 if (group == NULL
12816 && (htab->brlt->rawsize == htab->brlt->size
12817 || (htab->stub_iteration > STUB_SHRINK_ITER
12818 && htab->brlt->rawsize > htab->brlt->size))
12819 && (htab->glink_eh_frame == NULL
12820 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12821 break;
12822
12823 /* Ask the linker to do its stuff. */
12824 (*htab->params->layout_sections_again) ();
12825 }
12826
12827 if (htab->glink_eh_frame != NULL
12828 && htab->glink_eh_frame->size != 0)
12829 {
12830 bfd_vma val;
12831 bfd_byte *p, *last_fde;
12832 size_t last_fde_len, size, align, pad;
12833 struct map_stub *group;
12834
12835 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12836 if (p == NULL)
12837 return FALSE;
12838 htab->glink_eh_frame->contents = p;
12839 last_fde = p;
12840 align = 4;
12841
12842 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12843 /* CIE length (rewrite in case little-endian). */
12844 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12845 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12846 p += last_fde_len + 4;
12847
12848 for (group = htab->group; group != NULL; group = group->next)
12849 if (group->stub_sec != NULL)
12850 {
12851 last_fde = p;
12852 last_fde_len = stub_eh_frame_size (group, align) - 4;
12853 /* FDE length. */
12854 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12855 p += 4;
12856 /* CIE pointer. */
12857 val = p - htab->glink_eh_frame->contents;
12858 bfd_put_32 (htab->elf.dynobj, val, p);
12859 p += 4;
12860 /* Offset to stub section, written later. */
12861 p += 4;
12862 /* stub section size. */
12863 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
12864 p += 4;
12865 /* Augmentation. */
12866 p += 1;
12867 if (group->tls_get_addr_opt_bctrl != -1u)
12868 {
12869 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12870
12871 /* This FDE needs more than just the default.
12872 Describe __tls_get_addr_opt stub LR. */
12873 if (to_bctrl < 64)
12874 *p++ = DW_CFA_advance_loc + to_bctrl;
12875 else if (to_bctrl < 256)
12876 {
12877 *p++ = DW_CFA_advance_loc1;
12878 *p++ = to_bctrl;
12879 }
12880 else if (to_bctrl < 65536)
12881 {
12882 *p++ = DW_CFA_advance_loc2;
12883 bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
12884 p += 2;
12885 }
12886 else
12887 {
12888 *p++ = DW_CFA_advance_loc4;
12889 bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
12890 p += 4;
12891 }
12892 *p++ = DW_CFA_offset_extended_sf;
12893 *p++ = 65;
12894 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
12895 *p++ = DW_CFA_advance_loc + 4;
12896 *p++ = DW_CFA_restore_extended;
12897 *p++ = 65;
12898 }
12899 /* Pad. */
12900 p = last_fde + last_fde_len + 4;
12901 }
12902 if (htab->glink != NULL && htab->glink->size != 0)
12903 {
12904 last_fde = p;
12905 last_fde_len = ((24 + align - 1) & -align) - 4;
12906 /* FDE length. */
12907 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12908 p += 4;
12909 /* CIE pointer. */
12910 val = p - htab->glink_eh_frame->contents;
12911 bfd_put_32 (htab->elf.dynobj, val, p);
12912 p += 4;
12913 /* Offset to .glink, written later. */
12914 p += 4;
12915 /* .glink size. */
12916 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12917 p += 4;
12918 /* Augmentation. */
12919 p += 1;
12920
12921 *p++ = DW_CFA_advance_loc + 1;
12922 *p++ = DW_CFA_register;
12923 *p++ = 65;
12924 *p++ = htab->opd_abi ? 12 : 0;
12925 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
12926 *p++ = DW_CFA_restore_extended;
12927 *p++ = 65;
12928 p += ((24 + align - 1) & -align) - 24;
12929 }
12930 /* Subsume any padding into the last FDE if user .eh_frame
12931 sections are aligned more than glink_eh_frame. Otherwise any
12932 zero padding will be seen as a terminator. */
12933 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12934 size = p - htab->glink_eh_frame->contents;
12935 pad = ((size + align - 1) & -align) - size;
12936 htab->glink_eh_frame->size = size + pad;
12937 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12938 }
12939
12940 maybe_strip_output (info, htab->brlt);
12941 if (htab->glink_eh_frame != NULL)
12942 maybe_strip_output (info, htab->glink_eh_frame);
12943
12944 return TRUE;
12945 }
12946
12947 /* Called after we have determined section placement. If sections
12948 move, we'll be called again. Provide a value for TOCstart. */
12949
12950 bfd_vma
12951 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12952 {
12953 asection *s;
12954 bfd_vma TOCstart, adjust;
12955
12956 if (info != NULL)
12957 {
12958 struct elf_link_hash_entry *h;
12959 struct elf_link_hash_table *htab = elf_hash_table (info);
12960
12961 if (is_elf_hash_table (htab)
12962 && htab->hgot != NULL)
12963 h = htab->hgot;
12964 else
12965 {
12966 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12967 if (is_elf_hash_table (htab))
12968 htab->hgot = h;
12969 }
12970 if (h != NULL
12971 && h->root.type == bfd_link_hash_defined
12972 && !h->root.linker_def
12973 && (!is_elf_hash_table (htab)
12974 || h->def_regular))
12975 {
12976 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12977 + h->root.u.def.section->output_offset
12978 + h->root.u.def.section->output_section->vma);
12979 _bfd_set_gp_value (obfd, TOCstart);
12980 return TOCstart;
12981 }
12982 }
12983
12984 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12985 order. The TOC starts where the first of these sections starts. */
12986 s = bfd_get_section_by_name (obfd, ".got");
12987 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12988 s = bfd_get_section_by_name (obfd, ".toc");
12989 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12990 s = bfd_get_section_by_name (obfd, ".tocbss");
12991 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12992 s = bfd_get_section_by_name (obfd, ".plt");
12993 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12994 {
12995 /* This may happen for
12996 o references to TOC base (SYM@toc / TOC[tc0]) without a
12997 .toc directive
12998 o bad linker script
12999 o --gc-sections and empty TOC sections
13000
13001 FIXME: Warn user? */
13002
13003 /* Look for a likely section. We probably won't even be
13004 using TOCstart. */
13005 for (s = obfd->sections; s != NULL; s = s->next)
13006 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13007 | SEC_EXCLUDE))
13008 == (SEC_ALLOC | SEC_SMALL_DATA))
13009 break;
13010 if (s == NULL)
13011 for (s = obfd->sections; s != NULL; s = s->next)
13012 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13013 == (SEC_ALLOC | SEC_SMALL_DATA))
13014 break;
13015 if (s == NULL)
13016 for (s = obfd->sections; s != NULL; s = s->next)
13017 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13018 == SEC_ALLOC)
13019 break;
13020 if (s == NULL)
13021 for (s = obfd->sections; s != NULL; s = s->next)
13022 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13023 break;
13024 }
13025
13026 TOCstart = 0;
13027 if (s != NULL)
13028 TOCstart = s->output_section->vma + s->output_offset;
13029
13030 /* Force alignment. */
13031 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13032 TOCstart -= adjust;
13033 _bfd_set_gp_value (obfd, TOCstart);
13034
13035 if (info != NULL && s != NULL)
13036 {
13037 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13038
13039 if (htab != NULL)
13040 {
13041 if (htab->elf.hgot != NULL)
13042 {
13043 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13044 htab->elf.hgot->root.u.def.section = s;
13045 }
13046 }
13047 else
13048 {
13049 struct bfd_link_hash_entry *bh = NULL;
13050 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13051 s, TOC_BASE_OFF - adjust,
13052 NULL, FALSE, FALSE, &bh);
13053 }
13054 }
13055 return TOCstart;
13056 }
13057
13058 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13059 write out any global entry stubs, and PLT relocations. */
13060
13061 static bfd_boolean
13062 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13063 {
13064 struct bfd_link_info *info;
13065 struct ppc_link_hash_table *htab;
13066 struct plt_entry *ent;
13067 asection *s;
13068
13069 if (h->root.type == bfd_link_hash_indirect)
13070 return TRUE;
13071
13072 info = inf;
13073 htab = ppc_hash_table (info);
13074 if (htab == NULL)
13075 return FALSE;
13076
13077 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13078 if (ent->plt.offset != (bfd_vma) -1)
13079 {
13080 /* This symbol has an entry in the procedure linkage
13081 table. Set it up. */
13082 Elf_Internal_Rela rela;
13083 asection *plt, *relplt;
13084 bfd_byte *loc;
13085
13086 if (!htab->elf.dynamic_sections_created
13087 || h->dynindx == -1)
13088 {
13089 if (!(h->def_regular
13090 && (h->root.type == bfd_link_hash_defined
13091 || h->root.type == bfd_link_hash_defweak)))
13092 continue;
13093 if (h->type == STT_GNU_IFUNC)
13094 {
13095 plt = htab->elf.iplt;
13096 relplt = htab->elf.irelplt;
13097 htab->local_ifunc_resolver = 1;
13098 if (htab->opd_abi)
13099 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13100 else
13101 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13102 }
13103 else
13104 {
13105 plt = htab->pltlocal;
13106 if (bfd_link_pic (info))
13107 {
13108 relplt = htab->relpltlocal;
13109 if (htab->opd_abi)
13110 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13111 else
13112 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13113 }
13114 else
13115 relplt = NULL;
13116 }
13117 rela.r_addend = (h->root.u.def.value
13118 + h->root.u.def.section->output_offset
13119 + h->root.u.def.section->output_section->vma
13120 + ent->addend);
13121
13122 if (relplt == NULL)
13123 {
13124 loc = plt->contents + ent->plt.offset;
13125 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13126 if (htab->opd_abi)
13127 {
13128 bfd_vma toc = elf_gp (info->output_bfd);
13129 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13130 bfd_put_64 (info->output_bfd, toc, loc + 8);
13131 }
13132 }
13133 else
13134 {
13135 rela.r_offset = (plt->output_section->vma
13136 + plt->output_offset
13137 + ent->plt.offset);
13138 loc = relplt->contents + (relplt->reloc_count++
13139 * sizeof (Elf64_External_Rela));
13140 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13141 }
13142 }
13143 else
13144 {
13145 rela.r_offset = (htab->elf.splt->output_section->vma
13146 + htab->elf.splt->output_offset
13147 + ent->plt.offset);
13148 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13149 rela.r_addend = ent->addend;
13150 loc = (htab->elf.srelplt->contents
13151 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13152 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13153 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13154 htab->maybe_local_ifunc_resolver = 1;
13155 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13156 }
13157 }
13158
13159 if (!h->pointer_equality_needed)
13160 return TRUE;
13161
13162 if (h->def_regular)
13163 return TRUE;
13164
13165 s = htab->global_entry;
13166 if (s == NULL || s->size == 0)
13167 return TRUE;
13168
13169 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13170 if (ent->plt.offset != (bfd_vma) -1
13171 && ent->addend == 0)
13172 {
13173 bfd_byte *p;
13174 asection *plt;
13175 bfd_vma off;
13176
13177 p = s->contents + h->root.u.def.value;
13178 plt = htab->elf.splt;
13179 if (!htab->elf.dynamic_sections_created
13180 || h->dynindx == -1)
13181 {
13182 if (h->type == STT_GNU_IFUNC)
13183 plt = htab->elf.iplt;
13184 else
13185 plt = htab->pltlocal;
13186 }
13187 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13188 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13189
13190 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13191 {
13192 info->callbacks->einfo
13193 (_("%P: linkage table error against `%pT'\n"),
13194 h->root.root.string);
13195 bfd_set_error (bfd_error_bad_value);
13196 htab->stub_error = TRUE;
13197 }
13198
13199 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13200 if (htab->params->emit_stub_syms)
13201 {
13202 size_t len = strlen (h->root.root.string);
13203 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13204
13205 if (name == NULL)
13206 return FALSE;
13207
13208 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13209 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13210 if (h == NULL)
13211 return FALSE;
13212 if (h->root.type == bfd_link_hash_new)
13213 {
13214 h->root.type = bfd_link_hash_defined;
13215 h->root.u.def.section = s;
13216 h->root.u.def.value = p - s->contents;
13217 h->ref_regular = 1;
13218 h->def_regular = 1;
13219 h->ref_regular_nonweak = 1;
13220 h->forced_local = 1;
13221 h->non_elf = 0;
13222 h->root.linker_def = 1;
13223 }
13224 }
13225
13226 if (PPC_HA (off) != 0)
13227 {
13228 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13229 p += 4;
13230 }
13231 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13232 p += 4;
13233 bfd_put_32 (s->owner, MTCTR_R12, p);
13234 p += 4;
13235 bfd_put_32 (s->owner, BCTR, p);
13236 break;
13237 }
13238 return TRUE;
13239 }
13240
13241 /* Write PLT relocs for locals. */
13242
13243 static bfd_boolean
13244 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13245 {
13246 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13247 bfd *ibfd;
13248
13249 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13250 {
13251 struct got_entry **lgot_ents, **end_lgot_ents;
13252 struct plt_entry **local_plt, **lplt, **end_local_plt;
13253 Elf_Internal_Shdr *symtab_hdr;
13254 bfd_size_type locsymcount;
13255 Elf_Internal_Sym *local_syms = NULL;
13256 struct plt_entry *ent;
13257
13258 if (!is_ppc64_elf (ibfd))
13259 continue;
13260
13261 lgot_ents = elf_local_got_ents (ibfd);
13262 if (!lgot_ents)
13263 continue;
13264
13265 symtab_hdr = &elf_symtab_hdr (ibfd);
13266 locsymcount = symtab_hdr->sh_info;
13267 end_lgot_ents = lgot_ents + locsymcount;
13268 local_plt = (struct plt_entry **) end_lgot_ents;
13269 end_local_plt = local_plt + locsymcount;
13270 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13271 for (ent = *lplt; ent != NULL; ent = ent->next)
13272 if (ent->plt.offset != (bfd_vma) -1)
13273 {
13274 Elf_Internal_Sym *sym;
13275 asection *sym_sec;
13276 asection *plt, *relplt;
13277 bfd_byte *loc;
13278 bfd_vma val;
13279
13280 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13281 lplt - local_plt, ibfd))
13282 {
13283 if (local_syms != NULL
13284 && symtab_hdr->contents != (unsigned char *) local_syms)
13285 free (local_syms);
13286 return FALSE;
13287 }
13288
13289 val = sym->st_value + ent->addend;
13290 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13291 if (sym_sec != NULL && sym_sec->output_section != NULL)
13292 val += sym_sec->output_offset + sym_sec->output_section->vma;
13293
13294 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13295 {
13296 htab->local_ifunc_resolver = 1;
13297 plt = htab->elf.iplt;
13298 relplt = htab->elf.irelplt;
13299 }
13300 else
13301 {
13302 plt = htab->pltlocal;
13303 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13304 }
13305
13306 if (relplt == NULL)
13307 {
13308 loc = plt->contents + ent->plt.offset;
13309 bfd_put_64 (info->output_bfd, val, loc);
13310 if (htab->opd_abi)
13311 {
13312 bfd_vma toc = elf_gp (ibfd);
13313 bfd_put_64 (info->output_bfd, toc, loc + 8);
13314 }
13315 }
13316 else
13317 {
13318 Elf_Internal_Rela rela;
13319 rela.r_offset = (ent->plt.offset
13320 + plt->output_offset
13321 + plt->output_section->vma);
13322 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13323 {
13324 if (htab->opd_abi)
13325 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13326 else
13327 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13328 }
13329 else
13330 {
13331 if (htab->opd_abi)
13332 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13333 else
13334 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13335 }
13336 rela.r_addend = val;
13337 loc = relplt->contents + (relplt->reloc_count++
13338 * sizeof (Elf64_External_Rela));
13339 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13340 }
13341 }
13342
13343 if (local_syms != NULL
13344 && symtab_hdr->contents != (unsigned char *) local_syms)
13345 {
13346 if (!info->keep_memory)
13347 free (local_syms);
13348 else
13349 symtab_hdr->contents = (unsigned char *) local_syms;
13350 }
13351 }
13352 return TRUE;
13353 }
13354
13355 /* Build all the stubs associated with the current output file.
13356 The stubs are kept in a hash table attached to the main linker
13357 hash table. This function is called via gldelf64ppc_finish. */
13358
13359 bfd_boolean
13360 ppc64_elf_build_stubs (struct bfd_link_info *info,
13361 char **stats)
13362 {
13363 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13364 struct map_stub *group;
13365 asection *stub_sec;
13366 bfd_byte *p;
13367 int stub_sec_count = 0;
13368
13369 if (htab == NULL)
13370 return FALSE;
13371
13372 /* Allocate memory to hold the linker stubs. */
13373 for (group = htab->group; group != NULL; group = group->next)
13374 if ((stub_sec = group->stub_sec) != NULL
13375 && stub_sec->size != 0)
13376 {
13377 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13378 if (stub_sec->contents == NULL)
13379 return FALSE;
13380 stub_sec->size = 0;
13381 }
13382
13383 if (htab->glink != NULL && htab->glink->size != 0)
13384 {
13385 unsigned int indx;
13386 bfd_vma plt0;
13387
13388 /* Build the .glink plt call stub. */
13389 if (htab->params->emit_stub_syms)
13390 {
13391 struct elf_link_hash_entry *h;
13392 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13393 TRUE, FALSE, FALSE);
13394 if (h == NULL)
13395 return FALSE;
13396 if (h->root.type == bfd_link_hash_new)
13397 {
13398 h->root.type = bfd_link_hash_defined;
13399 h->root.u.def.section = htab->glink;
13400 h->root.u.def.value = 8;
13401 h->ref_regular = 1;
13402 h->def_regular = 1;
13403 h->ref_regular_nonweak = 1;
13404 h->forced_local = 1;
13405 h->non_elf = 0;
13406 h->root.linker_def = 1;
13407 }
13408 }
13409 plt0 = (htab->elf.splt->output_section->vma
13410 + htab->elf.splt->output_offset
13411 - 16);
13412 if (info->emitrelocations)
13413 {
13414 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13415 if (r == NULL)
13416 return FALSE;
13417 r->r_offset = (htab->glink->output_offset
13418 + htab->glink->output_section->vma);
13419 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13420 r->r_addend = plt0;
13421 }
13422 p = htab->glink->contents;
13423 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13424 bfd_put_64 (htab->glink->owner, plt0, p);
13425 p += 8;
13426 if (htab->opd_abi)
13427 {
13428 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13429 p += 4;
13430 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13431 p += 4;
13432 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13433 p += 4;
13434 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13435 p += 4;
13436 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13437 p += 4;
13438 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13439 p += 4;
13440 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13441 p += 4;
13442 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13443 p += 4;
13444 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13445 p += 4;
13446 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13447 p += 4;
13448 }
13449 else
13450 {
13451 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13452 p += 4;
13453 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13454 p += 4;
13455 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13456 p += 4;
13457 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13458 p += 4;
13459 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13460 p += 4;
13461 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13462 p += 4;
13463 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13464 p += 4;
13465 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13466 p += 4;
13467 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13468 p += 4;
13469 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13470 p += 4;
13471 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13472 p += 4;
13473 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13474 p += 4;
13475 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13476 p += 4;
13477 }
13478 bfd_put_32 (htab->glink->owner, BCTR, p);
13479 p += 4;
13480 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13481
13482 /* Build the .glink lazy link call stubs. */
13483 indx = 0;
13484 while (p < htab->glink->contents + htab->glink->size)
13485 {
13486 if (htab->opd_abi)
13487 {
13488 if (indx < 0x8000)
13489 {
13490 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13491 p += 4;
13492 }
13493 else
13494 {
13495 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13496 p += 4;
13497 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13498 p);
13499 p += 4;
13500 }
13501 }
13502 bfd_put_32 (htab->glink->owner,
13503 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13504 indx++;
13505 p += 4;
13506 }
13507 }
13508
13509 /* Build .glink global entry stubs, and PLT relocs for globals. */
13510 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13511
13512 if (!write_plt_relocs_for_local_syms (info))
13513 return FALSE;
13514
13515 if (htab->brlt != NULL && htab->brlt->size != 0)
13516 {
13517 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13518 htab->brlt->size);
13519 if (htab->brlt->contents == NULL)
13520 return FALSE;
13521 }
13522 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13523 {
13524 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13525 htab->relbrlt->size);
13526 if (htab->relbrlt->contents == NULL)
13527 return FALSE;
13528 }
13529
13530 /* Build the stubs as directed by the stub hash table. */
13531 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13532
13533 for (group = htab->group; group != NULL; group = group->next)
13534 if (group->needs_save_res)
13535 group->stub_sec->size += htab->sfpr->size;
13536
13537 if (htab->relbrlt != NULL)
13538 htab->relbrlt->reloc_count = 0;
13539
13540 if (htab->params->plt_stub_align != 0)
13541 for (group = htab->group; group != NULL; group = group->next)
13542 if ((stub_sec = group->stub_sec) != NULL)
13543 {
13544 int align = abs (htab->params->plt_stub_align);
13545 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13546 }
13547
13548 for (group = htab->group; group != NULL; group = group->next)
13549 if (group->needs_save_res)
13550 {
13551 stub_sec = group->stub_sec;
13552 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13553 htab->sfpr->contents, htab->sfpr->size);
13554 if (htab->params->emit_stub_syms)
13555 {
13556 unsigned int i;
13557
13558 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13559 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13560 return FALSE;
13561 }
13562 }
13563
13564 for (group = htab->group; group != NULL; group = group->next)
13565 if ((stub_sec = group->stub_sec) != NULL)
13566 {
13567 stub_sec_count += 1;
13568 if (stub_sec->rawsize != stub_sec->size
13569 && (htab->stub_iteration <= STUB_SHRINK_ITER
13570 || stub_sec->rawsize < stub_sec->size))
13571 break;
13572 }
13573
13574 if (group != NULL)
13575 {
13576 htab->stub_error = TRUE;
13577 _bfd_error_handler (_("stubs don't match calculated size"));
13578 }
13579
13580 if (htab->stub_error)
13581 return FALSE;
13582
13583 if (stats != NULL)
13584 {
13585 size_t len;
13586 *stats = bfd_malloc (500);
13587 if (*stats == NULL)
13588 return FALSE;
13589
13590 len = sprintf (*stats,
13591 ngettext ("linker stubs in %u group\n",
13592 "linker stubs in %u groups\n",
13593 stub_sec_count),
13594 stub_sec_count);
13595 sprintf (*stats + len, _(" branch %lu\n"
13596 " toc adjust %lu\n"
13597 " long branch %lu\n"
13598 " long toc adj %lu\n"
13599 " plt call %lu\n"
13600 " plt call toc %lu\n"
13601 " global entry %lu"),
13602 htab->stub_count[ppc_stub_long_branch - 1],
13603 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13604 htab->stub_count[ppc_stub_plt_branch - 1],
13605 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13606 htab->stub_count[ppc_stub_plt_call - 1],
13607 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13608 htab->stub_count[ppc_stub_global_entry - 1]);
13609 }
13610 return TRUE;
13611 }
13612
13613 /* What to do when ld finds relocations against symbols defined in
13614 discarded sections. */
13615
13616 static unsigned int
13617 ppc64_elf_action_discarded (asection *sec)
13618 {
13619 if (strcmp (".opd", sec->name) == 0)
13620 return 0;
13621
13622 if (strcmp (".toc", sec->name) == 0)
13623 return 0;
13624
13625 if (strcmp (".toc1", sec->name) == 0)
13626 return 0;
13627
13628 return _bfd_elf_default_action_discarded (sec);
13629 }
13630
13631 /* The RELOCATE_SECTION function is called by the ELF backend linker
13632 to handle the relocations for a section.
13633
13634 The relocs are always passed as Rela structures; if the section
13635 actually uses Rel structures, the r_addend field will always be
13636 zero.
13637
13638 This function is responsible for adjust the section contents as
13639 necessary, and (if using Rela relocs and generating a
13640 relocatable output file) adjusting the reloc addend as
13641 necessary.
13642
13643 This function does not have to worry about setting the reloc
13644 address or the reloc symbol index.
13645
13646 LOCAL_SYMS is a pointer to the swapped in local symbols.
13647
13648 LOCAL_SECTIONS is an array giving the section in the input file
13649 corresponding to the st_shndx field of each local symbol.
13650
13651 The global hash table entry for the global symbols can be found
13652 via elf_sym_hashes (input_bfd).
13653
13654 When generating relocatable output, this function must handle
13655 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13656 going to be the section symbol corresponding to the output
13657 section, which means that the addend must be adjusted
13658 accordingly. */
13659
13660 static bfd_boolean
13661 ppc64_elf_relocate_section (bfd *output_bfd,
13662 struct bfd_link_info *info,
13663 bfd *input_bfd,
13664 asection *input_section,
13665 bfd_byte *contents,
13666 Elf_Internal_Rela *relocs,
13667 Elf_Internal_Sym *local_syms,
13668 asection **local_sections)
13669 {
13670 struct ppc_link_hash_table *htab;
13671 Elf_Internal_Shdr *symtab_hdr;
13672 struct elf_link_hash_entry **sym_hashes;
13673 Elf_Internal_Rela *rel;
13674 Elf_Internal_Rela *wrel;
13675 Elf_Internal_Rela *relend;
13676 Elf_Internal_Rela outrel;
13677 bfd_byte *loc;
13678 struct got_entry **local_got_ents;
13679 bfd_vma TOCstart;
13680 bfd_boolean ret = TRUE;
13681 bfd_boolean is_opd;
13682 /* Assume 'at' branch hints. */
13683 bfd_boolean is_isa_v2 = TRUE;
13684 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13685
13686 /* Initialize howto table if needed. */
13687 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13688 ppc_howto_init ();
13689
13690 htab = ppc_hash_table (info);
13691 if (htab == NULL)
13692 return FALSE;
13693
13694 /* Don't relocate stub sections. */
13695 if (input_section->owner == htab->params->stub_bfd)
13696 return TRUE;
13697
13698 BFD_ASSERT (is_ppc64_elf (input_bfd));
13699
13700 local_got_ents = elf_local_got_ents (input_bfd);
13701 TOCstart = elf_gp (output_bfd);
13702 symtab_hdr = &elf_symtab_hdr (input_bfd);
13703 sym_hashes = elf_sym_hashes (input_bfd);
13704 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13705
13706 rel = wrel = relocs;
13707 relend = relocs + input_section->reloc_count;
13708 for (; rel < relend; wrel++, rel++)
13709 {
13710 enum elf_ppc64_reloc_type r_type;
13711 bfd_vma addend;
13712 bfd_reloc_status_type r;
13713 Elf_Internal_Sym *sym;
13714 asection *sec;
13715 struct elf_link_hash_entry *h_elf;
13716 struct ppc_link_hash_entry *h;
13717 struct ppc_link_hash_entry *fdh;
13718 const char *sym_name;
13719 unsigned long r_symndx, toc_symndx;
13720 bfd_vma toc_addend;
13721 unsigned char tls_mask, tls_gd, tls_type;
13722 unsigned char sym_type;
13723 bfd_vma relocation;
13724 bfd_boolean unresolved_reloc;
13725 bfd_boolean warned;
13726 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13727 unsigned int insn;
13728 unsigned int mask;
13729 struct ppc_stub_hash_entry *stub_entry;
13730 bfd_vma max_br_offset;
13731 bfd_vma from;
13732 Elf_Internal_Rela orig_rel;
13733 reloc_howto_type *howto;
13734 struct reloc_howto_struct alt_howto;
13735
13736 again:
13737 orig_rel = *rel;
13738
13739 r_type = ELF64_R_TYPE (rel->r_info);
13740 r_symndx = ELF64_R_SYM (rel->r_info);
13741
13742 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13743 symbol of the previous ADDR64 reloc. The symbol gives us the
13744 proper TOC base to use. */
13745 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13746 && wrel != relocs
13747 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13748 && is_opd)
13749 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13750
13751 sym = NULL;
13752 sec = NULL;
13753 h_elf = NULL;
13754 sym_name = NULL;
13755 unresolved_reloc = FALSE;
13756 warned = FALSE;
13757
13758 if (r_symndx < symtab_hdr->sh_info)
13759 {
13760 /* It's a local symbol. */
13761 struct _opd_sec_data *opd;
13762
13763 sym = local_syms + r_symndx;
13764 sec = local_sections[r_symndx];
13765 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13766 sym_type = ELF64_ST_TYPE (sym->st_info);
13767 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13768 opd = get_opd_info (sec);
13769 if (opd != NULL && opd->adjust != NULL)
13770 {
13771 long adjust = opd->adjust[OPD_NDX (sym->st_value
13772 + rel->r_addend)];
13773 if (adjust == -1)
13774 relocation = 0;
13775 else
13776 {
13777 /* If this is a relocation against the opd section sym
13778 and we have edited .opd, adjust the reloc addend so
13779 that ld -r and ld --emit-relocs output is correct.
13780 If it is a reloc against some other .opd symbol,
13781 then the symbol value will be adjusted later. */
13782 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13783 rel->r_addend += adjust;
13784 else
13785 relocation += adjust;
13786 }
13787 }
13788 }
13789 else
13790 {
13791 bfd_boolean ignored;
13792
13793 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13794 r_symndx, symtab_hdr, sym_hashes,
13795 h_elf, sec, relocation,
13796 unresolved_reloc, warned, ignored);
13797 sym_name = h_elf->root.root.string;
13798 sym_type = h_elf->type;
13799 if (sec != NULL
13800 && sec->owner == output_bfd
13801 && strcmp (sec->name, ".opd") == 0)
13802 {
13803 /* This is a symbol defined in a linker script. All
13804 such are defined in output sections, even those
13805 defined by simple assignment from a symbol defined in
13806 an input section. Transfer the symbol to an
13807 appropriate input .opd section, so that a branch to
13808 this symbol will be mapped to the location specified
13809 by the opd entry. */
13810 struct bfd_link_order *lo;
13811 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13812 if (lo->type == bfd_indirect_link_order)
13813 {
13814 asection *isec = lo->u.indirect.section;
13815 if (h_elf->root.u.def.value >= isec->output_offset
13816 && h_elf->root.u.def.value < (isec->output_offset
13817 + isec->size))
13818 {
13819 h_elf->root.u.def.value -= isec->output_offset;
13820 h_elf->root.u.def.section = isec;
13821 sec = isec;
13822 break;
13823 }
13824 }
13825 }
13826 }
13827 h = (struct ppc_link_hash_entry *) h_elf;
13828
13829 if (sec != NULL && discarded_section (sec))
13830 {
13831 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13832 input_bfd, input_section,
13833 contents + rel->r_offset);
13834 wrel->r_offset = rel->r_offset;
13835 wrel->r_info = 0;
13836 wrel->r_addend = 0;
13837
13838 /* For ld -r, remove relocations in debug sections against
13839 symbols defined in discarded sections. Not done for
13840 non-debug to preserve relocs in .eh_frame which the
13841 eh_frame editing code expects to be present. */
13842 if (bfd_link_relocatable (info)
13843 && (input_section->flags & SEC_DEBUGGING))
13844 wrel--;
13845
13846 continue;
13847 }
13848
13849 if (bfd_link_relocatable (info))
13850 goto copy_reloc;
13851
13852 if (h != NULL && &h->elf == htab->elf.hgot)
13853 {
13854 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13855 sec = bfd_abs_section_ptr;
13856 unresolved_reloc = FALSE;
13857 }
13858
13859 /* TLS optimizations. Replace instruction sequences and relocs
13860 based on information we collected in tls_optimize. We edit
13861 RELOCS so that --emit-relocs will output something sensible
13862 for the final instruction stream. */
13863 tls_mask = 0;
13864 tls_gd = 0;
13865 toc_symndx = 0;
13866 if (h != NULL)
13867 tls_mask = h->tls_mask;
13868 else if (local_got_ents != NULL)
13869 {
13870 struct plt_entry **local_plt = (struct plt_entry **)
13871 (local_got_ents + symtab_hdr->sh_info);
13872 unsigned char *lgot_masks = (unsigned char *)
13873 (local_plt + symtab_hdr->sh_info);
13874 tls_mask = lgot_masks[r_symndx];
13875 }
13876 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
13877 && (r_type == R_PPC64_TLS
13878 || r_type == R_PPC64_TLSGD
13879 || r_type == R_PPC64_TLSLD))
13880 {
13881 /* Check for toc tls entries. */
13882 unsigned char *toc_tls;
13883
13884 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13885 &local_syms, rel, input_bfd))
13886 return FALSE;
13887
13888 if (toc_tls)
13889 tls_mask = *toc_tls;
13890 }
13891
13892 /* Check that tls relocs are used with tls syms, and non-tls
13893 relocs are used with non-tls syms. */
13894 if (r_symndx != STN_UNDEF
13895 && r_type != R_PPC64_NONE
13896 && (h == NULL
13897 || h->elf.root.type == bfd_link_hash_defined
13898 || h->elf.root.type == bfd_link_hash_defweak)
13899 && (IS_PPC64_TLS_RELOC (r_type)
13900 != (sym_type == STT_TLS
13901 || (sym_type == STT_SECTION
13902 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13903 {
13904 if ((tls_mask & TLS_TLS) != 0
13905 && (r_type == R_PPC64_TLS
13906 || r_type == R_PPC64_TLSGD
13907 || r_type == R_PPC64_TLSLD))
13908 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13909 ;
13910 else
13911 info->callbacks->einfo
13912 (!IS_PPC64_TLS_RELOC (r_type)
13913 /* xgettext:c-format */
13914 ? _("%H: %s used with TLS symbol `%pT'\n")
13915 /* xgettext:c-format */
13916 : _("%H: %s used with non-TLS symbol `%pT'\n"),
13917 input_bfd, input_section, rel->r_offset,
13918 ppc64_elf_howto_table[r_type]->name,
13919 sym_name);
13920 }
13921
13922 /* Ensure reloc mapping code below stays sane. */
13923 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13924 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13925 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13926 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13927 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13928 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13929 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13930 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13931 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13932 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13933 abort ();
13934
13935 switch (r_type)
13936 {
13937 default:
13938 break;
13939
13940 case R_PPC64_LO_DS_OPT:
13941 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13942 if ((insn & (0x3f << 26)) != 58u << 26)
13943 abort ();
13944 insn += (14u << 26) - (58u << 26);
13945 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13946 r_type = R_PPC64_TOC16_LO;
13947 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13948 break;
13949
13950 case R_PPC64_TOC16:
13951 case R_PPC64_TOC16_LO:
13952 case R_PPC64_TOC16_DS:
13953 case R_PPC64_TOC16_LO_DS:
13954 {
13955 /* Check for toc tls entries. */
13956 unsigned char *toc_tls;
13957 int retval;
13958
13959 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13960 &local_syms, rel, input_bfd);
13961 if (retval == 0)
13962 return FALSE;
13963
13964 if (toc_tls)
13965 {
13966 tls_mask = *toc_tls;
13967 if (r_type == R_PPC64_TOC16_DS
13968 || r_type == R_PPC64_TOC16_LO_DS)
13969 {
13970 if ((tls_mask & TLS_TLS) != 0
13971 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13972 goto toctprel;
13973 }
13974 else
13975 {
13976 /* If we found a GD reloc pair, then we might be
13977 doing a GD->IE transition. */
13978 if (retval == 2)
13979 {
13980 tls_gd = TLS_TPRELGD;
13981 if ((tls_mask & TLS_TLS) != 0
13982 && (tls_mask & TLS_GD) == 0)
13983 goto tls_ldgd_opt;
13984 }
13985 else if (retval == 3)
13986 {
13987 if ((tls_mask & TLS_TLS) != 0
13988 && (tls_mask & TLS_LD) == 0)
13989 goto tls_ldgd_opt;
13990 }
13991 }
13992 }
13993 }
13994 break;
13995
13996 case R_PPC64_GOT_TPREL16_HI:
13997 case R_PPC64_GOT_TPREL16_HA:
13998 if ((tls_mask & TLS_TLS) != 0
13999 && (tls_mask & TLS_TPREL) == 0)
14000 {
14001 rel->r_offset -= d_offset;
14002 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14003 r_type = R_PPC64_NONE;
14004 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14005 }
14006 break;
14007
14008 case R_PPC64_GOT_TPREL16_DS:
14009 case R_PPC64_GOT_TPREL16_LO_DS:
14010 if ((tls_mask & TLS_TLS) != 0
14011 && (tls_mask & TLS_TPREL) == 0)
14012 {
14013 toctprel:
14014 insn = bfd_get_32 (input_bfd,
14015 contents + rel->r_offset - d_offset);
14016 insn &= 31 << 21;
14017 insn |= 0x3c0d0000; /* addis 0,13,0 */
14018 bfd_put_32 (input_bfd, insn,
14019 contents + rel->r_offset - d_offset);
14020 r_type = R_PPC64_TPREL16_HA;
14021 if (toc_symndx != 0)
14022 {
14023 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14024 rel->r_addend = toc_addend;
14025 /* We changed the symbol. Start over in order to
14026 get h, sym, sec etc. right. */
14027 goto again;
14028 }
14029 else
14030 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14031 }
14032 break;
14033
14034 case R_PPC64_TLS:
14035 if ((tls_mask & TLS_TLS) != 0
14036 && (tls_mask & TLS_TPREL) == 0)
14037 {
14038 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14039 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14040 if (insn == 0)
14041 abort ();
14042 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14043 /* Was PPC64_TLS which sits on insn boundary, now
14044 PPC64_TPREL16_LO which is at low-order half-word. */
14045 rel->r_offset += d_offset;
14046 r_type = R_PPC64_TPREL16_LO;
14047 if (toc_symndx != 0)
14048 {
14049 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14050 rel->r_addend = toc_addend;
14051 /* We changed the symbol. Start over in order to
14052 get h, sym, sec etc. right. */
14053 goto again;
14054 }
14055 else
14056 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14057 }
14058 break;
14059
14060 case R_PPC64_GOT_TLSGD16_HI:
14061 case R_PPC64_GOT_TLSGD16_HA:
14062 tls_gd = TLS_TPRELGD;
14063 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14064 goto tls_gdld_hi;
14065 break;
14066
14067 case R_PPC64_GOT_TLSLD16_HI:
14068 case R_PPC64_GOT_TLSLD16_HA:
14069 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14070 {
14071 tls_gdld_hi:
14072 if ((tls_mask & tls_gd) != 0)
14073 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14074 + R_PPC64_GOT_TPREL16_DS);
14075 else
14076 {
14077 rel->r_offset -= d_offset;
14078 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14079 r_type = R_PPC64_NONE;
14080 }
14081 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14082 }
14083 break;
14084
14085 case R_PPC64_GOT_TLSGD16:
14086 case R_PPC64_GOT_TLSGD16_LO:
14087 tls_gd = TLS_TPRELGD;
14088 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14089 goto tls_ldgd_opt;
14090 break;
14091
14092 case R_PPC64_GOT_TLSLD16:
14093 case R_PPC64_GOT_TLSLD16_LO:
14094 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14095 {
14096 unsigned int insn1, insn2;
14097 bfd_vma offset;
14098
14099 tls_ldgd_opt:
14100 offset = (bfd_vma) -1;
14101 /* If not using the newer R_PPC64_TLSGD/LD to mark
14102 __tls_get_addr calls, we must trust that the call
14103 stays with its arg setup insns, ie. that the next
14104 reloc is the __tls_get_addr call associated with
14105 the current reloc. Edit both insns. */
14106 if (input_section->has_tls_get_addr_call
14107 && rel + 1 < relend
14108 && branch_reloc_hash_match (input_bfd, rel + 1,
14109 htab->tls_get_addr,
14110 htab->tls_get_addr_fd))
14111 offset = rel[1].r_offset;
14112 /* We read the low GOT_TLS (or TOC16) insn because we
14113 need to keep the destination reg. It may be
14114 something other than the usual r3, and moved to r3
14115 before the call by intervening code. */
14116 insn1 = bfd_get_32 (input_bfd,
14117 contents + rel->r_offset - d_offset);
14118 if ((tls_mask & tls_gd) != 0)
14119 {
14120 /* IE */
14121 insn1 &= (0x1f << 21) | (0x1f << 16);
14122 insn1 |= 58 << 26; /* ld */
14123 insn2 = 0x7c636a14; /* add 3,3,13 */
14124 if (offset != (bfd_vma) -1)
14125 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14126 if ((tls_mask & TLS_EXPLICIT) == 0)
14127 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14128 + R_PPC64_GOT_TPREL16_DS);
14129 else
14130 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14131 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14132 }
14133 else
14134 {
14135 /* LE */
14136 insn1 &= 0x1f << 21;
14137 insn1 |= 0x3c0d0000; /* addis r,13,0 */
14138 insn2 = 0x38630000; /* addi 3,3,0 */
14139 if (tls_gd == 0)
14140 {
14141 /* Was an LD reloc. */
14142 if (toc_symndx)
14143 sec = local_sections[toc_symndx];
14144 for (r_symndx = 0;
14145 r_symndx < symtab_hdr->sh_info;
14146 r_symndx++)
14147 if (local_sections[r_symndx] == sec)
14148 break;
14149 if (r_symndx >= symtab_hdr->sh_info)
14150 r_symndx = STN_UNDEF;
14151 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14152 if (r_symndx != STN_UNDEF)
14153 rel->r_addend -= (local_syms[r_symndx].st_value
14154 + sec->output_offset
14155 + sec->output_section->vma);
14156 }
14157 else if (toc_symndx != 0)
14158 {
14159 r_symndx = toc_symndx;
14160 rel->r_addend = toc_addend;
14161 }
14162 r_type = R_PPC64_TPREL16_HA;
14163 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14164 if (offset != (bfd_vma) -1)
14165 {
14166 rel[1].r_info = ELF64_R_INFO (r_symndx,
14167 R_PPC64_TPREL16_LO);
14168 rel[1].r_offset = offset + d_offset;
14169 rel[1].r_addend = rel->r_addend;
14170 }
14171 }
14172 bfd_put_32 (input_bfd, insn1,
14173 contents + rel->r_offset - d_offset);
14174 if (offset != (bfd_vma) -1)
14175 bfd_put_32 (input_bfd, insn2, contents + offset);
14176 if ((tls_mask & tls_gd) == 0
14177 && (tls_gd == 0 || toc_symndx != 0))
14178 {
14179 /* We changed the symbol. Start over in order
14180 to get h, sym, sec etc. right. */
14181 goto again;
14182 }
14183 }
14184 break;
14185
14186 case R_PPC64_TLSGD:
14187 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14188 && rel + 1 < relend)
14189 {
14190 unsigned int insn2;
14191 bfd_vma offset = rel->r_offset;
14192
14193 if ((tls_mask & TLS_TPRELGD) != 0)
14194 {
14195 /* IE */
14196 r_type = R_PPC64_NONE;
14197 insn2 = 0x7c636a14; /* add 3,3,13 */
14198 }
14199 else
14200 {
14201 /* LE */
14202 if (toc_symndx != 0)
14203 {
14204 r_symndx = toc_symndx;
14205 rel->r_addend = toc_addend;
14206 }
14207 r_type = R_PPC64_TPREL16_LO;
14208 rel->r_offset = offset + d_offset;
14209 insn2 = 0x38630000; /* addi 3,3,0 */
14210 }
14211 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14212 /* Zap the reloc on the _tls_get_addr call too. */
14213 BFD_ASSERT (offset == rel[1].r_offset);
14214 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14215 bfd_put_32 (input_bfd, insn2, contents + offset);
14216 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
14217 goto again;
14218 }
14219 break;
14220
14221 case R_PPC64_TLSLD:
14222 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14223 && rel + 1 < relend)
14224 {
14225 unsigned int insn2;
14226 bfd_vma offset = rel->r_offset;
14227
14228 if (toc_symndx)
14229 sec = local_sections[toc_symndx];
14230 for (r_symndx = 0;
14231 r_symndx < symtab_hdr->sh_info;
14232 r_symndx++)
14233 if (local_sections[r_symndx] == sec)
14234 break;
14235 if (r_symndx >= symtab_hdr->sh_info)
14236 r_symndx = STN_UNDEF;
14237 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14238 if (r_symndx != STN_UNDEF)
14239 rel->r_addend -= (local_syms[r_symndx].st_value
14240 + sec->output_offset
14241 + sec->output_section->vma);
14242
14243 r_type = R_PPC64_TPREL16_LO;
14244 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14245 rel->r_offset = offset + d_offset;
14246 /* Zap the reloc on the _tls_get_addr call too. */
14247 BFD_ASSERT (offset == rel[1].r_offset);
14248 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14249 insn2 = 0x38630000; /* addi 3,3,0 */
14250 bfd_put_32 (input_bfd, insn2, contents + offset);
14251 goto again;
14252 }
14253 break;
14254
14255 case R_PPC64_DTPMOD64:
14256 if (rel + 1 < relend
14257 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14258 && rel[1].r_offset == rel->r_offset + 8)
14259 {
14260 if ((tls_mask & TLS_GD) == 0)
14261 {
14262 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14263 if ((tls_mask & TLS_TPRELGD) != 0)
14264 r_type = R_PPC64_TPREL64;
14265 else
14266 {
14267 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14268 r_type = R_PPC64_NONE;
14269 }
14270 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14271 }
14272 }
14273 else
14274 {
14275 if ((tls_mask & TLS_LD) == 0)
14276 {
14277 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14278 r_type = R_PPC64_NONE;
14279 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14280 }
14281 }
14282 break;
14283
14284 case R_PPC64_TPREL64:
14285 if ((tls_mask & TLS_TPREL) == 0)
14286 {
14287 r_type = R_PPC64_NONE;
14288 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14289 }
14290 break;
14291
14292 case R_PPC64_ENTRY:
14293 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14294 if (!bfd_link_pic (info)
14295 && !info->traditional_format
14296 && relocation + 0x80008000 <= 0xffffffff)
14297 {
14298 unsigned int insn1, insn2;
14299
14300 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14301 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14302 if ((insn1 & ~0xfffc) == LD_R2_0R12
14303 && insn2 == ADD_R2_R2_R12)
14304 {
14305 bfd_put_32 (input_bfd,
14306 LIS_R2 + PPC_HA (relocation),
14307 contents + rel->r_offset);
14308 bfd_put_32 (input_bfd,
14309 ADDI_R2_R2 + PPC_LO (relocation),
14310 contents + rel->r_offset + 4);
14311 }
14312 }
14313 else
14314 {
14315 relocation -= (rel->r_offset
14316 + input_section->output_offset
14317 + input_section->output_section->vma);
14318 if (relocation + 0x80008000 <= 0xffffffff)
14319 {
14320 unsigned int insn1, insn2;
14321
14322 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14323 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14324 if ((insn1 & ~0xfffc) == LD_R2_0R12
14325 && insn2 == ADD_R2_R2_R12)
14326 {
14327 bfd_put_32 (input_bfd,
14328 ADDIS_R2_R12 + PPC_HA (relocation),
14329 contents + rel->r_offset);
14330 bfd_put_32 (input_bfd,
14331 ADDI_R2_R2 + PPC_LO (relocation),
14332 contents + rel->r_offset + 4);
14333 }
14334 }
14335 }
14336 break;
14337
14338 case R_PPC64_REL16_HA:
14339 /* If we are generating a non-PIC executable, edit
14340 . 0: addis 2,12,.TOC.-0b@ha
14341 . addi 2,2,.TOC.-0b@l
14342 used by ELFv2 global entry points to set up r2, to
14343 . lis 2,.TOC.@ha
14344 . addi 2,2,.TOC.@l
14345 if .TOC. is in range. */
14346 if (!bfd_link_pic (info)
14347 && !info->traditional_format
14348 && !htab->opd_abi
14349 && rel->r_addend == d_offset
14350 && h != NULL && &h->elf == htab->elf.hgot
14351 && rel + 1 < relend
14352 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14353 && rel[1].r_offset == rel->r_offset + 4
14354 && rel[1].r_addend == rel->r_addend + 4
14355 && relocation + 0x80008000 <= 0xffffffff)
14356 {
14357 unsigned int insn1, insn2;
14358 bfd_vma offset = rel->r_offset - d_offset;
14359 insn1 = bfd_get_32 (input_bfd, contents + offset);
14360 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14361 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14362 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14363 {
14364 r_type = R_PPC64_ADDR16_HA;
14365 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14366 rel->r_addend -= d_offset;
14367 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14368 rel[1].r_addend -= d_offset + 4;
14369 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14370 }
14371 }
14372 break;
14373 }
14374
14375 /* Handle other relocations that tweak non-addend part of insn. */
14376 insn = 0;
14377 max_br_offset = 1 << 25;
14378 addend = rel->r_addend;
14379 reloc_dest = DEST_NORMAL;
14380 switch (r_type)
14381 {
14382 default:
14383 break;
14384
14385 case R_PPC64_TOCSAVE:
14386 if (relocation + addend == (rel->r_offset
14387 + input_section->output_offset
14388 + input_section->output_section->vma)
14389 && tocsave_find (htab, NO_INSERT,
14390 &local_syms, rel, input_bfd))
14391 {
14392 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14393 if (insn == NOP
14394 || insn == CROR_151515 || insn == CROR_313131)
14395 bfd_put_32 (input_bfd,
14396 STD_R2_0R1 + STK_TOC (htab),
14397 contents + rel->r_offset);
14398 }
14399 break;
14400
14401 /* Branch taken prediction relocations. */
14402 case R_PPC64_ADDR14_BRTAKEN:
14403 case R_PPC64_REL14_BRTAKEN:
14404 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14405 /* Fall through. */
14406
14407 /* Branch not taken prediction relocations. */
14408 case R_PPC64_ADDR14_BRNTAKEN:
14409 case R_PPC64_REL14_BRNTAKEN:
14410 insn |= bfd_get_32 (input_bfd,
14411 contents + rel->r_offset) & ~(0x01 << 21);
14412 /* Fall through. */
14413
14414 case R_PPC64_REL14:
14415 max_br_offset = 1 << 15;
14416 /* Fall through. */
14417
14418 case R_PPC64_REL24:
14419 /* Calls to functions with a different TOC, such as calls to
14420 shared objects, need to alter the TOC pointer. This is
14421 done using a linkage stub. A REL24 branching to these
14422 linkage stubs needs to be followed by a nop, as the nop
14423 will be replaced with an instruction to restore the TOC
14424 base pointer. */
14425 fdh = h;
14426 if (h != NULL
14427 && h->oh != NULL
14428 && h->oh->is_func_descriptor)
14429 fdh = ppc_follow_link (h->oh);
14430 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14431 htab);
14432 if (stub_entry != NULL
14433 && (stub_entry->stub_type == ppc_stub_plt_call
14434 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14435 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14436 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14437 {
14438 bfd_boolean can_plt_call = FALSE;
14439
14440 if (stub_entry->stub_type == ppc_stub_plt_call
14441 && !htab->opd_abi
14442 && htab->params->plt_localentry0 != 0
14443 && is_elfv2_localentry0 (&h->elf))
14444 {
14445 /* The function doesn't use or change r2. */
14446 can_plt_call = TRUE;
14447 }
14448
14449 /* All of these stubs may modify r2, so there must be a
14450 branch and link followed by a nop. The nop is
14451 replaced by an insn to restore r2. */
14452 else if (rel->r_offset + 8 <= input_section->size)
14453 {
14454 unsigned long br;
14455
14456 br = bfd_get_32 (input_bfd,
14457 contents + rel->r_offset);
14458 if ((br & 1) != 0)
14459 {
14460 unsigned long nop;
14461
14462 nop = bfd_get_32 (input_bfd,
14463 contents + rel->r_offset + 4);
14464 if (nop == NOP
14465 || nop == CROR_151515 || nop == CROR_313131)
14466 {
14467 if (h != NULL
14468 && (h == htab->tls_get_addr_fd
14469 || h == htab->tls_get_addr)
14470 && htab->params->tls_get_addr_opt)
14471 {
14472 /* Special stub used, leave nop alone. */
14473 }
14474 else
14475 bfd_put_32 (input_bfd,
14476 LD_R2_0R1 + STK_TOC (htab),
14477 contents + rel->r_offset + 4);
14478 can_plt_call = TRUE;
14479 }
14480 }
14481 }
14482
14483 if (!can_plt_call && h != NULL)
14484 {
14485 const char *name = h->elf.root.root.string;
14486
14487 if (*name == '.')
14488 ++name;
14489
14490 if (strncmp (name, "__libc_start_main", 17) == 0
14491 && (name[17] == 0 || name[17] == '@'))
14492 {
14493 /* Allow crt1 branch to go via a toc adjusting
14494 stub. Other calls that never return could do
14495 the same, if we could detect such. */
14496 can_plt_call = TRUE;
14497 }
14498 }
14499
14500 if (!can_plt_call)
14501 {
14502 /* g++ as of 20130507 emits self-calls without a
14503 following nop. This is arguably wrong since we
14504 have conflicting information. On the one hand a
14505 global symbol and on the other a local call
14506 sequence, but don't error for this special case.
14507 It isn't possible to cheaply verify we have
14508 exactly such a call. Allow all calls to the same
14509 section. */
14510 asection *code_sec = sec;
14511
14512 if (get_opd_info (sec) != NULL)
14513 {
14514 bfd_vma off = (relocation + addend
14515 - sec->output_section->vma
14516 - sec->output_offset);
14517
14518 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14519 }
14520 if (code_sec == input_section)
14521 can_plt_call = TRUE;
14522 }
14523
14524 if (!can_plt_call)
14525 {
14526 if (stub_entry->stub_type == ppc_stub_plt_call
14527 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14528 info->callbacks->einfo
14529 /* xgettext:c-format */
14530 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14531 "recompile with -fPIC\n"),
14532 input_bfd, input_section, rel->r_offset, sym_name);
14533 else
14534 info->callbacks->einfo
14535 /* xgettext:c-format */
14536 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14537 "(-mcmodel=small toc adjust stub)\n"),
14538 input_bfd, input_section, rel->r_offset, sym_name);
14539
14540 bfd_set_error (bfd_error_bad_value);
14541 ret = FALSE;
14542 }
14543
14544 if (can_plt_call
14545 && (stub_entry->stub_type == ppc_stub_plt_call
14546 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14547 unresolved_reloc = FALSE;
14548 }
14549
14550 if ((stub_entry == NULL
14551 || stub_entry->stub_type == ppc_stub_long_branch
14552 || stub_entry->stub_type == ppc_stub_plt_branch)
14553 && get_opd_info (sec) != NULL)
14554 {
14555 /* The branch destination is the value of the opd entry. */
14556 bfd_vma off = (relocation + addend
14557 - sec->output_section->vma
14558 - sec->output_offset);
14559 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14560 if (dest != (bfd_vma) -1)
14561 {
14562 relocation = dest;
14563 addend = 0;
14564 reloc_dest = DEST_OPD;
14565 }
14566 }
14567
14568 /* If the branch is out of reach we ought to have a long
14569 branch stub. */
14570 from = (rel->r_offset
14571 + input_section->output_offset
14572 + input_section->output_section->vma);
14573
14574 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14575 ? fdh->elf.other
14576 : sym->st_other);
14577
14578 if (stub_entry != NULL
14579 && (stub_entry->stub_type == ppc_stub_long_branch
14580 || stub_entry->stub_type == ppc_stub_plt_branch)
14581 && (r_type == R_PPC64_ADDR14_BRTAKEN
14582 || r_type == R_PPC64_ADDR14_BRNTAKEN
14583 || (relocation + addend - from + max_br_offset
14584 < 2 * max_br_offset)))
14585 /* Don't use the stub if this branch is in range. */
14586 stub_entry = NULL;
14587
14588 if (stub_entry != NULL)
14589 {
14590 /* Munge up the value and addend so that we call the stub
14591 rather than the procedure directly. */
14592 asection *stub_sec = stub_entry->group->stub_sec;
14593
14594 if (stub_entry->stub_type == ppc_stub_save_res)
14595 relocation += (stub_sec->output_offset
14596 + stub_sec->output_section->vma
14597 + stub_sec->size - htab->sfpr->size
14598 - htab->sfpr->output_offset
14599 - htab->sfpr->output_section->vma);
14600 else
14601 relocation = (stub_entry->stub_offset
14602 + stub_sec->output_offset
14603 + stub_sec->output_section->vma);
14604 addend = 0;
14605 reloc_dest = DEST_STUB;
14606
14607 if ((stub_entry->stub_type == ppc_stub_plt_call
14608 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14609 && (ALWAYS_EMIT_R2SAVE
14610 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14611 && rel + 1 < relend
14612 && rel[1].r_offset == rel->r_offset + 4
14613 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14614 relocation += 4;
14615 }
14616
14617 if (insn != 0)
14618 {
14619 if (is_isa_v2)
14620 {
14621 /* Set 'a' bit. This is 0b00010 in BO field for branch
14622 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14623 for branch on CTR insns (BO == 1a00t or 1a01t). */
14624 if ((insn & (0x14 << 21)) == (0x04 << 21))
14625 insn |= 0x02 << 21;
14626 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14627 insn |= 0x08 << 21;
14628 else
14629 break;
14630 }
14631 else
14632 {
14633 /* Invert 'y' bit if not the default. */
14634 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14635 insn ^= 0x01 << 21;
14636 }
14637
14638 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14639 }
14640
14641 /* NOP out calls to undefined weak functions.
14642 We can thus call a weak function without first
14643 checking whether the function is defined. */
14644 else if (h != NULL
14645 && h->elf.root.type == bfd_link_hash_undefweak
14646 && h->elf.dynindx == -1
14647 && r_type == R_PPC64_REL24
14648 && relocation == 0
14649 && addend == 0)
14650 {
14651 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14652 goto copy_reloc;
14653 }
14654 break;
14655 }
14656
14657 /* Set `addend'. */
14658 tls_type = 0;
14659 switch (r_type)
14660 {
14661 default:
14662 /* xgettext:c-format */
14663 _bfd_error_handler (_("%pB: %s unsupported"),
14664 input_bfd, ppc64_elf_howto_table[r_type]->name);
14665
14666 bfd_set_error (bfd_error_bad_value);
14667 ret = FALSE;
14668 goto copy_reloc;
14669
14670 case R_PPC64_NONE:
14671 case R_PPC64_TLS:
14672 case R_PPC64_TLSGD:
14673 case R_PPC64_TLSLD:
14674 case R_PPC64_TOCSAVE:
14675 case R_PPC64_GNU_VTINHERIT:
14676 case R_PPC64_GNU_VTENTRY:
14677 case R_PPC64_ENTRY:
14678 goto copy_reloc;
14679
14680 /* GOT16 relocations. Like an ADDR16 using the symbol's
14681 address in the GOT as relocation value instead of the
14682 symbol's value itself. Also, create a GOT entry for the
14683 symbol and put the symbol value there. */
14684 case R_PPC64_GOT_TLSGD16:
14685 case R_PPC64_GOT_TLSGD16_LO:
14686 case R_PPC64_GOT_TLSGD16_HI:
14687 case R_PPC64_GOT_TLSGD16_HA:
14688 tls_type = TLS_TLS | TLS_GD;
14689 goto dogot;
14690
14691 case R_PPC64_GOT_TLSLD16:
14692 case R_PPC64_GOT_TLSLD16_LO:
14693 case R_PPC64_GOT_TLSLD16_HI:
14694 case R_PPC64_GOT_TLSLD16_HA:
14695 tls_type = TLS_TLS | TLS_LD;
14696 goto dogot;
14697
14698 case R_PPC64_GOT_TPREL16_DS:
14699 case R_PPC64_GOT_TPREL16_LO_DS:
14700 case R_PPC64_GOT_TPREL16_HI:
14701 case R_PPC64_GOT_TPREL16_HA:
14702 tls_type = TLS_TLS | TLS_TPREL;
14703 goto dogot;
14704
14705 case R_PPC64_GOT_DTPREL16_DS:
14706 case R_PPC64_GOT_DTPREL16_LO_DS:
14707 case R_PPC64_GOT_DTPREL16_HI:
14708 case R_PPC64_GOT_DTPREL16_HA:
14709 tls_type = TLS_TLS | TLS_DTPREL;
14710 goto dogot;
14711
14712 case R_PPC64_GOT16:
14713 case R_PPC64_GOT16_LO:
14714 case R_PPC64_GOT16_HI:
14715 case R_PPC64_GOT16_HA:
14716 case R_PPC64_GOT16_DS:
14717 case R_PPC64_GOT16_LO_DS:
14718 dogot:
14719 {
14720 /* Relocation is to the entry for this symbol in the global
14721 offset table. */
14722 asection *got;
14723 bfd_vma *offp;
14724 bfd_vma off;
14725 unsigned long indx = 0;
14726 struct got_entry *ent;
14727
14728 if (tls_type == (TLS_TLS | TLS_LD)
14729 && (h == NULL
14730 || !h->elf.def_dynamic))
14731 ent = ppc64_tlsld_got (input_bfd);
14732 else
14733 {
14734 if (h != NULL)
14735 {
14736 if (!htab->elf.dynamic_sections_created
14737 || h->elf.dynindx == -1
14738 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14739 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
14740 /* This is actually a static link, or it is a
14741 -Bsymbolic link and the symbol is defined
14742 locally, or the symbol was forced to be local
14743 because of a version file. */
14744 ;
14745 else
14746 {
14747 indx = h->elf.dynindx;
14748 unresolved_reloc = FALSE;
14749 }
14750 ent = h->elf.got.glist;
14751 }
14752 else
14753 {
14754 if (local_got_ents == NULL)
14755 abort ();
14756 ent = local_got_ents[r_symndx];
14757 }
14758
14759 for (; ent != NULL; ent = ent->next)
14760 if (ent->addend == orig_rel.r_addend
14761 && ent->owner == input_bfd
14762 && ent->tls_type == tls_type)
14763 break;
14764 }
14765
14766 if (ent == NULL)
14767 abort ();
14768 if (ent->is_indirect)
14769 ent = ent->got.ent;
14770 offp = &ent->got.offset;
14771 got = ppc64_elf_tdata (ent->owner)->got;
14772 if (got == NULL)
14773 abort ();
14774
14775 /* The offset must always be a multiple of 8. We use the
14776 least significant bit to record whether we have already
14777 processed this entry. */
14778 off = *offp;
14779 if ((off & 1) != 0)
14780 off &= ~1;
14781 else
14782 {
14783 /* Generate relocs for the dynamic linker, except in
14784 the case of TLSLD where we'll use one entry per
14785 module. */
14786 asection *relgot;
14787 bfd_boolean ifunc;
14788
14789 *offp = off | 1;
14790 relgot = NULL;
14791 ifunc = (h != NULL
14792 ? h->elf.type == STT_GNU_IFUNC
14793 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14794 if (ifunc)
14795 {
14796 relgot = htab->elf.irelplt;
14797 if (indx == 0)
14798 htab->local_ifunc_resolver = 1;
14799 else if (is_static_defined (&h->elf))
14800 htab->maybe_local_ifunc_resolver = 1;
14801 }
14802 else if (indx != 0
14803 || (bfd_link_pic (info)
14804 && (h == NULL
14805 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
14806 || (tls_type == (TLS_TLS | TLS_LD)
14807 && !h->elf.def_dynamic))
14808 && !(tls_type == (TLS_TLS | TLS_TPREL)
14809 && bfd_link_executable (info)
14810 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
14811 relgot = ppc64_elf_tdata (ent->owner)->relgot;
14812 if (relgot != NULL)
14813 {
14814 outrel.r_offset = (got->output_section->vma
14815 + got->output_offset
14816 + off);
14817 outrel.r_addend = addend;
14818 if (tls_type & (TLS_LD | TLS_GD))
14819 {
14820 outrel.r_addend = 0;
14821 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14822 if (tls_type == (TLS_TLS | TLS_GD))
14823 {
14824 loc = relgot->contents;
14825 loc += (relgot->reloc_count++
14826 * sizeof (Elf64_External_Rela));
14827 bfd_elf64_swap_reloca_out (output_bfd,
14828 &outrel, loc);
14829 outrel.r_offset += 8;
14830 outrel.r_addend = addend;
14831 outrel.r_info
14832 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14833 }
14834 }
14835 else if (tls_type == (TLS_TLS | TLS_DTPREL))
14836 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14837 else if (tls_type == (TLS_TLS | TLS_TPREL))
14838 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14839 else if (indx != 0)
14840 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14841 else
14842 {
14843 if (ifunc)
14844 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14845 else
14846 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14847
14848 /* Write the .got section contents for the sake
14849 of prelink. */
14850 loc = got->contents + off;
14851 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14852 loc);
14853 }
14854
14855 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14856 {
14857 outrel.r_addend += relocation;
14858 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14859 {
14860 if (htab->elf.tls_sec == NULL)
14861 outrel.r_addend = 0;
14862 else
14863 outrel.r_addend -= htab->elf.tls_sec->vma;
14864 }
14865 }
14866 loc = relgot->contents;
14867 loc += (relgot->reloc_count++
14868 * sizeof (Elf64_External_Rela));
14869 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14870 }
14871
14872 /* Init the .got section contents here if we're not
14873 emitting a reloc. */
14874 else
14875 {
14876 relocation += addend;
14877 if (tls_type != 0)
14878 {
14879 if (htab->elf.tls_sec == NULL)
14880 relocation = 0;
14881 else
14882 {
14883 if (tls_type & TLS_LD)
14884 relocation = 0;
14885 else
14886 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14887 if (tls_type & TLS_TPREL)
14888 relocation += DTP_OFFSET - TP_OFFSET;
14889 }
14890
14891 if (tls_type & (TLS_GD | TLS_LD))
14892 {
14893 bfd_put_64 (output_bfd, relocation,
14894 got->contents + off + 8);
14895 relocation = 1;
14896 }
14897 }
14898 bfd_put_64 (output_bfd, relocation,
14899 got->contents + off);
14900 }
14901 }
14902
14903 if (off >= (bfd_vma) -2)
14904 abort ();
14905
14906 relocation = got->output_section->vma + got->output_offset + off;
14907 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14908 }
14909 break;
14910
14911 case R_PPC64_PLT16_HA:
14912 case R_PPC64_PLT16_HI:
14913 case R_PPC64_PLT16_LO:
14914 case R_PPC64_PLT16_LO_DS:
14915 case R_PPC64_PLT32:
14916 case R_PPC64_PLT64:
14917 /* Relocation is to the entry for this symbol in the
14918 procedure linkage table. */
14919 {
14920 struct plt_entry **plt_list = NULL;
14921 if (h != NULL)
14922 plt_list = &h->elf.plt.plist;
14923 else if (local_got_ents != NULL)
14924 {
14925 struct plt_entry **local_plt = (struct plt_entry **)
14926 (local_got_ents + symtab_hdr->sh_info);
14927 plt_list = local_plt + r_symndx;
14928 }
14929 if (plt_list)
14930 {
14931 struct plt_entry *ent;
14932
14933 for (ent = *plt_list; ent != NULL; ent = ent->next)
14934 if (ent->plt.offset != (bfd_vma) -1
14935 && ent->addend == orig_rel.r_addend)
14936 {
14937 asection *plt;
14938 bfd_vma got;
14939
14940 plt = htab->elf.splt;
14941 if (!htab->elf.dynamic_sections_created
14942 || h == NULL
14943 || h->elf.dynindx == -1)
14944 {
14945 if (h != NULL
14946 ? h->elf.type == STT_GNU_IFUNC
14947 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14948 plt = htab->elf.iplt;
14949 else
14950 plt = htab->pltlocal;
14951 }
14952 relocation = (plt->output_section->vma
14953 + plt->output_offset
14954 + ent->plt.offset);
14955 if (r_type == R_PPC64_PLT16_HA
14956 || r_type ==R_PPC64_PLT16_HI
14957 || r_type ==R_PPC64_PLT16_LO
14958 || r_type ==R_PPC64_PLT16_LO_DS)
14959 {
14960 got = (elf_gp (output_bfd)
14961 + htab->sec_info[input_section->id].toc_off);
14962 relocation -= got;
14963 }
14964 addend = 0;
14965 unresolved_reloc = FALSE;
14966 break;
14967 }
14968 }
14969 }
14970 break;
14971
14972 case R_PPC64_TOC:
14973 /* Relocation value is TOC base. */
14974 relocation = TOCstart;
14975 if (r_symndx == STN_UNDEF)
14976 relocation += htab->sec_info[input_section->id].toc_off;
14977 else if (unresolved_reloc)
14978 ;
14979 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14980 relocation += htab->sec_info[sec->id].toc_off;
14981 else
14982 unresolved_reloc = TRUE;
14983 goto dodyn;
14984
14985 /* TOC16 relocs. We want the offset relative to the TOC base,
14986 which is the address of the start of the TOC plus 0x8000.
14987 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14988 in this order. */
14989 case R_PPC64_TOC16:
14990 case R_PPC64_TOC16_LO:
14991 case R_PPC64_TOC16_HI:
14992 case R_PPC64_TOC16_DS:
14993 case R_PPC64_TOC16_LO_DS:
14994 case R_PPC64_TOC16_HA:
14995 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14996 break;
14997
14998 /* Relocate against the beginning of the section. */
14999 case R_PPC64_SECTOFF:
15000 case R_PPC64_SECTOFF_LO:
15001 case R_PPC64_SECTOFF_HI:
15002 case R_PPC64_SECTOFF_DS:
15003 case R_PPC64_SECTOFF_LO_DS:
15004 case R_PPC64_SECTOFF_HA:
15005 if (sec != NULL)
15006 addend -= sec->output_section->vma;
15007 break;
15008
15009 case R_PPC64_REL16:
15010 case R_PPC64_REL16_LO:
15011 case R_PPC64_REL16_HI:
15012 case R_PPC64_REL16_HA:
15013 case R_PPC64_REL16DX_HA:
15014 break;
15015
15016 case R_PPC64_REL14:
15017 case R_PPC64_REL14_BRNTAKEN:
15018 case R_PPC64_REL14_BRTAKEN:
15019 case R_PPC64_REL24:
15020 break;
15021
15022 case R_PPC64_TPREL16:
15023 case R_PPC64_TPREL16_LO:
15024 case R_PPC64_TPREL16_HI:
15025 case R_PPC64_TPREL16_HA:
15026 case R_PPC64_TPREL16_DS:
15027 case R_PPC64_TPREL16_LO_DS:
15028 case R_PPC64_TPREL16_HIGH:
15029 case R_PPC64_TPREL16_HIGHA:
15030 case R_PPC64_TPREL16_HIGHER:
15031 case R_PPC64_TPREL16_HIGHERA:
15032 case R_PPC64_TPREL16_HIGHEST:
15033 case R_PPC64_TPREL16_HIGHESTA:
15034 if (h != NULL
15035 && h->elf.root.type == bfd_link_hash_undefweak
15036 && h->elf.dynindx == -1)
15037 {
15038 /* Make this relocation against an undefined weak symbol
15039 resolve to zero. This is really just a tweak, since
15040 code using weak externs ought to check that they are
15041 defined before using them. */
15042 bfd_byte *p = contents + rel->r_offset - d_offset;
15043
15044 insn = bfd_get_32 (input_bfd, p);
15045 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15046 if (insn != 0)
15047 bfd_put_32 (input_bfd, insn, p);
15048 break;
15049 }
15050 if (htab->elf.tls_sec != NULL)
15051 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15052 /* The TPREL16 relocs shouldn't really be used in shared
15053 libs or with non-local symbols as that will result in
15054 DT_TEXTREL being set, but support them anyway. */
15055 goto dodyn;
15056
15057 case R_PPC64_DTPREL16:
15058 case R_PPC64_DTPREL16_LO:
15059 case R_PPC64_DTPREL16_HI:
15060 case R_PPC64_DTPREL16_HA:
15061 case R_PPC64_DTPREL16_DS:
15062 case R_PPC64_DTPREL16_LO_DS:
15063 case R_PPC64_DTPREL16_HIGH:
15064 case R_PPC64_DTPREL16_HIGHA:
15065 case R_PPC64_DTPREL16_HIGHER:
15066 case R_PPC64_DTPREL16_HIGHERA:
15067 case R_PPC64_DTPREL16_HIGHEST:
15068 case R_PPC64_DTPREL16_HIGHESTA:
15069 if (htab->elf.tls_sec != NULL)
15070 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15071 break;
15072
15073 case R_PPC64_ADDR64_LOCAL:
15074 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15075 ? h->elf.other
15076 : sym->st_other);
15077 break;
15078
15079 case R_PPC64_DTPMOD64:
15080 relocation = 1;
15081 addend = 0;
15082 goto dodyn;
15083
15084 case R_PPC64_TPREL64:
15085 if (htab->elf.tls_sec != NULL)
15086 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15087 goto dodyn;
15088
15089 case R_PPC64_DTPREL64:
15090 if (htab->elf.tls_sec != NULL)
15091 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15092 /* Fall through. */
15093
15094 /* Relocations that may need to be propagated if this is a
15095 dynamic object. */
15096 case R_PPC64_REL30:
15097 case R_PPC64_REL32:
15098 case R_PPC64_REL64:
15099 case R_PPC64_ADDR14:
15100 case R_PPC64_ADDR14_BRNTAKEN:
15101 case R_PPC64_ADDR14_BRTAKEN:
15102 case R_PPC64_ADDR16:
15103 case R_PPC64_ADDR16_DS:
15104 case R_PPC64_ADDR16_HA:
15105 case R_PPC64_ADDR16_HI:
15106 case R_PPC64_ADDR16_HIGH:
15107 case R_PPC64_ADDR16_HIGHA:
15108 case R_PPC64_ADDR16_HIGHER:
15109 case R_PPC64_ADDR16_HIGHERA:
15110 case R_PPC64_ADDR16_HIGHEST:
15111 case R_PPC64_ADDR16_HIGHESTA:
15112 case R_PPC64_ADDR16_LO:
15113 case R_PPC64_ADDR16_LO_DS:
15114 case R_PPC64_ADDR24:
15115 case R_PPC64_ADDR32:
15116 case R_PPC64_ADDR64:
15117 case R_PPC64_UADDR16:
15118 case R_PPC64_UADDR32:
15119 case R_PPC64_UADDR64:
15120 dodyn:
15121 if ((input_section->flags & SEC_ALLOC) == 0)
15122 break;
15123
15124 if (NO_OPD_RELOCS && is_opd)
15125 break;
15126
15127 if (bfd_link_pic (info)
15128 ? ((h == NULL
15129 || h->dyn_relocs != NULL)
15130 && ((h != NULL && pc_dynrelocs (h))
15131 || must_be_dyn_reloc (info, r_type)))
15132 : (h != NULL
15133 ? h->dyn_relocs != NULL
15134 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15135 {
15136 bfd_boolean skip, relocate;
15137 asection *sreloc;
15138 bfd_vma out_off;
15139 long indx = 0;
15140
15141 /* When generating a dynamic object, these relocations
15142 are copied into the output file to be resolved at run
15143 time. */
15144
15145 skip = FALSE;
15146 relocate = FALSE;
15147
15148 out_off = _bfd_elf_section_offset (output_bfd, info,
15149 input_section, rel->r_offset);
15150 if (out_off == (bfd_vma) -1)
15151 skip = TRUE;
15152 else if (out_off == (bfd_vma) -2)
15153 skip = TRUE, relocate = TRUE;
15154 out_off += (input_section->output_section->vma
15155 + input_section->output_offset);
15156 outrel.r_offset = out_off;
15157 outrel.r_addend = rel->r_addend;
15158
15159 /* Optimize unaligned reloc use. */
15160 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15161 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15162 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15163 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15164 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15165 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15166 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15167 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15168 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15169
15170 if (skip)
15171 memset (&outrel, 0, sizeof outrel);
15172 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15173 && !is_opd
15174 && r_type != R_PPC64_TOC)
15175 {
15176 indx = h->elf.dynindx;
15177 BFD_ASSERT (indx != -1);
15178 outrel.r_info = ELF64_R_INFO (indx, r_type);
15179 }
15180 else
15181 {
15182 /* This symbol is local, or marked to become local,
15183 or this is an opd section reloc which must point
15184 at a local function. */
15185 outrel.r_addend += relocation;
15186 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
15187 {
15188 if (is_opd && h != NULL)
15189 {
15190 /* Lie about opd entries. This case occurs
15191 when building shared libraries and we
15192 reference a function in another shared
15193 lib. The same thing happens for a weak
15194 definition in an application that's
15195 overridden by a strong definition in a
15196 shared lib. (I believe this is a generic
15197 bug in binutils handling of weak syms.)
15198 In these cases we won't use the opd
15199 entry in this lib. */
15200 unresolved_reloc = FALSE;
15201 }
15202 if (!is_opd
15203 && r_type == R_PPC64_ADDR64
15204 && (h != NULL
15205 ? h->elf.type == STT_GNU_IFUNC
15206 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15207 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15208 else
15209 {
15210 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15211
15212 /* We need to relocate .opd contents for ld.so.
15213 Prelink also wants simple and consistent rules
15214 for relocs. This make all RELATIVE relocs have
15215 *r_offset equal to r_addend. */
15216 relocate = TRUE;
15217 }
15218 }
15219 else
15220 {
15221 if (h != NULL
15222 ? h->elf.type == STT_GNU_IFUNC
15223 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15224 {
15225 info->callbacks->einfo
15226 /* xgettext:c-format */
15227 (_("%H: %s for indirect "
15228 "function `%pT' unsupported\n"),
15229 input_bfd, input_section, rel->r_offset,
15230 ppc64_elf_howto_table[r_type]->name,
15231 sym_name);
15232 ret = FALSE;
15233 }
15234 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
15235 ;
15236 else if (sec == NULL || sec->owner == NULL)
15237 {
15238 bfd_set_error (bfd_error_bad_value);
15239 return FALSE;
15240 }
15241 else
15242 {
15243 asection *osec;
15244
15245 osec = sec->output_section;
15246 indx = elf_section_data (osec)->dynindx;
15247
15248 if (indx == 0)
15249 {
15250 if ((osec->flags & SEC_READONLY) == 0
15251 && htab->elf.data_index_section != NULL)
15252 osec = htab->elf.data_index_section;
15253 else
15254 osec = htab->elf.text_index_section;
15255 indx = elf_section_data (osec)->dynindx;
15256 }
15257 BFD_ASSERT (indx != 0);
15258
15259 /* We are turning this relocation into one
15260 against a section symbol, so subtract out
15261 the output section's address but not the
15262 offset of the input section in the output
15263 section. */
15264 outrel.r_addend -= osec->vma;
15265 }
15266
15267 outrel.r_info = ELF64_R_INFO (indx, r_type);
15268 }
15269 }
15270
15271 sreloc = elf_section_data (input_section)->sreloc;
15272 if (h != NULL
15273 ? h->elf.type == STT_GNU_IFUNC
15274 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15275 {
15276 sreloc = htab->elf.irelplt;
15277 if (indx == 0)
15278 htab->local_ifunc_resolver = 1;
15279 else if (is_static_defined (&h->elf))
15280 htab->maybe_local_ifunc_resolver = 1;
15281 }
15282 if (sreloc == NULL)
15283 abort ();
15284
15285 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15286 >= sreloc->size)
15287 abort ();
15288 loc = sreloc->contents;
15289 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15290 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15291
15292 /* If this reloc is against an external symbol, it will
15293 be computed at runtime, so there's no need to do
15294 anything now. However, for the sake of prelink ensure
15295 that the section contents are a known value. */
15296 if (! relocate)
15297 {
15298 unresolved_reloc = FALSE;
15299 /* The value chosen here is quite arbitrary as ld.so
15300 ignores section contents except for the special
15301 case of .opd where the contents might be accessed
15302 before relocation. Choose zero, as that won't
15303 cause reloc overflow. */
15304 relocation = 0;
15305 addend = 0;
15306 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15307 to improve backward compatibility with older
15308 versions of ld. */
15309 if (r_type == R_PPC64_ADDR64)
15310 addend = outrel.r_addend;
15311 /* Adjust pc_relative relocs to have zero in *r_offset. */
15312 else if (ppc64_elf_howto_table[r_type]->pc_relative)
15313 addend = outrel.r_offset;
15314 }
15315 }
15316 break;
15317
15318 case R_PPC64_COPY:
15319 case R_PPC64_GLOB_DAT:
15320 case R_PPC64_JMP_SLOT:
15321 case R_PPC64_JMP_IREL:
15322 case R_PPC64_RELATIVE:
15323 /* We shouldn't ever see these dynamic relocs in relocatable
15324 files. */
15325 /* Fall through. */
15326
15327 case R_PPC64_PLTGOT16:
15328 case R_PPC64_PLTGOT16_DS:
15329 case R_PPC64_PLTGOT16_HA:
15330 case R_PPC64_PLTGOT16_HI:
15331 case R_PPC64_PLTGOT16_LO:
15332 case R_PPC64_PLTGOT16_LO_DS:
15333 case R_PPC64_PLTREL32:
15334 case R_PPC64_PLTREL64:
15335 /* These ones haven't been implemented yet. */
15336
15337 info->callbacks->einfo
15338 /* xgettext:c-format */
15339 (_("%P: %pB: %s is not supported for `%pT'\n"),
15340 input_bfd,
15341 ppc64_elf_howto_table[r_type]->name, sym_name);
15342
15343 bfd_set_error (bfd_error_invalid_operation);
15344 ret = FALSE;
15345 goto copy_reloc;
15346 }
15347
15348 /* Multi-instruction sequences that access the TOC can be
15349 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15350 to nop; addi rb,r2,x; */
15351 switch (r_type)
15352 {
15353 default:
15354 break;
15355
15356 case R_PPC64_GOT_TLSLD16_HI:
15357 case R_PPC64_GOT_TLSGD16_HI:
15358 case R_PPC64_GOT_TPREL16_HI:
15359 case R_PPC64_GOT_DTPREL16_HI:
15360 case R_PPC64_GOT16_HI:
15361 case R_PPC64_TOC16_HI:
15362 /* These relocs would only be useful if building up an
15363 offset to later add to r2, perhaps in an indexed
15364 addressing mode instruction. Don't try to optimize.
15365 Unfortunately, the possibility of someone building up an
15366 offset like this or even with the HA relocs, means that
15367 we need to check the high insn when optimizing the low
15368 insn. */
15369 break;
15370
15371 case R_PPC64_GOT_TLSLD16_HA:
15372 case R_PPC64_GOT_TLSGD16_HA:
15373 case R_PPC64_GOT_TPREL16_HA:
15374 case R_PPC64_GOT_DTPREL16_HA:
15375 case R_PPC64_GOT16_HA:
15376 case R_PPC64_TOC16_HA:
15377 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15378 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15379 {
15380 bfd_byte *p = contents + (rel->r_offset & ~3);
15381 bfd_put_32 (input_bfd, NOP, p);
15382 goto copy_reloc;
15383 }
15384 break;
15385
15386 case R_PPC64_GOT_TLSLD16_LO:
15387 case R_PPC64_GOT_TLSGD16_LO:
15388 case R_PPC64_GOT_TPREL16_LO_DS:
15389 case R_PPC64_GOT_DTPREL16_LO_DS:
15390 case R_PPC64_GOT16_LO:
15391 case R_PPC64_GOT16_LO_DS:
15392 case R_PPC64_TOC16_LO:
15393 case R_PPC64_TOC16_LO_DS:
15394 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15395 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15396 {
15397 bfd_byte *p = contents + (rel->r_offset & ~3);
15398 insn = bfd_get_32 (input_bfd, p);
15399 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15400 {
15401 /* Transform addic to addi when we change reg. */
15402 insn &= ~((0x3f << 26) | (0x1f << 16));
15403 insn |= (14u << 26) | (2 << 16);
15404 }
15405 else
15406 {
15407 insn &= ~(0x1f << 16);
15408 insn |= 2 << 16;
15409 }
15410 bfd_put_32 (input_bfd, insn, p);
15411 }
15412 break;
15413
15414 case R_PPC64_TPREL16_HA:
15415 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15416 {
15417 bfd_byte *p = contents + (rel->r_offset & ~3);
15418 insn = bfd_get_32 (input_bfd, p);
15419 if ((insn & ((0x3f << 26) | 0x1f << 16))
15420 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15421 /* xgettext:c-format */
15422 info->callbacks->minfo
15423 (_("%H: warning: %s unexpected insn %#x.\n"),
15424 input_bfd, input_section, rel->r_offset,
15425 ppc64_elf_howto_table[r_type]->name, insn);
15426 else
15427 {
15428 bfd_put_32 (input_bfd, NOP, p);
15429 goto copy_reloc;
15430 }
15431 }
15432 break;
15433
15434 case R_PPC64_TPREL16_LO:
15435 case R_PPC64_TPREL16_LO_DS:
15436 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15437 {
15438 bfd_byte *p = contents + (rel->r_offset & ~3);
15439 insn = bfd_get_32 (input_bfd, p);
15440 insn &= ~(0x1f << 16);
15441 insn |= 13 << 16;
15442 bfd_put_32 (input_bfd, insn, p);
15443 }
15444 break;
15445 }
15446
15447 /* Do any further special processing. */
15448 switch (r_type)
15449 {
15450 default:
15451 break;
15452
15453 case R_PPC64_REL16_HA:
15454 case R_PPC64_REL16DX_HA:
15455 case R_PPC64_ADDR16_HA:
15456 case R_PPC64_ADDR16_HIGHA:
15457 case R_PPC64_ADDR16_HIGHERA:
15458 case R_PPC64_ADDR16_HIGHESTA:
15459 case R_PPC64_TOC16_HA:
15460 case R_PPC64_SECTOFF_HA:
15461 case R_PPC64_TPREL16_HA:
15462 case R_PPC64_TPREL16_HIGHA:
15463 case R_PPC64_TPREL16_HIGHERA:
15464 case R_PPC64_TPREL16_HIGHESTA:
15465 case R_PPC64_DTPREL16_HA:
15466 case R_PPC64_DTPREL16_HIGHA:
15467 case R_PPC64_DTPREL16_HIGHERA:
15468 case R_PPC64_DTPREL16_HIGHESTA:
15469 /* It's just possible that this symbol is a weak symbol
15470 that's not actually defined anywhere. In that case,
15471 'sec' would be NULL, and we should leave the symbol
15472 alone (it will be set to zero elsewhere in the link). */
15473 if (sec == NULL)
15474 break;
15475 /* Fall through. */
15476
15477 case R_PPC64_GOT16_HA:
15478 case R_PPC64_PLTGOT16_HA:
15479 case R_PPC64_PLT16_HA:
15480 case R_PPC64_GOT_TLSGD16_HA:
15481 case R_PPC64_GOT_TLSLD16_HA:
15482 case R_PPC64_GOT_TPREL16_HA:
15483 case R_PPC64_GOT_DTPREL16_HA:
15484 /* Add 0x10000 if sign bit in 0:15 is set.
15485 Bits 0:15 are not used. */
15486 addend += 0x8000;
15487 break;
15488
15489 case R_PPC64_ADDR16_DS:
15490 case R_PPC64_ADDR16_LO_DS:
15491 case R_PPC64_GOT16_DS:
15492 case R_PPC64_GOT16_LO_DS:
15493 case R_PPC64_PLT16_LO_DS:
15494 case R_PPC64_SECTOFF_DS:
15495 case R_PPC64_SECTOFF_LO_DS:
15496 case R_PPC64_TOC16_DS:
15497 case R_PPC64_TOC16_LO_DS:
15498 case R_PPC64_PLTGOT16_DS:
15499 case R_PPC64_PLTGOT16_LO_DS:
15500 case R_PPC64_GOT_TPREL16_DS:
15501 case R_PPC64_GOT_TPREL16_LO_DS:
15502 case R_PPC64_GOT_DTPREL16_DS:
15503 case R_PPC64_GOT_DTPREL16_LO_DS:
15504 case R_PPC64_TPREL16_DS:
15505 case R_PPC64_TPREL16_LO_DS:
15506 case R_PPC64_DTPREL16_DS:
15507 case R_PPC64_DTPREL16_LO_DS:
15508 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15509 mask = 3;
15510 /* If this reloc is against an lq, lxv, or stxv insn, then
15511 the value must be a multiple of 16. This is somewhat of
15512 a hack, but the "correct" way to do this by defining _DQ
15513 forms of all the _DS relocs bloats all reloc switches in
15514 this file. It doesn't make much sense to use these
15515 relocs in data, so testing the insn should be safe. */
15516 if ((insn & (0x3f << 26)) == (56u << 26)
15517 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15518 mask = 15;
15519 relocation += addend;
15520 addend = insn & (mask ^ 3);
15521 if ((relocation & mask) != 0)
15522 {
15523 relocation ^= relocation & mask;
15524 info->callbacks->einfo
15525 /* xgettext:c-format */
15526 (_("%H: error: %s not a multiple of %u\n"),
15527 input_bfd, input_section, rel->r_offset,
15528 ppc64_elf_howto_table[r_type]->name,
15529 mask + 1);
15530 bfd_set_error (bfd_error_bad_value);
15531 ret = FALSE;
15532 goto copy_reloc;
15533 }
15534 break;
15535 }
15536
15537 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15538 because such sections are not SEC_ALLOC and thus ld.so will
15539 not process them. */
15540 howto = ppc64_elf_howto_table[(int) r_type];
15541 if (unresolved_reloc
15542 && !((input_section->flags & SEC_DEBUGGING) != 0
15543 && h->elf.def_dynamic)
15544 && _bfd_elf_section_offset (output_bfd, info, input_section,
15545 rel->r_offset) != (bfd_vma) -1)
15546 {
15547 info->callbacks->einfo
15548 /* xgettext:c-format */
15549 (_("%H: unresolvable %s against `%pT'\n"),
15550 input_bfd, input_section, rel->r_offset,
15551 howto->name,
15552 h->elf.root.root.string);
15553 ret = FALSE;
15554 }
15555
15556 /* 16-bit fields in insns mostly have signed values, but a
15557 few insns have 16-bit unsigned values. Really, we should
15558 have different reloc types. */
15559 if (howto->complain_on_overflow != complain_overflow_dont
15560 && howto->dst_mask == 0xffff
15561 && (input_section->flags & SEC_CODE) != 0)
15562 {
15563 enum complain_overflow complain = complain_overflow_signed;
15564
15565 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15566 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15567 complain = complain_overflow_bitfield;
15568 else if (howto->rightshift == 0
15569 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15570 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15571 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15572 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15573 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15574 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15575 complain = complain_overflow_unsigned;
15576 if (howto->complain_on_overflow != complain)
15577 {
15578 alt_howto = *howto;
15579 alt_howto.complain_on_overflow = complain;
15580 howto = &alt_howto;
15581 }
15582 }
15583
15584 if (r_type == R_PPC64_REL16DX_HA)
15585 {
15586 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15587 if (rel->r_offset + 4 > input_section->size)
15588 r = bfd_reloc_outofrange;
15589 else
15590 {
15591 relocation += addend;
15592 relocation -= (rel->r_offset
15593 + input_section->output_offset
15594 + input_section->output_section->vma);
15595 relocation = (bfd_signed_vma) relocation >> 16;
15596 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15597 insn &= ~0x1fffc1;
15598 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15599 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15600 r = bfd_reloc_ok;
15601 if (relocation + 0x8000 > 0xffff)
15602 r = bfd_reloc_overflow;
15603 }
15604 }
15605 else
15606 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15607 rel->r_offset, relocation, addend);
15608
15609 if (r != bfd_reloc_ok)
15610 {
15611 char *more_info = NULL;
15612 const char *reloc_name = howto->name;
15613
15614 if (reloc_dest != DEST_NORMAL)
15615 {
15616 more_info = bfd_malloc (strlen (reloc_name) + 8);
15617 if (more_info != NULL)
15618 {
15619 strcpy (more_info, reloc_name);
15620 strcat (more_info, (reloc_dest == DEST_OPD
15621 ? " (OPD)" : " (stub)"));
15622 reloc_name = more_info;
15623 }
15624 }
15625
15626 if (r == bfd_reloc_overflow)
15627 {
15628 /* On code like "if (foo) foo();" don't report overflow
15629 on a branch to zero when foo is undefined. */
15630 if (!warned
15631 && (reloc_dest == DEST_STUB
15632 || !(h != NULL
15633 && (h->elf.root.type == bfd_link_hash_undefweak
15634 || h->elf.root.type == bfd_link_hash_undefined)
15635 && is_branch_reloc (r_type))))
15636 info->callbacks->reloc_overflow (info, &h->elf.root,
15637 sym_name, reloc_name,
15638 orig_rel.r_addend,
15639 input_bfd, input_section,
15640 rel->r_offset);
15641 }
15642 else
15643 {
15644 info->callbacks->einfo
15645 /* xgettext:c-format */
15646 (_("%H: %s against `%pT': error %d\n"),
15647 input_bfd, input_section, rel->r_offset,
15648 reloc_name, sym_name, (int) r);
15649 ret = FALSE;
15650 }
15651 if (more_info != NULL)
15652 free (more_info);
15653 }
15654 copy_reloc:
15655 if (wrel != rel)
15656 *wrel = *rel;
15657 }
15658
15659 if (wrel != rel)
15660 {
15661 Elf_Internal_Shdr *rel_hdr;
15662 size_t deleted = rel - wrel;
15663
15664 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15665 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15666 if (rel_hdr->sh_size == 0)
15667 {
15668 /* It is too late to remove an empty reloc section. Leave
15669 one NONE reloc.
15670 ??? What is wrong with an empty section??? */
15671 rel_hdr->sh_size = rel_hdr->sh_entsize;
15672 deleted -= 1;
15673 }
15674 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15675 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15676 input_section->reloc_count -= deleted;
15677 }
15678
15679 /* If we're emitting relocations, then shortly after this function
15680 returns, reloc offsets and addends for this section will be
15681 adjusted. Worse, reloc symbol indices will be for the output
15682 file rather than the input. Save a copy of the relocs for
15683 opd_entry_value. */
15684 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15685 {
15686 bfd_size_type amt;
15687 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15688 rel = bfd_alloc (input_bfd, amt);
15689 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15690 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15691 if (rel == NULL)
15692 return FALSE;
15693 memcpy (rel, relocs, amt);
15694 }
15695 return ret;
15696 }
15697
15698 /* Adjust the value of any local symbols in opd sections. */
15699
15700 static int
15701 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15702 const char *name ATTRIBUTE_UNUSED,
15703 Elf_Internal_Sym *elfsym,
15704 asection *input_sec,
15705 struct elf_link_hash_entry *h)
15706 {
15707 struct _opd_sec_data *opd;
15708 long adjust;
15709 bfd_vma value;
15710
15711 if (h != NULL)
15712 return 1;
15713
15714 opd = get_opd_info (input_sec);
15715 if (opd == NULL || opd->adjust == NULL)
15716 return 1;
15717
15718 value = elfsym->st_value - input_sec->output_offset;
15719 if (!bfd_link_relocatable (info))
15720 value -= input_sec->output_section->vma;
15721
15722 adjust = opd->adjust[OPD_NDX (value)];
15723 if (adjust == -1)
15724 return 2;
15725
15726 elfsym->st_value += adjust;
15727 return 1;
15728 }
15729
15730 /* Finish up dynamic symbol handling. We set the contents of various
15731 dynamic sections here. */
15732
15733 static bfd_boolean
15734 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15735 struct bfd_link_info *info,
15736 struct elf_link_hash_entry *h,
15737 Elf_Internal_Sym *sym)
15738 {
15739 struct ppc_link_hash_table *htab;
15740 struct plt_entry *ent;
15741
15742 htab = ppc_hash_table (info);
15743 if (htab == NULL)
15744 return FALSE;
15745
15746 if (!htab->opd_abi && !h->def_regular)
15747 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15748 if (ent->plt.offset != (bfd_vma) -1)
15749 {
15750 /* Mark the symbol as undefined, rather than as
15751 defined in glink. Leave the value if there were
15752 any relocations where pointer equality matters
15753 (this is a clue for the dynamic linker, to make
15754 function pointer comparisons work between an
15755 application and shared library), otherwise set it
15756 to zero. */
15757 sym->st_shndx = SHN_UNDEF;
15758 if (!h->pointer_equality_needed)
15759 sym->st_value = 0;
15760 else if (!h->ref_regular_nonweak)
15761 {
15762 /* This breaks function pointer comparisons, but
15763 that is better than breaking tests for a NULL
15764 function pointer. */
15765 sym->st_value = 0;
15766 }
15767 break;
15768 }
15769
15770 if (h->needs_copy)
15771 {
15772 /* This symbol needs a copy reloc. Set it up. */
15773 Elf_Internal_Rela rela;
15774 asection *srel;
15775 bfd_byte *loc;
15776
15777 if (h->dynindx == -1
15778 || (h->root.type != bfd_link_hash_defined
15779 && h->root.type != bfd_link_hash_defweak)
15780 || htab->elf.srelbss == NULL
15781 || htab->elf.sreldynrelro == NULL)
15782 abort ();
15783
15784 rela.r_offset = (h->root.u.def.value
15785 + h->root.u.def.section->output_section->vma
15786 + h->root.u.def.section->output_offset);
15787 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15788 rela.r_addend = 0;
15789 if (h->root.u.def.section == htab->elf.sdynrelro)
15790 srel = htab->elf.sreldynrelro;
15791 else
15792 srel = htab->elf.srelbss;
15793 loc = srel->contents;
15794 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
15795 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15796 }
15797
15798 return TRUE;
15799 }
15800
15801 /* Used to decide how to sort relocs in an optimal manner for the
15802 dynamic linker, before writing them out. */
15803
15804 static enum elf_reloc_type_class
15805 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15806 const asection *rel_sec,
15807 const Elf_Internal_Rela *rela)
15808 {
15809 enum elf_ppc64_reloc_type r_type;
15810 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15811
15812 if (rel_sec == htab->elf.irelplt)
15813 return reloc_class_ifunc;
15814
15815 r_type = ELF64_R_TYPE (rela->r_info);
15816 switch (r_type)
15817 {
15818 case R_PPC64_RELATIVE:
15819 return reloc_class_relative;
15820 case R_PPC64_JMP_SLOT:
15821 return reloc_class_plt;
15822 case R_PPC64_COPY:
15823 return reloc_class_copy;
15824 default:
15825 return reloc_class_normal;
15826 }
15827 }
15828
15829 /* Finish up the dynamic sections. */
15830
15831 static bfd_boolean
15832 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15833 struct bfd_link_info *info)
15834 {
15835 struct ppc_link_hash_table *htab;
15836 bfd *dynobj;
15837 asection *sdyn;
15838
15839 htab = ppc_hash_table (info);
15840 if (htab == NULL)
15841 return FALSE;
15842
15843 dynobj = htab->elf.dynobj;
15844 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15845
15846 if (htab->elf.dynamic_sections_created)
15847 {
15848 Elf64_External_Dyn *dyncon, *dynconend;
15849
15850 if (sdyn == NULL || htab->elf.sgot == NULL)
15851 abort ();
15852
15853 dyncon = (Elf64_External_Dyn *) sdyn->contents;
15854 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15855 for (; dyncon < dynconend; dyncon++)
15856 {
15857 Elf_Internal_Dyn dyn;
15858 asection *s;
15859
15860 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15861
15862 switch (dyn.d_tag)
15863 {
15864 default:
15865 continue;
15866
15867 case DT_PPC64_GLINK:
15868 s = htab->glink;
15869 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15870 /* We stupidly defined DT_PPC64_GLINK to be the start
15871 of glink rather than the first entry point, which is
15872 what ld.so needs, and now have a bigger stub to
15873 support automatic multiple TOCs. */
15874 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
15875 break;
15876
15877 case DT_PPC64_OPD:
15878 s = bfd_get_section_by_name (output_bfd, ".opd");
15879 if (s == NULL)
15880 continue;
15881 dyn.d_un.d_ptr = s->vma;
15882 break;
15883
15884 case DT_PPC64_OPT:
15885 if (htab->do_multi_toc && htab->multi_toc_needed)
15886 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15887 if (htab->has_plt_localentry0)
15888 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
15889 break;
15890
15891 case DT_PPC64_OPDSZ:
15892 s = bfd_get_section_by_name (output_bfd, ".opd");
15893 if (s == NULL)
15894 continue;
15895 dyn.d_un.d_val = s->size;
15896 break;
15897
15898 case DT_PLTGOT:
15899 s = htab->elf.splt;
15900 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15901 break;
15902
15903 case DT_JMPREL:
15904 s = htab->elf.srelplt;
15905 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15906 break;
15907
15908 case DT_PLTRELSZ:
15909 dyn.d_un.d_val = htab->elf.srelplt->size;
15910 break;
15911
15912 case DT_TEXTREL:
15913 if (htab->local_ifunc_resolver)
15914 info->callbacks->einfo
15915 (_("%X%P: text relocations and GNU indirect "
15916 "functions will result in a segfault at runtime\n"));
15917 else if (htab->maybe_local_ifunc_resolver)
15918 info->callbacks->einfo
15919 (_("%P: warning: text relocations and GNU indirect "
15920 "functions may result in a segfault at runtime\n"));
15921 continue;
15922 }
15923
15924 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15925 }
15926 }
15927
15928 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15929 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
15930 {
15931 /* Fill in the first entry in the global offset table.
15932 We use it to hold the link-time TOCbase. */
15933 bfd_put_64 (output_bfd,
15934 elf_gp (output_bfd) + TOC_BASE_OFF,
15935 htab->elf.sgot->contents);
15936
15937 /* Set .got entry size. */
15938 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
15939 }
15940
15941 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15942 && htab->elf.splt->output_section != bfd_abs_section_ptr)
15943 {
15944 /* Set .plt entry size. */
15945 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15946 = PLT_ENTRY_SIZE (htab);
15947 }
15948
15949 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15950 brlt ourselves if emitrelocations. */
15951 if (htab->brlt != NULL
15952 && htab->brlt->reloc_count != 0
15953 && !_bfd_elf_link_output_relocs (output_bfd,
15954 htab->brlt,
15955 elf_section_data (htab->brlt)->rela.hdr,
15956 elf_section_data (htab->brlt)->relocs,
15957 NULL))
15958 return FALSE;
15959
15960 if (htab->glink != NULL
15961 && htab->glink->reloc_count != 0
15962 && !_bfd_elf_link_output_relocs (output_bfd,
15963 htab->glink,
15964 elf_section_data (htab->glink)->rela.hdr,
15965 elf_section_data (htab->glink)->relocs,
15966 NULL))
15967 return FALSE;
15968
15969 if (htab->glink_eh_frame != NULL
15970 && htab->glink_eh_frame->size != 0)
15971 {
15972 bfd_vma val;
15973 bfd_byte *p;
15974 struct map_stub *group;
15975 size_t align = 4;
15976
15977 p = htab->glink_eh_frame->contents;
15978 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15979
15980 for (group = htab->group; group != NULL; group = group->next)
15981 if (group->stub_sec != NULL)
15982 {
15983 /* Offset to stub section. */
15984 val = (group->stub_sec->output_section->vma
15985 + group->stub_sec->output_offset);
15986 val -= (htab->glink_eh_frame->output_section->vma
15987 + htab->glink_eh_frame->output_offset
15988 + (p + 8 - htab->glink_eh_frame->contents));
15989 if (val + 0x80000000 > 0xffffffff)
15990 {
15991 _bfd_error_handler
15992 (_("%s offset too large for .eh_frame sdata4 encoding"),
15993 group->stub_sec->name);
15994 return FALSE;
15995 }
15996 bfd_put_32 (dynobj, val, p + 8);
15997 p += stub_eh_frame_size (group, align);
15998 }
15999 if (htab->glink != NULL && htab->glink->size != 0)
16000 {
16001 /* Offset to .glink. */
16002 val = (htab->glink->output_section->vma
16003 + htab->glink->output_offset
16004 + 8);
16005 val -= (htab->glink_eh_frame->output_section->vma
16006 + htab->glink_eh_frame->output_offset
16007 + (p + 8 - htab->glink_eh_frame->contents));
16008 if (val + 0x80000000 > 0xffffffff)
16009 {
16010 _bfd_error_handler
16011 (_("%s offset too large for .eh_frame sdata4 encoding"),
16012 htab->glink->name);
16013 return FALSE;
16014 }
16015 bfd_put_32 (dynobj, val, p + 8);
16016 p += (24 + align - 1) & -align;
16017 }
16018
16019 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16020 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16021 htab->glink_eh_frame,
16022 htab->glink_eh_frame->contents))
16023 return FALSE;
16024 }
16025
16026 /* We need to handle writing out multiple GOT sections ourselves,
16027 since we didn't add them to DYNOBJ. We know dynobj is the first
16028 bfd. */
16029 while ((dynobj = dynobj->link.next) != NULL)
16030 {
16031 asection *s;
16032
16033 if (!is_ppc64_elf (dynobj))
16034 continue;
16035
16036 s = ppc64_elf_tdata (dynobj)->got;
16037 if (s != NULL
16038 && s->size != 0
16039 && s->output_section != bfd_abs_section_ptr
16040 && !bfd_set_section_contents (output_bfd, s->output_section,
16041 s->contents, s->output_offset,
16042 s->size))
16043 return FALSE;
16044 s = ppc64_elf_tdata (dynobj)->relgot;
16045 if (s != NULL
16046 && s->size != 0
16047 && s->output_section != bfd_abs_section_ptr
16048 && !bfd_set_section_contents (output_bfd, s->output_section,
16049 s->contents, s->output_offset,
16050 s->size))
16051 return FALSE;
16052 }
16053
16054 return TRUE;
16055 }
16056
16057 #include "elf64-target.h"
16058
16059 /* FreeBSD support */
16060
16061 #undef TARGET_LITTLE_SYM
16062 #undef TARGET_LITTLE_NAME
16063
16064 #undef TARGET_BIG_SYM
16065 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
16066 #undef TARGET_BIG_NAME
16067 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16068
16069 #undef ELF_OSABI
16070 #define ELF_OSABI ELFOSABI_FREEBSD
16071
16072 #undef elf64_bed
16073 #define elf64_bed elf64_powerpc_fbsd_bed
16074
16075 #include "elf64-target.h"