10f895d73bd5cfc9b0d53307e1d3923338cca716
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2017 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_gc_keep ppc64_elf_gc_keep
105 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
106 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
107 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
108 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
109 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
110 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
111 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
112 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
113 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
114 #define elf_backend_action_discarded ppc64_elf_action_discarded
115 #define elf_backend_relocate_section ppc64_elf_relocate_section
116 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
117 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
118 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
119 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
120 #define elf_backend_special_sections ppc64_elf_special_sections
121 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
122 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
123 #define elf_backend_get_reloc_section bfd_get_section_by_name
124
125 /* The name of the dynamic interpreter. This is put in the .interp
126 section. */
127 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
128
129 /* The size in bytes of an entry in the procedure linkage table. */
130 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
131
132 /* The initial size of the plt reserved for the dynamic linker. */
133 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
134
135 /* Offsets to some stack save slots. */
136 #define STK_LR 16
137 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
138 /* This one is dodgy. ELFv2 does not have a linker word, so use the
139 CR save slot. Used only by optimised __tls_get_addr call stub,
140 relying on __tls_get_addr_opt not saving CR.. */
141 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
142
143 /* TOC base pointers offset from start of TOC. */
144 #define TOC_BASE_OFF 0x8000
145 /* TOC base alignment. */
146 #define TOC_BASE_ALIGN 256
147
148 /* Offset of tp and dtp pointers from start of TLS block. */
149 #define TP_OFFSET 0x7000
150 #define DTP_OFFSET 0x8000
151
152 /* .plt call stub instructions. The normal stub is like this, but
153 sometimes the .plt entry crosses a 64k boundary and we need to
154 insert an addi to adjust r11. */
155 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
156 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
157 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
158 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
159 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
160 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
161 #define BCTR 0x4e800420 /* bctr */
162
163 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
164 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
165 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
166
167 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
168 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
169 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
170 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
171 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
172 #define BNECTR 0x4ca20420 /* bnectr+ */
173 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
174
175 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
176 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
177 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
178
179 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
180 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
181 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
182
183 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
184 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
185 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
186 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
187 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
188
189 /* glink call stub instructions. We enter with the index in R0. */
190 #define GLINK_CALL_STUB_SIZE (16*4)
191 /* 0: */
192 /* .quad plt0-1f */
193 /* __glink: */
194 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
195 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
196 /* 1: */
197 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
198 /* ld %2,(0b-1b)(%11) */
199 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
200 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
201 /* ld %12,0(%11) */
202 /* ld %2,8(%11) */
203 /* mtctr %12 */
204 /* ld %11,16(%11) */
205 /* bctr */
206 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
207 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
208 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
209 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
210 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
211
212 /* Pad with this. */
213 #define NOP 0x60000000
214
215 /* Some other nops. */
216 #define CROR_151515 0x4def7b82
217 #define CROR_313131 0x4ffffb82
218
219 /* .glink entries for the first 32k functions are two instructions. */
220 #define LI_R0_0 0x38000000 /* li %r0,0 */
221 #define B_DOT 0x48000000 /* b . */
222
223 /* After that, we need two instructions to load the index, followed by
224 a branch. */
225 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
226 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
227
228 /* Instructions used by the save and restore reg functions. */
229 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
230 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
231 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
232 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
233 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
234 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
235 #define LI_R12_0 0x39800000 /* li %r12,0 */
236 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
237 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
238 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
239 #define BLR 0x4e800020 /* blr */
240
241 /* Since .opd is an array of descriptors and each entry will end up
242 with identical R_PPC64_RELATIVE relocs, there is really no need to
243 propagate .opd relocs; The dynamic linker should be taught to
244 relocate .opd without reloc entries. */
245 #ifndef NO_OPD_RELOCS
246 #define NO_OPD_RELOCS 0
247 #endif
248
249 #ifndef ARRAY_SIZE
250 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
251 #endif
252
253 static inline int
254 abiversion (bfd *abfd)
255 {
256 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
257 }
258
259 static inline void
260 set_abiversion (bfd *abfd, int ver)
261 {
262 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
263 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
264 }
265 \f
266 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
267
268 /* Relocation HOWTO's. */
269 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
270
271 static reloc_howto_type ppc64_elf_howto_raw[] = {
272 /* This reloc does nothing. */
273 HOWTO (R_PPC64_NONE, /* type */
274 0, /* rightshift */
275 3, /* size (0 = byte, 1 = short, 2 = long) */
276 0, /* bitsize */
277 FALSE, /* pc_relative */
278 0, /* bitpos */
279 complain_overflow_dont, /* complain_on_overflow */
280 bfd_elf_generic_reloc, /* special_function */
281 "R_PPC64_NONE", /* name */
282 FALSE, /* partial_inplace */
283 0, /* src_mask */
284 0, /* dst_mask */
285 FALSE), /* pcrel_offset */
286
287 /* A standard 32 bit relocation. */
288 HOWTO (R_PPC64_ADDR32, /* type */
289 0, /* rightshift */
290 2, /* size (0 = byte, 1 = short, 2 = long) */
291 32, /* bitsize */
292 FALSE, /* pc_relative */
293 0, /* bitpos */
294 complain_overflow_bitfield, /* complain_on_overflow */
295 bfd_elf_generic_reloc, /* special_function */
296 "R_PPC64_ADDR32", /* name */
297 FALSE, /* partial_inplace */
298 0, /* src_mask */
299 0xffffffff, /* dst_mask */
300 FALSE), /* pcrel_offset */
301
302 /* An absolute 26 bit branch; the lower two bits must be zero.
303 FIXME: we don't check that, we just clear them. */
304 HOWTO (R_PPC64_ADDR24, /* type */
305 0, /* rightshift */
306 2, /* size (0 = byte, 1 = short, 2 = long) */
307 26, /* bitsize */
308 FALSE, /* pc_relative */
309 0, /* bitpos */
310 complain_overflow_bitfield, /* complain_on_overflow */
311 bfd_elf_generic_reloc, /* special_function */
312 "R_PPC64_ADDR24", /* name */
313 FALSE, /* partial_inplace */
314 0, /* src_mask */
315 0x03fffffc, /* dst_mask */
316 FALSE), /* pcrel_offset */
317
318 /* A standard 16 bit relocation. */
319 HOWTO (R_PPC64_ADDR16, /* type */
320 0, /* rightshift */
321 1, /* size (0 = byte, 1 = short, 2 = long) */
322 16, /* bitsize */
323 FALSE, /* pc_relative */
324 0, /* bitpos */
325 complain_overflow_bitfield, /* complain_on_overflow */
326 bfd_elf_generic_reloc, /* special_function */
327 "R_PPC64_ADDR16", /* name */
328 FALSE, /* partial_inplace */
329 0, /* src_mask */
330 0xffff, /* dst_mask */
331 FALSE), /* pcrel_offset */
332
333 /* A 16 bit relocation without overflow. */
334 HOWTO (R_PPC64_ADDR16_LO, /* type */
335 0, /* rightshift */
336 1, /* size (0 = byte, 1 = short, 2 = long) */
337 16, /* bitsize */
338 FALSE, /* pc_relative */
339 0, /* bitpos */
340 complain_overflow_dont,/* complain_on_overflow */
341 bfd_elf_generic_reloc, /* special_function */
342 "R_PPC64_ADDR16_LO", /* name */
343 FALSE, /* partial_inplace */
344 0, /* src_mask */
345 0xffff, /* dst_mask */
346 FALSE), /* pcrel_offset */
347
348 /* Bits 16-31 of an address. */
349 HOWTO (R_PPC64_ADDR16_HI, /* type */
350 16, /* rightshift */
351 1, /* size (0 = byte, 1 = short, 2 = long) */
352 16, /* bitsize */
353 FALSE, /* pc_relative */
354 0, /* bitpos */
355 complain_overflow_signed, /* complain_on_overflow */
356 bfd_elf_generic_reloc, /* special_function */
357 "R_PPC64_ADDR16_HI", /* name */
358 FALSE, /* partial_inplace */
359 0, /* src_mask */
360 0xffff, /* dst_mask */
361 FALSE), /* pcrel_offset */
362
363 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
364 bits, treated as a signed number, is negative. */
365 HOWTO (R_PPC64_ADDR16_HA, /* type */
366 16, /* rightshift */
367 1, /* size (0 = byte, 1 = short, 2 = long) */
368 16, /* bitsize */
369 FALSE, /* pc_relative */
370 0, /* bitpos */
371 complain_overflow_signed, /* complain_on_overflow */
372 ppc64_elf_ha_reloc, /* special_function */
373 "R_PPC64_ADDR16_HA", /* name */
374 FALSE, /* partial_inplace */
375 0, /* src_mask */
376 0xffff, /* dst_mask */
377 FALSE), /* pcrel_offset */
378
379 /* An absolute 16 bit branch; the lower two bits must be zero.
380 FIXME: we don't check that, we just clear them. */
381 HOWTO (R_PPC64_ADDR14, /* type */
382 0, /* rightshift */
383 2, /* size (0 = byte, 1 = short, 2 = long) */
384 16, /* bitsize */
385 FALSE, /* pc_relative */
386 0, /* bitpos */
387 complain_overflow_signed, /* complain_on_overflow */
388 ppc64_elf_branch_reloc, /* special_function */
389 "R_PPC64_ADDR14", /* name */
390 FALSE, /* partial_inplace */
391 0, /* src_mask */
392 0x0000fffc, /* dst_mask */
393 FALSE), /* pcrel_offset */
394
395 /* An absolute 16 bit branch, for which bit 10 should be set to
396 indicate that the branch is expected to be taken. The lower two
397 bits must be zero. */
398 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
399 0, /* rightshift */
400 2, /* size (0 = byte, 1 = short, 2 = long) */
401 16, /* bitsize */
402 FALSE, /* pc_relative */
403 0, /* bitpos */
404 complain_overflow_signed, /* complain_on_overflow */
405 ppc64_elf_brtaken_reloc, /* special_function */
406 "R_PPC64_ADDR14_BRTAKEN",/* name */
407 FALSE, /* partial_inplace */
408 0, /* src_mask */
409 0x0000fffc, /* dst_mask */
410 FALSE), /* pcrel_offset */
411
412 /* An absolute 16 bit branch, for which bit 10 should be set to
413 indicate that the branch is not expected to be taken. The lower
414 two bits must be zero. */
415 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
416 0, /* rightshift */
417 2, /* size (0 = byte, 1 = short, 2 = long) */
418 16, /* bitsize */
419 FALSE, /* pc_relative */
420 0, /* bitpos */
421 complain_overflow_signed, /* complain_on_overflow */
422 ppc64_elf_brtaken_reloc, /* special_function */
423 "R_PPC64_ADDR14_BRNTAKEN",/* name */
424 FALSE, /* partial_inplace */
425 0, /* src_mask */
426 0x0000fffc, /* dst_mask */
427 FALSE), /* pcrel_offset */
428
429 /* A relative 26 bit branch; the lower two bits must be zero. */
430 HOWTO (R_PPC64_REL24, /* type */
431 0, /* rightshift */
432 2, /* size (0 = byte, 1 = short, 2 = long) */
433 26, /* bitsize */
434 TRUE, /* pc_relative */
435 0, /* bitpos */
436 complain_overflow_signed, /* complain_on_overflow */
437 ppc64_elf_branch_reloc, /* special_function */
438 "R_PPC64_REL24", /* name */
439 FALSE, /* partial_inplace */
440 0, /* src_mask */
441 0x03fffffc, /* dst_mask */
442 TRUE), /* pcrel_offset */
443
444 /* A relative 16 bit branch; the lower two bits must be zero. */
445 HOWTO (R_PPC64_REL14, /* type */
446 0, /* rightshift */
447 2, /* size (0 = byte, 1 = short, 2 = long) */
448 16, /* bitsize */
449 TRUE, /* pc_relative */
450 0, /* bitpos */
451 complain_overflow_signed, /* complain_on_overflow */
452 ppc64_elf_branch_reloc, /* special_function */
453 "R_PPC64_REL14", /* name */
454 FALSE, /* partial_inplace */
455 0, /* src_mask */
456 0x0000fffc, /* dst_mask */
457 TRUE), /* pcrel_offset */
458
459 /* A relative 16 bit branch. Bit 10 should be set to indicate that
460 the branch is expected to be taken. The lower two bits must be
461 zero. */
462 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
463 0, /* rightshift */
464 2, /* size (0 = byte, 1 = short, 2 = long) */
465 16, /* bitsize */
466 TRUE, /* pc_relative */
467 0, /* bitpos */
468 complain_overflow_signed, /* complain_on_overflow */
469 ppc64_elf_brtaken_reloc, /* special_function */
470 "R_PPC64_REL14_BRTAKEN", /* name */
471 FALSE, /* partial_inplace */
472 0, /* src_mask */
473 0x0000fffc, /* dst_mask */
474 TRUE), /* pcrel_offset */
475
476 /* A relative 16 bit branch. Bit 10 should be set to indicate that
477 the branch is not expected to be taken. The lower two bits must
478 be zero. */
479 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
480 0, /* rightshift */
481 2, /* size (0 = byte, 1 = short, 2 = long) */
482 16, /* bitsize */
483 TRUE, /* pc_relative */
484 0, /* bitpos */
485 complain_overflow_signed, /* complain_on_overflow */
486 ppc64_elf_brtaken_reloc, /* special_function */
487 "R_PPC64_REL14_BRNTAKEN",/* name */
488 FALSE, /* partial_inplace */
489 0, /* src_mask */
490 0x0000fffc, /* dst_mask */
491 TRUE), /* pcrel_offset */
492
493 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
494 symbol. */
495 HOWTO (R_PPC64_GOT16, /* type */
496 0, /* rightshift */
497 1, /* size (0 = byte, 1 = short, 2 = long) */
498 16, /* bitsize */
499 FALSE, /* pc_relative */
500 0, /* bitpos */
501 complain_overflow_signed, /* complain_on_overflow */
502 ppc64_elf_unhandled_reloc, /* special_function */
503 "R_PPC64_GOT16", /* name */
504 FALSE, /* partial_inplace */
505 0, /* src_mask */
506 0xffff, /* dst_mask */
507 FALSE), /* pcrel_offset */
508
509 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
510 the symbol. */
511 HOWTO (R_PPC64_GOT16_LO, /* type */
512 0, /* rightshift */
513 1, /* size (0 = byte, 1 = short, 2 = long) */
514 16, /* bitsize */
515 FALSE, /* pc_relative */
516 0, /* bitpos */
517 complain_overflow_dont, /* complain_on_overflow */
518 ppc64_elf_unhandled_reloc, /* special_function */
519 "R_PPC64_GOT16_LO", /* name */
520 FALSE, /* partial_inplace */
521 0, /* src_mask */
522 0xffff, /* dst_mask */
523 FALSE), /* pcrel_offset */
524
525 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
526 the symbol. */
527 HOWTO (R_PPC64_GOT16_HI, /* type */
528 16, /* rightshift */
529 1, /* size (0 = byte, 1 = short, 2 = long) */
530 16, /* bitsize */
531 FALSE, /* pc_relative */
532 0, /* bitpos */
533 complain_overflow_signed,/* complain_on_overflow */
534 ppc64_elf_unhandled_reloc, /* special_function */
535 "R_PPC64_GOT16_HI", /* name */
536 FALSE, /* partial_inplace */
537 0, /* src_mask */
538 0xffff, /* dst_mask */
539 FALSE), /* pcrel_offset */
540
541 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
542 the symbol. */
543 HOWTO (R_PPC64_GOT16_HA, /* type */
544 16, /* rightshift */
545 1, /* size (0 = byte, 1 = short, 2 = long) */
546 16, /* bitsize */
547 FALSE, /* pc_relative */
548 0, /* bitpos */
549 complain_overflow_signed,/* complain_on_overflow */
550 ppc64_elf_unhandled_reloc, /* special_function */
551 "R_PPC64_GOT16_HA", /* name */
552 FALSE, /* partial_inplace */
553 0, /* src_mask */
554 0xffff, /* dst_mask */
555 FALSE), /* pcrel_offset */
556
557 /* This is used only by the dynamic linker. The symbol should exist
558 both in the object being run and in some shared library. The
559 dynamic linker copies the data addressed by the symbol from the
560 shared library into the object, because the object being
561 run has to have the data at some particular address. */
562 HOWTO (R_PPC64_COPY, /* type */
563 0, /* rightshift */
564 0, /* this one is variable size */
565 0, /* bitsize */
566 FALSE, /* pc_relative */
567 0, /* bitpos */
568 complain_overflow_dont, /* complain_on_overflow */
569 ppc64_elf_unhandled_reloc, /* special_function */
570 "R_PPC64_COPY", /* name */
571 FALSE, /* partial_inplace */
572 0, /* src_mask */
573 0, /* dst_mask */
574 FALSE), /* pcrel_offset */
575
576 /* Like R_PPC64_ADDR64, but used when setting global offset table
577 entries. */
578 HOWTO (R_PPC64_GLOB_DAT, /* type */
579 0, /* rightshift */
580 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
581 64, /* bitsize */
582 FALSE, /* pc_relative */
583 0, /* bitpos */
584 complain_overflow_dont, /* complain_on_overflow */
585 ppc64_elf_unhandled_reloc, /* special_function */
586 "R_PPC64_GLOB_DAT", /* name */
587 FALSE, /* partial_inplace */
588 0, /* src_mask */
589 ONES (64), /* dst_mask */
590 FALSE), /* pcrel_offset */
591
592 /* Created by the link editor. Marks a procedure linkage table
593 entry for a symbol. */
594 HOWTO (R_PPC64_JMP_SLOT, /* type */
595 0, /* rightshift */
596 0, /* size (0 = byte, 1 = short, 2 = long) */
597 0, /* bitsize */
598 FALSE, /* pc_relative */
599 0, /* bitpos */
600 complain_overflow_dont, /* complain_on_overflow */
601 ppc64_elf_unhandled_reloc, /* special_function */
602 "R_PPC64_JMP_SLOT", /* name */
603 FALSE, /* partial_inplace */
604 0, /* src_mask */
605 0, /* dst_mask */
606 FALSE), /* pcrel_offset */
607
608 /* Used only by the dynamic linker. When the object is run, this
609 doubleword64 is set to the load address of the object, plus the
610 addend. */
611 HOWTO (R_PPC64_RELATIVE, /* type */
612 0, /* rightshift */
613 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
614 64, /* bitsize */
615 FALSE, /* pc_relative */
616 0, /* bitpos */
617 complain_overflow_dont, /* complain_on_overflow */
618 bfd_elf_generic_reloc, /* special_function */
619 "R_PPC64_RELATIVE", /* name */
620 FALSE, /* partial_inplace */
621 0, /* src_mask */
622 ONES (64), /* dst_mask */
623 FALSE), /* pcrel_offset */
624
625 /* Like R_PPC64_ADDR32, but may be unaligned. */
626 HOWTO (R_PPC64_UADDR32, /* type */
627 0, /* rightshift */
628 2, /* size (0 = byte, 1 = short, 2 = long) */
629 32, /* bitsize */
630 FALSE, /* pc_relative */
631 0, /* bitpos */
632 complain_overflow_bitfield, /* complain_on_overflow */
633 bfd_elf_generic_reloc, /* special_function */
634 "R_PPC64_UADDR32", /* name */
635 FALSE, /* partial_inplace */
636 0, /* src_mask */
637 0xffffffff, /* dst_mask */
638 FALSE), /* pcrel_offset */
639
640 /* Like R_PPC64_ADDR16, but may be unaligned. */
641 HOWTO (R_PPC64_UADDR16, /* type */
642 0, /* rightshift */
643 1, /* size (0 = byte, 1 = short, 2 = long) */
644 16, /* bitsize */
645 FALSE, /* pc_relative */
646 0, /* bitpos */
647 complain_overflow_bitfield, /* complain_on_overflow */
648 bfd_elf_generic_reloc, /* special_function */
649 "R_PPC64_UADDR16", /* name */
650 FALSE, /* partial_inplace */
651 0, /* src_mask */
652 0xffff, /* dst_mask */
653 FALSE), /* pcrel_offset */
654
655 /* 32-bit PC relative. */
656 HOWTO (R_PPC64_REL32, /* type */
657 0, /* rightshift */
658 2, /* size (0 = byte, 1 = short, 2 = long) */
659 32, /* bitsize */
660 TRUE, /* pc_relative */
661 0, /* bitpos */
662 complain_overflow_signed, /* complain_on_overflow */
663 bfd_elf_generic_reloc, /* special_function */
664 "R_PPC64_REL32", /* name */
665 FALSE, /* partial_inplace */
666 0, /* src_mask */
667 0xffffffff, /* dst_mask */
668 TRUE), /* pcrel_offset */
669
670 /* 32-bit relocation to the symbol's procedure linkage table. */
671 HOWTO (R_PPC64_PLT32, /* type */
672 0, /* rightshift */
673 2, /* size (0 = byte, 1 = short, 2 = long) */
674 32, /* bitsize */
675 FALSE, /* pc_relative */
676 0, /* bitpos */
677 complain_overflow_bitfield, /* complain_on_overflow */
678 ppc64_elf_unhandled_reloc, /* special_function */
679 "R_PPC64_PLT32", /* name */
680 FALSE, /* partial_inplace */
681 0, /* src_mask */
682 0xffffffff, /* dst_mask */
683 FALSE), /* pcrel_offset */
684
685 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
686 FIXME: R_PPC64_PLTREL32 not supported. */
687 HOWTO (R_PPC64_PLTREL32, /* type */
688 0, /* rightshift */
689 2, /* size (0 = byte, 1 = short, 2 = long) */
690 32, /* bitsize */
691 TRUE, /* pc_relative */
692 0, /* bitpos */
693 complain_overflow_signed, /* complain_on_overflow */
694 ppc64_elf_unhandled_reloc, /* special_function */
695 "R_PPC64_PLTREL32", /* name */
696 FALSE, /* partial_inplace */
697 0, /* src_mask */
698 0xffffffff, /* dst_mask */
699 TRUE), /* pcrel_offset */
700
701 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
702 the symbol. */
703 HOWTO (R_PPC64_PLT16_LO, /* type */
704 0, /* rightshift */
705 1, /* size (0 = byte, 1 = short, 2 = long) */
706 16, /* bitsize */
707 FALSE, /* pc_relative */
708 0, /* bitpos */
709 complain_overflow_dont, /* complain_on_overflow */
710 ppc64_elf_unhandled_reloc, /* special_function */
711 "R_PPC64_PLT16_LO", /* name */
712 FALSE, /* partial_inplace */
713 0, /* src_mask */
714 0xffff, /* dst_mask */
715 FALSE), /* pcrel_offset */
716
717 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
718 the symbol. */
719 HOWTO (R_PPC64_PLT16_HI, /* type */
720 16, /* rightshift */
721 1, /* size (0 = byte, 1 = short, 2 = long) */
722 16, /* bitsize */
723 FALSE, /* pc_relative */
724 0, /* bitpos */
725 complain_overflow_signed, /* complain_on_overflow */
726 ppc64_elf_unhandled_reloc, /* special_function */
727 "R_PPC64_PLT16_HI", /* name */
728 FALSE, /* partial_inplace */
729 0, /* src_mask */
730 0xffff, /* dst_mask */
731 FALSE), /* pcrel_offset */
732
733 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
734 the symbol. */
735 HOWTO (R_PPC64_PLT16_HA, /* type */
736 16, /* rightshift */
737 1, /* size (0 = byte, 1 = short, 2 = long) */
738 16, /* bitsize */
739 FALSE, /* pc_relative */
740 0, /* bitpos */
741 complain_overflow_signed, /* complain_on_overflow */
742 ppc64_elf_unhandled_reloc, /* special_function */
743 "R_PPC64_PLT16_HA", /* name */
744 FALSE, /* partial_inplace */
745 0, /* src_mask */
746 0xffff, /* dst_mask */
747 FALSE), /* pcrel_offset */
748
749 /* 16-bit section relative relocation. */
750 HOWTO (R_PPC64_SECTOFF, /* type */
751 0, /* rightshift */
752 1, /* size (0 = byte, 1 = short, 2 = long) */
753 16, /* bitsize */
754 FALSE, /* pc_relative */
755 0, /* bitpos */
756 complain_overflow_signed, /* complain_on_overflow */
757 ppc64_elf_sectoff_reloc, /* special_function */
758 "R_PPC64_SECTOFF", /* name */
759 FALSE, /* partial_inplace */
760 0, /* src_mask */
761 0xffff, /* dst_mask */
762 FALSE), /* pcrel_offset */
763
764 /* Like R_PPC64_SECTOFF, but no overflow warning. */
765 HOWTO (R_PPC64_SECTOFF_LO, /* type */
766 0, /* rightshift */
767 1, /* size (0 = byte, 1 = short, 2 = long) */
768 16, /* bitsize */
769 FALSE, /* pc_relative */
770 0, /* bitpos */
771 complain_overflow_dont, /* complain_on_overflow */
772 ppc64_elf_sectoff_reloc, /* special_function */
773 "R_PPC64_SECTOFF_LO", /* name */
774 FALSE, /* partial_inplace */
775 0, /* src_mask */
776 0xffff, /* dst_mask */
777 FALSE), /* pcrel_offset */
778
779 /* 16-bit upper half section relative relocation. */
780 HOWTO (R_PPC64_SECTOFF_HI, /* type */
781 16, /* rightshift */
782 1, /* size (0 = byte, 1 = short, 2 = long) */
783 16, /* bitsize */
784 FALSE, /* pc_relative */
785 0, /* bitpos */
786 complain_overflow_signed, /* complain_on_overflow */
787 ppc64_elf_sectoff_reloc, /* special_function */
788 "R_PPC64_SECTOFF_HI", /* name */
789 FALSE, /* partial_inplace */
790 0, /* src_mask */
791 0xffff, /* dst_mask */
792 FALSE), /* pcrel_offset */
793
794 /* 16-bit upper half adjusted section relative relocation. */
795 HOWTO (R_PPC64_SECTOFF_HA, /* type */
796 16, /* rightshift */
797 1, /* size (0 = byte, 1 = short, 2 = long) */
798 16, /* bitsize */
799 FALSE, /* pc_relative */
800 0, /* bitpos */
801 complain_overflow_signed, /* complain_on_overflow */
802 ppc64_elf_sectoff_ha_reloc, /* special_function */
803 "R_PPC64_SECTOFF_HA", /* name */
804 FALSE, /* partial_inplace */
805 0, /* src_mask */
806 0xffff, /* dst_mask */
807 FALSE), /* pcrel_offset */
808
809 /* Like R_PPC64_REL24 without touching the two least significant bits. */
810 HOWTO (R_PPC64_REL30, /* type */
811 2, /* rightshift */
812 2, /* size (0 = byte, 1 = short, 2 = long) */
813 30, /* bitsize */
814 TRUE, /* pc_relative */
815 0, /* bitpos */
816 complain_overflow_dont, /* complain_on_overflow */
817 bfd_elf_generic_reloc, /* special_function */
818 "R_PPC64_REL30", /* name */
819 FALSE, /* partial_inplace */
820 0, /* src_mask */
821 0xfffffffc, /* dst_mask */
822 TRUE), /* pcrel_offset */
823
824 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
825
826 /* A standard 64-bit relocation. */
827 HOWTO (R_PPC64_ADDR64, /* type */
828 0, /* rightshift */
829 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
830 64, /* bitsize */
831 FALSE, /* pc_relative */
832 0, /* bitpos */
833 complain_overflow_dont, /* complain_on_overflow */
834 bfd_elf_generic_reloc, /* special_function */
835 "R_PPC64_ADDR64", /* name */
836 FALSE, /* partial_inplace */
837 0, /* src_mask */
838 ONES (64), /* dst_mask */
839 FALSE), /* pcrel_offset */
840
841 /* The bits 32-47 of an address. */
842 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
843 32, /* rightshift */
844 1, /* size (0 = byte, 1 = short, 2 = long) */
845 16, /* bitsize */
846 FALSE, /* pc_relative */
847 0, /* bitpos */
848 complain_overflow_dont, /* complain_on_overflow */
849 bfd_elf_generic_reloc, /* special_function */
850 "R_PPC64_ADDR16_HIGHER", /* name */
851 FALSE, /* partial_inplace */
852 0, /* src_mask */
853 0xffff, /* dst_mask */
854 FALSE), /* pcrel_offset */
855
856 /* The bits 32-47 of an address, plus 1 if the contents of the low
857 16 bits, treated as a signed number, is negative. */
858 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
859 32, /* rightshift */
860 1, /* size (0 = byte, 1 = short, 2 = long) */
861 16, /* bitsize */
862 FALSE, /* pc_relative */
863 0, /* bitpos */
864 complain_overflow_dont, /* complain_on_overflow */
865 ppc64_elf_ha_reloc, /* special_function */
866 "R_PPC64_ADDR16_HIGHERA", /* name */
867 FALSE, /* partial_inplace */
868 0, /* src_mask */
869 0xffff, /* dst_mask */
870 FALSE), /* pcrel_offset */
871
872 /* The bits 48-63 of an address. */
873 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
874 48, /* rightshift */
875 1, /* size (0 = byte, 1 = short, 2 = long) */
876 16, /* bitsize */
877 FALSE, /* pc_relative */
878 0, /* bitpos */
879 complain_overflow_dont, /* complain_on_overflow */
880 bfd_elf_generic_reloc, /* special_function */
881 "R_PPC64_ADDR16_HIGHEST", /* name */
882 FALSE, /* partial_inplace */
883 0, /* src_mask */
884 0xffff, /* dst_mask */
885 FALSE), /* pcrel_offset */
886
887 /* The bits 48-63 of an address, plus 1 if the contents of the low
888 16 bits, treated as a signed number, is negative. */
889 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
890 48, /* rightshift */
891 1, /* size (0 = byte, 1 = short, 2 = long) */
892 16, /* bitsize */
893 FALSE, /* pc_relative */
894 0, /* bitpos */
895 complain_overflow_dont, /* complain_on_overflow */
896 ppc64_elf_ha_reloc, /* special_function */
897 "R_PPC64_ADDR16_HIGHESTA", /* name */
898 FALSE, /* partial_inplace */
899 0, /* src_mask */
900 0xffff, /* dst_mask */
901 FALSE), /* pcrel_offset */
902
903 /* Like ADDR64, but may be unaligned. */
904 HOWTO (R_PPC64_UADDR64, /* type */
905 0, /* rightshift */
906 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
907 64, /* bitsize */
908 FALSE, /* pc_relative */
909 0, /* bitpos */
910 complain_overflow_dont, /* complain_on_overflow */
911 bfd_elf_generic_reloc, /* special_function */
912 "R_PPC64_UADDR64", /* name */
913 FALSE, /* partial_inplace */
914 0, /* src_mask */
915 ONES (64), /* dst_mask */
916 FALSE), /* pcrel_offset */
917
918 /* 64-bit relative relocation. */
919 HOWTO (R_PPC64_REL64, /* type */
920 0, /* rightshift */
921 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
922 64, /* bitsize */
923 TRUE, /* pc_relative */
924 0, /* bitpos */
925 complain_overflow_dont, /* complain_on_overflow */
926 bfd_elf_generic_reloc, /* special_function */
927 "R_PPC64_REL64", /* name */
928 FALSE, /* partial_inplace */
929 0, /* src_mask */
930 ONES (64), /* dst_mask */
931 TRUE), /* pcrel_offset */
932
933 /* 64-bit relocation to the symbol's procedure linkage table. */
934 HOWTO (R_PPC64_PLT64, /* type */
935 0, /* rightshift */
936 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
937 64, /* bitsize */
938 FALSE, /* pc_relative */
939 0, /* bitpos */
940 complain_overflow_dont, /* complain_on_overflow */
941 ppc64_elf_unhandled_reloc, /* special_function */
942 "R_PPC64_PLT64", /* name */
943 FALSE, /* partial_inplace */
944 0, /* src_mask */
945 ONES (64), /* dst_mask */
946 FALSE), /* pcrel_offset */
947
948 /* 64-bit PC relative relocation to the symbol's procedure linkage
949 table. */
950 /* FIXME: R_PPC64_PLTREL64 not supported. */
951 HOWTO (R_PPC64_PLTREL64, /* type */
952 0, /* rightshift */
953 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
954 64, /* bitsize */
955 TRUE, /* pc_relative */
956 0, /* bitpos */
957 complain_overflow_dont, /* complain_on_overflow */
958 ppc64_elf_unhandled_reloc, /* special_function */
959 "R_PPC64_PLTREL64", /* name */
960 FALSE, /* partial_inplace */
961 0, /* src_mask */
962 ONES (64), /* dst_mask */
963 TRUE), /* pcrel_offset */
964
965 /* 16 bit TOC-relative relocation. */
966
967 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
968 HOWTO (R_PPC64_TOC16, /* type */
969 0, /* rightshift */
970 1, /* size (0 = byte, 1 = short, 2 = long) */
971 16, /* bitsize */
972 FALSE, /* pc_relative */
973 0, /* bitpos */
974 complain_overflow_signed, /* complain_on_overflow */
975 ppc64_elf_toc_reloc, /* special_function */
976 "R_PPC64_TOC16", /* name */
977 FALSE, /* partial_inplace */
978 0, /* src_mask */
979 0xffff, /* dst_mask */
980 FALSE), /* pcrel_offset */
981
982 /* 16 bit TOC-relative relocation without overflow. */
983
984 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
985 HOWTO (R_PPC64_TOC16_LO, /* type */
986 0, /* rightshift */
987 1, /* size (0 = byte, 1 = short, 2 = long) */
988 16, /* bitsize */
989 FALSE, /* pc_relative */
990 0, /* bitpos */
991 complain_overflow_dont, /* complain_on_overflow */
992 ppc64_elf_toc_reloc, /* special_function */
993 "R_PPC64_TOC16_LO", /* name */
994 FALSE, /* partial_inplace */
995 0, /* src_mask */
996 0xffff, /* dst_mask */
997 FALSE), /* pcrel_offset */
998
999 /* 16 bit TOC-relative relocation, high 16 bits. */
1000
1001 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
1002 HOWTO (R_PPC64_TOC16_HI, /* type */
1003 16, /* rightshift */
1004 1, /* size (0 = byte, 1 = short, 2 = long) */
1005 16, /* bitsize */
1006 FALSE, /* pc_relative */
1007 0, /* bitpos */
1008 complain_overflow_signed, /* complain_on_overflow */
1009 ppc64_elf_toc_reloc, /* special_function */
1010 "R_PPC64_TOC16_HI", /* name */
1011 FALSE, /* partial_inplace */
1012 0, /* src_mask */
1013 0xffff, /* dst_mask */
1014 FALSE), /* pcrel_offset */
1015
1016 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1017 contents of the low 16 bits, treated as a signed number, is
1018 negative. */
1019
1020 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1021 HOWTO (R_PPC64_TOC16_HA, /* type */
1022 16, /* rightshift */
1023 1, /* size (0 = byte, 1 = short, 2 = long) */
1024 16, /* bitsize */
1025 FALSE, /* pc_relative */
1026 0, /* bitpos */
1027 complain_overflow_signed, /* complain_on_overflow */
1028 ppc64_elf_toc_ha_reloc, /* special_function */
1029 "R_PPC64_TOC16_HA", /* name */
1030 FALSE, /* partial_inplace */
1031 0, /* src_mask */
1032 0xffff, /* dst_mask */
1033 FALSE), /* pcrel_offset */
1034
1035 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1036
1037 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1038 HOWTO (R_PPC64_TOC, /* type */
1039 0, /* rightshift */
1040 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1041 64, /* bitsize */
1042 FALSE, /* pc_relative */
1043 0, /* bitpos */
1044 complain_overflow_dont, /* complain_on_overflow */
1045 ppc64_elf_toc64_reloc, /* special_function */
1046 "R_PPC64_TOC", /* name */
1047 FALSE, /* partial_inplace */
1048 0, /* src_mask */
1049 ONES (64), /* dst_mask */
1050 FALSE), /* pcrel_offset */
1051
1052 /* Like R_PPC64_GOT16, but also informs the link editor that the
1053 value to relocate may (!) refer to a PLT entry which the link
1054 editor (a) may replace with the symbol value. If the link editor
1055 is unable to fully resolve the symbol, it may (b) create a PLT
1056 entry and store the address to the new PLT entry in the GOT.
1057 This permits lazy resolution of function symbols at run time.
1058 The link editor may also skip all of this and just (c) emit a
1059 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1060 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1061 HOWTO (R_PPC64_PLTGOT16, /* type */
1062 0, /* rightshift */
1063 1, /* size (0 = byte, 1 = short, 2 = long) */
1064 16, /* bitsize */
1065 FALSE, /* pc_relative */
1066 0, /* bitpos */
1067 complain_overflow_signed, /* complain_on_overflow */
1068 ppc64_elf_unhandled_reloc, /* special_function */
1069 "R_PPC64_PLTGOT16", /* name */
1070 FALSE, /* partial_inplace */
1071 0, /* src_mask */
1072 0xffff, /* dst_mask */
1073 FALSE), /* pcrel_offset */
1074
1075 /* Like R_PPC64_PLTGOT16, but without overflow. */
1076 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1077 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1078 0, /* rightshift */
1079 1, /* size (0 = byte, 1 = short, 2 = long) */
1080 16, /* bitsize */
1081 FALSE, /* pc_relative */
1082 0, /* bitpos */
1083 complain_overflow_dont, /* complain_on_overflow */
1084 ppc64_elf_unhandled_reloc, /* special_function */
1085 "R_PPC64_PLTGOT16_LO", /* name */
1086 FALSE, /* partial_inplace */
1087 0, /* src_mask */
1088 0xffff, /* dst_mask */
1089 FALSE), /* pcrel_offset */
1090
1091 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1092 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1093 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1094 16, /* rightshift */
1095 1, /* size (0 = byte, 1 = short, 2 = long) */
1096 16, /* bitsize */
1097 FALSE, /* pc_relative */
1098 0, /* bitpos */
1099 complain_overflow_signed, /* complain_on_overflow */
1100 ppc64_elf_unhandled_reloc, /* special_function */
1101 "R_PPC64_PLTGOT16_HI", /* name */
1102 FALSE, /* partial_inplace */
1103 0, /* src_mask */
1104 0xffff, /* dst_mask */
1105 FALSE), /* pcrel_offset */
1106
1107 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1108 1 if the contents of the low 16 bits, treated as a signed number,
1109 is negative. */
1110 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1111 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1112 16, /* rightshift */
1113 1, /* size (0 = byte, 1 = short, 2 = long) */
1114 16, /* bitsize */
1115 FALSE, /* pc_relative */
1116 0, /* bitpos */
1117 complain_overflow_signed, /* complain_on_overflow */
1118 ppc64_elf_unhandled_reloc, /* special_function */
1119 "R_PPC64_PLTGOT16_HA", /* name */
1120 FALSE, /* partial_inplace */
1121 0, /* src_mask */
1122 0xffff, /* dst_mask */
1123 FALSE), /* pcrel_offset */
1124
1125 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1126 HOWTO (R_PPC64_ADDR16_DS, /* type */
1127 0, /* rightshift */
1128 1, /* size (0 = byte, 1 = short, 2 = long) */
1129 16, /* bitsize */
1130 FALSE, /* pc_relative */
1131 0, /* bitpos */
1132 complain_overflow_signed, /* complain_on_overflow */
1133 bfd_elf_generic_reloc, /* special_function */
1134 "R_PPC64_ADDR16_DS", /* name */
1135 FALSE, /* partial_inplace */
1136 0, /* src_mask */
1137 0xfffc, /* dst_mask */
1138 FALSE), /* pcrel_offset */
1139
1140 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1141 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1142 0, /* rightshift */
1143 1, /* size (0 = byte, 1 = short, 2 = long) */
1144 16, /* bitsize */
1145 FALSE, /* pc_relative */
1146 0, /* bitpos */
1147 complain_overflow_dont,/* complain_on_overflow */
1148 bfd_elf_generic_reloc, /* special_function */
1149 "R_PPC64_ADDR16_LO_DS",/* name */
1150 FALSE, /* partial_inplace */
1151 0, /* src_mask */
1152 0xfffc, /* dst_mask */
1153 FALSE), /* pcrel_offset */
1154
1155 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1156 HOWTO (R_PPC64_GOT16_DS, /* type */
1157 0, /* rightshift */
1158 1, /* size (0 = byte, 1 = short, 2 = long) */
1159 16, /* bitsize */
1160 FALSE, /* pc_relative */
1161 0, /* bitpos */
1162 complain_overflow_signed, /* complain_on_overflow */
1163 ppc64_elf_unhandled_reloc, /* special_function */
1164 "R_PPC64_GOT16_DS", /* name */
1165 FALSE, /* partial_inplace */
1166 0, /* src_mask */
1167 0xfffc, /* dst_mask */
1168 FALSE), /* pcrel_offset */
1169
1170 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1171 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1172 0, /* rightshift */
1173 1, /* size (0 = byte, 1 = short, 2 = long) */
1174 16, /* bitsize */
1175 FALSE, /* pc_relative */
1176 0, /* bitpos */
1177 complain_overflow_dont, /* complain_on_overflow */
1178 ppc64_elf_unhandled_reloc, /* special_function */
1179 "R_PPC64_GOT16_LO_DS", /* name */
1180 FALSE, /* partial_inplace */
1181 0, /* src_mask */
1182 0xfffc, /* dst_mask */
1183 FALSE), /* pcrel_offset */
1184
1185 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1186 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1187 0, /* rightshift */
1188 1, /* size (0 = byte, 1 = short, 2 = long) */
1189 16, /* bitsize */
1190 FALSE, /* pc_relative */
1191 0, /* bitpos */
1192 complain_overflow_dont, /* complain_on_overflow */
1193 ppc64_elf_unhandled_reloc, /* special_function */
1194 "R_PPC64_PLT16_LO_DS", /* name */
1195 FALSE, /* partial_inplace */
1196 0, /* src_mask */
1197 0xfffc, /* dst_mask */
1198 FALSE), /* pcrel_offset */
1199
1200 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1201 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1202 0, /* rightshift */
1203 1, /* size (0 = byte, 1 = short, 2 = long) */
1204 16, /* bitsize */
1205 FALSE, /* pc_relative */
1206 0, /* bitpos */
1207 complain_overflow_signed, /* complain_on_overflow */
1208 ppc64_elf_sectoff_reloc, /* special_function */
1209 "R_PPC64_SECTOFF_DS", /* name */
1210 FALSE, /* partial_inplace */
1211 0, /* src_mask */
1212 0xfffc, /* dst_mask */
1213 FALSE), /* pcrel_offset */
1214
1215 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1216 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1217 0, /* rightshift */
1218 1, /* size (0 = byte, 1 = short, 2 = long) */
1219 16, /* bitsize */
1220 FALSE, /* pc_relative */
1221 0, /* bitpos */
1222 complain_overflow_dont, /* complain_on_overflow */
1223 ppc64_elf_sectoff_reloc, /* special_function */
1224 "R_PPC64_SECTOFF_LO_DS",/* name */
1225 FALSE, /* partial_inplace */
1226 0, /* src_mask */
1227 0xfffc, /* dst_mask */
1228 FALSE), /* pcrel_offset */
1229
1230 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1231 HOWTO (R_PPC64_TOC16_DS, /* type */
1232 0, /* rightshift */
1233 1, /* size (0 = byte, 1 = short, 2 = long) */
1234 16, /* bitsize */
1235 FALSE, /* pc_relative */
1236 0, /* bitpos */
1237 complain_overflow_signed, /* complain_on_overflow */
1238 ppc64_elf_toc_reloc, /* special_function */
1239 "R_PPC64_TOC16_DS", /* name */
1240 FALSE, /* partial_inplace */
1241 0, /* src_mask */
1242 0xfffc, /* dst_mask */
1243 FALSE), /* pcrel_offset */
1244
1245 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1246 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1247 0, /* rightshift */
1248 1, /* size (0 = byte, 1 = short, 2 = long) */
1249 16, /* bitsize */
1250 FALSE, /* pc_relative */
1251 0, /* bitpos */
1252 complain_overflow_dont, /* complain_on_overflow */
1253 ppc64_elf_toc_reloc, /* special_function */
1254 "R_PPC64_TOC16_LO_DS", /* name */
1255 FALSE, /* partial_inplace */
1256 0, /* src_mask */
1257 0xfffc, /* dst_mask */
1258 FALSE), /* pcrel_offset */
1259
1260 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1261 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1262 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1263 0, /* rightshift */
1264 1, /* size (0 = byte, 1 = short, 2 = long) */
1265 16, /* bitsize */
1266 FALSE, /* pc_relative */
1267 0, /* bitpos */
1268 complain_overflow_signed, /* complain_on_overflow */
1269 ppc64_elf_unhandled_reloc, /* special_function */
1270 "R_PPC64_PLTGOT16_DS", /* name */
1271 FALSE, /* partial_inplace */
1272 0, /* src_mask */
1273 0xfffc, /* dst_mask */
1274 FALSE), /* pcrel_offset */
1275
1276 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1277 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1278 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1279 0, /* rightshift */
1280 1, /* size (0 = byte, 1 = short, 2 = long) */
1281 16, /* bitsize */
1282 FALSE, /* pc_relative */
1283 0, /* bitpos */
1284 complain_overflow_dont, /* complain_on_overflow */
1285 ppc64_elf_unhandled_reloc, /* special_function */
1286 "R_PPC64_PLTGOT16_LO_DS",/* name */
1287 FALSE, /* partial_inplace */
1288 0, /* src_mask */
1289 0xfffc, /* dst_mask */
1290 FALSE), /* pcrel_offset */
1291
1292 /* Marker relocs for TLS. */
1293 HOWTO (R_PPC64_TLS,
1294 0, /* rightshift */
1295 2, /* size (0 = byte, 1 = short, 2 = long) */
1296 32, /* bitsize */
1297 FALSE, /* pc_relative */
1298 0, /* bitpos */
1299 complain_overflow_dont, /* complain_on_overflow */
1300 bfd_elf_generic_reloc, /* special_function */
1301 "R_PPC64_TLS", /* name */
1302 FALSE, /* partial_inplace */
1303 0, /* src_mask */
1304 0, /* dst_mask */
1305 FALSE), /* pcrel_offset */
1306
1307 HOWTO (R_PPC64_TLSGD,
1308 0, /* rightshift */
1309 2, /* size (0 = byte, 1 = short, 2 = long) */
1310 32, /* bitsize */
1311 FALSE, /* pc_relative */
1312 0, /* bitpos */
1313 complain_overflow_dont, /* complain_on_overflow */
1314 bfd_elf_generic_reloc, /* special_function */
1315 "R_PPC64_TLSGD", /* name */
1316 FALSE, /* partial_inplace */
1317 0, /* src_mask */
1318 0, /* dst_mask */
1319 FALSE), /* pcrel_offset */
1320
1321 HOWTO (R_PPC64_TLSLD,
1322 0, /* rightshift */
1323 2, /* size (0 = byte, 1 = short, 2 = long) */
1324 32, /* bitsize */
1325 FALSE, /* pc_relative */
1326 0, /* bitpos */
1327 complain_overflow_dont, /* complain_on_overflow */
1328 bfd_elf_generic_reloc, /* special_function */
1329 "R_PPC64_TLSLD", /* name */
1330 FALSE, /* partial_inplace */
1331 0, /* src_mask */
1332 0, /* dst_mask */
1333 FALSE), /* pcrel_offset */
1334
1335 HOWTO (R_PPC64_TOCSAVE,
1336 0, /* rightshift */
1337 2, /* size (0 = byte, 1 = short, 2 = long) */
1338 32, /* bitsize */
1339 FALSE, /* pc_relative */
1340 0, /* bitpos */
1341 complain_overflow_dont, /* complain_on_overflow */
1342 bfd_elf_generic_reloc, /* special_function */
1343 "R_PPC64_TOCSAVE", /* name */
1344 FALSE, /* partial_inplace */
1345 0, /* src_mask */
1346 0, /* dst_mask */
1347 FALSE), /* pcrel_offset */
1348
1349 /* Computes the load module index of the load module that contains the
1350 definition of its TLS sym. */
1351 HOWTO (R_PPC64_DTPMOD64,
1352 0, /* rightshift */
1353 4, /* size (0 = byte, 1 = short, 2 = long) */
1354 64, /* bitsize */
1355 FALSE, /* pc_relative */
1356 0, /* bitpos */
1357 complain_overflow_dont, /* complain_on_overflow */
1358 ppc64_elf_unhandled_reloc, /* special_function */
1359 "R_PPC64_DTPMOD64", /* name */
1360 FALSE, /* partial_inplace */
1361 0, /* src_mask */
1362 ONES (64), /* dst_mask */
1363 FALSE), /* pcrel_offset */
1364
1365 /* Computes a dtv-relative displacement, the difference between the value
1366 of sym+add and the base address of the thread-local storage block that
1367 contains the definition of sym, minus 0x8000. */
1368 HOWTO (R_PPC64_DTPREL64,
1369 0, /* rightshift */
1370 4, /* size (0 = byte, 1 = short, 2 = long) */
1371 64, /* bitsize */
1372 FALSE, /* pc_relative */
1373 0, /* bitpos */
1374 complain_overflow_dont, /* complain_on_overflow */
1375 ppc64_elf_unhandled_reloc, /* special_function */
1376 "R_PPC64_DTPREL64", /* name */
1377 FALSE, /* partial_inplace */
1378 0, /* src_mask */
1379 ONES (64), /* dst_mask */
1380 FALSE), /* pcrel_offset */
1381
1382 /* A 16 bit dtprel reloc. */
1383 HOWTO (R_PPC64_DTPREL16,
1384 0, /* rightshift */
1385 1, /* size (0 = byte, 1 = short, 2 = long) */
1386 16, /* bitsize */
1387 FALSE, /* pc_relative */
1388 0, /* bitpos */
1389 complain_overflow_signed, /* complain_on_overflow */
1390 ppc64_elf_unhandled_reloc, /* special_function */
1391 "R_PPC64_DTPREL16", /* name */
1392 FALSE, /* partial_inplace */
1393 0, /* src_mask */
1394 0xffff, /* dst_mask */
1395 FALSE), /* pcrel_offset */
1396
1397 /* Like DTPREL16, but no overflow. */
1398 HOWTO (R_PPC64_DTPREL16_LO,
1399 0, /* rightshift */
1400 1, /* size (0 = byte, 1 = short, 2 = long) */
1401 16, /* bitsize */
1402 FALSE, /* pc_relative */
1403 0, /* bitpos */
1404 complain_overflow_dont, /* complain_on_overflow */
1405 ppc64_elf_unhandled_reloc, /* special_function */
1406 "R_PPC64_DTPREL16_LO", /* name */
1407 FALSE, /* partial_inplace */
1408 0, /* src_mask */
1409 0xffff, /* dst_mask */
1410 FALSE), /* pcrel_offset */
1411
1412 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1413 HOWTO (R_PPC64_DTPREL16_HI,
1414 16, /* rightshift */
1415 1, /* size (0 = byte, 1 = short, 2 = long) */
1416 16, /* bitsize */
1417 FALSE, /* pc_relative */
1418 0, /* bitpos */
1419 complain_overflow_signed, /* complain_on_overflow */
1420 ppc64_elf_unhandled_reloc, /* special_function */
1421 "R_PPC64_DTPREL16_HI", /* name */
1422 FALSE, /* partial_inplace */
1423 0, /* src_mask */
1424 0xffff, /* dst_mask */
1425 FALSE), /* pcrel_offset */
1426
1427 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1428 HOWTO (R_PPC64_DTPREL16_HA,
1429 16, /* rightshift */
1430 1, /* size (0 = byte, 1 = short, 2 = long) */
1431 16, /* bitsize */
1432 FALSE, /* pc_relative */
1433 0, /* bitpos */
1434 complain_overflow_signed, /* complain_on_overflow */
1435 ppc64_elf_unhandled_reloc, /* special_function */
1436 "R_PPC64_DTPREL16_HA", /* name */
1437 FALSE, /* partial_inplace */
1438 0, /* src_mask */
1439 0xffff, /* dst_mask */
1440 FALSE), /* pcrel_offset */
1441
1442 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1443 HOWTO (R_PPC64_DTPREL16_HIGHER,
1444 32, /* rightshift */
1445 1, /* size (0 = byte, 1 = short, 2 = long) */
1446 16, /* bitsize */
1447 FALSE, /* pc_relative */
1448 0, /* bitpos */
1449 complain_overflow_dont, /* complain_on_overflow */
1450 ppc64_elf_unhandled_reloc, /* special_function */
1451 "R_PPC64_DTPREL16_HIGHER", /* name */
1452 FALSE, /* partial_inplace */
1453 0, /* src_mask */
1454 0xffff, /* dst_mask */
1455 FALSE), /* pcrel_offset */
1456
1457 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1458 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1459 32, /* rightshift */
1460 1, /* size (0 = byte, 1 = short, 2 = long) */
1461 16, /* bitsize */
1462 FALSE, /* pc_relative */
1463 0, /* bitpos */
1464 complain_overflow_dont, /* complain_on_overflow */
1465 ppc64_elf_unhandled_reloc, /* special_function */
1466 "R_PPC64_DTPREL16_HIGHERA", /* name */
1467 FALSE, /* partial_inplace */
1468 0, /* src_mask */
1469 0xffff, /* dst_mask */
1470 FALSE), /* pcrel_offset */
1471
1472 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1473 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1474 48, /* rightshift */
1475 1, /* size (0 = byte, 1 = short, 2 = long) */
1476 16, /* bitsize */
1477 FALSE, /* pc_relative */
1478 0, /* bitpos */
1479 complain_overflow_dont, /* complain_on_overflow */
1480 ppc64_elf_unhandled_reloc, /* special_function */
1481 "R_PPC64_DTPREL16_HIGHEST", /* name */
1482 FALSE, /* partial_inplace */
1483 0, /* src_mask */
1484 0xffff, /* dst_mask */
1485 FALSE), /* pcrel_offset */
1486
1487 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1488 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1489 48, /* rightshift */
1490 1, /* size (0 = byte, 1 = short, 2 = long) */
1491 16, /* bitsize */
1492 FALSE, /* pc_relative */
1493 0, /* bitpos */
1494 complain_overflow_dont, /* complain_on_overflow */
1495 ppc64_elf_unhandled_reloc, /* special_function */
1496 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1497 FALSE, /* partial_inplace */
1498 0, /* src_mask */
1499 0xffff, /* dst_mask */
1500 FALSE), /* pcrel_offset */
1501
1502 /* Like DTPREL16, but for insns with a DS field. */
1503 HOWTO (R_PPC64_DTPREL16_DS,
1504 0, /* rightshift */
1505 1, /* size (0 = byte, 1 = short, 2 = long) */
1506 16, /* bitsize */
1507 FALSE, /* pc_relative */
1508 0, /* bitpos */
1509 complain_overflow_signed, /* complain_on_overflow */
1510 ppc64_elf_unhandled_reloc, /* special_function */
1511 "R_PPC64_DTPREL16_DS", /* name */
1512 FALSE, /* partial_inplace */
1513 0, /* src_mask */
1514 0xfffc, /* dst_mask */
1515 FALSE), /* pcrel_offset */
1516
1517 /* Like DTPREL16_DS, but no overflow. */
1518 HOWTO (R_PPC64_DTPREL16_LO_DS,
1519 0, /* rightshift */
1520 1, /* size (0 = byte, 1 = short, 2 = long) */
1521 16, /* bitsize */
1522 FALSE, /* pc_relative */
1523 0, /* bitpos */
1524 complain_overflow_dont, /* complain_on_overflow */
1525 ppc64_elf_unhandled_reloc, /* special_function */
1526 "R_PPC64_DTPREL16_LO_DS", /* name */
1527 FALSE, /* partial_inplace */
1528 0, /* src_mask */
1529 0xfffc, /* dst_mask */
1530 FALSE), /* pcrel_offset */
1531
1532 /* Computes a tp-relative displacement, the difference between the value of
1533 sym+add and the value of the thread pointer (r13). */
1534 HOWTO (R_PPC64_TPREL64,
1535 0, /* rightshift */
1536 4, /* size (0 = byte, 1 = short, 2 = long) */
1537 64, /* bitsize */
1538 FALSE, /* pc_relative */
1539 0, /* bitpos */
1540 complain_overflow_dont, /* complain_on_overflow */
1541 ppc64_elf_unhandled_reloc, /* special_function */
1542 "R_PPC64_TPREL64", /* name */
1543 FALSE, /* partial_inplace */
1544 0, /* src_mask */
1545 ONES (64), /* dst_mask */
1546 FALSE), /* pcrel_offset */
1547
1548 /* A 16 bit tprel reloc. */
1549 HOWTO (R_PPC64_TPREL16,
1550 0, /* rightshift */
1551 1, /* size (0 = byte, 1 = short, 2 = long) */
1552 16, /* bitsize */
1553 FALSE, /* pc_relative */
1554 0, /* bitpos */
1555 complain_overflow_signed, /* complain_on_overflow */
1556 ppc64_elf_unhandled_reloc, /* special_function */
1557 "R_PPC64_TPREL16", /* name */
1558 FALSE, /* partial_inplace */
1559 0, /* src_mask */
1560 0xffff, /* dst_mask */
1561 FALSE), /* pcrel_offset */
1562
1563 /* Like TPREL16, but no overflow. */
1564 HOWTO (R_PPC64_TPREL16_LO,
1565 0, /* rightshift */
1566 1, /* size (0 = byte, 1 = short, 2 = long) */
1567 16, /* bitsize */
1568 FALSE, /* pc_relative */
1569 0, /* bitpos */
1570 complain_overflow_dont, /* complain_on_overflow */
1571 ppc64_elf_unhandled_reloc, /* special_function */
1572 "R_PPC64_TPREL16_LO", /* name */
1573 FALSE, /* partial_inplace */
1574 0, /* src_mask */
1575 0xffff, /* dst_mask */
1576 FALSE), /* pcrel_offset */
1577
1578 /* Like TPREL16_LO, but next higher group of 16 bits. */
1579 HOWTO (R_PPC64_TPREL16_HI,
1580 16, /* rightshift */
1581 1, /* size (0 = byte, 1 = short, 2 = long) */
1582 16, /* bitsize */
1583 FALSE, /* pc_relative */
1584 0, /* bitpos */
1585 complain_overflow_signed, /* complain_on_overflow */
1586 ppc64_elf_unhandled_reloc, /* special_function */
1587 "R_PPC64_TPREL16_HI", /* name */
1588 FALSE, /* partial_inplace */
1589 0, /* src_mask */
1590 0xffff, /* dst_mask */
1591 FALSE), /* pcrel_offset */
1592
1593 /* Like TPREL16_HI, but adjust for low 16 bits. */
1594 HOWTO (R_PPC64_TPREL16_HA,
1595 16, /* rightshift */
1596 1, /* size (0 = byte, 1 = short, 2 = long) */
1597 16, /* bitsize */
1598 FALSE, /* pc_relative */
1599 0, /* bitpos */
1600 complain_overflow_signed, /* complain_on_overflow */
1601 ppc64_elf_unhandled_reloc, /* special_function */
1602 "R_PPC64_TPREL16_HA", /* name */
1603 FALSE, /* partial_inplace */
1604 0, /* src_mask */
1605 0xffff, /* dst_mask */
1606 FALSE), /* pcrel_offset */
1607
1608 /* Like TPREL16_HI, but next higher group of 16 bits. */
1609 HOWTO (R_PPC64_TPREL16_HIGHER,
1610 32, /* rightshift */
1611 1, /* size (0 = byte, 1 = short, 2 = long) */
1612 16, /* bitsize */
1613 FALSE, /* pc_relative */
1614 0, /* bitpos */
1615 complain_overflow_dont, /* complain_on_overflow */
1616 ppc64_elf_unhandled_reloc, /* special_function */
1617 "R_PPC64_TPREL16_HIGHER", /* name */
1618 FALSE, /* partial_inplace */
1619 0, /* src_mask */
1620 0xffff, /* dst_mask */
1621 FALSE), /* pcrel_offset */
1622
1623 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1624 HOWTO (R_PPC64_TPREL16_HIGHERA,
1625 32, /* rightshift */
1626 1, /* size (0 = byte, 1 = short, 2 = long) */
1627 16, /* bitsize */
1628 FALSE, /* pc_relative */
1629 0, /* bitpos */
1630 complain_overflow_dont, /* complain_on_overflow */
1631 ppc64_elf_unhandled_reloc, /* special_function */
1632 "R_PPC64_TPREL16_HIGHERA", /* name */
1633 FALSE, /* partial_inplace */
1634 0, /* src_mask */
1635 0xffff, /* dst_mask */
1636 FALSE), /* pcrel_offset */
1637
1638 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1639 HOWTO (R_PPC64_TPREL16_HIGHEST,
1640 48, /* rightshift */
1641 1, /* size (0 = byte, 1 = short, 2 = long) */
1642 16, /* bitsize */
1643 FALSE, /* pc_relative */
1644 0, /* bitpos */
1645 complain_overflow_dont, /* complain_on_overflow */
1646 ppc64_elf_unhandled_reloc, /* special_function */
1647 "R_PPC64_TPREL16_HIGHEST", /* name */
1648 FALSE, /* partial_inplace */
1649 0, /* src_mask */
1650 0xffff, /* dst_mask */
1651 FALSE), /* pcrel_offset */
1652
1653 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1654 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1655 48, /* rightshift */
1656 1, /* size (0 = byte, 1 = short, 2 = long) */
1657 16, /* bitsize */
1658 FALSE, /* pc_relative */
1659 0, /* bitpos */
1660 complain_overflow_dont, /* complain_on_overflow */
1661 ppc64_elf_unhandled_reloc, /* special_function */
1662 "R_PPC64_TPREL16_HIGHESTA", /* name */
1663 FALSE, /* partial_inplace */
1664 0, /* src_mask */
1665 0xffff, /* dst_mask */
1666 FALSE), /* pcrel_offset */
1667
1668 /* Like TPREL16, but for insns with a DS field. */
1669 HOWTO (R_PPC64_TPREL16_DS,
1670 0, /* rightshift */
1671 1, /* size (0 = byte, 1 = short, 2 = long) */
1672 16, /* bitsize */
1673 FALSE, /* pc_relative */
1674 0, /* bitpos */
1675 complain_overflow_signed, /* complain_on_overflow */
1676 ppc64_elf_unhandled_reloc, /* special_function */
1677 "R_PPC64_TPREL16_DS", /* name */
1678 FALSE, /* partial_inplace */
1679 0, /* src_mask */
1680 0xfffc, /* dst_mask */
1681 FALSE), /* pcrel_offset */
1682
1683 /* Like TPREL16_DS, but no overflow. */
1684 HOWTO (R_PPC64_TPREL16_LO_DS,
1685 0, /* rightshift */
1686 1, /* size (0 = byte, 1 = short, 2 = long) */
1687 16, /* bitsize */
1688 FALSE, /* pc_relative */
1689 0, /* bitpos */
1690 complain_overflow_dont, /* complain_on_overflow */
1691 ppc64_elf_unhandled_reloc, /* special_function */
1692 "R_PPC64_TPREL16_LO_DS", /* name */
1693 FALSE, /* partial_inplace */
1694 0, /* src_mask */
1695 0xfffc, /* dst_mask */
1696 FALSE), /* pcrel_offset */
1697
1698 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1699 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1700 to the first entry relative to the TOC base (r2). */
1701 HOWTO (R_PPC64_GOT_TLSGD16,
1702 0, /* rightshift */
1703 1, /* size (0 = byte, 1 = short, 2 = long) */
1704 16, /* bitsize */
1705 FALSE, /* pc_relative */
1706 0, /* bitpos */
1707 complain_overflow_signed, /* complain_on_overflow */
1708 ppc64_elf_unhandled_reloc, /* special_function */
1709 "R_PPC64_GOT_TLSGD16", /* name */
1710 FALSE, /* partial_inplace */
1711 0, /* src_mask */
1712 0xffff, /* dst_mask */
1713 FALSE), /* pcrel_offset */
1714
1715 /* Like GOT_TLSGD16, but no overflow. */
1716 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1717 0, /* rightshift */
1718 1, /* size (0 = byte, 1 = short, 2 = long) */
1719 16, /* bitsize */
1720 FALSE, /* pc_relative */
1721 0, /* bitpos */
1722 complain_overflow_dont, /* complain_on_overflow */
1723 ppc64_elf_unhandled_reloc, /* special_function */
1724 "R_PPC64_GOT_TLSGD16_LO", /* name */
1725 FALSE, /* partial_inplace */
1726 0, /* src_mask */
1727 0xffff, /* dst_mask */
1728 FALSE), /* pcrel_offset */
1729
1730 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1731 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1732 16, /* rightshift */
1733 1, /* size (0 = byte, 1 = short, 2 = long) */
1734 16, /* bitsize */
1735 FALSE, /* pc_relative */
1736 0, /* bitpos */
1737 complain_overflow_signed, /* complain_on_overflow */
1738 ppc64_elf_unhandled_reloc, /* special_function */
1739 "R_PPC64_GOT_TLSGD16_HI", /* name */
1740 FALSE, /* partial_inplace */
1741 0, /* src_mask */
1742 0xffff, /* dst_mask */
1743 FALSE), /* pcrel_offset */
1744
1745 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1746 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1747 16, /* rightshift */
1748 1, /* size (0 = byte, 1 = short, 2 = long) */
1749 16, /* bitsize */
1750 FALSE, /* pc_relative */
1751 0, /* bitpos */
1752 complain_overflow_signed, /* complain_on_overflow */
1753 ppc64_elf_unhandled_reloc, /* special_function */
1754 "R_PPC64_GOT_TLSGD16_HA", /* name */
1755 FALSE, /* partial_inplace */
1756 0, /* src_mask */
1757 0xffff, /* dst_mask */
1758 FALSE), /* pcrel_offset */
1759
1760 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1761 with values (sym+add)@dtpmod and zero, and computes the offset to the
1762 first entry relative to the TOC base (r2). */
1763 HOWTO (R_PPC64_GOT_TLSLD16,
1764 0, /* rightshift */
1765 1, /* size (0 = byte, 1 = short, 2 = long) */
1766 16, /* bitsize */
1767 FALSE, /* pc_relative */
1768 0, /* bitpos */
1769 complain_overflow_signed, /* complain_on_overflow */
1770 ppc64_elf_unhandled_reloc, /* special_function */
1771 "R_PPC64_GOT_TLSLD16", /* name */
1772 FALSE, /* partial_inplace */
1773 0, /* src_mask */
1774 0xffff, /* dst_mask */
1775 FALSE), /* pcrel_offset */
1776
1777 /* Like GOT_TLSLD16, but no overflow. */
1778 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1779 0, /* rightshift */
1780 1, /* size (0 = byte, 1 = short, 2 = long) */
1781 16, /* bitsize */
1782 FALSE, /* pc_relative */
1783 0, /* bitpos */
1784 complain_overflow_dont, /* complain_on_overflow */
1785 ppc64_elf_unhandled_reloc, /* special_function */
1786 "R_PPC64_GOT_TLSLD16_LO", /* name */
1787 FALSE, /* partial_inplace */
1788 0, /* src_mask */
1789 0xffff, /* dst_mask */
1790 FALSE), /* pcrel_offset */
1791
1792 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1793 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1794 16, /* rightshift */
1795 1, /* size (0 = byte, 1 = short, 2 = long) */
1796 16, /* bitsize */
1797 FALSE, /* pc_relative */
1798 0, /* bitpos */
1799 complain_overflow_signed, /* complain_on_overflow */
1800 ppc64_elf_unhandled_reloc, /* special_function */
1801 "R_PPC64_GOT_TLSLD16_HI", /* name */
1802 FALSE, /* partial_inplace */
1803 0, /* src_mask */
1804 0xffff, /* dst_mask */
1805 FALSE), /* pcrel_offset */
1806
1807 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1808 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1809 16, /* rightshift */
1810 1, /* size (0 = byte, 1 = short, 2 = long) */
1811 16, /* bitsize */
1812 FALSE, /* pc_relative */
1813 0, /* bitpos */
1814 complain_overflow_signed, /* complain_on_overflow */
1815 ppc64_elf_unhandled_reloc, /* special_function */
1816 "R_PPC64_GOT_TLSLD16_HA", /* name */
1817 FALSE, /* partial_inplace */
1818 0, /* src_mask */
1819 0xffff, /* dst_mask */
1820 FALSE), /* pcrel_offset */
1821
1822 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1823 the offset to the entry relative to the TOC base (r2). */
1824 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1825 0, /* rightshift */
1826 1, /* size (0 = byte, 1 = short, 2 = long) */
1827 16, /* bitsize */
1828 FALSE, /* pc_relative */
1829 0, /* bitpos */
1830 complain_overflow_signed, /* complain_on_overflow */
1831 ppc64_elf_unhandled_reloc, /* special_function */
1832 "R_PPC64_GOT_DTPREL16_DS", /* name */
1833 FALSE, /* partial_inplace */
1834 0, /* src_mask */
1835 0xfffc, /* dst_mask */
1836 FALSE), /* pcrel_offset */
1837
1838 /* Like GOT_DTPREL16_DS, but no overflow. */
1839 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1840 0, /* rightshift */
1841 1, /* size (0 = byte, 1 = short, 2 = long) */
1842 16, /* bitsize */
1843 FALSE, /* pc_relative */
1844 0, /* bitpos */
1845 complain_overflow_dont, /* complain_on_overflow */
1846 ppc64_elf_unhandled_reloc, /* special_function */
1847 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1848 FALSE, /* partial_inplace */
1849 0, /* src_mask */
1850 0xfffc, /* dst_mask */
1851 FALSE), /* pcrel_offset */
1852
1853 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1854 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1855 16, /* rightshift */
1856 1, /* size (0 = byte, 1 = short, 2 = long) */
1857 16, /* bitsize */
1858 FALSE, /* pc_relative */
1859 0, /* bitpos */
1860 complain_overflow_signed, /* complain_on_overflow */
1861 ppc64_elf_unhandled_reloc, /* special_function */
1862 "R_PPC64_GOT_DTPREL16_HI", /* name */
1863 FALSE, /* partial_inplace */
1864 0, /* src_mask */
1865 0xffff, /* dst_mask */
1866 FALSE), /* pcrel_offset */
1867
1868 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1869 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1870 16, /* rightshift */
1871 1, /* size (0 = byte, 1 = short, 2 = long) */
1872 16, /* bitsize */
1873 FALSE, /* pc_relative */
1874 0, /* bitpos */
1875 complain_overflow_signed, /* complain_on_overflow */
1876 ppc64_elf_unhandled_reloc, /* special_function */
1877 "R_PPC64_GOT_DTPREL16_HA", /* name */
1878 FALSE, /* partial_inplace */
1879 0, /* src_mask */
1880 0xffff, /* dst_mask */
1881 FALSE), /* pcrel_offset */
1882
1883 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1884 offset to the entry relative to the TOC base (r2). */
1885 HOWTO (R_PPC64_GOT_TPREL16_DS,
1886 0, /* rightshift */
1887 1, /* size (0 = byte, 1 = short, 2 = long) */
1888 16, /* bitsize */
1889 FALSE, /* pc_relative */
1890 0, /* bitpos */
1891 complain_overflow_signed, /* complain_on_overflow */
1892 ppc64_elf_unhandled_reloc, /* special_function */
1893 "R_PPC64_GOT_TPREL16_DS", /* name */
1894 FALSE, /* partial_inplace */
1895 0, /* src_mask */
1896 0xfffc, /* dst_mask */
1897 FALSE), /* pcrel_offset */
1898
1899 /* Like GOT_TPREL16_DS, but no overflow. */
1900 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1901 0, /* rightshift */
1902 1, /* size (0 = byte, 1 = short, 2 = long) */
1903 16, /* bitsize */
1904 FALSE, /* pc_relative */
1905 0, /* bitpos */
1906 complain_overflow_dont, /* complain_on_overflow */
1907 ppc64_elf_unhandled_reloc, /* special_function */
1908 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1909 FALSE, /* partial_inplace */
1910 0, /* src_mask */
1911 0xfffc, /* dst_mask */
1912 FALSE), /* pcrel_offset */
1913
1914 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1915 HOWTO (R_PPC64_GOT_TPREL16_HI,
1916 16, /* rightshift */
1917 1, /* size (0 = byte, 1 = short, 2 = long) */
1918 16, /* bitsize */
1919 FALSE, /* pc_relative */
1920 0, /* bitpos */
1921 complain_overflow_signed, /* complain_on_overflow */
1922 ppc64_elf_unhandled_reloc, /* special_function */
1923 "R_PPC64_GOT_TPREL16_HI", /* name */
1924 FALSE, /* partial_inplace */
1925 0, /* src_mask */
1926 0xffff, /* dst_mask */
1927 FALSE), /* pcrel_offset */
1928
1929 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1930 HOWTO (R_PPC64_GOT_TPREL16_HA,
1931 16, /* rightshift */
1932 1, /* size (0 = byte, 1 = short, 2 = long) */
1933 16, /* bitsize */
1934 FALSE, /* pc_relative */
1935 0, /* bitpos */
1936 complain_overflow_signed, /* complain_on_overflow */
1937 ppc64_elf_unhandled_reloc, /* special_function */
1938 "R_PPC64_GOT_TPREL16_HA", /* name */
1939 FALSE, /* partial_inplace */
1940 0, /* src_mask */
1941 0xffff, /* dst_mask */
1942 FALSE), /* pcrel_offset */
1943
1944 HOWTO (R_PPC64_JMP_IREL, /* type */
1945 0, /* rightshift */
1946 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1947 0, /* bitsize */
1948 FALSE, /* pc_relative */
1949 0, /* bitpos */
1950 complain_overflow_dont, /* complain_on_overflow */
1951 ppc64_elf_unhandled_reloc, /* special_function */
1952 "R_PPC64_JMP_IREL", /* name */
1953 FALSE, /* partial_inplace */
1954 0, /* src_mask */
1955 0, /* dst_mask */
1956 FALSE), /* pcrel_offset */
1957
1958 HOWTO (R_PPC64_IRELATIVE, /* type */
1959 0, /* rightshift */
1960 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1961 64, /* bitsize */
1962 FALSE, /* pc_relative */
1963 0, /* bitpos */
1964 complain_overflow_dont, /* complain_on_overflow */
1965 bfd_elf_generic_reloc, /* special_function */
1966 "R_PPC64_IRELATIVE", /* name */
1967 FALSE, /* partial_inplace */
1968 0, /* src_mask */
1969 ONES (64), /* dst_mask */
1970 FALSE), /* pcrel_offset */
1971
1972 /* A 16 bit relative relocation. */
1973 HOWTO (R_PPC64_REL16, /* type */
1974 0, /* rightshift */
1975 1, /* size (0 = byte, 1 = short, 2 = long) */
1976 16, /* bitsize */
1977 TRUE, /* pc_relative */
1978 0, /* bitpos */
1979 complain_overflow_signed, /* complain_on_overflow */
1980 bfd_elf_generic_reloc, /* special_function */
1981 "R_PPC64_REL16", /* name */
1982 FALSE, /* partial_inplace */
1983 0, /* src_mask */
1984 0xffff, /* dst_mask */
1985 TRUE), /* pcrel_offset */
1986
1987 /* A 16 bit relative relocation without overflow. */
1988 HOWTO (R_PPC64_REL16_LO, /* type */
1989 0, /* rightshift */
1990 1, /* size (0 = byte, 1 = short, 2 = long) */
1991 16, /* bitsize */
1992 TRUE, /* pc_relative */
1993 0, /* bitpos */
1994 complain_overflow_dont,/* complain_on_overflow */
1995 bfd_elf_generic_reloc, /* special_function */
1996 "R_PPC64_REL16_LO", /* name */
1997 FALSE, /* partial_inplace */
1998 0, /* src_mask */
1999 0xffff, /* dst_mask */
2000 TRUE), /* pcrel_offset */
2001
2002 /* The high order 16 bits of a relative address. */
2003 HOWTO (R_PPC64_REL16_HI, /* type */
2004 16, /* rightshift */
2005 1, /* size (0 = byte, 1 = short, 2 = long) */
2006 16, /* bitsize */
2007 TRUE, /* pc_relative */
2008 0, /* bitpos */
2009 complain_overflow_signed, /* complain_on_overflow */
2010 bfd_elf_generic_reloc, /* special_function */
2011 "R_PPC64_REL16_HI", /* name */
2012 FALSE, /* partial_inplace */
2013 0, /* src_mask */
2014 0xffff, /* dst_mask */
2015 TRUE), /* pcrel_offset */
2016
2017 /* The high order 16 bits of a relative address, plus 1 if the contents of
2018 the low 16 bits, treated as a signed number, is negative. */
2019 HOWTO (R_PPC64_REL16_HA, /* type */
2020 16, /* rightshift */
2021 1, /* size (0 = byte, 1 = short, 2 = long) */
2022 16, /* bitsize */
2023 TRUE, /* pc_relative */
2024 0, /* bitpos */
2025 complain_overflow_signed, /* complain_on_overflow */
2026 ppc64_elf_ha_reloc, /* special_function */
2027 "R_PPC64_REL16_HA", /* name */
2028 FALSE, /* partial_inplace */
2029 0, /* src_mask */
2030 0xffff, /* dst_mask */
2031 TRUE), /* pcrel_offset */
2032
2033 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2034 HOWTO (R_PPC64_REL16DX_HA, /* type */
2035 16, /* rightshift */
2036 2, /* size (0 = byte, 1 = short, 2 = long) */
2037 16, /* bitsize */
2038 TRUE, /* pc_relative */
2039 0, /* bitpos */
2040 complain_overflow_signed, /* complain_on_overflow */
2041 ppc64_elf_ha_reloc, /* special_function */
2042 "R_PPC64_REL16DX_HA", /* name */
2043 FALSE, /* partial_inplace */
2044 0, /* src_mask */
2045 0x1fffc1, /* dst_mask */
2046 TRUE), /* pcrel_offset */
2047
2048 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
2049 HOWTO (R_PPC64_16DX_HA, /* type */
2050 16, /* rightshift */
2051 2, /* size (0 = byte, 1 = short, 2 = long) */
2052 16, /* bitsize */
2053 FALSE, /* pc_relative */
2054 0, /* bitpos */
2055 complain_overflow_signed, /* complain_on_overflow */
2056 ppc64_elf_ha_reloc, /* special_function */
2057 "R_PPC64_16DX_HA", /* name */
2058 FALSE, /* partial_inplace */
2059 0, /* src_mask */
2060 0x1fffc1, /* dst_mask */
2061 FALSE), /* pcrel_offset */
2062
2063 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2064 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2065 16, /* rightshift */
2066 1, /* size (0 = byte, 1 = short, 2 = long) */
2067 16, /* bitsize */
2068 FALSE, /* pc_relative */
2069 0, /* bitpos */
2070 complain_overflow_dont, /* complain_on_overflow */
2071 bfd_elf_generic_reloc, /* special_function */
2072 "R_PPC64_ADDR16_HIGH", /* name */
2073 FALSE, /* partial_inplace */
2074 0, /* src_mask */
2075 0xffff, /* dst_mask */
2076 FALSE), /* pcrel_offset */
2077
2078 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2079 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2080 16, /* rightshift */
2081 1, /* size (0 = byte, 1 = short, 2 = long) */
2082 16, /* bitsize */
2083 FALSE, /* pc_relative */
2084 0, /* bitpos */
2085 complain_overflow_dont, /* complain_on_overflow */
2086 ppc64_elf_ha_reloc, /* special_function */
2087 "R_PPC64_ADDR16_HIGHA", /* name */
2088 FALSE, /* partial_inplace */
2089 0, /* src_mask */
2090 0xffff, /* dst_mask */
2091 FALSE), /* pcrel_offset */
2092
2093 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2094 HOWTO (R_PPC64_DTPREL16_HIGH,
2095 16, /* rightshift */
2096 1, /* size (0 = byte, 1 = short, 2 = long) */
2097 16, /* bitsize */
2098 FALSE, /* pc_relative */
2099 0, /* bitpos */
2100 complain_overflow_dont, /* complain_on_overflow */
2101 ppc64_elf_unhandled_reloc, /* special_function */
2102 "R_PPC64_DTPREL16_HIGH", /* name */
2103 FALSE, /* partial_inplace */
2104 0, /* src_mask */
2105 0xffff, /* dst_mask */
2106 FALSE), /* pcrel_offset */
2107
2108 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2109 HOWTO (R_PPC64_DTPREL16_HIGHA,
2110 16, /* rightshift */
2111 1, /* size (0 = byte, 1 = short, 2 = long) */
2112 16, /* bitsize */
2113 FALSE, /* pc_relative */
2114 0, /* bitpos */
2115 complain_overflow_dont, /* complain_on_overflow */
2116 ppc64_elf_unhandled_reloc, /* special_function */
2117 "R_PPC64_DTPREL16_HIGHA", /* name */
2118 FALSE, /* partial_inplace */
2119 0, /* src_mask */
2120 0xffff, /* dst_mask */
2121 FALSE), /* pcrel_offset */
2122
2123 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2124 HOWTO (R_PPC64_TPREL16_HIGH,
2125 16, /* rightshift */
2126 1, /* size (0 = byte, 1 = short, 2 = long) */
2127 16, /* bitsize */
2128 FALSE, /* pc_relative */
2129 0, /* bitpos */
2130 complain_overflow_dont, /* complain_on_overflow */
2131 ppc64_elf_unhandled_reloc, /* special_function */
2132 "R_PPC64_TPREL16_HIGH", /* name */
2133 FALSE, /* partial_inplace */
2134 0, /* src_mask */
2135 0xffff, /* dst_mask */
2136 FALSE), /* pcrel_offset */
2137
2138 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2139 HOWTO (R_PPC64_TPREL16_HIGHA,
2140 16, /* rightshift */
2141 1, /* size (0 = byte, 1 = short, 2 = long) */
2142 16, /* bitsize */
2143 FALSE, /* pc_relative */
2144 0, /* bitpos */
2145 complain_overflow_dont, /* complain_on_overflow */
2146 ppc64_elf_unhandled_reloc, /* special_function */
2147 "R_PPC64_TPREL16_HIGHA", /* name */
2148 FALSE, /* partial_inplace */
2149 0, /* src_mask */
2150 0xffff, /* dst_mask */
2151 FALSE), /* pcrel_offset */
2152
2153 /* Marker reloc on ELFv2 large-model function entry. */
2154 HOWTO (R_PPC64_ENTRY,
2155 0, /* rightshift */
2156 2, /* size (0 = byte, 1 = short, 2 = long) */
2157 32, /* bitsize */
2158 FALSE, /* pc_relative */
2159 0, /* bitpos */
2160 complain_overflow_dont, /* complain_on_overflow */
2161 bfd_elf_generic_reloc, /* special_function */
2162 "R_PPC64_ENTRY", /* name */
2163 FALSE, /* partial_inplace */
2164 0, /* src_mask */
2165 0, /* dst_mask */
2166 FALSE), /* pcrel_offset */
2167
2168 /* Like ADDR64, but use local entry point of function. */
2169 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2170 0, /* rightshift */
2171 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2172 64, /* bitsize */
2173 FALSE, /* pc_relative */
2174 0, /* bitpos */
2175 complain_overflow_dont, /* complain_on_overflow */
2176 bfd_elf_generic_reloc, /* special_function */
2177 "R_PPC64_ADDR64_LOCAL", /* name */
2178 FALSE, /* partial_inplace */
2179 0, /* src_mask */
2180 ONES (64), /* dst_mask */
2181 FALSE), /* pcrel_offset */
2182
2183 /* GNU extension to record C++ vtable hierarchy. */
2184 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2185 0, /* rightshift */
2186 0, /* size (0 = byte, 1 = short, 2 = long) */
2187 0, /* bitsize */
2188 FALSE, /* pc_relative */
2189 0, /* bitpos */
2190 complain_overflow_dont, /* complain_on_overflow */
2191 NULL, /* special_function */
2192 "R_PPC64_GNU_VTINHERIT", /* name */
2193 FALSE, /* partial_inplace */
2194 0, /* src_mask */
2195 0, /* dst_mask */
2196 FALSE), /* pcrel_offset */
2197
2198 /* GNU extension to record C++ vtable member usage. */
2199 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2200 0, /* rightshift */
2201 0, /* size (0 = byte, 1 = short, 2 = long) */
2202 0, /* bitsize */
2203 FALSE, /* pc_relative */
2204 0, /* bitpos */
2205 complain_overflow_dont, /* complain_on_overflow */
2206 NULL, /* special_function */
2207 "R_PPC64_GNU_VTENTRY", /* name */
2208 FALSE, /* partial_inplace */
2209 0, /* src_mask */
2210 0, /* dst_mask */
2211 FALSE), /* pcrel_offset */
2212 };
2213
2214 \f
2215 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2216 be done. */
2217
2218 static void
2219 ppc_howto_init (void)
2220 {
2221 unsigned int i, type;
2222
2223 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2224 {
2225 type = ppc64_elf_howto_raw[i].type;
2226 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2227 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2228 }
2229 }
2230
2231 static reloc_howto_type *
2232 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2233 bfd_reloc_code_real_type code)
2234 {
2235 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2236
2237 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2238 /* Initialize howto table if needed. */
2239 ppc_howto_init ();
2240
2241 switch (code)
2242 {
2243 default:
2244 return NULL;
2245
2246 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2247 break;
2248 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2249 break;
2250 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2251 break;
2252 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2253 break;
2254 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2255 break;
2256 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2257 break;
2258 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2259 break;
2260 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2261 break;
2262 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2263 break;
2264 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2265 break;
2266 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2267 break;
2268 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2269 break;
2270 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2271 break;
2272 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2273 break;
2274 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2275 break;
2276 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2277 break;
2278 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2279 break;
2280 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2281 break;
2282 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2283 break;
2284 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2285 break;
2286 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2287 break;
2288 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2289 break;
2290 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2291 break;
2292 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2293 break;
2294 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2295 break;
2296 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2297 break;
2298 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2299 break;
2300 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2301 break;
2302 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2303 break;
2304 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2305 break;
2306 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2307 break;
2308 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2309 break;
2310 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2311 break;
2312 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2313 break;
2314 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2315 break;
2316 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2317 break;
2318 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2319 break;
2320 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2321 break;
2322 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2323 break;
2324 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2325 break;
2326 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2327 break;
2328 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2329 break;
2330 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2331 break;
2332 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2333 break;
2334 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2335 break;
2336 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2337 break;
2338 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2339 break;
2340 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2341 break;
2342 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2343 break;
2344 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2345 break;
2346 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2347 break;
2348 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2349 break;
2350 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2351 break;
2352 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2353 break;
2354 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2355 break;
2356 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2357 break;
2358 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2359 break;
2360 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2361 break;
2362 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2363 break;
2364 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2365 break;
2366 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2367 break;
2368 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2369 break;
2370 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2371 break;
2372 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2373 break;
2374 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2375 break;
2376 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2377 break;
2378 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2379 break;
2380 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2381 break;
2382 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2383 break;
2384 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2385 break;
2386 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2387 break;
2388 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2389 break;
2390 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2391 break;
2392 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2393 break;
2394 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2395 break;
2396 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2397 break;
2398 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2399 break;
2400 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2401 break;
2402 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2403 break;
2404 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2405 break;
2406 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2407 break;
2408 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2409 break;
2410 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2411 break;
2412 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2413 break;
2414 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2415 break;
2416 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2417 break;
2418 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2419 break;
2420 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2421 break;
2422 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2423 break;
2424 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2425 break;
2426 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2427 break;
2428 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2429 break;
2430 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2431 break;
2432 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2433 break;
2434 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2435 break;
2436 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2437 break;
2438 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2439 break;
2440 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2441 break;
2442 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2443 break;
2444 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2445 break;
2446 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2447 break;
2448 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2449 break;
2450 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2451 break;
2452 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2453 break;
2454 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2455 break;
2456 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2457 break;
2458 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2459 break;
2460 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2461 break;
2462 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2463 break;
2464 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2465 break;
2466 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2467 break;
2468 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2469 break;
2470 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2471 break;
2472 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2473 break;
2474 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2475 break;
2476 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2477 break;
2478 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2479 break;
2480 }
2481
2482 return ppc64_elf_howto_table[r];
2483 };
2484
2485 static reloc_howto_type *
2486 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2487 const char *r_name)
2488 {
2489 unsigned int i;
2490
2491 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2492 if (ppc64_elf_howto_raw[i].name != NULL
2493 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2494 return &ppc64_elf_howto_raw[i];
2495
2496 return NULL;
2497 }
2498
2499 /* Set the howto pointer for a PowerPC ELF reloc. */
2500
2501 static void
2502 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
2503 Elf_Internal_Rela *dst)
2504 {
2505 unsigned int type;
2506
2507 /* Initialize howto table if needed. */
2508 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2509 ppc_howto_init ();
2510
2511 type = ELF64_R_TYPE (dst->r_info);
2512 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2513 {
2514 /* xgettext:c-format */
2515 _bfd_error_handler (_("%B: invalid relocation type %d"),
2516 abfd, (int) type);
2517 type = R_PPC64_NONE;
2518 }
2519 cache_ptr->howto = ppc64_elf_howto_table[type];
2520 }
2521
2522 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2523
2524 static bfd_reloc_status_type
2525 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2526 void *data, asection *input_section,
2527 bfd *output_bfd, char **error_message)
2528 {
2529 enum elf_ppc64_reloc_type r_type;
2530 long insn;
2531 bfd_size_type octets;
2532 bfd_vma value;
2533
2534 /* If this is a relocatable link (output_bfd test tells us), just
2535 call the generic function. Any adjustment will be done at final
2536 link time. */
2537 if (output_bfd != NULL)
2538 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2539 input_section, output_bfd, error_message);
2540
2541 /* Adjust the addend for sign extension of the low 16 bits.
2542 We won't actually be using the low 16 bits, so trashing them
2543 doesn't matter. */
2544 reloc_entry->addend += 0x8000;
2545 r_type = reloc_entry->howto->type;
2546 if (r_type != R_PPC64_REL16DX_HA)
2547 return bfd_reloc_continue;
2548
2549 value = 0;
2550 if (!bfd_is_com_section (symbol->section))
2551 value = symbol->value;
2552 value += (reloc_entry->addend
2553 + symbol->section->output_offset
2554 + symbol->section->output_section->vma);
2555 value -= (reloc_entry->address
2556 + input_section->output_offset
2557 + input_section->output_section->vma);
2558 value = (bfd_signed_vma) value >> 16;
2559
2560 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2561 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2562 insn &= ~0x1fffc1;
2563 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2564 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2565 if (value + 0x8000 > 0xffff)
2566 return bfd_reloc_overflow;
2567 return bfd_reloc_ok;
2568 }
2569
2570 static bfd_reloc_status_type
2571 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2572 void *data, asection *input_section,
2573 bfd *output_bfd, char **error_message)
2574 {
2575 if (output_bfd != NULL)
2576 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2577 input_section, output_bfd, error_message);
2578
2579 if (strcmp (symbol->section->name, ".opd") == 0
2580 && (symbol->section->owner->flags & DYNAMIC) == 0)
2581 {
2582 bfd_vma dest = opd_entry_value (symbol->section,
2583 symbol->value + reloc_entry->addend,
2584 NULL, NULL, FALSE);
2585 if (dest != (bfd_vma) -1)
2586 reloc_entry->addend = dest - (symbol->value
2587 + symbol->section->output_section->vma
2588 + symbol->section->output_offset);
2589 }
2590 else
2591 {
2592 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2593
2594 if (symbol->section->owner != abfd
2595 && symbol->section->owner != NULL
2596 && abiversion (symbol->section->owner) >= 2)
2597 {
2598 unsigned int i;
2599
2600 for (i = 0; i < symbol->section->owner->symcount; ++i)
2601 {
2602 asymbol *symdef = symbol->section->owner->outsymbols[i];
2603
2604 if (strcmp (symdef->name, symbol->name) == 0)
2605 {
2606 elfsym = (elf_symbol_type *) symdef;
2607 break;
2608 }
2609 }
2610 }
2611 reloc_entry->addend
2612 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2613 }
2614 return bfd_reloc_continue;
2615 }
2616
2617 static bfd_reloc_status_type
2618 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2619 void *data, asection *input_section,
2620 bfd *output_bfd, char **error_message)
2621 {
2622 long insn;
2623 enum elf_ppc64_reloc_type r_type;
2624 bfd_size_type octets;
2625 /* Assume 'at' branch hints. */
2626 bfd_boolean is_isa_v2 = TRUE;
2627
2628 /* If this is a relocatable link (output_bfd test tells us), just
2629 call the generic function. Any adjustment will be done at final
2630 link time. */
2631 if (output_bfd != NULL)
2632 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2633 input_section, output_bfd, error_message);
2634
2635 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2636 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2637 insn &= ~(0x01 << 21);
2638 r_type = reloc_entry->howto->type;
2639 if (r_type == R_PPC64_ADDR14_BRTAKEN
2640 || r_type == R_PPC64_REL14_BRTAKEN)
2641 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2642
2643 if (is_isa_v2)
2644 {
2645 /* Set 'a' bit. This is 0b00010 in BO field for branch
2646 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2647 for branch on CTR insns (BO == 1a00t or 1a01t). */
2648 if ((insn & (0x14 << 21)) == (0x04 << 21))
2649 insn |= 0x02 << 21;
2650 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2651 insn |= 0x08 << 21;
2652 else
2653 goto out;
2654 }
2655 else
2656 {
2657 bfd_vma target = 0;
2658 bfd_vma from;
2659
2660 if (!bfd_is_com_section (symbol->section))
2661 target = symbol->value;
2662 target += symbol->section->output_section->vma;
2663 target += symbol->section->output_offset;
2664 target += reloc_entry->addend;
2665
2666 from = (reloc_entry->address
2667 + input_section->output_offset
2668 + input_section->output_section->vma);
2669
2670 /* Invert 'y' bit if not the default. */
2671 if ((bfd_signed_vma) (target - from) < 0)
2672 insn ^= 0x01 << 21;
2673 }
2674 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2675 out:
2676 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2677 input_section, output_bfd, error_message);
2678 }
2679
2680 static bfd_reloc_status_type
2681 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2682 void *data, asection *input_section,
2683 bfd *output_bfd, char **error_message)
2684 {
2685 /* If this is a relocatable link (output_bfd test tells us), just
2686 call the generic function. Any adjustment will be done at final
2687 link time. */
2688 if (output_bfd != NULL)
2689 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2690 input_section, output_bfd, error_message);
2691
2692 /* Subtract the symbol section base address. */
2693 reloc_entry->addend -= symbol->section->output_section->vma;
2694 return bfd_reloc_continue;
2695 }
2696
2697 static bfd_reloc_status_type
2698 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2699 void *data, asection *input_section,
2700 bfd *output_bfd, char **error_message)
2701 {
2702 /* If this is a relocatable link (output_bfd test tells us), just
2703 call the generic function. Any adjustment will be done at final
2704 link time. */
2705 if (output_bfd != NULL)
2706 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2707 input_section, output_bfd, error_message);
2708
2709 /* Subtract the symbol section base address. */
2710 reloc_entry->addend -= symbol->section->output_section->vma;
2711
2712 /* Adjust the addend for sign extension of the low 16 bits. */
2713 reloc_entry->addend += 0x8000;
2714 return bfd_reloc_continue;
2715 }
2716
2717 static bfd_reloc_status_type
2718 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2719 void *data, asection *input_section,
2720 bfd *output_bfd, char **error_message)
2721 {
2722 bfd_vma TOCstart;
2723
2724 /* If this is a relocatable link (output_bfd test tells us), just
2725 call the generic function. Any adjustment will be done at final
2726 link time. */
2727 if (output_bfd != NULL)
2728 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2729 input_section, output_bfd, error_message);
2730
2731 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2732 if (TOCstart == 0)
2733 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2734
2735 /* Subtract the TOC base address. */
2736 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2737 return bfd_reloc_continue;
2738 }
2739
2740 static bfd_reloc_status_type
2741 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2742 void *data, asection *input_section,
2743 bfd *output_bfd, char **error_message)
2744 {
2745 bfd_vma TOCstart;
2746
2747 /* If this is a relocatable link (output_bfd test tells us), just
2748 call the generic function. Any adjustment will be done at final
2749 link time. */
2750 if (output_bfd != NULL)
2751 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2752 input_section, output_bfd, error_message);
2753
2754 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2755 if (TOCstart == 0)
2756 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2757
2758 /* Subtract the TOC base address. */
2759 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2760
2761 /* Adjust the addend for sign extension of the low 16 bits. */
2762 reloc_entry->addend += 0x8000;
2763 return bfd_reloc_continue;
2764 }
2765
2766 static bfd_reloc_status_type
2767 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2768 void *data, asection *input_section,
2769 bfd *output_bfd, char **error_message)
2770 {
2771 bfd_vma TOCstart;
2772 bfd_size_type octets;
2773
2774 /* If this is a relocatable link (output_bfd test tells us), just
2775 call the generic function. Any adjustment will be done at final
2776 link time. */
2777 if (output_bfd != NULL)
2778 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2779 input_section, output_bfd, error_message);
2780
2781 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2782 if (TOCstart == 0)
2783 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2784
2785 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2786 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2787 return bfd_reloc_ok;
2788 }
2789
2790 static bfd_reloc_status_type
2791 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2792 void *data, asection *input_section,
2793 bfd *output_bfd, char **error_message)
2794 {
2795 /* If this is a relocatable link (output_bfd test tells us), just
2796 call the generic function. Any adjustment will be done at final
2797 link time. */
2798 if (output_bfd != NULL)
2799 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2800 input_section, output_bfd, error_message);
2801
2802 if (error_message != NULL)
2803 {
2804 static char buf[60];
2805 sprintf (buf, "generic linker can't handle %s",
2806 reloc_entry->howto->name);
2807 *error_message = buf;
2808 }
2809 return bfd_reloc_dangerous;
2810 }
2811
2812 /* Track GOT entries needed for a given symbol. We might need more
2813 than one got entry per symbol. */
2814 struct got_entry
2815 {
2816 struct got_entry *next;
2817
2818 /* The symbol addend that we'll be placing in the GOT. */
2819 bfd_vma addend;
2820
2821 /* Unlike other ELF targets, we use separate GOT entries for the same
2822 symbol referenced from different input files. This is to support
2823 automatic multiple TOC/GOT sections, where the TOC base can vary
2824 from one input file to another. After partitioning into TOC groups
2825 we merge entries within the group.
2826
2827 Point to the BFD owning this GOT entry. */
2828 bfd *owner;
2829
2830 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2831 TLS_TPREL or TLS_DTPREL for tls entries. */
2832 unsigned char tls_type;
2833
2834 /* Non-zero if got.ent points to real entry. */
2835 unsigned char is_indirect;
2836
2837 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2838 union
2839 {
2840 bfd_signed_vma refcount;
2841 bfd_vma offset;
2842 struct got_entry *ent;
2843 } got;
2844 };
2845
2846 /* The same for PLT. */
2847 struct plt_entry
2848 {
2849 struct plt_entry *next;
2850
2851 bfd_vma addend;
2852
2853 union
2854 {
2855 bfd_signed_vma refcount;
2856 bfd_vma offset;
2857 } plt;
2858 };
2859
2860 struct ppc64_elf_obj_tdata
2861 {
2862 struct elf_obj_tdata elf;
2863
2864 /* Shortcuts to dynamic linker sections. */
2865 asection *got;
2866 asection *relgot;
2867
2868 /* Used during garbage collection. We attach global symbols defined
2869 on removed .opd entries to this section so that the sym is removed. */
2870 asection *deleted_section;
2871
2872 /* TLS local dynamic got entry handling. Support for multiple GOT
2873 sections means we potentially need one of these for each input bfd. */
2874 struct got_entry tlsld_got;
2875
2876 union {
2877 /* A copy of relocs before they are modified for --emit-relocs. */
2878 Elf_Internal_Rela *relocs;
2879
2880 /* Section contents. */
2881 bfd_byte *contents;
2882 } opd;
2883
2884 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2885 the reloc to be in the range -32768 to 32767. */
2886 unsigned int has_small_toc_reloc : 1;
2887
2888 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2889 instruction not one we handle. */
2890 unsigned int unexpected_toc_insn : 1;
2891 };
2892
2893 #define ppc64_elf_tdata(bfd) \
2894 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2895
2896 #define ppc64_tlsld_got(bfd) \
2897 (&ppc64_elf_tdata (bfd)->tlsld_got)
2898
2899 #define is_ppc64_elf(bfd) \
2900 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2901 && elf_object_id (bfd) == PPC64_ELF_DATA)
2902
2903 /* Override the generic function because we store some extras. */
2904
2905 static bfd_boolean
2906 ppc64_elf_mkobject (bfd *abfd)
2907 {
2908 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2909 PPC64_ELF_DATA);
2910 }
2911
2912 /* Fix bad default arch selected for a 64 bit input bfd when the
2913 default is 32 bit. Also select arch based on apuinfo. */
2914
2915 static bfd_boolean
2916 ppc64_elf_object_p (bfd *abfd)
2917 {
2918 if (!abfd->arch_info->the_default)
2919 return TRUE;
2920
2921 if (abfd->arch_info->bits_per_word == 32)
2922 {
2923 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2924
2925 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2926 {
2927 /* Relies on arch after 32 bit default being 64 bit default. */
2928 abfd->arch_info = abfd->arch_info->next;
2929 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2930 }
2931 }
2932 return _bfd_elf_ppc_set_arch (abfd);
2933 }
2934
2935 /* Support for core dump NOTE sections. */
2936
2937 static bfd_boolean
2938 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2939 {
2940 size_t offset, size;
2941
2942 if (note->descsz != 504)
2943 return FALSE;
2944
2945 /* pr_cursig */
2946 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2947
2948 /* pr_pid */
2949 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2950
2951 /* pr_reg */
2952 offset = 112;
2953 size = 384;
2954
2955 /* Make a ".reg/999" section. */
2956 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2957 size, note->descpos + offset);
2958 }
2959
2960 static bfd_boolean
2961 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2962 {
2963 if (note->descsz != 136)
2964 return FALSE;
2965
2966 elf_tdata (abfd)->core->pid
2967 = bfd_get_32 (abfd, note->descdata + 24);
2968 elf_tdata (abfd)->core->program
2969 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2970 elf_tdata (abfd)->core->command
2971 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2972
2973 return TRUE;
2974 }
2975
2976 static char *
2977 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2978 ...)
2979 {
2980 switch (note_type)
2981 {
2982 default:
2983 return NULL;
2984
2985 case NT_PRPSINFO:
2986 {
2987 char data[136];
2988 va_list ap;
2989
2990 va_start (ap, note_type);
2991 memset (data, 0, sizeof (data));
2992 strncpy (data + 40, va_arg (ap, const char *), 16);
2993 strncpy (data + 56, va_arg (ap, const char *), 80);
2994 va_end (ap);
2995 return elfcore_write_note (abfd, buf, bufsiz,
2996 "CORE", note_type, data, sizeof (data));
2997 }
2998
2999 case NT_PRSTATUS:
3000 {
3001 char data[504];
3002 va_list ap;
3003 long pid;
3004 int cursig;
3005 const void *greg;
3006
3007 va_start (ap, note_type);
3008 memset (data, 0, 112);
3009 pid = va_arg (ap, long);
3010 bfd_put_32 (abfd, pid, data + 32);
3011 cursig = va_arg (ap, int);
3012 bfd_put_16 (abfd, cursig, data + 12);
3013 greg = va_arg (ap, const void *);
3014 memcpy (data + 112, greg, 384);
3015 memset (data + 496, 0, 8);
3016 va_end (ap);
3017 return elfcore_write_note (abfd, buf, bufsiz,
3018 "CORE", note_type, data, sizeof (data));
3019 }
3020 }
3021 }
3022
3023 /* Add extra PPC sections. */
3024
3025 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3026 {
3027 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3028 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3029 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3030 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3031 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3032 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3033 { NULL, 0, 0, 0, 0 }
3034 };
3035
3036 enum _ppc64_sec_type {
3037 sec_normal = 0,
3038 sec_opd = 1,
3039 sec_toc = 2
3040 };
3041
3042 struct _ppc64_elf_section_data
3043 {
3044 struct bfd_elf_section_data elf;
3045
3046 union
3047 {
3048 /* An array with one entry for each opd function descriptor,
3049 and some spares since opd entries may be either 16 or 24 bytes. */
3050 #define OPD_NDX(OFF) ((OFF) >> 4)
3051 struct _opd_sec_data
3052 {
3053 /* Points to the function code section for local opd entries. */
3054 asection **func_sec;
3055
3056 /* After editing .opd, adjust references to opd local syms. */
3057 long *adjust;
3058 } opd;
3059
3060 /* An array for toc sections, indexed by offset/8. */
3061 struct _toc_sec_data
3062 {
3063 /* Specifies the relocation symbol index used at a given toc offset. */
3064 unsigned *symndx;
3065
3066 /* And the relocation addend. */
3067 bfd_vma *add;
3068 } toc;
3069 } u;
3070
3071 enum _ppc64_sec_type sec_type:2;
3072
3073 /* Flag set when small branches are detected. Used to
3074 select suitable defaults for the stub group size. */
3075 unsigned int has_14bit_branch:1;
3076 };
3077
3078 #define ppc64_elf_section_data(sec) \
3079 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3080
3081 static bfd_boolean
3082 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3083 {
3084 if (!sec->used_by_bfd)
3085 {
3086 struct _ppc64_elf_section_data *sdata;
3087 bfd_size_type amt = sizeof (*sdata);
3088
3089 sdata = bfd_zalloc (abfd, amt);
3090 if (sdata == NULL)
3091 return FALSE;
3092 sec->used_by_bfd = sdata;
3093 }
3094
3095 return _bfd_elf_new_section_hook (abfd, sec);
3096 }
3097
3098 static struct _opd_sec_data *
3099 get_opd_info (asection * sec)
3100 {
3101 if (sec != NULL
3102 && ppc64_elf_section_data (sec) != NULL
3103 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3104 return &ppc64_elf_section_data (sec)->u.opd;
3105 return NULL;
3106 }
3107 \f
3108 /* Parameters for the qsort hook. */
3109 static bfd_boolean synthetic_relocatable;
3110 static asection *synthetic_opd;
3111
3112 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
3113
3114 static int
3115 compare_symbols (const void *ap, const void *bp)
3116 {
3117 const asymbol *a = * (const asymbol **) ap;
3118 const asymbol *b = * (const asymbol **) bp;
3119
3120 /* Section symbols first. */
3121 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3122 return -1;
3123 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3124 return 1;
3125
3126 /* then .opd symbols. */
3127 if (synthetic_opd != NULL)
3128 {
3129 if (strcmp (a->section->name, ".opd") == 0
3130 && strcmp (b->section->name, ".opd") != 0)
3131 return -1;
3132 if (strcmp (a->section->name, ".opd") != 0
3133 && strcmp (b->section->name, ".opd") == 0)
3134 return 1;
3135 }
3136
3137 /* then other code symbols. */
3138 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3139 == (SEC_CODE | SEC_ALLOC)
3140 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3141 != (SEC_CODE | SEC_ALLOC))
3142 return -1;
3143
3144 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3145 != (SEC_CODE | SEC_ALLOC)
3146 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3147 == (SEC_CODE | SEC_ALLOC))
3148 return 1;
3149
3150 if (synthetic_relocatable)
3151 {
3152 if (a->section->id < b->section->id)
3153 return -1;
3154
3155 if (a->section->id > b->section->id)
3156 return 1;
3157 }
3158
3159 if (a->value + a->section->vma < b->value + b->section->vma)
3160 return -1;
3161
3162 if (a->value + a->section->vma > b->value + b->section->vma)
3163 return 1;
3164
3165 /* For syms with the same value, prefer strong dynamic global function
3166 syms over other syms. */
3167 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3168 return -1;
3169
3170 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3171 return 1;
3172
3173 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3174 return -1;
3175
3176 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3177 return 1;
3178
3179 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3180 return -1;
3181
3182 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3183 return 1;
3184
3185 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3186 return -1;
3187
3188 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3189 return 1;
3190
3191 return a > b;
3192 }
3193
3194 /* Search SYMS for a symbol of the given VALUE. */
3195
3196 static asymbol *
3197 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3198 {
3199 long mid;
3200
3201 if (id == (unsigned) -1)
3202 {
3203 while (lo < hi)
3204 {
3205 mid = (lo + hi) >> 1;
3206 if (syms[mid]->value + syms[mid]->section->vma < value)
3207 lo = mid + 1;
3208 else if (syms[mid]->value + syms[mid]->section->vma > value)
3209 hi = mid;
3210 else
3211 return syms[mid];
3212 }
3213 }
3214 else
3215 {
3216 while (lo < hi)
3217 {
3218 mid = (lo + hi) >> 1;
3219 if (syms[mid]->section->id < id)
3220 lo = mid + 1;
3221 else if (syms[mid]->section->id > id)
3222 hi = mid;
3223 else if (syms[mid]->value < value)
3224 lo = mid + 1;
3225 else if (syms[mid]->value > value)
3226 hi = mid;
3227 else
3228 return syms[mid];
3229 }
3230 }
3231 return NULL;
3232 }
3233
3234 static bfd_boolean
3235 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3236 {
3237 bfd_vma vma = *(bfd_vma *) ptr;
3238 return ((section->flags & SEC_ALLOC) != 0
3239 && section->vma <= vma
3240 && vma < section->vma + section->size);
3241 }
3242
3243 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3244 entry syms. Also generate @plt symbols for the glink branch table.
3245 Returns count of synthetic symbols in RET or -1 on error. */
3246
3247 static long
3248 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3249 long static_count, asymbol **static_syms,
3250 long dyn_count, asymbol **dyn_syms,
3251 asymbol **ret)
3252 {
3253 asymbol *s;
3254 long i;
3255 long count;
3256 char *names;
3257 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3258 asection *opd = NULL;
3259 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3260 asymbol **syms;
3261 int abi = abiversion (abfd);
3262
3263 *ret = NULL;
3264
3265 if (abi < 2)
3266 {
3267 opd = bfd_get_section_by_name (abfd, ".opd");
3268 if (opd == NULL && abi == 1)
3269 return 0;
3270 }
3271
3272 syms = NULL;
3273 codesecsym = 0;
3274 codesecsymend = 0;
3275 secsymend = 0;
3276 opdsymend = 0;
3277 symcount = 0;
3278 if (opd != NULL)
3279 {
3280 symcount = static_count;
3281 if (!relocatable)
3282 symcount += dyn_count;
3283 if (symcount == 0)
3284 return 0;
3285
3286 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3287 if (syms == NULL)
3288 return -1;
3289
3290 if (!relocatable && static_count != 0 && dyn_count != 0)
3291 {
3292 /* Use both symbol tables. */
3293 memcpy (syms, static_syms, static_count * sizeof (*syms));
3294 memcpy (syms + static_count, dyn_syms,
3295 (dyn_count + 1) * sizeof (*syms));
3296 }
3297 else if (!relocatable && static_count == 0)
3298 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3299 else
3300 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3301
3302 synthetic_relocatable = relocatable;
3303 synthetic_opd = opd;
3304 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3305
3306 if (!relocatable && symcount > 1)
3307 {
3308 long j;
3309 /* Trim duplicate syms, since we may have merged the normal and
3310 dynamic symbols. Actually, we only care about syms that have
3311 different values, so trim any with the same value. */
3312 for (i = 1, j = 1; i < symcount; ++i)
3313 if (syms[i - 1]->value + syms[i - 1]->section->vma
3314 != syms[i]->value + syms[i]->section->vma)
3315 syms[j++] = syms[i];
3316 symcount = j;
3317 }
3318
3319 i = 0;
3320 /* Note that here and in compare_symbols we can't compare opd and
3321 sym->section directly. With separate debug info files, the
3322 symbols will be extracted from the debug file while abfd passed
3323 to this function is the real binary. */
3324 if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
3325 ++i;
3326 codesecsym = i;
3327
3328 for (; i < symcount; ++i)
3329 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3330 | SEC_THREAD_LOCAL))
3331 != (SEC_CODE | SEC_ALLOC))
3332 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3333 break;
3334 codesecsymend = i;
3335
3336 for (; i < symcount; ++i)
3337 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3338 break;
3339 secsymend = i;
3340
3341 for (; i < symcount; ++i)
3342 if (strcmp (syms[i]->section->name, ".opd") != 0)
3343 break;
3344 opdsymend = i;
3345
3346 for (; i < symcount; ++i)
3347 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3348 != (SEC_CODE | SEC_ALLOC))
3349 break;
3350 symcount = i;
3351 }
3352 count = 0;
3353
3354 if (relocatable)
3355 {
3356 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3357 arelent *r;
3358 size_t size;
3359 long relcount;
3360
3361 if (opdsymend == secsymend)
3362 goto done;
3363
3364 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3365 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3366 if (relcount == 0)
3367 goto done;
3368
3369 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3370 {
3371 count = -1;
3372 goto done;
3373 }
3374
3375 size = 0;
3376 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3377 {
3378 asymbol *sym;
3379
3380 while (r < opd->relocation + relcount
3381 && r->address < syms[i]->value + opd->vma)
3382 ++r;
3383
3384 if (r == opd->relocation + relcount)
3385 break;
3386
3387 if (r->address != syms[i]->value + opd->vma)
3388 continue;
3389
3390 if (r->howto->type != R_PPC64_ADDR64)
3391 continue;
3392
3393 sym = *r->sym_ptr_ptr;
3394 if (!sym_exists_at (syms, opdsymend, symcount,
3395 sym->section->id, sym->value + r->addend))
3396 {
3397 ++count;
3398 size += sizeof (asymbol);
3399 size += strlen (syms[i]->name) + 2;
3400 }
3401 }
3402
3403 if (size == 0)
3404 goto done;
3405 s = *ret = bfd_malloc (size);
3406 if (s == NULL)
3407 {
3408 count = -1;
3409 goto done;
3410 }
3411
3412 names = (char *) (s + count);
3413
3414 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3415 {
3416 asymbol *sym;
3417
3418 while (r < opd->relocation + relcount
3419 && r->address < syms[i]->value + opd->vma)
3420 ++r;
3421
3422 if (r == opd->relocation + relcount)
3423 break;
3424
3425 if (r->address != syms[i]->value + opd->vma)
3426 continue;
3427
3428 if (r->howto->type != R_PPC64_ADDR64)
3429 continue;
3430
3431 sym = *r->sym_ptr_ptr;
3432 if (!sym_exists_at (syms, opdsymend, symcount,
3433 sym->section->id, sym->value + r->addend))
3434 {
3435 size_t len;
3436
3437 *s = *syms[i];
3438 s->flags |= BSF_SYNTHETIC;
3439 s->section = sym->section;
3440 s->value = sym->value + r->addend;
3441 s->name = names;
3442 *names++ = '.';
3443 len = strlen (syms[i]->name);
3444 memcpy (names, syms[i]->name, len + 1);
3445 names += len + 1;
3446 /* Have udata.p point back to the original symbol this
3447 synthetic symbol was derived from. */
3448 s->udata.p = syms[i];
3449 s++;
3450 }
3451 }
3452 }
3453 else
3454 {
3455 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3456 bfd_byte *contents = NULL;
3457 size_t size;
3458 long plt_count = 0;
3459 bfd_vma glink_vma = 0, resolv_vma = 0;
3460 asection *dynamic, *glink = NULL, *relplt = NULL;
3461 arelent *p;
3462
3463 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3464 {
3465 free_contents_and_exit_err:
3466 count = -1;
3467 free_contents_and_exit:
3468 if (contents)
3469 free (contents);
3470 goto done;
3471 }
3472
3473 size = 0;
3474 for (i = secsymend; i < opdsymend; ++i)
3475 {
3476 bfd_vma ent;
3477
3478 /* Ignore bogus symbols. */
3479 if (syms[i]->value > opd->size - 8)
3480 continue;
3481
3482 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3483 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3484 {
3485 ++count;
3486 size += sizeof (asymbol);
3487 size += strlen (syms[i]->name) + 2;
3488 }
3489 }
3490
3491 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3492 if (dyn_count != 0
3493 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3494 {
3495 bfd_byte *dynbuf, *extdyn, *extdynend;
3496 size_t extdynsize;
3497 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3498
3499 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3500 goto free_contents_and_exit_err;
3501
3502 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3503 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3504
3505 extdyn = dynbuf;
3506 extdynend = extdyn + dynamic->size;
3507 for (; extdyn < extdynend; extdyn += extdynsize)
3508 {
3509 Elf_Internal_Dyn dyn;
3510 (*swap_dyn_in) (abfd, extdyn, &dyn);
3511
3512 if (dyn.d_tag == DT_NULL)
3513 break;
3514
3515 if (dyn.d_tag == DT_PPC64_GLINK)
3516 {
3517 /* The first glink stub starts at offset 32; see
3518 comment in ppc64_elf_finish_dynamic_sections. */
3519 glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
3520 /* The .glink section usually does not survive the final
3521 link; search for the section (usually .text) where the
3522 glink stubs now reside. */
3523 glink = bfd_sections_find_if (abfd, section_covers_vma,
3524 &glink_vma);
3525 break;
3526 }
3527 }
3528
3529 free (dynbuf);
3530 }
3531
3532 if (glink != NULL)
3533 {
3534 /* Determine __glink trampoline by reading the relative branch
3535 from the first glink stub. */
3536 bfd_byte buf[4];
3537 unsigned int off = 0;
3538
3539 while (bfd_get_section_contents (abfd, glink, buf,
3540 glink_vma + off - glink->vma, 4))
3541 {
3542 unsigned int insn = bfd_get_32 (abfd, buf);
3543 insn ^= B_DOT;
3544 if ((insn & ~0x3fffffc) == 0)
3545 {
3546 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3547 break;
3548 }
3549 off += 4;
3550 if (off > 4)
3551 break;
3552 }
3553
3554 if (resolv_vma)
3555 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3556
3557 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3558 if (relplt != NULL)
3559 {
3560 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3561 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3562 goto free_contents_and_exit_err;
3563
3564 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3565 size += plt_count * sizeof (asymbol);
3566
3567 p = relplt->relocation;
3568 for (i = 0; i < plt_count; i++, p++)
3569 {
3570 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3571 if (p->addend != 0)
3572 size += sizeof ("+0x") - 1 + 16;
3573 }
3574 }
3575 }
3576
3577 if (size == 0)
3578 goto free_contents_and_exit;
3579 s = *ret = bfd_malloc (size);
3580 if (s == NULL)
3581 goto free_contents_and_exit_err;
3582
3583 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3584
3585 for (i = secsymend; i < opdsymend; ++i)
3586 {
3587 bfd_vma ent;
3588
3589 if (syms[i]->value > opd->size - 8)
3590 continue;
3591
3592 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3593 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3594 {
3595 long lo, hi;
3596 size_t len;
3597 asection *sec = abfd->sections;
3598
3599 *s = *syms[i];
3600 lo = codesecsym;
3601 hi = codesecsymend;
3602 while (lo < hi)
3603 {
3604 long mid = (lo + hi) >> 1;
3605 if (syms[mid]->section->vma < ent)
3606 lo = mid + 1;
3607 else if (syms[mid]->section->vma > ent)
3608 hi = mid;
3609 else
3610 {
3611 sec = syms[mid]->section;
3612 break;
3613 }
3614 }
3615
3616 if (lo >= hi && lo > codesecsym)
3617 sec = syms[lo - 1]->section;
3618
3619 for (; sec != NULL; sec = sec->next)
3620 {
3621 if (sec->vma > ent)
3622 break;
3623 /* SEC_LOAD may not be set if SEC is from a separate debug
3624 info file. */
3625 if ((sec->flags & SEC_ALLOC) == 0)
3626 break;
3627 if ((sec->flags & SEC_CODE) != 0)
3628 s->section = sec;
3629 }
3630 s->flags |= BSF_SYNTHETIC;
3631 s->value = ent - s->section->vma;
3632 s->name = names;
3633 *names++ = '.';
3634 len = strlen (syms[i]->name);
3635 memcpy (names, syms[i]->name, len + 1);
3636 names += len + 1;
3637 /* Have udata.p point back to the original symbol this
3638 synthetic symbol was derived from. */
3639 s->udata.p = syms[i];
3640 s++;
3641 }
3642 }
3643 free (contents);
3644
3645 if (glink != NULL && relplt != NULL)
3646 {
3647 if (resolv_vma)
3648 {
3649 /* Add a symbol for the main glink trampoline. */
3650 memset (s, 0, sizeof *s);
3651 s->the_bfd = abfd;
3652 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3653 s->section = glink;
3654 s->value = resolv_vma - glink->vma;
3655 s->name = names;
3656 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3657 names += sizeof ("__glink_PLTresolve");
3658 s++;
3659 count++;
3660 }
3661
3662 /* FIXME: It would be very much nicer to put sym@plt on the
3663 stub rather than on the glink branch table entry. The
3664 objdump disassembler would then use a sensible symbol
3665 name on plt calls. The difficulty in doing so is
3666 a) finding the stubs, and,
3667 b) matching stubs against plt entries, and,
3668 c) there can be multiple stubs for a given plt entry.
3669
3670 Solving (a) could be done by code scanning, but older
3671 ppc64 binaries used different stubs to current code.
3672 (b) is the tricky one since you need to known the toc
3673 pointer for at least one function that uses a pic stub to
3674 be able to calculate the plt address referenced.
3675 (c) means gdb would need to set multiple breakpoints (or
3676 find the glink branch itself) when setting breakpoints
3677 for pending shared library loads. */
3678 p = relplt->relocation;
3679 for (i = 0; i < plt_count; i++, p++)
3680 {
3681 size_t len;
3682
3683 *s = **p->sym_ptr_ptr;
3684 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3685 we are defining a symbol, ensure one of them is set. */
3686 if ((s->flags & BSF_LOCAL) == 0)
3687 s->flags |= BSF_GLOBAL;
3688 s->flags |= BSF_SYNTHETIC;
3689 s->section = glink;
3690 s->value = glink_vma - glink->vma;
3691 s->name = names;
3692 s->udata.p = NULL;
3693 len = strlen ((*p->sym_ptr_ptr)->name);
3694 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3695 names += len;
3696 if (p->addend != 0)
3697 {
3698 memcpy (names, "+0x", sizeof ("+0x") - 1);
3699 names += sizeof ("+0x") - 1;
3700 bfd_sprintf_vma (abfd, names, p->addend);
3701 names += strlen (names);
3702 }
3703 memcpy (names, "@plt", sizeof ("@plt"));
3704 names += sizeof ("@plt");
3705 s++;
3706 if (abi < 2)
3707 {
3708 glink_vma += 8;
3709 if (i >= 0x8000)
3710 glink_vma += 4;
3711 }
3712 else
3713 glink_vma += 4;
3714 }
3715 count += plt_count;
3716 }
3717 }
3718
3719 done:
3720 free (syms);
3721 return count;
3722 }
3723 \f
3724 /* The following functions are specific to the ELF linker, while
3725 functions above are used generally. Those named ppc64_elf_* are
3726 called by the main ELF linker code. They appear in this file more
3727 or less in the order in which they are called. eg.
3728 ppc64_elf_check_relocs is called early in the link process,
3729 ppc64_elf_finish_dynamic_sections is one of the last functions
3730 called.
3731
3732 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3733 functions have both a function code symbol and a function descriptor
3734 symbol. A call to foo in a relocatable object file looks like:
3735
3736 . .text
3737 . x:
3738 . bl .foo
3739 . nop
3740
3741 The function definition in another object file might be:
3742
3743 . .section .opd
3744 . foo: .quad .foo
3745 . .quad .TOC.@tocbase
3746 . .quad 0
3747 .
3748 . .text
3749 . .foo: blr
3750
3751 When the linker resolves the call during a static link, the branch
3752 unsurprisingly just goes to .foo and the .opd information is unused.
3753 If the function definition is in a shared library, things are a little
3754 different: The call goes via a plt call stub, the opd information gets
3755 copied to the plt, and the linker patches the nop.
3756
3757 . x:
3758 . bl .foo_stub
3759 . ld 2,40(1)
3760 .
3761 .
3762 . .foo_stub:
3763 . std 2,40(1) # in practice, the call stub
3764 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3765 . addi 11,11,Lfoo@toc@l # this is the general idea
3766 . ld 12,0(11)
3767 . ld 2,8(11)
3768 . mtctr 12
3769 . ld 11,16(11)
3770 . bctr
3771 .
3772 . .section .plt
3773 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3774
3775 The "reloc ()" notation is supposed to indicate that the linker emits
3776 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3777 copying.
3778
3779 What are the difficulties here? Well, firstly, the relocations
3780 examined by the linker in check_relocs are against the function code
3781 sym .foo, while the dynamic relocation in the plt is emitted against
3782 the function descriptor symbol, foo. Somewhere along the line, we need
3783 to carefully copy dynamic link information from one symbol to the other.
3784 Secondly, the generic part of the elf linker will make .foo a dynamic
3785 symbol as is normal for most other backends. We need foo dynamic
3786 instead, at least for an application final link. However, when
3787 creating a shared library containing foo, we need to have both symbols
3788 dynamic so that references to .foo are satisfied during the early
3789 stages of linking. Otherwise the linker might decide to pull in a
3790 definition from some other object, eg. a static library.
3791
3792 Update: As of August 2004, we support a new convention. Function
3793 calls may use the function descriptor symbol, ie. "bl foo". This
3794 behaves exactly as "bl .foo". */
3795
3796 /* Of those relocs that might be copied as dynamic relocs, this
3797 function selects those that must be copied when linking a shared
3798 library or PIE, even when the symbol is local. */
3799
3800 static int
3801 must_be_dyn_reloc (struct bfd_link_info *info,
3802 enum elf_ppc64_reloc_type r_type)
3803 {
3804 switch (r_type)
3805 {
3806 default:
3807 /* Only relative relocs can be resolved when the object load
3808 address isn't fixed. DTPREL64 is excluded because the
3809 dynamic linker needs to differentiate global dynamic from
3810 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
3811 return 1;
3812
3813 case R_PPC64_REL32:
3814 case R_PPC64_REL64:
3815 case R_PPC64_REL30:
3816 return 0;
3817
3818 case R_PPC64_TPREL16:
3819 case R_PPC64_TPREL16_LO:
3820 case R_PPC64_TPREL16_HI:
3821 case R_PPC64_TPREL16_HA:
3822 case R_PPC64_TPREL16_DS:
3823 case R_PPC64_TPREL16_LO_DS:
3824 case R_PPC64_TPREL16_HIGH:
3825 case R_PPC64_TPREL16_HIGHA:
3826 case R_PPC64_TPREL16_HIGHER:
3827 case R_PPC64_TPREL16_HIGHERA:
3828 case R_PPC64_TPREL16_HIGHEST:
3829 case R_PPC64_TPREL16_HIGHESTA:
3830 case R_PPC64_TPREL64:
3831 /* These relocations are relative but in a shared library the
3832 linker doesn't know the thread pointer base. */
3833 return bfd_link_dll (info);
3834 }
3835 }
3836
3837 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3838 copying dynamic variables from a shared lib into an app's dynbss
3839 section, and instead use a dynamic relocation to point into the
3840 shared lib. With code that gcc generates, it's vital that this be
3841 enabled; In the PowerPC64 ABI, the address of a function is actually
3842 the address of a function descriptor, which resides in the .opd
3843 section. gcc uses the descriptor directly rather than going via the
3844 GOT as some other ABI's do, which means that initialized function
3845 pointers must reference the descriptor. Thus, a function pointer
3846 initialized to the address of a function in a shared library will
3847 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3848 redefines the function descriptor symbol to point to the copy. This
3849 presents a problem as a plt entry for that function is also
3850 initialized from the function descriptor symbol and the copy reloc
3851 may not be initialized first. */
3852 #define ELIMINATE_COPY_RELOCS 1
3853
3854 /* Section name for stubs is the associated section name plus this
3855 string. */
3856 #define STUB_SUFFIX ".stub"
3857
3858 /* Linker stubs.
3859 ppc_stub_long_branch:
3860 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3861 destination, but a 24 bit branch in a stub section will reach.
3862 . b dest
3863
3864 ppc_stub_plt_branch:
3865 Similar to the above, but a 24 bit branch in the stub section won't
3866 reach its destination.
3867 . addis %r11,%r2,xxx@toc@ha
3868 . ld %r12,xxx@toc@l(%r11)
3869 . mtctr %r12
3870 . bctr
3871
3872 ppc_stub_plt_call:
3873 Used to call a function in a shared library. If it so happens that
3874 the plt entry referenced crosses a 64k boundary, then an extra
3875 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3876 . std %r2,40(%r1)
3877 . addis %r11,%r2,xxx@toc@ha
3878 . ld %r12,xxx+0@toc@l(%r11)
3879 . mtctr %r12
3880 . ld %r2,xxx+8@toc@l(%r11)
3881 . ld %r11,xxx+16@toc@l(%r11)
3882 . bctr
3883
3884 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3885 code to adjust the value and save r2 to support multiple toc sections.
3886 A ppc_stub_long_branch with an r2 offset looks like:
3887 . std %r2,40(%r1)
3888 . addis %r2,%r2,off@ha
3889 . addi %r2,%r2,off@l
3890 . b dest
3891
3892 A ppc_stub_plt_branch with an r2 offset looks like:
3893 . std %r2,40(%r1)
3894 . addis %r11,%r2,xxx@toc@ha
3895 . ld %r12,xxx@toc@l(%r11)
3896 . addis %r2,%r2,off@ha
3897 . addi %r2,%r2,off@l
3898 . mtctr %r12
3899 . bctr
3900
3901 In cases where the "addis" instruction would add zero, the "addis" is
3902 omitted and following instructions modified slightly in some cases.
3903 */
3904
3905 enum ppc_stub_type {
3906 ppc_stub_none,
3907 ppc_stub_long_branch,
3908 ppc_stub_long_branch_r2off,
3909 ppc_stub_plt_branch,
3910 ppc_stub_plt_branch_r2off,
3911 ppc_stub_plt_call,
3912 ppc_stub_plt_call_r2save,
3913 ppc_stub_global_entry,
3914 ppc_stub_save_res
3915 };
3916
3917 /* Information on stub grouping. */
3918 struct map_stub
3919 {
3920 /* The stub section. */
3921 asection *stub_sec;
3922 /* This is the section to which stubs in the group will be attached. */
3923 asection *link_sec;
3924 /* Next group. */
3925 struct map_stub *next;
3926 /* Whether to emit a copy of register save/restore functions in this
3927 group. */
3928 int needs_save_res;
3929 /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
3930 or -1u if no such stub with bctrl exists. */
3931 unsigned int tls_get_addr_opt_bctrl;
3932 };
3933
3934 struct ppc_stub_hash_entry {
3935
3936 /* Base hash table entry structure. */
3937 struct bfd_hash_entry root;
3938
3939 enum ppc_stub_type stub_type;
3940
3941 /* Group information. */
3942 struct map_stub *group;
3943
3944 /* Offset within stub_sec of the beginning of this stub. */
3945 bfd_vma stub_offset;
3946
3947 /* Given the symbol's value and its section we can determine its final
3948 value when building the stubs (so the stub knows where to jump. */
3949 bfd_vma target_value;
3950 asection *target_section;
3951
3952 /* The symbol table entry, if any, that this was derived from. */
3953 struct ppc_link_hash_entry *h;
3954 struct plt_entry *plt_ent;
3955
3956 /* Symbol st_other. */
3957 unsigned char other;
3958 };
3959
3960 struct ppc_branch_hash_entry {
3961
3962 /* Base hash table entry structure. */
3963 struct bfd_hash_entry root;
3964
3965 /* Offset within branch lookup table. */
3966 unsigned int offset;
3967
3968 /* Generation marker. */
3969 unsigned int iter;
3970 };
3971
3972 /* Used to track dynamic relocations for local symbols. */
3973 struct ppc_dyn_relocs
3974 {
3975 struct ppc_dyn_relocs *next;
3976
3977 /* The input section of the reloc. */
3978 asection *sec;
3979
3980 /* Total number of relocs copied for the input section. */
3981 unsigned int count : 31;
3982
3983 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3984 unsigned int ifunc : 1;
3985 };
3986
3987 struct ppc_link_hash_entry
3988 {
3989 struct elf_link_hash_entry elf;
3990
3991 union {
3992 /* A pointer to the most recently used stub hash entry against this
3993 symbol. */
3994 struct ppc_stub_hash_entry *stub_cache;
3995
3996 /* A pointer to the next symbol starting with a '.' */
3997 struct ppc_link_hash_entry *next_dot_sym;
3998 } u;
3999
4000 /* Track dynamic relocs copied for this symbol. */
4001 struct elf_dyn_relocs *dyn_relocs;
4002
4003 /* Chain of aliases referring to a weakdef. */
4004 struct ppc_link_hash_entry *weakref;
4005
4006 /* Link between function code and descriptor symbols. */
4007 struct ppc_link_hash_entry *oh;
4008
4009 /* Flag function code and descriptor symbols. */
4010 unsigned int is_func:1;
4011 unsigned int is_func_descriptor:1;
4012 unsigned int fake:1;
4013
4014 /* Whether global opd/toc sym has been adjusted or not.
4015 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4016 should be set for all globals defined in any opd/toc section. */
4017 unsigned int adjust_done:1;
4018
4019 /* Set if this is an out-of-line register save/restore function,
4020 with non-standard calling convention. */
4021 unsigned int save_res:1;
4022
4023 /* Set if a duplicate symbol with non-zero localentry is detected,
4024 even when the duplicate symbol does not provide a definition. */
4025 unsigned int non_zero_localentry:1;
4026
4027 /* Contexts in which symbol is used in the GOT (or TOC).
4028 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
4029 corresponding relocs are encountered during check_relocs.
4030 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
4031 indicate the corresponding GOT entry type is not needed.
4032 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
4033 a TPREL one. We use a separate flag rather than setting TPREL
4034 just for convenience in distinguishing the two cases. */
4035 #define TLS_GD 1 /* GD reloc. */
4036 #define TLS_LD 2 /* LD reloc. */
4037 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
4038 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
4039 #define TLS_TLS 16 /* Any TLS reloc. */
4040 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
4041 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
4042 #define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
4043 unsigned char tls_mask;
4044 };
4045
4046 /* ppc64 ELF linker hash table. */
4047
4048 struct ppc_link_hash_table
4049 {
4050 struct elf_link_hash_table elf;
4051
4052 /* The stub hash table. */
4053 struct bfd_hash_table stub_hash_table;
4054
4055 /* Another hash table for plt_branch stubs. */
4056 struct bfd_hash_table branch_hash_table;
4057
4058 /* Hash table for function prologue tocsave. */
4059 htab_t tocsave_htab;
4060
4061 /* Various options and other info passed from the linker. */
4062 struct ppc64_elf_params *params;
4063
4064 /* The size of sec_info below. */
4065 unsigned int sec_info_arr_size;
4066
4067 /* Per-section array of extra section info. Done this way rather
4068 than as part of ppc64_elf_section_data so we have the info for
4069 non-ppc64 sections. */
4070 struct
4071 {
4072 /* Along with elf_gp, specifies the TOC pointer used by this section. */
4073 bfd_vma toc_off;
4074
4075 union
4076 {
4077 /* The section group that this section belongs to. */
4078 struct map_stub *group;
4079 /* A temp section list pointer. */
4080 asection *list;
4081 } u;
4082 } *sec_info;
4083
4084 /* Linked list of groups. */
4085 struct map_stub *group;
4086
4087 /* Temp used when calculating TOC pointers. */
4088 bfd_vma toc_curr;
4089 bfd *toc_bfd;
4090 asection *toc_first_sec;
4091
4092 /* Used when adding symbols. */
4093 struct ppc_link_hash_entry *dot_syms;
4094
4095 /* Shortcuts to get to dynamic linker sections. */
4096 asection *glink;
4097 asection *sfpr;
4098 asection *brlt;
4099 asection *relbrlt;
4100 asection *glink_eh_frame;
4101
4102 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4103 struct ppc_link_hash_entry *tls_get_addr;
4104 struct ppc_link_hash_entry *tls_get_addr_fd;
4105
4106 /* The size of reliplt used by got entry relocs. */
4107 bfd_size_type got_reli_size;
4108
4109 /* Statistics. */
4110 unsigned long stub_count[ppc_stub_global_entry];
4111
4112 /* Number of stubs against global syms. */
4113 unsigned long stub_globals;
4114
4115 /* Set if we're linking code with function descriptors. */
4116 unsigned int opd_abi:1;
4117
4118 /* Support for multiple toc sections. */
4119 unsigned int do_multi_toc:1;
4120 unsigned int multi_toc_needed:1;
4121 unsigned int second_toc_pass:1;
4122 unsigned int do_toc_opt:1;
4123
4124 /* Set if tls optimization is enabled. */
4125 unsigned int do_tls_opt:1;
4126
4127 /* Set on error. */
4128 unsigned int stub_error:1;
4129
4130 /* Whether func_desc_adjust needs to be run over symbols. */
4131 unsigned int need_func_desc_adj:1;
4132
4133 /* Whether there exist local gnu indirect function resolvers,
4134 referenced by dynamic relocations. */
4135 unsigned int local_ifunc_resolver:1;
4136 unsigned int maybe_local_ifunc_resolver:1;
4137
4138 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4139 unsigned int has_plt_localentry0:1;
4140
4141 /* Incremented every time we size stubs. */
4142 unsigned int stub_iteration;
4143
4144 /* Small local sym cache. */
4145 struct sym_cache sym_cache;
4146 };
4147
4148 /* Rename some of the generic section flags to better document how they
4149 are used here. */
4150
4151 /* Nonzero if this section has TLS related relocations. */
4152 #define has_tls_reloc sec_flg0
4153
4154 /* Nonzero if this section has a call to __tls_get_addr. */
4155 #define has_tls_get_addr_call sec_flg1
4156
4157 /* Nonzero if this section has any toc or got relocs. */
4158 #define has_toc_reloc sec_flg2
4159
4160 /* Nonzero if this section has a call to another section that uses
4161 the toc or got. */
4162 #define makes_toc_func_call sec_flg3
4163
4164 /* Recursion protection when determining above flag. */
4165 #define call_check_in_progress sec_flg4
4166 #define call_check_done sec_flg5
4167
4168 /* Get the ppc64 ELF linker hash table from a link_info structure. */
4169
4170 #define ppc_hash_table(p) \
4171 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4172 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4173
4174 #define ppc_stub_hash_lookup(table, string, create, copy) \
4175 ((struct ppc_stub_hash_entry *) \
4176 bfd_hash_lookup ((table), (string), (create), (copy)))
4177
4178 #define ppc_branch_hash_lookup(table, string, create, copy) \
4179 ((struct ppc_branch_hash_entry *) \
4180 bfd_hash_lookup ((table), (string), (create), (copy)))
4181
4182 /* Create an entry in the stub hash table. */
4183
4184 static struct bfd_hash_entry *
4185 stub_hash_newfunc (struct bfd_hash_entry *entry,
4186 struct bfd_hash_table *table,
4187 const char *string)
4188 {
4189 /* Allocate the structure if it has not already been allocated by a
4190 subclass. */
4191 if (entry == NULL)
4192 {
4193 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4194 if (entry == NULL)
4195 return entry;
4196 }
4197
4198 /* Call the allocation method of the superclass. */
4199 entry = bfd_hash_newfunc (entry, table, string);
4200 if (entry != NULL)
4201 {
4202 struct ppc_stub_hash_entry *eh;
4203
4204 /* Initialize the local fields. */
4205 eh = (struct ppc_stub_hash_entry *) entry;
4206 eh->stub_type = ppc_stub_none;
4207 eh->group = NULL;
4208 eh->stub_offset = 0;
4209 eh->target_value = 0;
4210 eh->target_section = NULL;
4211 eh->h = NULL;
4212 eh->plt_ent = NULL;
4213 eh->other = 0;
4214 }
4215
4216 return entry;
4217 }
4218
4219 /* Create an entry in the branch hash table. */
4220
4221 static struct bfd_hash_entry *
4222 branch_hash_newfunc (struct bfd_hash_entry *entry,
4223 struct bfd_hash_table *table,
4224 const char *string)
4225 {
4226 /* Allocate the structure if it has not already been allocated by a
4227 subclass. */
4228 if (entry == NULL)
4229 {
4230 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4231 if (entry == NULL)
4232 return entry;
4233 }
4234
4235 /* Call the allocation method of the superclass. */
4236 entry = bfd_hash_newfunc (entry, table, string);
4237 if (entry != NULL)
4238 {
4239 struct ppc_branch_hash_entry *eh;
4240
4241 /* Initialize the local fields. */
4242 eh = (struct ppc_branch_hash_entry *) entry;
4243 eh->offset = 0;
4244 eh->iter = 0;
4245 }
4246
4247 return entry;
4248 }
4249
4250 /* Create an entry in a ppc64 ELF linker hash table. */
4251
4252 static struct bfd_hash_entry *
4253 link_hash_newfunc (struct bfd_hash_entry *entry,
4254 struct bfd_hash_table *table,
4255 const char *string)
4256 {
4257 /* Allocate the structure if it has not already been allocated by a
4258 subclass. */
4259 if (entry == NULL)
4260 {
4261 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4262 if (entry == NULL)
4263 return entry;
4264 }
4265
4266 /* Call the allocation method of the superclass. */
4267 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4268 if (entry != NULL)
4269 {
4270 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4271
4272 memset (&eh->u.stub_cache, 0,
4273 (sizeof (struct ppc_link_hash_entry)
4274 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4275
4276 /* When making function calls, old ABI code references function entry
4277 points (dot symbols), while new ABI code references the function
4278 descriptor symbol. We need to make any combination of reference and
4279 definition work together, without breaking archive linking.
4280
4281 For a defined function "foo" and an undefined call to "bar":
4282 An old object defines "foo" and ".foo", references ".bar" (possibly
4283 "bar" too).
4284 A new object defines "foo" and references "bar".
4285
4286 A new object thus has no problem with its undefined symbols being
4287 satisfied by definitions in an old object. On the other hand, the
4288 old object won't have ".bar" satisfied by a new object.
4289
4290 Keep a list of newly added dot-symbols. */
4291
4292 if (string[0] == '.')
4293 {
4294 struct ppc_link_hash_table *htab;
4295
4296 htab = (struct ppc_link_hash_table *) table;
4297 eh->u.next_dot_sym = htab->dot_syms;
4298 htab->dot_syms = eh;
4299 }
4300 }
4301
4302 return entry;
4303 }
4304
4305 struct tocsave_entry {
4306 asection *sec;
4307 bfd_vma offset;
4308 };
4309
4310 static hashval_t
4311 tocsave_htab_hash (const void *p)
4312 {
4313 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4314 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4315 }
4316
4317 static int
4318 tocsave_htab_eq (const void *p1, const void *p2)
4319 {
4320 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4321 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4322 return e1->sec == e2->sec && e1->offset == e2->offset;
4323 }
4324
4325 /* Destroy a ppc64 ELF linker hash table. */
4326
4327 static void
4328 ppc64_elf_link_hash_table_free (bfd *obfd)
4329 {
4330 struct ppc_link_hash_table *htab;
4331
4332 htab = (struct ppc_link_hash_table *) obfd->link.hash;
4333 if (htab->tocsave_htab)
4334 htab_delete (htab->tocsave_htab);
4335 bfd_hash_table_free (&htab->branch_hash_table);
4336 bfd_hash_table_free (&htab->stub_hash_table);
4337 _bfd_elf_link_hash_table_free (obfd);
4338 }
4339
4340 /* Create a ppc64 ELF linker hash table. */
4341
4342 static struct bfd_link_hash_table *
4343 ppc64_elf_link_hash_table_create (bfd *abfd)
4344 {
4345 struct ppc_link_hash_table *htab;
4346 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4347
4348 htab = bfd_zmalloc (amt);
4349 if (htab == NULL)
4350 return NULL;
4351
4352 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4353 sizeof (struct ppc_link_hash_entry),
4354 PPC64_ELF_DATA))
4355 {
4356 free (htab);
4357 return NULL;
4358 }
4359
4360 /* Init the stub hash table too. */
4361 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4362 sizeof (struct ppc_stub_hash_entry)))
4363 {
4364 _bfd_elf_link_hash_table_free (abfd);
4365 return NULL;
4366 }
4367
4368 /* And the branch hash table. */
4369 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4370 sizeof (struct ppc_branch_hash_entry)))
4371 {
4372 bfd_hash_table_free (&htab->stub_hash_table);
4373 _bfd_elf_link_hash_table_free (abfd);
4374 return NULL;
4375 }
4376
4377 htab->tocsave_htab = htab_try_create (1024,
4378 tocsave_htab_hash,
4379 tocsave_htab_eq,
4380 NULL);
4381 if (htab->tocsave_htab == NULL)
4382 {
4383 ppc64_elf_link_hash_table_free (abfd);
4384 return NULL;
4385 }
4386 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4387
4388 /* Initializing two fields of the union is just cosmetic. We really
4389 only care about glist, but when compiled on a 32-bit host the
4390 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4391 debugger inspection of these fields look nicer. */
4392 htab->elf.init_got_refcount.refcount = 0;
4393 htab->elf.init_got_refcount.glist = NULL;
4394 htab->elf.init_plt_refcount.refcount = 0;
4395 htab->elf.init_plt_refcount.glist = NULL;
4396 htab->elf.init_got_offset.offset = 0;
4397 htab->elf.init_got_offset.glist = NULL;
4398 htab->elf.init_plt_offset.offset = 0;
4399 htab->elf.init_plt_offset.glist = NULL;
4400
4401 return &htab->elf.root;
4402 }
4403
4404 /* Create sections for linker generated code. */
4405
4406 static bfd_boolean
4407 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4408 {
4409 struct ppc_link_hash_table *htab;
4410 flagword flags;
4411
4412 htab = ppc_hash_table (info);
4413
4414 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4415 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4416 if (htab->params->save_restore_funcs)
4417 {
4418 /* Create .sfpr for code to save and restore fp regs. */
4419 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4420 flags);
4421 if (htab->sfpr == NULL
4422 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4423 return FALSE;
4424 }
4425
4426 if (bfd_link_relocatable (info))
4427 return TRUE;
4428
4429 /* Create .glink for lazy dynamic linking support. */
4430 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4431 flags);
4432 if (htab->glink == NULL
4433 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4434 return FALSE;
4435
4436 if (!info->no_ld_generated_unwind_info)
4437 {
4438 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4439 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4440 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4441 ".eh_frame",
4442 flags);
4443 if (htab->glink_eh_frame == NULL
4444 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4445 return FALSE;
4446 }
4447
4448 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4449 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4450 if (htab->elf.iplt == NULL
4451 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4452 return FALSE;
4453
4454 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4455 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4456 htab->elf.irelplt
4457 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4458 if (htab->elf.irelplt == NULL
4459 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4460 return FALSE;
4461
4462 /* Create branch lookup table for plt_branch stubs. */
4463 flags = (SEC_ALLOC | SEC_LOAD
4464 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4465 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4466 flags);
4467 if (htab->brlt == NULL
4468 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4469 return FALSE;
4470
4471 if (!bfd_link_pic (info))
4472 return TRUE;
4473
4474 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4475 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4476 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4477 ".rela.branch_lt",
4478 flags);
4479 if (htab->relbrlt == NULL
4480 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4481 return FALSE;
4482
4483 return TRUE;
4484 }
4485
4486 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4487
4488 bfd_boolean
4489 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4490 struct ppc64_elf_params *params)
4491 {
4492 struct ppc_link_hash_table *htab;
4493
4494 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4495
4496 /* Always hook our dynamic sections into the first bfd, which is the
4497 linker created stub bfd. This ensures that the GOT header is at
4498 the start of the output TOC section. */
4499 htab = ppc_hash_table (info);
4500 htab->elf.dynobj = params->stub_bfd;
4501 htab->params = params;
4502
4503 return create_linkage_sections (htab->elf.dynobj, info);
4504 }
4505
4506 /* Build a name for an entry in the stub hash table. */
4507
4508 static char *
4509 ppc_stub_name (const asection *input_section,
4510 const asection *sym_sec,
4511 const struct ppc_link_hash_entry *h,
4512 const Elf_Internal_Rela *rel)
4513 {
4514 char *stub_name;
4515 ssize_t len;
4516
4517 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4518 offsets from a sym as a branch target? In fact, we could
4519 probably assume the addend is always zero. */
4520 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4521
4522 if (h)
4523 {
4524 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4525 stub_name = bfd_malloc (len);
4526 if (stub_name == NULL)
4527 return stub_name;
4528
4529 len = sprintf (stub_name, "%08x.%s+%x",
4530 input_section->id & 0xffffffff,
4531 h->elf.root.root.string,
4532 (int) rel->r_addend & 0xffffffff);
4533 }
4534 else
4535 {
4536 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4537 stub_name = bfd_malloc (len);
4538 if (stub_name == NULL)
4539 return stub_name;
4540
4541 len = sprintf (stub_name, "%08x.%x:%x+%x",
4542 input_section->id & 0xffffffff,
4543 sym_sec->id & 0xffffffff,
4544 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4545 (int) rel->r_addend & 0xffffffff);
4546 }
4547 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4548 stub_name[len - 2] = 0;
4549 return stub_name;
4550 }
4551
4552 /* Look up an entry in the stub hash. Stub entries are cached because
4553 creating the stub name takes a bit of time. */
4554
4555 static struct ppc_stub_hash_entry *
4556 ppc_get_stub_entry (const asection *input_section,
4557 const asection *sym_sec,
4558 struct ppc_link_hash_entry *h,
4559 const Elf_Internal_Rela *rel,
4560 struct ppc_link_hash_table *htab)
4561 {
4562 struct ppc_stub_hash_entry *stub_entry;
4563 struct map_stub *group;
4564
4565 /* If this input section is part of a group of sections sharing one
4566 stub section, then use the id of the first section in the group.
4567 Stub names need to include a section id, as there may well be
4568 more than one stub used to reach say, printf, and we need to
4569 distinguish between them. */
4570 group = htab->sec_info[input_section->id].u.group;
4571 if (group == NULL)
4572 return NULL;
4573
4574 if (h != NULL && h->u.stub_cache != NULL
4575 && h->u.stub_cache->h == h
4576 && h->u.stub_cache->group == group)
4577 {
4578 stub_entry = h->u.stub_cache;
4579 }
4580 else
4581 {
4582 char *stub_name;
4583
4584 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4585 if (stub_name == NULL)
4586 return NULL;
4587
4588 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4589 stub_name, FALSE, FALSE);
4590 if (h != NULL)
4591 h->u.stub_cache = stub_entry;
4592
4593 free (stub_name);
4594 }
4595
4596 return stub_entry;
4597 }
4598
4599 /* Add a new stub entry to the stub hash. Not all fields of the new
4600 stub entry are initialised. */
4601
4602 static struct ppc_stub_hash_entry *
4603 ppc_add_stub (const char *stub_name,
4604 asection *section,
4605 struct bfd_link_info *info)
4606 {
4607 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4608 struct map_stub *group;
4609 asection *link_sec;
4610 asection *stub_sec;
4611 struct ppc_stub_hash_entry *stub_entry;
4612
4613 group = htab->sec_info[section->id].u.group;
4614 link_sec = group->link_sec;
4615 stub_sec = group->stub_sec;
4616 if (stub_sec == NULL)
4617 {
4618 size_t namelen;
4619 bfd_size_type len;
4620 char *s_name;
4621
4622 namelen = strlen (link_sec->name);
4623 len = namelen + sizeof (STUB_SUFFIX);
4624 s_name = bfd_alloc (htab->params->stub_bfd, len);
4625 if (s_name == NULL)
4626 return NULL;
4627
4628 memcpy (s_name, link_sec->name, namelen);
4629 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4630 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4631 if (stub_sec == NULL)
4632 return NULL;
4633 group->stub_sec = stub_sec;
4634 }
4635
4636 /* Enter this entry into the linker stub hash table. */
4637 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4638 TRUE, FALSE);
4639 if (stub_entry == NULL)
4640 {
4641 /* xgettext:c-format */
4642 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4643 section->owner, stub_name);
4644 return NULL;
4645 }
4646
4647 stub_entry->group = group;
4648 stub_entry->stub_offset = 0;
4649 return stub_entry;
4650 }
4651
4652 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4653 not already done. */
4654
4655 static bfd_boolean
4656 create_got_section (bfd *abfd, struct bfd_link_info *info)
4657 {
4658 asection *got, *relgot;
4659 flagword flags;
4660 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4661
4662 if (!is_ppc64_elf (abfd))
4663 return FALSE;
4664 if (htab == NULL)
4665 return FALSE;
4666
4667 if (!htab->elf.sgot
4668 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4669 return FALSE;
4670
4671 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4672 | SEC_LINKER_CREATED);
4673
4674 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4675 if (!got
4676 || !bfd_set_section_alignment (abfd, got, 3))
4677 return FALSE;
4678
4679 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4680 flags | SEC_READONLY);
4681 if (!relgot
4682 || ! bfd_set_section_alignment (abfd, relgot, 3))
4683 return FALSE;
4684
4685 ppc64_elf_tdata (abfd)->got = got;
4686 ppc64_elf_tdata (abfd)->relgot = relgot;
4687 return TRUE;
4688 }
4689
4690 /* Follow indirect and warning symbol links. */
4691
4692 static inline struct bfd_link_hash_entry *
4693 follow_link (struct bfd_link_hash_entry *h)
4694 {
4695 while (h->type == bfd_link_hash_indirect
4696 || h->type == bfd_link_hash_warning)
4697 h = h->u.i.link;
4698 return h;
4699 }
4700
4701 static inline struct elf_link_hash_entry *
4702 elf_follow_link (struct elf_link_hash_entry *h)
4703 {
4704 return (struct elf_link_hash_entry *) follow_link (&h->root);
4705 }
4706
4707 static inline struct ppc_link_hash_entry *
4708 ppc_follow_link (struct ppc_link_hash_entry *h)
4709 {
4710 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4711 }
4712
4713 /* Merge PLT info on FROM with that on TO. */
4714
4715 static void
4716 move_plt_plist (struct ppc_link_hash_entry *from,
4717 struct ppc_link_hash_entry *to)
4718 {
4719 if (from->elf.plt.plist != NULL)
4720 {
4721 if (to->elf.plt.plist != NULL)
4722 {
4723 struct plt_entry **entp;
4724 struct plt_entry *ent;
4725
4726 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4727 {
4728 struct plt_entry *dent;
4729
4730 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4731 if (dent->addend == ent->addend)
4732 {
4733 dent->plt.refcount += ent->plt.refcount;
4734 *entp = ent->next;
4735 break;
4736 }
4737 if (dent == NULL)
4738 entp = &ent->next;
4739 }
4740 *entp = to->elf.plt.plist;
4741 }
4742
4743 to->elf.plt.plist = from->elf.plt.plist;
4744 from->elf.plt.plist = NULL;
4745 }
4746 }
4747
4748 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4749
4750 static void
4751 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4752 struct elf_link_hash_entry *dir,
4753 struct elf_link_hash_entry *ind)
4754 {
4755 struct ppc_link_hash_entry *edir, *eind;
4756
4757 edir = (struct ppc_link_hash_entry *) dir;
4758 eind = (struct ppc_link_hash_entry *) ind;
4759
4760 edir->is_func |= eind->is_func;
4761 edir->is_func_descriptor |= eind->is_func_descriptor;
4762 edir->tls_mask |= eind->tls_mask;
4763 if (eind->oh != NULL)
4764 edir->oh = ppc_follow_link (eind->oh);
4765
4766 /* If called to transfer flags for a weakdef during processing
4767 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4768 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4769 if (!(ELIMINATE_COPY_RELOCS
4770 && eind->elf.root.type != bfd_link_hash_indirect
4771 && edir->elf.dynamic_adjusted))
4772 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4773
4774 if (edir->elf.versioned != versioned_hidden)
4775 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4776 edir->elf.ref_regular |= eind->elf.ref_regular;
4777 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4778 edir->elf.needs_plt |= eind->elf.needs_plt;
4779 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4780
4781 /* If we were called to copy over info for a weak sym, don't copy
4782 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4783 in order to simplify readonly_dynrelocs and save a field in the
4784 symbol hash entry, but that means dyn_relocs can't be used in any
4785 tests about a specific symbol, or affect other symbol flags which
4786 are then tested.
4787 Chain weakdefs so we can get from the weakdef back to an alias.
4788 The list is circular so that we don't need to use u.weakdef as
4789 well as this list to look at all aliases. */
4790 if (eind->elf.root.type != bfd_link_hash_indirect)
4791 {
4792 struct ppc_link_hash_entry *cur, *add, *next;
4793
4794 add = eind;
4795 do
4796 {
4797 cur = edir->weakref;
4798 if (cur != NULL)
4799 {
4800 do
4801 {
4802 /* We can be called twice for the same symbols.
4803 Don't make multiple loops. */
4804 if (cur == add)
4805 return;
4806 cur = cur->weakref;
4807 } while (cur != edir);
4808 }
4809 next = add->weakref;
4810 if (cur != add)
4811 {
4812 add->weakref = edir->weakref != NULL ? edir->weakref : edir;
4813 edir->weakref = add;
4814 }
4815 add = next;
4816 } while (add != NULL && add != eind);
4817 return;
4818 }
4819
4820 /* Copy over any dynamic relocs we may have on the indirect sym. */
4821 if (eind->dyn_relocs != NULL)
4822 {
4823 if (edir->dyn_relocs != NULL)
4824 {
4825 struct elf_dyn_relocs **pp;
4826 struct elf_dyn_relocs *p;
4827
4828 /* Add reloc counts against the indirect sym to the direct sym
4829 list. Merge any entries against the same section. */
4830 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4831 {
4832 struct elf_dyn_relocs *q;
4833
4834 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4835 if (q->sec == p->sec)
4836 {
4837 q->pc_count += p->pc_count;
4838 q->count += p->count;
4839 *pp = p->next;
4840 break;
4841 }
4842 if (q == NULL)
4843 pp = &p->next;
4844 }
4845 *pp = edir->dyn_relocs;
4846 }
4847
4848 edir->dyn_relocs = eind->dyn_relocs;
4849 eind->dyn_relocs = NULL;
4850 }
4851
4852 /* Copy over got entries that we may have already seen to the
4853 symbol which just became indirect. */
4854 if (eind->elf.got.glist != NULL)
4855 {
4856 if (edir->elf.got.glist != NULL)
4857 {
4858 struct got_entry **entp;
4859 struct got_entry *ent;
4860
4861 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4862 {
4863 struct got_entry *dent;
4864
4865 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4866 if (dent->addend == ent->addend
4867 && dent->owner == ent->owner
4868 && dent->tls_type == ent->tls_type)
4869 {
4870 dent->got.refcount += ent->got.refcount;
4871 *entp = ent->next;
4872 break;
4873 }
4874 if (dent == NULL)
4875 entp = &ent->next;
4876 }
4877 *entp = edir->elf.got.glist;
4878 }
4879
4880 edir->elf.got.glist = eind->elf.got.glist;
4881 eind->elf.got.glist = NULL;
4882 }
4883
4884 /* And plt entries. */
4885 move_plt_plist (eind, edir);
4886
4887 if (eind->elf.dynindx != -1)
4888 {
4889 if (edir->elf.dynindx != -1)
4890 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4891 edir->elf.dynstr_index);
4892 edir->elf.dynindx = eind->elf.dynindx;
4893 edir->elf.dynstr_index = eind->elf.dynstr_index;
4894 eind->elf.dynindx = -1;
4895 eind->elf.dynstr_index = 0;
4896 }
4897 }
4898
4899 /* Find the function descriptor hash entry from the given function code
4900 hash entry FH. Link the entries via their OH fields. */
4901
4902 static struct ppc_link_hash_entry *
4903 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4904 {
4905 struct ppc_link_hash_entry *fdh = fh->oh;
4906
4907 if (fdh == NULL)
4908 {
4909 const char *fd_name = fh->elf.root.root.string + 1;
4910
4911 fdh = (struct ppc_link_hash_entry *)
4912 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4913 if (fdh == NULL)
4914 return fdh;
4915
4916 fdh->is_func_descriptor = 1;
4917 fdh->oh = fh;
4918 fh->is_func = 1;
4919 fh->oh = fdh;
4920 }
4921
4922 fdh = ppc_follow_link (fdh);
4923 fdh->is_func_descriptor = 1;
4924 fdh->oh = fh;
4925 return fdh;
4926 }
4927
4928 /* Make a fake function descriptor sym for the undefined code sym FH. */
4929
4930 static struct ppc_link_hash_entry *
4931 make_fdh (struct bfd_link_info *info,
4932 struct ppc_link_hash_entry *fh)
4933 {
4934 bfd *abfd = fh->elf.root.u.undef.abfd;
4935 struct bfd_link_hash_entry *bh = NULL;
4936 struct ppc_link_hash_entry *fdh;
4937 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4938 ? BSF_WEAK
4939 : BSF_GLOBAL);
4940
4941 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4942 fh->elf.root.root.string + 1,
4943 flags, bfd_und_section_ptr, 0,
4944 NULL, FALSE, FALSE, &bh))
4945 return NULL;
4946
4947 fdh = (struct ppc_link_hash_entry *) bh;
4948 fdh->elf.non_elf = 0;
4949 fdh->fake = 1;
4950 fdh->is_func_descriptor = 1;
4951 fdh->oh = fh;
4952 fh->is_func = 1;
4953 fh->oh = fdh;
4954 return fdh;
4955 }
4956
4957 /* Fix function descriptor symbols defined in .opd sections to be
4958 function type. */
4959
4960 static bfd_boolean
4961 ppc64_elf_add_symbol_hook (bfd *ibfd,
4962 struct bfd_link_info *info,
4963 Elf_Internal_Sym *isym,
4964 const char **name,
4965 flagword *flags ATTRIBUTE_UNUSED,
4966 asection **sec,
4967 bfd_vma *value)
4968 {
4969 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4970 && (ibfd->flags & DYNAMIC) == 0
4971 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4972 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
4973
4974 if (*sec != NULL
4975 && strcmp ((*sec)->name, ".opd") == 0)
4976 {
4977 asection *code_sec;
4978
4979 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4980 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4981 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4982
4983 /* If the symbol is a function defined in .opd, and the function
4984 code is in a discarded group, let it appear to be undefined. */
4985 if (!bfd_link_relocatable (info)
4986 && (*sec)->reloc_count != 0
4987 && opd_entry_value (*sec, *value, &code_sec, NULL,
4988 FALSE) != (bfd_vma) -1
4989 && discarded_section (code_sec))
4990 {
4991 *sec = bfd_und_section_ptr;
4992 isym->st_shndx = SHN_UNDEF;
4993 }
4994 }
4995 else if (*sec != NULL
4996 && strcmp ((*sec)->name, ".toc") == 0
4997 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4998 {
4999 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5000 if (htab != NULL)
5001 htab->params->object_in_toc = 1;
5002 }
5003
5004 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5005 {
5006 if (abiversion (ibfd) == 0)
5007 set_abiversion (ibfd, 2);
5008 else if (abiversion (ibfd) == 1)
5009 {
5010 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
5011 " for ABI version 1\n"), name);
5012 bfd_set_error (bfd_error_bad_value);
5013 return FALSE;
5014 }
5015 }
5016
5017 return TRUE;
5018 }
5019
5020 /* Merge non-visibility st_other attributes: local entry point. */
5021
5022 static void
5023 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5024 const Elf_Internal_Sym *isym,
5025 bfd_boolean definition,
5026 bfd_boolean dynamic)
5027 {
5028 if (definition && (!dynamic || !h->def_regular))
5029 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5030 | ELF_ST_VISIBILITY (h->other));
5031 }
5032
5033 /* Hook called on merging a symbol. We use this to clear "fake" since
5034 we now have a real symbol. */
5035
5036 static bfd_boolean
5037 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5038 const Elf_Internal_Sym *isym,
5039 asection **psec ATTRIBUTE_UNUSED,
5040 bfd_boolean newdef ATTRIBUTE_UNUSED,
5041 bfd_boolean olddef ATTRIBUTE_UNUSED,
5042 bfd *oldbfd ATTRIBUTE_UNUSED,
5043 const asection *oldsec ATTRIBUTE_UNUSED)
5044 {
5045 ((struct ppc_link_hash_entry *) h)->fake = 0;
5046 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5047 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
5048 return TRUE;
5049 }
5050
5051 /* This function makes an old ABI object reference to ".bar" cause the
5052 inclusion of a new ABI object archive that defines "bar".
5053 NAME is a symbol defined in an archive. Return a symbol in the hash
5054 table that might be satisfied by the archive symbols. */
5055
5056 static struct elf_link_hash_entry *
5057 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5058 struct bfd_link_info *info,
5059 const char *name)
5060 {
5061 struct elf_link_hash_entry *h;
5062 char *dot_name;
5063 size_t len;
5064
5065 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5066 if (h != NULL
5067 /* Don't return this sym if it is a fake function descriptor
5068 created by add_symbol_adjust. */
5069 && !((struct ppc_link_hash_entry *) h)->fake)
5070 return h;
5071
5072 if (name[0] == '.')
5073 return h;
5074
5075 len = strlen (name);
5076 dot_name = bfd_alloc (abfd, len + 2);
5077 if (dot_name == NULL)
5078 return (struct elf_link_hash_entry *) 0 - 1;
5079 dot_name[0] = '.';
5080 memcpy (dot_name + 1, name, len + 1);
5081 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5082 bfd_release (abfd, dot_name);
5083 return h;
5084 }
5085
5086 /* This function satisfies all old ABI object references to ".bar" if a
5087 new ABI object defines "bar". Well, at least, undefined dot symbols
5088 are made weak. This stops later archive searches from including an
5089 object if we already have a function descriptor definition. It also
5090 prevents the linker complaining about undefined symbols.
5091 We also check and correct mismatched symbol visibility here. The
5092 most restrictive visibility of the function descriptor and the
5093 function entry symbol is used. */
5094
5095 static bfd_boolean
5096 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5097 {
5098 struct ppc_link_hash_table *htab;
5099 struct ppc_link_hash_entry *fdh;
5100
5101 if (eh->elf.root.type == bfd_link_hash_warning)
5102 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5103
5104 if (eh->elf.root.type == bfd_link_hash_indirect)
5105 return TRUE;
5106
5107 if (eh->elf.root.root.string[0] != '.')
5108 abort ();
5109
5110 htab = ppc_hash_table (info);
5111 if (htab == NULL)
5112 return FALSE;
5113
5114 fdh = lookup_fdh (eh, htab);
5115 if (fdh == NULL
5116 && !bfd_link_relocatable (info)
5117 && (eh->elf.root.type == bfd_link_hash_undefined
5118 || eh->elf.root.type == bfd_link_hash_undefweak)
5119 && eh->elf.ref_regular)
5120 {
5121 /* Make an undefined function descriptor sym, in order to
5122 pull in an --as-needed shared lib. Archives are handled
5123 elsewhere. */
5124 fdh = make_fdh (info, eh);
5125 if (fdh == NULL)
5126 return FALSE;
5127 }
5128
5129 if (fdh != NULL)
5130 {
5131 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5132 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5133
5134 /* Make both descriptor and entry symbol have the most
5135 constraining visibility of either symbol. */
5136 if (entry_vis < descr_vis)
5137 fdh->elf.other += entry_vis - descr_vis;
5138 else if (entry_vis > descr_vis)
5139 eh->elf.other += descr_vis - entry_vis;
5140
5141 /* Propagate reference flags from entry symbol to function
5142 descriptor symbol. */
5143 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
5144 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
5145 fdh->elf.ref_regular |= eh->elf.ref_regular;
5146 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5147
5148 if (!fdh->elf.forced_local
5149 && fdh->elf.dynindx == -1
5150 && fdh->elf.versioned != versioned_hidden
5151 && (bfd_link_dll (info)
5152 || fdh->elf.def_dynamic
5153 || fdh->elf.ref_dynamic)
5154 && (eh->elf.ref_regular
5155 || eh->elf.def_regular))
5156 {
5157 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5158 return FALSE;
5159 }
5160 }
5161
5162 return TRUE;
5163 }
5164
5165 /* Set up opd section info and abiversion for IBFD, and process list
5166 of dot-symbols we made in link_hash_newfunc. */
5167
5168 static bfd_boolean
5169 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5170 {
5171 struct ppc_link_hash_table *htab;
5172 struct ppc_link_hash_entry **p, *eh;
5173 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5174
5175 if (opd != NULL && opd->size != 0)
5176 {
5177 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5178 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5179
5180 if (abiversion (ibfd) == 0)
5181 set_abiversion (ibfd, 1);
5182 else if (abiversion (ibfd) >= 2)
5183 {
5184 /* xgettext:c-format */
5185 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5186 " version %d\n"),
5187 ibfd, abiversion (ibfd));
5188 bfd_set_error (bfd_error_bad_value);
5189 return FALSE;
5190 }
5191 }
5192
5193 if (is_ppc64_elf (info->output_bfd))
5194 {
5195 /* For input files without an explicit abiversion in e_flags
5196 we should have flagged any with symbol st_other bits set
5197 as ELFv1 and above flagged those with .opd as ELFv2.
5198 Set the output abiversion if not yet set, and for any input
5199 still ambiguous, take its abiversion from the output.
5200 Differences in ABI are reported later. */
5201 if (abiversion (info->output_bfd) == 0)
5202 set_abiversion (info->output_bfd, abiversion (ibfd));
5203 else if (abiversion (ibfd) == 0)
5204 set_abiversion (ibfd, abiversion (info->output_bfd));
5205 }
5206
5207 htab = ppc_hash_table (info);
5208 if (htab == NULL)
5209 return TRUE;
5210
5211 if (opd != NULL && opd->size != 0
5212 && (ibfd->flags & DYNAMIC) == 0
5213 && (opd->flags & SEC_RELOC) != 0
5214 && opd->reloc_count != 0
5215 && !bfd_is_abs_section (opd->output_section)
5216 && info->gc_sections)
5217 {
5218 /* Garbage collection needs some extra help with .opd sections.
5219 We don't want to necessarily keep everything referenced by
5220 relocs in .opd, as that would keep all functions. Instead,
5221 if we reference an .opd symbol (a function descriptor), we
5222 want to keep the function code symbol's section. This is
5223 easy for global symbols, but for local syms we need to keep
5224 information about the associated function section. */
5225 bfd_size_type amt;
5226 asection **opd_sym_map;
5227 Elf_Internal_Shdr *symtab_hdr;
5228 Elf_Internal_Rela *relocs, *rel_end, *rel;
5229
5230 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5231 opd_sym_map = bfd_zalloc (ibfd, amt);
5232 if (opd_sym_map == NULL)
5233 return FALSE;
5234 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5235 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5236 info->keep_memory);
5237 if (relocs == NULL)
5238 return FALSE;
5239 symtab_hdr = &elf_symtab_hdr (ibfd);
5240 rel_end = relocs + opd->reloc_count - 1;
5241 for (rel = relocs; rel < rel_end; rel++)
5242 {
5243 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5244 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5245
5246 if (r_type == R_PPC64_ADDR64
5247 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5248 && r_symndx < symtab_hdr->sh_info)
5249 {
5250 Elf_Internal_Sym *isym;
5251 asection *s;
5252
5253 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5254 if (isym == NULL)
5255 {
5256 if (elf_section_data (opd)->relocs != relocs)
5257 free (relocs);
5258 return FALSE;
5259 }
5260
5261 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5262 if (s != NULL && s != opd)
5263 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5264 }
5265 }
5266 if (elf_section_data (opd)->relocs != relocs)
5267 free (relocs);
5268 }
5269
5270 p = &htab->dot_syms;
5271 while ((eh = *p) != NULL)
5272 {
5273 *p = NULL;
5274 if (&eh->elf == htab->elf.hgot)
5275 ;
5276 else if (htab->elf.hgot == NULL
5277 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5278 htab->elf.hgot = &eh->elf;
5279 else if (abiversion (ibfd) <= 1)
5280 {
5281 htab->need_func_desc_adj = 1;
5282 if (!add_symbol_adjust (eh, info))
5283 return FALSE;
5284 }
5285 p = &eh->u.next_dot_sym;
5286 }
5287 return TRUE;
5288 }
5289
5290 /* Undo hash table changes when an --as-needed input file is determined
5291 not to be needed. */
5292
5293 static bfd_boolean
5294 ppc64_elf_notice_as_needed (bfd *ibfd,
5295 struct bfd_link_info *info,
5296 enum notice_asneeded_action act)
5297 {
5298 if (act == notice_not_needed)
5299 {
5300 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5301
5302 if (htab == NULL)
5303 return FALSE;
5304
5305 htab->dot_syms = NULL;
5306 }
5307 return _bfd_elf_notice_as_needed (ibfd, info, act);
5308 }
5309
5310 /* If --just-symbols against a final linked binary, then assume we need
5311 toc adjusting stubs when calling functions defined there. */
5312
5313 static void
5314 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5315 {
5316 if ((sec->flags & SEC_CODE) != 0
5317 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5318 && is_ppc64_elf (sec->owner))
5319 {
5320 if (abiversion (sec->owner) >= 2
5321 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5322 sec->has_toc_reloc = 1;
5323 }
5324 _bfd_elf_link_just_syms (sec, info);
5325 }
5326
5327 static struct plt_entry **
5328 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5329 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5330 {
5331 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5332 struct plt_entry **local_plt;
5333 unsigned char *local_got_tls_masks;
5334
5335 if (local_got_ents == NULL)
5336 {
5337 bfd_size_type size = symtab_hdr->sh_info;
5338
5339 size *= (sizeof (*local_got_ents)
5340 + sizeof (*local_plt)
5341 + sizeof (*local_got_tls_masks));
5342 local_got_ents = bfd_zalloc (abfd, size);
5343 if (local_got_ents == NULL)
5344 return NULL;
5345 elf_local_got_ents (abfd) = local_got_ents;
5346 }
5347
5348 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5349 {
5350 struct got_entry *ent;
5351
5352 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5353 if (ent->addend == r_addend
5354 && ent->owner == abfd
5355 && ent->tls_type == tls_type)
5356 break;
5357 if (ent == NULL)
5358 {
5359 bfd_size_type amt = sizeof (*ent);
5360 ent = bfd_alloc (abfd, amt);
5361 if (ent == NULL)
5362 return FALSE;
5363 ent->next = local_got_ents[r_symndx];
5364 ent->addend = r_addend;
5365 ent->owner = abfd;
5366 ent->tls_type = tls_type;
5367 ent->is_indirect = FALSE;
5368 ent->got.refcount = 0;
5369 local_got_ents[r_symndx] = ent;
5370 }
5371 ent->got.refcount += 1;
5372 }
5373
5374 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5375 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5376 local_got_tls_masks[r_symndx] |= tls_type;
5377
5378 return local_plt + r_symndx;
5379 }
5380
5381 static bfd_boolean
5382 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5383 {
5384 struct plt_entry *ent;
5385
5386 for (ent = *plist; ent != NULL; ent = ent->next)
5387 if (ent->addend == addend)
5388 break;
5389 if (ent == NULL)
5390 {
5391 bfd_size_type amt = sizeof (*ent);
5392 ent = bfd_alloc (abfd, amt);
5393 if (ent == NULL)
5394 return FALSE;
5395 ent->next = *plist;
5396 ent->addend = addend;
5397 ent->plt.refcount = 0;
5398 *plist = ent;
5399 }
5400 ent->plt.refcount += 1;
5401 return TRUE;
5402 }
5403
5404 static bfd_boolean
5405 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5406 {
5407 return (r_type == R_PPC64_REL24
5408 || r_type == R_PPC64_REL14
5409 || r_type == R_PPC64_REL14_BRTAKEN
5410 || r_type == R_PPC64_REL14_BRNTAKEN
5411 || r_type == R_PPC64_ADDR24
5412 || r_type == R_PPC64_ADDR14
5413 || r_type == R_PPC64_ADDR14_BRTAKEN
5414 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5415 }
5416
5417 /* Look through the relocs for a section during the first phase, and
5418 calculate needed space in the global offset table, procedure
5419 linkage table, and dynamic reloc sections. */
5420
5421 static bfd_boolean
5422 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5423 asection *sec, const Elf_Internal_Rela *relocs)
5424 {
5425 struct ppc_link_hash_table *htab;
5426 Elf_Internal_Shdr *symtab_hdr;
5427 struct elf_link_hash_entry **sym_hashes;
5428 const Elf_Internal_Rela *rel;
5429 const Elf_Internal_Rela *rel_end;
5430 asection *sreloc;
5431 struct elf_link_hash_entry *tga, *dottga;
5432 bfd_boolean is_opd;
5433
5434 if (bfd_link_relocatable (info))
5435 return TRUE;
5436
5437 /* Don't do anything special with non-loaded, non-alloced sections.
5438 In particular, any relocs in such sections should not affect GOT
5439 and PLT reference counting (ie. we don't allow them to create GOT
5440 or PLT entries), there's no possibility or desire to optimize TLS
5441 relocs, and there's not much point in propagating relocs to shared
5442 libs that the dynamic linker won't relocate. */
5443 if ((sec->flags & SEC_ALLOC) == 0)
5444 return TRUE;
5445
5446 BFD_ASSERT (is_ppc64_elf (abfd));
5447
5448 htab = ppc_hash_table (info);
5449 if (htab == NULL)
5450 return FALSE;
5451
5452 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5453 FALSE, FALSE, TRUE);
5454 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5455 FALSE, FALSE, TRUE);
5456 symtab_hdr = &elf_symtab_hdr (abfd);
5457 sym_hashes = elf_sym_hashes (abfd);
5458 sreloc = NULL;
5459 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5460 rel_end = relocs + sec->reloc_count;
5461 for (rel = relocs; rel < rel_end; rel++)
5462 {
5463 unsigned long r_symndx;
5464 struct elf_link_hash_entry *h;
5465 enum elf_ppc64_reloc_type r_type;
5466 int tls_type;
5467 struct _ppc64_elf_section_data *ppc64_sec;
5468 struct plt_entry **ifunc, **plt_list;
5469
5470 r_symndx = ELF64_R_SYM (rel->r_info);
5471 if (r_symndx < symtab_hdr->sh_info)
5472 h = NULL;
5473 else
5474 {
5475 struct ppc_link_hash_entry *eh;
5476
5477 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5478 h = elf_follow_link (h);
5479 eh = (struct ppc_link_hash_entry *) h;
5480
5481 /* PR15323, ref flags aren't set for references in the same
5482 object. */
5483 h->root.non_ir_ref_regular = 1;
5484 if (eh->is_func && eh->oh != NULL)
5485 eh->oh->elf.root.non_ir_ref_regular = 1;
5486
5487 if (h == htab->elf.hgot)
5488 sec->has_toc_reloc = 1;
5489 }
5490
5491 tls_type = 0;
5492 ifunc = NULL;
5493 if (h != NULL)
5494 {
5495 if (h->type == STT_GNU_IFUNC)
5496 {
5497 h->needs_plt = 1;
5498 ifunc = &h->plt.plist;
5499 }
5500 }
5501 else
5502 {
5503 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5504 abfd, r_symndx);
5505 if (isym == NULL)
5506 return FALSE;
5507
5508 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5509 {
5510 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5511 rel->r_addend, PLT_IFUNC);
5512 if (ifunc == NULL)
5513 return FALSE;
5514 }
5515 }
5516
5517 r_type = ELF64_R_TYPE (rel->r_info);
5518 switch (r_type)
5519 {
5520 case R_PPC64_TLSGD:
5521 case R_PPC64_TLSLD:
5522 /* These special tls relocs tie a call to __tls_get_addr with
5523 its parameter symbol. */
5524 break;
5525
5526 case R_PPC64_GOT_TLSLD16:
5527 case R_PPC64_GOT_TLSLD16_LO:
5528 case R_PPC64_GOT_TLSLD16_HI:
5529 case R_PPC64_GOT_TLSLD16_HA:
5530 tls_type = TLS_TLS | TLS_LD;
5531 goto dogottls;
5532
5533 case R_PPC64_GOT_TLSGD16:
5534 case R_PPC64_GOT_TLSGD16_LO:
5535 case R_PPC64_GOT_TLSGD16_HI:
5536 case R_PPC64_GOT_TLSGD16_HA:
5537 tls_type = TLS_TLS | TLS_GD;
5538 goto dogottls;
5539
5540 case R_PPC64_GOT_TPREL16_DS:
5541 case R_PPC64_GOT_TPREL16_LO_DS:
5542 case R_PPC64_GOT_TPREL16_HI:
5543 case R_PPC64_GOT_TPREL16_HA:
5544 if (bfd_link_dll (info))
5545 info->flags |= DF_STATIC_TLS;
5546 tls_type = TLS_TLS | TLS_TPREL;
5547 goto dogottls;
5548
5549 case R_PPC64_GOT_DTPREL16_DS:
5550 case R_PPC64_GOT_DTPREL16_LO_DS:
5551 case R_PPC64_GOT_DTPREL16_HI:
5552 case R_PPC64_GOT_DTPREL16_HA:
5553 tls_type = TLS_TLS | TLS_DTPREL;
5554 dogottls:
5555 sec->has_tls_reloc = 1;
5556 /* Fall through */
5557
5558 case R_PPC64_GOT16:
5559 case R_PPC64_GOT16_DS:
5560 case R_PPC64_GOT16_HA:
5561 case R_PPC64_GOT16_HI:
5562 case R_PPC64_GOT16_LO:
5563 case R_PPC64_GOT16_LO_DS:
5564 /* This symbol requires a global offset table entry. */
5565 sec->has_toc_reloc = 1;
5566 if (r_type == R_PPC64_GOT_TLSLD16
5567 || r_type == R_PPC64_GOT_TLSGD16
5568 || r_type == R_PPC64_GOT_TPREL16_DS
5569 || r_type == R_PPC64_GOT_DTPREL16_DS
5570 || r_type == R_PPC64_GOT16
5571 || r_type == R_PPC64_GOT16_DS)
5572 {
5573 htab->do_multi_toc = 1;
5574 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5575 }
5576
5577 if (ppc64_elf_tdata (abfd)->got == NULL
5578 && !create_got_section (abfd, info))
5579 return FALSE;
5580
5581 if (h != NULL)
5582 {
5583 struct ppc_link_hash_entry *eh;
5584 struct got_entry *ent;
5585
5586 eh = (struct ppc_link_hash_entry *) h;
5587 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5588 if (ent->addend == rel->r_addend
5589 && ent->owner == abfd
5590 && ent->tls_type == tls_type)
5591 break;
5592 if (ent == NULL)
5593 {
5594 bfd_size_type amt = sizeof (*ent);
5595 ent = bfd_alloc (abfd, amt);
5596 if (ent == NULL)
5597 return FALSE;
5598 ent->next = eh->elf.got.glist;
5599 ent->addend = rel->r_addend;
5600 ent->owner = abfd;
5601 ent->tls_type = tls_type;
5602 ent->is_indirect = FALSE;
5603 ent->got.refcount = 0;
5604 eh->elf.got.glist = ent;
5605 }
5606 ent->got.refcount += 1;
5607 eh->tls_mask |= tls_type;
5608 }
5609 else
5610 /* This is a global offset table entry for a local symbol. */
5611 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5612 rel->r_addend, tls_type))
5613 return FALSE;
5614
5615 /* We may also need a plt entry if the symbol turns out to be
5616 an ifunc. */
5617 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5618 {
5619 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5620 return FALSE;
5621 }
5622 break;
5623
5624 case R_PPC64_PLT16_HA:
5625 case R_PPC64_PLT16_HI:
5626 case R_PPC64_PLT16_LO:
5627 case R_PPC64_PLT32:
5628 case R_PPC64_PLT64:
5629 /* This symbol requires a procedure linkage table entry. */
5630 plt_list = ifunc;
5631 if (h != NULL)
5632 {
5633 h->needs_plt = 1;
5634 if (h->root.root.string[0] == '.'
5635 && h->root.root.string[1] != '\0')
5636 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5637 plt_list = &h->plt.plist;
5638 }
5639 if (plt_list == NULL)
5640 {
5641 /* It does not make sense to have a procedure linkage
5642 table entry for a non-ifunc local symbol. */
5643 info->callbacks->einfo
5644 /* xgettext:c-format */
5645 (_("%H: %s reloc against local symbol\n"),
5646 abfd, sec, rel->r_offset,
5647 ppc64_elf_howto_table[r_type]->name);
5648 bfd_set_error (bfd_error_bad_value);
5649 return FALSE;
5650 }
5651 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5652 return FALSE;
5653 break;
5654
5655 /* The following relocations don't need to propagate the
5656 relocation if linking a shared object since they are
5657 section relative. */
5658 case R_PPC64_SECTOFF:
5659 case R_PPC64_SECTOFF_LO:
5660 case R_PPC64_SECTOFF_HI:
5661 case R_PPC64_SECTOFF_HA:
5662 case R_PPC64_SECTOFF_DS:
5663 case R_PPC64_SECTOFF_LO_DS:
5664 case R_PPC64_DTPREL16:
5665 case R_PPC64_DTPREL16_LO:
5666 case R_PPC64_DTPREL16_HI:
5667 case R_PPC64_DTPREL16_HA:
5668 case R_PPC64_DTPREL16_DS:
5669 case R_PPC64_DTPREL16_LO_DS:
5670 case R_PPC64_DTPREL16_HIGH:
5671 case R_PPC64_DTPREL16_HIGHA:
5672 case R_PPC64_DTPREL16_HIGHER:
5673 case R_PPC64_DTPREL16_HIGHERA:
5674 case R_PPC64_DTPREL16_HIGHEST:
5675 case R_PPC64_DTPREL16_HIGHESTA:
5676 break;
5677
5678 /* Nor do these. */
5679 case R_PPC64_REL16:
5680 case R_PPC64_REL16_LO:
5681 case R_PPC64_REL16_HI:
5682 case R_PPC64_REL16_HA:
5683 case R_PPC64_REL16DX_HA:
5684 break;
5685
5686 /* Not supported as a dynamic relocation. */
5687 case R_PPC64_ADDR64_LOCAL:
5688 if (bfd_link_pic (info))
5689 {
5690 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5691 ppc_howto_init ();
5692 /* xgettext:c-format */
5693 info->callbacks->einfo (_("%H: %s reloc unsupported "
5694 "in shared libraries and PIEs.\n"),
5695 abfd, sec, rel->r_offset,
5696 ppc64_elf_howto_table[r_type]->name);
5697 bfd_set_error (bfd_error_bad_value);
5698 return FALSE;
5699 }
5700 break;
5701
5702 case R_PPC64_TOC16:
5703 case R_PPC64_TOC16_DS:
5704 htab->do_multi_toc = 1;
5705 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5706 /* Fall through. */
5707 case R_PPC64_TOC16_LO:
5708 case R_PPC64_TOC16_HI:
5709 case R_PPC64_TOC16_HA:
5710 case R_PPC64_TOC16_LO_DS:
5711 sec->has_toc_reloc = 1;
5712 break;
5713
5714 /* Marker reloc. */
5715 case R_PPC64_ENTRY:
5716 break;
5717
5718 /* This relocation describes the C++ object vtable hierarchy.
5719 Reconstruct it for later use during GC. */
5720 case R_PPC64_GNU_VTINHERIT:
5721 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5722 return FALSE;
5723 break;
5724
5725 /* This relocation describes which C++ vtable entries are actually
5726 used. Record for later use during GC. */
5727 case R_PPC64_GNU_VTENTRY:
5728 BFD_ASSERT (h != NULL);
5729 if (h != NULL
5730 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5731 return FALSE;
5732 break;
5733
5734 case R_PPC64_REL14:
5735 case R_PPC64_REL14_BRTAKEN:
5736 case R_PPC64_REL14_BRNTAKEN:
5737 {
5738 asection *dest = NULL;
5739
5740 /* Heuristic: If jumping outside our section, chances are
5741 we are going to need a stub. */
5742 if (h != NULL)
5743 {
5744 /* If the sym is weak it may be overridden later, so
5745 don't assume we know where a weak sym lives. */
5746 if (h->root.type == bfd_link_hash_defined)
5747 dest = h->root.u.def.section;
5748 }
5749 else
5750 {
5751 Elf_Internal_Sym *isym;
5752
5753 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5754 abfd, r_symndx);
5755 if (isym == NULL)
5756 return FALSE;
5757
5758 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5759 }
5760
5761 if (dest != sec)
5762 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5763 }
5764 /* Fall through. */
5765
5766 case R_PPC64_REL24:
5767 plt_list = ifunc;
5768 if (h != NULL)
5769 {
5770 h->needs_plt = 1;
5771 if (h->root.root.string[0] == '.'
5772 && h->root.root.string[1] != '\0')
5773 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5774
5775 if (h == tga || h == dottga)
5776 {
5777 sec->has_tls_reloc = 1;
5778 if (rel != relocs
5779 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5780 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5781 /* We have a new-style __tls_get_addr call with
5782 a marker reloc. */
5783 ;
5784 else
5785 /* Mark this section as having an old-style call. */
5786 sec->has_tls_get_addr_call = 1;
5787 }
5788 plt_list = &h->plt.plist;
5789 }
5790
5791 /* We may need a .plt entry if the function this reloc
5792 refers to is in a shared lib. */
5793 if (plt_list
5794 && !update_plt_info (abfd, plt_list, rel->r_addend))
5795 return FALSE;
5796 break;
5797
5798 case R_PPC64_ADDR14:
5799 case R_PPC64_ADDR14_BRNTAKEN:
5800 case R_PPC64_ADDR14_BRTAKEN:
5801 case R_PPC64_ADDR24:
5802 goto dodyn;
5803
5804 case R_PPC64_TPREL64:
5805 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5806 if (bfd_link_dll (info))
5807 info->flags |= DF_STATIC_TLS;
5808 goto dotlstoc;
5809
5810 case R_PPC64_DTPMOD64:
5811 if (rel + 1 < rel_end
5812 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5813 && rel[1].r_offset == rel->r_offset + 8)
5814 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5815 else
5816 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5817 goto dotlstoc;
5818
5819 case R_PPC64_DTPREL64:
5820 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5821 if (rel != relocs
5822 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5823 && rel[-1].r_offset == rel->r_offset - 8)
5824 /* This is the second reloc of a dtpmod, dtprel pair.
5825 Don't mark with TLS_DTPREL. */
5826 goto dodyn;
5827
5828 dotlstoc:
5829 sec->has_tls_reloc = 1;
5830 if (h != NULL)
5831 {
5832 struct ppc_link_hash_entry *eh;
5833 eh = (struct ppc_link_hash_entry *) h;
5834 eh->tls_mask |= tls_type;
5835 }
5836 else
5837 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5838 rel->r_addend, tls_type))
5839 return FALSE;
5840
5841 ppc64_sec = ppc64_elf_section_data (sec);
5842 if (ppc64_sec->sec_type != sec_toc)
5843 {
5844 bfd_size_type amt;
5845
5846 /* One extra to simplify get_tls_mask. */
5847 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5848 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5849 if (ppc64_sec->u.toc.symndx == NULL)
5850 return FALSE;
5851 amt = sec->size * sizeof (bfd_vma) / 8;
5852 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5853 if (ppc64_sec->u.toc.add == NULL)
5854 return FALSE;
5855 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5856 ppc64_sec->sec_type = sec_toc;
5857 }
5858 BFD_ASSERT (rel->r_offset % 8 == 0);
5859 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5860 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5861
5862 /* Mark the second slot of a GD or LD entry.
5863 -1 to indicate GD and -2 to indicate LD. */
5864 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5865 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5866 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5867 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5868 goto dodyn;
5869
5870 case R_PPC64_TPREL16:
5871 case R_PPC64_TPREL16_LO:
5872 case R_PPC64_TPREL16_HI:
5873 case R_PPC64_TPREL16_HA:
5874 case R_PPC64_TPREL16_DS:
5875 case R_PPC64_TPREL16_LO_DS:
5876 case R_PPC64_TPREL16_HIGH:
5877 case R_PPC64_TPREL16_HIGHA:
5878 case R_PPC64_TPREL16_HIGHER:
5879 case R_PPC64_TPREL16_HIGHERA:
5880 case R_PPC64_TPREL16_HIGHEST:
5881 case R_PPC64_TPREL16_HIGHESTA:
5882 if (bfd_link_dll (info))
5883 info->flags |= DF_STATIC_TLS;
5884 goto dodyn;
5885
5886 case R_PPC64_ADDR64:
5887 if (is_opd
5888 && rel + 1 < rel_end
5889 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5890 {
5891 if (h != NULL)
5892 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5893 }
5894 /* Fall through. */
5895
5896 case R_PPC64_ADDR16:
5897 case R_PPC64_ADDR16_DS:
5898 case R_PPC64_ADDR16_HA:
5899 case R_PPC64_ADDR16_HI:
5900 case R_PPC64_ADDR16_HIGH:
5901 case R_PPC64_ADDR16_HIGHA:
5902 case R_PPC64_ADDR16_HIGHER:
5903 case R_PPC64_ADDR16_HIGHERA:
5904 case R_PPC64_ADDR16_HIGHEST:
5905 case R_PPC64_ADDR16_HIGHESTA:
5906 case R_PPC64_ADDR16_LO:
5907 case R_PPC64_ADDR16_LO_DS:
5908 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5909 && rel->r_addend == 0)
5910 {
5911 /* We may need a .plt entry if this reloc refers to a
5912 function in a shared lib. */
5913 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5914 return FALSE;
5915 h->pointer_equality_needed = 1;
5916 }
5917 /* Fall through. */
5918
5919 case R_PPC64_REL30:
5920 case R_PPC64_REL32:
5921 case R_PPC64_REL64:
5922 case R_PPC64_ADDR32:
5923 case R_PPC64_UADDR16:
5924 case R_PPC64_UADDR32:
5925 case R_PPC64_UADDR64:
5926 case R_PPC64_TOC:
5927 if (h != NULL && !bfd_link_pic (info))
5928 /* We may need a copy reloc. */
5929 h->non_got_ref = 1;
5930
5931 /* Don't propagate .opd relocs. */
5932 if (NO_OPD_RELOCS && is_opd)
5933 break;
5934
5935 /* If we are creating a shared library, and this is a reloc
5936 against a global symbol, or a non PC relative reloc
5937 against a local symbol, then we need to copy the reloc
5938 into the shared library. However, if we are linking with
5939 -Bsymbolic, we do not need to copy a reloc against a
5940 global symbol which is defined in an object we are
5941 including in the link (i.e., DEF_REGULAR is set). At
5942 this point we have not seen all the input files, so it is
5943 possible that DEF_REGULAR is not set now but will be set
5944 later (it is never cleared). In case of a weak definition,
5945 DEF_REGULAR may be cleared later by a strong definition in
5946 a shared library. We account for that possibility below by
5947 storing information in the dyn_relocs field of the hash
5948 table entry. A similar situation occurs when creating
5949 shared libraries and symbol visibility changes render the
5950 symbol local.
5951
5952 If on the other hand, we are creating an executable, we
5953 may need to keep relocations for symbols satisfied by a
5954 dynamic library if we manage to avoid copy relocs for the
5955 symbol. */
5956 dodyn:
5957 if ((bfd_link_pic (info)
5958 && (must_be_dyn_reloc (info, r_type)
5959 || (h != NULL
5960 && (!SYMBOLIC_BIND (info, h)
5961 || h->root.type == bfd_link_hash_defweak
5962 || !h->def_regular))))
5963 || (ELIMINATE_COPY_RELOCS
5964 && !bfd_link_pic (info)
5965 && h != NULL
5966 && (h->root.type == bfd_link_hash_defweak
5967 || !h->def_regular))
5968 || (!bfd_link_pic (info)
5969 && ifunc != NULL))
5970 {
5971 /* We must copy these reloc types into the output file.
5972 Create a reloc section in dynobj and make room for
5973 this reloc. */
5974 if (sreloc == NULL)
5975 {
5976 sreloc = _bfd_elf_make_dynamic_reloc_section
5977 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5978
5979 if (sreloc == NULL)
5980 return FALSE;
5981 }
5982
5983 /* If this is a global symbol, we count the number of
5984 relocations we need for this symbol. */
5985 if (h != NULL)
5986 {
5987 struct elf_dyn_relocs *p;
5988 struct elf_dyn_relocs **head;
5989
5990 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5991 p = *head;
5992 if (p == NULL || p->sec != sec)
5993 {
5994 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5995 if (p == NULL)
5996 return FALSE;
5997 p->next = *head;
5998 *head = p;
5999 p->sec = sec;
6000 p->count = 0;
6001 p->pc_count = 0;
6002 }
6003 p->count += 1;
6004 if (!must_be_dyn_reloc (info, r_type))
6005 p->pc_count += 1;
6006 }
6007 else
6008 {
6009 /* Track dynamic relocs needed for local syms too.
6010 We really need local syms available to do this
6011 easily. Oh well. */
6012 struct ppc_dyn_relocs *p;
6013 struct ppc_dyn_relocs **head;
6014 bfd_boolean is_ifunc;
6015 asection *s;
6016 void *vpp;
6017 Elf_Internal_Sym *isym;
6018
6019 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6020 abfd, r_symndx);
6021 if (isym == NULL)
6022 return FALSE;
6023
6024 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6025 if (s == NULL)
6026 s = sec;
6027
6028 vpp = &elf_section_data (s)->local_dynrel;
6029 head = (struct ppc_dyn_relocs **) vpp;
6030 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6031 p = *head;
6032 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6033 p = p->next;
6034 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6035 {
6036 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6037 if (p == NULL)
6038 return FALSE;
6039 p->next = *head;
6040 *head = p;
6041 p->sec = sec;
6042 p->ifunc = is_ifunc;
6043 p->count = 0;
6044 }
6045 p->count += 1;
6046 }
6047 }
6048 break;
6049
6050 default:
6051 break;
6052 }
6053 }
6054
6055 return TRUE;
6056 }
6057
6058 /* Merge backend specific data from an object file to the output
6059 object file when linking. */
6060
6061 static bfd_boolean
6062 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6063 {
6064 bfd *obfd = info->output_bfd;
6065 unsigned long iflags, oflags;
6066
6067 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6068 return TRUE;
6069
6070 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6071 return TRUE;
6072
6073 if (!_bfd_generic_verify_endian_match (ibfd, info))
6074 return FALSE;
6075
6076 iflags = elf_elfheader (ibfd)->e_flags;
6077 oflags = elf_elfheader (obfd)->e_flags;
6078
6079 if (iflags & ~EF_PPC64_ABI)
6080 {
6081 _bfd_error_handler
6082 /* xgettext:c-format */
6083 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
6084 bfd_set_error (bfd_error_bad_value);
6085 return FALSE;
6086 }
6087 else if (iflags != oflags && iflags != 0)
6088 {
6089 _bfd_error_handler
6090 /* xgettext:c-format */
6091 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
6092 ibfd, iflags, oflags);
6093 bfd_set_error (bfd_error_bad_value);
6094 return FALSE;
6095 }
6096
6097 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
6098
6099 /* Merge Tag_compatibility attributes and any common GNU ones. */
6100 _bfd_elf_merge_object_attributes (ibfd, info);
6101
6102 return TRUE;
6103 }
6104
6105 static bfd_boolean
6106 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6107 {
6108 /* Print normal ELF private data. */
6109 _bfd_elf_print_private_bfd_data (abfd, ptr);
6110
6111 if (elf_elfheader (abfd)->e_flags != 0)
6112 {
6113 FILE *file = ptr;
6114
6115 fprintf (file, _("private flags = 0x%lx:"),
6116 elf_elfheader (abfd)->e_flags);
6117
6118 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6119 fprintf (file, _(" [abiv%ld]"),
6120 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6121 fputc ('\n', file);
6122 }
6123
6124 return TRUE;
6125 }
6126
6127 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
6128 of the code entry point, and its section, which must be in the same
6129 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
6130
6131 static bfd_vma
6132 opd_entry_value (asection *opd_sec,
6133 bfd_vma offset,
6134 asection **code_sec,
6135 bfd_vma *code_off,
6136 bfd_boolean in_code_sec)
6137 {
6138 bfd *opd_bfd = opd_sec->owner;
6139 Elf_Internal_Rela *relocs;
6140 Elf_Internal_Rela *lo, *hi, *look;
6141 bfd_vma val;
6142
6143 /* No relocs implies we are linking a --just-symbols object, or looking
6144 at a final linked executable with addr2line or somesuch. */
6145 if (opd_sec->reloc_count == 0)
6146 {
6147 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6148
6149 if (contents == NULL)
6150 {
6151 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6152 return (bfd_vma) -1;
6153 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6154 }
6155
6156 /* PR 17512: file: 64b9dfbb. */
6157 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6158 return (bfd_vma) -1;
6159
6160 val = bfd_get_64 (opd_bfd, contents + offset);
6161 if (code_sec != NULL)
6162 {
6163 asection *sec, *likely = NULL;
6164
6165 if (in_code_sec)
6166 {
6167 sec = *code_sec;
6168 if (sec->vma <= val
6169 && val < sec->vma + sec->size)
6170 likely = sec;
6171 else
6172 val = -1;
6173 }
6174 else
6175 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6176 if (sec->vma <= val
6177 && (sec->flags & SEC_LOAD) != 0
6178 && (sec->flags & SEC_ALLOC) != 0)
6179 likely = sec;
6180 if (likely != NULL)
6181 {
6182 *code_sec = likely;
6183 if (code_off != NULL)
6184 *code_off = val - likely->vma;
6185 }
6186 }
6187 return val;
6188 }
6189
6190 BFD_ASSERT (is_ppc64_elf (opd_bfd));
6191
6192 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6193 if (relocs == NULL)
6194 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6195 /* PR 17512: file: df8e1fd6. */
6196 if (relocs == NULL)
6197 return (bfd_vma) -1;
6198
6199 /* Go find the opd reloc at the sym address. */
6200 lo = relocs;
6201 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6202 val = (bfd_vma) -1;
6203 while (lo < hi)
6204 {
6205 look = lo + (hi - lo) / 2;
6206 if (look->r_offset < offset)
6207 lo = look + 1;
6208 else if (look->r_offset > offset)
6209 hi = look;
6210 else
6211 {
6212 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6213
6214 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6215 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6216 {
6217 unsigned long symndx = ELF64_R_SYM (look->r_info);
6218 asection *sec = NULL;
6219
6220 if (symndx >= symtab_hdr->sh_info
6221 && elf_sym_hashes (opd_bfd) != NULL)
6222 {
6223 struct elf_link_hash_entry **sym_hashes;
6224 struct elf_link_hash_entry *rh;
6225
6226 sym_hashes = elf_sym_hashes (opd_bfd);
6227 rh = sym_hashes[symndx - symtab_hdr->sh_info];
6228 if (rh != NULL)
6229 {
6230 rh = elf_follow_link (rh);
6231 if (rh->root.type != bfd_link_hash_defined
6232 && rh->root.type != bfd_link_hash_defweak)
6233 break;
6234 if (rh->root.u.def.section->owner == opd_bfd)
6235 {
6236 val = rh->root.u.def.value;
6237 sec = rh->root.u.def.section;
6238 }
6239 }
6240 }
6241
6242 if (sec == NULL)
6243 {
6244 Elf_Internal_Sym *sym;
6245
6246 if (symndx < symtab_hdr->sh_info)
6247 {
6248 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6249 if (sym == NULL)
6250 {
6251 size_t symcnt = symtab_hdr->sh_info;
6252 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6253 symcnt, 0,
6254 NULL, NULL, NULL);
6255 if (sym == NULL)
6256 break;
6257 symtab_hdr->contents = (bfd_byte *) sym;
6258 }
6259 sym += symndx;
6260 }
6261 else
6262 {
6263 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6264 1, symndx,
6265 NULL, NULL, NULL);
6266 if (sym == NULL)
6267 break;
6268 }
6269 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6270 if (sec == NULL)
6271 break;
6272 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6273 val = sym->st_value;
6274 }
6275
6276 val += look->r_addend;
6277 if (code_off != NULL)
6278 *code_off = val;
6279 if (code_sec != NULL)
6280 {
6281 if (in_code_sec && *code_sec != sec)
6282 return -1;
6283 else
6284 *code_sec = sec;
6285 }
6286 if (sec->output_section != NULL)
6287 val += sec->output_section->vma + sec->output_offset;
6288 }
6289 break;
6290 }
6291 }
6292
6293 return val;
6294 }
6295
6296 /* If the ELF symbol SYM might be a function in SEC, return the
6297 function size and set *CODE_OFF to the function's entry point,
6298 otherwise return zero. */
6299
6300 static bfd_size_type
6301 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6302 bfd_vma *code_off)
6303 {
6304 bfd_size_type size;
6305
6306 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6307 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6308 return 0;
6309
6310 size = 0;
6311 if (!(sym->flags & BSF_SYNTHETIC))
6312 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6313
6314 if (strcmp (sym->section->name, ".opd") == 0)
6315 {
6316 struct _opd_sec_data *opd = get_opd_info (sym->section);
6317 bfd_vma symval = sym->value;
6318
6319 if (opd != NULL
6320 && opd->adjust != NULL
6321 && elf_section_data (sym->section)->relocs != NULL)
6322 {
6323 /* opd_entry_value will use cached relocs that have been
6324 adjusted, but with raw symbols. That means both local
6325 and global symbols need adjusting. */
6326 long adjust = opd->adjust[OPD_NDX (symval)];
6327 if (adjust == -1)
6328 return 0;
6329 symval += adjust;
6330 }
6331
6332 if (opd_entry_value (sym->section, symval,
6333 &sec, code_off, TRUE) == (bfd_vma) -1)
6334 return 0;
6335 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6336 symbol. This size has nothing to do with the code size of the
6337 function, which is what we're supposed to return, but the
6338 code size isn't available without looking up the dot-sym.
6339 However, doing that would be a waste of time particularly
6340 since elf_find_function will look at the dot-sym anyway.
6341 Now, elf_find_function will keep the largest size of any
6342 function sym found at the code address of interest, so return
6343 1 here to avoid it incorrectly caching a larger function size
6344 for a small function. This does mean we return the wrong
6345 size for a new-ABI function of size 24, but all that does is
6346 disable caching for such functions. */
6347 if (size == 24)
6348 size = 1;
6349 }
6350 else
6351 {
6352 if (sym->section != sec)
6353 return 0;
6354 *code_off = sym->value;
6355 }
6356 if (size == 0)
6357 size = 1;
6358 return size;
6359 }
6360
6361 /* Return true if symbol is a strong function defined in an ELFv2
6362 object with st_other localentry bits of zero, ie. its local entry
6363 point coincides with its global entry point. */
6364
6365 static bfd_boolean
6366 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6367 {
6368 return (h != NULL
6369 && h->type == STT_FUNC
6370 && h->root.type == bfd_link_hash_defined
6371 && (STO_PPC64_LOCAL_MASK & h->other) == 0
6372 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
6373 && is_ppc64_elf (h->root.u.def.section->owner)
6374 && abiversion (h->root.u.def.section->owner) >= 2);
6375 }
6376
6377 /* Return true if symbol is defined in a regular object file. */
6378
6379 static bfd_boolean
6380 is_static_defined (struct elf_link_hash_entry *h)
6381 {
6382 return ((h->root.type == bfd_link_hash_defined
6383 || h->root.type == bfd_link_hash_defweak)
6384 && h->root.u.def.section != NULL
6385 && h->root.u.def.section->output_section != NULL);
6386 }
6387
6388 /* If FDH is a function descriptor symbol, return the associated code
6389 entry symbol if it is defined. Return NULL otherwise. */
6390
6391 static struct ppc_link_hash_entry *
6392 defined_code_entry (struct ppc_link_hash_entry *fdh)
6393 {
6394 if (fdh->is_func_descriptor)
6395 {
6396 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6397 if (fh->elf.root.type == bfd_link_hash_defined
6398 || fh->elf.root.type == bfd_link_hash_defweak)
6399 return fh;
6400 }
6401 return NULL;
6402 }
6403
6404 /* If FH is a function code entry symbol, return the associated
6405 function descriptor symbol if it is defined. Return NULL otherwise. */
6406
6407 static struct ppc_link_hash_entry *
6408 defined_func_desc (struct ppc_link_hash_entry *fh)
6409 {
6410 if (fh->oh != NULL
6411 && fh->oh->is_func_descriptor)
6412 {
6413 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6414 if (fdh->elf.root.type == bfd_link_hash_defined
6415 || fdh->elf.root.type == bfd_link_hash_defweak)
6416 return fdh;
6417 }
6418 return NULL;
6419 }
6420
6421 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6422
6423 /* Garbage collect sections, after first dealing with dot-symbols. */
6424
6425 static bfd_boolean
6426 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6427 {
6428 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6429
6430 if (htab != NULL && htab->need_func_desc_adj)
6431 {
6432 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6433 htab->need_func_desc_adj = 0;
6434 }
6435 return bfd_elf_gc_sections (abfd, info);
6436 }
6437
6438 /* Mark all our entry sym sections, both opd and code section. */
6439
6440 static void
6441 ppc64_elf_gc_keep (struct bfd_link_info *info)
6442 {
6443 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6444 struct bfd_sym_chain *sym;
6445
6446 if (htab == NULL)
6447 return;
6448
6449 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6450 {
6451 struct ppc_link_hash_entry *eh, *fh;
6452 asection *sec;
6453
6454 eh = (struct ppc_link_hash_entry *)
6455 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6456 if (eh == NULL)
6457 continue;
6458 if (eh->elf.root.type != bfd_link_hash_defined
6459 && eh->elf.root.type != bfd_link_hash_defweak)
6460 continue;
6461
6462 fh = defined_code_entry (eh);
6463 if (fh != NULL)
6464 {
6465 sec = fh->elf.root.u.def.section;
6466 sec->flags |= SEC_KEEP;
6467 }
6468 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6469 && opd_entry_value (eh->elf.root.u.def.section,
6470 eh->elf.root.u.def.value,
6471 &sec, NULL, FALSE) != (bfd_vma) -1)
6472 sec->flags |= SEC_KEEP;
6473
6474 sec = eh->elf.root.u.def.section;
6475 sec->flags |= SEC_KEEP;
6476 }
6477 }
6478
6479 /* Mark sections containing dynamically referenced symbols. When
6480 building shared libraries, we must assume that any visible symbol is
6481 referenced. */
6482
6483 static bfd_boolean
6484 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6485 {
6486 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6487 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6488 struct ppc_link_hash_entry *fdh;
6489 struct bfd_elf_dynamic_list *d = info->dynamic_list;
6490
6491 /* Dynamic linking info is on the func descriptor sym. */
6492 fdh = defined_func_desc (eh);
6493 if (fdh != NULL)
6494 eh = fdh;
6495
6496 if ((eh->elf.root.type == bfd_link_hash_defined
6497 || eh->elf.root.type == bfd_link_hash_defweak)
6498 && (eh->elf.ref_dynamic
6499 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6500 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6501 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6502 && (!bfd_link_executable (info)
6503 || info->gc_keep_exported
6504 || info->export_dynamic
6505 || (eh->elf.dynamic
6506 && d != NULL
6507 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6508 && (eh->elf.versioned >= versioned
6509 || !bfd_hide_sym_by_version (info->version_info,
6510 eh->elf.root.root.string)))))
6511 {
6512 asection *code_sec;
6513 struct ppc_link_hash_entry *fh;
6514
6515 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6516
6517 /* Function descriptor syms cause the associated
6518 function code sym section to be marked. */
6519 fh = defined_code_entry (eh);
6520 if (fh != NULL)
6521 {
6522 code_sec = fh->elf.root.u.def.section;
6523 code_sec->flags |= SEC_KEEP;
6524 }
6525 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6526 && opd_entry_value (eh->elf.root.u.def.section,
6527 eh->elf.root.u.def.value,
6528 &code_sec, NULL, FALSE) != (bfd_vma) -1)
6529 code_sec->flags |= SEC_KEEP;
6530 }
6531
6532 return TRUE;
6533 }
6534
6535 /* Return the section that should be marked against GC for a given
6536 relocation. */
6537
6538 static asection *
6539 ppc64_elf_gc_mark_hook (asection *sec,
6540 struct bfd_link_info *info,
6541 Elf_Internal_Rela *rel,
6542 struct elf_link_hash_entry *h,
6543 Elf_Internal_Sym *sym)
6544 {
6545 asection *rsec;
6546
6547 /* Syms return NULL if we're marking .opd, so we avoid marking all
6548 function sections, as all functions are referenced in .opd. */
6549 rsec = NULL;
6550 if (get_opd_info (sec) != NULL)
6551 return rsec;
6552
6553 if (h != NULL)
6554 {
6555 enum elf_ppc64_reloc_type r_type;
6556 struct ppc_link_hash_entry *eh, *fh, *fdh;
6557
6558 r_type = ELF64_R_TYPE (rel->r_info);
6559 switch (r_type)
6560 {
6561 case R_PPC64_GNU_VTINHERIT:
6562 case R_PPC64_GNU_VTENTRY:
6563 break;
6564
6565 default:
6566 switch (h->root.type)
6567 {
6568 case bfd_link_hash_defined:
6569 case bfd_link_hash_defweak:
6570 eh = (struct ppc_link_hash_entry *) h;
6571 fdh = defined_func_desc (eh);
6572 if (fdh != NULL)
6573 {
6574 /* -mcall-aixdesc code references the dot-symbol on
6575 a call reloc. Mark the function descriptor too
6576 against garbage collection. */
6577 fdh->elf.mark = 1;
6578 if (fdh->elf.u.weakdef != NULL)
6579 fdh->elf.u.weakdef->mark = 1;
6580 eh = fdh;
6581 }
6582
6583 /* Function descriptor syms cause the associated
6584 function code sym section to be marked. */
6585 fh = defined_code_entry (eh);
6586 if (fh != NULL)
6587 {
6588 /* They also mark their opd section. */
6589 eh->elf.root.u.def.section->gc_mark = 1;
6590
6591 rsec = fh->elf.root.u.def.section;
6592 }
6593 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6594 && opd_entry_value (eh->elf.root.u.def.section,
6595 eh->elf.root.u.def.value,
6596 &rsec, NULL, FALSE) != (bfd_vma) -1)
6597 eh->elf.root.u.def.section->gc_mark = 1;
6598 else
6599 rsec = h->root.u.def.section;
6600 break;
6601
6602 case bfd_link_hash_common:
6603 rsec = h->root.u.c.p->section;
6604 break;
6605
6606 default:
6607 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6608 }
6609 }
6610 }
6611 else
6612 {
6613 struct _opd_sec_data *opd;
6614
6615 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6616 opd = get_opd_info (rsec);
6617 if (opd != NULL && opd->func_sec != NULL)
6618 {
6619 rsec->gc_mark = 1;
6620
6621 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6622 }
6623 }
6624
6625 return rsec;
6626 }
6627
6628 /* The maximum size of .sfpr. */
6629 #define SFPR_MAX (218*4)
6630
6631 struct sfpr_def_parms
6632 {
6633 const char name[12];
6634 unsigned char lo, hi;
6635 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6636 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6637 };
6638
6639 /* Auto-generate _save*, _rest* functions in .sfpr.
6640 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6641 instead. */
6642
6643 static bfd_boolean
6644 sfpr_define (struct bfd_link_info *info,
6645 const struct sfpr_def_parms *parm,
6646 asection *stub_sec)
6647 {
6648 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6649 unsigned int i;
6650 size_t len = strlen (parm->name);
6651 bfd_boolean writing = FALSE;
6652 char sym[16];
6653
6654 if (htab == NULL)
6655 return FALSE;
6656
6657 memcpy (sym, parm->name, len);
6658 sym[len + 2] = 0;
6659
6660 for (i = parm->lo; i <= parm->hi; i++)
6661 {
6662 struct ppc_link_hash_entry *h;
6663
6664 sym[len + 0] = i / 10 + '0';
6665 sym[len + 1] = i % 10 + '0';
6666 h = (struct ppc_link_hash_entry *)
6667 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6668 if (stub_sec != NULL)
6669 {
6670 if (h != NULL
6671 && h->elf.root.type == bfd_link_hash_defined
6672 && h->elf.root.u.def.section == htab->sfpr)
6673 {
6674 struct elf_link_hash_entry *s;
6675 char buf[32];
6676 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6677 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6678 if (s == NULL)
6679 return FALSE;
6680 if (s->root.type == bfd_link_hash_new
6681 || (s->root.type = bfd_link_hash_defined
6682 && s->root.u.def.section == stub_sec))
6683 {
6684 s->root.type = bfd_link_hash_defined;
6685 s->root.u.def.section = stub_sec;
6686 s->root.u.def.value = (stub_sec->size
6687 + h->elf.root.u.def.value);
6688 s->ref_regular = 1;
6689 s->def_regular = 1;
6690 s->ref_regular_nonweak = 1;
6691 s->forced_local = 1;
6692 s->non_elf = 0;
6693 s->root.linker_def = 1;
6694 }
6695 }
6696 continue;
6697 }
6698 if (h != NULL)
6699 {
6700 h->save_res = 1;
6701 if (!h->elf.def_regular)
6702 {
6703 h->elf.root.type = bfd_link_hash_defined;
6704 h->elf.root.u.def.section = htab->sfpr;
6705 h->elf.root.u.def.value = htab->sfpr->size;
6706 h->elf.type = STT_FUNC;
6707 h->elf.def_regular = 1;
6708 h->elf.non_elf = 0;
6709 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6710 writing = TRUE;
6711 if (htab->sfpr->contents == NULL)
6712 {
6713 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6714 if (htab->sfpr->contents == NULL)
6715 return FALSE;
6716 }
6717 }
6718 }
6719 if (writing)
6720 {
6721 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6722 if (i != parm->hi)
6723 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6724 else
6725 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6726 htab->sfpr->size = p - htab->sfpr->contents;
6727 }
6728 }
6729
6730 return TRUE;
6731 }
6732
6733 static bfd_byte *
6734 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6735 {
6736 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6737 return p + 4;
6738 }
6739
6740 static bfd_byte *
6741 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6742 {
6743 p = savegpr0 (abfd, p, r);
6744 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6745 p = p + 4;
6746 bfd_put_32 (abfd, BLR, p);
6747 return p + 4;
6748 }
6749
6750 static bfd_byte *
6751 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6752 {
6753 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6754 return p + 4;
6755 }
6756
6757 static bfd_byte *
6758 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6759 {
6760 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6761 p = p + 4;
6762 p = restgpr0 (abfd, p, r);
6763 bfd_put_32 (abfd, MTLR_R0, p);
6764 p = p + 4;
6765 if (r == 29)
6766 {
6767 p = restgpr0 (abfd, p, 30);
6768 p = restgpr0 (abfd, p, 31);
6769 }
6770 bfd_put_32 (abfd, BLR, p);
6771 return p + 4;
6772 }
6773
6774 static bfd_byte *
6775 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6776 {
6777 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6778 return p + 4;
6779 }
6780
6781 static bfd_byte *
6782 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6783 {
6784 p = savegpr1 (abfd, p, r);
6785 bfd_put_32 (abfd, BLR, p);
6786 return p + 4;
6787 }
6788
6789 static bfd_byte *
6790 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6791 {
6792 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6793 return p + 4;
6794 }
6795
6796 static bfd_byte *
6797 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6798 {
6799 p = restgpr1 (abfd, p, r);
6800 bfd_put_32 (abfd, BLR, p);
6801 return p + 4;
6802 }
6803
6804 static bfd_byte *
6805 savefpr (bfd *abfd, bfd_byte *p, int r)
6806 {
6807 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6808 return p + 4;
6809 }
6810
6811 static bfd_byte *
6812 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6813 {
6814 p = savefpr (abfd, p, r);
6815 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6816 p = p + 4;
6817 bfd_put_32 (abfd, BLR, p);
6818 return p + 4;
6819 }
6820
6821 static bfd_byte *
6822 restfpr (bfd *abfd, bfd_byte *p, int r)
6823 {
6824 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6825 return p + 4;
6826 }
6827
6828 static bfd_byte *
6829 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6830 {
6831 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6832 p = p + 4;
6833 p = restfpr (abfd, p, r);
6834 bfd_put_32 (abfd, MTLR_R0, p);
6835 p = p + 4;
6836 if (r == 29)
6837 {
6838 p = restfpr (abfd, p, 30);
6839 p = restfpr (abfd, p, 31);
6840 }
6841 bfd_put_32 (abfd, BLR, p);
6842 return p + 4;
6843 }
6844
6845 static bfd_byte *
6846 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6847 {
6848 p = savefpr (abfd, p, r);
6849 bfd_put_32 (abfd, BLR, p);
6850 return p + 4;
6851 }
6852
6853 static bfd_byte *
6854 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6855 {
6856 p = restfpr (abfd, p, r);
6857 bfd_put_32 (abfd, BLR, p);
6858 return p + 4;
6859 }
6860
6861 static bfd_byte *
6862 savevr (bfd *abfd, bfd_byte *p, int r)
6863 {
6864 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6865 p = p + 4;
6866 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6867 return p + 4;
6868 }
6869
6870 static bfd_byte *
6871 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6872 {
6873 p = savevr (abfd, p, r);
6874 bfd_put_32 (abfd, BLR, p);
6875 return p + 4;
6876 }
6877
6878 static bfd_byte *
6879 restvr (bfd *abfd, bfd_byte *p, int r)
6880 {
6881 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6882 p = p + 4;
6883 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6884 return p + 4;
6885 }
6886
6887 static bfd_byte *
6888 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6889 {
6890 p = restvr (abfd, p, r);
6891 bfd_put_32 (abfd, BLR, p);
6892 return p + 4;
6893 }
6894
6895 /* Called via elf_link_hash_traverse to transfer dynamic linking
6896 information on function code symbol entries to their corresponding
6897 function descriptor symbol entries. */
6898
6899 static bfd_boolean
6900 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6901 {
6902 struct bfd_link_info *info;
6903 struct ppc_link_hash_table *htab;
6904 struct ppc_link_hash_entry *fh;
6905 struct ppc_link_hash_entry *fdh;
6906 bfd_boolean force_local;
6907
6908 fh = (struct ppc_link_hash_entry *) h;
6909 if (fh->elf.root.type == bfd_link_hash_indirect)
6910 return TRUE;
6911
6912 if (!fh->is_func)
6913 return TRUE;
6914
6915 if (fh->elf.root.root.string[0] != '.'
6916 || fh->elf.root.root.string[1] == '\0')
6917 return TRUE;
6918
6919 info = inf;
6920 htab = ppc_hash_table (info);
6921 if (htab == NULL)
6922 return FALSE;
6923
6924 /* Find the corresponding function descriptor symbol. */
6925 fdh = lookup_fdh (fh, htab);
6926
6927 /* Resolve undefined references to dot-symbols as the value
6928 in the function descriptor, if we have one in a regular object.
6929 This is to satisfy cases like ".quad .foo". Calls to functions
6930 in dynamic objects are handled elsewhere. */
6931 if ((fh->elf.root.type == bfd_link_hash_undefined
6932 || fh->elf.root.type == bfd_link_hash_undefweak)
6933 && (fdh->elf.root.type == bfd_link_hash_defined
6934 || fdh->elf.root.type == bfd_link_hash_defweak)
6935 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6936 && opd_entry_value (fdh->elf.root.u.def.section,
6937 fdh->elf.root.u.def.value,
6938 &fh->elf.root.u.def.section,
6939 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6940 {
6941 fh->elf.root.type = fdh->elf.root.type;
6942 fh->elf.forced_local = 1;
6943 fh->elf.def_regular = fdh->elf.def_regular;
6944 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6945 }
6946
6947 if (!fh->elf.dynamic)
6948 {
6949 struct plt_entry *ent;
6950
6951 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6952 if (ent->plt.refcount > 0)
6953 break;
6954 if (ent == NULL)
6955 return TRUE;
6956 }
6957
6958 /* Create a descriptor as undefined if necessary. */
6959 if (fdh == NULL
6960 && !bfd_link_executable (info)
6961 && (fh->elf.root.type == bfd_link_hash_undefined
6962 || fh->elf.root.type == bfd_link_hash_undefweak))
6963 {
6964 fdh = make_fdh (info, fh);
6965 if (fdh == NULL)
6966 return FALSE;
6967 }
6968
6969 /* We can't support overriding of symbols on a fake descriptor. */
6970 if (fdh != NULL
6971 && fdh->fake
6972 && (fh->elf.root.type == bfd_link_hash_defined
6973 || fh->elf.root.type == bfd_link_hash_defweak))
6974 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6975
6976 /* Transfer dynamic linking information to the function descriptor. */
6977 if (fdh != NULL)
6978 {
6979 fdh->elf.ref_regular |= fh->elf.ref_regular;
6980 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6981 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6982 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6983 fdh->elf.dynamic |= fh->elf.dynamic;
6984 fdh->elf.needs_plt |= (fh->elf.needs_plt
6985 || fh->elf.type == STT_FUNC
6986 || fh->elf.type == STT_GNU_IFUNC);
6987 move_plt_plist (fh, fdh);
6988
6989 if (!fdh->elf.forced_local
6990 && fh->elf.dynindx != -1)
6991 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6992 return FALSE;
6993 }
6994
6995 /* Now that the info is on the function descriptor, clear the
6996 function code sym info. Any function code syms for which we
6997 don't have a definition in a regular file, we force local.
6998 This prevents a shared library from exporting syms that have
6999 been imported from another library. Function code syms that
7000 are really in the library we must leave global to prevent the
7001 linker dragging in a definition from a static library. */
7002 force_local = (!fh->elf.def_regular
7003 || fdh == NULL
7004 || !fdh->elf.def_regular
7005 || fdh->elf.forced_local);
7006 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7007
7008 return TRUE;
7009 }
7010
7011 static const struct sfpr_def_parms save_res_funcs[] =
7012 {
7013 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7014 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7015 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7016 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7017 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7018 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7019 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7020 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7021 { "._savef", 14, 31, savefpr, savefpr1_tail },
7022 { "._restf", 14, 31, restfpr, restfpr1_tail },
7023 { "_savevr_", 20, 31, savevr, savevr_tail },
7024 { "_restvr_", 20, 31, restvr, restvr_tail }
7025 };
7026
7027 /* Called near the start of bfd_elf_size_dynamic_sections. We use
7028 this hook to a) provide some gcc support functions, and b) transfer
7029 dynamic linking information gathered so far on function code symbol
7030 entries, to their corresponding function descriptor symbol entries. */
7031
7032 static bfd_boolean
7033 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7034 struct bfd_link_info *info)
7035 {
7036 struct ppc_link_hash_table *htab;
7037
7038 htab = ppc_hash_table (info);
7039 if (htab == NULL)
7040 return FALSE;
7041
7042 /* Provide any missing _save* and _rest* functions. */
7043 if (htab->sfpr != NULL)
7044 {
7045 unsigned int i;
7046
7047 htab->sfpr->size = 0;
7048 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7049 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7050 return FALSE;
7051 if (htab->sfpr->size == 0)
7052 htab->sfpr->flags |= SEC_EXCLUDE;
7053 }
7054
7055 if (bfd_link_relocatable (info))
7056 return TRUE;
7057
7058 if (htab->elf.hgot != NULL)
7059 {
7060 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7061 /* Make .TOC. defined so as to prevent it being made dynamic.
7062 The wrong value here is fixed later in ppc64_elf_set_toc. */
7063 if (!htab->elf.hgot->def_regular
7064 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7065 {
7066 htab->elf.hgot->root.type = bfd_link_hash_defined;
7067 htab->elf.hgot->root.u.def.value = 0;
7068 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7069 htab->elf.hgot->def_regular = 1;
7070 htab->elf.hgot->root.linker_def = 1;
7071 }
7072 htab->elf.hgot->type = STT_OBJECT;
7073 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7074 | STV_HIDDEN);
7075 }
7076
7077 if (htab->need_func_desc_adj)
7078 {
7079 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7080 htab->need_func_desc_adj = 0;
7081 }
7082
7083 return TRUE;
7084 }
7085
7086 /* Return true if we have dynamic relocs against H that apply to
7087 read-only sections. */
7088
7089 static bfd_boolean
7090 readonly_dynrelocs (struct elf_link_hash_entry *h)
7091 {
7092 struct ppc_link_hash_entry *eh;
7093 struct elf_dyn_relocs *p;
7094
7095 eh = (struct ppc_link_hash_entry *) h;
7096 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7097 {
7098 asection *s = p->sec->output_section;
7099
7100 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7101 return TRUE;
7102 }
7103 return FALSE;
7104 }
7105
7106 /* Return true if we have dynamic relocs against H or any of its weak
7107 aliases, that apply to read-only sections. */
7108
7109 static bfd_boolean
7110 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7111 {
7112 struct ppc_link_hash_entry *eh;
7113
7114 eh = (struct ppc_link_hash_entry *) h;
7115 do
7116 {
7117 if (readonly_dynrelocs (&eh->elf))
7118 return TRUE;
7119 eh = eh->weakref;
7120 } while (eh != NULL && &eh->elf != h);
7121
7122 return FALSE;
7123 }
7124
7125 /* Return whether EH has pc-relative dynamic relocs. */
7126
7127 static bfd_boolean
7128 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7129 {
7130 struct elf_dyn_relocs *p;
7131
7132 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7133 if (p->pc_count != 0)
7134 return TRUE;
7135 return FALSE;
7136 }
7137
7138 /* Return true if a global entry stub will be created for H. Valid
7139 for ELFv2 before plt entries have been allocated. */
7140
7141 static bfd_boolean
7142 global_entry_stub (struct elf_link_hash_entry *h)
7143 {
7144 struct plt_entry *pent;
7145
7146 if (!h->pointer_equality_needed
7147 || h->def_regular)
7148 return FALSE;
7149
7150 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7151 if (pent->plt.refcount > 0
7152 && pent->addend == 0)
7153 return TRUE;
7154
7155 return FALSE;
7156 }
7157
7158 /* Adjust a symbol defined by a dynamic object and referenced by a
7159 regular object. The current definition is in some section of the
7160 dynamic object, but we're not including those sections. We have to
7161 change the definition to something the rest of the link can
7162 understand. */
7163
7164 static bfd_boolean
7165 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7166 struct elf_link_hash_entry *h)
7167 {
7168 struct ppc_link_hash_table *htab;
7169 asection *s, *srel;
7170
7171 htab = ppc_hash_table (info);
7172 if (htab == NULL)
7173 return FALSE;
7174
7175 /* Deal with function syms. */
7176 if (h->type == STT_FUNC
7177 || h->type == STT_GNU_IFUNC
7178 || h->needs_plt)
7179 {
7180 /* Clear procedure linkage table information for any symbol that
7181 won't need a .plt entry. */
7182 struct plt_entry *ent;
7183 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7184 if (ent->plt.refcount > 0)
7185 break;
7186 if (ent == NULL
7187 || (h->type != STT_GNU_IFUNC
7188 && (SYMBOL_CALLS_LOCAL (info, h)
7189 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
7190 || ((struct ppc_link_hash_entry *) h)->save_res)
7191 {
7192 h->plt.plist = NULL;
7193 h->needs_plt = 0;
7194 h->pointer_equality_needed = 0;
7195 }
7196 else if (abiversion (info->output_bfd) >= 2)
7197 {
7198 /* Taking a function's address in a read/write section
7199 doesn't require us to define the function symbol in the
7200 executable on a global entry stub. A dynamic reloc can
7201 be used instead. The reason we prefer a few more dynamic
7202 relocs is that calling via a global entry stub costs a
7203 few more instructions, and pointer_equality_needed causes
7204 extra work in ld.so when resolving these symbols. */
7205 if (global_entry_stub (h)
7206 && !alias_readonly_dynrelocs (h))
7207 {
7208 h->pointer_equality_needed = 0;
7209 /* After adjust_dynamic_symbol, non_got_ref set in
7210 the non-pic case means that dyn_relocs for this
7211 symbol should be discarded. */
7212 h->non_got_ref = 0;
7213 }
7214
7215 /* If making a plt entry, then we don't need copy relocs. */
7216 return TRUE;
7217 }
7218 }
7219 else
7220 h->plt.plist = NULL;
7221
7222 /* If this is a weak symbol, and there is a real definition, the
7223 processor independent code will have arranged for us to see the
7224 real definition first, and we can just use the same value. */
7225 if (h->u.weakdef != NULL)
7226 {
7227 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7228 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7229 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7230 h->root.u.def.value = h->u.weakdef->root.u.def.value;
7231 if (ELIMINATE_COPY_RELOCS)
7232 h->non_got_ref = h->u.weakdef->non_got_ref;
7233 return TRUE;
7234 }
7235
7236 /* If we are creating a shared library, we must presume that the
7237 only references to the symbol are via the global offset table.
7238 For such cases we need not do anything here; the relocations will
7239 be handled correctly by relocate_section. */
7240 if (bfd_link_pic (info))
7241 return TRUE;
7242
7243 /* If there are no references to this symbol that do not use the
7244 GOT, we don't need to generate a copy reloc. */
7245 if (!h->non_got_ref)
7246 return TRUE;
7247
7248 /* Don't generate a copy reloc for symbols defined in the executable. */
7249 if (!h->def_dynamic || !h->ref_regular || h->def_regular
7250
7251 /* If -z nocopyreloc was given, don't generate them either. */
7252 || info->nocopyreloc
7253
7254 /* If we didn't find any dynamic relocs in read-only sections, then
7255 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7256 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7257
7258 /* Protected variables do not work with .dynbss. The copy in
7259 .dynbss won't be used by the shared library with the protected
7260 definition for the variable. Text relocations are preferable
7261 to an incorrect program. */
7262 || h->protected_def)
7263 {
7264 h->non_got_ref = 0;
7265 return TRUE;
7266 }
7267
7268 if (h->plt.plist != NULL)
7269 {
7270 /* We should never get here, but unfortunately there are versions
7271 of gcc out there that improperly (for this ABI) put initialized
7272 function pointers, vtable refs and suchlike in read-only
7273 sections. Allow them to proceed, but warn that this might
7274 break at runtime. */
7275 info->callbacks->einfo
7276 (_("%P: copy reloc against `%T' requires lazy plt linking; "
7277 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7278 h->root.root.string);
7279 }
7280
7281 /* This is a reference to a symbol defined by a dynamic object which
7282 is not a function. */
7283
7284 /* We must allocate the symbol in our .dynbss section, which will
7285 become part of the .bss section of the executable. There will be
7286 an entry for this symbol in the .dynsym section. The dynamic
7287 object will contain position independent code, so all references
7288 from the dynamic object to this symbol will go through the global
7289 offset table. The dynamic linker will use the .dynsym entry to
7290 determine the address it must put in the global offset table, so
7291 both the dynamic object and the regular object will refer to the
7292 same memory location for the variable. */
7293
7294 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7295 to copy the initial value out of the dynamic object and into the
7296 runtime process image. We need to remember the offset into the
7297 .rela.bss section we are going to use. */
7298 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7299 {
7300 s = htab->elf.sdynrelro;
7301 srel = htab->elf.sreldynrelro;
7302 }
7303 else
7304 {
7305 s = htab->elf.sdynbss;
7306 srel = htab->elf.srelbss;
7307 }
7308 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7309 {
7310 srel->size += sizeof (Elf64_External_Rela);
7311 h->needs_copy = 1;
7312 }
7313
7314 return _bfd_elf_adjust_dynamic_copy (info, h, s);
7315 }
7316
7317 /* If given a function descriptor symbol, hide both the function code
7318 sym and the descriptor. */
7319 static void
7320 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7321 struct elf_link_hash_entry *h,
7322 bfd_boolean force_local)
7323 {
7324 struct ppc_link_hash_entry *eh;
7325 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7326
7327 eh = (struct ppc_link_hash_entry *) h;
7328 if (eh->is_func_descriptor)
7329 {
7330 struct ppc_link_hash_entry *fh = eh->oh;
7331
7332 if (fh == NULL)
7333 {
7334 const char *p, *q;
7335 struct elf_link_hash_table *htab = elf_hash_table (info);
7336 char save;
7337
7338 /* We aren't supposed to use alloca in BFD because on
7339 systems which do not have alloca the version in libiberty
7340 calls xmalloc, which might cause the program to crash
7341 when it runs out of memory. This function doesn't have a
7342 return status, so there's no way to gracefully return an
7343 error. So cheat. We know that string[-1] can be safely
7344 accessed; It's either a string in an ELF string table,
7345 or allocated in an objalloc structure. */
7346
7347 p = eh->elf.root.root.string - 1;
7348 save = *p;
7349 *(char *) p = '.';
7350 fh = (struct ppc_link_hash_entry *)
7351 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7352 *(char *) p = save;
7353
7354 /* Unfortunately, if it so happens that the string we were
7355 looking for was allocated immediately before this string,
7356 then we overwrote the string terminator. That's the only
7357 reason the lookup should fail. */
7358 if (fh == NULL)
7359 {
7360 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7361 while (q >= eh->elf.root.root.string && *q == *p)
7362 --q, --p;
7363 if (q < eh->elf.root.root.string && *p == '.')
7364 fh = (struct ppc_link_hash_entry *)
7365 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7366 }
7367 if (fh != NULL)
7368 {
7369 eh->oh = fh;
7370 fh->oh = eh;
7371 }
7372 }
7373 if (fh != NULL)
7374 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7375 }
7376 }
7377
7378 static bfd_boolean
7379 get_sym_h (struct elf_link_hash_entry **hp,
7380 Elf_Internal_Sym **symp,
7381 asection **symsecp,
7382 unsigned char **tls_maskp,
7383 Elf_Internal_Sym **locsymsp,
7384 unsigned long r_symndx,
7385 bfd *ibfd)
7386 {
7387 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7388
7389 if (r_symndx >= symtab_hdr->sh_info)
7390 {
7391 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7392 struct elf_link_hash_entry *h;
7393
7394 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7395 h = elf_follow_link (h);
7396
7397 if (hp != NULL)
7398 *hp = h;
7399
7400 if (symp != NULL)
7401 *symp = NULL;
7402
7403 if (symsecp != NULL)
7404 {
7405 asection *symsec = NULL;
7406 if (h->root.type == bfd_link_hash_defined
7407 || h->root.type == bfd_link_hash_defweak)
7408 symsec = h->root.u.def.section;
7409 *symsecp = symsec;
7410 }
7411
7412 if (tls_maskp != NULL)
7413 {
7414 struct ppc_link_hash_entry *eh;
7415
7416 eh = (struct ppc_link_hash_entry *) h;
7417 *tls_maskp = &eh->tls_mask;
7418 }
7419 }
7420 else
7421 {
7422 Elf_Internal_Sym *sym;
7423 Elf_Internal_Sym *locsyms = *locsymsp;
7424
7425 if (locsyms == NULL)
7426 {
7427 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7428 if (locsyms == NULL)
7429 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7430 symtab_hdr->sh_info,
7431 0, NULL, NULL, NULL);
7432 if (locsyms == NULL)
7433 return FALSE;
7434 *locsymsp = locsyms;
7435 }
7436 sym = locsyms + r_symndx;
7437
7438 if (hp != NULL)
7439 *hp = NULL;
7440
7441 if (symp != NULL)
7442 *symp = sym;
7443
7444 if (symsecp != NULL)
7445 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7446
7447 if (tls_maskp != NULL)
7448 {
7449 struct got_entry **lgot_ents;
7450 unsigned char *tls_mask;
7451
7452 tls_mask = NULL;
7453 lgot_ents = elf_local_got_ents (ibfd);
7454 if (lgot_ents != NULL)
7455 {
7456 struct plt_entry **local_plt = (struct plt_entry **)
7457 (lgot_ents + symtab_hdr->sh_info);
7458 unsigned char *lgot_masks = (unsigned char *)
7459 (local_plt + symtab_hdr->sh_info);
7460 tls_mask = &lgot_masks[r_symndx];
7461 }
7462 *tls_maskp = tls_mask;
7463 }
7464 }
7465 return TRUE;
7466 }
7467
7468 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7469 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7470 type suitable for optimization, and 1 otherwise. */
7471
7472 static int
7473 get_tls_mask (unsigned char **tls_maskp,
7474 unsigned long *toc_symndx,
7475 bfd_vma *toc_addend,
7476 Elf_Internal_Sym **locsymsp,
7477 const Elf_Internal_Rela *rel,
7478 bfd *ibfd)
7479 {
7480 unsigned long r_symndx;
7481 int next_r;
7482 struct elf_link_hash_entry *h;
7483 Elf_Internal_Sym *sym;
7484 asection *sec;
7485 bfd_vma off;
7486
7487 r_symndx = ELF64_R_SYM (rel->r_info);
7488 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7489 return 0;
7490
7491 if ((*tls_maskp != NULL && **tls_maskp != 0)
7492 || sec == NULL
7493 || ppc64_elf_section_data (sec) == NULL
7494 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7495 return 1;
7496
7497 /* Look inside a TOC section too. */
7498 if (h != NULL)
7499 {
7500 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7501 off = h->root.u.def.value;
7502 }
7503 else
7504 off = sym->st_value;
7505 off += rel->r_addend;
7506 BFD_ASSERT (off % 8 == 0);
7507 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7508 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7509 if (toc_symndx != NULL)
7510 *toc_symndx = r_symndx;
7511 if (toc_addend != NULL)
7512 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7513 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7514 return 0;
7515 if ((h == NULL || is_static_defined (h))
7516 && (next_r == -1 || next_r == -2))
7517 return 1 - next_r;
7518 return 1;
7519 }
7520
7521 /* Find (or create) an entry in the tocsave hash table. */
7522
7523 static struct tocsave_entry *
7524 tocsave_find (struct ppc_link_hash_table *htab,
7525 enum insert_option insert,
7526 Elf_Internal_Sym **local_syms,
7527 const Elf_Internal_Rela *irela,
7528 bfd *ibfd)
7529 {
7530 unsigned long r_indx;
7531 struct elf_link_hash_entry *h;
7532 Elf_Internal_Sym *sym;
7533 struct tocsave_entry ent, *p;
7534 hashval_t hash;
7535 struct tocsave_entry **slot;
7536
7537 r_indx = ELF64_R_SYM (irela->r_info);
7538 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7539 return NULL;
7540 if (ent.sec == NULL || ent.sec->output_section == NULL)
7541 {
7542 _bfd_error_handler
7543 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7544 return NULL;
7545 }
7546
7547 if (h != NULL)
7548 ent.offset = h->root.u.def.value;
7549 else
7550 ent.offset = sym->st_value;
7551 ent.offset += irela->r_addend;
7552
7553 hash = tocsave_htab_hash (&ent);
7554 slot = ((struct tocsave_entry **)
7555 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7556 if (slot == NULL)
7557 return NULL;
7558
7559 if (*slot == NULL)
7560 {
7561 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7562 if (p == NULL)
7563 return NULL;
7564 *p = ent;
7565 *slot = p;
7566 }
7567 return *slot;
7568 }
7569
7570 /* Adjust all global syms defined in opd sections. In gcc generated
7571 code for the old ABI, these will already have been done. */
7572
7573 static bfd_boolean
7574 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7575 {
7576 struct ppc_link_hash_entry *eh;
7577 asection *sym_sec;
7578 struct _opd_sec_data *opd;
7579
7580 if (h->root.type == bfd_link_hash_indirect)
7581 return TRUE;
7582
7583 if (h->root.type != bfd_link_hash_defined
7584 && h->root.type != bfd_link_hash_defweak)
7585 return TRUE;
7586
7587 eh = (struct ppc_link_hash_entry *) h;
7588 if (eh->adjust_done)
7589 return TRUE;
7590
7591 sym_sec = eh->elf.root.u.def.section;
7592 opd = get_opd_info (sym_sec);
7593 if (opd != NULL && opd->adjust != NULL)
7594 {
7595 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7596 if (adjust == -1)
7597 {
7598 /* This entry has been deleted. */
7599 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7600 if (dsec == NULL)
7601 {
7602 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7603 if (discarded_section (dsec))
7604 {
7605 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7606 break;
7607 }
7608 }
7609 eh->elf.root.u.def.value = 0;
7610 eh->elf.root.u.def.section = dsec;
7611 }
7612 else
7613 eh->elf.root.u.def.value += adjust;
7614 eh->adjust_done = 1;
7615 }
7616 return TRUE;
7617 }
7618
7619 /* Handles decrementing dynamic reloc counts for the reloc specified by
7620 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7621 have already been determined. */
7622
7623 static bfd_boolean
7624 dec_dynrel_count (bfd_vma r_info,
7625 asection *sec,
7626 struct bfd_link_info *info,
7627 Elf_Internal_Sym **local_syms,
7628 struct elf_link_hash_entry *h,
7629 Elf_Internal_Sym *sym)
7630 {
7631 enum elf_ppc64_reloc_type r_type;
7632 asection *sym_sec = NULL;
7633
7634 /* Can this reloc be dynamic? This switch, and later tests here
7635 should be kept in sync with the code in check_relocs. */
7636 r_type = ELF64_R_TYPE (r_info);
7637 switch (r_type)
7638 {
7639 default:
7640 return TRUE;
7641
7642 case R_PPC64_TPREL16:
7643 case R_PPC64_TPREL16_LO:
7644 case R_PPC64_TPREL16_HI:
7645 case R_PPC64_TPREL16_HA:
7646 case R_PPC64_TPREL16_DS:
7647 case R_PPC64_TPREL16_LO_DS:
7648 case R_PPC64_TPREL16_HIGH:
7649 case R_PPC64_TPREL16_HIGHA:
7650 case R_PPC64_TPREL16_HIGHER:
7651 case R_PPC64_TPREL16_HIGHERA:
7652 case R_PPC64_TPREL16_HIGHEST:
7653 case R_PPC64_TPREL16_HIGHESTA:
7654 case R_PPC64_TPREL64:
7655 case R_PPC64_DTPMOD64:
7656 case R_PPC64_DTPREL64:
7657 case R_PPC64_ADDR64:
7658 case R_PPC64_REL30:
7659 case R_PPC64_REL32:
7660 case R_PPC64_REL64:
7661 case R_PPC64_ADDR14:
7662 case R_PPC64_ADDR14_BRNTAKEN:
7663 case R_PPC64_ADDR14_BRTAKEN:
7664 case R_PPC64_ADDR16:
7665 case R_PPC64_ADDR16_DS:
7666 case R_PPC64_ADDR16_HA:
7667 case R_PPC64_ADDR16_HI:
7668 case R_PPC64_ADDR16_HIGH:
7669 case R_PPC64_ADDR16_HIGHA:
7670 case R_PPC64_ADDR16_HIGHER:
7671 case R_PPC64_ADDR16_HIGHERA:
7672 case R_PPC64_ADDR16_HIGHEST:
7673 case R_PPC64_ADDR16_HIGHESTA:
7674 case R_PPC64_ADDR16_LO:
7675 case R_PPC64_ADDR16_LO_DS:
7676 case R_PPC64_ADDR24:
7677 case R_PPC64_ADDR32:
7678 case R_PPC64_UADDR16:
7679 case R_PPC64_UADDR32:
7680 case R_PPC64_UADDR64:
7681 case R_PPC64_TOC:
7682 break;
7683 }
7684
7685 if (local_syms != NULL)
7686 {
7687 unsigned long r_symndx;
7688 bfd *ibfd = sec->owner;
7689
7690 r_symndx = ELF64_R_SYM (r_info);
7691 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7692 return FALSE;
7693 }
7694
7695 if ((bfd_link_pic (info)
7696 && (must_be_dyn_reloc (info, r_type)
7697 || (h != NULL
7698 && (!SYMBOLIC_BIND (info, h)
7699 || h->root.type == bfd_link_hash_defweak
7700 || !h->def_regular))))
7701 || (ELIMINATE_COPY_RELOCS
7702 && !bfd_link_pic (info)
7703 && h != NULL
7704 && (h->root.type == bfd_link_hash_defweak
7705 || !h->def_regular)))
7706 ;
7707 else
7708 return TRUE;
7709
7710 if (h != NULL)
7711 {
7712 struct elf_dyn_relocs *p;
7713 struct elf_dyn_relocs **pp;
7714 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7715
7716 /* elf_gc_sweep may have already removed all dyn relocs associated
7717 with local syms for a given section. Also, symbol flags are
7718 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7719 report a dynreloc miscount. */
7720 if (*pp == NULL && info->gc_sections)
7721 return TRUE;
7722
7723 while ((p = *pp) != NULL)
7724 {
7725 if (p->sec == sec)
7726 {
7727 if (!must_be_dyn_reloc (info, r_type))
7728 p->pc_count -= 1;
7729 p->count -= 1;
7730 if (p->count == 0)
7731 *pp = p->next;
7732 return TRUE;
7733 }
7734 pp = &p->next;
7735 }
7736 }
7737 else
7738 {
7739 struct ppc_dyn_relocs *p;
7740 struct ppc_dyn_relocs **pp;
7741 void *vpp;
7742 bfd_boolean is_ifunc;
7743
7744 if (local_syms == NULL)
7745 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7746 if (sym_sec == NULL)
7747 sym_sec = sec;
7748
7749 vpp = &elf_section_data (sym_sec)->local_dynrel;
7750 pp = (struct ppc_dyn_relocs **) vpp;
7751
7752 if (*pp == NULL && info->gc_sections)
7753 return TRUE;
7754
7755 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7756 while ((p = *pp) != NULL)
7757 {
7758 if (p->sec == sec && p->ifunc == is_ifunc)
7759 {
7760 p->count -= 1;
7761 if (p->count == 0)
7762 *pp = p->next;
7763 return TRUE;
7764 }
7765 pp = &p->next;
7766 }
7767 }
7768
7769 /* xgettext:c-format */
7770 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7771 sec->owner, sec);
7772 bfd_set_error (bfd_error_bad_value);
7773 return FALSE;
7774 }
7775
7776 /* Remove unused Official Procedure Descriptor entries. Currently we
7777 only remove those associated with functions in discarded link-once
7778 sections, or weakly defined functions that have been overridden. It
7779 would be possible to remove many more entries for statically linked
7780 applications. */
7781
7782 bfd_boolean
7783 ppc64_elf_edit_opd (struct bfd_link_info *info)
7784 {
7785 bfd *ibfd;
7786 bfd_boolean some_edited = FALSE;
7787 asection *need_pad = NULL;
7788 struct ppc_link_hash_table *htab;
7789
7790 htab = ppc_hash_table (info);
7791 if (htab == NULL)
7792 return FALSE;
7793
7794 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7795 {
7796 asection *sec;
7797 Elf_Internal_Rela *relstart, *rel, *relend;
7798 Elf_Internal_Shdr *symtab_hdr;
7799 Elf_Internal_Sym *local_syms;
7800 struct _opd_sec_data *opd;
7801 bfd_boolean need_edit, add_aux_fields, broken;
7802 bfd_size_type cnt_16b = 0;
7803
7804 if (!is_ppc64_elf (ibfd))
7805 continue;
7806
7807 sec = bfd_get_section_by_name (ibfd, ".opd");
7808 if (sec == NULL || sec->size == 0)
7809 continue;
7810
7811 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7812 continue;
7813
7814 if (sec->output_section == bfd_abs_section_ptr)
7815 continue;
7816
7817 /* Look through the section relocs. */
7818 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7819 continue;
7820
7821 local_syms = NULL;
7822 symtab_hdr = &elf_symtab_hdr (ibfd);
7823
7824 /* Read the relocations. */
7825 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7826 info->keep_memory);
7827 if (relstart == NULL)
7828 return FALSE;
7829
7830 /* First run through the relocs to check they are sane, and to
7831 determine whether we need to edit this opd section. */
7832 need_edit = FALSE;
7833 broken = FALSE;
7834 need_pad = sec;
7835 relend = relstart + sec->reloc_count;
7836 for (rel = relstart; rel < relend; )
7837 {
7838 enum elf_ppc64_reloc_type r_type;
7839 unsigned long r_symndx;
7840 asection *sym_sec;
7841 struct elf_link_hash_entry *h;
7842 Elf_Internal_Sym *sym;
7843 bfd_vma offset;
7844
7845 /* .opd contains an array of 16 or 24 byte entries. We're
7846 only interested in the reloc pointing to a function entry
7847 point. */
7848 offset = rel->r_offset;
7849 if (rel + 1 == relend
7850 || rel[1].r_offset != offset + 8)
7851 {
7852 /* If someone messes with .opd alignment then after a
7853 "ld -r" we might have padding in the middle of .opd.
7854 Also, there's nothing to prevent someone putting
7855 something silly in .opd with the assembler. No .opd
7856 optimization for them! */
7857 broken_opd:
7858 _bfd_error_handler
7859 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7860 broken = TRUE;
7861 break;
7862 }
7863
7864 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7865 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7866 {
7867 _bfd_error_handler
7868 /* xgettext:c-format */
7869 (_("%B: unexpected reloc type %u in .opd section"),
7870 ibfd, r_type);
7871 broken = TRUE;
7872 break;
7873 }
7874
7875 r_symndx = ELF64_R_SYM (rel->r_info);
7876 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7877 r_symndx, ibfd))
7878 goto error_ret;
7879
7880 if (sym_sec == NULL || sym_sec->owner == NULL)
7881 {
7882 const char *sym_name;
7883 if (h != NULL)
7884 sym_name = h->root.root.string;
7885 else
7886 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7887 sym_sec);
7888
7889 _bfd_error_handler
7890 /* xgettext:c-format */
7891 (_("%B: undefined sym `%s' in .opd section"),
7892 ibfd, sym_name);
7893 broken = TRUE;
7894 break;
7895 }
7896
7897 /* opd entries are always for functions defined in the
7898 current input bfd. If the symbol isn't defined in the
7899 input bfd, then we won't be using the function in this
7900 bfd; It must be defined in a linkonce section in another
7901 bfd, or is weak. It's also possible that we are
7902 discarding the function due to a linker script /DISCARD/,
7903 which we test for via the output_section. */
7904 if (sym_sec->owner != ibfd
7905 || sym_sec->output_section == bfd_abs_section_ptr)
7906 need_edit = TRUE;
7907
7908 rel += 2;
7909 if (rel + 1 == relend
7910 || (rel + 2 < relend
7911 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7912 ++rel;
7913
7914 if (rel == relend)
7915 {
7916 if (sec->size == offset + 24)
7917 {
7918 need_pad = NULL;
7919 break;
7920 }
7921 if (sec->size == offset + 16)
7922 {
7923 cnt_16b++;
7924 break;
7925 }
7926 goto broken_opd;
7927 }
7928 else if (rel + 1 < relend
7929 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7930 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7931 {
7932 if (rel[0].r_offset == offset + 16)
7933 cnt_16b++;
7934 else if (rel[0].r_offset != offset + 24)
7935 goto broken_opd;
7936 }
7937 else
7938 goto broken_opd;
7939 }
7940
7941 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7942
7943 if (!broken && (need_edit || add_aux_fields))
7944 {
7945 Elf_Internal_Rela *write_rel;
7946 Elf_Internal_Shdr *rel_hdr;
7947 bfd_byte *rptr, *wptr;
7948 bfd_byte *new_contents;
7949 bfd_size_type amt;
7950
7951 new_contents = NULL;
7952 amt = OPD_NDX (sec->size) * sizeof (long);
7953 opd = &ppc64_elf_section_data (sec)->u.opd;
7954 opd->adjust = bfd_zalloc (sec->owner, amt);
7955 if (opd->adjust == NULL)
7956 return FALSE;
7957
7958 /* This seems a waste of time as input .opd sections are all
7959 zeros as generated by gcc, but I suppose there's no reason
7960 this will always be so. We might start putting something in
7961 the third word of .opd entries. */
7962 if ((sec->flags & SEC_IN_MEMORY) == 0)
7963 {
7964 bfd_byte *loc;
7965 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7966 {
7967 if (loc != NULL)
7968 free (loc);
7969 error_ret:
7970 if (local_syms != NULL
7971 && symtab_hdr->contents != (unsigned char *) local_syms)
7972 free (local_syms);
7973 if (elf_section_data (sec)->relocs != relstart)
7974 free (relstart);
7975 return FALSE;
7976 }
7977 sec->contents = loc;
7978 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7979 }
7980
7981 elf_section_data (sec)->relocs = relstart;
7982
7983 new_contents = sec->contents;
7984 if (add_aux_fields)
7985 {
7986 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7987 if (new_contents == NULL)
7988 return FALSE;
7989 need_pad = NULL;
7990 }
7991 wptr = new_contents;
7992 rptr = sec->contents;
7993 write_rel = relstart;
7994 for (rel = relstart; rel < relend; )
7995 {
7996 unsigned long r_symndx;
7997 asection *sym_sec;
7998 struct elf_link_hash_entry *h;
7999 struct ppc_link_hash_entry *fdh = NULL;
8000 Elf_Internal_Sym *sym;
8001 long opd_ent_size;
8002 Elf_Internal_Rela *next_rel;
8003 bfd_boolean skip;
8004
8005 r_symndx = ELF64_R_SYM (rel->r_info);
8006 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8007 r_symndx, ibfd))
8008 goto error_ret;
8009
8010 next_rel = rel + 2;
8011 if (next_rel + 1 == relend
8012 || (next_rel + 2 < relend
8013 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8014 ++next_rel;
8015
8016 /* See if the .opd entry is full 24 byte or
8017 16 byte (with fd_aux entry overlapped with next
8018 fd_func). */
8019 opd_ent_size = 24;
8020 if (next_rel == relend)
8021 {
8022 if (sec->size == rel->r_offset + 16)
8023 opd_ent_size = 16;
8024 }
8025 else if (next_rel->r_offset == rel->r_offset + 16)
8026 opd_ent_size = 16;
8027
8028 if (h != NULL
8029 && h->root.root.string[0] == '.')
8030 {
8031 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8032 if (fdh != NULL)
8033 {
8034 fdh = ppc_follow_link (fdh);
8035 if (fdh->elf.root.type != bfd_link_hash_defined
8036 && fdh->elf.root.type != bfd_link_hash_defweak)
8037 fdh = NULL;
8038 }
8039 }
8040
8041 skip = (sym_sec->owner != ibfd
8042 || sym_sec->output_section == bfd_abs_section_ptr);
8043 if (skip)
8044 {
8045 if (fdh != NULL && sym_sec->owner == ibfd)
8046 {
8047 /* Arrange for the function descriptor sym
8048 to be dropped. */
8049 fdh->elf.root.u.def.value = 0;
8050 fdh->elf.root.u.def.section = sym_sec;
8051 }
8052 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8053
8054 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8055 rel = next_rel;
8056 else
8057 while (1)
8058 {
8059 if (!dec_dynrel_count (rel->r_info, sec, info,
8060 NULL, h, sym))
8061 goto error_ret;
8062
8063 if (++rel == next_rel)
8064 break;
8065
8066 r_symndx = ELF64_R_SYM (rel->r_info);
8067 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8068 r_symndx, ibfd))
8069 goto error_ret;
8070 }
8071 }
8072 else
8073 {
8074 /* We'll be keeping this opd entry. */
8075 long adjust;
8076
8077 if (fdh != NULL)
8078 {
8079 /* Redefine the function descriptor symbol to
8080 this location in the opd section. It is
8081 necessary to update the value here rather
8082 than using an array of adjustments as we do
8083 for local symbols, because various places
8084 in the generic ELF code use the value
8085 stored in u.def.value. */
8086 fdh->elf.root.u.def.value = wptr - new_contents;
8087 fdh->adjust_done = 1;
8088 }
8089
8090 /* Local syms are a bit tricky. We could
8091 tweak them as they can be cached, but
8092 we'd need to look through the local syms
8093 for the function descriptor sym which we
8094 don't have at the moment. So keep an
8095 array of adjustments. */
8096 adjust = (wptr - new_contents) - (rptr - sec->contents);
8097 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8098
8099 if (wptr != rptr)
8100 memcpy (wptr, rptr, opd_ent_size);
8101 wptr += opd_ent_size;
8102 if (add_aux_fields && opd_ent_size == 16)
8103 {
8104 memset (wptr, '\0', 8);
8105 wptr += 8;
8106 }
8107
8108 /* We need to adjust any reloc offsets to point to the
8109 new opd entries. */
8110 for ( ; rel != next_rel; ++rel)
8111 {
8112 rel->r_offset += adjust;
8113 if (write_rel != rel)
8114 memcpy (write_rel, rel, sizeof (*rel));
8115 ++write_rel;
8116 }
8117 }
8118
8119 rptr += opd_ent_size;
8120 }
8121
8122 sec->size = wptr - new_contents;
8123 sec->reloc_count = write_rel - relstart;
8124 if (add_aux_fields)
8125 {
8126 free (sec->contents);
8127 sec->contents = new_contents;
8128 }
8129
8130 /* Fudge the header size too, as this is used later in
8131 elf_bfd_final_link if we are emitting relocs. */
8132 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8133 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8134 some_edited = TRUE;
8135 }
8136 else if (elf_section_data (sec)->relocs != relstart)
8137 free (relstart);
8138
8139 if (local_syms != NULL
8140 && symtab_hdr->contents != (unsigned char *) local_syms)
8141 {
8142 if (!info->keep_memory)
8143 free (local_syms);
8144 else
8145 symtab_hdr->contents = (unsigned char *) local_syms;
8146 }
8147 }
8148
8149 if (some_edited)
8150 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8151
8152 /* If we are doing a final link and the last .opd entry is just 16 byte
8153 long, add a 8 byte padding after it. */
8154 if (need_pad != NULL && !bfd_link_relocatable (info))
8155 {
8156 bfd_byte *p;
8157
8158 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8159 {
8160 BFD_ASSERT (need_pad->size > 0);
8161
8162 p = bfd_malloc (need_pad->size + 8);
8163 if (p == NULL)
8164 return FALSE;
8165
8166 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8167 p, 0, need_pad->size))
8168 return FALSE;
8169
8170 need_pad->contents = p;
8171 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8172 }
8173 else
8174 {
8175 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8176 if (p == NULL)
8177 return FALSE;
8178
8179 need_pad->contents = p;
8180 }
8181
8182 memset (need_pad->contents + need_pad->size, 0, 8);
8183 need_pad->size += 8;
8184 }
8185
8186 return TRUE;
8187 }
8188
8189 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
8190
8191 asection *
8192 ppc64_elf_tls_setup (struct bfd_link_info *info)
8193 {
8194 struct ppc_link_hash_table *htab;
8195
8196 htab = ppc_hash_table (info);
8197 if (htab == NULL)
8198 return NULL;
8199
8200 if (abiversion (info->output_bfd) == 1)
8201 htab->opd_abi = 1;
8202
8203 if (htab->params->no_multi_toc)
8204 htab->do_multi_toc = 0;
8205 else if (!htab->do_multi_toc)
8206 htab->params->no_multi_toc = 1;
8207
8208 /* Default to --no-plt-localentry, as this option can cause problems
8209 with symbol interposition. For example, glibc libpthread.so and
8210 libc.so duplicate many pthread symbols, with a fallback
8211 implementation in libc.so. In some cases the fallback does more
8212 work than the pthread implementation. __pthread_condattr_destroy
8213 is one such symbol: the libpthread.so implementation is
8214 localentry:0 while the libc.so implementation is localentry:8.
8215 An app that "cleverly" uses dlopen to only load necessary
8216 libraries at runtime may omit loading libpthread.so when not
8217 running multi-threaded, which then results in the libc.so
8218 fallback symbols being used and ld.so complaining. Now there
8219 are workarounds in ld (see non_zero_localentry) to detect the
8220 pthread situation, but that may not be the only case where
8221 --plt-localentry can cause trouble. */
8222 if (htab->params->plt_localentry0 < 0)
8223 htab->params->plt_localentry0 = 0;
8224 if (htab->params->plt_localentry0
8225 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8226 FALSE, FALSE, FALSE) == NULL)
8227 info->callbacks->einfo
8228 (_("%P: warning: --plt-localentry is especially dangerous without "
8229 "ld.so support to detect ABI violations.\n"));
8230
8231 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8232 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8233 FALSE, FALSE, TRUE));
8234 /* Move dynamic linking info to the function descriptor sym. */
8235 if (htab->tls_get_addr != NULL)
8236 func_desc_adjust (&htab->tls_get_addr->elf, info);
8237 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8238 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8239 FALSE, FALSE, TRUE));
8240 if (htab->params->tls_get_addr_opt)
8241 {
8242 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8243
8244 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8245 FALSE, FALSE, TRUE);
8246 if (opt != NULL)
8247 func_desc_adjust (opt, info);
8248 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8249 FALSE, FALSE, TRUE);
8250 if (opt_fd != NULL
8251 && (opt_fd->root.type == bfd_link_hash_defined
8252 || opt_fd->root.type == bfd_link_hash_defweak))
8253 {
8254 /* If glibc supports an optimized __tls_get_addr call stub,
8255 signalled by the presence of __tls_get_addr_opt, and we'll
8256 be calling __tls_get_addr via a plt call stub, then
8257 make __tls_get_addr point to __tls_get_addr_opt. */
8258 tga_fd = &htab->tls_get_addr_fd->elf;
8259 if (htab->elf.dynamic_sections_created
8260 && tga_fd != NULL
8261 && (tga_fd->type == STT_FUNC
8262 || tga_fd->needs_plt)
8263 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8264 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
8265 {
8266 struct plt_entry *ent;
8267
8268 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8269 if (ent->plt.refcount > 0)
8270 break;
8271 if (ent != NULL)
8272 {
8273 tga_fd->root.type = bfd_link_hash_indirect;
8274 tga_fd->root.u.i.link = &opt_fd->root;
8275 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8276 opt_fd->mark = 1;
8277 if (opt_fd->dynindx != -1)
8278 {
8279 /* Use __tls_get_addr_opt in dynamic relocations. */
8280 opt_fd->dynindx = -1;
8281 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8282 opt_fd->dynstr_index);
8283 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8284 return NULL;
8285 }
8286 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8287 tga = &htab->tls_get_addr->elf;
8288 if (opt != NULL && tga != NULL)
8289 {
8290 tga->root.type = bfd_link_hash_indirect;
8291 tga->root.u.i.link = &opt->root;
8292 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8293 opt->mark = 1;
8294 _bfd_elf_link_hash_hide_symbol (info, opt,
8295 tga->forced_local);
8296 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8297 }
8298 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8299 htab->tls_get_addr_fd->is_func_descriptor = 1;
8300 if (htab->tls_get_addr != NULL)
8301 {
8302 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8303 htab->tls_get_addr->is_func = 1;
8304 }
8305 }
8306 }
8307 }
8308 else if (htab->params->tls_get_addr_opt < 0)
8309 htab->params->tls_get_addr_opt = 0;
8310 }
8311 return _bfd_elf_tls_setup (info->output_bfd, info);
8312 }
8313
8314 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8315 HASH1 or HASH2. */
8316
8317 static bfd_boolean
8318 branch_reloc_hash_match (const bfd *ibfd,
8319 const Elf_Internal_Rela *rel,
8320 const struct ppc_link_hash_entry *hash1,
8321 const struct ppc_link_hash_entry *hash2)
8322 {
8323 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8324 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8325 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8326
8327 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8328 {
8329 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8330 struct elf_link_hash_entry *h;
8331
8332 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8333 h = elf_follow_link (h);
8334 if (h == &hash1->elf || h == &hash2->elf)
8335 return TRUE;
8336 }
8337 return FALSE;
8338 }
8339
8340 /* Run through all the TLS relocs looking for optimization
8341 opportunities. The linker has been hacked (see ppc64elf.em) to do
8342 a preliminary section layout so that we know the TLS segment
8343 offsets. We can't optimize earlier because some optimizations need
8344 to know the tp offset, and we need to optimize before allocating
8345 dynamic relocations. */
8346
8347 bfd_boolean
8348 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8349 {
8350 bfd *ibfd;
8351 asection *sec;
8352 struct ppc_link_hash_table *htab;
8353 unsigned char *toc_ref;
8354 int pass;
8355
8356 if (!bfd_link_executable (info))
8357 return TRUE;
8358
8359 htab = ppc_hash_table (info);
8360 if (htab == NULL)
8361 return FALSE;
8362
8363 /* Make two passes over the relocs. On the first pass, mark toc
8364 entries involved with tls relocs, and check that tls relocs
8365 involved in setting up a tls_get_addr call are indeed followed by
8366 such a call. If they are not, we can't do any tls optimization.
8367 On the second pass twiddle tls_mask flags to notify
8368 relocate_section that optimization can be done, and adjust got
8369 and plt refcounts. */
8370 toc_ref = NULL;
8371 for (pass = 0; pass < 2; ++pass)
8372 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8373 {
8374 Elf_Internal_Sym *locsyms = NULL;
8375 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8376
8377 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8378 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8379 {
8380 Elf_Internal_Rela *relstart, *rel, *relend;
8381 bfd_boolean found_tls_get_addr_arg = 0;
8382
8383 /* Read the relocations. */
8384 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8385 info->keep_memory);
8386 if (relstart == NULL)
8387 {
8388 free (toc_ref);
8389 return FALSE;
8390 }
8391
8392 relend = relstart + sec->reloc_count;
8393 for (rel = relstart; rel < relend; rel++)
8394 {
8395 enum elf_ppc64_reloc_type r_type;
8396 unsigned long r_symndx;
8397 struct elf_link_hash_entry *h;
8398 Elf_Internal_Sym *sym;
8399 asection *sym_sec;
8400 unsigned char *tls_mask;
8401 unsigned char tls_set, tls_clear, tls_type = 0;
8402 bfd_vma value;
8403 bfd_boolean ok_tprel, is_local;
8404 long toc_ref_index = 0;
8405 int expecting_tls_get_addr = 0;
8406 bfd_boolean ret = FALSE;
8407
8408 r_symndx = ELF64_R_SYM (rel->r_info);
8409 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8410 r_symndx, ibfd))
8411 {
8412 err_free_rel:
8413 if (elf_section_data (sec)->relocs != relstart)
8414 free (relstart);
8415 if (toc_ref != NULL)
8416 free (toc_ref);
8417 if (locsyms != NULL
8418 && (elf_symtab_hdr (ibfd).contents
8419 != (unsigned char *) locsyms))
8420 free (locsyms);
8421 return ret;
8422 }
8423
8424 if (h != NULL)
8425 {
8426 if (h->root.type == bfd_link_hash_defined
8427 || h->root.type == bfd_link_hash_defweak)
8428 value = h->root.u.def.value;
8429 else if (h->root.type == bfd_link_hash_undefweak)
8430 value = 0;
8431 else
8432 {
8433 found_tls_get_addr_arg = 0;
8434 continue;
8435 }
8436 }
8437 else
8438 /* Symbols referenced by TLS relocs must be of type
8439 STT_TLS. So no need for .opd local sym adjust. */
8440 value = sym->st_value;
8441
8442 ok_tprel = FALSE;
8443 is_local = FALSE;
8444 if (h == NULL
8445 || !h->def_dynamic)
8446 {
8447 is_local = TRUE;
8448 if (h != NULL
8449 && h->root.type == bfd_link_hash_undefweak)
8450 ok_tprel = TRUE;
8451 else if (sym_sec != NULL
8452 && sym_sec->output_section != NULL)
8453 {
8454 value += sym_sec->output_offset;
8455 value += sym_sec->output_section->vma;
8456 value -= htab->elf.tls_sec->vma;
8457 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8458 < (bfd_vma) 1 << 32);
8459 }
8460 }
8461
8462 r_type = ELF64_R_TYPE (rel->r_info);
8463 /* If this section has old-style __tls_get_addr calls
8464 without marker relocs, then check that each
8465 __tls_get_addr call reloc is preceded by a reloc
8466 that conceivably belongs to the __tls_get_addr arg
8467 setup insn. If we don't find matching arg setup
8468 relocs, don't do any tls optimization. */
8469 if (pass == 0
8470 && sec->has_tls_get_addr_call
8471 && h != NULL
8472 && (h == &htab->tls_get_addr->elf
8473 || h == &htab->tls_get_addr_fd->elf)
8474 && !found_tls_get_addr_arg
8475 && is_branch_reloc (r_type))
8476 {
8477 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8478 "TLS optimization disabled\n"),
8479 ibfd, sec, rel->r_offset);
8480 ret = TRUE;
8481 goto err_free_rel;
8482 }
8483
8484 found_tls_get_addr_arg = 0;
8485 switch (r_type)
8486 {
8487 case R_PPC64_GOT_TLSLD16:
8488 case R_PPC64_GOT_TLSLD16_LO:
8489 expecting_tls_get_addr = 1;
8490 found_tls_get_addr_arg = 1;
8491 /* Fall through. */
8492
8493 case R_PPC64_GOT_TLSLD16_HI:
8494 case R_PPC64_GOT_TLSLD16_HA:
8495 /* These relocs should never be against a symbol
8496 defined in a shared lib. Leave them alone if
8497 that turns out to be the case. */
8498 if (!is_local)
8499 continue;
8500
8501 /* LD -> LE */
8502 tls_set = 0;
8503 tls_clear = TLS_LD;
8504 tls_type = TLS_TLS | TLS_LD;
8505 break;
8506
8507 case R_PPC64_GOT_TLSGD16:
8508 case R_PPC64_GOT_TLSGD16_LO:
8509 expecting_tls_get_addr = 1;
8510 found_tls_get_addr_arg = 1;
8511 /* Fall through. */
8512
8513 case R_PPC64_GOT_TLSGD16_HI:
8514 case R_PPC64_GOT_TLSGD16_HA:
8515 if (ok_tprel)
8516 /* GD -> LE */
8517 tls_set = 0;
8518 else
8519 /* GD -> IE */
8520 tls_set = TLS_TLS | TLS_TPRELGD;
8521 tls_clear = TLS_GD;
8522 tls_type = TLS_TLS | TLS_GD;
8523 break;
8524
8525 case R_PPC64_GOT_TPREL16_DS:
8526 case R_PPC64_GOT_TPREL16_LO_DS:
8527 case R_PPC64_GOT_TPREL16_HI:
8528 case R_PPC64_GOT_TPREL16_HA:
8529 if (ok_tprel)
8530 {
8531 /* IE -> LE */
8532 tls_set = 0;
8533 tls_clear = TLS_TPREL;
8534 tls_type = TLS_TLS | TLS_TPREL;
8535 break;
8536 }
8537 continue;
8538
8539 case R_PPC64_TLSGD:
8540 case R_PPC64_TLSLD:
8541 found_tls_get_addr_arg = 1;
8542 /* Fall through. */
8543
8544 case R_PPC64_TLS:
8545 case R_PPC64_TOC16:
8546 case R_PPC64_TOC16_LO:
8547 if (sym_sec == NULL || sym_sec != toc)
8548 continue;
8549
8550 /* Mark this toc entry as referenced by a TLS
8551 code sequence. We can do that now in the
8552 case of R_PPC64_TLS, and after checking for
8553 tls_get_addr for the TOC16 relocs. */
8554 if (toc_ref == NULL)
8555 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8556 if (toc_ref == NULL)
8557 goto err_free_rel;
8558
8559 if (h != NULL)
8560 value = h->root.u.def.value;
8561 else
8562 value = sym->st_value;
8563 value += rel->r_addend;
8564 if (value % 8 != 0)
8565 continue;
8566 BFD_ASSERT (value < toc->size
8567 && toc->output_offset % 8 == 0);
8568 toc_ref_index = (value + toc->output_offset) / 8;
8569 if (r_type == R_PPC64_TLS
8570 || r_type == R_PPC64_TLSGD
8571 || r_type == R_PPC64_TLSLD)
8572 {
8573 toc_ref[toc_ref_index] = 1;
8574 continue;
8575 }
8576
8577 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8578 continue;
8579
8580 tls_set = 0;
8581 tls_clear = 0;
8582 expecting_tls_get_addr = 2;
8583 break;
8584
8585 case R_PPC64_TPREL64:
8586 if (pass == 0
8587 || sec != toc
8588 || toc_ref == NULL
8589 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8590 continue;
8591 if (ok_tprel)
8592 {
8593 /* IE -> LE */
8594 tls_set = TLS_EXPLICIT;
8595 tls_clear = TLS_TPREL;
8596 break;
8597 }
8598 continue;
8599
8600 case R_PPC64_DTPMOD64:
8601 if (pass == 0
8602 || sec != toc
8603 || toc_ref == NULL
8604 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8605 continue;
8606 if (rel + 1 < relend
8607 && (rel[1].r_info
8608 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8609 && rel[1].r_offset == rel->r_offset + 8)
8610 {
8611 if (ok_tprel)
8612 /* GD -> LE */
8613 tls_set = TLS_EXPLICIT | TLS_GD;
8614 else
8615 /* GD -> IE */
8616 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8617 tls_clear = TLS_GD;
8618 }
8619 else
8620 {
8621 if (!is_local)
8622 continue;
8623
8624 /* LD -> LE */
8625 tls_set = TLS_EXPLICIT;
8626 tls_clear = TLS_LD;
8627 }
8628 break;
8629
8630 default:
8631 continue;
8632 }
8633
8634 if (pass == 0)
8635 {
8636 if (!expecting_tls_get_addr
8637 || !sec->has_tls_get_addr_call)
8638 continue;
8639
8640 if (rel + 1 < relend
8641 && branch_reloc_hash_match (ibfd, rel + 1,
8642 htab->tls_get_addr,
8643 htab->tls_get_addr_fd))
8644 {
8645 if (expecting_tls_get_addr == 2)
8646 {
8647 /* Check for toc tls entries. */
8648 unsigned char *toc_tls;
8649 int retval;
8650
8651 retval = get_tls_mask (&toc_tls, NULL, NULL,
8652 &locsyms,
8653 rel, ibfd);
8654 if (retval == 0)
8655 goto err_free_rel;
8656 if (toc_tls != NULL)
8657 {
8658 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8659 found_tls_get_addr_arg = 1;
8660 if (retval > 1)
8661 toc_ref[toc_ref_index] = 1;
8662 }
8663 }
8664 continue;
8665 }
8666
8667 if (expecting_tls_get_addr != 1)
8668 continue;
8669
8670 /* Uh oh, we didn't find the expected call. We
8671 could just mark this symbol to exclude it
8672 from tls optimization but it's safer to skip
8673 the entire optimization. */
8674 /* xgettext:c-format */
8675 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8676 "TLS optimization disabled\n"),
8677 ibfd, sec, rel->r_offset);
8678 ret = TRUE;
8679 goto err_free_rel;
8680 }
8681
8682 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8683 {
8684 struct plt_entry *ent;
8685 for (ent = htab->tls_get_addr->elf.plt.plist;
8686 ent != NULL;
8687 ent = ent->next)
8688 if (ent->addend == 0)
8689 {
8690 if (ent->plt.refcount > 0)
8691 {
8692 ent->plt.refcount -= 1;
8693 expecting_tls_get_addr = 0;
8694 }
8695 break;
8696 }
8697 }
8698
8699 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8700 {
8701 struct plt_entry *ent;
8702 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8703 ent != NULL;
8704 ent = ent->next)
8705 if (ent->addend == 0)
8706 {
8707 if (ent->plt.refcount > 0)
8708 ent->plt.refcount -= 1;
8709 break;
8710 }
8711 }
8712
8713 if (tls_clear == 0)
8714 continue;
8715
8716 if ((tls_set & TLS_EXPLICIT) == 0)
8717 {
8718 struct got_entry *ent;
8719
8720 /* Adjust got entry for this reloc. */
8721 if (h != NULL)
8722 ent = h->got.glist;
8723 else
8724 ent = elf_local_got_ents (ibfd)[r_symndx];
8725
8726 for (; ent != NULL; ent = ent->next)
8727 if (ent->addend == rel->r_addend
8728 && ent->owner == ibfd
8729 && ent->tls_type == tls_type)
8730 break;
8731 if (ent == NULL)
8732 abort ();
8733
8734 if (tls_set == 0)
8735 {
8736 /* We managed to get rid of a got entry. */
8737 if (ent->got.refcount > 0)
8738 ent->got.refcount -= 1;
8739 }
8740 }
8741 else
8742 {
8743 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8744 we'll lose one or two dyn relocs. */
8745 if (!dec_dynrel_count (rel->r_info, sec, info,
8746 NULL, h, sym))
8747 return FALSE;
8748
8749 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8750 {
8751 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8752 NULL, h, sym))
8753 return FALSE;
8754 }
8755 }
8756
8757 *tls_mask |= tls_set;
8758 *tls_mask &= ~tls_clear;
8759 }
8760
8761 if (elf_section_data (sec)->relocs != relstart)
8762 free (relstart);
8763 }
8764
8765 if (locsyms != NULL
8766 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8767 {
8768 if (!info->keep_memory)
8769 free (locsyms);
8770 else
8771 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8772 }
8773 }
8774
8775 if (toc_ref != NULL)
8776 free (toc_ref);
8777 htab->do_tls_opt = 1;
8778 return TRUE;
8779 }
8780
8781 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8782 the values of any global symbols in a toc section that has been
8783 edited. Globals in toc sections should be a rarity, so this function
8784 sets a flag if any are found in toc sections other than the one just
8785 edited, so that further hash table traversals can be avoided. */
8786
8787 struct adjust_toc_info
8788 {
8789 asection *toc;
8790 unsigned long *skip;
8791 bfd_boolean global_toc_syms;
8792 };
8793
8794 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8795
8796 static bfd_boolean
8797 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8798 {
8799 struct ppc_link_hash_entry *eh;
8800 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8801 unsigned long i;
8802
8803 if (h->root.type != bfd_link_hash_defined
8804 && h->root.type != bfd_link_hash_defweak)
8805 return TRUE;
8806
8807 eh = (struct ppc_link_hash_entry *) h;
8808 if (eh->adjust_done)
8809 return TRUE;
8810
8811 if (eh->elf.root.u.def.section == toc_inf->toc)
8812 {
8813 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8814 i = toc_inf->toc->rawsize >> 3;
8815 else
8816 i = eh->elf.root.u.def.value >> 3;
8817
8818 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8819 {
8820 _bfd_error_handler
8821 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8822 do
8823 ++i;
8824 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8825 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8826 }
8827
8828 eh->elf.root.u.def.value -= toc_inf->skip[i];
8829 eh->adjust_done = 1;
8830 }
8831 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8832 toc_inf->global_toc_syms = TRUE;
8833
8834 return TRUE;
8835 }
8836
8837 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8838 on a _LO variety toc/got reloc. */
8839
8840 static bfd_boolean
8841 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8842 {
8843 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8844 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8845 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8846 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8847 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8848 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8849 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8850 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8851 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8852 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8853 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8854 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8855 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8856 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8857 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8858 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8859 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8860 /* Exclude lfqu by testing reloc. If relocs are ever
8861 defined for the reduced D field in psq_lu then those
8862 will need testing too. */
8863 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8864 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8865 && (insn & 1) == 0)
8866 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8867 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8868 /* Exclude stfqu. psq_stu as above for psq_lu. */
8869 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8870 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8871 && (insn & 1) == 0));
8872 }
8873
8874 /* Examine all relocs referencing .toc sections in order to remove
8875 unused .toc entries. */
8876
8877 bfd_boolean
8878 ppc64_elf_edit_toc (struct bfd_link_info *info)
8879 {
8880 bfd *ibfd;
8881 struct adjust_toc_info toc_inf;
8882 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8883
8884 htab->do_toc_opt = 1;
8885 toc_inf.global_toc_syms = TRUE;
8886 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8887 {
8888 asection *toc, *sec;
8889 Elf_Internal_Shdr *symtab_hdr;
8890 Elf_Internal_Sym *local_syms;
8891 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8892 unsigned long *skip, *drop;
8893 unsigned char *used;
8894 unsigned char *keep, last, some_unused;
8895
8896 if (!is_ppc64_elf (ibfd))
8897 continue;
8898
8899 toc = bfd_get_section_by_name (ibfd, ".toc");
8900 if (toc == NULL
8901 || toc->size == 0
8902 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8903 || discarded_section (toc))
8904 continue;
8905
8906 toc_relocs = NULL;
8907 local_syms = NULL;
8908 symtab_hdr = &elf_symtab_hdr (ibfd);
8909
8910 /* Look at sections dropped from the final link. */
8911 skip = NULL;
8912 relstart = NULL;
8913 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8914 {
8915 if (sec->reloc_count == 0
8916 || !discarded_section (sec)
8917 || get_opd_info (sec)
8918 || (sec->flags & SEC_ALLOC) == 0
8919 || (sec->flags & SEC_DEBUGGING) != 0)
8920 continue;
8921
8922 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8923 if (relstart == NULL)
8924 goto error_ret;
8925
8926 /* Run through the relocs to see which toc entries might be
8927 unused. */
8928 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8929 {
8930 enum elf_ppc64_reloc_type r_type;
8931 unsigned long r_symndx;
8932 asection *sym_sec;
8933 struct elf_link_hash_entry *h;
8934 Elf_Internal_Sym *sym;
8935 bfd_vma val;
8936
8937 r_type = ELF64_R_TYPE (rel->r_info);
8938 switch (r_type)
8939 {
8940 default:
8941 continue;
8942
8943 case R_PPC64_TOC16:
8944 case R_PPC64_TOC16_LO:
8945 case R_PPC64_TOC16_HI:
8946 case R_PPC64_TOC16_HA:
8947 case R_PPC64_TOC16_DS:
8948 case R_PPC64_TOC16_LO_DS:
8949 break;
8950 }
8951
8952 r_symndx = ELF64_R_SYM (rel->r_info);
8953 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8954 r_symndx, ibfd))
8955 goto error_ret;
8956
8957 if (sym_sec != toc)
8958 continue;
8959
8960 if (h != NULL)
8961 val = h->root.u.def.value;
8962 else
8963 val = sym->st_value;
8964 val += rel->r_addend;
8965
8966 if (val >= toc->size)
8967 continue;
8968
8969 /* Anything in the toc ought to be aligned to 8 bytes.
8970 If not, don't mark as unused. */
8971 if (val & 7)
8972 continue;
8973
8974 if (skip == NULL)
8975 {
8976 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8977 if (skip == NULL)
8978 goto error_ret;
8979 }
8980
8981 skip[val >> 3] = ref_from_discarded;
8982 }
8983
8984 if (elf_section_data (sec)->relocs != relstart)
8985 free (relstart);
8986 }
8987
8988 /* For largetoc loads of address constants, we can convert
8989 . addis rx,2,addr@got@ha
8990 . ld ry,addr@got@l(rx)
8991 to
8992 . addis rx,2,addr@toc@ha
8993 . addi ry,rx,addr@toc@l
8994 when addr is within 2G of the toc pointer. This then means
8995 that the word storing "addr" in the toc is no longer needed. */
8996
8997 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8998 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8999 && toc->reloc_count != 0)
9000 {
9001 /* Read toc relocs. */
9002 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9003 info->keep_memory);
9004 if (toc_relocs == NULL)
9005 goto error_ret;
9006
9007 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9008 {
9009 enum elf_ppc64_reloc_type r_type;
9010 unsigned long r_symndx;
9011 asection *sym_sec;
9012 struct elf_link_hash_entry *h;
9013 Elf_Internal_Sym *sym;
9014 bfd_vma val, addr;
9015
9016 r_type = ELF64_R_TYPE (rel->r_info);
9017 if (r_type != R_PPC64_ADDR64)
9018 continue;
9019
9020 r_symndx = ELF64_R_SYM (rel->r_info);
9021 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9022 r_symndx, ibfd))
9023 goto error_ret;
9024
9025 if (sym_sec == NULL
9026 || sym_sec->output_section == NULL
9027 || discarded_section (sym_sec))
9028 continue;
9029
9030 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9031 continue;
9032
9033 if (h != NULL)
9034 {
9035 if (h->type == STT_GNU_IFUNC)
9036 continue;
9037 val = h->root.u.def.value;
9038 }
9039 else
9040 {
9041 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9042 continue;
9043 val = sym->st_value;
9044 }
9045 val += rel->r_addend;
9046 val += sym_sec->output_section->vma + sym_sec->output_offset;
9047
9048 /* We don't yet know the exact toc pointer value, but we
9049 know it will be somewhere in the toc section. Don't
9050 optimize if the difference from any possible toc
9051 pointer is outside [ff..f80008000, 7fff7fff]. */
9052 addr = toc->output_section->vma + TOC_BASE_OFF;
9053 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9054 continue;
9055
9056 addr = toc->output_section->vma + toc->output_section->rawsize;
9057 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9058 continue;
9059
9060 if (skip == NULL)
9061 {
9062 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9063 if (skip == NULL)
9064 goto error_ret;
9065 }
9066
9067 skip[rel->r_offset >> 3]
9068 |= can_optimize | ((rel - toc_relocs) << 2);
9069 }
9070 }
9071
9072 if (skip == NULL)
9073 continue;
9074
9075 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9076 if (used == NULL)
9077 {
9078 error_ret:
9079 if (local_syms != NULL
9080 && symtab_hdr->contents != (unsigned char *) local_syms)
9081 free (local_syms);
9082 if (sec != NULL
9083 && relstart != NULL
9084 && elf_section_data (sec)->relocs != relstart)
9085 free (relstart);
9086 if (toc_relocs != NULL
9087 && elf_section_data (toc)->relocs != toc_relocs)
9088 free (toc_relocs);
9089 if (skip != NULL)
9090 free (skip);
9091 return FALSE;
9092 }
9093
9094 /* Now check all kept sections that might reference the toc.
9095 Check the toc itself last. */
9096 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9097 : ibfd->sections);
9098 sec != NULL;
9099 sec = (sec == toc ? NULL
9100 : sec->next == NULL ? toc
9101 : sec->next == toc && toc->next ? toc->next
9102 : sec->next))
9103 {
9104 int repeat;
9105
9106 if (sec->reloc_count == 0
9107 || discarded_section (sec)
9108 || get_opd_info (sec)
9109 || (sec->flags & SEC_ALLOC) == 0
9110 || (sec->flags & SEC_DEBUGGING) != 0)
9111 continue;
9112
9113 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9114 info->keep_memory);
9115 if (relstart == NULL)
9116 {
9117 free (used);
9118 goto error_ret;
9119 }
9120
9121 /* Mark toc entries referenced as used. */
9122 do
9123 {
9124 repeat = 0;
9125 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9126 {
9127 enum elf_ppc64_reloc_type r_type;
9128 unsigned long r_symndx;
9129 asection *sym_sec;
9130 struct elf_link_hash_entry *h;
9131 Elf_Internal_Sym *sym;
9132 bfd_vma val;
9133 enum {no_check, check_lo, check_ha} insn_check;
9134
9135 r_type = ELF64_R_TYPE (rel->r_info);
9136 switch (r_type)
9137 {
9138 default:
9139 insn_check = no_check;
9140 break;
9141
9142 case R_PPC64_GOT_TLSLD16_HA:
9143 case R_PPC64_GOT_TLSGD16_HA:
9144 case R_PPC64_GOT_TPREL16_HA:
9145 case R_PPC64_GOT_DTPREL16_HA:
9146 case R_PPC64_GOT16_HA:
9147 case R_PPC64_TOC16_HA:
9148 insn_check = check_ha;
9149 break;
9150
9151 case R_PPC64_GOT_TLSLD16_LO:
9152 case R_PPC64_GOT_TLSGD16_LO:
9153 case R_PPC64_GOT_TPREL16_LO_DS:
9154 case R_PPC64_GOT_DTPREL16_LO_DS:
9155 case R_PPC64_GOT16_LO:
9156 case R_PPC64_GOT16_LO_DS:
9157 case R_PPC64_TOC16_LO:
9158 case R_PPC64_TOC16_LO_DS:
9159 insn_check = check_lo;
9160 break;
9161 }
9162
9163 if (insn_check != no_check)
9164 {
9165 bfd_vma off = rel->r_offset & ~3;
9166 unsigned char buf[4];
9167 unsigned int insn;
9168
9169 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9170 {
9171 free (used);
9172 goto error_ret;
9173 }
9174 insn = bfd_get_32 (ibfd, buf);
9175 if (insn_check == check_lo
9176 ? !ok_lo_toc_insn (insn, r_type)
9177 : ((insn & ((0x3f << 26) | 0x1f << 16))
9178 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9179 {
9180 char str[12];
9181
9182 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9183 sprintf (str, "%#08x", insn);
9184 info->callbacks->einfo
9185 /* xgettext:c-format */
9186 (_("%H: toc optimization is not supported for"
9187 " %s instruction.\n"),
9188 ibfd, sec, rel->r_offset & ~3, str);
9189 }
9190 }
9191
9192 switch (r_type)
9193 {
9194 case R_PPC64_TOC16:
9195 case R_PPC64_TOC16_LO:
9196 case R_PPC64_TOC16_HI:
9197 case R_PPC64_TOC16_HA:
9198 case R_PPC64_TOC16_DS:
9199 case R_PPC64_TOC16_LO_DS:
9200 /* In case we're taking addresses of toc entries. */
9201 case R_PPC64_ADDR64:
9202 break;
9203
9204 default:
9205 continue;
9206 }
9207
9208 r_symndx = ELF64_R_SYM (rel->r_info);
9209 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9210 r_symndx, ibfd))
9211 {
9212 free (used);
9213 goto error_ret;
9214 }
9215
9216 if (sym_sec != toc)
9217 continue;
9218
9219 if (h != NULL)
9220 val = h->root.u.def.value;
9221 else
9222 val = sym->st_value;
9223 val += rel->r_addend;
9224
9225 if (val >= toc->size)
9226 continue;
9227
9228 if ((skip[val >> 3] & can_optimize) != 0)
9229 {
9230 bfd_vma off;
9231 unsigned char opc;
9232
9233 switch (r_type)
9234 {
9235 case R_PPC64_TOC16_HA:
9236 break;
9237
9238 case R_PPC64_TOC16_LO_DS:
9239 off = rel->r_offset;
9240 off += (bfd_big_endian (ibfd) ? -2 : 3);
9241 if (!bfd_get_section_contents (ibfd, sec, &opc,
9242 off, 1))
9243 {
9244 free (used);
9245 goto error_ret;
9246 }
9247 if ((opc & (0x3f << 2)) == (58u << 2))
9248 break;
9249 /* Fall through. */
9250
9251 default:
9252 /* Wrong sort of reloc, or not a ld. We may
9253 as well clear ref_from_discarded too. */
9254 skip[val >> 3] = 0;
9255 }
9256 }
9257
9258 if (sec != toc)
9259 used[val >> 3] = 1;
9260 /* For the toc section, we only mark as used if this
9261 entry itself isn't unused. */
9262 else if ((used[rel->r_offset >> 3]
9263 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9264 && !used[val >> 3])
9265 {
9266 /* Do all the relocs again, to catch reference
9267 chains. */
9268 repeat = 1;
9269 used[val >> 3] = 1;
9270 }
9271 }
9272 }
9273 while (repeat);
9274
9275 if (elf_section_data (sec)->relocs != relstart)
9276 free (relstart);
9277 }
9278
9279 /* Merge the used and skip arrays. Assume that TOC
9280 doublewords not appearing as either used or unused belong
9281 to an entry more than one doubleword in size. */
9282 for (drop = skip, keep = used, last = 0, some_unused = 0;
9283 drop < skip + (toc->size + 7) / 8;
9284 ++drop, ++keep)
9285 {
9286 if (*keep)
9287 {
9288 *drop &= ~ref_from_discarded;
9289 if ((*drop & can_optimize) != 0)
9290 some_unused = 1;
9291 last = 0;
9292 }
9293 else if ((*drop & ref_from_discarded) != 0)
9294 {
9295 some_unused = 1;
9296 last = ref_from_discarded;
9297 }
9298 else
9299 *drop = last;
9300 }
9301
9302 free (used);
9303
9304 if (some_unused)
9305 {
9306 bfd_byte *contents, *src;
9307 unsigned long off;
9308 Elf_Internal_Sym *sym;
9309 bfd_boolean local_toc_syms = FALSE;
9310
9311 /* Shuffle the toc contents, and at the same time convert the
9312 skip array from booleans into offsets. */
9313 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9314 goto error_ret;
9315
9316 elf_section_data (toc)->this_hdr.contents = contents;
9317
9318 for (src = contents, off = 0, drop = skip;
9319 src < contents + toc->size;
9320 src += 8, ++drop)
9321 {
9322 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9323 off += 8;
9324 else if (off != 0)
9325 {
9326 *drop = off;
9327 memcpy (src - off, src, 8);
9328 }
9329 }
9330 *drop = off;
9331 toc->rawsize = toc->size;
9332 toc->size = src - contents - off;
9333
9334 /* Adjust addends for relocs against the toc section sym,
9335 and optimize any accesses we can. */
9336 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9337 {
9338 if (sec->reloc_count == 0
9339 || discarded_section (sec))
9340 continue;
9341
9342 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9343 info->keep_memory);
9344 if (relstart == NULL)
9345 goto error_ret;
9346
9347 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9348 {
9349 enum elf_ppc64_reloc_type r_type;
9350 unsigned long r_symndx;
9351 asection *sym_sec;
9352 struct elf_link_hash_entry *h;
9353 bfd_vma val;
9354
9355 r_type = ELF64_R_TYPE (rel->r_info);
9356 switch (r_type)
9357 {
9358 default:
9359 continue;
9360
9361 case R_PPC64_TOC16:
9362 case R_PPC64_TOC16_LO:
9363 case R_PPC64_TOC16_HI:
9364 case R_PPC64_TOC16_HA:
9365 case R_PPC64_TOC16_DS:
9366 case R_PPC64_TOC16_LO_DS:
9367 case R_PPC64_ADDR64:
9368 break;
9369 }
9370
9371 r_symndx = ELF64_R_SYM (rel->r_info);
9372 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9373 r_symndx, ibfd))
9374 goto error_ret;
9375
9376 if (sym_sec != toc)
9377 continue;
9378
9379 if (h != NULL)
9380 val = h->root.u.def.value;
9381 else
9382 {
9383 val = sym->st_value;
9384 if (val != 0)
9385 local_toc_syms = TRUE;
9386 }
9387
9388 val += rel->r_addend;
9389
9390 if (val > toc->rawsize)
9391 val = toc->rawsize;
9392 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9393 continue;
9394 else if ((skip[val >> 3] & can_optimize) != 0)
9395 {
9396 Elf_Internal_Rela *tocrel
9397 = toc_relocs + (skip[val >> 3] >> 2);
9398 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9399
9400 switch (r_type)
9401 {
9402 case R_PPC64_TOC16_HA:
9403 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9404 break;
9405
9406 case R_PPC64_TOC16_LO_DS:
9407 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9408 break;
9409
9410 default:
9411 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9412 ppc_howto_init ();
9413 info->callbacks->einfo
9414 /* xgettext:c-format */
9415 (_("%H: %s references "
9416 "optimized away TOC entry\n"),
9417 ibfd, sec, rel->r_offset,
9418 ppc64_elf_howto_table[r_type]->name);
9419 bfd_set_error (bfd_error_bad_value);
9420 goto error_ret;
9421 }
9422 rel->r_addend = tocrel->r_addend;
9423 elf_section_data (sec)->relocs = relstart;
9424 continue;
9425 }
9426
9427 if (h != NULL || sym->st_value != 0)
9428 continue;
9429
9430 rel->r_addend -= skip[val >> 3];
9431 elf_section_data (sec)->relocs = relstart;
9432 }
9433
9434 if (elf_section_data (sec)->relocs != relstart)
9435 free (relstart);
9436 }
9437
9438 /* We shouldn't have local or global symbols defined in the TOC,
9439 but handle them anyway. */
9440 if (local_syms != NULL)
9441 for (sym = local_syms;
9442 sym < local_syms + symtab_hdr->sh_info;
9443 ++sym)
9444 if (sym->st_value != 0
9445 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9446 {
9447 unsigned long i;
9448
9449 if (sym->st_value > toc->rawsize)
9450 i = toc->rawsize >> 3;
9451 else
9452 i = sym->st_value >> 3;
9453
9454 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9455 {
9456 if (local_toc_syms)
9457 _bfd_error_handler
9458 (_("%s defined on removed toc entry"),
9459 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9460 do
9461 ++i;
9462 while ((skip[i] & (ref_from_discarded | can_optimize)));
9463 sym->st_value = (bfd_vma) i << 3;
9464 }
9465
9466 sym->st_value -= skip[i];
9467 symtab_hdr->contents = (unsigned char *) local_syms;
9468 }
9469
9470 /* Adjust any global syms defined in this toc input section. */
9471 if (toc_inf.global_toc_syms)
9472 {
9473 toc_inf.toc = toc;
9474 toc_inf.skip = skip;
9475 toc_inf.global_toc_syms = FALSE;
9476 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9477 &toc_inf);
9478 }
9479
9480 if (toc->reloc_count != 0)
9481 {
9482 Elf_Internal_Shdr *rel_hdr;
9483 Elf_Internal_Rela *wrel;
9484 bfd_size_type sz;
9485
9486 /* Remove unused toc relocs, and adjust those we keep. */
9487 if (toc_relocs == NULL)
9488 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9489 info->keep_memory);
9490 if (toc_relocs == NULL)
9491 goto error_ret;
9492
9493 wrel = toc_relocs;
9494 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9495 if ((skip[rel->r_offset >> 3]
9496 & (ref_from_discarded | can_optimize)) == 0)
9497 {
9498 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9499 wrel->r_info = rel->r_info;
9500 wrel->r_addend = rel->r_addend;
9501 ++wrel;
9502 }
9503 else if (!dec_dynrel_count (rel->r_info, toc, info,
9504 &local_syms, NULL, NULL))
9505 goto error_ret;
9506
9507 elf_section_data (toc)->relocs = toc_relocs;
9508 toc->reloc_count = wrel - toc_relocs;
9509 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9510 sz = rel_hdr->sh_entsize;
9511 rel_hdr->sh_size = toc->reloc_count * sz;
9512 }
9513 }
9514 else if (toc_relocs != NULL
9515 && elf_section_data (toc)->relocs != toc_relocs)
9516 free (toc_relocs);
9517
9518 if (local_syms != NULL
9519 && symtab_hdr->contents != (unsigned char *) local_syms)
9520 {
9521 if (!info->keep_memory)
9522 free (local_syms);
9523 else
9524 symtab_hdr->contents = (unsigned char *) local_syms;
9525 }
9526 free (skip);
9527 }
9528
9529 return TRUE;
9530 }
9531
9532 /* Return true iff input section I references the TOC using
9533 instructions limited to +/-32k offsets. */
9534
9535 bfd_boolean
9536 ppc64_elf_has_small_toc_reloc (asection *i)
9537 {
9538 return (is_ppc64_elf (i->owner)
9539 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9540 }
9541
9542 /* Allocate space for one GOT entry. */
9543
9544 static void
9545 allocate_got (struct elf_link_hash_entry *h,
9546 struct bfd_link_info *info,
9547 struct got_entry *gent)
9548 {
9549 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9550 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9551 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9552 ? 16 : 8);
9553 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9554 ? 2 : 1) * sizeof (Elf64_External_Rela);
9555 asection *got = ppc64_elf_tdata (gent->owner)->got;
9556
9557 gent->got.offset = got->size;
9558 got->size += entsize;
9559
9560 if (h->type == STT_GNU_IFUNC)
9561 {
9562 htab->elf.irelplt->size += rentsize;
9563 htab->got_reli_size += rentsize;
9564 }
9565 else if ((bfd_link_pic (info)
9566 || (htab->elf.dynamic_sections_created
9567 && h->dynindx != -1
9568 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9569 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9570 {
9571 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9572 relgot->size += rentsize;
9573 }
9574 }
9575
9576 /* This function merges got entries in the same toc group. */
9577
9578 static void
9579 merge_got_entries (struct got_entry **pent)
9580 {
9581 struct got_entry *ent, *ent2;
9582
9583 for (ent = *pent; ent != NULL; ent = ent->next)
9584 if (!ent->is_indirect)
9585 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9586 if (!ent2->is_indirect
9587 && ent2->addend == ent->addend
9588 && ent2->tls_type == ent->tls_type
9589 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9590 {
9591 ent2->is_indirect = TRUE;
9592 ent2->got.ent = ent;
9593 }
9594 }
9595
9596 /* If H is undefined, make it dynamic if that makes sense. */
9597
9598 static bfd_boolean
9599 ensure_undef_dynamic (struct bfd_link_info *info,
9600 struct elf_link_hash_entry *h)
9601 {
9602 struct elf_link_hash_table *htab = elf_hash_table (info);
9603
9604 if (htab->dynamic_sections_created
9605 && ((info->dynamic_undefined_weak != 0
9606 && h->root.type == bfd_link_hash_undefweak)
9607 || h->root.type == bfd_link_hash_undefined)
9608 && h->dynindx == -1
9609 && !h->forced_local
9610 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9611 return bfd_elf_link_record_dynamic_symbol (info, h);
9612 return TRUE;
9613 }
9614
9615 /* Allocate space in .plt, .got and associated reloc sections for
9616 dynamic relocs. */
9617
9618 static bfd_boolean
9619 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9620 {
9621 struct bfd_link_info *info;
9622 struct ppc_link_hash_table *htab;
9623 asection *s;
9624 struct ppc_link_hash_entry *eh;
9625 struct got_entry **pgent, *gent;
9626
9627 if (h->root.type == bfd_link_hash_indirect)
9628 return TRUE;
9629
9630 info = (struct bfd_link_info *) inf;
9631 htab = ppc_hash_table (info);
9632 if (htab == NULL)
9633 return FALSE;
9634
9635 eh = (struct ppc_link_hash_entry *) h;
9636 /* Run through the TLS GD got entries first if we're changing them
9637 to TPREL. */
9638 if ((eh->tls_mask & TLS_TPRELGD) != 0)
9639 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9640 if (gent->got.refcount > 0
9641 && (gent->tls_type & TLS_GD) != 0)
9642 {
9643 /* This was a GD entry that has been converted to TPREL. If
9644 there happens to be a TPREL entry we can use that one. */
9645 struct got_entry *ent;
9646 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9647 if (ent->got.refcount > 0
9648 && (ent->tls_type & TLS_TPREL) != 0
9649 && ent->addend == gent->addend
9650 && ent->owner == gent->owner)
9651 {
9652 gent->got.refcount = 0;
9653 break;
9654 }
9655
9656 /* If not, then we'll be using our own TPREL entry. */
9657 if (gent->got.refcount != 0)
9658 gent->tls_type = TLS_TLS | TLS_TPREL;
9659 }
9660
9661 /* Remove any list entry that won't generate a word in the GOT before
9662 we call merge_got_entries. Otherwise we risk merging to empty
9663 entries. */
9664 pgent = &h->got.glist;
9665 while ((gent = *pgent) != NULL)
9666 if (gent->got.refcount > 0)
9667 {
9668 if ((gent->tls_type & TLS_LD) != 0
9669 && !h->def_dynamic)
9670 {
9671 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9672 *pgent = gent->next;
9673 }
9674 else
9675 pgent = &gent->next;
9676 }
9677 else
9678 *pgent = gent->next;
9679
9680 if (!htab->do_multi_toc)
9681 merge_got_entries (&h->got.glist);
9682
9683 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9684 if (!gent->is_indirect)
9685 {
9686 /* Make sure this symbol is output as a dynamic symbol. */
9687 if (!ensure_undef_dynamic (info, h))
9688 return FALSE;
9689
9690 if (!is_ppc64_elf (gent->owner))
9691 abort ();
9692
9693 allocate_got (h, info, gent);
9694 }
9695
9696 /* If no dynamic sections we can't have dynamic relocs, except for
9697 IFUNCs which are handled even in static executables. */
9698 if (!htab->elf.dynamic_sections_created
9699 && h->type != STT_GNU_IFUNC)
9700 eh->dyn_relocs = NULL;
9701
9702 /* Also discard relocs on undefined weak syms with non-default
9703 visibility, or when dynamic_undefined_weak says so. */
9704 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9705 eh->dyn_relocs = NULL;
9706
9707 if (eh->dyn_relocs != NULL)
9708 {
9709 struct elf_dyn_relocs *p, **pp;
9710
9711 /* In the shared -Bsymbolic case, discard space allocated for
9712 dynamic pc-relative relocs against symbols which turn out to
9713 be defined in regular objects. For the normal shared case,
9714 discard space for relocs that have become local due to symbol
9715 visibility changes. */
9716
9717 if (bfd_link_pic (info))
9718 {
9719 /* Relocs that use pc_count are those that appear on a call
9720 insn, or certain REL relocs (see must_be_dyn_reloc) that
9721 can be generated via assembly. We want calls to
9722 protected symbols to resolve directly to the function
9723 rather than going via the plt. If people want function
9724 pointer comparisons to work as expected then they should
9725 avoid writing weird assembly. */
9726 if (SYMBOL_CALLS_LOCAL (info, h))
9727 {
9728 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9729 {
9730 p->count -= p->pc_count;
9731 p->pc_count = 0;
9732 if (p->count == 0)
9733 *pp = p->next;
9734 else
9735 pp = &p->next;
9736 }
9737 }
9738
9739 if (eh->dyn_relocs != NULL)
9740 {
9741 /* Make sure this symbol is output as a dynamic symbol. */
9742 if (!ensure_undef_dynamic (info, h))
9743 return FALSE;
9744 }
9745 }
9746 else if (h->type == STT_GNU_IFUNC)
9747 {
9748 /* A plt entry is always created when making direct calls to
9749 an ifunc, even when building a static executable, but
9750 that doesn't cover all cases. We may have only an ifunc
9751 initialised function pointer for a given ifunc symbol.
9752
9753 For ELFv2, dynamic relocations are not required when
9754 generating a global entry PLT stub. */
9755 if (abiversion (info->output_bfd) >= 2)
9756 {
9757 if (global_entry_stub (h))
9758 eh->dyn_relocs = NULL;
9759 }
9760
9761 /* For ELFv1 we have function descriptors. Descriptors need
9762 to be treated like PLT entries and thus have dynamic
9763 relocations. One exception is when the function
9764 descriptor is copied into .dynbss (which should only
9765 happen with ancient versions of gcc). */
9766 else if (h->needs_copy)
9767 eh->dyn_relocs = NULL;
9768 }
9769 else if (ELIMINATE_COPY_RELOCS)
9770 {
9771 /* For the non-pic case, discard space for relocs against
9772 symbols which turn out to need copy relocs or are not
9773 dynamic. */
9774 if (!h->non_got_ref
9775 && !h->def_regular)
9776 {
9777 /* Make sure this symbol is output as a dynamic symbol. */
9778 if (!ensure_undef_dynamic (info, h))
9779 return FALSE;
9780
9781 if (h->dynindx == -1)
9782 eh->dyn_relocs = NULL;
9783 }
9784 else
9785 eh->dyn_relocs = NULL;
9786 }
9787
9788 /* Finally, allocate space. */
9789 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9790 {
9791 asection *sreloc = elf_section_data (p->sec)->sreloc;
9792 if (eh->elf.type == STT_GNU_IFUNC)
9793 sreloc = htab->elf.irelplt;
9794 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9795 }
9796 }
9797
9798 if ((htab->elf.dynamic_sections_created
9799 && h->dynindx != -1)
9800 || h->type == STT_GNU_IFUNC)
9801 {
9802 struct plt_entry *pent;
9803 bfd_boolean doneone = FALSE;
9804 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9805 if (pent->plt.refcount > 0)
9806 {
9807 if (!htab->elf.dynamic_sections_created
9808 || h->dynindx == -1)
9809 {
9810 s = htab->elf.iplt;
9811 pent->plt.offset = s->size;
9812 s->size += PLT_ENTRY_SIZE (htab);
9813 s = htab->elf.irelplt;
9814 }
9815 else
9816 {
9817 /* If this is the first .plt entry, make room for the special
9818 first entry. */
9819 s = htab->elf.splt;
9820 if (s->size == 0)
9821 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9822
9823 pent->plt.offset = s->size;
9824
9825 /* Make room for this entry. */
9826 s->size += PLT_ENTRY_SIZE (htab);
9827
9828 /* Make room for the .glink code. */
9829 s = htab->glink;
9830 if (s->size == 0)
9831 s->size += GLINK_CALL_STUB_SIZE;
9832 if (htab->opd_abi)
9833 {
9834 /* We need bigger stubs past index 32767. */
9835 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9836 s->size += 4;
9837 s->size += 2*4;
9838 }
9839 else
9840 s->size += 4;
9841
9842 /* We also need to make an entry in the .rela.plt section. */
9843 s = htab->elf.srelplt;
9844 }
9845 s->size += sizeof (Elf64_External_Rela);
9846 doneone = TRUE;
9847 }
9848 else
9849 pent->plt.offset = (bfd_vma) -1;
9850 if (!doneone)
9851 {
9852 h->plt.plist = NULL;
9853 h->needs_plt = 0;
9854 }
9855 }
9856 else
9857 {
9858 h->plt.plist = NULL;
9859 h->needs_plt = 0;
9860 }
9861
9862 return TRUE;
9863 }
9864
9865 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9866 to set up space for global entry stubs. These are put in glink,
9867 after the branch table. */
9868
9869 static bfd_boolean
9870 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9871 {
9872 struct bfd_link_info *info;
9873 struct ppc_link_hash_table *htab;
9874 struct plt_entry *pent;
9875 asection *s;
9876
9877 if (h->root.type == bfd_link_hash_indirect)
9878 return TRUE;
9879
9880 if (!h->pointer_equality_needed)
9881 return TRUE;
9882
9883 if (h->def_regular)
9884 return TRUE;
9885
9886 info = inf;
9887 htab = ppc_hash_table (info);
9888 if (htab == NULL)
9889 return FALSE;
9890
9891 s = htab->glink;
9892 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9893 if (pent->plt.offset != (bfd_vma) -1
9894 && pent->addend == 0)
9895 {
9896 /* For ELFv2, if this symbol is not defined in a regular file
9897 and we are not generating a shared library or pie, then we
9898 need to define the symbol in the executable on a call stub.
9899 This is to avoid text relocations. */
9900 s->size = (s->size + 15) & -16;
9901 h->root.type = bfd_link_hash_defined;
9902 h->root.u.def.section = s;
9903 h->root.u.def.value = s->size;
9904 s->size += 16;
9905 break;
9906 }
9907 return TRUE;
9908 }
9909
9910 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9911 read-only sections. */
9912
9913 static bfd_boolean
9914 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9915 {
9916 if (h->root.type == bfd_link_hash_indirect)
9917 return TRUE;
9918
9919 if (readonly_dynrelocs (h))
9920 {
9921 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9922
9923 /* Not an error, just cut short the traversal. */
9924 return FALSE;
9925 }
9926 return TRUE;
9927 }
9928
9929 /* Set the sizes of the dynamic sections. */
9930
9931 static bfd_boolean
9932 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9933 struct bfd_link_info *info)
9934 {
9935 struct ppc_link_hash_table *htab;
9936 bfd *dynobj;
9937 asection *s;
9938 bfd_boolean relocs;
9939 bfd *ibfd;
9940 struct got_entry *first_tlsld;
9941
9942 htab = ppc_hash_table (info);
9943 if (htab == NULL)
9944 return FALSE;
9945
9946 dynobj = htab->elf.dynobj;
9947 if (dynobj == NULL)
9948 abort ();
9949
9950 if (htab->elf.dynamic_sections_created)
9951 {
9952 /* Set the contents of the .interp section to the interpreter. */
9953 if (bfd_link_executable (info) && !info->nointerp)
9954 {
9955 s = bfd_get_linker_section (dynobj, ".interp");
9956 if (s == NULL)
9957 abort ();
9958 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9959 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9960 }
9961 }
9962
9963 /* Set up .got offsets for local syms, and space for local dynamic
9964 relocs. */
9965 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9966 {
9967 struct got_entry **lgot_ents;
9968 struct got_entry **end_lgot_ents;
9969 struct plt_entry **local_plt;
9970 struct plt_entry **end_local_plt;
9971 unsigned char *lgot_masks;
9972 bfd_size_type locsymcount;
9973 Elf_Internal_Shdr *symtab_hdr;
9974
9975 if (!is_ppc64_elf (ibfd))
9976 continue;
9977
9978 for (s = ibfd->sections; s != NULL; s = s->next)
9979 {
9980 struct ppc_dyn_relocs *p;
9981
9982 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9983 {
9984 if (!bfd_is_abs_section (p->sec)
9985 && bfd_is_abs_section (p->sec->output_section))
9986 {
9987 /* Input section has been discarded, either because
9988 it is a copy of a linkonce section or due to
9989 linker script /DISCARD/, so we'll be discarding
9990 the relocs too. */
9991 }
9992 else if (p->count != 0)
9993 {
9994 asection *srel = elf_section_data (p->sec)->sreloc;
9995 if (p->ifunc)
9996 srel = htab->elf.irelplt;
9997 srel->size += p->count * sizeof (Elf64_External_Rela);
9998 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9999 info->flags |= DF_TEXTREL;
10000 }
10001 }
10002 }
10003
10004 lgot_ents = elf_local_got_ents (ibfd);
10005 if (!lgot_ents)
10006 continue;
10007
10008 symtab_hdr = &elf_symtab_hdr (ibfd);
10009 locsymcount = symtab_hdr->sh_info;
10010 end_lgot_ents = lgot_ents + locsymcount;
10011 local_plt = (struct plt_entry **) end_lgot_ents;
10012 end_local_plt = local_plt + locsymcount;
10013 lgot_masks = (unsigned char *) end_local_plt;
10014 s = ppc64_elf_tdata (ibfd)->got;
10015 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10016 {
10017 struct got_entry **pent, *ent;
10018
10019 pent = lgot_ents;
10020 while ((ent = *pent) != NULL)
10021 if (ent->got.refcount > 0)
10022 {
10023 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10024 {
10025 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10026 *pent = ent->next;
10027 }
10028 else
10029 {
10030 unsigned int ent_size = 8;
10031 unsigned int rel_size = sizeof (Elf64_External_Rela);
10032
10033 ent->got.offset = s->size;
10034 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10035 {
10036 ent_size *= 2;
10037 rel_size *= 2;
10038 }
10039 s->size += ent_size;
10040 if ((*lgot_masks & PLT_IFUNC) != 0)
10041 {
10042 htab->elf.irelplt->size += rel_size;
10043 htab->got_reli_size += rel_size;
10044 }
10045 else if (bfd_link_pic (info))
10046 {
10047 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10048 srel->size += rel_size;
10049 }
10050 pent = &ent->next;
10051 }
10052 }
10053 else
10054 *pent = ent->next;
10055 }
10056
10057 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10058 for (; local_plt < end_local_plt; ++local_plt)
10059 {
10060 struct plt_entry *ent;
10061
10062 for (ent = *local_plt; ent != NULL; ent = ent->next)
10063 if (ent->plt.refcount > 0)
10064 {
10065 s = htab->elf.iplt;
10066 ent->plt.offset = s->size;
10067 s->size += PLT_ENTRY_SIZE (htab);
10068
10069 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10070 }
10071 else
10072 ent->plt.offset = (bfd_vma) -1;
10073 }
10074 }
10075
10076 /* Allocate global sym .plt and .got entries, and space for global
10077 sym dynamic relocs. */
10078 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10079 /* Stash the end of glink branch table. */
10080 if (htab->glink != NULL)
10081 htab->glink->rawsize = htab->glink->size;
10082
10083 if (!htab->opd_abi && !bfd_link_pic (info))
10084 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10085
10086 first_tlsld = NULL;
10087 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10088 {
10089 struct got_entry *ent;
10090
10091 if (!is_ppc64_elf (ibfd))
10092 continue;
10093
10094 ent = ppc64_tlsld_got (ibfd);
10095 if (ent->got.refcount > 0)
10096 {
10097 if (!htab->do_multi_toc && first_tlsld != NULL)
10098 {
10099 ent->is_indirect = TRUE;
10100 ent->got.ent = first_tlsld;
10101 }
10102 else
10103 {
10104 if (first_tlsld == NULL)
10105 first_tlsld = ent;
10106 s = ppc64_elf_tdata (ibfd)->got;
10107 ent->got.offset = s->size;
10108 ent->owner = ibfd;
10109 s->size += 16;
10110 if (bfd_link_pic (info))
10111 {
10112 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10113 srel->size += sizeof (Elf64_External_Rela);
10114 }
10115 }
10116 }
10117 else
10118 ent->got.offset = (bfd_vma) -1;
10119 }
10120
10121 /* We now have determined the sizes of the various dynamic sections.
10122 Allocate memory for them. */
10123 relocs = FALSE;
10124 for (s = dynobj->sections; s != NULL; s = s->next)
10125 {
10126 if ((s->flags & SEC_LINKER_CREATED) == 0)
10127 continue;
10128
10129 if (s == htab->brlt || s == htab->relbrlt)
10130 /* These haven't been allocated yet; don't strip. */
10131 continue;
10132 else if (s == htab->elf.sgot
10133 || s == htab->elf.splt
10134 || s == htab->elf.iplt
10135 || s == htab->glink
10136 || s == htab->elf.sdynbss
10137 || s == htab->elf.sdynrelro)
10138 {
10139 /* Strip this section if we don't need it; see the
10140 comment below. */
10141 }
10142 else if (s == htab->glink_eh_frame)
10143 {
10144 if (!bfd_is_abs_section (s->output_section))
10145 /* Not sized yet. */
10146 continue;
10147 }
10148 else if (CONST_STRNEQ (s->name, ".rela"))
10149 {
10150 if (s->size != 0)
10151 {
10152 if (s != htab->elf.srelplt)
10153 relocs = TRUE;
10154
10155 /* We use the reloc_count field as a counter if we need
10156 to copy relocs into the output file. */
10157 s->reloc_count = 0;
10158 }
10159 }
10160 else
10161 {
10162 /* It's not one of our sections, so don't allocate space. */
10163 continue;
10164 }
10165
10166 if (s->size == 0)
10167 {
10168 /* If we don't need this section, strip it from the
10169 output file. This is mostly to handle .rela.bss and
10170 .rela.plt. We must create both sections in
10171 create_dynamic_sections, because they must be created
10172 before the linker maps input sections to output
10173 sections. The linker does that before
10174 adjust_dynamic_symbol is called, and it is that
10175 function which decides whether anything needs to go
10176 into these sections. */
10177 s->flags |= SEC_EXCLUDE;
10178 continue;
10179 }
10180
10181 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10182 continue;
10183
10184 /* Allocate memory for the section contents. We use bfd_zalloc
10185 here in case unused entries are not reclaimed before the
10186 section's contents are written out. This should not happen,
10187 but this way if it does we get a R_PPC64_NONE reloc in .rela
10188 sections instead of garbage.
10189 We also rely on the section contents being zero when writing
10190 the GOT and .dynrelro. */
10191 s->contents = bfd_zalloc (dynobj, s->size);
10192 if (s->contents == NULL)
10193 return FALSE;
10194 }
10195
10196 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10197 {
10198 if (!is_ppc64_elf (ibfd))
10199 continue;
10200
10201 s = ppc64_elf_tdata (ibfd)->got;
10202 if (s != NULL && s != htab->elf.sgot)
10203 {
10204 if (s->size == 0)
10205 s->flags |= SEC_EXCLUDE;
10206 else
10207 {
10208 s->contents = bfd_zalloc (ibfd, s->size);
10209 if (s->contents == NULL)
10210 return FALSE;
10211 }
10212 }
10213 s = ppc64_elf_tdata (ibfd)->relgot;
10214 if (s != NULL)
10215 {
10216 if (s->size == 0)
10217 s->flags |= SEC_EXCLUDE;
10218 else
10219 {
10220 s->contents = bfd_zalloc (ibfd, s->size);
10221 if (s->contents == NULL)
10222 return FALSE;
10223 relocs = TRUE;
10224 s->reloc_count = 0;
10225 }
10226 }
10227 }
10228
10229 if (htab->elf.dynamic_sections_created)
10230 {
10231 bfd_boolean tls_opt;
10232
10233 /* Add some entries to the .dynamic section. We fill in the
10234 values later, in ppc64_elf_finish_dynamic_sections, but we
10235 must add the entries now so that we get the correct size for
10236 the .dynamic section. The DT_DEBUG entry is filled in by the
10237 dynamic linker and used by the debugger. */
10238 #define add_dynamic_entry(TAG, VAL) \
10239 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10240
10241 if (bfd_link_executable (info))
10242 {
10243 if (!add_dynamic_entry (DT_DEBUG, 0))
10244 return FALSE;
10245 }
10246
10247 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10248 {
10249 if (!add_dynamic_entry (DT_PLTGOT, 0)
10250 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10251 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10252 || !add_dynamic_entry (DT_JMPREL, 0)
10253 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10254 return FALSE;
10255 }
10256
10257 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10258 {
10259 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10260 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10261 return FALSE;
10262 }
10263
10264 tls_opt = (htab->params->tls_get_addr_opt
10265 && htab->tls_get_addr_fd != NULL
10266 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10267 if (tls_opt || !htab->opd_abi)
10268 {
10269 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10270 return FALSE;
10271 }
10272
10273 if (relocs)
10274 {
10275 if (!add_dynamic_entry (DT_RELA, 0)
10276 || !add_dynamic_entry (DT_RELASZ, 0)
10277 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10278 return FALSE;
10279
10280 /* If any dynamic relocs apply to a read-only section,
10281 then we need a DT_TEXTREL entry. */
10282 if ((info->flags & DF_TEXTREL) == 0)
10283 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10284
10285 if ((info->flags & DF_TEXTREL) != 0)
10286 {
10287 if (!add_dynamic_entry (DT_TEXTREL, 0))
10288 return FALSE;
10289 }
10290 }
10291 }
10292 #undef add_dynamic_entry
10293
10294 return TRUE;
10295 }
10296
10297 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10298
10299 static bfd_boolean
10300 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10301 {
10302 if (h->plt.plist != NULL
10303 && !h->def_regular
10304 && !h->pointer_equality_needed)
10305 return FALSE;
10306
10307 return _bfd_elf_hash_symbol (h);
10308 }
10309
10310 /* Determine the type of stub needed, if any, for a call. */
10311
10312 static inline enum ppc_stub_type
10313 ppc_type_of_stub (asection *input_sec,
10314 const Elf_Internal_Rela *rel,
10315 struct ppc_link_hash_entry **hash,
10316 struct plt_entry **plt_ent,
10317 bfd_vma destination,
10318 unsigned long local_off)
10319 {
10320 struct ppc_link_hash_entry *h = *hash;
10321 bfd_vma location;
10322 bfd_vma branch_offset;
10323 bfd_vma max_branch_offset;
10324 enum elf_ppc64_reloc_type r_type;
10325
10326 if (h != NULL)
10327 {
10328 struct plt_entry *ent;
10329 struct ppc_link_hash_entry *fdh = h;
10330 if (h->oh != NULL
10331 && h->oh->is_func_descriptor)
10332 {
10333 fdh = ppc_follow_link (h->oh);
10334 *hash = fdh;
10335 }
10336
10337 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10338 if (ent->addend == rel->r_addend
10339 && ent->plt.offset != (bfd_vma) -1)
10340 {
10341 *plt_ent = ent;
10342 return ppc_stub_plt_call;
10343 }
10344
10345 /* Here, we know we don't have a plt entry. If we don't have a
10346 either a defined function descriptor or a defined entry symbol
10347 in a regular object file, then it is pointless trying to make
10348 any other type of stub. */
10349 if (!is_static_defined (&fdh->elf)
10350 && !is_static_defined (&h->elf))
10351 return ppc_stub_none;
10352 }
10353 else if (elf_local_got_ents (input_sec->owner) != NULL)
10354 {
10355 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10356 struct plt_entry **local_plt = (struct plt_entry **)
10357 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10358 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10359
10360 if (local_plt[r_symndx] != NULL)
10361 {
10362 struct plt_entry *ent;
10363
10364 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10365 if (ent->addend == rel->r_addend
10366 && ent->plt.offset != (bfd_vma) -1)
10367 {
10368 *plt_ent = ent;
10369 return ppc_stub_plt_call;
10370 }
10371 }
10372 }
10373
10374 /* Determine where the call point is. */
10375 location = (input_sec->output_offset
10376 + input_sec->output_section->vma
10377 + rel->r_offset);
10378
10379 branch_offset = destination - location;
10380 r_type = ELF64_R_TYPE (rel->r_info);
10381
10382 /* Determine if a long branch stub is needed. */
10383 max_branch_offset = 1 << 25;
10384 if (r_type != R_PPC64_REL24)
10385 max_branch_offset = 1 << 15;
10386
10387 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10388 /* We need a stub. Figure out whether a long_branch or plt_branch
10389 is needed later. */
10390 return ppc_stub_long_branch;
10391
10392 return ppc_stub_none;
10393 }
10394
10395 /* With power7 weakly ordered memory model, it is possible for ld.so
10396 to update a plt entry in one thread and have another thread see a
10397 stale zero toc entry. To avoid this we need some sort of acquire
10398 barrier in the call stub. One solution is to make the load of the
10399 toc word seem to appear to depend on the load of the function entry
10400 word. Another solution is to test for r2 being zero, and branch to
10401 the appropriate glink entry if so.
10402
10403 . fake dep barrier compare
10404 . ld 12,xxx(2) ld 12,xxx(2)
10405 . mtctr 12 mtctr 12
10406 . xor 11,12,12 ld 2,xxx+8(2)
10407 . add 2,2,11 cmpldi 2,0
10408 . ld 2,xxx+8(2) bnectr+
10409 . bctr b <glink_entry>
10410
10411 The solution involving the compare turns out to be faster, so
10412 that's what we use unless the branch won't reach. */
10413
10414 #define ALWAYS_USE_FAKE_DEP 0
10415 #define ALWAYS_EMIT_R2SAVE 0
10416
10417 #define PPC_LO(v) ((v) & 0xffff)
10418 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10419 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10420
10421 static inline unsigned int
10422 plt_stub_size (struct ppc_link_hash_table *htab,
10423 struct ppc_stub_hash_entry *stub_entry,
10424 bfd_vma off)
10425 {
10426 unsigned size = 12;
10427
10428 if (ALWAYS_EMIT_R2SAVE
10429 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10430 size += 4;
10431 if (PPC_HA (off) != 0)
10432 size += 4;
10433 if (htab->opd_abi)
10434 {
10435 size += 4;
10436 if (htab->params->plt_static_chain)
10437 size += 4;
10438 if (htab->params->plt_thread_safe
10439 && htab->elf.dynamic_sections_created
10440 && stub_entry->h != NULL
10441 && stub_entry->h->elf.dynindx != -1)
10442 size += 8;
10443 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10444 size += 4;
10445 }
10446 if (stub_entry->h != NULL
10447 && (stub_entry->h == htab->tls_get_addr_fd
10448 || stub_entry->h == htab->tls_get_addr)
10449 && htab->params->tls_get_addr_opt)
10450 {
10451 size += 7 * 4;
10452 if (ALWAYS_EMIT_R2SAVE
10453 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10454 size += 6 * 4;
10455 }
10456 return size;
10457 }
10458
10459 /* Depending on the sign of plt_stub_align:
10460 If positive, return the padding to align to a 2**plt_stub_align
10461 boundary.
10462 If negative, if this stub would cross fewer 2**plt_stub_align
10463 boundaries if we align, then return the padding needed to do so. */
10464
10465 static inline unsigned int
10466 plt_stub_pad (struct ppc_link_hash_table *htab,
10467 struct ppc_stub_hash_entry *stub_entry,
10468 bfd_vma plt_off)
10469 {
10470 int stub_align;
10471 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10472 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10473
10474 if (htab->params->plt_stub_align >= 0)
10475 {
10476 stub_align = 1 << htab->params->plt_stub_align;
10477 if ((stub_off & (stub_align - 1)) != 0)
10478 return stub_align - (stub_off & (stub_align - 1));
10479 return 0;
10480 }
10481
10482 stub_align = 1 << -htab->params->plt_stub_align;
10483 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10484 > ((stub_size - 1) & -stub_align))
10485 return stub_align - (stub_off & (stub_align - 1));
10486 return 0;
10487 }
10488
10489 /* Build a .plt call stub. */
10490
10491 static inline bfd_byte *
10492 build_plt_stub (struct ppc_link_hash_table *htab,
10493 struct ppc_stub_hash_entry *stub_entry,
10494 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10495 {
10496 bfd *obfd = htab->params->stub_bfd;
10497 bfd_boolean plt_load_toc = htab->opd_abi;
10498 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10499 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10500 && htab->elf.dynamic_sections_created
10501 && stub_entry->h != NULL
10502 && stub_entry->h->elf.dynindx != -1);
10503 bfd_boolean use_fake_dep = plt_thread_safe;
10504 bfd_vma cmp_branch_off = 0;
10505
10506 if (!ALWAYS_USE_FAKE_DEP
10507 && plt_load_toc
10508 && plt_thread_safe
10509 && !((stub_entry->h == htab->tls_get_addr_fd
10510 || stub_entry->h == htab->tls_get_addr)
10511 && htab->params->tls_get_addr_opt))
10512 {
10513 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10514 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10515 / PLT_ENTRY_SIZE (htab));
10516 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10517 bfd_vma to, from;
10518
10519 if (pltindex > 32768)
10520 glinkoff += (pltindex - 32768) * 4;
10521 to = (glinkoff
10522 + htab->glink->output_offset
10523 + htab->glink->output_section->vma);
10524 from = (p - stub_entry->group->stub_sec->contents
10525 + 4 * (ALWAYS_EMIT_R2SAVE
10526 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10527 + 4 * (PPC_HA (offset) != 0)
10528 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10529 != PPC_HA (offset))
10530 + 4 * (plt_static_chain != 0)
10531 + 20
10532 + stub_entry->group->stub_sec->output_offset
10533 + stub_entry->group->stub_sec->output_section->vma);
10534 cmp_branch_off = to - from;
10535 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10536 }
10537
10538 if (PPC_HA (offset) != 0)
10539 {
10540 if (r != NULL)
10541 {
10542 if (ALWAYS_EMIT_R2SAVE
10543 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10544 r[0].r_offset += 4;
10545 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10546 r[1].r_offset = r[0].r_offset + 4;
10547 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10548 r[1].r_addend = r[0].r_addend;
10549 if (plt_load_toc)
10550 {
10551 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10552 {
10553 r[2].r_offset = r[1].r_offset + 4;
10554 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10555 r[2].r_addend = r[0].r_addend;
10556 }
10557 else
10558 {
10559 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10560 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10561 r[2].r_addend = r[0].r_addend + 8;
10562 if (plt_static_chain)
10563 {
10564 r[3].r_offset = r[2].r_offset + 4;
10565 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10566 r[3].r_addend = r[0].r_addend + 16;
10567 }
10568 }
10569 }
10570 }
10571 if (ALWAYS_EMIT_R2SAVE
10572 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10573 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10574 if (plt_load_toc)
10575 {
10576 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10577 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10578 }
10579 else
10580 {
10581 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10582 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10583 }
10584 if (plt_load_toc
10585 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10586 {
10587 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10588 offset = 0;
10589 }
10590 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10591 if (plt_load_toc)
10592 {
10593 if (use_fake_dep)
10594 {
10595 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10596 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10597 }
10598 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10599 if (plt_static_chain)
10600 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10601 }
10602 }
10603 else
10604 {
10605 if (r != NULL)
10606 {
10607 if (ALWAYS_EMIT_R2SAVE
10608 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10609 r[0].r_offset += 4;
10610 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10611 if (plt_load_toc)
10612 {
10613 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10614 {
10615 r[1].r_offset = r[0].r_offset + 4;
10616 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10617 r[1].r_addend = r[0].r_addend;
10618 }
10619 else
10620 {
10621 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10622 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10623 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10624 if (plt_static_chain)
10625 {
10626 r[2].r_offset = r[1].r_offset + 4;
10627 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10628 r[2].r_addend = r[0].r_addend + 8;
10629 }
10630 }
10631 }
10632 }
10633 if (ALWAYS_EMIT_R2SAVE
10634 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10635 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10636 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10637 if (plt_load_toc
10638 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10639 {
10640 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10641 offset = 0;
10642 }
10643 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10644 if (plt_load_toc)
10645 {
10646 if (use_fake_dep)
10647 {
10648 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10649 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10650 }
10651 if (plt_static_chain)
10652 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10653 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10654 }
10655 }
10656 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10657 {
10658 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10659 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10660 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10661 }
10662 else
10663 bfd_put_32 (obfd, BCTR, p), p += 4;
10664 return p;
10665 }
10666
10667 /* Build a special .plt call stub for __tls_get_addr. */
10668
10669 #define LD_R11_0R3 0xe9630000
10670 #define LD_R12_0R3 0xe9830000
10671 #define MR_R0_R3 0x7c601b78
10672 #define CMPDI_R11_0 0x2c2b0000
10673 #define ADD_R3_R12_R13 0x7c6c6a14
10674 #define BEQLR 0x4d820020
10675 #define MR_R3_R0 0x7c030378
10676 #define STD_R11_0R1 0xf9610000
10677 #define BCTRL 0x4e800421
10678 #define LD_R11_0R1 0xe9610000
10679 #define MTLR_R11 0x7d6803a6
10680
10681 static inline bfd_byte *
10682 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10683 struct ppc_stub_hash_entry *stub_entry,
10684 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10685 {
10686 bfd *obfd = htab->params->stub_bfd;
10687
10688 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10689 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10690 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10691 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10692 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10693 bfd_put_32 (obfd, BEQLR, p), p += 4;
10694 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10695 if (r != NULL)
10696 r[0].r_offset += 7 * 4;
10697 if (!ALWAYS_EMIT_R2SAVE
10698 && stub_entry->stub_type != ppc_stub_plt_call_r2save)
10699 return build_plt_stub (htab, stub_entry, p, offset, r);
10700
10701 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10702 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10703
10704 if (r != NULL)
10705 r[0].r_offset += 2 * 4;
10706 p = build_plt_stub (htab, stub_entry, p, offset, r);
10707 bfd_put_32 (obfd, BCTRL, p - 4);
10708
10709 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10710 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10711 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10712 bfd_put_32 (obfd, BLR, p), p += 4;
10713
10714 return p;
10715 }
10716
10717 static Elf_Internal_Rela *
10718 get_relocs (asection *sec, int count)
10719 {
10720 Elf_Internal_Rela *relocs;
10721 struct bfd_elf_section_data *elfsec_data;
10722
10723 elfsec_data = elf_section_data (sec);
10724 relocs = elfsec_data->relocs;
10725 if (relocs == NULL)
10726 {
10727 bfd_size_type relsize;
10728 relsize = sec->reloc_count * sizeof (*relocs);
10729 relocs = bfd_alloc (sec->owner, relsize);
10730 if (relocs == NULL)
10731 return NULL;
10732 elfsec_data->relocs = relocs;
10733 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10734 sizeof (Elf_Internal_Shdr));
10735 if (elfsec_data->rela.hdr == NULL)
10736 return NULL;
10737 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10738 * sizeof (Elf64_External_Rela));
10739 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10740 sec->reloc_count = 0;
10741 }
10742 relocs += sec->reloc_count;
10743 sec->reloc_count += count;
10744 return relocs;
10745 }
10746
10747 static bfd_vma
10748 get_r2off (struct bfd_link_info *info,
10749 struct ppc_stub_hash_entry *stub_entry)
10750 {
10751 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10752 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10753
10754 if (r2off == 0)
10755 {
10756 /* Support linking -R objects. Get the toc pointer from the
10757 opd entry. */
10758 char buf[8];
10759 if (!htab->opd_abi)
10760 return r2off;
10761 asection *opd = stub_entry->h->elf.root.u.def.section;
10762 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10763
10764 if (strcmp (opd->name, ".opd") != 0
10765 || opd->reloc_count != 0)
10766 {
10767 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10768 stub_entry->h->elf.root.root.string);
10769 bfd_set_error (bfd_error_bad_value);
10770 return (bfd_vma) -1;
10771 }
10772 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10773 return (bfd_vma) -1;
10774 r2off = bfd_get_64 (opd->owner, buf);
10775 r2off -= elf_gp (info->output_bfd);
10776 }
10777 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10778 return r2off;
10779 }
10780
10781 static bfd_boolean
10782 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10783 {
10784 struct ppc_stub_hash_entry *stub_entry;
10785 struct ppc_branch_hash_entry *br_entry;
10786 struct bfd_link_info *info;
10787 struct ppc_link_hash_table *htab;
10788 bfd_byte *loc;
10789 bfd_byte *p;
10790 bfd_vma dest, off;
10791 int size;
10792 Elf_Internal_Rela *r;
10793 asection *plt;
10794
10795 /* Massage our args to the form they really have. */
10796 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10797 info = in_arg;
10798
10799 htab = ppc_hash_table (info);
10800 if (htab == NULL)
10801 return FALSE;
10802
10803 /* Make a note of the offset within the stubs for this entry. */
10804 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10805 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10806
10807 htab->stub_count[stub_entry->stub_type - 1] += 1;
10808 switch (stub_entry->stub_type)
10809 {
10810 case ppc_stub_long_branch:
10811 case ppc_stub_long_branch_r2off:
10812 /* Branches are relative. This is where we are going to. */
10813 dest = (stub_entry->target_value
10814 + stub_entry->target_section->output_offset
10815 + stub_entry->target_section->output_section->vma);
10816 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10817 off = dest;
10818
10819 /* And this is where we are coming from. */
10820 off -= (stub_entry->stub_offset
10821 + stub_entry->group->stub_sec->output_offset
10822 + stub_entry->group->stub_sec->output_section->vma);
10823
10824 size = 4;
10825 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10826 {
10827 bfd_vma r2off = get_r2off (info, stub_entry);
10828
10829 if (r2off == (bfd_vma) -1)
10830 {
10831 htab->stub_error = TRUE;
10832 return FALSE;
10833 }
10834 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10835 loc += 4;
10836 size = 8;
10837 if (PPC_HA (r2off) != 0)
10838 {
10839 bfd_put_32 (htab->params->stub_bfd,
10840 ADDIS_R2_R2 | PPC_HA (r2off), loc);
10841 loc += 4;
10842 size += 4;
10843 }
10844 if (PPC_LO (r2off) != 0)
10845 {
10846 bfd_put_32 (htab->params->stub_bfd,
10847 ADDI_R2_R2 | PPC_LO (r2off), loc);
10848 loc += 4;
10849 size += 4;
10850 }
10851 off -= size - 4;
10852 }
10853 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10854
10855 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10856 {
10857 info->callbacks->einfo
10858 (_("%P: long branch stub `%s' offset overflow\n"),
10859 stub_entry->root.string);
10860 htab->stub_error = TRUE;
10861 return FALSE;
10862 }
10863
10864 if (info->emitrelocations)
10865 {
10866 r = get_relocs (stub_entry->group->stub_sec, 1);
10867 if (r == NULL)
10868 return FALSE;
10869 r->r_offset = loc - stub_entry->group->stub_sec->contents;
10870 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10871 r->r_addend = dest;
10872 if (stub_entry->h != NULL)
10873 {
10874 struct elf_link_hash_entry **hashes;
10875 unsigned long symndx;
10876 struct ppc_link_hash_entry *h;
10877
10878 hashes = elf_sym_hashes (htab->params->stub_bfd);
10879 if (hashes == NULL)
10880 {
10881 bfd_size_type hsize;
10882
10883 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10884 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10885 if (hashes == NULL)
10886 return FALSE;
10887 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10888 htab->stub_globals = 1;
10889 }
10890 symndx = htab->stub_globals++;
10891 h = stub_entry->h;
10892 hashes[symndx] = &h->elf;
10893 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10894 if (h->oh != NULL && h->oh->is_func)
10895 h = ppc_follow_link (h->oh);
10896 if (h->elf.root.u.def.section != stub_entry->target_section)
10897 /* H is an opd symbol. The addend must be zero. */
10898 r->r_addend = 0;
10899 else
10900 {
10901 off = (h->elf.root.u.def.value
10902 + h->elf.root.u.def.section->output_offset
10903 + h->elf.root.u.def.section->output_section->vma);
10904 r->r_addend -= off;
10905 }
10906 }
10907 }
10908 break;
10909
10910 case ppc_stub_plt_branch:
10911 case ppc_stub_plt_branch_r2off:
10912 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10913 stub_entry->root.string + 9,
10914 FALSE, FALSE);
10915 if (br_entry == NULL)
10916 {
10917 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10918 stub_entry->root.string);
10919 htab->stub_error = TRUE;
10920 return FALSE;
10921 }
10922
10923 dest = (stub_entry->target_value
10924 + stub_entry->target_section->output_offset
10925 + stub_entry->target_section->output_section->vma);
10926 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10927 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10928
10929 bfd_put_64 (htab->brlt->owner, dest,
10930 htab->brlt->contents + br_entry->offset);
10931
10932 if (br_entry->iter == htab->stub_iteration)
10933 {
10934 br_entry->iter = 0;
10935
10936 if (htab->relbrlt != NULL)
10937 {
10938 /* Create a reloc for the branch lookup table entry. */
10939 Elf_Internal_Rela rela;
10940 bfd_byte *rl;
10941
10942 rela.r_offset = (br_entry->offset
10943 + htab->brlt->output_offset
10944 + htab->brlt->output_section->vma);
10945 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10946 rela.r_addend = dest;
10947
10948 rl = htab->relbrlt->contents;
10949 rl += (htab->relbrlt->reloc_count++
10950 * sizeof (Elf64_External_Rela));
10951 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10952 }
10953 else if (info->emitrelocations)
10954 {
10955 r = get_relocs (htab->brlt, 1);
10956 if (r == NULL)
10957 return FALSE;
10958 /* brlt, being SEC_LINKER_CREATED does not go through the
10959 normal reloc processing. Symbols and offsets are not
10960 translated from input file to output file form, so
10961 set up the offset per the output file. */
10962 r->r_offset = (br_entry->offset
10963 + htab->brlt->output_offset
10964 + htab->brlt->output_section->vma);
10965 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10966 r->r_addend = dest;
10967 }
10968 }
10969
10970 dest = (br_entry->offset
10971 + htab->brlt->output_offset
10972 + htab->brlt->output_section->vma);
10973
10974 off = (dest
10975 - elf_gp (htab->brlt->output_section->owner)
10976 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10977
10978 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10979 {
10980 info->callbacks->einfo
10981 (_("%P: linkage table error against `%T'\n"),
10982 stub_entry->root.string);
10983 bfd_set_error (bfd_error_bad_value);
10984 htab->stub_error = TRUE;
10985 return FALSE;
10986 }
10987
10988 if (info->emitrelocations)
10989 {
10990 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
10991 if (r == NULL)
10992 return FALSE;
10993 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10994 if (bfd_big_endian (info->output_bfd))
10995 r[0].r_offset += 2;
10996 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10997 r[0].r_offset += 4;
10998 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10999 r[0].r_addend = dest;
11000 if (PPC_HA (off) != 0)
11001 {
11002 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11003 r[1].r_offset = r[0].r_offset + 4;
11004 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11005 r[1].r_addend = r[0].r_addend;
11006 }
11007 }
11008
11009 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11010 {
11011 if (PPC_HA (off) != 0)
11012 {
11013 size = 16;
11014 bfd_put_32 (htab->params->stub_bfd,
11015 ADDIS_R12_R2 | PPC_HA (off), loc);
11016 loc += 4;
11017 bfd_put_32 (htab->params->stub_bfd,
11018 LD_R12_0R12 | PPC_LO (off), loc);
11019 }
11020 else
11021 {
11022 size = 12;
11023 bfd_put_32 (htab->params->stub_bfd,
11024 LD_R12_0R2 | PPC_LO (off), loc);
11025 }
11026 }
11027 else
11028 {
11029 bfd_vma r2off = get_r2off (info, stub_entry);
11030
11031 if (r2off == (bfd_vma) -1)
11032 {
11033 htab->stub_error = TRUE;
11034 return FALSE;
11035 }
11036
11037 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
11038 loc += 4;
11039 size = 16;
11040 if (PPC_HA (off) != 0)
11041 {
11042 size += 4;
11043 bfd_put_32 (htab->params->stub_bfd,
11044 ADDIS_R12_R2 | PPC_HA (off), loc);
11045 loc += 4;
11046 bfd_put_32 (htab->params->stub_bfd,
11047 LD_R12_0R12 | PPC_LO (off), loc);
11048 }
11049 else
11050 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
11051
11052 if (PPC_HA (r2off) != 0)
11053 {
11054 size += 4;
11055 loc += 4;
11056 bfd_put_32 (htab->params->stub_bfd,
11057 ADDIS_R2_R2 | PPC_HA (r2off), loc);
11058 }
11059 if (PPC_LO (r2off) != 0)
11060 {
11061 size += 4;
11062 loc += 4;
11063 bfd_put_32 (htab->params->stub_bfd,
11064 ADDI_R2_R2 | PPC_LO (r2off), loc);
11065 }
11066 }
11067 loc += 4;
11068 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
11069 loc += 4;
11070 bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
11071 break;
11072
11073 case ppc_stub_plt_call:
11074 case ppc_stub_plt_call_r2save:
11075 if (stub_entry->h != NULL
11076 && stub_entry->h->is_func_descriptor
11077 && stub_entry->h->oh != NULL)
11078 {
11079 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11080
11081 /* If the old-ABI "dot-symbol" is undefined make it weak so
11082 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11083 if (fh->elf.root.type == bfd_link_hash_undefined
11084 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11085 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11086 fh->elf.root.type = bfd_link_hash_undefweak;
11087 }
11088
11089 /* Now build the stub. */
11090 dest = stub_entry->plt_ent->plt.offset & ~1;
11091 if (dest >= (bfd_vma) -2)
11092 abort ();
11093
11094 plt = htab->elf.splt;
11095 if (!htab->elf.dynamic_sections_created
11096 || stub_entry->h == NULL
11097 || stub_entry->h->elf.dynindx == -1)
11098 plt = htab->elf.iplt;
11099
11100 dest += plt->output_offset + plt->output_section->vma;
11101
11102 if (stub_entry->h == NULL
11103 && (stub_entry->plt_ent->plt.offset & 1) == 0)
11104 {
11105 Elf_Internal_Rela rela;
11106 bfd_byte *rl;
11107
11108 rela.r_offset = dest;
11109 if (htab->opd_abi)
11110 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11111 else
11112 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
11113 rela.r_addend = (stub_entry->target_value
11114 + stub_entry->target_section->output_offset
11115 + stub_entry->target_section->output_section->vma);
11116
11117 rl = (htab->elf.irelplt->contents
11118 + (htab->elf.irelplt->reloc_count++
11119 * sizeof (Elf64_External_Rela)));
11120 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
11121 stub_entry->plt_ent->plt.offset |= 1;
11122 htab->local_ifunc_resolver = 1;
11123 }
11124
11125 off = (dest
11126 - elf_gp (plt->output_section->owner)
11127 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11128
11129 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11130 {
11131 info->callbacks->einfo
11132 /* xgettext:c-format */
11133 (_("%P: linkage table error against `%T'\n"),
11134 stub_entry->h != NULL
11135 ? stub_entry->h->elf.root.root.string
11136 : "<local sym>");
11137 bfd_set_error (bfd_error_bad_value);
11138 htab->stub_error = TRUE;
11139 return FALSE;
11140 }
11141
11142 if (htab->params->plt_stub_align != 0)
11143 {
11144 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11145
11146 stub_entry->group->stub_sec->size += pad;
11147 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11148 loc += pad;
11149 }
11150
11151 r = NULL;
11152 if (info->emitrelocations)
11153 {
11154 r = get_relocs (stub_entry->group->stub_sec,
11155 ((PPC_HA (off) != 0)
11156 + (htab->opd_abi
11157 ? 2 + (htab->params->plt_static_chain
11158 && PPC_HA (off + 16) == PPC_HA (off))
11159 : 1)));
11160 if (r == NULL)
11161 return FALSE;
11162 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11163 if (bfd_big_endian (info->output_bfd))
11164 r[0].r_offset += 2;
11165 r[0].r_addend = dest;
11166 }
11167 if (stub_entry->h != NULL
11168 && (stub_entry->h == htab->tls_get_addr_fd
11169 || stub_entry->h == htab->tls_get_addr)
11170 && htab->params->tls_get_addr_opt)
11171 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11172 else
11173 p = build_plt_stub (htab, stub_entry, loc, off, r);
11174 size = p - loc;
11175 break;
11176
11177 case ppc_stub_save_res:
11178 return TRUE;
11179
11180 default:
11181 BFD_FAIL ();
11182 return FALSE;
11183 }
11184
11185 stub_entry->group->stub_sec->size += size;
11186
11187 if (htab->params->emit_stub_syms)
11188 {
11189 struct elf_link_hash_entry *h;
11190 size_t len1, len2;
11191 char *name;
11192 const char *const stub_str[] = { "long_branch",
11193 "long_branch_r2off",
11194 "plt_branch",
11195 "plt_branch_r2off",
11196 "plt_call",
11197 "plt_call" };
11198
11199 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11200 len2 = strlen (stub_entry->root.string);
11201 name = bfd_malloc (len1 + len2 + 2);
11202 if (name == NULL)
11203 return FALSE;
11204 memcpy (name, stub_entry->root.string, 9);
11205 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11206 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11207 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11208 if (h == NULL)
11209 return FALSE;
11210 if (h->root.type == bfd_link_hash_new)
11211 {
11212 h->root.type = bfd_link_hash_defined;
11213 h->root.u.def.section = stub_entry->group->stub_sec;
11214 h->root.u.def.value = stub_entry->stub_offset;
11215 h->ref_regular = 1;
11216 h->def_regular = 1;
11217 h->ref_regular_nonweak = 1;
11218 h->forced_local = 1;
11219 h->non_elf = 0;
11220 h->root.linker_def = 1;
11221 }
11222 }
11223
11224 return TRUE;
11225 }
11226
11227 /* As above, but don't actually build the stub. Just bump offset so
11228 we know stub section sizes, and select plt_branch stubs where
11229 long_branch stubs won't do. */
11230
11231 static bfd_boolean
11232 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11233 {
11234 struct ppc_stub_hash_entry *stub_entry;
11235 struct bfd_link_info *info;
11236 struct ppc_link_hash_table *htab;
11237 bfd_vma off;
11238 int size;
11239
11240 /* Massage our args to the form they really have. */
11241 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11242 info = in_arg;
11243
11244 htab = ppc_hash_table (info);
11245 if (htab == NULL)
11246 return FALSE;
11247
11248 if (stub_entry->h != NULL
11249 && stub_entry->h->save_res
11250 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11251 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11252 {
11253 /* Don't make stubs to out-of-line register save/restore
11254 functions. Instead, emit copies of the functions. */
11255 stub_entry->group->needs_save_res = 1;
11256 stub_entry->stub_type = ppc_stub_save_res;
11257 return TRUE;
11258 }
11259
11260 if (stub_entry->stub_type == ppc_stub_plt_call
11261 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11262 {
11263 asection *plt;
11264 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11265 if (off >= (bfd_vma) -2)
11266 abort ();
11267 plt = htab->elf.splt;
11268 if (!htab->elf.dynamic_sections_created
11269 || stub_entry->h == NULL
11270 || stub_entry->h->elf.dynindx == -1)
11271 plt = htab->elf.iplt;
11272 off += (plt->output_offset
11273 + plt->output_section->vma
11274 - elf_gp (plt->output_section->owner)
11275 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11276
11277 size = plt_stub_size (htab, stub_entry, off);
11278 if (stub_entry->h != NULL
11279 && (stub_entry->h == htab->tls_get_addr_fd
11280 || stub_entry->h == htab->tls_get_addr)
11281 && htab->params->tls_get_addr_opt
11282 && (ALWAYS_EMIT_R2SAVE
11283 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11284 stub_entry->group->tls_get_addr_opt_bctrl
11285 = stub_entry->group->stub_sec->size + size - 5 * 4;
11286
11287 if (htab->params->plt_stub_align)
11288 size += plt_stub_pad (htab, stub_entry, off);
11289 if (info->emitrelocations)
11290 {
11291 stub_entry->group->stub_sec->reloc_count
11292 += ((PPC_HA (off) != 0)
11293 + (htab->opd_abi
11294 ? 2 + (htab->params->plt_static_chain
11295 && PPC_HA (off + 16) == PPC_HA (off))
11296 : 1));
11297 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11298 }
11299 }
11300 else
11301 {
11302 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11303 variants. */
11304 bfd_vma r2off = 0;
11305 bfd_vma local_off = 0;
11306
11307 off = (stub_entry->target_value
11308 + stub_entry->target_section->output_offset
11309 + stub_entry->target_section->output_section->vma);
11310 off -= (stub_entry->group->stub_sec->size
11311 + stub_entry->group->stub_sec->output_offset
11312 + stub_entry->group->stub_sec->output_section->vma);
11313
11314 /* Reset the stub type from the plt variant in case we now
11315 can reach with a shorter stub. */
11316 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11317 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11318
11319 size = 4;
11320 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11321 {
11322 r2off = get_r2off (info, stub_entry);
11323 if (r2off == (bfd_vma) -1)
11324 {
11325 htab->stub_error = TRUE;
11326 return FALSE;
11327 }
11328 size = 8;
11329 if (PPC_HA (r2off) != 0)
11330 size += 4;
11331 if (PPC_LO (r2off) != 0)
11332 size += 4;
11333 off -= size - 4;
11334 }
11335
11336 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11337
11338 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11339 Do the same for -R objects without function descriptors. */
11340 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11341 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11342 && r2off == 0
11343 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11344 {
11345 struct ppc_branch_hash_entry *br_entry;
11346
11347 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11348 stub_entry->root.string + 9,
11349 TRUE, FALSE);
11350 if (br_entry == NULL)
11351 {
11352 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
11353 stub_entry->root.string);
11354 htab->stub_error = TRUE;
11355 return FALSE;
11356 }
11357
11358 if (br_entry->iter != htab->stub_iteration)
11359 {
11360 br_entry->iter = htab->stub_iteration;
11361 br_entry->offset = htab->brlt->size;
11362 htab->brlt->size += 8;
11363
11364 if (htab->relbrlt != NULL)
11365 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11366 else if (info->emitrelocations)
11367 {
11368 htab->brlt->reloc_count += 1;
11369 htab->brlt->flags |= SEC_RELOC;
11370 }
11371 }
11372
11373 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11374 off = (br_entry->offset
11375 + htab->brlt->output_offset
11376 + htab->brlt->output_section->vma
11377 - elf_gp (htab->brlt->output_section->owner)
11378 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11379
11380 if (info->emitrelocations)
11381 {
11382 stub_entry->group->stub_sec->reloc_count
11383 += 1 + (PPC_HA (off) != 0);
11384 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11385 }
11386
11387 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11388 {
11389 size = 12;
11390 if (PPC_HA (off) != 0)
11391 size = 16;
11392 }
11393 else
11394 {
11395 size = 16;
11396 if (PPC_HA (off) != 0)
11397 size += 4;
11398
11399 if (PPC_HA (r2off) != 0)
11400 size += 4;
11401 if (PPC_LO (r2off) != 0)
11402 size += 4;
11403 }
11404 }
11405 else if (info->emitrelocations)
11406 {
11407 stub_entry->group->stub_sec->reloc_count += 1;
11408 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11409 }
11410 }
11411
11412 stub_entry->group->stub_sec->size += size;
11413 return TRUE;
11414 }
11415
11416 /* Set up various things so that we can make a list of input sections
11417 for each output section included in the link. Returns -1 on error,
11418 0 when no stubs will be needed, and 1 on success. */
11419
11420 int
11421 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11422 {
11423 unsigned int id;
11424 bfd_size_type amt;
11425 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11426
11427 if (htab == NULL)
11428 return -1;
11429
11430 htab->sec_info_arr_size = bfd_get_next_section_id ();
11431 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11432 htab->sec_info = bfd_zmalloc (amt);
11433 if (htab->sec_info == NULL)
11434 return -1;
11435
11436 /* Set toc_off for com, und, abs and ind sections. */
11437 for (id = 0; id < 3; id++)
11438 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11439
11440 return 1;
11441 }
11442
11443 /* Set up for first pass at multitoc partitioning. */
11444
11445 void
11446 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11447 {
11448 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11449
11450 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11451 htab->toc_bfd = NULL;
11452 htab->toc_first_sec = NULL;
11453 }
11454
11455 /* The linker repeatedly calls this function for each TOC input section
11456 and linker generated GOT section. Group input bfds such that the toc
11457 within a group is less than 64k in size. */
11458
11459 bfd_boolean
11460 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11461 {
11462 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11463 bfd_vma addr, off, limit;
11464
11465 if (htab == NULL)
11466 return FALSE;
11467
11468 if (!htab->second_toc_pass)
11469 {
11470 /* Keep track of the first .toc or .got section for this input bfd. */
11471 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11472
11473 if (new_bfd)
11474 {
11475 htab->toc_bfd = isec->owner;
11476 htab->toc_first_sec = isec;
11477 }
11478
11479 addr = isec->output_offset + isec->output_section->vma;
11480 off = addr - htab->toc_curr;
11481 limit = 0x80008000;
11482 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11483 limit = 0x10000;
11484 if (off + isec->size > limit)
11485 {
11486 addr = (htab->toc_first_sec->output_offset
11487 + htab->toc_first_sec->output_section->vma);
11488 htab->toc_curr = addr;
11489 htab->toc_curr &= -TOC_BASE_ALIGN;
11490 }
11491
11492 /* toc_curr is the base address of this toc group. Set elf_gp
11493 for the input section to be the offset relative to the
11494 output toc base plus 0x8000. Making the input elf_gp an
11495 offset allows us to move the toc as a whole without
11496 recalculating input elf_gp. */
11497 off = htab->toc_curr - elf_gp (isec->output_section->owner);
11498 off += TOC_BASE_OFF;
11499
11500 /* Die if someone uses a linker script that doesn't keep input
11501 file .toc and .got together. */
11502 if (new_bfd
11503 && elf_gp (isec->owner) != 0
11504 && elf_gp (isec->owner) != off)
11505 return FALSE;
11506
11507 elf_gp (isec->owner) = off;
11508 return TRUE;
11509 }
11510
11511 /* During the second pass toc_first_sec points to the start of
11512 a toc group, and toc_curr is used to track the old elf_gp.
11513 We use toc_bfd to ensure we only look at each bfd once. */
11514 if (htab->toc_bfd == isec->owner)
11515 return TRUE;
11516 htab->toc_bfd = isec->owner;
11517
11518 if (htab->toc_first_sec == NULL
11519 || htab->toc_curr != elf_gp (isec->owner))
11520 {
11521 htab->toc_curr = elf_gp (isec->owner);
11522 htab->toc_first_sec = isec;
11523 }
11524 addr = (htab->toc_first_sec->output_offset
11525 + htab->toc_first_sec->output_section->vma);
11526 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11527 elf_gp (isec->owner) = off;
11528
11529 return TRUE;
11530 }
11531
11532 /* Called via elf_link_hash_traverse to merge GOT entries for global
11533 symbol H. */
11534
11535 static bfd_boolean
11536 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11537 {
11538 if (h->root.type == bfd_link_hash_indirect)
11539 return TRUE;
11540
11541 merge_got_entries (&h->got.glist);
11542
11543 return TRUE;
11544 }
11545
11546 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11547 symbol H. */
11548
11549 static bfd_boolean
11550 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11551 {
11552 struct got_entry *gent;
11553
11554 if (h->root.type == bfd_link_hash_indirect)
11555 return TRUE;
11556
11557 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11558 if (!gent->is_indirect)
11559 allocate_got (h, (struct bfd_link_info *) inf, gent);
11560 return TRUE;
11561 }
11562
11563 /* Called on the first multitoc pass after the last call to
11564 ppc64_elf_next_toc_section. This function removes duplicate GOT
11565 entries. */
11566
11567 bfd_boolean
11568 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11569 {
11570 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11571 struct bfd *ibfd, *ibfd2;
11572 bfd_boolean done_something;
11573
11574 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11575
11576 if (!htab->do_multi_toc)
11577 return FALSE;
11578
11579 /* Merge global sym got entries within a toc group. */
11580 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11581
11582 /* And tlsld_got. */
11583 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11584 {
11585 struct got_entry *ent, *ent2;
11586
11587 if (!is_ppc64_elf (ibfd))
11588 continue;
11589
11590 ent = ppc64_tlsld_got (ibfd);
11591 if (!ent->is_indirect
11592 && ent->got.offset != (bfd_vma) -1)
11593 {
11594 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11595 {
11596 if (!is_ppc64_elf (ibfd2))
11597 continue;
11598
11599 ent2 = ppc64_tlsld_got (ibfd2);
11600 if (!ent2->is_indirect
11601 && ent2->got.offset != (bfd_vma) -1
11602 && elf_gp (ibfd2) == elf_gp (ibfd))
11603 {
11604 ent2->is_indirect = TRUE;
11605 ent2->got.ent = ent;
11606 }
11607 }
11608 }
11609 }
11610
11611 /* Zap sizes of got sections. */
11612 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11613 htab->elf.irelplt->size -= htab->got_reli_size;
11614 htab->got_reli_size = 0;
11615
11616 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11617 {
11618 asection *got, *relgot;
11619
11620 if (!is_ppc64_elf (ibfd))
11621 continue;
11622
11623 got = ppc64_elf_tdata (ibfd)->got;
11624 if (got != NULL)
11625 {
11626 got->rawsize = got->size;
11627 got->size = 0;
11628 relgot = ppc64_elf_tdata (ibfd)->relgot;
11629 relgot->rawsize = relgot->size;
11630 relgot->size = 0;
11631 }
11632 }
11633
11634 /* Now reallocate the got, local syms first. We don't need to
11635 allocate section contents again since we never increase size. */
11636 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11637 {
11638 struct got_entry **lgot_ents;
11639 struct got_entry **end_lgot_ents;
11640 struct plt_entry **local_plt;
11641 struct plt_entry **end_local_plt;
11642 unsigned char *lgot_masks;
11643 bfd_size_type locsymcount;
11644 Elf_Internal_Shdr *symtab_hdr;
11645 asection *s;
11646
11647 if (!is_ppc64_elf (ibfd))
11648 continue;
11649
11650 lgot_ents = elf_local_got_ents (ibfd);
11651 if (!lgot_ents)
11652 continue;
11653
11654 symtab_hdr = &elf_symtab_hdr (ibfd);
11655 locsymcount = symtab_hdr->sh_info;
11656 end_lgot_ents = lgot_ents + locsymcount;
11657 local_plt = (struct plt_entry **) end_lgot_ents;
11658 end_local_plt = local_plt + locsymcount;
11659 lgot_masks = (unsigned char *) end_local_plt;
11660 s = ppc64_elf_tdata (ibfd)->got;
11661 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11662 {
11663 struct got_entry *ent;
11664
11665 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11666 {
11667 unsigned int ent_size = 8;
11668 unsigned int rel_size = sizeof (Elf64_External_Rela);
11669
11670 ent->got.offset = s->size;
11671 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11672 {
11673 ent_size *= 2;
11674 rel_size *= 2;
11675 }
11676 s->size += ent_size;
11677 if ((*lgot_masks & PLT_IFUNC) != 0)
11678 {
11679 htab->elf.irelplt->size += rel_size;
11680 htab->got_reli_size += rel_size;
11681 }
11682 else if (bfd_link_pic (info))
11683 {
11684 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11685 srel->size += rel_size;
11686 }
11687 }
11688 }
11689 }
11690
11691 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11692
11693 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11694 {
11695 struct got_entry *ent;
11696
11697 if (!is_ppc64_elf (ibfd))
11698 continue;
11699
11700 ent = ppc64_tlsld_got (ibfd);
11701 if (!ent->is_indirect
11702 && ent->got.offset != (bfd_vma) -1)
11703 {
11704 asection *s = ppc64_elf_tdata (ibfd)->got;
11705 ent->got.offset = s->size;
11706 s->size += 16;
11707 if (bfd_link_pic (info))
11708 {
11709 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11710 srel->size += sizeof (Elf64_External_Rela);
11711 }
11712 }
11713 }
11714
11715 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11716 if (!done_something)
11717 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11718 {
11719 asection *got;
11720
11721 if (!is_ppc64_elf (ibfd))
11722 continue;
11723
11724 got = ppc64_elf_tdata (ibfd)->got;
11725 if (got != NULL)
11726 {
11727 done_something = got->rawsize != got->size;
11728 if (done_something)
11729 break;
11730 }
11731 }
11732
11733 if (done_something)
11734 (*htab->params->layout_sections_again) ();
11735
11736 /* Set up for second pass over toc sections to recalculate elf_gp
11737 on input sections. */
11738 htab->toc_bfd = NULL;
11739 htab->toc_first_sec = NULL;
11740 htab->second_toc_pass = TRUE;
11741 return done_something;
11742 }
11743
11744 /* Called after second pass of multitoc partitioning. */
11745
11746 void
11747 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11748 {
11749 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11750
11751 /* After the second pass, toc_curr tracks the TOC offset used
11752 for code sections below in ppc64_elf_next_input_section. */
11753 htab->toc_curr = TOC_BASE_OFF;
11754 }
11755
11756 /* No toc references were found in ISEC. If the code in ISEC makes no
11757 calls, then there's no need to use toc adjusting stubs when branching
11758 into ISEC. Actually, indirect calls from ISEC are OK as they will
11759 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11760 needed, and 2 if a cyclical call-graph was found but no other reason
11761 for a stub was detected. If called from the top level, a return of
11762 2 means the same as a return of 0. */
11763
11764 static int
11765 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11766 {
11767 int ret;
11768
11769 /* Mark this section as checked. */
11770 isec->call_check_done = 1;
11771
11772 /* We know none of our code bearing sections will need toc stubs. */
11773 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11774 return 0;
11775
11776 if (isec->size == 0)
11777 return 0;
11778
11779 if (isec->output_section == NULL)
11780 return 0;
11781
11782 ret = 0;
11783 if (isec->reloc_count != 0)
11784 {
11785 Elf_Internal_Rela *relstart, *rel;
11786 Elf_Internal_Sym *local_syms;
11787 struct ppc_link_hash_table *htab;
11788
11789 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11790 info->keep_memory);
11791 if (relstart == NULL)
11792 return -1;
11793
11794 /* Look for branches to outside of this section. */
11795 local_syms = NULL;
11796 htab = ppc_hash_table (info);
11797 if (htab == NULL)
11798 return -1;
11799
11800 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11801 {
11802 enum elf_ppc64_reloc_type r_type;
11803 unsigned long r_symndx;
11804 struct elf_link_hash_entry *h;
11805 struct ppc_link_hash_entry *eh;
11806 Elf_Internal_Sym *sym;
11807 asection *sym_sec;
11808 struct _opd_sec_data *opd;
11809 bfd_vma sym_value;
11810 bfd_vma dest;
11811
11812 r_type = ELF64_R_TYPE (rel->r_info);
11813 if (r_type != R_PPC64_REL24
11814 && r_type != R_PPC64_REL14
11815 && r_type != R_PPC64_REL14_BRTAKEN
11816 && r_type != R_PPC64_REL14_BRNTAKEN)
11817 continue;
11818
11819 r_symndx = ELF64_R_SYM (rel->r_info);
11820 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11821 isec->owner))
11822 {
11823 ret = -1;
11824 break;
11825 }
11826
11827 /* Calls to dynamic lib functions go through a plt call stub
11828 that uses r2. */
11829 eh = (struct ppc_link_hash_entry *) h;
11830 if (eh != NULL
11831 && (eh->elf.plt.plist != NULL
11832 || (eh->oh != NULL
11833 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11834 {
11835 ret = 1;
11836 break;
11837 }
11838
11839 if (sym_sec == NULL)
11840 /* Ignore other undefined symbols. */
11841 continue;
11842
11843 /* Assume branches to other sections not included in the
11844 link need stubs too, to cover -R and absolute syms. */
11845 if (sym_sec->output_section == NULL)
11846 {
11847 ret = 1;
11848 break;
11849 }
11850
11851 if (h == NULL)
11852 sym_value = sym->st_value;
11853 else
11854 {
11855 if (h->root.type != bfd_link_hash_defined
11856 && h->root.type != bfd_link_hash_defweak)
11857 abort ();
11858 sym_value = h->root.u.def.value;
11859 }
11860 sym_value += rel->r_addend;
11861
11862 /* If this branch reloc uses an opd sym, find the code section. */
11863 opd = get_opd_info (sym_sec);
11864 if (opd != NULL)
11865 {
11866 if (h == NULL && opd->adjust != NULL)
11867 {
11868 long adjust;
11869
11870 adjust = opd->adjust[OPD_NDX (sym_value)];
11871 if (adjust == -1)
11872 /* Assume deleted functions won't ever be called. */
11873 continue;
11874 sym_value += adjust;
11875 }
11876
11877 dest = opd_entry_value (sym_sec, sym_value,
11878 &sym_sec, NULL, FALSE);
11879 if (dest == (bfd_vma) -1)
11880 continue;
11881 }
11882 else
11883 dest = (sym_value
11884 + sym_sec->output_offset
11885 + sym_sec->output_section->vma);
11886
11887 /* Ignore branch to self. */
11888 if (sym_sec == isec)
11889 continue;
11890
11891 /* If the called function uses the toc, we need a stub. */
11892 if (sym_sec->has_toc_reloc
11893 || sym_sec->makes_toc_func_call)
11894 {
11895 ret = 1;
11896 break;
11897 }
11898
11899 /* Assume any branch that needs a long branch stub might in fact
11900 need a plt_branch stub. A plt_branch stub uses r2. */
11901 else if (dest - (isec->output_offset
11902 + isec->output_section->vma
11903 + rel->r_offset) + (1 << 25)
11904 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11905 ? h->other
11906 : sym->st_other))
11907 {
11908 ret = 1;
11909 break;
11910 }
11911
11912 /* If calling back to a section in the process of being
11913 tested, we can't say for sure that no toc adjusting stubs
11914 are needed, so don't return zero. */
11915 else if (sym_sec->call_check_in_progress)
11916 ret = 2;
11917
11918 /* Branches to another section that itself doesn't have any TOC
11919 references are OK. Recursively call ourselves to check. */
11920 else if (!sym_sec->call_check_done)
11921 {
11922 int recur;
11923
11924 /* Mark current section as indeterminate, so that other
11925 sections that call back to current won't be marked as
11926 known. */
11927 isec->call_check_in_progress = 1;
11928 recur = toc_adjusting_stub_needed (info, sym_sec);
11929 isec->call_check_in_progress = 0;
11930
11931 if (recur != 0)
11932 {
11933 ret = recur;
11934 if (recur != 2)
11935 break;
11936 }
11937 }
11938 }
11939
11940 if (local_syms != NULL
11941 && (elf_symtab_hdr (isec->owner).contents
11942 != (unsigned char *) local_syms))
11943 free (local_syms);
11944 if (elf_section_data (isec)->relocs != relstart)
11945 free (relstart);
11946 }
11947
11948 if ((ret & 1) == 0
11949 && isec->map_head.s != NULL
11950 && (strcmp (isec->output_section->name, ".init") == 0
11951 || strcmp (isec->output_section->name, ".fini") == 0))
11952 {
11953 if (isec->map_head.s->has_toc_reloc
11954 || isec->map_head.s->makes_toc_func_call)
11955 ret = 1;
11956 else if (!isec->map_head.s->call_check_done)
11957 {
11958 int recur;
11959 isec->call_check_in_progress = 1;
11960 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11961 isec->call_check_in_progress = 0;
11962 if (recur != 0)
11963 ret = recur;
11964 }
11965 }
11966
11967 if (ret == 1)
11968 isec->makes_toc_func_call = 1;
11969
11970 return ret;
11971 }
11972
11973 /* The linker repeatedly calls this function for each input section,
11974 in the order that input sections are linked into output sections.
11975 Build lists of input sections to determine groupings between which
11976 we may insert linker stubs. */
11977
11978 bfd_boolean
11979 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11980 {
11981 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11982
11983 if (htab == NULL)
11984 return FALSE;
11985
11986 if ((isec->output_section->flags & SEC_CODE) != 0
11987 && isec->output_section->id < htab->sec_info_arr_size)
11988 {
11989 /* This happens to make the list in reverse order,
11990 which is what we want. */
11991 htab->sec_info[isec->id].u.list
11992 = htab->sec_info[isec->output_section->id].u.list;
11993 htab->sec_info[isec->output_section->id].u.list = isec;
11994 }
11995
11996 if (htab->multi_toc_needed)
11997 {
11998 /* Analyse sections that aren't already flagged as needing a
11999 valid toc pointer. Exclude .fixup for the linux kernel.
12000 .fixup contains branches, but only back to the function that
12001 hit an exception. */
12002 if (!(isec->has_toc_reloc
12003 || (isec->flags & SEC_CODE) == 0
12004 || strcmp (isec->name, ".fixup") == 0
12005 || isec->call_check_done))
12006 {
12007 if (toc_adjusting_stub_needed (info, isec) < 0)
12008 return FALSE;
12009 }
12010 /* Make all sections use the TOC assigned for this object file.
12011 This will be wrong for pasted sections; We fix that in
12012 check_pasted_section(). */
12013 if (elf_gp (isec->owner) != 0)
12014 htab->toc_curr = elf_gp (isec->owner);
12015 }
12016
12017 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12018 return TRUE;
12019 }
12020
12021 /* Check that all .init and .fini sections use the same toc, if they
12022 have toc relocs. */
12023
12024 static bfd_boolean
12025 check_pasted_section (struct bfd_link_info *info, const char *name)
12026 {
12027 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12028
12029 if (o != NULL)
12030 {
12031 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12032 bfd_vma toc_off = 0;
12033 asection *i;
12034
12035 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12036 if (i->has_toc_reloc)
12037 {
12038 if (toc_off == 0)
12039 toc_off = htab->sec_info[i->id].toc_off;
12040 else if (toc_off != htab->sec_info[i->id].toc_off)
12041 return FALSE;
12042 }
12043
12044 if (toc_off == 0)
12045 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12046 if (i->makes_toc_func_call)
12047 {
12048 toc_off = htab->sec_info[i->id].toc_off;
12049 break;
12050 }
12051
12052 /* Make sure the whole pasted function uses the same toc offset. */
12053 if (toc_off != 0)
12054 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12055 htab->sec_info[i->id].toc_off = toc_off;
12056 }
12057 return TRUE;
12058 }
12059
12060 bfd_boolean
12061 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12062 {
12063 return (check_pasted_section (info, ".init")
12064 & check_pasted_section (info, ".fini"));
12065 }
12066
12067 /* See whether we can group stub sections together. Grouping stub
12068 sections may result in fewer stubs. More importantly, we need to
12069 put all .init* and .fini* stubs at the beginning of the .init or
12070 .fini output sections respectively, because glibc splits the
12071 _init and _fini functions into multiple parts. Putting a stub in
12072 the middle of a function is not a good idea. */
12073
12074 static bfd_boolean
12075 group_sections (struct bfd_link_info *info,
12076 bfd_size_type stub_group_size,
12077 bfd_boolean stubs_always_before_branch)
12078 {
12079 struct ppc_link_hash_table *htab;
12080 asection *osec;
12081 bfd_boolean suppress_size_errors;
12082
12083 htab = ppc_hash_table (info);
12084 if (htab == NULL)
12085 return FALSE;
12086
12087 suppress_size_errors = FALSE;
12088 if (stub_group_size == 1)
12089 {
12090 /* Default values. */
12091 if (stubs_always_before_branch)
12092 stub_group_size = 0x1e00000;
12093 else
12094 stub_group_size = 0x1c00000;
12095 suppress_size_errors = TRUE;
12096 }
12097
12098 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12099 {
12100 asection *tail;
12101
12102 if (osec->id >= htab->sec_info_arr_size)
12103 continue;
12104
12105 tail = htab->sec_info[osec->id].u.list;
12106 while (tail != NULL)
12107 {
12108 asection *curr;
12109 asection *prev;
12110 bfd_size_type total;
12111 bfd_boolean big_sec;
12112 bfd_vma curr_toc;
12113 struct map_stub *group;
12114 bfd_size_type group_size;
12115
12116 curr = tail;
12117 total = tail->size;
12118 group_size = (ppc64_elf_section_data (tail) != NULL
12119 && ppc64_elf_section_data (tail)->has_14bit_branch
12120 ? stub_group_size >> 10 : stub_group_size);
12121
12122 big_sec = total > group_size;
12123 if (big_sec && !suppress_size_errors)
12124 /* xgettext:c-format */
12125 _bfd_error_handler (_("%B section %A exceeds stub group size"),
12126 tail->owner, tail);
12127 curr_toc = htab->sec_info[tail->id].toc_off;
12128
12129 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12130 && ((total += curr->output_offset - prev->output_offset)
12131 < (ppc64_elf_section_data (prev) != NULL
12132 && ppc64_elf_section_data (prev)->has_14bit_branch
12133 ? (group_size = stub_group_size >> 10) : group_size))
12134 && htab->sec_info[prev->id].toc_off == curr_toc)
12135 curr = prev;
12136
12137 /* OK, the size from the start of CURR to the end is less
12138 than group_size and thus can be handled by one stub
12139 section. (or the tail section is itself larger than
12140 group_size, in which case we may be toast.) We should
12141 really be keeping track of the total size of stubs added
12142 here, as stubs contribute to the final output section
12143 size. That's a little tricky, and this way will only
12144 break if stubs added make the total size more than 2^25,
12145 ie. for the default stub_group_size, if stubs total more
12146 than 2097152 bytes, or nearly 75000 plt call stubs. */
12147 group = bfd_alloc (curr->owner, sizeof (*group));
12148 if (group == NULL)
12149 return FALSE;
12150 group->link_sec = curr;
12151 group->stub_sec = NULL;
12152 group->needs_save_res = 0;
12153 group->tls_get_addr_opt_bctrl = -1u;
12154 group->next = htab->group;
12155 htab->group = group;
12156 do
12157 {
12158 prev = htab->sec_info[tail->id].u.list;
12159 /* Set up this stub group. */
12160 htab->sec_info[tail->id].u.group = group;
12161 }
12162 while (tail != curr && (tail = prev) != NULL);
12163
12164 /* But wait, there's more! Input sections up to group_size
12165 bytes before the stub section can be handled by it too.
12166 Don't do this if we have a really large section after the
12167 stubs, as adding more stubs increases the chance that
12168 branches may not reach into the stub section. */
12169 if (!stubs_always_before_branch && !big_sec)
12170 {
12171 total = 0;
12172 while (prev != NULL
12173 && ((total += tail->output_offset - prev->output_offset)
12174 < (ppc64_elf_section_data (prev) != NULL
12175 && ppc64_elf_section_data (prev)->has_14bit_branch
12176 ? (group_size = stub_group_size >> 10) : group_size))
12177 && htab->sec_info[prev->id].toc_off == curr_toc)
12178 {
12179 tail = prev;
12180 prev = htab->sec_info[tail->id].u.list;
12181 htab->sec_info[tail->id].u.group = group;
12182 }
12183 }
12184 tail = prev;
12185 }
12186 }
12187 return TRUE;
12188 }
12189
12190 static const unsigned char glink_eh_frame_cie[] =
12191 {
12192 0, 0, 0, 16, /* length. */
12193 0, 0, 0, 0, /* id. */
12194 1, /* CIE version. */
12195 'z', 'R', 0, /* Augmentation string. */
12196 4, /* Code alignment. */
12197 0x78, /* Data alignment. */
12198 65, /* RA reg. */
12199 1, /* Augmentation size. */
12200 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12201 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12202 };
12203
12204 static size_t
12205 stub_eh_frame_size (struct map_stub *group, size_t align)
12206 {
12207 size_t this_size = 17;
12208 if (group->tls_get_addr_opt_bctrl != -1u)
12209 {
12210 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12211 if (to_bctrl < 64)
12212 this_size += 1;
12213 else if (to_bctrl < 256)
12214 this_size += 2;
12215 else if (to_bctrl < 65536)
12216 this_size += 3;
12217 else
12218 this_size += 5;
12219 this_size += 6;
12220 }
12221 this_size = (this_size + align - 1) & -align;
12222 return this_size;
12223 }
12224
12225 /* Stripping output sections is normally done before dynamic section
12226 symbols have been allocated. This function is called later, and
12227 handles cases like htab->brlt which is mapped to its own output
12228 section. */
12229
12230 static void
12231 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12232 {
12233 if (isec->size == 0
12234 && isec->output_section->size == 0
12235 && !(isec->output_section->flags & SEC_KEEP)
12236 && !bfd_section_removed_from_list (info->output_bfd,
12237 isec->output_section)
12238 && elf_section_data (isec->output_section)->dynindx == 0)
12239 {
12240 isec->output_section->flags |= SEC_EXCLUDE;
12241 bfd_section_list_remove (info->output_bfd, isec->output_section);
12242 info->output_bfd->section_count--;
12243 }
12244 }
12245
12246 /* Determine and set the size of the stub section for a final link.
12247
12248 The basic idea here is to examine all the relocations looking for
12249 PC-relative calls to a target that is unreachable with a "bl"
12250 instruction. */
12251
12252 bfd_boolean
12253 ppc64_elf_size_stubs (struct bfd_link_info *info)
12254 {
12255 bfd_size_type stub_group_size;
12256 bfd_boolean stubs_always_before_branch;
12257 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12258
12259 if (htab == NULL)
12260 return FALSE;
12261
12262 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12263 htab->params->plt_thread_safe = 1;
12264 if (!htab->opd_abi)
12265 htab->params->plt_thread_safe = 0;
12266 else if (htab->params->plt_thread_safe == -1)
12267 {
12268 static const char *const thread_starter[] =
12269 {
12270 "pthread_create",
12271 /* libstdc++ */
12272 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12273 /* librt */
12274 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12275 "mq_notify", "create_timer",
12276 /* libanl */
12277 "getaddrinfo_a",
12278 /* libgomp */
12279 "GOMP_parallel",
12280 "GOMP_parallel_start",
12281 "GOMP_parallel_loop_static",
12282 "GOMP_parallel_loop_static_start",
12283 "GOMP_parallel_loop_dynamic",
12284 "GOMP_parallel_loop_dynamic_start",
12285 "GOMP_parallel_loop_guided",
12286 "GOMP_parallel_loop_guided_start",
12287 "GOMP_parallel_loop_runtime",
12288 "GOMP_parallel_loop_runtime_start",
12289 "GOMP_parallel_sections",
12290 "GOMP_parallel_sections_start",
12291 /* libgo */
12292 "__go_go",
12293 };
12294 unsigned i;
12295
12296 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12297 {
12298 struct elf_link_hash_entry *h;
12299 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12300 FALSE, FALSE, TRUE);
12301 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12302 if (htab->params->plt_thread_safe)
12303 break;
12304 }
12305 }
12306 stubs_always_before_branch = htab->params->group_size < 0;
12307 if (htab->params->group_size < 0)
12308 stub_group_size = -htab->params->group_size;
12309 else
12310 stub_group_size = htab->params->group_size;
12311
12312 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12313 return FALSE;
12314
12315 #define STUB_SHRINK_ITER 20
12316 /* Loop until no stubs added. After iteration 20 of this loop we may
12317 exit on a stub section shrinking. This is to break out of a
12318 pathological case where adding stubs on one iteration decreases
12319 section gaps (perhaps due to alignment), which then requires
12320 fewer or smaller stubs on the next iteration. */
12321
12322 while (1)
12323 {
12324 bfd *input_bfd;
12325 unsigned int bfd_indx;
12326 struct map_stub *group;
12327
12328 htab->stub_iteration += 1;
12329
12330 for (input_bfd = info->input_bfds, bfd_indx = 0;
12331 input_bfd != NULL;
12332 input_bfd = input_bfd->link.next, bfd_indx++)
12333 {
12334 Elf_Internal_Shdr *symtab_hdr;
12335 asection *section;
12336 Elf_Internal_Sym *local_syms = NULL;
12337
12338 if (!is_ppc64_elf (input_bfd))
12339 continue;
12340
12341 /* We'll need the symbol table in a second. */
12342 symtab_hdr = &elf_symtab_hdr (input_bfd);
12343 if (symtab_hdr->sh_info == 0)
12344 continue;
12345
12346 /* Walk over each section attached to the input bfd. */
12347 for (section = input_bfd->sections;
12348 section != NULL;
12349 section = section->next)
12350 {
12351 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12352
12353 /* If there aren't any relocs, then there's nothing more
12354 to do. */
12355 if ((section->flags & SEC_RELOC) == 0
12356 || (section->flags & SEC_ALLOC) == 0
12357 || (section->flags & SEC_LOAD) == 0
12358 || (section->flags & SEC_CODE) == 0
12359 || section->reloc_count == 0)
12360 continue;
12361
12362 /* If this section is a link-once section that will be
12363 discarded, then don't create any stubs. */
12364 if (section->output_section == NULL
12365 || section->output_section->owner != info->output_bfd)
12366 continue;
12367
12368 /* Get the relocs. */
12369 internal_relocs
12370 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12371 info->keep_memory);
12372 if (internal_relocs == NULL)
12373 goto error_ret_free_local;
12374
12375 /* Now examine each relocation. */
12376 irela = internal_relocs;
12377 irelaend = irela + section->reloc_count;
12378 for (; irela < irelaend; irela++)
12379 {
12380 enum elf_ppc64_reloc_type r_type;
12381 unsigned int r_indx;
12382 enum ppc_stub_type stub_type;
12383 struct ppc_stub_hash_entry *stub_entry;
12384 asection *sym_sec, *code_sec;
12385 bfd_vma sym_value, code_value;
12386 bfd_vma destination;
12387 unsigned long local_off;
12388 bfd_boolean ok_dest;
12389 struct ppc_link_hash_entry *hash;
12390 struct ppc_link_hash_entry *fdh;
12391 struct elf_link_hash_entry *h;
12392 Elf_Internal_Sym *sym;
12393 char *stub_name;
12394 const asection *id_sec;
12395 struct _opd_sec_data *opd;
12396 struct plt_entry *plt_ent;
12397
12398 r_type = ELF64_R_TYPE (irela->r_info);
12399 r_indx = ELF64_R_SYM (irela->r_info);
12400
12401 if (r_type >= R_PPC64_max)
12402 {
12403 bfd_set_error (bfd_error_bad_value);
12404 goto error_ret_free_internal;
12405 }
12406
12407 /* Only look for stubs on branch instructions. */
12408 if (r_type != R_PPC64_REL24
12409 && r_type != R_PPC64_REL14
12410 && r_type != R_PPC64_REL14_BRTAKEN
12411 && r_type != R_PPC64_REL14_BRNTAKEN)
12412 continue;
12413
12414 /* Now determine the call target, its name, value,
12415 section. */
12416 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12417 r_indx, input_bfd))
12418 goto error_ret_free_internal;
12419 hash = (struct ppc_link_hash_entry *) h;
12420
12421 ok_dest = FALSE;
12422 fdh = NULL;
12423 sym_value = 0;
12424 if (hash == NULL)
12425 {
12426 sym_value = sym->st_value;
12427 if (sym_sec != NULL
12428 && sym_sec->output_section != NULL)
12429 ok_dest = TRUE;
12430 }
12431 else if (hash->elf.root.type == bfd_link_hash_defined
12432 || hash->elf.root.type == bfd_link_hash_defweak)
12433 {
12434 sym_value = hash->elf.root.u.def.value;
12435 if (sym_sec->output_section != NULL)
12436 ok_dest = TRUE;
12437 }
12438 else if (hash->elf.root.type == bfd_link_hash_undefweak
12439 || hash->elf.root.type == bfd_link_hash_undefined)
12440 {
12441 /* Recognise an old ABI func code entry sym, and
12442 use the func descriptor sym instead if it is
12443 defined. */
12444 if (hash->elf.root.root.string[0] == '.'
12445 && hash->oh != NULL)
12446 {
12447 fdh = ppc_follow_link (hash->oh);
12448 if (fdh->elf.root.type == bfd_link_hash_defined
12449 || fdh->elf.root.type == bfd_link_hash_defweak)
12450 {
12451 sym_sec = fdh->elf.root.u.def.section;
12452 sym_value = fdh->elf.root.u.def.value;
12453 if (sym_sec->output_section != NULL)
12454 ok_dest = TRUE;
12455 }
12456 else
12457 fdh = NULL;
12458 }
12459 }
12460 else
12461 {
12462 bfd_set_error (bfd_error_bad_value);
12463 goto error_ret_free_internal;
12464 }
12465
12466 destination = 0;
12467 local_off = 0;
12468 if (ok_dest)
12469 {
12470 sym_value += irela->r_addend;
12471 destination = (sym_value
12472 + sym_sec->output_offset
12473 + sym_sec->output_section->vma);
12474 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12475 ? hash->elf.other
12476 : sym->st_other);
12477 }
12478
12479 code_sec = sym_sec;
12480 code_value = sym_value;
12481 opd = get_opd_info (sym_sec);
12482 if (opd != NULL)
12483 {
12484 bfd_vma dest;
12485
12486 if (hash == NULL && opd->adjust != NULL)
12487 {
12488 long adjust = opd->adjust[OPD_NDX (sym_value)];
12489 if (adjust == -1)
12490 continue;
12491 code_value += adjust;
12492 sym_value += adjust;
12493 }
12494 dest = opd_entry_value (sym_sec, sym_value,
12495 &code_sec, &code_value, FALSE);
12496 if (dest != (bfd_vma) -1)
12497 {
12498 destination = dest;
12499 if (fdh != NULL)
12500 {
12501 /* Fixup old ABI sym to point at code
12502 entry. */
12503 hash->elf.root.type = bfd_link_hash_defweak;
12504 hash->elf.root.u.def.section = code_sec;
12505 hash->elf.root.u.def.value = code_value;
12506 }
12507 }
12508 }
12509
12510 /* Determine what (if any) linker stub is needed. */
12511 plt_ent = NULL;
12512 stub_type = ppc_type_of_stub (section, irela, &hash,
12513 &plt_ent, destination,
12514 local_off);
12515
12516 if (stub_type != ppc_stub_plt_call)
12517 {
12518 /* Check whether we need a TOC adjusting stub.
12519 Since the linker pastes together pieces from
12520 different object files when creating the
12521 _init and _fini functions, it may be that a
12522 call to what looks like a local sym is in
12523 fact a call needing a TOC adjustment. */
12524 if (code_sec != NULL
12525 && code_sec->output_section != NULL
12526 && (htab->sec_info[code_sec->id].toc_off
12527 != htab->sec_info[section->id].toc_off)
12528 && (code_sec->has_toc_reloc
12529 || code_sec->makes_toc_func_call))
12530 stub_type = ppc_stub_long_branch_r2off;
12531 }
12532
12533 if (stub_type == ppc_stub_none)
12534 continue;
12535
12536 /* __tls_get_addr calls might be eliminated. */
12537 if (stub_type != ppc_stub_plt_call
12538 && hash != NULL
12539 && (hash == htab->tls_get_addr
12540 || hash == htab->tls_get_addr_fd)
12541 && section->has_tls_reloc
12542 && irela != internal_relocs)
12543 {
12544 /* Get tls info. */
12545 unsigned char *tls_mask;
12546
12547 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12548 irela - 1, input_bfd))
12549 goto error_ret_free_internal;
12550 if (*tls_mask != 0)
12551 continue;
12552 }
12553
12554 if (stub_type == ppc_stub_plt_call)
12555 {
12556 if (!htab->opd_abi
12557 && htab->params->plt_localentry0 != 0
12558 && is_elfv2_localentry0 (&hash->elf))
12559 htab->has_plt_localentry0 = 1;
12560 else if (irela + 1 < irelaend
12561 && irela[1].r_offset == irela->r_offset + 4
12562 && (ELF64_R_TYPE (irela[1].r_info)
12563 == R_PPC64_TOCSAVE))
12564 {
12565 if (!tocsave_find (htab, INSERT,
12566 &local_syms, irela + 1, input_bfd))
12567 goto error_ret_free_internal;
12568 }
12569 else
12570 stub_type = ppc_stub_plt_call_r2save;
12571 }
12572
12573 /* Support for grouping stub sections. */
12574 id_sec = htab->sec_info[section->id].u.group->link_sec;
12575
12576 /* Get the name of this stub. */
12577 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12578 if (!stub_name)
12579 goto error_ret_free_internal;
12580
12581 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12582 stub_name, FALSE, FALSE);
12583 if (stub_entry != NULL)
12584 {
12585 /* The proper stub has already been created. */
12586 free (stub_name);
12587 if (stub_type == ppc_stub_plt_call_r2save)
12588 stub_entry->stub_type = stub_type;
12589 continue;
12590 }
12591
12592 stub_entry = ppc_add_stub (stub_name, section, info);
12593 if (stub_entry == NULL)
12594 {
12595 free (stub_name);
12596 error_ret_free_internal:
12597 if (elf_section_data (section)->relocs == NULL)
12598 free (internal_relocs);
12599 error_ret_free_local:
12600 if (local_syms != NULL
12601 && (symtab_hdr->contents
12602 != (unsigned char *) local_syms))
12603 free (local_syms);
12604 return FALSE;
12605 }
12606
12607 stub_entry->stub_type = stub_type;
12608 if (stub_type != ppc_stub_plt_call
12609 && stub_type != ppc_stub_plt_call_r2save)
12610 {
12611 stub_entry->target_value = code_value;
12612 stub_entry->target_section = code_sec;
12613 }
12614 else
12615 {
12616 stub_entry->target_value = sym_value;
12617 stub_entry->target_section = sym_sec;
12618 }
12619 stub_entry->h = hash;
12620 stub_entry->plt_ent = plt_ent;
12621 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12622
12623 if (stub_entry->h != NULL)
12624 htab->stub_globals += 1;
12625 }
12626
12627 /* We're done with the internal relocs, free them. */
12628 if (elf_section_data (section)->relocs != internal_relocs)
12629 free (internal_relocs);
12630 }
12631
12632 if (local_syms != NULL
12633 && symtab_hdr->contents != (unsigned char *) local_syms)
12634 {
12635 if (!info->keep_memory)
12636 free (local_syms);
12637 else
12638 symtab_hdr->contents = (unsigned char *) local_syms;
12639 }
12640 }
12641
12642 /* We may have added some stubs. Find out the new size of the
12643 stub sections. */
12644 for (group = htab->group; group != NULL; group = group->next)
12645 if (group->stub_sec != NULL)
12646 {
12647 asection *stub_sec = group->stub_sec;
12648
12649 if (htab->stub_iteration <= STUB_SHRINK_ITER
12650 || stub_sec->rawsize < stub_sec->size)
12651 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12652 stub_sec->rawsize = stub_sec->size;
12653 stub_sec->size = 0;
12654 stub_sec->reloc_count = 0;
12655 stub_sec->flags &= ~SEC_RELOC;
12656 }
12657
12658 htab->brlt->size = 0;
12659 htab->brlt->reloc_count = 0;
12660 htab->brlt->flags &= ~SEC_RELOC;
12661 if (htab->relbrlt != NULL)
12662 htab->relbrlt->size = 0;
12663
12664 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12665
12666 for (group = htab->group; group != NULL; group = group->next)
12667 if (group->needs_save_res)
12668 group->stub_sec->size += htab->sfpr->size;
12669
12670 if (info->emitrelocations
12671 && htab->glink != NULL && htab->glink->size != 0)
12672 {
12673 htab->glink->reloc_count = 1;
12674 htab->glink->flags |= SEC_RELOC;
12675 }
12676
12677 if (htab->glink_eh_frame != NULL
12678 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12679 && htab->glink_eh_frame->output_section->size != 0)
12680 {
12681 size_t size = 0, align = 4;
12682
12683 for (group = htab->group; group != NULL; group = group->next)
12684 if (group->stub_sec != NULL)
12685 size += stub_eh_frame_size (group, align);
12686 if (htab->glink != NULL && htab->glink->size != 0)
12687 size += (24 + align - 1) & -align;
12688 if (size != 0)
12689 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12690 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12691 size = (size + align - 1) & -align;
12692 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12693 htab->glink_eh_frame->size = size;
12694 }
12695
12696 if (htab->params->plt_stub_align != 0)
12697 for (group = htab->group; group != NULL; group = group->next)
12698 if (group->stub_sec != NULL)
12699 group->stub_sec->size = ((group->stub_sec->size
12700 + (1 << htab->params->plt_stub_align) - 1)
12701 & -(1 << htab->params->plt_stub_align));
12702
12703 for (group = htab->group; group != NULL; group = group->next)
12704 if (group->stub_sec != NULL
12705 && group->stub_sec->rawsize != group->stub_sec->size
12706 && (htab->stub_iteration <= STUB_SHRINK_ITER
12707 || group->stub_sec->rawsize < group->stub_sec->size))
12708 break;
12709
12710 if (group == NULL
12711 && (htab->glink_eh_frame == NULL
12712 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12713 break;
12714
12715 /* Ask the linker to do its stuff. */
12716 (*htab->params->layout_sections_again) ();
12717 }
12718
12719 if (htab->glink_eh_frame != NULL
12720 && htab->glink_eh_frame->size != 0)
12721 {
12722 bfd_vma val;
12723 bfd_byte *p, *last_fde;
12724 size_t last_fde_len, size, align, pad;
12725 struct map_stub *group;
12726
12727 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12728 if (p == NULL)
12729 return FALSE;
12730 htab->glink_eh_frame->contents = p;
12731 last_fde = p;
12732 align = 4;
12733
12734 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12735 /* CIE length (rewrite in case little-endian). */
12736 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12737 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12738 p += last_fde_len + 4;
12739
12740 for (group = htab->group; group != NULL; group = group->next)
12741 if (group->stub_sec != NULL)
12742 {
12743 last_fde = p;
12744 last_fde_len = stub_eh_frame_size (group, align) - 4;
12745 /* FDE length. */
12746 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12747 p += 4;
12748 /* CIE pointer. */
12749 val = p - htab->glink_eh_frame->contents;
12750 bfd_put_32 (htab->elf.dynobj, val, p);
12751 p += 4;
12752 /* Offset to stub section, written later. */
12753 p += 4;
12754 /* stub section size. */
12755 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
12756 p += 4;
12757 /* Augmentation. */
12758 p += 1;
12759 if (group->tls_get_addr_opt_bctrl != -1u)
12760 {
12761 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12762
12763 /* This FDE needs more than just the default.
12764 Describe __tls_get_addr_opt stub LR. */
12765 if (to_bctrl < 64)
12766 *p++ = DW_CFA_advance_loc + to_bctrl;
12767 else if (to_bctrl < 256)
12768 {
12769 *p++ = DW_CFA_advance_loc1;
12770 *p++ = to_bctrl;
12771 }
12772 else if (to_bctrl < 65536)
12773 {
12774 *p++ = DW_CFA_advance_loc2;
12775 bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
12776 p += 2;
12777 }
12778 else
12779 {
12780 *p++ = DW_CFA_advance_loc4;
12781 bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
12782 p += 4;
12783 }
12784 *p++ = DW_CFA_offset_extended_sf;
12785 *p++ = 65;
12786 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
12787 *p++ = DW_CFA_advance_loc + 4;
12788 *p++ = DW_CFA_restore_extended;
12789 *p++ = 65;
12790 }
12791 /* Pad. */
12792 p = last_fde + last_fde_len + 4;
12793 }
12794 if (htab->glink != NULL && htab->glink->size != 0)
12795 {
12796 last_fde = p;
12797 last_fde_len = ((24 + align - 1) & -align) - 4;
12798 /* FDE length. */
12799 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12800 p += 4;
12801 /* CIE pointer. */
12802 val = p - htab->glink_eh_frame->contents;
12803 bfd_put_32 (htab->elf.dynobj, val, p);
12804 p += 4;
12805 /* Offset to .glink, written later. */
12806 p += 4;
12807 /* .glink size. */
12808 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12809 p += 4;
12810 /* Augmentation. */
12811 p += 1;
12812
12813 *p++ = DW_CFA_advance_loc + 1;
12814 *p++ = DW_CFA_register;
12815 *p++ = 65;
12816 *p++ = htab->opd_abi ? 12 : 0;
12817 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
12818 *p++ = DW_CFA_restore_extended;
12819 *p++ = 65;
12820 p += ((24 + align - 1) & -align) - 24;
12821 }
12822 /* Subsume any padding into the last FDE if user .eh_frame
12823 sections are aligned more than glink_eh_frame. Otherwise any
12824 zero padding will be seen as a terminator. */
12825 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12826 size = p - htab->glink_eh_frame->contents;
12827 pad = ((size + align - 1) & -align) - size;
12828 htab->glink_eh_frame->size = size + pad;
12829 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12830 }
12831
12832 maybe_strip_output (info, htab->brlt);
12833 if (htab->glink_eh_frame != NULL)
12834 maybe_strip_output (info, htab->glink_eh_frame);
12835
12836 return TRUE;
12837 }
12838
12839 /* Called after we have determined section placement. If sections
12840 move, we'll be called again. Provide a value for TOCstart. */
12841
12842 bfd_vma
12843 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12844 {
12845 asection *s;
12846 bfd_vma TOCstart, adjust;
12847
12848 if (info != NULL)
12849 {
12850 struct elf_link_hash_entry *h;
12851 struct elf_link_hash_table *htab = elf_hash_table (info);
12852
12853 if (is_elf_hash_table (htab)
12854 && htab->hgot != NULL)
12855 h = htab->hgot;
12856 else
12857 {
12858 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12859 if (is_elf_hash_table (htab))
12860 htab->hgot = h;
12861 }
12862 if (h != NULL
12863 && h->root.type == bfd_link_hash_defined
12864 && !h->root.linker_def
12865 && (!is_elf_hash_table (htab)
12866 || h->def_regular))
12867 {
12868 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12869 + h->root.u.def.section->output_offset
12870 + h->root.u.def.section->output_section->vma);
12871 _bfd_set_gp_value (obfd, TOCstart);
12872 return TOCstart;
12873 }
12874 }
12875
12876 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12877 order. The TOC starts where the first of these sections starts. */
12878 s = bfd_get_section_by_name (obfd, ".got");
12879 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12880 s = bfd_get_section_by_name (obfd, ".toc");
12881 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12882 s = bfd_get_section_by_name (obfd, ".tocbss");
12883 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12884 s = bfd_get_section_by_name (obfd, ".plt");
12885 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12886 {
12887 /* This may happen for
12888 o references to TOC base (SYM@toc / TOC[tc0]) without a
12889 .toc directive
12890 o bad linker script
12891 o --gc-sections and empty TOC sections
12892
12893 FIXME: Warn user? */
12894
12895 /* Look for a likely section. We probably won't even be
12896 using TOCstart. */
12897 for (s = obfd->sections; s != NULL; s = s->next)
12898 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12899 | SEC_EXCLUDE))
12900 == (SEC_ALLOC | SEC_SMALL_DATA))
12901 break;
12902 if (s == NULL)
12903 for (s = obfd->sections; s != NULL; s = s->next)
12904 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12905 == (SEC_ALLOC | SEC_SMALL_DATA))
12906 break;
12907 if (s == NULL)
12908 for (s = obfd->sections; s != NULL; s = s->next)
12909 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12910 == SEC_ALLOC)
12911 break;
12912 if (s == NULL)
12913 for (s = obfd->sections; s != NULL; s = s->next)
12914 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12915 break;
12916 }
12917
12918 TOCstart = 0;
12919 if (s != NULL)
12920 TOCstart = s->output_section->vma + s->output_offset;
12921
12922 /* Force alignment. */
12923 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12924 TOCstart -= adjust;
12925 _bfd_set_gp_value (obfd, TOCstart);
12926
12927 if (info != NULL && s != NULL)
12928 {
12929 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12930
12931 if (htab != NULL)
12932 {
12933 if (htab->elf.hgot != NULL)
12934 {
12935 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12936 htab->elf.hgot->root.u.def.section = s;
12937 }
12938 }
12939 else
12940 {
12941 struct bfd_link_hash_entry *bh = NULL;
12942 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12943 s, TOC_BASE_OFF - adjust,
12944 NULL, FALSE, FALSE, &bh);
12945 }
12946 }
12947 return TOCstart;
12948 }
12949
12950 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12951 write out any global entry stubs. */
12952
12953 static bfd_boolean
12954 build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12955 {
12956 struct bfd_link_info *info;
12957 struct ppc_link_hash_table *htab;
12958 struct plt_entry *pent;
12959 asection *s;
12960
12961 if (h->root.type == bfd_link_hash_indirect)
12962 return TRUE;
12963
12964 if (!h->pointer_equality_needed)
12965 return TRUE;
12966
12967 if (h->def_regular)
12968 return TRUE;
12969
12970 info = inf;
12971 htab = ppc_hash_table (info);
12972 if (htab == NULL)
12973 return FALSE;
12974
12975 s = htab->glink;
12976 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12977 if (pent->plt.offset != (bfd_vma) -1
12978 && pent->addend == 0)
12979 {
12980 bfd_byte *p;
12981 asection *plt;
12982 bfd_vma off;
12983
12984 p = s->contents + h->root.u.def.value;
12985 plt = htab->elf.splt;
12986 if (!htab->elf.dynamic_sections_created
12987 || h->dynindx == -1)
12988 plt = htab->elf.iplt;
12989 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12990 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12991
12992 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12993 {
12994 info->callbacks->einfo
12995 (_("%P: linkage table error against `%T'\n"),
12996 h->root.root.string);
12997 bfd_set_error (bfd_error_bad_value);
12998 htab->stub_error = TRUE;
12999 }
13000
13001 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13002 if (htab->params->emit_stub_syms)
13003 {
13004 size_t len = strlen (h->root.root.string);
13005 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13006
13007 if (name == NULL)
13008 return FALSE;
13009
13010 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13011 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13012 if (h == NULL)
13013 return FALSE;
13014 if (h->root.type == bfd_link_hash_new)
13015 {
13016 h->root.type = bfd_link_hash_defined;
13017 h->root.u.def.section = s;
13018 h->root.u.def.value = p - s->contents;
13019 h->ref_regular = 1;
13020 h->def_regular = 1;
13021 h->ref_regular_nonweak = 1;
13022 h->forced_local = 1;
13023 h->non_elf = 0;
13024 h->root.linker_def = 1;
13025 }
13026 }
13027
13028 if (PPC_HA (off) != 0)
13029 {
13030 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13031 p += 4;
13032 }
13033 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13034 p += 4;
13035 bfd_put_32 (s->owner, MTCTR_R12, p);
13036 p += 4;
13037 bfd_put_32 (s->owner, BCTR, p);
13038 break;
13039 }
13040 return TRUE;
13041 }
13042
13043 /* Build all the stubs associated with the current output file.
13044 The stubs are kept in a hash table attached to the main linker
13045 hash table. This function is called via gldelf64ppc_finish. */
13046
13047 bfd_boolean
13048 ppc64_elf_build_stubs (struct bfd_link_info *info,
13049 char **stats)
13050 {
13051 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13052 struct map_stub *group;
13053 asection *stub_sec;
13054 bfd_byte *p;
13055 int stub_sec_count = 0;
13056
13057 if (htab == NULL)
13058 return FALSE;
13059
13060 /* Allocate memory to hold the linker stubs. */
13061 for (group = htab->group; group != NULL; group = group->next)
13062 if ((stub_sec = group->stub_sec) != NULL
13063 && stub_sec->size != 0)
13064 {
13065 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13066 if (stub_sec->contents == NULL)
13067 return FALSE;
13068 stub_sec->size = 0;
13069 }
13070
13071 if (htab->glink != NULL && htab->glink->size != 0)
13072 {
13073 unsigned int indx;
13074 bfd_vma plt0;
13075
13076 /* Build the .glink plt call stub. */
13077 if (htab->params->emit_stub_syms)
13078 {
13079 struct elf_link_hash_entry *h;
13080 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13081 TRUE, FALSE, FALSE);
13082 if (h == NULL)
13083 return FALSE;
13084 if (h->root.type == bfd_link_hash_new)
13085 {
13086 h->root.type = bfd_link_hash_defined;
13087 h->root.u.def.section = htab->glink;
13088 h->root.u.def.value = 8;
13089 h->ref_regular = 1;
13090 h->def_regular = 1;
13091 h->ref_regular_nonweak = 1;
13092 h->forced_local = 1;
13093 h->non_elf = 0;
13094 h->root.linker_def = 1;
13095 }
13096 }
13097 plt0 = (htab->elf.splt->output_section->vma
13098 + htab->elf.splt->output_offset
13099 - 16);
13100 if (info->emitrelocations)
13101 {
13102 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13103 if (r == NULL)
13104 return FALSE;
13105 r->r_offset = (htab->glink->output_offset
13106 + htab->glink->output_section->vma);
13107 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13108 r->r_addend = plt0;
13109 }
13110 p = htab->glink->contents;
13111 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13112 bfd_put_64 (htab->glink->owner, plt0, p);
13113 p += 8;
13114 if (htab->opd_abi)
13115 {
13116 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13117 p += 4;
13118 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13119 p += 4;
13120 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13121 p += 4;
13122 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13123 p += 4;
13124 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13125 p += 4;
13126 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13127 p += 4;
13128 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13129 p += 4;
13130 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13131 p += 4;
13132 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13133 p += 4;
13134 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13135 p += 4;
13136 }
13137 else
13138 {
13139 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13140 p += 4;
13141 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13142 p += 4;
13143 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13144 p += 4;
13145 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13146 p += 4;
13147 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13148 p += 4;
13149 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13150 p += 4;
13151 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13152 p += 4;
13153 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13154 p += 4;
13155 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13156 p += 4;
13157 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13158 p += 4;
13159 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13160 p += 4;
13161 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13162 p += 4;
13163 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13164 p += 4;
13165 }
13166 bfd_put_32 (htab->glink->owner, BCTR, p);
13167 p += 4;
13168 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
13169 {
13170 bfd_put_32 (htab->glink->owner, NOP, p);
13171 p += 4;
13172 }
13173
13174 /* Build the .glink lazy link call stubs. */
13175 indx = 0;
13176 while (p < htab->glink->contents + htab->glink->rawsize)
13177 {
13178 if (htab->opd_abi)
13179 {
13180 if (indx < 0x8000)
13181 {
13182 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13183 p += 4;
13184 }
13185 else
13186 {
13187 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13188 p += 4;
13189 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13190 p);
13191 p += 4;
13192 }
13193 }
13194 bfd_put_32 (htab->glink->owner,
13195 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13196 indx++;
13197 p += 4;
13198 }
13199
13200 /* Build .glink global entry stubs. */
13201 if (htab->glink->size > htab->glink->rawsize)
13202 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
13203 }
13204
13205 if (htab->brlt != NULL && htab->brlt->size != 0)
13206 {
13207 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13208 htab->brlt->size);
13209 if (htab->brlt->contents == NULL)
13210 return FALSE;
13211 }
13212 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13213 {
13214 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13215 htab->relbrlt->size);
13216 if (htab->relbrlt->contents == NULL)
13217 return FALSE;
13218 }
13219
13220 /* Build the stubs as directed by the stub hash table. */
13221 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13222
13223 for (group = htab->group; group != NULL; group = group->next)
13224 if (group->needs_save_res)
13225 {
13226 stub_sec = group->stub_sec;
13227 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13228 htab->sfpr->size);
13229 if (htab->params->emit_stub_syms)
13230 {
13231 unsigned int i;
13232
13233 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13234 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13235 return FALSE;
13236 }
13237 stub_sec->size += htab->sfpr->size;
13238 }
13239
13240 if (htab->relbrlt != NULL)
13241 htab->relbrlt->reloc_count = 0;
13242
13243 if (htab->params->plt_stub_align != 0)
13244 for (group = htab->group; group != NULL; group = group->next)
13245 if ((stub_sec = group->stub_sec) != NULL)
13246 stub_sec->size = ((stub_sec->size
13247 + (1 << htab->params->plt_stub_align) - 1)
13248 & -(1 << htab->params->plt_stub_align));
13249
13250 for (group = htab->group; group != NULL; group = group->next)
13251 if ((stub_sec = group->stub_sec) != NULL)
13252 {
13253 stub_sec_count += 1;
13254 if (stub_sec->rawsize != stub_sec->size
13255 && (htab->stub_iteration <= STUB_SHRINK_ITER
13256 || stub_sec->rawsize < stub_sec->size))
13257 break;
13258 }
13259
13260 if (group != NULL)
13261 {
13262 htab->stub_error = TRUE;
13263 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
13264 }
13265
13266 if (htab->stub_error)
13267 return FALSE;
13268
13269 if (stats != NULL)
13270 {
13271 *stats = bfd_malloc (500);
13272 if (*stats == NULL)
13273 return FALSE;
13274
13275 sprintf (*stats, _("linker stubs in %u group%s\n"
13276 " branch %lu\n"
13277 " toc adjust %lu\n"
13278 " long branch %lu\n"
13279 " long toc adj %lu\n"
13280 " plt call %lu\n"
13281 " plt call toc %lu\n"
13282 " global entry %lu"),
13283 stub_sec_count,
13284 stub_sec_count == 1 ? "" : "s",
13285 htab->stub_count[ppc_stub_long_branch - 1],
13286 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13287 htab->stub_count[ppc_stub_plt_branch - 1],
13288 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13289 htab->stub_count[ppc_stub_plt_call - 1],
13290 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13291 htab->stub_count[ppc_stub_global_entry - 1]);
13292 }
13293 return TRUE;
13294 }
13295
13296 /* What to do when ld finds relocations against symbols defined in
13297 discarded sections. */
13298
13299 static unsigned int
13300 ppc64_elf_action_discarded (asection *sec)
13301 {
13302 if (strcmp (".opd", sec->name) == 0)
13303 return 0;
13304
13305 if (strcmp (".toc", sec->name) == 0)
13306 return 0;
13307
13308 if (strcmp (".toc1", sec->name) == 0)
13309 return 0;
13310
13311 return _bfd_elf_default_action_discarded (sec);
13312 }
13313
13314 /* The RELOCATE_SECTION function is called by the ELF backend linker
13315 to handle the relocations for a section.
13316
13317 The relocs are always passed as Rela structures; if the section
13318 actually uses Rel structures, the r_addend field will always be
13319 zero.
13320
13321 This function is responsible for adjust the section contents as
13322 necessary, and (if using Rela relocs and generating a
13323 relocatable output file) adjusting the reloc addend as
13324 necessary.
13325
13326 This function does not have to worry about setting the reloc
13327 address or the reloc symbol index.
13328
13329 LOCAL_SYMS is a pointer to the swapped in local symbols.
13330
13331 LOCAL_SECTIONS is an array giving the section in the input file
13332 corresponding to the st_shndx field of each local symbol.
13333
13334 The global hash table entry for the global symbols can be found
13335 via elf_sym_hashes (input_bfd).
13336
13337 When generating relocatable output, this function must handle
13338 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13339 going to be the section symbol corresponding to the output
13340 section, which means that the addend must be adjusted
13341 accordingly. */
13342
13343 static bfd_boolean
13344 ppc64_elf_relocate_section (bfd *output_bfd,
13345 struct bfd_link_info *info,
13346 bfd *input_bfd,
13347 asection *input_section,
13348 bfd_byte *contents,
13349 Elf_Internal_Rela *relocs,
13350 Elf_Internal_Sym *local_syms,
13351 asection **local_sections)
13352 {
13353 struct ppc_link_hash_table *htab;
13354 Elf_Internal_Shdr *symtab_hdr;
13355 struct elf_link_hash_entry **sym_hashes;
13356 Elf_Internal_Rela *rel;
13357 Elf_Internal_Rela *wrel;
13358 Elf_Internal_Rela *relend;
13359 Elf_Internal_Rela outrel;
13360 bfd_byte *loc;
13361 struct got_entry **local_got_ents;
13362 bfd_vma TOCstart;
13363 bfd_boolean ret = TRUE;
13364 bfd_boolean is_opd;
13365 /* Assume 'at' branch hints. */
13366 bfd_boolean is_isa_v2 = TRUE;
13367 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13368
13369 /* Initialize howto table if needed. */
13370 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13371 ppc_howto_init ();
13372
13373 htab = ppc_hash_table (info);
13374 if (htab == NULL)
13375 return FALSE;
13376
13377 /* Don't relocate stub sections. */
13378 if (input_section->owner == htab->params->stub_bfd)
13379 return TRUE;
13380
13381 BFD_ASSERT (is_ppc64_elf (input_bfd));
13382
13383 local_got_ents = elf_local_got_ents (input_bfd);
13384 TOCstart = elf_gp (output_bfd);
13385 symtab_hdr = &elf_symtab_hdr (input_bfd);
13386 sym_hashes = elf_sym_hashes (input_bfd);
13387 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13388
13389 rel = wrel = relocs;
13390 relend = relocs + input_section->reloc_count;
13391 for (; rel < relend; wrel++, rel++)
13392 {
13393 enum elf_ppc64_reloc_type r_type;
13394 bfd_vma addend;
13395 bfd_reloc_status_type r;
13396 Elf_Internal_Sym *sym;
13397 asection *sec;
13398 struct elf_link_hash_entry *h_elf;
13399 struct ppc_link_hash_entry *h;
13400 struct ppc_link_hash_entry *fdh;
13401 const char *sym_name;
13402 unsigned long r_symndx, toc_symndx;
13403 bfd_vma toc_addend;
13404 unsigned char tls_mask, tls_gd, tls_type;
13405 unsigned char sym_type;
13406 bfd_vma relocation;
13407 bfd_boolean unresolved_reloc;
13408 bfd_boolean warned;
13409 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13410 unsigned int insn;
13411 unsigned int mask;
13412 struct ppc_stub_hash_entry *stub_entry;
13413 bfd_vma max_br_offset;
13414 bfd_vma from;
13415 Elf_Internal_Rela orig_rel;
13416 reloc_howto_type *howto;
13417 struct reloc_howto_struct alt_howto;
13418
13419 again:
13420 orig_rel = *rel;
13421
13422 r_type = ELF64_R_TYPE (rel->r_info);
13423 r_symndx = ELF64_R_SYM (rel->r_info);
13424
13425 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13426 symbol of the previous ADDR64 reloc. The symbol gives us the
13427 proper TOC base to use. */
13428 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13429 && wrel != relocs
13430 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13431 && is_opd)
13432 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13433
13434 sym = NULL;
13435 sec = NULL;
13436 h_elf = NULL;
13437 sym_name = NULL;
13438 unresolved_reloc = FALSE;
13439 warned = FALSE;
13440
13441 if (r_symndx < symtab_hdr->sh_info)
13442 {
13443 /* It's a local symbol. */
13444 struct _opd_sec_data *opd;
13445
13446 sym = local_syms + r_symndx;
13447 sec = local_sections[r_symndx];
13448 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13449 sym_type = ELF64_ST_TYPE (sym->st_info);
13450 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13451 opd = get_opd_info (sec);
13452 if (opd != NULL && opd->adjust != NULL)
13453 {
13454 long adjust = opd->adjust[OPD_NDX (sym->st_value
13455 + rel->r_addend)];
13456 if (adjust == -1)
13457 relocation = 0;
13458 else
13459 {
13460 /* If this is a relocation against the opd section sym
13461 and we have edited .opd, adjust the reloc addend so
13462 that ld -r and ld --emit-relocs output is correct.
13463 If it is a reloc against some other .opd symbol,
13464 then the symbol value will be adjusted later. */
13465 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13466 rel->r_addend += adjust;
13467 else
13468 relocation += adjust;
13469 }
13470 }
13471 }
13472 else
13473 {
13474 bfd_boolean ignored;
13475
13476 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13477 r_symndx, symtab_hdr, sym_hashes,
13478 h_elf, sec, relocation,
13479 unresolved_reloc, warned, ignored);
13480 sym_name = h_elf->root.root.string;
13481 sym_type = h_elf->type;
13482 if (sec != NULL
13483 && sec->owner == output_bfd
13484 && strcmp (sec->name, ".opd") == 0)
13485 {
13486 /* This is a symbol defined in a linker script. All
13487 such are defined in output sections, even those
13488 defined by simple assignment from a symbol defined in
13489 an input section. Transfer the symbol to an
13490 appropriate input .opd section, so that a branch to
13491 this symbol will be mapped to the location specified
13492 by the opd entry. */
13493 struct bfd_link_order *lo;
13494 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13495 if (lo->type == bfd_indirect_link_order)
13496 {
13497 asection *isec = lo->u.indirect.section;
13498 if (h_elf->root.u.def.value >= isec->output_offset
13499 && h_elf->root.u.def.value < (isec->output_offset
13500 + isec->size))
13501 {
13502 h_elf->root.u.def.value -= isec->output_offset;
13503 h_elf->root.u.def.section = isec;
13504 sec = isec;
13505 break;
13506 }
13507 }
13508 }
13509 }
13510 h = (struct ppc_link_hash_entry *) h_elf;
13511
13512 if (sec != NULL && discarded_section (sec))
13513 {
13514 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13515 input_bfd, input_section,
13516 contents + rel->r_offset);
13517 wrel->r_offset = rel->r_offset;
13518 wrel->r_info = 0;
13519 wrel->r_addend = 0;
13520
13521 /* For ld -r, remove relocations in debug sections against
13522 symbols defined in discarded sections. Not done for
13523 non-debug to preserve relocs in .eh_frame which the
13524 eh_frame editing code expects to be present. */
13525 if (bfd_link_relocatable (info)
13526 && (input_section->flags & SEC_DEBUGGING))
13527 wrel--;
13528
13529 continue;
13530 }
13531
13532 if (bfd_link_relocatable (info))
13533 goto copy_reloc;
13534
13535 if (h != NULL && &h->elf == htab->elf.hgot)
13536 {
13537 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13538 sec = bfd_abs_section_ptr;
13539 unresolved_reloc = FALSE;
13540 }
13541
13542 /* TLS optimizations. Replace instruction sequences and relocs
13543 based on information we collected in tls_optimize. We edit
13544 RELOCS so that --emit-relocs will output something sensible
13545 for the final instruction stream. */
13546 tls_mask = 0;
13547 tls_gd = 0;
13548 toc_symndx = 0;
13549 if (h != NULL)
13550 tls_mask = h->tls_mask;
13551 else if (local_got_ents != NULL)
13552 {
13553 struct plt_entry **local_plt = (struct plt_entry **)
13554 (local_got_ents + symtab_hdr->sh_info);
13555 unsigned char *lgot_masks = (unsigned char *)
13556 (local_plt + symtab_hdr->sh_info);
13557 tls_mask = lgot_masks[r_symndx];
13558 }
13559 if (tls_mask == 0
13560 && (r_type == R_PPC64_TLS
13561 || r_type == R_PPC64_TLSGD
13562 || r_type == R_PPC64_TLSLD))
13563 {
13564 /* Check for toc tls entries. */
13565 unsigned char *toc_tls;
13566
13567 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13568 &local_syms, rel, input_bfd))
13569 return FALSE;
13570
13571 if (toc_tls)
13572 tls_mask = *toc_tls;
13573 }
13574
13575 /* Check that tls relocs are used with tls syms, and non-tls
13576 relocs are used with non-tls syms. */
13577 if (r_symndx != STN_UNDEF
13578 && r_type != R_PPC64_NONE
13579 && (h == NULL
13580 || h->elf.root.type == bfd_link_hash_defined
13581 || h->elf.root.type == bfd_link_hash_defweak)
13582 && (IS_PPC64_TLS_RELOC (r_type)
13583 != (sym_type == STT_TLS
13584 || (sym_type == STT_SECTION
13585 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13586 {
13587 if (tls_mask != 0
13588 && (r_type == R_PPC64_TLS
13589 || r_type == R_PPC64_TLSGD
13590 || r_type == R_PPC64_TLSLD))
13591 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13592 ;
13593 else
13594 info->callbacks->einfo
13595 (!IS_PPC64_TLS_RELOC (r_type)
13596 /* xgettext:c-format */
13597 ? _("%H: %s used with TLS symbol `%T'\n")
13598 /* xgettext:c-format */
13599 : _("%H: %s used with non-TLS symbol `%T'\n"),
13600 input_bfd, input_section, rel->r_offset,
13601 ppc64_elf_howto_table[r_type]->name,
13602 sym_name);
13603 }
13604
13605 /* Ensure reloc mapping code below stays sane. */
13606 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13607 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13608 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13609 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13610 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13611 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13612 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13613 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13614 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13615 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13616 abort ();
13617
13618 switch (r_type)
13619 {
13620 default:
13621 break;
13622
13623 case R_PPC64_LO_DS_OPT:
13624 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13625 if ((insn & (0x3f << 26)) != 58u << 26)
13626 abort ();
13627 insn += (14u << 26) - (58u << 26);
13628 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13629 r_type = R_PPC64_TOC16_LO;
13630 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13631 break;
13632
13633 case R_PPC64_TOC16:
13634 case R_PPC64_TOC16_LO:
13635 case R_PPC64_TOC16_DS:
13636 case R_PPC64_TOC16_LO_DS:
13637 {
13638 /* Check for toc tls entries. */
13639 unsigned char *toc_tls;
13640 int retval;
13641
13642 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13643 &local_syms, rel, input_bfd);
13644 if (retval == 0)
13645 return FALSE;
13646
13647 if (toc_tls)
13648 {
13649 tls_mask = *toc_tls;
13650 if (r_type == R_PPC64_TOC16_DS
13651 || r_type == R_PPC64_TOC16_LO_DS)
13652 {
13653 if (tls_mask != 0
13654 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13655 goto toctprel;
13656 }
13657 else
13658 {
13659 /* If we found a GD reloc pair, then we might be
13660 doing a GD->IE transition. */
13661 if (retval == 2)
13662 {
13663 tls_gd = TLS_TPRELGD;
13664 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13665 goto tls_ldgd_opt;
13666 }
13667 else if (retval == 3)
13668 {
13669 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13670 goto tls_ldgd_opt;
13671 }
13672 }
13673 }
13674 }
13675 break;
13676
13677 case R_PPC64_GOT_TPREL16_HI:
13678 case R_PPC64_GOT_TPREL16_HA:
13679 if (tls_mask != 0
13680 && (tls_mask & TLS_TPREL) == 0)
13681 {
13682 rel->r_offset -= d_offset;
13683 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13684 r_type = R_PPC64_NONE;
13685 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13686 }
13687 break;
13688
13689 case R_PPC64_GOT_TPREL16_DS:
13690 case R_PPC64_GOT_TPREL16_LO_DS:
13691 if (tls_mask != 0
13692 && (tls_mask & TLS_TPREL) == 0)
13693 {
13694 toctprel:
13695 insn = bfd_get_32 (input_bfd,
13696 contents + rel->r_offset - d_offset);
13697 insn &= 31 << 21;
13698 insn |= 0x3c0d0000; /* addis 0,13,0 */
13699 bfd_put_32 (input_bfd, insn,
13700 contents + rel->r_offset - d_offset);
13701 r_type = R_PPC64_TPREL16_HA;
13702 if (toc_symndx != 0)
13703 {
13704 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13705 rel->r_addend = toc_addend;
13706 /* We changed the symbol. Start over in order to
13707 get h, sym, sec etc. right. */
13708 goto again;
13709 }
13710 else
13711 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13712 }
13713 break;
13714
13715 case R_PPC64_TLS:
13716 if (tls_mask != 0
13717 && (tls_mask & TLS_TPREL) == 0)
13718 {
13719 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13720 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13721 if (insn == 0)
13722 abort ();
13723 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
13724 /* Was PPC64_TLS which sits on insn boundary, now
13725 PPC64_TPREL16_LO which is at low-order half-word. */
13726 rel->r_offset += d_offset;
13727 r_type = R_PPC64_TPREL16_LO;
13728 if (toc_symndx != 0)
13729 {
13730 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13731 rel->r_addend = toc_addend;
13732 /* We changed the symbol. Start over in order to
13733 get h, sym, sec etc. right. */
13734 goto again;
13735 }
13736 else
13737 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13738 }
13739 break;
13740
13741 case R_PPC64_GOT_TLSGD16_HI:
13742 case R_PPC64_GOT_TLSGD16_HA:
13743 tls_gd = TLS_TPRELGD;
13744 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13745 goto tls_gdld_hi;
13746 break;
13747
13748 case R_PPC64_GOT_TLSLD16_HI:
13749 case R_PPC64_GOT_TLSLD16_HA:
13750 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13751 {
13752 tls_gdld_hi:
13753 if ((tls_mask & tls_gd) != 0)
13754 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13755 + R_PPC64_GOT_TPREL16_DS);
13756 else
13757 {
13758 rel->r_offset -= d_offset;
13759 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13760 r_type = R_PPC64_NONE;
13761 }
13762 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13763 }
13764 break;
13765
13766 case R_PPC64_GOT_TLSGD16:
13767 case R_PPC64_GOT_TLSGD16_LO:
13768 tls_gd = TLS_TPRELGD;
13769 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13770 goto tls_ldgd_opt;
13771 break;
13772
13773 case R_PPC64_GOT_TLSLD16:
13774 case R_PPC64_GOT_TLSLD16_LO:
13775 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13776 {
13777 unsigned int insn1, insn2;
13778 bfd_vma offset;
13779
13780 tls_ldgd_opt:
13781 offset = (bfd_vma) -1;
13782 /* If not using the newer R_PPC64_TLSGD/LD to mark
13783 __tls_get_addr calls, we must trust that the call
13784 stays with its arg setup insns, ie. that the next
13785 reloc is the __tls_get_addr call associated with
13786 the current reloc. Edit both insns. */
13787 if (input_section->has_tls_get_addr_call
13788 && rel + 1 < relend
13789 && branch_reloc_hash_match (input_bfd, rel + 1,
13790 htab->tls_get_addr,
13791 htab->tls_get_addr_fd))
13792 offset = rel[1].r_offset;
13793 /* We read the low GOT_TLS (or TOC16) insn because we
13794 need to keep the destination reg. It may be
13795 something other than the usual r3, and moved to r3
13796 before the call by intervening code. */
13797 insn1 = bfd_get_32 (input_bfd,
13798 contents + rel->r_offset - d_offset);
13799 if ((tls_mask & tls_gd) != 0)
13800 {
13801 /* IE */
13802 insn1 &= (0x1f << 21) | (0x1f << 16);
13803 insn1 |= 58 << 26; /* ld */
13804 insn2 = 0x7c636a14; /* add 3,3,13 */
13805 if (offset != (bfd_vma) -1)
13806 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13807 if ((tls_mask & TLS_EXPLICIT) == 0)
13808 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13809 + R_PPC64_GOT_TPREL16_DS);
13810 else
13811 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13812 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13813 }
13814 else
13815 {
13816 /* LE */
13817 insn1 &= 0x1f << 21;
13818 insn1 |= 0x3c0d0000; /* addis r,13,0 */
13819 insn2 = 0x38630000; /* addi 3,3,0 */
13820 if (tls_gd == 0)
13821 {
13822 /* Was an LD reloc. */
13823 if (toc_symndx)
13824 sec = local_sections[toc_symndx];
13825 for (r_symndx = 0;
13826 r_symndx < symtab_hdr->sh_info;
13827 r_symndx++)
13828 if (local_sections[r_symndx] == sec)
13829 break;
13830 if (r_symndx >= symtab_hdr->sh_info)
13831 r_symndx = STN_UNDEF;
13832 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13833 if (r_symndx != STN_UNDEF)
13834 rel->r_addend -= (local_syms[r_symndx].st_value
13835 + sec->output_offset
13836 + sec->output_section->vma);
13837 }
13838 else if (toc_symndx != 0)
13839 {
13840 r_symndx = toc_symndx;
13841 rel->r_addend = toc_addend;
13842 }
13843 r_type = R_PPC64_TPREL16_HA;
13844 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13845 if (offset != (bfd_vma) -1)
13846 {
13847 rel[1].r_info = ELF64_R_INFO (r_symndx,
13848 R_PPC64_TPREL16_LO);
13849 rel[1].r_offset = offset + d_offset;
13850 rel[1].r_addend = rel->r_addend;
13851 }
13852 }
13853 bfd_put_32 (input_bfd, insn1,
13854 contents + rel->r_offset - d_offset);
13855 if (offset != (bfd_vma) -1)
13856 bfd_put_32 (input_bfd, insn2, contents + offset);
13857 if ((tls_mask & tls_gd) == 0
13858 && (tls_gd == 0 || toc_symndx != 0))
13859 {
13860 /* We changed the symbol. Start over in order
13861 to get h, sym, sec etc. right. */
13862 goto again;
13863 }
13864 }
13865 break;
13866
13867 case R_PPC64_TLSGD:
13868 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13869 {
13870 unsigned int insn2;
13871 bfd_vma offset = rel->r_offset;
13872
13873 if ((tls_mask & TLS_TPRELGD) != 0)
13874 {
13875 /* IE */
13876 r_type = R_PPC64_NONE;
13877 insn2 = 0x7c636a14; /* add 3,3,13 */
13878 }
13879 else
13880 {
13881 /* LE */
13882 if (toc_symndx != 0)
13883 {
13884 r_symndx = toc_symndx;
13885 rel->r_addend = toc_addend;
13886 }
13887 r_type = R_PPC64_TPREL16_LO;
13888 rel->r_offset = offset + d_offset;
13889 insn2 = 0x38630000; /* addi 3,3,0 */
13890 }
13891 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13892 /* Zap the reloc on the _tls_get_addr call too. */
13893 BFD_ASSERT (offset == rel[1].r_offset);
13894 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13895 bfd_put_32 (input_bfd, insn2, contents + offset);
13896 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13897 goto again;
13898 }
13899 break;
13900
13901 case R_PPC64_TLSLD:
13902 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13903 {
13904 unsigned int insn2;
13905 bfd_vma offset = rel->r_offset;
13906
13907 if (toc_symndx)
13908 sec = local_sections[toc_symndx];
13909 for (r_symndx = 0;
13910 r_symndx < symtab_hdr->sh_info;
13911 r_symndx++)
13912 if (local_sections[r_symndx] == sec)
13913 break;
13914 if (r_symndx >= symtab_hdr->sh_info)
13915 r_symndx = STN_UNDEF;
13916 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13917 if (r_symndx != STN_UNDEF)
13918 rel->r_addend -= (local_syms[r_symndx].st_value
13919 + sec->output_offset
13920 + sec->output_section->vma);
13921
13922 r_type = R_PPC64_TPREL16_LO;
13923 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13924 rel->r_offset = offset + d_offset;
13925 /* Zap the reloc on the _tls_get_addr call too. */
13926 BFD_ASSERT (offset == rel[1].r_offset);
13927 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13928 insn2 = 0x38630000; /* addi 3,3,0 */
13929 bfd_put_32 (input_bfd, insn2, contents + offset);
13930 goto again;
13931 }
13932 break;
13933
13934 case R_PPC64_DTPMOD64:
13935 if (rel + 1 < relend
13936 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13937 && rel[1].r_offset == rel->r_offset + 8)
13938 {
13939 if ((tls_mask & TLS_GD) == 0)
13940 {
13941 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13942 if ((tls_mask & TLS_TPRELGD) != 0)
13943 r_type = R_PPC64_TPREL64;
13944 else
13945 {
13946 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13947 r_type = R_PPC64_NONE;
13948 }
13949 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13950 }
13951 }
13952 else
13953 {
13954 if ((tls_mask & TLS_LD) == 0)
13955 {
13956 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13957 r_type = R_PPC64_NONE;
13958 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13959 }
13960 }
13961 break;
13962
13963 case R_PPC64_TPREL64:
13964 if ((tls_mask & TLS_TPREL) == 0)
13965 {
13966 r_type = R_PPC64_NONE;
13967 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13968 }
13969 break;
13970
13971 case R_PPC64_ENTRY:
13972 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13973 if (!bfd_link_pic (info)
13974 && !info->traditional_format
13975 && relocation + 0x80008000 <= 0xffffffff)
13976 {
13977 unsigned int insn1, insn2;
13978
13979 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13980 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13981 if ((insn1 & ~0xfffc) == LD_R2_0R12
13982 && insn2 == ADD_R2_R2_R12)
13983 {
13984 bfd_put_32 (input_bfd,
13985 LIS_R2 + PPC_HA (relocation),
13986 contents + rel->r_offset);
13987 bfd_put_32 (input_bfd,
13988 ADDI_R2_R2 + PPC_LO (relocation),
13989 contents + rel->r_offset + 4);
13990 }
13991 }
13992 else
13993 {
13994 relocation -= (rel->r_offset
13995 + input_section->output_offset
13996 + input_section->output_section->vma);
13997 if (relocation + 0x80008000 <= 0xffffffff)
13998 {
13999 unsigned int insn1, insn2;
14000
14001 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14002 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14003 if ((insn1 & ~0xfffc) == LD_R2_0R12
14004 && insn2 == ADD_R2_R2_R12)
14005 {
14006 bfd_put_32 (input_bfd,
14007 ADDIS_R2_R12 + PPC_HA (relocation),
14008 contents + rel->r_offset);
14009 bfd_put_32 (input_bfd,
14010 ADDI_R2_R2 + PPC_LO (relocation),
14011 contents + rel->r_offset + 4);
14012 }
14013 }
14014 }
14015 break;
14016
14017 case R_PPC64_REL16_HA:
14018 /* If we are generating a non-PIC executable, edit
14019 . 0: addis 2,12,.TOC.-0b@ha
14020 . addi 2,2,.TOC.-0b@l
14021 used by ELFv2 global entry points to set up r2, to
14022 . lis 2,.TOC.@ha
14023 . addi 2,2,.TOC.@l
14024 if .TOC. is in range. */
14025 if (!bfd_link_pic (info)
14026 && !info->traditional_format
14027 && !htab->opd_abi
14028 && rel->r_addend == d_offset
14029 && h != NULL && &h->elf == htab->elf.hgot
14030 && rel + 1 < relend
14031 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14032 && rel[1].r_offset == rel->r_offset + 4
14033 && rel[1].r_addend == rel->r_addend + 4
14034 && relocation + 0x80008000 <= 0xffffffff)
14035 {
14036 unsigned int insn1, insn2;
14037 bfd_vma offset = rel->r_offset - d_offset;
14038 insn1 = bfd_get_32 (input_bfd, contents + offset);
14039 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14040 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14041 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14042 {
14043 r_type = R_PPC64_ADDR16_HA;
14044 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14045 rel->r_addend -= d_offset;
14046 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14047 rel[1].r_addend -= d_offset + 4;
14048 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14049 }
14050 }
14051 break;
14052 }
14053
14054 /* Handle other relocations that tweak non-addend part of insn. */
14055 insn = 0;
14056 max_br_offset = 1 << 25;
14057 addend = rel->r_addend;
14058 reloc_dest = DEST_NORMAL;
14059 switch (r_type)
14060 {
14061 default:
14062 break;
14063
14064 case R_PPC64_TOCSAVE:
14065 if (relocation + addend == (rel->r_offset
14066 + input_section->output_offset
14067 + input_section->output_section->vma)
14068 && tocsave_find (htab, NO_INSERT,
14069 &local_syms, rel, input_bfd))
14070 {
14071 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14072 if (insn == NOP
14073 || insn == CROR_151515 || insn == CROR_313131)
14074 bfd_put_32 (input_bfd,
14075 STD_R2_0R1 + STK_TOC (htab),
14076 contents + rel->r_offset);
14077 }
14078 break;
14079
14080 /* Branch taken prediction relocations. */
14081 case R_PPC64_ADDR14_BRTAKEN:
14082 case R_PPC64_REL14_BRTAKEN:
14083 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14084 /* Fall through. */
14085
14086 /* Branch not taken prediction relocations. */
14087 case R_PPC64_ADDR14_BRNTAKEN:
14088 case R_PPC64_REL14_BRNTAKEN:
14089 insn |= bfd_get_32 (input_bfd,
14090 contents + rel->r_offset) & ~(0x01 << 21);
14091 /* Fall through. */
14092
14093 case R_PPC64_REL14:
14094 max_br_offset = 1 << 15;
14095 /* Fall through. */
14096
14097 case R_PPC64_REL24:
14098 /* Calls to functions with a different TOC, such as calls to
14099 shared objects, need to alter the TOC pointer. This is
14100 done using a linkage stub. A REL24 branching to these
14101 linkage stubs needs to be followed by a nop, as the nop
14102 will be replaced with an instruction to restore the TOC
14103 base pointer. */
14104 fdh = h;
14105 if (h != NULL
14106 && h->oh != NULL
14107 && h->oh->is_func_descriptor)
14108 fdh = ppc_follow_link (h->oh);
14109 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14110 htab);
14111 if (stub_entry != NULL
14112 && (stub_entry->stub_type == ppc_stub_plt_call
14113 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14114 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14115 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14116 {
14117 bfd_boolean can_plt_call = FALSE;
14118
14119 if (stub_entry->stub_type == ppc_stub_plt_call
14120 && !htab->opd_abi
14121 && htab->params->plt_localentry0 != 0
14122 && is_elfv2_localentry0 (&h->elf))
14123 {
14124 /* The function doesn't use or change r2. */
14125 can_plt_call = TRUE;
14126 }
14127
14128 /* All of these stubs may modify r2, so there must be a
14129 branch and link followed by a nop. The nop is
14130 replaced by an insn to restore r2. */
14131 else if (rel->r_offset + 8 <= input_section->size)
14132 {
14133 unsigned long br;
14134
14135 br = bfd_get_32 (input_bfd,
14136 contents + rel->r_offset);
14137 if ((br & 1) != 0)
14138 {
14139 unsigned long nop;
14140
14141 nop = bfd_get_32 (input_bfd,
14142 contents + rel->r_offset + 4);
14143 if (nop == NOP
14144 || nop == CROR_151515 || nop == CROR_313131)
14145 {
14146 if (h != NULL
14147 && (h == htab->tls_get_addr_fd
14148 || h == htab->tls_get_addr)
14149 && htab->params->tls_get_addr_opt)
14150 {
14151 /* Special stub used, leave nop alone. */
14152 }
14153 else
14154 bfd_put_32 (input_bfd,
14155 LD_R2_0R1 + STK_TOC (htab),
14156 contents + rel->r_offset + 4);
14157 can_plt_call = TRUE;
14158 }
14159 }
14160 }
14161
14162 if (!can_plt_call && h != NULL)
14163 {
14164 const char *name = h->elf.root.root.string;
14165
14166 if (*name == '.')
14167 ++name;
14168
14169 if (strncmp (name, "__libc_start_main", 17) == 0
14170 && (name[17] == 0 || name[17] == '@'))
14171 {
14172 /* Allow crt1 branch to go via a toc adjusting
14173 stub. Other calls that never return could do
14174 the same, if we could detect such. */
14175 can_plt_call = TRUE;
14176 }
14177 }
14178
14179 if (!can_plt_call)
14180 {
14181 /* g++ as of 20130507 emits self-calls without a
14182 following nop. This is arguably wrong since we
14183 have conflicting information. On the one hand a
14184 global symbol and on the other a local call
14185 sequence, but don't error for this special case.
14186 It isn't possible to cheaply verify we have
14187 exactly such a call. Allow all calls to the same
14188 section. */
14189 asection *code_sec = sec;
14190
14191 if (get_opd_info (sec) != NULL)
14192 {
14193 bfd_vma off = (relocation + addend
14194 - sec->output_section->vma
14195 - sec->output_offset);
14196
14197 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14198 }
14199 if (code_sec == input_section)
14200 can_plt_call = TRUE;
14201 }
14202
14203 if (!can_plt_call)
14204 {
14205 if (stub_entry->stub_type == ppc_stub_plt_call
14206 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14207 info->callbacks->einfo
14208 /* xgettext:c-format */
14209 (_("%H: call to `%T' lacks nop, can't restore toc; "
14210 "recompile with -fPIC\n"),
14211 input_bfd, input_section, rel->r_offset, sym_name);
14212 else
14213 info->callbacks->einfo
14214 /* xgettext:c-format */
14215 (_("%H: call to `%T' lacks nop, can't restore toc; "
14216 "(-mcmodel=small toc adjust stub)\n"),
14217 input_bfd, input_section, rel->r_offset, sym_name);
14218
14219 bfd_set_error (bfd_error_bad_value);
14220 ret = FALSE;
14221 }
14222
14223 if (can_plt_call
14224 && (stub_entry->stub_type == ppc_stub_plt_call
14225 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14226 unresolved_reloc = FALSE;
14227 }
14228
14229 if ((stub_entry == NULL
14230 || stub_entry->stub_type == ppc_stub_long_branch
14231 || stub_entry->stub_type == ppc_stub_plt_branch)
14232 && get_opd_info (sec) != NULL)
14233 {
14234 /* The branch destination is the value of the opd entry. */
14235 bfd_vma off = (relocation + addend
14236 - sec->output_section->vma
14237 - sec->output_offset);
14238 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14239 if (dest != (bfd_vma) -1)
14240 {
14241 relocation = dest;
14242 addend = 0;
14243 reloc_dest = DEST_OPD;
14244 }
14245 }
14246
14247 /* If the branch is out of reach we ought to have a long
14248 branch stub. */
14249 from = (rel->r_offset
14250 + input_section->output_offset
14251 + input_section->output_section->vma);
14252
14253 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14254 ? fdh->elf.other
14255 : sym->st_other);
14256
14257 if (stub_entry != NULL
14258 && (stub_entry->stub_type == ppc_stub_long_branch
14259 || stub_entry->stub_type == ppc_stub_plt_branch)
14260 && (r_type == R_PPC64_ADDR14_BRTAKEN
14261 || r_type == R_PPC64_ADDR14_BRNTAKEN
14262 || (relocation + addend - from + max_br_offset
14263 < 2 * max_br_offset)))
14264 /* Don't use the stub if this branch is in range. */
14265 stub_entry = NULL;
14266
14267 if (stub_entry != NULL)
14268 {
14269 /* Munge up the value and addend so that we call the stub
14270 rather than the procedure directly. */
14271 asection *stub_sec = stub_entry->group->stub_sec;
14272
14273 if (stub_entry->stub_type == ppc_stub_save_res)
14274 relocation += (stub_sec->output_offset
14275 + stub_sec->output_section->vma
14276 + stub_sec->size - htab->sfpr->size
14277 - htab->sfpr->output_offset
14278 - htab->sfpr->output_section->vma);
14279 else
14280 relocation = (stub_entry->stub_offset
14281 + stub_sec->output_offset
14282 + stub_sec->output_section->vma);
14283 addend = 0;
14284 reloc_dest = DEST_STUB;
14285
14286 if ((stub_entry->stub_type == ppc_stub_plt_call
14287 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14288 && (ALWAYS_EMIT_R2SAVE
14289 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14290 && rel + 1 < relend
14291 && rel[1].r_offset == rel->r_offset + 4
14292 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14293 relocation += 4;
14294 }
14295
14296 if (insn != 0)
14297 {
14298 if (is_isa_v2)
14299 {
14300 /* Set 'a' bit. This is 0b00010 in BO field for branch
14301 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14302 for branch on CTR insns (BO == 1a00t or 1a01t). */
14303 if ((insn & (0x14 << 21)) == (0x04 << 21))
14304 insn |= 0x02 << 21;
14305 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14306 insn |= 0x08 << 21;
14307 else
14308 break;
14309 }
14310 else
14311 {
14312 /* Invert 'y' bit if not the default. */
14313 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14314 insn ^= 0x01 << 21;
14315 }
14316
14317 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14318 }
14319
14320 /* NOP out calls to undefined weak functions.
14321 We can thus call a weak function without first
14322 checking whether the function is defined. */
14323 else if (h != NULL
14324 && h->elf.root.type == bfd_link_hash_undefweak
14325 && h->elf.dynindx == -1
14326 && r_type == R_PPC64_REL24
14327 && relocation == 0
14328 && addend == 0)
14329 {
14330 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14331 goto copy_reloc;
14332 }
14333 break;
14334 }
14335
14336 /* Set `addend'. */
14337 tls_type = 0;
14338 switch (r_type)
14339 {
14340 default:
14341 info->callbacks->einfo
14342 /* xgettext:c-format */
14343 (_("%P: %B: unknown relocation type %d for `%T'\n"),
14344 input_bfd, (int) r_type, sym_name);
14345
14346 bfd_set_error (bfd_error_bad_value);
14347 ret = FALSE;
14348 goto copy_reloc;
14349
14350 case R_PPC64_NONE:
14351 case R_PPC64_TLS:
14352 case R_PPC64_TLSGD:
14353 case R_PPC64_TLSLD:
14354 case R_PPC64_TOCSAVE:
14355 case R_PPC64_GNU_VTINHERIT:
14356 case R_PPC64_GNU_VTENTRY:
14357 case R_PPC64_ENTRY:
14358 goto copy_reloc;
14359
14360 /* GOT16 relocations. Like an ADDR16 using the symbol's
14361 address in the GOT as relocation value instead of the
14362 symbol's value itself. Also, create a GOT entry for the
14363 symbol and put the symbol value there. */
14364 case R_PPC64_GOT_TLSGD16:
14365 case R_PPC64_GOT_TLSGD16_LO:
14366 case R_PPC64_GOT_TLSGD16_HI:
14367 case R_PPC64_GOT_TLSGD16_HA:
14368 tls_type = TLS_TLS | TLS_GD;
14369 goto dogot;
14370
14371 case R_PPC64_GOT_TLSLD16:
14372 case R_PPC64_GOT_TLSLD16_LO:
14373 case R_PPC64_GOT_TLSLD16_HI:
14374 case R_PPC64_GOT_TLSLD16_HA:
14375 tls_type = TLS_TLS | TLS_LD;
14376 goto dogot;
14377
14378 case R_PPC64_GOT_TPREL16_DS:
14379 case R_PPC64_GOT_TPREL16_LO_DS:
14380 case R_PPC64_GOT_TPREL16_HI:
14381 case R_PPC64_GOT_TPREL16_HA:
14382 tls_type = TLS_TLS | TLS_TPREL;
14383 goto dogot;
14384
14385 case R_PPC64_GOT_DTPREL16_DS:
14386 case R_PPC64_GOT_DTPREL16_LO_DS:
14387 case R_PPC64_GOT_DTPREL16_HI:
14388 case R_PPC64_GOT_DTPREL16_HA:
14389 tls_type = TLS_TLS | TLS_DTPREL;
14390 goto dogot;
14391
14392 case R_PPC64_GOT16:
14393 case R_PPC64_GOT16_LO:
14394 case R_PPC64_GOT16_HI:
14395 case R_PPC64_GOT16_HA:
14396 case R_PPC64_GOT16_DS:
14397 case R_PPC64_GOT16_LO_DS:
14398 dogot:
14399 {
14400 /* Relocation is to the entry for this symbol in the global
14401 offset table. */
14402 asection *got;
14403 bfd_vma *offp;
14404 bfd_vma off;
14405 unsigned long indx = 0;
14406 struct got_entry *ent;
14407
14408 if (tls_type == (TLS_TLS | TLS_LD)
14409 && (h == NULL
14410 || !h->elf.def_dynamic))
14411 ent = ppc64_tlsld_got (input_bfd);
14412 else
14413 {
14414 if (h != NULL)
14415 {
14416 if (!htab->elf.dynamic_sections_created
14417 || h->elf.dynindx == -1
14418 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14419 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
14420 /* This is actually a static link, or it is a
14421 -Bsymbolic link and the symbol is defined
14422 locally, or the symbol was forced to be local
14423 because of a version file. */
14424 ;
14425 else
14426 {
14427 indx = h->elf.dynindx;
14428 unresolved_reloc = FALSE;
14429 }
14430 ent = h->elf.got.glist;
14431 }
14432 else
14433 {
14434 if (local_got_ents == NULL)
14435 abort ();
14436 ent = local_got_ents[r_symndx];
14437 }
14438
14439 for (; ent != NULL; ent = ent->next)
14440 if (ent->addend == orig_rel.r_addend
14441 && ent->owner == input_bfd
14442 && ent->tls_type == tls_type)
14443 break;
14444 }
14445
14446 if (ent == NULL)
14447 abort ();
14448 if (ent->is_indirect)
14449 ent = ent->got.ent;
14450 offp = &ent->got.offset;
14451 got = ppc64_elf_tdata (ent->owner)->got;
14452 if (got == NULL)
14453 abort ();
14454
14455 /* The offset must always be a multiple of 8. We use the
14456 least significant bit to record whether we have already
14457 processed this entry. */
14458 off = *offp;
14459 if ((off & 1) != 0)
14460 off &= ~1;
14461 else
14462 {
14463 /* Generate relocs for the dynamic linker, except in
14464 the case of TLSLD where we'll use one entry per
14465 module. */
14466 asection *relgot;
14467 bfd_boolean ifunc;
14468
14469 *offp = off | 1;
14470 relgot = NULL;
14471 ifunc = (h != NULL
14472 ? h->elf.type == STT_GNU_IFUNC
14473 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14474 if (ifunc)
14475 {
14476 relgot = htab->elf.irelplt;
14477 if (indx == 0)
14478 htab->local_ifunc_resolver = 1;
14479 else if (is_static_defined (&h->elf))
14480 htab->maybe_local_ifunc_resolver = 1;
14481 }
14482 else if (indx != 0
14483 || (bfd_link_pic (info)
14484 && (h == NULL
14485 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
14486 || (tls_type == (TLS_TLS | TLS_LD)
14487 && !h->elf.def_dynamic))))
14488 relgot = ppc64_elf_tdata (ent->owner)->relgot;
14489 if (relgot != NULL)
14490 {
14491 outrel.r_offset = (got->output_section->vma
14492 + got->output_offset
14493 + off);
14494 outrel.r_addend = addend;
14495 if (tls_type & (TLS_LD | TLS_GD))
14496 {
14497 outrel.r_addend = 0;
14498 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14499 if (tls_type == (TLS_TLS | TLS_GD))
14500 {
14501 loc = relgot->contents;
14502 loc += (relgot->reloc_count++
14503 * sizeof (Elf64_External_Rela));
14504 bfd_elf64_swap_reloca_out (output_bfd,
14505 &outrel, loc);
14506 outrel.r_offset += 8;
14507 outrel.r_addend = addend;
14508 outrel.r_info
14509 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14510 }
14511 }
14512 else if (tls_type == (TLS_TLS | TLS_DTPREL))
14513 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14514 else if (tls_type == (TLS_TLS | TLS_TPREL))
14515 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14516 else if (indx != 0)
14517 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14518 else
14519 {
14520 if (ifunc)
14521 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14522 else
14523 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14524
14525 /* Write the .got section contents for the sake
14526 of prelink. */
14527 loc = got->contents + off;
14528 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14529 loc);
14530 }
14531
14532 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14533 {
14534 outrel.r_addend += relocation;
14535 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14536 {
14537 if (htab->elf.tls_sec == NULL)
14538 outrel.r_addend = 0;
14539 else
14540 outrel.r_addend -= htab->elf.tls_sec->vma;
14541 }
14542 }
14543 loc = relgot->contents;
14544 loc += (relgot->reloc_count++
14545 * sizeof (Elf64_External_Rela));
14546 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14547 }
14548
14549 /* Init the .got section contents here if we're not
14550 emitting a reloc. */
14551 else
14552 {
14553 relocation += addend;
14554 if (tls_type != 0)
14555 {
14556 if (htab->elf.tls_sec == NULL)
14557 relocation = 0;
14558 else
14559 {
14560 if (tls_type & TLS_LD)
14561 relocation = 0;
14562 else
14563 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14564 if (tls_type & TLS_TPREL)
14565 relocation += DTP_OFFSET - TP_OFFSET;
14566 }
14567
14568 if (tls_type & (TLS_GD | TLS_LD))
14569 {
14570 bfd_put_64 (output_bfd, relocation,
14571 got->contents + off + 8);
14572 relocation = 1;
14573 }
14574 }
14575 bfd_put_64 (output_bfd, relocation,
14576 got->contents + off);
14577 }
14578 }
14579
14580 if (off >= (bfd_vma) -2)
14581 abort ();
14582
14583 relocation = got->output_section->vma + got->output_offset + off;
14584 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14585 }
14586 break;
14587
14588 case R_PPC64_PLT16_HA:
14589 case R_PPC64_PLT16_HI:
14590 case R_PPC64_PLT16_LO:
14591 case R_PPC64_PLT32:
14592 case R_PPC64_PLT64:
14593 /* Relocation is to the entry for this symbol in the
14594 procedure linkage table. */
14595 {
14596 struct plt_entry **plt_list = NULL;
14597 if (h != NULL)
14598 plt_list = &h->elf.plt.plist;
14599 else if (local_got_ents != NULL)
14600 {
14601 struct plt_entry **local_plt = (struct plt_entry **)
14602 (local_got_ents + symtab_hdr->sh_info);
14603 unsigned char *local_got_tls_masks = (unsigned char *)
14604 (local_plt + symtab_hdr->sh_info);
14605 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14606 plt_list = local_plt + r_symndx;
14607 }
14608 if (plt_list)
14609 {
14610 struct plt_entry *ent;
14611
14612 for (ent = *plt_list; ent != NULL; ent = ent->next)
14613 if (ent->plt.offset != (bfd_vma) -1
14614 && ent->addend == orig_rel.r_addend)
14615 {
14616 asection *plt;
14617
14618 plt = htab->elf.splt;
14619 if (!htab->elf.dynamic_sections_created
14620 || h == NULL
14621 || h->elf.dynindx == -1)
14622 plt = htab->elf.iplt;
14623 relocation = (plt->output_section->vma
14624 + plt->output_offset
14625 + ent->plt.offset);
14626 addend = 0;
14627 unresolved_reloc = FALSE;
14628 break;
14629 }
14630 }
14631 }
14632 break;
14633
14634 case R_PPC64_TOC:
14635 /* Relocation value is TOC base. */
14636 relocation = TOCstart;
14637 if (r_symndx == STN_UNDEF)
14638 relocation += htab->sec_info[input_section->id].toc_off;
14639 else if (unresolved_reloc)
14640 ;
14641 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14642 relocation += htab->sec_info[sec->id].toc_off;
14643 else
14644 unresolved_reloc = TRUE;
14645 goto dodyn;
14646
14647 /* TOC16 relocs. We want the offset relative to the TOC base,
14648 which is the address of the start of the TOC plus 0x8000.
14649 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14650 in this order. */
14651 case R_PPC64_TOC16:
14652 case R_PPC64_TOC16_LO:
14653 case R_PPC64_TOC16_HI:
14654 case R_PPC64_TOC16_DS:
14655 case R_PPC64_TOC16_LO_DS:
14656 case R_PPC64_TOC16_HA:
14657 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14658 break;
14659
14660 /* Relocate against the beginning of the section. */
14661 case R_PPC64_SECTOFF:
14662 case R_PPC64_SECTOFF_LO:
14663 case R_PPC64_SECTOFF_HI:
14664 case R_PPC64_SECTOFF_DS:
14665 case R_PPC64_SECTOFF_LO_DS:
14666 case R_PPC64_SECTOFF_HA:
14667 if (sec != NULL)
14668 addend -= sec->output_section->vma;
14669 break;
14670
14671 case R_PPC64_REL16:
14672 case R_PPC64_REL16_LO:
14673 case R_PPC64_REL16_HI:
14674 case R_PPC64_REL16_HA:
14675 case R_PPC64_REL16DX_HA:
14676 break;
14677
14678 case R_PPC64_REL14:
14679 case R_PPC64_REL14_BRNTAKEN:
14680 case R_PPC64_REL14_BRTAKEN:
14681 case R_PPC64_REL24:
14682 break;
14683
14684 case R_PPC64_TPREL16:
14685 case R_PPC64_TPREL16_LO:
14686 case R_PPC64_TPREL16_HI:
14687 case R_PPC64_TPREL16_HA:
14688 case R_PPC64_TPREL16_DS:
14689 case R_PPC64_TPREL16_LO_DS:
14690 case R_PPC64_TPREL16_HIGH:
14691 case R_PPC64_TPREL16_HIGHA:
14692 case R_PPC64_TPREL16_HIGHER:
14693 case R_PPC64_TPREL16_HIGHERA:
14694 case R_PPC64_TPREL16_HIGHEST:
14695 case R_PPC64_TPREL16_HIGHESTA:
14696 if (h != NULL
14697 && h->elf.root.type == bfd_link_hash_undefweak
14698 && h->elf.dynindx == -1)
14699 {
14700 /* Make this relocation against an undefined weak symbol
14701 resolve to zero. This is really just a tweak, since
14702 code using weak externs ought to check that they are
14703 defined before using them. */
14704 bfd_byte *p = contents + rel->r_offset - d_offset;
14705
14706 insn = bfd_get_32 (input_bfd, p);
14707 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14708 if (insn != 0)
14709 bfd_put_32 (input_bfd, insn, p);
14710 break;
14711 }
14712 if (htab->elf.tls_sec != NULL)
14713 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14714 /* The TPREL16 relocs shouldn't really be used in shared
14715 libs or with non-local symbols as that will result in
14716 DT_TEXTREL being set, but support them anyway. */
14717 goto dodyn;
14718
14719 case R_PPC64_DTPREL16:
14720 case R_PPC64_DTPREL16_LO:
14721 case R_PPC64_DTPREL16_HI:
14722 case R_PPC64_DTPREL16_HA:
14723 case R_PPC64_DTPREL16_DS:
14724 case R_PPC64_DTPREL16_LO_DS:
14725 case R_PPC64_DTPREL16_HIGH:
14726 case R_PPC64_DTPREL16_HIGHA:
14727 case R_PPC64_DTPREL16_HIGHER:
14728 case R_PPC64_DTPREL16_HIGHERA:
14729 case R_PPC64_DTPREL16_HIGHEST:
14730 case R_PPC64_DTPREL16_HIGHESTA:
14731 if (htab->elf.tls_sec != NULL)
14732 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14733 break;
14734
14735 case R_PPC64_ADDR64_LOCAL:
14736 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14737 ? h->elf.other
14738 : sym->st_other);
14739 break;
14740
14741 case R_PPC64_DTPMOD64:
14742 relocation = 1;
14743 addend = 0;
14744 goto dodyn;
14745
14746 case R_PPC64_TPREL64:
14747 if (htab->elf.tls_sec != NULL)
14748 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14749 goto dodyn;
14750
14751 case R_PPC64_DTPREL64:
14752 if (htab->elf.tls_sec != NULL)
14753 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14754 /* Fall through. */
14755
14756 /* Relocations that may need to be propagated if this is a
14757 dynamic object. */
14758 case R_PPC64_REL30:
14759 case R_PPC64_REL32:
14760 case R_PPC64_REL64:
14761 case R_PPC64_ADDR14:
14762 case R_PPC64_ADDR14_BRNTAKEN:
14763 case R_PPC64_ADDR14_BRTAKEN:
14764 case R_PPC64_ADDR16:
14765 case R_PPC64_ADDR16_DS:
14766 case R_PPC64_ADDR16_HA:
14767 case R_PPC64_ADDR16_HI:
14768 case R_PPC64_ADDR16_HIGH:
14769 case R_PPC64_ADDR16_HIGHA:
14770 case R_PPC64_ADDR16_HIGHER:
14771 case R_PPC64_ADDR16_HIGHERA:
14772 case R_PPC64_ADDR16_HIGHEST:
14773 case R_PPC64_ADDR16_HIGHESTA:
14774 case R_PPC64_ADDR16_LO:
14775 case R_PPC64_ADDR16_LO_DS:
14776 case R_PPC64_ADDR24:
14777 case R_PPC64_ADDR32:
14778 case R_PPC64_ADDR64:
14779 case R_PPC64_UADDR16:
14780 case R_PPC64_UADDR32:
14781 case R_PPC64_UADDR64:
14782 dodyn:
14783 if ((input_section->flags & SEC_ALLOC) == 0)
14784 break;
14785
14786 if (NO_OPD_RELOCS && is_opd)
14787 break;
14788
14789 if (bfd_link_pic (info)
14790 ? ((h == NULL
14791 || h->dyn_relocs != NULL)
14792 && ((h != NULL && pc_dynrelocs (h))
14793 || must_be_dyn_reloc (info, r_type)))
14794 : (h != NULL
14795 ? h->dyn_relocs != NULL
14796 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14797 {
14798 bfd_boolean skip, relocate;
14799 asection *sreloc;
14800 bfd_vma out_off;
14801 long indx = 0;
14802
14803 /* When generating a dynamic object, these relocations
14804 are copied into the output file to be resolved at run
14805 time. */
14806
14807 skip = FALSE;
14808 relocate = FALSE;
14809
14810 out_off = _bfd_elf_section_offset (output_bfd, info,
14811 input_section, rel->r_offset);
14812 if (out_off == (bfd_vma) -1)
14813 skip = TRUE;
14814 else if (out_off == (bfd_vma) -2)
14815 skip = TRUE, relocate = TRUE;
14816 out_off += (input_section->output_section->vma
14817 + input_section->output_offset);
14818 outrel.r_offset = out_off;
14819 outrel.r_addend = rel->r_addend;
14820
14821 /* Optimize unaligned reloc use. */
14822 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14823 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14824 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14825 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14826 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14827 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14828 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14829 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14830 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14831
14832 if (skip)
14833 memset (&outrel, 0, sizeof outrel);
14834 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14835 && !is_opd
14836 && r_type != R_PPC64_TOC)
14837 {
14838 indx = h->elf.dynindx;
14839 BFD_ASSERT (indx != -1);
14840 outrel.r_info = ELF64_R_INFO (indx, r_type);
14841 }
14842 else
14843 {
14844 /* This symbol is local, or marked to become local,
14845 or this is an opd section reloc which must point
14846 at a local function. */
14847 outrel.r_addend += relocation;
14848 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14849 {
14850 if (is_opd && h != NULL)
14851 {
14852 /* Lie about opd entries. This case occurs
14853 when building shared libraries and we
14854 reference a function in another shared
14855 lib. The same thing happens for a weak
14856 definition in an application that's
14857 overridden by a strong definition in a
14858 shared lib. (I believe this is a generic
14859 bug in binutils handling of weak syms.)
14860 In these cases we won't use the opd
14861 entry in this lib. */
14862 unresolved_reloc = FALSE;
14863 }
14864 if (!is_opd
14865 && r_type == R_PPC64_ADDR64
14866 && (h != NULL
14867 ? h->elf.type == STT_GNU_IFUNC
14868 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14869 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14870 else
14871 {
14872 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14873
14874 /* We need to relocate .opd contents for ld.so.
14875 Prelink also wants simple and consistent rules
14876 for relocs. This make all RELATIVE relocs have
14877 *r_offset equal to r_addend. */
14878 relocate = TRUE;
14879 }
14880 }
14881 else
14882 {
14883 if (h != NULL
14884 ? h->elf.type == STT_GNU_IFUNC
14885 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14886 {
14887 info->callbacks->einfo
14888 /* xgettext:c-format */
14889 (_("%H: %s for indirect "
14890 "function `%T' unsupported\n"),
14891 input_bfd, input_section, rel->r_offset,
14892 ppc64_elf_howto_table[r_type]->name,
14893 sym_name);
14894 ret = FALSE;
14895 }
14896 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14897 ;
14898 else if (sec == NULL || sec->owner == NULL)
14899 {
14900 bfd_set_error (bfd_error_bad_value);
14901 return FALSE;
14902 }
14903 else
14904 {
14905 asection *osec;
14906
14907 osec = sec->output_section;
14908 indx = elf_section_data (osec)->dynindx;
14909
14910 if (indx == 0)
14911 {
14912 if ((osec->flags & SEC_READONLY) == 0
14913 && htab->elf.data_index_section != NULL)
14914 osec = htab->elf.data_index_section;
14915 else
14916 osec = htab->elf.text_index_section;
14917 indx = elf_section_data (osec)->dynindx;
14918 }
14919 BFD_ASSERT (indx != 0);
14920
14921 /* We are turning this relocation into one
14922 against a section symbol, so subtract out
14923 the output section's address but not the
14924 offset of the input section in the output
14925 section. */
14926 outrel.r_addend -= osec->vma;
14927 }
14928
14929 outrel.r_info = ELF64_R_INFO (indx, r_type);
14930 }
14931 }
14932
14933 sreloc = elf_section_data (input_section)->sreloc;
14934 if (h != NULL
14935 ? h->elf.type == STT_GNU_IFUNC
14936 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14937 {
14938 sreloc = htab->elf.irelplt;
14939 if (indx == 0)
14940 htab->local_ifunc_resolver = 1;
14941 else if (is_static_defined (&h->elf))
14942 htab->maybe_local_ifunc_resolver = 1;
14943 }
14944 if (sreloc == NULL)
14945 abort ();
14946
14947 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14948 >= sreloc->size)
14949 abort ();
14950 loc = sreloc->contents;
14951 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14952 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14953
14954 /* If this reloc is against an external symbol, it will
14955 be computed at runtime, so there's no need to do
14956 anything now. However, for the sake of prelink ensure
14957 that the section contents are a known value. */
14958 if (! relocate)
14959 {
14960 unresolved_reloc = FALSE;
14961 /* The value chosen here is quite arbitrary as ld.so
14962 ignores section contents except for the special
14963 case of .opd where the contents might be accessed
14964 before relocation. Choose zero, as that won't
14965 cause reloc overflow. */
14966 relocation = 0;
14967 addend = 0;
14968 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14969 to improve backward compatibility with older
14970 versions of ld. */
14971 if (r_type == R_PPC64_ADDR64)
14972 addend = outrel.r_addend;
14973 /* Adjust pc_relative relocs to have zero in *r_offset. */
14974 else if (ppc64_elf_howto_table[r_type]->pc_relative)
14975 addend = outrel.r_offset;
14976 }
14977 }
14978 break;
14979
14980 case R_PPC64_COPY:
14981 case R_PPC64_GLOB_DAT:
14982 case R_PPC64_JMP_SLOT:
14983 case R_PPC64_JMP_IREL:
14984 case R_PPC64_RELATIVE:
14985 /* We shouldn't ever see these dynamic relocs in relocatable
14986 files. */
14987 /* Fall through. */
14988
14989 case R_PPC64_PLTGOT16:
14990 case R_PPC64_PLTGOT16_DS:
14991 case R_PPC64_PLTGOT16_HA:
14992 case R_PPC64_PLTGOT16_HI:
14993 case R_PPC64_PLTGOT16_LO:
14994 case R_PPC64_PLTGOT16_LO_DS:
14995 case R_PPC64_PLTREL32:
14996 case R_PPC64_PLTREL64:
14997 /* These ones haven't been implemented yet. */
14998
14999 info->callbacks->einfo
15000 /* xgettext:c-format */
15001 (_("%P: %B: %s is not supported for `%T'\n"),
15002 input_bfd,
15003 ppc64_elf_howto_table[r_type]->name, sym_name);
15004
15005 bfd_set_error (bfd_error_invalid_operation);
15006 ret = FALSE;
15007 goto copy_reloc;
15008 }
15009
15010 /* Multi-instruction sequences that access the TOC can be
15011 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15012 to nop; addi rb,r2,x; */
15013 howto = ppc64_elf_howto_table[(int) r_type];
15014 switch (r_type)
15015 {
15016 default:
15017 break;
15018
15019 case R_PPC64_GOT_TLSLD16_HI:
15020 case R_PPC64_GOT_TLSGD16_HI:
15021 case R_PPC64_GOT_TPREL16_HI:
15022 case R_PPC64_GOT_DTPREL16_HI:
15023 case R_PPC64_GOT16_HI:
15024 case R_PPC64_TOC16_HI:
15025 /* These relocs would only be useful if building up an
15026 offset to later add to r2, perhaps in an indexed
15027 addressing mode instruction. Don't try to optimize.
15028 Unfortunately, the possibility of someone building up an
15029 offset like this or even with the HA relocs, means that
15030 we need to check the high insn when optimizing the low
15031 insn. */
15032 break;
15033
15034 case R_PPC64_GOT_TLSLD16_HA:
15035 case R_PPC64_GOT_TLSGD16_HA:
15036 case R_PPC64_GOT_TPREL16_HA:
15037 case R_PPC64_GOT_DTPREL16_HA:
15038 case R_PPC64_GOT16_HA:
15039 case R_PPC64_TOC16_HA:
15040 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15041 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15042 {
15043 bfd_byte *p = contents + (rel->r_offset & ~3);
15044 bfd_put_32 (input_bfd, NOP, p);
15045 }
15046 break;
15047
15048 case R_PPC64_GOT_TLSLD16_LO:
15049 case R_PPC64_GOT_TLSGD16_LO:
15050 case R_PPC64_GOT_TPREL16_LO_DS:
15051 case R_PPC64_GOT_DTPREL16_LO_DS:
15052 case R_PPC64_GOT16_LO:
15053 case R_PPC64_GOT16_LO_DS:
15054 case R_PPC64_TOC16_LO:
15055 case R_PPC64_TOC16_LO_DS:
15056 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15057 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15058 {
15059 bfd_byte *p = contents + (rel->r_offset & ~3);
15060 insn = bfd_get_32 (input_bfd, p);
15061 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15062 {
15063 /* Transform addic to addi when we change reg. */
15064 insn &= ~((0x3f << 26) | (0x1f << 16));
15065 insn |= (14u << 26) | (2 << 16);
15066 }
15067 else
15068 {
15069 insn &= ~(0x1f << 16);
15070 insn |= 2 << 16;
15071 }
15072 bfd_put_32 (input_bfd, insn, p);
15073 }
15074 break;
15075
15076 case R_PPC64_TPREL16_HA:
15077 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15078 {
15079 bfd_byte *p = contents + (rel->r_offset & ~3);
15080 insn = bfd_get_32 (input_bfd, p);
15081 if ((insn & ((0x3f << 26) | 0x1f << 16))
15082 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15083 /* xgettext:c-format */
15084 info->callbacks->minfo
15085 (_("%H: warning: %s unexpected insn %#x.\n"),
15086 input_bfd, input_section, rel->r_offset, howto->name, insn);
15087 else
15088 bfd_put_32 (input_bfd, NOP, p);
15089 }
15090 break;
15091
15092 case R_PPC64_TPREL16_LO:
15093 case R_PPC64_TPREL16_LO_DS:
15094 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15095 {
15096 bfd_byte *p = contents + (rel->r_offset & ~3);
15097 insn = bfd_get_32 (input_bfd, p);
15098 insn &= ~(0x1f << 16);
15099 insn |= 13 << 16;
15100 bfd_put_32 (input_bfd, insn, p);
15101 }
15102 break;
15103 }
15104
15105 /* Do any further special processing. */
15106 switch (r_type)
15107 {
15108 default:
15109 break;
15110
15111 case R_PPC64_REL16_HA:
15112 case R_PPC64_REL16DX_HA:
15113 case R_PPC64_ADDR16_HA:
15114 case R_PPC64_ADDR16_HIGHA:
15115 case R_PPC64_ADDR16_HIGHERA:
15116 case R_PPC64_ADDR16_HIGHESTA:
15117 case R_PPC64_TOC16_HA:
15118 case R_PPC64_SECTOFF_HA:
15119 case R_PPC64_TPREL16_HA:
15120 case R_PPC64_TPREL16_HIGHA:
15121 case R_PPC64_TPREL16_HIGHERA:
15122 case R_PPC64_TPREL16_HIGHESTA:
15123 case R_PPC64_DTPREL16_HA:
15124 case R_PPC64_DTPREL16_HIGHA:
15125 case R_PPC64_DTPREL16_HIGHERA:
15126 case R_PPC64_DTPREL16_HIGHESTA:
15127 /* It's just possible that this symbol is a weak symbol
15128 that's not actually defined anywhere. In that case,
15129 'sec' would be NULL, and we should leave the symbol
15130 alone (it will be set to zero elsewhere in the link). */
15131 if (sec == NULL)
15132 break;
15133 /* Fall through. */
15134
15135 case R_PPC64_GOT16_HA:
15136 case R_PPC64_PLTGOT16_HA:
15137 case R_PPC64_PLT16_HA:
15138 case R_PPC64_GOT_TLSGD16_HA:
15139 case R_PPC64_GOT_TLSLD16_HA:
15140 case R_PPC64_GOT_TPREL16_HA:
15141 case R_PPC64_GOT_DTPREL16_HA:
15142 /* Add 0x10000 if sign bit in 0:15 is set.
15143 Bits 0:15 are not used. */
15144 addend += 0x8000;
15145 break;
15146
15147 case R_PPC64_ADDR16_DS:
15148 case R_PPC64_ADDR16_LO_DS:
15149 case R_PPC64_GOT16_DS:
15150 case R_PPC64_GOT16_LO_DS:
15151 case R_PPC64_PLT16_LO_DS:
15152 case R_PPC64_SECTOFF_DS:
15153 case R_PPC64_SECTOFF_LO_DS:
15154 case R_PPC64_TOC16_DS:
15155 case R_PPC64_TOC16_LO_DS:
15156 case R_PPC64_PLTGOT16_DS:
15157 case R_PPC64_PLTGOT16_LO_DS:
15158 case R_PPC64_GOT_TPREL16_DS:
15159 case R_PPC64_GOT_TPREL16_LO_DS:
15160 case R_PPC64_GOT_DTPREL16_DS:
15161 case R_PPC64_GOT_DTPREL16_LO_DS:
15162 case R_PPC64_TPREL16_DS:
15163 case R_PPC64_TPREL16_LO_DS:
15164 case R_PPC64_DTPREL16_DS:
15165 case R_PPC64_DTPREL16_LO_DS:
15166 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15167 mask = 3;
15168 /* If this reloc is against an lq, lxv, or stxv insn, then
15169 the value must be a multiple of 16. This is somewhat of
15170 a hack, but the "correct" way to do this by defining _DQ
15171 forms of all the _DS relocs bloats all reloc switches in
15172 this file. It doesn't make much sense to use these
15173 relocs in data, so testing the insn should be safe. */
15174 if ((insn & (0x3f << 26)) == (56u << 26)
15175 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15176 mask = 15;
15177 relocation += addend;
15178 addend = insn & (mask ^ 3);
15179 if ((relocation & mask) != 0)
15180 {
15181 relocation ^= relocation & mask;
15182 info->callbacks->einfo
15183 /* xgettext:c-format */
15184 (_("%H: error: %s not a multiple of %u\n"),
15185 input_bfd, input_section, rel->r_offset,
15186 howto->name,
15187 mask + 1);
15188 bfd_set_error (bfd_error_bad_value);
15189 ret = FALSE;
15190 goto copy_reloc;
15191 }
15192 break;
15193 }
15194
15195 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15196 because such sections are not SEC_ALLOC and thus ld.so will
15197 not process them. */
15198 if (unresolved_reloc
15199 && !((input_section->flags & SEC_DEBUGGING) != 0
15200 && h->elf.def_dynamic)
15201 && _bfd_elf_section_offset (output_bfd, info, input_section,
15202 rel->r_offset) != (bfd_vma) -1)
15203 {
15204 info->callbacks->einfo
15205 /* xgettext:c-format */
15206 (_("%H: unresolvable %s against `%T'\n"),
15207 input_bfd, input_section, rel->r_offset,
15208 howto->name,
15209 h->elf.root.root.string);
15210 ret = FALSE;
15211 }
15212
15213 /* 16-bit fields in insns mostly have signed values, but a
15214 few insns have 16-bit unsigned values. Really, we should
15215 have different reloc types. */
15216 if (howto->complain_on_overflow != complain_overflow_dont
15217 && howto->dst_mask == 0xffff
15218 && (input_section->flags & SEC_CODE) != 0)
15219 {
15220 enum complain_overflow complain = complain_overflow_signed;
15221
15222 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15223 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15224 complain = complain_overflow_bitfield;
15225 else if (howto->rightshift == 0
15226 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15227 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15228 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15229 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15230 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15231 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15232 complain = complain_overflow_unsigned;
15233 if (howto->complain_on_overflow != complain)
15234 {
15235 alt_howto = *howto;
15236 alt_howto.complain_on_overflow = complain;
15237 howto = &alt_howto;
15238 }
15239 }
15240
15241 if (r_type == R_PPC64_REL16DX_HA)
15242 {
15243 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15244 if (rel->r_offset + 4 > input_section->size)
15245 r = bfd_reloc_outofrange;
15246 else
15247 {
15248 relocation += addend;
15249 relocation -= (rel->r_offset
15250 + input_section->output_offset
15251 + input_section->output_section->vma);
15252 relocation = (bfd_signed_vma) relocation >> 16;
15253 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15254 insn &= ~0x1fffc1;
15255 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15256 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15257 r = bfd_reloc_ok;
15258 if (relocation + 0x8000 > 0xffff)
15259 r = bfd_reloc_overflow;
15260 }
15261 }
15262 else
15263 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15264 rel->r_offset, relocation, addend);
15265
15266 if (r != bfd_reloc_ok)
15267 {
15268 char *more_info = NULL;
15269 const char *reloc_name = howto->name;
15270
15271 if (reloc_dest != DEST_NORMAL)
15272 {
15273 more_info = bfd_malloc (strlen (reloc_name) + 8);
15274 if (more_info != NULL)
15275 {
15276 strcpy (more_info, reloc_name);
15277 strcat (more_info, (reloc_dest == DEST_OPD
15278 ? " (OPD)" : " (stub)"));
15279 reloc_name = more_info;
15280 }
15281 }
15282
15283 if (r == bfd_reloc_overflow)
15284 {
15285 /* On code like "if (foo) foo();" don't report overflow
15286 on a branch to zero when foo is undefined. */
15287 if (!warned
15288 && (reloc_dest == DEST_STUB
15289 || !(h != NULL
15290 && (h->elf.root.type == bfd_link_hash_undefweak
15291 || h->elf.root.type == bfd_link_hash_undefined)
15292 && is_branch_reloc (r_type))))
15293 info->callbacks->reloc_overflow (info, &h->elf.root,
15294 sym_name, reloc_name,
15295 orig_rel.r_addend,
15296 input_bfd, input_section,
15297 rel->r_offset);
15298 }
15299 else
15300 {
15301 info->callbacks->einfo
15302 /* xgettext:c-format */
15303 (_("%H: %s against `%T': error %d\n"),
15304 input_bfd, input_section, rel->r_offset,
15305 reloc_name, sym_name, (int) r);
15306 ret = FALSE;
15307 }
15308 if (more_info != NULL)
15309 free (more_info);
15310 }
15311 copy_reloc:
15312 if (wrel != rel)
15313 *wrel = *rel;
15314 }
15315
15316 if (wrel != rel)
15317 {
15318 Elf_Internal_Shdr *rel_hdr;
15319 size_t deleted = rel - wrel;
15320
15321 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15322 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15323 if (rel_hdr->sh_size == 0)
15324 {
15325 /* It is too late to remove an empty reloc section. Leave
15326 one NONE reloc.
15327 ??? What is wrong with an empty section??? */
15328 rel_hdr->sh_size = rel_hdr->sh_entsize;
15329 deleted -= 1;
15330 }
15331 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15332 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15333 input_section->reloc_count -= deleted;
15334 }
15335
15336 /* If we're emitting relocations, then shortly after this function
15337 returns, reloc offsets and addends for this section will be
15338 adjusted. Worse, reloc symbol indices will be for the output
15339 file rather than the input. Save a copy of the relocs for
15340 opd_entry_value. */
15341 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15342 {
15343 bfd_size_type amt;
15344 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15345 rel = bfd_alloc (input_bfd, amt);
15346 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15347 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15348 if (rel == NULL)
15349 return FALSE;
15350 memcpy (rel, relocs, amt);
15351 }
15352 return ret;
15353 }
15354
15355 /* Adjust the value of any local symbols in opd sections. */
15356
15357 static int
15358 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15359 const char *name ATTRIBUTE_UNUSED,
15360 Elf_Internal_Sym *elfsym,
15361 asection *input_sec,
15362 struct elf_link_hash_entry *h)
15363 {
15364 struct _opd_sec_data *opd;
15365 long adjust;
15366 bfd_vma value;
15367
15368 if (h != NULL)
15369 return 1;
15370
15371 opd = get_opd_info (input_sec);
15372 if (opd == NULL || opd->adjust == NULL)
15373 return 1;
15374
15375 value = elfsym->st_value - input_sec->output_offset;
15376 if (!bfd_link_relocatable (info))
15377 value -= input_sec->output_section->vma;
15378
15379 adjust = opd->adjust[OPD_NDX (value)];
15380 if (adjust == -1)
15381 return 2;
15382
15383 elfsym->st_value += adjust;
15384 return 1;
15385 }
15386
15387 /* Finish up dynamic symbol handling. We set the contents of various
15388 dynamic sections here. */
15389
15390 static bfd_boolean
15391 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15392 struct bfd_link_info *info,
15393 struct elf_link_hash_entry *h,
15394 Elf_Internal_Sym *sym)
15395 {
15396 struct ppc_link_hash_table *htab;
15397 struct plt_entry *ent;
15398 Elf_Internal_Rela rela;
15399 bfd_byte *loc;
15400
15401 htab = ppc_hash_table (info);
15402 if (htab == NULL)
15403 return FALSE;
15404
15405 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15406 if (ent->plt.offset != (bfd_vma) -1)
15407 {
15408 /* This symbol has an entry in the procedure linkage
15409 table. Set it up. */
15410 if (!htab->elf.dynamic_sections_created
15411 || h->dynindx == -1)
15412 {
15413 BFD_ASSERT (h->type == STT_GNU_IFUNC
15414 && h->def_regular
15415 && (h->root.type == bfd_link_hash_defined
15416 || h->root.type == bfd_link_hash_defweak));
15417 rela.r_offset = (htab->elf.iplt->output_section->vma
15418 + htab->elf.iplt->output_offset
15419 + ent->plt.offset);
15420 if (htab->opd_abi)
15421 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15422 else
15423 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15424 rela.r_addend = (h->root.u.def.value
15425 + h->root.u.def.section->output_offset
15426 + h->root.u.def.section->output_section->vma
15427 + ent->addend);
15428 loc = (htab->elf.irelplt->contents
15429 + (htab->elf.irelplt->reloc_count++
15430 * sizeof (Elf64_External_Rela)));
15431 htab->local_ifunc_resolver = 1;
15432 }
15433 else
15434 {
15435 rela.r_offset = (htab->elf.splt->output_section->vma
15436 + htab->elf.splt->output_offset
15437 + ent->plt.offset);
15438 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15439 rela.r_addend = ent->addend;
15440 loc = (htab->elf.srelplt->contents
15441 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15442 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
15443 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
15444 htab->maybe_local_ifunc_resolver = 1;
15445 }
15446 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15447
15448 if (!htab->opd_abi)
15449 {
15450 if (!h->def_regular)
15451 {
15452 /* Mark the symbol as undefined, rather than as
15453 defined in glink. Leave the value if there were
15454 any relocations where pointer equality matters
15455 (this is a clue for the dynamic linker, to make
15456 function pointer comparisons work between an
15457 application and shared library), otherwise set it
15458 to zero. */
15459 sym->st_shndx = SHN_UNDEF;
15460 if (!h->pointer_equality_needed)
15461 sym->st_value = 0;
15462 else if (!h->ref_regular_nonweak)
15463 {
15464 /* This breaks function pointer comparisons, but
15465 that is better than breaking tests for a NULL
15466 function pointer. */
15467 sym->st_value = 0;
15468 }
15469 }
15470 }
15471 }
15472
15473 if (h->needs_copy)
15474 {
15475 /* This symbol needs a copy reloc. Set it up. */
15476 asection *srel;
15477
15478 if (h->dynindx == -1
15479 || (h->root.type != bfd_link_hash_defined
15480 && h->root.type != bfd_link_hash_defweak)
15481 || htab->elf.srelbss == NULL
15482 || htab->elf.sreldynrelro == NULL)
15483 abort ();
15484
15485 rela.r_offset = (h->root.u.def.value
15486 + h->root.u.def.section->output_section->vma
15487 + h->root.u.def.section->output_offset);
15488 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15489 rela.r_addend = 0;
15490 if (h->root.u.def.section == htab->elf.sdynrelro)
15491 srel = htab->elf.sreldynrelro;
15492 else
15493 srel = htab->elf.srelbss;
15494 loc = srel->contents;
15495 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
15496 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15497 }
15498
15499 return TRUE;
15500 }
15501
15502 /* Used to decide how to sort relocs in an optimal manner for the
15503 dynamic linker, before writing them out. */
15504
15505 static enum elf_reloc_type_class
15506 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15507 const asection *rel_sec,
15508 const Elf_Internal_Rela *rela)
15509 {
15510 enum elf_ppc64_reloc_type r_type;
15511 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15512
15513 if (rel_sec == htab->elf.irelplt)
15514 return reloc_class_ifunc;
15515
15516 r_type = ELF64_R_TYPE (rela->r_info);
15517 switch (r_type)
15518 {
15519 case R_PPC64_RELATIVE:
15520 return reloc_class_relative;
15521 case R_PPC64_JMP_SLOT:
15522 return reloc_class_plt;
15523 case R_PPC64_COPY:
15524 return reloc_class_copy;
15525 default:
15526 return reloc_class_normal;
15527 }
15528 }
15529
15530 /* Finish up the dynamic sections. */
15531
15532 static bfd_boolean
15533 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15534 struct bfd_link_info *info)
15535 {
15536 struct ppc_link_hash_table *htab;
15537 bfd *dynobj;
15538 asection *sdyn;
15539
15540 htab = ppc_hash_table (info);
15541 if (htab == NULL)
15542 return FALSE;
15543
15544 dynobj = htab->elf.dynobj;
15545 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15546
15547 if (htab->elf.dynamic_sections_created)
15548 {
15549 Elf64_External_Dyn *dyncon, *dynconend;
15550
15551 if (sdyn == NULL || htab->elf.sgot == NULL)
15552 abort ();
15553
15554 dyncon = (Elf64_External_Dyn *) sdyn->contents;
15555 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15556 for (; dyncon < dynconend; dyncon++)
15557 {
15558 Elf_Internal_Dyn dyn;
15559 asection *s;
15560
15561 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15562
15563 switch (dyn.d_tag)
15564 {
15565 default:
15566 continue;
15567
15568 case DT_PPC64_GLINK:
15569 s = htab->glink;
15570 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15571 /* We stupidly defined DT_PPC64_GLINK to be the start
15572 of glink rather than the first entry point, which is
15573 what ld.so needs, and now have a bigger stub to
15574 support automatic multiple TOCs. */
15575 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
15576 break;
15577
15578 case DT_PPC64_OPD:
15579 s = bfd_get_section_by_name (output_bfd, ".opd");
15580 if (s == NULL)
15581 continue;
15582 dyn.d_un.d_ptr = s->vma;
15583 break;
15584
15585 case DT_PPC64_OPT:
15586 if (htab->do_multi_toc && htab->multi_toc_needed)
15587 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15588 if (htab->has_plt_localentry0)
15589 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
15590 break;
15591
15592 case DT_PPC64_OPDSZ:
15593 s = bfd_get_section_by_name (output_bfd, ".opd");
15594 if (s == NULL)
15595 continue;
15596 dyn.d_un.d_val = s->size;
15597 break;
15598
15599 case DT_PLTGOT:
15600 s = htab->elf.splt;
15601 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15602 break;
15603
15604 case DT_JMPREL:
15605 s = htab->elf.srelplt;
15606 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15607 break;
15608
15609 case DT_PLTRELSZ:
15610 dyn.d_un.d_val = htab->elf.srelplt->size;
15611 break;
15612
15613 case DT_TEXTREL:
15614 if (htab->local_ifunc_resolver)
15615 info->callbacks->einfo
15616 (_("%X%P: text relocations and GNU indirect "
15617 "functions will result in a segfault at runtime\n"));
15618 else if (htab->maybe_local_ifunc_resolver)
15619 info->callbacks->einfo
15620 (_("%P: warning: text relocations and GNU indirect "
15621 "functions may result in a segfault at runtime\n"));
15622 continue;
15623 }
15624
15625 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15626 }
15627 }
15628
15629 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15630 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
15631 {
15632 /* Fill in the first entry in the global offset table.
15633 We use it to hold the link-time TOCbase. */
15634 bfd_put_64 (output_bfd,
15635 elf_gp (output_bfd) + TOC_BASE_OFF,
15636 htab->elf.sgot->contents);
15637
15638 /* Set .got entry size. */
15639 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
15640 }
15641
15642 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15643 && htab->elf.splt->output_section != bfd_abs_section_ptr)
15644 {
15645 /* Set .plt entry size. */
15646 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15647 = PLT_ENTRY_SIZE (htab);
15648 }
15649
15650 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15651 brlt ourselves if emitrelocations. */
15652 if (htab->brlt != NULL
15653 && htab->brlt->reloc_count != 0
15654 && !_bfd_elf_link_output_relocs (output_bfd,
15655 htab->brlt,
15656 elf_section_data (htab->brlt)->rela.hdr,
15657 elf_section_data (htab->brlt)->relocs,
15658 NULL))
15659 return FALSE;
15660
15661 if (htab->glink != NULL
15662 && htab->glink->reloc_count != 0
15663 && !_bfd_elf_link_output_relocs (output_bfd,
15664 htab->glink,
15665 elf_section_data (htab->glink)->rela.hdr,
15666 elf_section_data (htab->glink)->relocs,
15667 NULL))
15668 return FALSE;
15669
15670 if (htab->glink_eh_frame != NULL
15671 && htab->glink_eh_frame->size != 0)
15672 {
15673 bfd_vma val;
15674 bfd_byte *p;
15675 struct map_stub *group;
15676 size_t align = 4;
15677
15678 p = htab->glink_eh_frame->contents;
15679 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15680
15681 for (group = htab->group; group != NULL; group = group->next)
15682 if (group->stub_sec != NULL)
15683 {
15684 /* Offset to stub section. */
15685 val = (group->stub_sec->output_section->vma
15686 + group->stub_sec->output_offset);
15687 val -= (htab->glink_eh_frame->output_section->vma
15688 + htab->glink_eh_frame->output_offset
15689 + (p + 8 - htab->glink_eh_frame->contents));
15690 if (val + 0x80000000 > 0xffffffff)
15691 {
15692 info->callbacks->einfo
15693 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15694 group->stub_sec->name);
15695 return FALSE;
15696 }
15697 bfd_put_32 (dynobj, val, p + 8);
15698 p += stub_eh_frame_size (group, align);
15699 }
15700 if (htab->glink != NULL && htab->glink->size != 0)
15701 {
15702 /* Offset to .glink. */
15703 val = (htab->glink->output_section->vma
15704 + htab->glink->output_offset
15705 + 8);
15706 val -= (htab->glink_eh_frame->output_section->vma
15707 + htab->glink_eh_frame->output_offset
15708 + (p + 8 - htab->glink_eh_frame->contents));
15709 if (val + 0x80000000 > 0xffffffff)
15710 {
15711 info->callbacks->einfo
15712 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15713 htab->glink->name);
15714 return FALSE;
15715 }
15716 bfd_put_32 (dynobj, val, p + 8);
15717 p += (24 + align - 1) & -align;
15718 }
15719
15720 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15721 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15722 htab->glink_eh_frame,
15723 htab->glink_eh_frame->contents))
15724 return FALSE;
15725 }
15726
15727 /* We need to handle writing out multiple GOT sections ourselves,
15728 since we didn't add them to DYNOBJ. We know dynobj is the first
15729 bfd. */
15730 while ((dynobj = dynobj->link.next) != NULL)
15731 {
15732 asection *s;
15733
15734 if (!is_ppc64_elf (dynobj))
15735 continue;
15736
15737 s = ppc64_elf_tdata (dynobj)->got;
15738 if (s != NULL
15739 && s->size != 0
15740 && s->output_section != bfd_abs_section_ptr
15741 && !bfd_set_section_contents (output_bfd, s->output_section,
15742 s->contents, s->output_offset,
15743 s->size))
15744 return FALSE;
15745 s = ppc64_elf_tdata (dynobj)->relgot;
15746 if (s != NULL
15747 && s->size != 0
15748 && s->output_section != bfd_abs_section_ptr
15749 && !bfd_set_section_contents (output_bfd, s->output_section,
15750 s->contents, s->output_offset,
15751 s->size))
15752 return FALSE;
15753 }
15754
15755 return TRUE;
15756 }
15757
15758 #include "elf64-target.h"
15759
15760 /* FreeBSD support */
15761
15762 #undef TARGET_LITTLE_SYM
15763 #undef TARGET_LITTLE_NAME
15764
15765 #undef TARGET_BIG_SYM
15766 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
15767 #undef TARGET_BIG_NAME
15768 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15769
15770 #undef ELF_OSABI
15771 #define ELF_OSABI ELFOSABI_FREEBSD
15772
15773 #undef elf64_bed
15774 #define elf64_bed elf64_powerpc_fbsd_bed
15775
15776 #include "elf64-target.h"
This page took 0.648821 seconds and 4 git commands to generate.