* read.c (read_a_source_file): Remove md_after_pass_hook.
[binutils.git] / bfd / elf64-ppc.c
blobf17402fad27374246c40a62bbd9c6238bacf8984
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010, 2011 Free Software Foundation, Inc.
4 Written by Linus Nordberg, Swox AB <info@swox.com>,
5 based on elf32-ppc.c by Ian Lance Taylor.
6 Largely rewritten by Alan Modra.
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc.,
22 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
25 /* The 64-bit PowerPC ELF ABI may be found at
26 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
29 #include "sysdep.h"
30 #include <stdarg.h>
31 #include "bfd.h"
32 #include "bfdlink.h"
33 #include "libbfd.h"
34 #include "elf-bfd.h"
35 #include "elf/ppc64.h"
36 #include "elf64-ppc.h"
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 *);
59 #define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
60 #define TARGET_LITTLE_NAME "elf64-powerpcle"
61 #define TARGET_BIG_SYM bfd_elf64_powerpc_vec
62 #define TARGET_BIG_NAME "elf64-powerpc"
63 #define ELF_ARCH bfd_arch_powerpc
64 #define ELF_TARGET_ID PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE EM_PPC64
66 #define ELF_MAXPAGESIZE 0x10000
67 #define ELF_COMMONPAGESIZE 0x1000
68 #define elf_info_to_howto ppc64_elf_info_to_howto
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_can_gc_sections 1
76 #define elf_backend_can_refcount 1
77 #define elf_backend_rela_normal 1
78 #define elf_backend_default_execstack 0
80 #define bfd_elf64_mkobject ppc64_elf_mkobject
81 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
82 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
83 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
84 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
85 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
86 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
87 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
88 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
90 #define elf_backend_object_p ppc64_elf_object_p
91 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
92 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
93 #define elf_backend_write_core_note ppc64_elf_write_core_note
94 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
95 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
96 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
97 #define elf_backend_check_directives ppc64_elf_process_dot_syms
98 #define elf_backend_as_needed_cleanup ppc64_elf_as_needed_cleanup
99 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
100 #define elf_backend_check_relocs ppc64_elf_check_relocs
101 #define elf_backend_gc_keep ppc64_elf_gc_keep
102 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
103 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
104 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
105 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
106 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
107 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
108 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
109 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
110 #define elf_backend_action_discarded ppc64_elf_action_discarded
111 #define elf_backend_relocate_section ppc64_elf_relocate_section
112 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
113 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
114 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
115 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
116 #define elf_backend_special_sections ppc64_elf_special_sections
117 #define elf_backend_post_process_headers _bfd_elf_set_osabi
119 /* The name of the dynamic interpreter. This is put in the .interp
120 section. */
121 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
123 /* The size in bytes of an entry in the procedure linkage table. */
124 #define PLT_ENTRY_SIZE 24
126 /* The initial size of the plt reserved for the dynamic linker. */
127 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
129 /* TOC base pointers offset from start of TOC. */
130 #define TOC_BASE_OFF 0x8000
132 /* Offset of tp and dtp pointers from start of TLS block. */
133 #define TP_OFFSET 0x7000
134 #define DTP_OFFSET 0x8000
136 /* .plt call stub instructions. The normal stub is like this, but
137 sometimes the .plt entry crosses a 64k boundary and we need to
138 insert an addi to adjust r12. */
139 #define PLT_CALL_STUB_SIZE (7*4)
140 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
141 #define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
142 #define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
143 #define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
144 #define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
145 /* ld %r11,xxx+16@l(%r12) */
146 #define BCTR 0x4e800420 /* bctr */
149 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,off@ha */
150 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
151 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
152 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
154 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
155 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
157 #define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
159 /* glink call stub instructions. We enter with the index in R0. */
160 #define GLINK_CALL_STUB_SIZE (16*4)
161 /* 0: */
162 /* .quad plt0-1f */
163 /* __glink: */
164 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
165 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
166 /* 1: */
167 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
168 #define LD_R2_M16R11 0xe84bfff0 /* ld %2,(0b-1b)(%11) */
169 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
170 #define ADD_R12_R2_R11 0x7d825a14 /* add %12,%2,%11 */
171 /* ld %11,0(%12) */
172 /* ld %2,8(%12) */
173 /* mtctr %11 */
174 /* ld %11,16(%12) */
175 /* bctr */
177 /* Pad with this. */
178 #define NOP 0x60000000
180 /* Some other nops. */
181 #define CROR_151515 0x4def7b82
182 #define CROR_313131 0x4ffffb82
184 /* .glink entries for the first 32k functions are two instructions. */
185 #define LI_R0_0 0x38000000 /* li %r0,0 */
186 #define B_DOT 0x48000000 /* b . */
188 /* After that, we need two instructions to load the index, followed by
189 a branch. */
190 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
191 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
193 /* Instructions used by the save and restore reg functions. */
194 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
195 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
196 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
197 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
198 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
199 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
200 #define LI_R12_0 0x39800000 /* li %r12,0 */
201 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
202 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
203 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
204 #define BLR 0x4e800020 /* blr */
206 /* Since .opd is an array of descriptors and each entry will end up
207 with identical R_PPC64_RELATIVE relocs, there is really no need to
208 propagate .opd relocs; The dynamic linker should be taught to
209 relocate .opd without reloc entries. */
210 #ifndef NO_OPD_RELOCS
211 #define NO_OPD_RELOCS 0
212 #endif
214 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
216 /* Relocation HOWTO's. */
217 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
219 static reloc_howto_type ppc64_elf_howto_raw[] = {
220 /* This reloc does nothing. */
221 HOWTO (R_PPC64_NONE, /* type */
222 0, /* rightshift */
223 2, /* size (0 = byte, 1 = short, 2 = long) */
224 32, /* bitsize */
225 FALSE, /* pc_relative */
226 0, /* bitpos */
227 complain_overflow_dont, /* complain_on_overflow */
228 bfd_elf_generic_reloc, /* special_function */
229 "R_PPC64_NONE", /* name */
230 FALSE, /* partial_inplace */
231 0, /* src_mask */
232 0, /* dst_mask */
233 FALSE), /* pcrel_offset */
235 /* A standard 32 bit relocation. */
236 HOWTO (R_PPC64_ADDR32, /* type */
237 0, /* rightshift */
238 2, /* size (0 = byte, 1 = short, 2 = long) */
239 32, /* bitsize */
240 FALSE, /* pc_relative */
241 0, /* bitpos */
242 complain_overflow_bitfield, /* complain_on_overflow */
243 bfd_elf_generic_reloc, /* special_function */
244 "R_PPC64_ADDR32", /* name */
245 FALSE, /* partial_inplace */
246 0, /* src_mask */
247 0xffffffff, /* dst_mask */
248 FALSE), /* pcrel_offset */
250 /* An absolute 26 bit branch; the lower two bits must be zero.
251 FIXME: we don't check that, we just clear them. */
252 HOWTO (R_PPC64_ADDR24, /* type */
253 0, /* rightshift */
254 2, /* size (0 = byte, 1 = short, 2 = long) */
255 26, /* bitsize */
256 FALSE, /* pc_relative */
257 0, /* bitpos */
258 complain_overflow_bitfield, /* complain_on_overflow */
259 bfd_elf_generic_reloc, /* special_function */
260 "R_PPC64_ADDR24", /* name */
261 FALSE, /* partial_inplace */
262 0, /* src_mask */
263 0x03fffffc, /* dst_mask */
264 FALSE), /* pcrel_offset */
266 /* A standard 16 bit relocation. */
267 HOWTO (R_PPC64_ADDR16, /* type */
268 0, /* rightshift */
269 1, /* size (0 = byte, 1 = short, 2 = long) */
270 16, /* bitsize */
271 FALSE, /* pc_relative */
272 0, /* bitpos */
273 complain_overflow_bitfield, /* complain_on_overflow */
274 bfd_elf_generic_reloc, /* special_function */
275 "R_PPC64_ADDR16", /* name */
276 FALSE, /* partial_inplace */
277 0, /* src_mask */
278 0xffff, /* dst_mask */
279 FALSE), /* pcrel_offset */
281 /* A 16 bit relocation without overflow. */
282 HOWTO (R_PPC64_ADDR16_LO, /* type */
283 0, /* rightshift */
284 1, /* size (0 = byte, 1 = short, 2 = long) */
285 16, /* bitsize */
286 FALSE, /* pc_relative */
287 0, /* bitpos */
288 complain_overflow_dont,/* complain_on_overflow */
289 bfd_elf_generic_reloc, /* special_function */
290 "R_PPC64_ADDR16_LO", /* name */
291 FALSE, /* partial_inplace */
292 0, /* src_mask */
293 0xffff, /* dst_mask */
294 FALSE), /* pcrel_offset */
296 /* Bits 16-31 of an address. */
297 HOWTO (R_PPC64_ADDR16_HI, /* type */
298 16, /* rightshift */
299 1, /* size (0 = byte, 1 = short, 2 = long) */
300 16, /* bitsize */
301 FALSE, /* pc_relative */
302 0, /* bitpos */
303 complain_overflow_dont, /* complain_on_overflow */
304 bfd_elf_generic_reloc, /* special_function */
305 "R_PPC64_ADDR16_HI", /* name */
306 FALSE, /* partial_inplace */
307 0, /* src_mask */
308 0xffff, /* dst_mask */
309 FALSE), /* pcrel_offset */
311 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
312 bits, treated as a signed number, is negative. */
313 HOWTO (R_PPC64_ADDR16_HA, /* type */
314 16, /* rightshift */
315 1, /* size (0 = byte, 1 = short, 2 = long) */
316 16, /* bitsize */
317 FALSE, /* pc_relative */
318 0, /* bitpos */
319 complain_overflow_dont, /* complain_on_overflow */
320 ppc64_elf_ha_reloc, /* special_function */
321 "R_PPC64_ADDR16_HA", /* name */
322 FALSE, /* partial_inplace */
323 0, /* src_mask */
324 0xffff, /* dst_mask */
325 FALSE), /* pcrel_offset */
327 /* An absolute 16 bit branch; the lower two bits must be zero.
328 FIXME: we don't check that, we just clear them. */
329 HOWTO (R_PPC64_ADDR14, /* type */
330 0, /* rightshift */
331 2, /* size (0 = byte, 1 = short, 2 = long) */
332 16, /* bitsize */
333 FALSE, /* pc_relative */
334 0, /* bitpos */
335 complain_overflow_bitfield, /* complain_on_overflow */
336 ppc64_elf_branch_reloc, /* special_function */
337 "R_PPC64_ADDR14", /* name */
338 FALSE, /* partial_inplace */
339 0, /* src_mask */
340 0x0000fffc, /* dst_mask */
341 FALSE), /* pcrel_offset */
343 /* An absolute 16 bit branch, for which bit 10 should be set to
344 indicate that the branch is expected to be taken. The lower two
345 bits must be zero. */
346 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
347 0, /* rightshift */
348 2, /* size (0 = byte, 1 = short, 2 = long) */
349 16, /* bitsize */
350 FALSE, /* pc_relative */
351 0, /* bitpos */
352 complain_overflow_bitfield, /* complain_on_overflow */
353 ppc64_elf_brtaken_reloc, /* special_function */
354 "R_PPC64_ADDR14_BRTAKEN",/* name */
355 FALSE, /* partial_inplace */
356 0, /* src_mask */
357 0x0000fffc, /* dst_mask */
358 FALSE), /* pcrel_offset */
360 /* An absolute 16 bit branch, for which bit 10 should be set to
361 indicate that the branch is not expected to be taken. The lower
362 two bits must be zero. */
363 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
364 0, /* rightshift */
365 2, /* size (0 = byte, 1 = short, 2 = long) */
366 16, /* bitsize */
367 FALSE, /* pc_relative */
368 0, /* bitpos */
369 complain_overflow_bitfield, /* complain_on_overflow */
370 ppc64_elf_brtaken_reloc, /* special_function */
371 "R_PPC64_ADDR14_BRNTAKEN",/* name */
372 FALSE, /* partial_inplace */
373 0, /* src_mask */
374 0x0000fffc, /* dst_mask */
375 FALSE), /* pcrel_offset */
377 /* A relative 26 bit branch; the lower two bits must be zero. */
378 HOWTO (R_PPC64_REL24, /* type */
379 0, /* rightshift */
380 2, /* size (0 = byte, 1 = short, 2 = long) */
381 26, /* bitsize */
382 TRUE, /* pc_relative */
383 0, /* bitpos */
384 complain_overflow_signed, /* complain_on_overflow */
385 ppc64_elf_branch_reloc, /* special_function */
386 "R_PPC64_REL24", /* name */
387 FALSE, /* partial_inplace */
388 0, /* src_mask */
389 0x03fffffc, /* dst_mask */
390 TRUE), /* pcrel_offset */
392 /* A relative 16 bit branch; the lower two bits must be zero. */
393 HOWTO (R_PPC64_REL14, /* type */
394 0, /* rightshift */
395 2, /* size (0 = byte, 1 = short, 2 = long) */
396 16, /* bitsize */
397 TRUE, /* pc_relative */
398 0, /* bitpos */
399 complain_overflow_signed, /* complain_on_overflow */
400 ppc64_elf_branch_reloc, /* special_function */
401 "R_PPC64_REL14", /* name */
402 FALSE, /* partial_inplace */
403 0, /* src_mask */
404 0x0000fffc, /* dst_mask */
405 TRUE), /* pcrel_offset */
407 /* A relative 16 bit branch. Bit 10 should be set to indicate that
408 the branch is expected to be taken. The lower two bits must be
409 zero. */
410 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
411 0, /* rightshift */
412 2, /* size (0 = byte, 1 = short, 2 = long) */
413 16, /* bitsize */
414 TRUE, /* pc_relative */
415 0, /* bitpos */
416 complain_overflow_signed, /* complain_on_overflow */
417 ppc64_elf_brtaken_reloc, /* special_function */
418 "R_PPC64_REL14_BRTAKEN", /* name */
419 FALSE, /* partial_inplace */
420 0, /* src_mask */
421 0x0000fffc, /* dst_mask */
422 TRUE), /* pcrel_offset */
424 /* A relative 16 bit branch. Bit 10 should be set to indicate that
425 the branch is not expected to be taken. The lower two bits must
426 be zero. */
427 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
428 0, /* rightshift */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
430 16, /* bitsize */
431 TRUE, /* pc_relative */
432 0, /* bitpos */
433 complain_overflow_signed, /* complain_on_overflow */
434 ppc64_elf_brtaken_reloc, /* special_function */
435 "R_PPC64_REL14_BRNTAKEN",/* name */
436 FALSE, /* partial_inplace */
437 0, /* src_mask */
438 0x0000fffc, /* dst_mask */
439 TRUE), /* pcrel_offset */
441 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
442 symbol. */
443 HOWTO (R_PPC64_GOT16, /* type */
444 0, /* rightshift */
445 1, /* size (0 = byte, 1 = short, 2 = long) */
446 16, /* bitsize */
447 FALSE, /* pc_relative */
448 0, /* bitpos */
449 complain_overflow_signed, /* complain_on_overflow */
450 ppc64_elf_unhandled_reloc, /* special_function */
451 "R_PPC64_GOT16", /* name */
452 FALSE, /* partial_inplace */
453 0, /* src_mask */
454 0xffff, /* dst_mask */
455 FALSE), /* pcrel_offset */
457 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
458 the symbol. */
459 HOWTO (R_PPC64_GOT16_LO, /* type */
460 0, /* rightshift */
461 1, /* size (0 = byte, 1 = short, 2 = long) */
462 16, /* bitsize */
463 FALSE, /* pc_relative */
464 0, /* bitpos */
465 complain_overflow_dont, /* complain_on_overflow */
466 ppc64_elf_unhandled_reloc, /* special_function */
467 "R_PPC64_GOT16_LO", /* name */
468 FALSE, /* partial_inplace */
469 0, /* src_mask */
470 0xffff, /* dst_mask */
471 FALSE), /* pcrel_offset */
473 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
474 the symbol. */
475 HOWTO (R_PPC64_GOT16_HI, /* type */
476 16, /* rightshift */
477 1, /* size (0 = byte, 1 = short, 2 = long) */
478 16, /* bitsize */
479 FALSE, /* pc_relative */
480 0, /* bitpos */
481 complain_overflow_dont,/* complain_on_overflow */
482 ppc64_elf_unhandled_reloc, /* special_function */
483 "R_PPC64_GOT16_HI", /* name */
484 FALSE, /* partial_inplace */
485 0, /* src_mask */
486 0xffff, /* dst_mask */
487 FALSE), /* pcrel_offset */
489 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
490 the symbol. */
491 HOWTO (R_PPC64_GOT16_HA, /* type */
492 16, /* rightshift */
493 1, /* size (0 = byte, 1 = short, 2 = long) */
494 16, /* bitsize */
495 FALSE, /* pc_relative */
496 0, /* bitpos */
497 complain_overflow_dont,/* complain_on_overflow */
498 ppc64_elf_unhandled_reloc, /* special_function */
499 "R_PPC64_GOT16_HA", /* name */
500 FALSE, /* partial_inplace */
501 0, /* src_mask */
502 0xffff, /* dst_mask */
503 FALSE), /* pcrel_offset */
505 /* This is used only by the dynamic linker. The symbol should exist
506 both in the object being run and in some shared library. The
507 dynamic linker copies the data addressed by the symbol from the
508 shared library into the object, because the object being
509 run has to have the data at some particular address. */
510 HOWTO (R_PPC64_COPY, /* type */
511 0, /* rightshift */
512 0, /* this one is variable size */
513 0, /* bitsize */
514 FALSE, /* pc_relative */
515 0, /* bitpos */
516 complain_overflow_dont, /* complain_on_overflow */
517 ppc64_elf_unhandled_reloc, /* special_function */
518 "R_PPC64_COPY", /* name */
519 FALSE, /* partial_inplace */
520 0, /* src_mask */
521 0, /* dst_mask */
522 FALSE), /* pcrel_offset */
524 /* Like R_PPC64_ADDR64, but used when setting global offset table
525 entries. */
526 HOWTO (R_PPC64_GLOB_DAT, /* type */
527 0, /* rightshift */
528 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
529 64, /* bitsize */
530 FALSE, /* pc_relative */
531 0, /* bitpos */
532 complain_overflow_dont, /* complain_on_overflow */
533 ppc64_elf_unhandled_reloc, /* special_function */
534 "R_PPC64_GLOB_DAT", /* name */
535 FALSE, /* partial_inplace */
536 0, /* src_mask */
537 ONES (64), /* dst_mask */
538 FALSE), /* pcrel_offset */
540 /* Created by the link editor. Marks a procedure linkage table
541 entry for a symbol. */
542 HOWTO (R_PPC64_JMP_SLOT, /* type */
543 0, /* rightshift */
544 0, /* size (0 = byte, 1 = short, 2 = long) */
545 0, /* bitsize */
546 FALSE, /* pc_relative */
547 0, /* bitpos */
548 complain_overflow_dont, /* complain_on_overflow */
549 ppc64_elf_unhandled_reloc, /* special_function */
550 "R_PPC64_JMP_SLOT", /* name */
551 FALSE, /* partial_inplace */
552 0, /* src_mask */
553 0, /* dst_mask */
554 FALSE), /* pcrel_offset */
556 /* Used only by the dynamic linker. When the object is run, this
557 doubleword64 is set to the load address of the object, plus the
558 addend. */
559 HOWTO (R_PPC64_RELATIVE, /* type */
560 0, /* rightshift */
561 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
562 64, /* bitsize */
563 FALSE, /* pc_relative */
564 0, /* bitpos */
565 complain_overflow_dont, /* complain_on_overflow */
566 bfd_elf_generic_reloc, /* special_function */
567 "R_PPC64_RELATIVE", /* name */
568 FALSE, /* partial_inplace */
569 0, /* src_mask */
570 ONES (64), /* dst_mask */
571 FALSE), /* pcrel_offset */
573 /* Like R_PPC64_ADDR32, but may be unaligned. */
574 HOWTO (R_PPC64_UADDR32, /* type */
575 0, /* rightshift */
576 2, /* size (0 = byte, 1 = short, 2 = long) */
577 32, /* bitsize */
578 FALSE, /* pc_relative */
579 0, /* bitpos */
580 complain_overflow_bitfield, /* complain_on_overflow */
581 bfd_elf_generic_reloc, /* special_function */
582 "R_PPC64_UADDR32", /* name */
583 FALSE, /* partial_inplace */
584 0, /* src_mask */
585 0xffffffff, /* dst_mask */
586 FALSE), /* pcrel_offset */
588 /* Like R_PPC64_ADDR16, but may be unaligned. */
589 HOWTO (R_PPC64_UADDR16, /* type */
590 0, /* rightshift */
591 1, /* size (0 = byte, 1 = short, 2 = long) */
592 16, /* bitsize */
593 FALSE, /* pc_relative */
594 0, /* bitpos */
595 complain_overflow_bitfield, /* complain_on_overflow */
596 bfd_elf_generic_reloc, /* special_function */
597 "R_PPC64_UADDR16", /* name */
598 FALSE, /* partial_inplace */
599 0, /* src_mask */
600 0xffff, /* dst_mask */
601 FALSE), /* pcrel_offset */
603 /* 32-bit PC relative. */
604 HOWTO (R_PPC64_REL32, /* type */
605 0, /* rightshift */
606 2, /* size (0 = byte, 1 = short, 2 = long) */
607 32, /* bitsize */
608 TRUE, /* pc_relative */
609 0, /* bitpos */
610 /* FIXME: Verify. Was complain_overflow_bitfield. */
611 complain_overflow_signed, /* complain_on_overflow */
612 bfd_elf_generic_reloc, /* special_function */
613 "R_PPC64_REL32", /* name */
614 FALSE, /* partial_inplace */
615 0, /* src_mask */
616 0xffffffff, /* dst_mask */
617 TRUE), /* pcrel_offset */
619 /* 32-bit relocation to the symbol's procedure linkage table. */
620 HOWTO (R_PPC64_PLT32, /* type */
621 0, /* rightshift */
622 2, /* size (0 = byte, 1 = short, 2 = long) */
623 32, /* bitsize */
624 FALSE, /* pc_relative */
625 0, /* bitpos */
626 complain_overflow_bitfield, /* complain_on_overflow */
627 ppc64_elf_unhandled_reloc, /* special_function */
628 "R_PPC64_PLT32", /* name */
629 FALSE, /* partial_inplace */
630 0, /* src_mask */
631 0xffffffff, /* dst_mask */
632 FALSE), /* pcrel_offset */
634 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
635 FIXME: R_PPC64_PLTREL32 not supported. */
636 HOWTO (R_PPC64_PLTREL32, /* type */
637 0, /* rightshift */
638 2, /* size (0 = byte, 1 = short, 2 = long) */
639 32, /* bitsize */
640 TRUE, /* pc_relative */
641 0, /* bitpos */
642 complain_overflow_signed, /* complain_on_overflow */
643 bfd_elf_generic_reloc, /* special_function */
644 "R_PPC64_PLTREL32", /* name */
645 FALSE, /* partial_inplace */
646 0, /* src_mask */
647 0xffffffff, /* dst_mask */
648 TRUE), /* pcrel_offset */
650 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
651 the symbol. */
652 HOWTO (R_PPC64_PLT16_LO, /* type */
653 0, /* rightshift */
654 1, /* size (0 = byte, 1 = short, 2 = long) */
655 16, /* bitsize */
656 FALSE, /* pc_relative */
657 0, /* bitpos */
658 complain_overflow_dont, /* complain_on_overflow */
659 ppc64_elf_unhandled_reloc, /* special_function */
660 "R_PPC64_PLT16_LO", /* name */
661 FALSE, /* partial_inplace */
662 0, /* src_mask */
663 0xffff, /* dst_mask */
664 FALSE), /* pcrel_offset */
666 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
667 the symbol. */
668 HOWTO (R_PPC64_PLT16_HI, /* type */
669 16, /* rightshift */
670 1, /* size (0 = byte, 1 = short, 2 = long) */
671 16, /* bitsize */
672 FALSE, /* pc_relative */
673 0, /* bitpos */
674 complain_overflow_dont, /* complain_on_overflow */
675 ppc64_elf_unhandled_reloc, /* special_function */
676 "R_PPC64_PLT16_HI", /* name */
677 FALSE, /* partial_inplace */
678 0, /* src_mask */
679 0xffff, /* dst_mask */
680 FALSE), /* pcrel_offset */
682 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
683 the symbol. */
684 HOWTO (R_PPC64_PLT16_HA, /* type */
685 16, /* rightshift */
686 1, /* size (0 = byte, 1 = short, 2 = long) */
687 16, /* bitsize */
688 FALSE, /* pc_relative */
689 0, /* bitpos */
690 complain_overflow_dont, /* complain_on_overflow */
691 ppc64_elf_unhandled_reloc, /* special_function */
692 "R_PPC64_PLT16_HA", /* name */
693 FALSE, /* partial_inplace */
694 0, /* src_mask */
695 0xffff, /* dst_mask */
696 FALSE), /* pcrel_offset */
698 /* 16-bit section relative relocation. */
699 HOWTO (R_PPC64_SECTOFF, /* type */
700 0, /* rightshift */
701 1, /* size (0 = byte, 1 = short, 2 = long) */
702 16, /* bitsize */
703 FALSE, /* pc_relative */
704 0, /* bitpos */
705 complain_overflow_bitfield, /* complain_on_overflow */
706 ppc64_elf_sectoff_reloc, /* special_function */
707 "R_PPC64_SECTOFF", /* name */
708 FALSE, /* partial_inplace */
709 0, /* src_mask */
710 0xffff, /* dst_mask */
711 FALSE), /* pcrel_offset */
713 /* Like R_PPC64_SECTOFF, but no overflow warning. */
714 HOWTO (R_PPC64_SECTOFF_LO, /* type */
715 0, /* rightshift */
716 1, /* size (0 = byte, 1 = short, 2 = long) */
717 16, /* bitsize */
718 FALSE, /* pc_relative */
719 0, /* bitpos */
720 complain_overflow_dont, /* complain_on_overflow */
721 ppc64_elf_sectoff_reloc, /* special_function */
722 "R_PPC64_SECTOFF_LO", /* name */
723 FALSE, /* partial_inplace */
724 0, /* src_mask */
725 0xffff, /* dst_mask */
726 FALSE), /* pcrel_offset */
728 /* 16-bit upper half section relative relocation. */
729 HOWTO (R_PPC64_SECTOFF_HI, /* type */
730 16, /* rightshift */
731 1, /* size (0 = byte, 1 = short, 2 = long) */
732 16, /* bitsize */
733 FALSE, /* pc_relative */
734 0, /* bitpos */
735 complain_overflow_dont, /* complain_on_overflow */
736 ppc64_elf_sectoff_reloc, /* special_function */
737 "R_PPC64_SECTOFF_HI", /* name */
738 FALSE, /* partial_inplace */
739 0, /* src_mask */
740 0xffff, /* dst_mask */
741 FALSE), /* pcrel_offset */
743 /* 16-bit upper half adjusted section relative relocation. */
744 HOWTO (R_PPC64_SECTOFF_HA, /* type */
745 16, /* rightshift */
746 1, /* size (0 = byte, 1 = short, 2 = long) */
747 16, /* bitsize */
748 FALSE, /* pc_relative */
749 0, /* bitpos */
750 complain_overflow_dont, /* complain_on_overflow */
751 ppc64_elf_sectoff_ha_reloc, /* special_function */
752 "R_PPC64_SECTOFF_HA", /* name */
753 FALSE, /* partial_inplace */
754 0, /* src_mask */
755 0xffff, /* dst_mask */
756 FALSE), /* pcrel_offset */
758 /* Like R_PPC64_REL24 without touching the two least significant bits. */
759 HOWTO (R_PPC64_REL30, /* type */
760 2, /* rightshift */
761 2, /* size (0 = byte, 1 = short, 2 = long) */
762 30, /* bitsize */
763 TRUE, /* pc_relative */
764 0, /* bitpos */
765 complain_overflow_dont, /* complain_on_overflow */
766 bfd_elf_generic_reloc, /* special_function */
767 "R_PPC64_REL30", /* name */
768 FALSE, /* partial_inplace */
769 0, /* src_mask */
770 0xfffffffc, /* dst_mask */
771 TRUE), /* pcrel_offset */
773 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
775 /* A standard 64-bit relocation. */
776 HOWTO (R_PPC64_ADDR64, /* type */
777 0, /* rightshift */
778 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
779 64, /* bitsize */
780 FALSE, /* pc_relative */
781 0, /* bitpos */
782 complain_overflow_dont, /* complain_on_overflow */
783 bfd_elf_generic_reloc, /* special_function */
784 "R_PPC64_ADDR64", /* name */
785 FALSE, /* partial_inplace */
786 0, /* src_mask */
787 ONES (64), /* dst_mask */
788 FALSE), /* pcrel_offset */
790 /* The bits 32-47 of an address. */
791 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
792 32, /* rightshift */
793 1, /* size (0 = byte, 1 = short, 2 = long) */
794 16, /* bitsize */
795 FALSE, /* pc_relative */
796 0, /* bitpos */
797 complain_overflow_dont, /* complain_on_overflow */
798 bfd_elf_generic_reloc, /* special_function */
799 "R_PPC64_ADDR16_HIGHER", /* name */
800 FALSE, /* partial_inplace */
801 0, /* src_mask */
802 0xffff, /* dst_mask */
803 FALSE), /* pcrel_offset */
805 /* The bits 32-47 of an address, plus 1 if the contents of the low
806 16 bits, treated as a signed number, is negative. */
807 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
808 32, /* rightshift */
809 1, /* size (0 = byte, 1 = short, 2 = long) */
810 16, /* bitsize */
811 FALSE, /* pc_relative */
812 0, /* bitpos */
813 complain_overflow_dont, /* complain_on_overflow */
814 ppc64_elf_ha_reloc, /* special_function */
815 "R_PPC64_ADDR16_HIGHERA", /* name */
816 FALSE, /* partial_inplace */
817 0, /* src_mask */
818 0xffff, /* dst_mask */
819 FALSE), /* pcrel_offset */
821 /* The bits 48-63 of an address. */
822 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
823 48, /* rightshift */
824 1, /* size (0 = byte, 1 = short, 2 = long) */
825 16, /* bitsize */
826 FALSE, /* pc_relative */
827 0, /* bitpos */
828 complain_overflow_dont, /* complain_on_overflow */
829 bfd_elf_generic_reloc, /* special_function */
830 "R_PPC64_ADDR16_HIGHEST", /* name */
831 FALSE, /* partial_inplace */
832 0, /* src_mask */
833 0xffff, /* dst_mask */
834 FALSE), /* pcrel_offset */
836 /* The bits 48-63 of an address, plus 1 if the contents of the low
837 16 bits, treated as a signed number, is negative. */
838 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
839 48, /* rightshift */
840 1, /* size (0 = byte, 1 = short, 2 = long) */
841 16, /* bitsize */
842 FALSE, /* pc_relative */
843 0, /* bitpos */
844 complain_overflow_dont, /* complain_on_overflow */
845 ppc64_elf_ha_reloc, /* special_function */
846 "R_PPC64_ADDR16_HIGHESTA", /* name */
847 FALSE, /* partial_inplace */
848 0, /* src_mask */
849 0xffff, /* dst_mask */
850 FALSE), /* pcrel_offset */
852 /* Like ADDR64, but may be unaligned. */
853 HOWTO (R_PPC64_UADDR64, /* type */
854 0, /* rightshift */
855 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
856 64, /* bitsize */
857 FALSE, /* pc_relative */
858 0, /* bitpos */
859 complain_overflow_dont, /* complain_on_overflow */
860 bfd_elf_generic_reloc, /* special_function */
861 "R_PPC64_UADDR64", /* name */
862 FALSE, /* partial_inplace */
863 0, /* src_mask */
864 ONES (64), /* dst_mask */
865 FALSE), /* pcrel_offset */
867 /* 64-bit relative relocation. */
868 HOWTO (R_PPC64_REL64, /* type */
869 0, /* rightshift */
870 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
871 64, /* bitsize */
872 TRUE, /* pc_relative */
873 0, /* bitpos */
874 complain_overflow_dont, /* complain_on_overflow */
875 bfd_elf_generic_reloc, /* special_function */
876 "R_PPC64_REL64", /* name */
877 FALSE, /* partial_inplace */
878 0, /* src_mask */
879 ONES (64), /* dst_mask */
880 TRUE), /* pcrel_offset */
882 /* 64-bit relocation to the symbol's procedure linkage table. */
883 HOWTO (R_PPC64_PLT64, /* type */
884 0, /* rightshift */
885 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
886 64, /* bitsize */
887 FALSE, /* pc_relative */
888 0, /* bitpos */
889 complain_overflow_dont, /* complain_on_overflow */
890 ppc64_elf_unhandled_reloc, /* special_function */
891 "R_PPC64_PLT64", /* name */
892 FALSE, /* partial_inplace */
893 0, /* src_mask */
894 ONES (64), /* dst_mask */
895 FALSE), /* pcrel_offset */
897 /* 64-bit PC relative relocation to the symbol's procedure linkage
898 table. */
899 /* FIXME: R_PPC64_PLTREL64 not supported. */
900 HOWTO (R_PPC64_PLTREL64, /* type */
901 0, /* rightshift */
902 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
903 64, /* bitsize */
904 TRUE, /* pc_relative */
905 0, /* bitpos */
906 complain_overflow_dont, /* complain_on_overflow */
907 ppc64_elf_unhandled_reloc, /* special_function */
908 "R_PPC64_PLTREL64", /* name */
909 FALSE, /* partial_inplace */
910 0, /* src_mask */
911 ONES (64), /* dst_mask */
912 TRUE), /* pcrel_offset */
914 /* 16 bit TOC-relative relocation. */
916 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
917 HOWTO (R_PPC64_TOC16, /* type */
918 0, /* rightshift */
919 1, /* size (0 = byte, 1 = short, 2 = long) */
920 16, /* bitsize */
921 FALSE, /* pc_relative */
922 0, /* bitpos */
923 complain_overflow_signed, /* complain_on_overflow */
924 ppc64_elf_toc_reloc, /* special_function */
925 "R_PPC64_TOC16", /* name */
926 FALSE, /* partial_inplace */
927 0, /* src_mask */
928 0xffff, /* dst_mask */
929 FALSE), /* pcrel_offset */
931 /* 16 bit TOC-relative relocation without overflow. */
933 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
934 HOWTO (R_PPC64_TOC16_LO, /* type */
935 0, /* rightshift */
936 1, /* size (0 = byte, 1 = short, 2 = long) */
937 16, /* bitsize */
938 FALSE, /* pc_relative */
939 0, /* bitpos */
940 complain_overflow_dont, /* complain_on_overflow */
941 ppc64_elf_toc_reloc, /* special_function */
942 "R_PPC64_TOC16_LO", /* name */
943 FALSE, /* partial_inplace */
944 0, /* src_mask */
945 0xffff, /* dst_mask */
946 FALSE), /* pcrel_offset */
948 /* 16 bit TOC-relative relocation, high 16 bits. */
950 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
951 HOWTO (R_PPC64_TOC16_HI, /* type */
952 16, /* rightshift */
953 1, /* size (0 = byte, 1 = short, 2 = long) */
954 16, /* bitsize */
955 FALSE, /* pc_relative */
956 0, /* bitpos */
957 complain_overflow_dont, /* complain_on_overflow */
958 ppc64_elf_toc_reloc, /* special_function */
959 "R_PPC64_TOC16_HI", /* name */
960 FALSE, /* partial_inplace */
961 0, /* src_mask */
962 0xffff, /* dst_mask */
963 FALSE), /* pcrel_offset */
965 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
966 contents of the low 16 bits, treated as a signed number, is
967 negative. */
969 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
970 HOWTO (R_PPC64_TOC16_HA, /* type */
971 16, /* rightshift */
972 1, /* size (0 = byte, 1 = short, 2 = long) */
973 16, /* bitsize */
974 FALSE, /* pc_relative */
975 0, /* bitpos */
976 complain_overflow_dont, /* complain_on_overflow */
977 ppc64_elf_toc_ha_reloc, /* special_function */
978 "R_PPC64_TOC16_HA", /* name */
979 FALSE, /* partial_inplace */
980 0, /* src_mask */
981 0xffff, /* dst_mask */
982 FALSE), /* pcrel_offset */
984 /* 64-bit relocation; insert value of TOC base (.TOC.). */
986 /* R_PPC64_TOC 51 doubleword64 .TOC. */
987 HOWTO (R_PPC64_TOC, /* type */
988 0, /* rightshift */
989 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
990 64, /* bitsize */
991 FALSE, /* pc_relative */
992 0, /* bitpos */
993 complain_overflow_bitfield, /* complain_on_overflow */
994 ppc64_elf_toc64_reloc, /* special_function */
995 "R_PPC64_TOC", /* name */
996 FALSE, /* partial_inplace */
997 0, /* src_mask */
998 ONES (64), /* dst_mask */
999 FALSE), /* pcrel_offset */
1001 /* Like R_PPC64_GOT16, but also informs the link editor that the
1002 value to relocate may (!) refer to a PLT entry which the link
1003 editor (a) may replace with the symbol value. If the link editor
1004 is unable to fully resolve the symbol, it may (b) create a PLT
1005 entry and store the address to the new PLT entry in the GOT.
1006 This permits lazy resolution of function symbols at run time.
1007 The link editor may also skip all of this and just (c) emit a
1008 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1009 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1010 HOWTO (R_PPC64_PLTGOT16, /* type */
1011 0, /* rightshift */
1012 1, /* size (0 = byte, 1 = short, 2 = long) */
1013 16, /* bitsize */
1014 FALSE, /* pc_relative */
1015 0, /* bitpos */
1016 complain_overflow_signed, /* complain_on_overflow */
1017 ppc64_elf_unhandled_reloc, /* special_function */
1018 "R_PPC64_PLTGOT16", /* name */
1019 FALSE, /* partial_inplace */
1020 0, /* src_mask */
1021 0xffff, /* dst_mask */
1022 FALSE), /* pcrel_offset */
1024 /* Like R_PPC64_PLTGOT16, but without overflow. */
1025 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1026 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1027 0, /* rightshift */
1028 1, /* size (0 = byte, 1 = short, 2 = long) */
1029 16, /* bitsize */
1030 FALSE, /* pc_relative */
1031 0, /* bitpos */
1032 complain_overflow_dont, /* complain_on_overflow */
1033 ppc64_elf_unhandled_reloc, /* special_function */
1034 "R_PPC64_PLTGOT16_LO", /* name */
1035 FALSE, /* partial_inplace */
1036 0, /* src_mask */
1037 0xffff, /* dst_mask */
1038 FALSE), /* pcrel_offset */
1040 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1041 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1042 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1043 16, /* rightshift */
1044 1, /* size (0 = byte, 1 = short, 2 = long) */
1045 16, /* bitsize */
1046 FALSE, /* pc_relative */
1047 0, /* bitpos */
1048 complain_overflow_dont, /* complain_on_overflow */
1049 ppc64_elf_unhandled_reloc, /* special_function */
1050 "R_PPC64_PLTGOT16_HI", /* name */
1051 FALSE, /* partial_inplace */
1052 0, /* src_mask */
1053 0xffff, /* dst_mask */
1054 FALSE), /* pcrel_offset */
1056 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1057 1 if the contents of the low 16 bits, treated as a signed number,
1058 is negative. */
1059 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1060 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1061 16, /* rightshift */
1062 1, /* size (0 = byte, 1 = short, 2 = long) */
1063 16, /* bitsize */
1064 FALSE, /* pc_relative */
1065 0, /* bitpos */
1066 complain_overflow_dont,/* complain_on_overflow */
1067 ppc64_elf_unhandled_reloc, /* special_function */
1068 "R_PPC64_PLTGOT16_HA", /* name */
1069 FALSE, /* partial_inplace */
1070 0, /* src_mask */
1071 0xffff, /* dst_mask */
1072 FALSE), /* pcrel_offset */
1074 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1075 HOWTO (R_PPC64_ADDR16_DS, /* type */
1076 0, /* rightshift */
1077 1, /* size (0 = byte, 1 = short, 2 = long) */
1078 16, /* bitsize */
1079 FALSE, /* pc_relative */
1080 0, /* bitpos */
1081 complain_overflow_bitfield, /* complain_on_overflow */
1082 bfd_elf_generic_reloc, /* special_function */
1083 "R_PPC64_ADDR16_DS", /* name */
1084 FALSE, /* partial_inplace */
1085 0, /* src_mask */
1086 0xfffc, /* dst_mask */
1087 FALSE), /* pcrel_offset */
1089 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1090 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1091 0, /* rightshift */
1092 1, /* size (0 = byte, 1 = short, 2 = long) */
1093 16, /* bitsize */
1094 FALSE, /* pc_relative */
1095 0, /* bitpos */
1096 complain_overflow_dont,/* complain_on_overflow */
1097 bfd_elf_generic_reloc, /* special_function */
1098 "R_PPC64_ADDR16_LO_DS",/* name */
1099 FALSE, /* partial_inplace */
1100 0, /* src_mask */
1101 0xfffc, /* dst_mask */
1102 FALSE), /* pcrel_offset */
1104 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1105 HOWTO (R_PPC64_GOT16_DS, /* type */
1106 0, /* rightshift */
1107 1, /* size (0 = byte, 1 = short, 2 = long) */
1108 16, /* bitsize */
1109 FALSE, /* pc_relative */
1110 0, /* bitpos */
1111 complain_overflow_signed, /* complain_on_overflow */
1112 ppc64_elf_unhandled_reloc, /* special_function */
1113 "R_PPC64_GOT16_DS", /* name */
1114 FALSE, /* partial_inplace */
1115 0, /* src_mask */
1116 0xfffc, /* dst_mask */
1117 FALSE), /* pcrel_offset */
1119 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1120 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1121 0, /* rightshift */
1122 1, /* size (0 = byte, 1 = short, 2 = long) */
1123 16, /* bitsize */
1124 FALSE, /* pc_relative */
1125 0, /* bitpos */
1126 complain_overflow_dont, /* complain_on_overflow */
1127 ppc64_elf_unhandled_reloc, /* special_function */
1128 "R_PPC64_GOT16_LO_DS", /* name */
1129 FALSE, /* partial_inplace */
1130 0, /* src_mask */
1131 0xfffc, /* dst_mask */
1132 FALSE), /* pcrel_offset */
1134 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1135 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1136 0, /* rightshift */
1137 1, /* size (0 = byte, 1 = short, 2 = long) */
1138 16, /* bitsize */
1139 FALSE, /* pc_relative */
1140 0, /* bitpos */
1141 complain_overflow_dont, /* complain_on_overflow */
1142 ppc64_elf_unhandled_reloc, /* special_function */
1143 "R_PPC64_PLT16_LO_DS", /* name */
1144 FALSE, /* partial_inplace */
1145 0, /* src_mask */
1146 0xfffc, /* dst_mask */
1147 FALSE), /* pcrel_offset */
1149 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1150 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1151 0, /* rightshift */
1152 1, /* size (0 = byte, 1 = short, 2 = long) */
1153 16, /* bitsize */
1154 FALSE, /* pc_relative */
1155 0, /* bitpos */
1156 complain_overflow_bitfield, /* complain_on_overflow */
1157 ppc64_elf_sectoff_reloc, /* special_function */
1158 "R_PPC64_SECTOFF_DS", /* name */
1159 FALSE, /* partial_inplace */
1160 0, /* src_mask */
1161 0xfffc, /* dst_mask */
1162 FALSE), /* pcrel_offset */
1164 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1165 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1166 0, /* rightshift */
1167 1, /* size (0 = byte, 1 = short, 2 = long) */
1168 16, /* bitsize */
1169 FALSE, /* pc_relative */
1170 0, /* bitpos */
1171 complain_overflow_dont, /* complain_on_overflow */
1172 ppc64_elf_sectoff_reloc, /* special_function */
1173 "R_PPC64_SECTOFF_LO_DS",/* name */
1174 FALSE, /* partial_inplace */
1175 0, /* src_mask */
1176 0xfffc, /* dst_mask */
1177 FALSE), /* pcrel_offset */
1179 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1180 HOWTO (R_PPC64_TOC16_DS, /* type */
1181 0, /* rightshift */
1182 1, /* size (0 = byte, 1 = short, 2 = long) */
1183 16, /* bitsize */
1184 FALSE, /* pc_relative */
1185 0, /* bitpos */
1186 complain_overflow_signed, /* complain_on_overflow */
1187 ppc64_elf_toc_reloc, /* special_function */
1188 "R_PPC64_TOC16_DS", /* name */
1189 FALSE, /* partial_inplace */
1190 0, /* src_mask */
1191 0xfffc, /* dst_mask */
1192 FALSE), /* pcrel_offset */
1194 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1195 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1196 0, /* rightshift */
1197 1, /* size (0 = byte, 1 = short, 2 = long) */
1198 16, /* bitsize */
1199 FALSE, /* pc_relative */
1200 0, /* bitpos */
1201 complain_overflow_dont, /* complain_on_overflow */
1202 ppc64_elf_toc_reloc, /* special_function */
1203 "R_PPC64_TOC16_LO_DS", /* name */
1204 FALSE, /* partial_inplace */
1205 0, /* src_mask */
1206 0xfffc, /* dst_mask */
1207 FALSE), /* pcrel_offset */
1209 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1210 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1211 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1212 0, /* rightshift */
1213 1, /* size (0 = byte, 1 = short, 2 = long) */
1214 16, /* bitsize */
1215 FALSE, /* pc_relative */
1216 0, /* bitpos */
1217 complain_overflow_signed, /* complain_on_overflow */
1218 ppc64_elf_unhandled_reloc, /* special_function */
1219 "R_PPC64_PLTGOT16_DS", /* name */
1220 FALSE, /* partial_inplace */
1221 0, /* src_mask */
1222 0xfffc, /* dst_mask */
1223 FALSE), /* pcrel_offset */
1225 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1226 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1227 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1228 0, /* rightshift */
1229 1, /* size (0 = byte, 1 = short, 2 = long) */
1230 16, /* bitsize */
1231 FALSE, /* pc_relative */
1232 0, /* bitpos */
1233 complain_overflow_dont, /* complain_on_overflow */
1234 ppc64_elf_unhandled_reloc, /* special_function */
1235 "R_PPC64_PLTGOT16_LO_DS",/* name */
1236 FALSE, /* partial_inplace */
1237 0, /* src_mask */
1238 0xfffc, /* dst_mask */
1239 FALSE), /* pcrel_offset */
1241 /* Marker relocs for TLS. */
1242 HOWTO (R_PPC64_TLS,
1243 0, /* rightshift */
1244 2, /* size (0 = byte, 1 = short, 2 = long) */
1245 32, /* bitsize */
1246 FALSE, /* pc_relative */
1247 0, /* bitpos */
1248 complain_overflow_dont, /* complain_on_overflow */
1249 bfd_elf_generic_reloc, /* special_function */
1250 "R_PPC64_TLS", /* name */
1251 FALSE, /* partial_inplace */
1252 0, /* src_mask */
1253 0, /* dst_mask */
1254 FALSE), /* pcrel_offset */
1256 HOWTO (R_PPC64_TLSGD,
1257 0, /* rightshift */
1258 2, /* size (0 = byte, 1 = short, 2 = long) */
1259 32, /* bitsize */
1260 FALSE, /* pc_relative */
1261 0, /* bitpos */
1262 complain_overflow_dont, /* complain_on_overflow */
1263 bfd_elf_generic_reloc, /* special_function */
1264 "R_PPC64_TLSGD", /* name */
1265 FALSE, /* partial_inplace */
1266 0, /* src_mask */
1267 0, /* dst_mask */
1268 FALSE), /* pcrel_offset */
1270 HOWTO (R_PPC64_TLSLD,
1271 0, /* rightshift */
1272 2, /* size (0 = byte, 1 = short, 2 = long) */
1273 32, /* bitsize */
1274 FALSE, /* pc_relative */
1275 0, /* bitpos */
1276 complain_overflow_dont, /* complain_on_overflow */
1277 bfd_elf_generic_reloc, /* special_function */
1278 "R_PPC64_TLSLD", /* name */
1279 FALSE, /* partial_inplace */
1280 0, /* src_mask */
1281 0, /* dst_mask */
1282 FALSE), /* pcrel_offset */
1284 /* Computes the load module index of the load module that contains the
1285 definition of its TLS sym. */
1286 HOWTO (R_PPC64_DTPMOD64,
1287 0, /* rightshift */
1288 4, /* size (0 = byte, 1 = short, 2 = long) */
1289 64, /* bitsize */
1290 FALSE, /* pc_relative */
1291 0, /* bitpos */
1292 complain_overflow_dont, /* complain_on_overflow */
1293 ppc64_elf_unhandled_reloc, /* special_function */
1294 "R_PPC64_DTPMOD64", /* name */
1295 FALSE, /* partial_inplace */
1296 0, /* src_mask */
1297 ONES (64), /* dst_mask */
1298 FALSE), /* pcrel_offset */
1300 /* Computes a dtv-relative displacement, the difference between the value
1301 of sym+add and the base address of the thread-local storage block that
1302 contains the definition of sym, minus 0x8000. */
1303 HOWTO (R_PPC64_DTPREL64,
1304 0, /* rightshift */
1305 4, /* size (0 = byte, 1 = short, 2 = long) */
1306 64, /* bitsize */
1307 FALSE, /* pc_relative */
1308 0, /* bitpos */
1309 complain_overflow_dont, /* complain_on_overflow */
1310 ppc64_elf_unhandled_reloc, /* special_function */
1311 "R_PPC64_DTPREL64", /* name */
1312 FALSE, /* partial_inplace */
1313 0, /* src_mask */
1314 ONES (64), /* dst_mask */
1315 FALSE), /* pcrel_offset */
1317 /* A 16 bit dtprel reloc. */
1318 HOWTO (R_PPC64_DTPREL16,
1319 0, /* rightshift */
1320 1, /* size (0 = byte, 1 = short, 2 = long) */
1321 16, /* bitsize */
1322 FALSE, /* pc_relative */
1323 0, /* bitpos */
1324 complain_overflow_signed, /* complain_on_overflow */
1325 ppc64_elf_unhandled_reloc, /* special_function */
1326 "R_PPC64_DTPREL16", /* name */
1327 FALSE, /* partial_inplace */
1328 0, /* src_mask */
1329 0xffff, /* dst_mask */
1330 FALSE), /* pcrel_offset */
1332 /* Like DTPREL16, but no overflow. */
1333 HOWTO (R_PPC64_DTPREL16_LO,
1334 0, /* rightshift */
1335 1, /* size (0 = byte, 1 = short, 2 = long) */
1336 16, /* bitsize */
1337 FALSE, /* pc_relative */
1338 0, /* bitpos */
1339 complain_overflow_dont, /* complain_on_overflow */
1340 ppc64_elf_unhandled_reloc, /* special_function */
1341 "R_PPC64_DTPREL16_LO", /* name */
1342 FALSE, /* partial_inplace */
1343 0, /* src_mask */
1344 0xffff, /* dst_mask */
1345 FALSE), /* pcrel_offset */
1347 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1348 HOWTO (R_PPC64_DTPREL16_HI,
1349 16, /* rightshift */
1350 1, /* size (0 = byte, 1 = short, 2 = long) */
1351 16, /* bitsize */
1352 FALSE, /* pc_relative */
1353 0, /* bitpos */
1354 complain_overflow_dont, /* complain_on_overflow */
1355 ppc64_elf_unhandled_reloc, /* special_function */
1356 "R_PPC64_DTPREL16_HI", /* name */
1357 FALSE, /* partial_inplace */
1358 0, /* src_mask */
1359 0xffff, /* dst_mask */
1360 FALSE), /* pcrel_offset */
1362 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1363 HOWTO (R_PPC64_DTPREL16_HA,
1364 16, /* rightshift */
1365 1, /* size (0 = byte, 1 = short, 2 = long) */
1366 16, /* bitsize */
1367 FALSE, /* pc_relative */
1368 0, /* bitpos */
1369 complain_overflow_dont, /* complain_on_overflow */
1370 ppc64_elf_unhandled_reloc, /* special_function */
1371 "R_PPC64_DTPREL16_HA", /* name */
1372 FALSE, /* partial_inplace */
1373 0, /* src_mask */
1374 0xffff, /* dst_mask */
1375 FALSE), /* pcrel_offset */
1377 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1378 HOWTO (R_PPC64_DTPREL16_HIGHER,
1379 32, /* rightshift */
1380 1, /* size (0 = byte, 1 = short, 2 = long) */
1381 16, /* bitsize */
1382 FALSE, /* pc_relative */
1383 0, /* bitpos */
1384 complain_overflow_dont, /* complain_on_overflow */
1385 ppc64_elf_unhandled_reloc, /* special_function */
1386 "R_PPC64_DTPREL16_HIGHER", /* name */
1387 FALSE, /* partial_inplace */
1388 0, /* src_mask */
1389 0xffff, /* dst_mask */
1390 FALSE), /* pcrel_offset */
1392 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1393 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1394 32, /* rightshift */
1395 1, /* size (0 = byte, 1 = short, 2 = long) */
1396 16, /* bitsize */
1397 FALSE, /* pc_relative */
1398 0, /* bitpos */
1399 complain_overflow_dont, /* complain_on_overflow */
1400 ppc64_elf_unhandled_reloc, /* special_function */
1401 "R_PPC64_DTPREL16_HIGHERA", /* name */
1402 FALSE, /* partial_inplace */
1403 0, /* src_mask */
1404 0xffff, /* dst_mask */
1405 FALSE), /* pcrel_offset */
1407 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1408 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1409 48, /* rightshift */
1410 1, /* size (0 = byte, 1 = short, 2 = long) */
1411 16, /* bitsize */
1412 FALSE, /* pc_relative */
1413 0, /* bitpos */
1414 complain_overflow_dont, /* complain_on_overflow */
1415 ppc64_elf_unhandled_reloc, /* special_function */
1416 "R_PPC64_DTPREL16_HIGHEST", /* name */
1417 FALSE, /* partial_inplace */
1418 0, /* src_mask */
1419 0xffff, /* dst_mask */
1420 FALSE), /* pcrel_offset */
1422 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1423 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1424 48, /* rightshift */
1425 1, /* size (0 = byte, 1 = short, 2 = long) */
1426 16, /* bitsize */
1427 FALSE, /* pc_relative */
1428 0, /* bitpos */
1429 complain_overflow_dont, /* complain_on_overflow */
1430 ppc64_elf_unhandled_reloc, /* special_function */
1431 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1432 FALSE, /* partial_inplace */
1433 0, /* src_mask */
1434 0xffff, /* dst_mask */
1435 FALSE), /* pcrel_offset */
1437 /* Like DTPREL16, but for insns with a DS field. */
1438 HOWTO (R_PPC64_DTPREL16_DS,
1439 0, /* rightshift */
1440 1, /* size (0 = byte, 1 = short, 2 = long) */
1441 16, /* bitsize */
1442 FALSE, /* pc_relative */
1443 0, /* bitpos */
1444 complain_overflow_signed, /* complain_on_overflow */
1445 ppc64_elf_unhandled_reloc, /* special_function */
1446 "R_PPC64_DTPREL16_DS", /* name */
1447 FALSE, /* partial_inplace */
1448 0, /* src_mask */
1449 0xfffc, /* dst_mask */
1450 FALSE), /* pcrel_offset */
1452 /* Like DTPREL16_DS, but no overflow. */
1453 HOWTO (R_PPC64_DTPREL16_LO_DS,
1454 0, /* rightshift */
1455 1, /* size (0 = byte, 1 = short, 2 = long) */
1456 16, /* bitsize */
1457 FALSE, /* pc_relative */
1458 0, /* bitpos */
1459 complain_overflow_dont, /* complain_on_overflow */
1460 ppc64_elf_unhandled_reloc, /* special_function */
1461 "R_PPC64_DTPREL16_LO_DS", /* name */
1462 FALSE, /* partial_inplace */
1463 0, /* src_mask */
1464 0xfffc, /* dst_mask */
1465 FALSE), /* pcrel_offset */
1467 /* Computes a tp-relative displacement, the difference between the value of
1468 sym+add and the value of the thread pointer (r13). */
1469 HOWTO (R_PPC64_TPREL64,
1470 0, /* rightshift */
1471 4, /* size (0 = byte, 1 = short, 2 = long) */
1472 64, /* bitsize */
1473 FALSE, /* pc_relative */
1474 0, /* bitpos */
1475 complain_overflow_dont, /* complain_on_overflow */
1476 ppc64_elf_unhandled_reloc, /* special_function */
1477 "R_PPC64_TPREL64", /* name */
1478 FALSE, /* partial_inplace */
1479 0, /* src_mask */
1480 ONES (64), /* dst_mask */
1481 FALSE), /* pcrel_offset */
1483 /* A 16 bit tprel reloc. */
1484 HOWTO (R_PPC64_TPREL16,
1485 0, /* rightshift */
1486 1, /* size (0 = byte, 1 = short, 2 = long) */
1487 16, /* bitsize */
1488 FALSE, /* pc_relative */
1489 0, /* bitpos */
1490 complain_overflow_signed, /* complain_on_overflow */
1491 ppc64_elf_unhandled_reloc, /* special_function */
1492 "R_PPC64_TPREL16", /* name */
1493 FALSE, /* partial_inplace */
1494 0, /* src_mask */
1495 0xffff, /* dst_mask */
1496 FALSE), /* pcrel_offset */
1498 /* Like TPREL16, but no overflow. */
1499 HOWTO (R_PPC64_TPREL16_LO,
1500 0, /* rightshift */
1501 1, /* size (0 = byte, 1 = short, 2 = long) */
1502 16, /* bitsize */
1503 FALSE, /* pc_relative */
1504 0, /* bitpos */
1505 complain_overflow_dont, /* complain_on_overflow */
1506 ppc64_elf_unhandled_reloc, /* special_function */
1507 "R_PPC64_TPREL16_LO", /* name */
1508 FALSE, /* partial_inplace */
1509 0, /* src_mask */
1510 0xffff, /* dst_mask */
1511 FALSE), /* pcrel_offset */
1513 /* Like TPREL16_LO, but next higher group of 16 bits. */
1514 HOWTO (R_PPC64_TPREL16_HI,
1515 16, /* rightshift */
1516 1, /* size (0 = byte, 1 = short, 2 = long) */
1517 16, /* bitsize */
1518 FALSE, /* pc_relative */
1519 0, /* bitpos */
1520 complain_overflow_dont, /* complain_on_overflow */
1521 ppc64_elf_unhandled_reloc, /* special_function */
1522 "R_PPC64_TPREL16_HI", /* name */
1523 FALSE, /* partial_inplace */
1524 0, /* src_mask */
1525 0xffff, /* dst_mask */
1526 FALSE), /* pcrel_offset */
1528 /* Like TPREL16_HI, but adjust for low 16 bits. */
1529 HOWTO (R_PPC64_TPREL16_HA,
1530 16, /* rightshift */
1531 1, /* size (0 = byte, 1 = short, 2 = long) */
1532 16, /* bitsize */
1533 FALSE, /* pc_relative */
1534 0, /* bitpos */
1535 complain_overflow_dont, /* complain_on_overflow */
1536 ppc64_elf_unhandled_reloc, /* special_function */
1537 "R_PPC64_TPREL16_HA", /* name */
1538 FALSE, /* partial_inplace */
1539 0, /* src_mask */
1540 0xffff, /* dst_mask */
1541 FALSE), /* pcrel_offset */
1543 /* Like TPREL16_HI, but next higher group of 16 bits. */
1544 HOWTO (R_PPC64_TPREL16_HIGHER,
1545 32, /* rightshift */
1546 1, /* size (0 = byte, 1 = short, 2 = long) */
1547 16, /* bitsize */
1548 FALSE, /* pc_relative */
1549 0, /* bitpos */
1550 complain_overflow_dont, /* complain_on_overflow */
1551 ppc64_elf_unhandled_reloc, /* special_function */
1552 "R_PPC64_TPREL16_HIGHER", /* name */
1553 FALSE, /* partial_inplace */
1554 0, /* src_mask */
1555 0xffff, /* dst_mask */
1556 FALSE), /* pcrel_offset */
1558 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1559 HOWTO (R_PPC64_TPREL16_HIGHERA,
1560 32, /* rightshift */
1561 1, /* size (0 = byte, 1 = short, 2 = long) */
1562 16, /* bitsize */
1563 FALSE, /* pc_relative */
1564 0, /* bitpos */
1565 complain_overflow_dont, /* complain_on_overflow */
1566 ppc64_elf_unhandled_reloc, /* special_function */
1567 "R_PPC64_TPREL16_HIGHERA", /* name */
1568 FALSE, /* partial_inplace */
1569 0, /* src_mask */
1570 0xffff, /* dst_mask */
1571 FALSE), /* pcrel_offset */
1573 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1574 HOWTO (R_PPC64_TPREL16_HIGHEST,
1575 48, /* rightshift */
1576 1, /* size (0 = byte, 1 = short, 2 = long) */
1577 16, /* bitsize */
1578 FALSE, /* pc_relative */
1579 0, /* bitpos */
1580 complain_overflow_dont, /* complain_on_overflow */
1581 ppc64_elf_unhandled_reloc, /* special_function */
1582 "R_PPC64_TPREL16_HIGHEST", /* name */
1583 FALSE, /* partial_inplace */
1584 0, /* src_mask */
1585 0xffff, /* dst_mask */
1586 FALSE), /* pcrel_offset */
1588 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1589 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1590 48, /* rightshift */
1591 1, /* size (0 = byte, 1 = short, 2 = long) */
1592 16, /* bitsize */
1593 FALSE, /* pc_relative */
1594 0, /* bitpos */
1595 complain_overflow_dont, /* complain_on_overflow */
1596 ppc64_elf_unhandled_reloc, /* special_function */
1597 "R_PPC64_TPREL16_HIGHESTA", /* name */
1598 FALSE, /* partial_inplace */
1599 0, /* src_mask */
1600 0xffff, /* dst_mask */
1601 FALSE), /* pcrel_offset */
1603 /* Like TPREL16, but for insns with a DS field. */
1604 HOWTO (R_PPC64_TPREL16_DS,
1605 0, /* rightshift */
1606 1, /* size (0 = byte, 1 = short, 2 = long) */
1607 16, /* bitsize */
1608 FALSE, /* pc_relative */
1609 0, /* bitpos */
1610 complain_overflow_signed, /* complain_on_overflow */
1611 ppc64_elf_unhandled_reloc, /* special_function */
1612 "R_PPC64_TPREL16_DS", /* name */
1613 FALSE, /* partial_inplace */
1614 0, /* src_mask */
1615 0xfffc, /* dst_mask */
1616 FALSE), /* pcrel_offset */
1618 /* Like TPREL16_DS, but no overflow. */
1619 HOWTO (R_PPC64_TPREL16_LO_DS,
1620 0, /* rightshift */
1621 1, /* size (0 = byte, 1 = short, 2 = long) */
1622 16, /* bitsize */
1623 FALSE, /* pc_relative */
1624 0, /* bitpos */
1625 complain_overflow_dont, /* complain_on_overflow */
1626 ppc64_elf_unhandled_reloc, /* special_function */
1627 "R_PPC64_TPREL16_LO_DS", /* name */
1628 FALSE, /* partial_inplace */
1629 0, /* src_mask */
1630 0xfffc, /* dst_mask */
1631 FALSE), /* pcrel_offset */
1633 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1634 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1635 to the first entry relative to the TOC base (r2). */
1636 HOWTO (R_PPC64_GOT_TLSGD16,
1637 0, /* rightshift */
1638 1, /* size (0 = byte, 1 = short, 2 = long) */
1639 16, /* bitsize */
1640 FALSE, /* pc_relative */
1641 0, /* bitpos */
1642 complain_overflow_signed, /* complain_on_overflow */
1643 ppc64_elf_unhandled_reloc, /* special_function */
1644 "R_PPC64_GOT_TLSGD16", /* name */
1645 FALSE, /* partial_inplace */
1646 0, /* src_mask */
1647 0xffff, /* dst_mask */
1648 FALSE), /* pcrel_offset */
1650 /* Like GOT_TLSGD16, but no overflow. */
1651 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1652 0, /* rightshift */
1653 1, /* size (0 = byte, 1 = short, 2 = long) */
1654 16, /* bitsize */
1655 FALSE, /* pc_relative */
1656 0, /* bitpos */
1657 complain_overflow_dont, /* complain_on_overflow */
1658 ppc64_elf_unhandled_reloc, /* special_function */
1659 "R_PPC64_GOT_TLSGD16_LO", /* name */
1660 FALSE, /* partial_inplace */
1661 0, /* src_mask */
1662 0xffff, /* dst_mask */
1663 FALSE), /* pcrel_offset */
1665 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1666 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1667 16, /* rightshift */
1668 1, /* size (0 = byte, 1 = short, 2 = long) */
1669 16, /* bitsize */
1670 FALSE, /* pc_relative */
1671 0, /* bitpos */
1672 complain_overflow_dont, /* complain_on_overflow */
1673 ppc64_elf_unhandled_reloc, /* special_function */
1674 "R_PPC64_GOT_TLSGD16_HI", /* name */
1675 FALSE, /* partial_inplace */
1676 0, /* src_mask */
1677 0xffff, /* dst_mask */
1678 FALSE), /* pcrel_offset */
1680 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1681 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1682 16, /* rightshift */
1683 1, /* size (0 = byte, 1 = short, 2 = long) */
1684 16, /* bitsize */
1685 FALSE, /* pc_relative */
1686 0, /* bitpos */
1687 complain_overflow_dont, /* complain_on_overflow */
1688 ppc64_elf_unhandled_reloc, /* special_function */
1689 "R_PPC64_GOT_TLSGD16_HA", /* name */
1690 FALSE, /* partial_inplace */
1691 0, /* src_mask */
1692 0xffff, /* dst_mask */
1693 FALSE), /* pcrel_offset */
1695 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1696 with values (sym+add)@dtpmod and zero, and computes the offset to the
1697 first entry relative to the TOC base (r2). */
1698 HOWTO (R_PPC64_GOT_TLSLD16,
1699 0, /* rightshift */
1700 1, /* size (0 = byte, 1 = short, 2 = long) */
1701 16, /* bitsize */
1702 FALSE, /* pc_relative */
1703 0, /* bitpos */
1704 complain_overflow_signed, /* complain_on_overflow */
1705 ppc64_elf_unhandled_reloc, /* special_function */
1706 "R_PPC64_GOT_TLSLD16", /* name */
1707 FALSE, /* partial_inplace */
1708 0, /* src_mask */
1709 0xffff, /* dst_mask */
1710 FALSE), /* pcrel_offset */
1712 /* Like GOT_TLSLD16, but no overflow. */
1713 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1714 0, /* rightshift */
1715 1, /* size (0 = byte, 1 = short, 2 = long) */
1716 16, /* bitsize */
1717 FALSE, /* pc_relative */
1718 0, /* bitpos */
1719 complain_overflow_dont, /* complain_on_overflow */
1720 ppc64_elf_unhandled_reloc, /* special_function */
1721 "R_PPC64_GOT_TLSLD16_LO", /* name */
1722 FALSE, /* partial_inplace */
1723 0, /* src_mask */
1724 0xffff, /* dst_mask */
1725 FALSE), /* pcrel_offset */
1727 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1728 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1729 16, /* rightshift */
1730 1, /* size (0 = byte, 1 = short, 2 = long) */
1731 16, /* bitsize */
1732 FALSE, /* pc_relative */
1733 0, /* bitpos */
1734 complain_overflow_dont, /* complain_on_overflow */
1735 ppc64_elf_unhandled_reloc, /* special_function */
1736 "R_PPC64_GOT_TLSLD16_HI", /* name */
1737 FALSE, /* partial_inplace */
1738 0, /* src_mask */
1739 0xffff, /* dst_mask */
1740 FALSE), /* pcrel_offset */
1742 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1743 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1744 16, /* rightshift */
1745 1, /* size (0 = byte, 1 = short, 2 = long) */
1746 16, /* bitsize */
1747 FALSE, /* pc_relative */
1748 0, /* bitpos */
1749 complain_overflow_dont, /* complain_on_overflow */
1750 ppc64_elf_unhandled_reloc, /* special_function */
1751 "R_PPC64_GOT_TLSLD16_HA", /* name */
1752 FALSE, /* partial_inplace */
1753 0, /* src_mask */
1754 0xffff, /* dst_mask */
1755 FALSE), /* pcrel_offset */
1757 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1758 the offset to the entry relative to the TOC base (r2). */
1759 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1760 0, /* rightshift */
1761 1, /* size (0 = byte, 1 = short, 2 = long) */
1762 16, /* bitsize */
1763 FALSE, /* pc_relative */
1764 0, /* bitpos */
1765 complain_overflow_signed, /* complain_on_overflow */
1766 ppc64_elf_unhandled_reloc, /* special_function */
1767 "R_PPC64_GOT_DTPREL16_DS", /* name */
1768 FALSE, /* partial_inplace */
1769 0, /* src_mask */
1770 0xfffc, /* dst_mask */
1771 FALSE), /* pcrel_offset */
1773 /* Like GOT_DTPREL16_DS, but no overflow. */
1774 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1775 0, /* rightshift */
1776 1, /* size (0 = byte, 1 = short, 2 = long) */
1777 16, /* bitsize */
1778 FALSE, /* pc_relative */
1779 0, /* bitpos */
1780 complain_overflow_dont, /* complain_on_overflow */
1781 ppc64_elf_unhandled_reloc, /* special_function */
1782 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1783 FALSE, /* partial_inplace */
1784 0, /* src_mask */
1785 0xfffc, /* dst_mask */
1786 FALSE), /* pcrel_offset */
1788 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1789 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1790 16, /* rightshift */
1791 1, /* size (0 = byte, 1 = short, 2 = long) */
1792 16, /* bitsize */
1793 FALSE, /* pc_relative */
1794 0, /* bitpos */
1795 complain_overflow_dont, /* complain_on_overflow */
1796 ppc64_elf_unhandled_reloc, /* special_function */
1797 "R_PPC64_GOT_DTPREL16_HI", /* name */
1798 FALSE, /* partial_inplace */
1799 0, /* src_mask */
1800 0xffff, /* dst_mask */
1801 FALSE), /* pcrel_offset */
1803 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1804 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1805 16, /* rightshift */
1806 1, /* size (0 = byte, 1 = short, 2 = long) */
1807 16, /* bitsize */
1808 FALSE, /* pc_relative */
1809 0, /* bitpos */
1810 complain_overflow_dont, /* complain_on_overflow */
1811 ppc64_elf_unhandled_reloc, /* special_function */
1812 "R_PPC64_GOT_DTPREL16_HA", /* name */
1813 FALSE, /* partial_inplace */
1814 0, /* src_mask */
1815 0xffff, /* dst_mask */
1816 FALSE), /* pcrel_offset */
1818 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1819 offset to the entry relative to the TOC base (r2). */
1820 HOWTO (R_PPC64_GOT_TPREL16_DS,
1821 0, /* rightshift */
1822 1, /* size (0 = byte, 1 = short, 2 = long) */
1823 16, /* bitsize */
1824 FALSE, /* pc_relative */
1825 0, /* bitpos */
1826 complain_overflow_signed, /* complain_on_overflow */
1827 ppc64_elf_unhandled_reloc, /* special_function */
1828 "R_PPC64_GOT_TPREL16_DS", /* name */
1829 FALSE, /* partial_inplace */
1830 0, /* src_mask */
1831 0xfffc, /* dst_mask */
1832 FALSE), /* pcrel_offset */
1834 /* Like GOT_TPREL16_DS, but no overflow. */
1835 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1836 0, /* rightshift */
1837 1, /* size (0 = byte, 1 = short, 2 = long) */
1838 16, /* bitsize */
1839 FALSE, /* pc_relative */
1840 0, /* bitpos */
1841 complain_overflow_dont, /* complain_on_overflow */
1842 ppc64_elf_unhandled_reloc, /* special_function */
1843 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1844 FALSE, /* partial_inplace */
1845 0, /* src_mask */
1846 0xfffc, /* dst_mask */
1847 FALSE), /* pcrel_offset */
1849 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1850 HOWTO (R_PPC64_GOT_TPREL16_HI,
1851 16, /* rightshift */
1852 1, /* size (0 = byte, 1 = short, 2 = long) */
1853 16, /* bitsize */
1854 FALSE, /* pc_relative */
1855 0, /* bitpos */
1856 complain_overflow_dont, /* complain_on_overflow */
1857 ppc64_elf_unhandled_reloc, /* special_function */
1858 "R_PPC64_GOT_TPREL16_HI", /* name */
1859 FALSE, /* partial_inplace */
1860 0, /* src_mask */
1861 0xffff, /* dst_mask */
1862 FALSE), /* pcrel_offset */
1864 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1865 HOWTO (R_PPC64_GOT_TPREL16_HA,
1866 16, /* rightshift */
1867 1, /* size (0 = byte, 1 = short, 2 = long) */
1868 16, /* bitsize */
1869 FALSE, /* pc_relative */
1870 0, /* bitpos */
1871 complain_overflow_dont, /* complain_on_overflow */
1872 ppc64_elf_unhandled_reloc, /* special_function */
1873 "R_PPC64_GOT_TPREL16_HA", /* name */
1874 FALSE, /* partial_inplace */
1875 0, /* src_mask */
1876 0xffff, /* dst_mask */
1877 FALSE), /* pcrel_offset */
1879 HOWTO (R_PPC64_JMP_IREL, /* type */
1880 0, /* rightshift */
1881 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1882 0, /* bitsize */
1883 FALSE, /* pc_relative */
1884 0, /* bitpos */
1885 complain_overflow_dont, /* complain_on_overflow */
1886 ppc64_elf_unhandled_reloc, /* special_function */
1887 "R_PPC64_JMP_IREL", /* name */
1888 FALSE, /* partial_inplace */
1889 0, /* src_mask */
1890 0, /* dst_mask */
1891 FALSE), /* pcrel_offset */
1893 HOWTO (R_PPC64_IRELATIVE, /* type */
1894 0, /* rightshift */
1895 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1896 64, /* bitsize */
1897 FALSE, /* pc_relative */
1898 0, /* bitpos */
1899 complain_overflow_dont, /* complain_on_overflow */
1900 bfd_elf_generic_reloc, /* special_function */
1901 "R_PPC64_IRELATIVE", /* name */
1902 FALSE, /* partial_inplace */
1903 0, /* src_mask */
1904 ONES (64), /* dst_mask */
1905 FALSE), /* pcrel_offset */
1907 /* A 16 bit relative relocation. */
1908 HOWTO (R_PPC64_REL16, /* type */
1909 0, /* rightshift */
1910 1, /* size (0 = byte, 1 = short, 2 = long) */
1911 16, /* bitsize */
1912 TRUE, /* pc_relative */
1913 0, /* bitpos */
1914 complain_overflow_bitfield, /* complain_on_overflow */
1915 bfd_elf_generic_reloc, /* special_function */
1916 "R_PPC64_REL16", /* name */
1917 FALSE, /* partial_inplace */
1918 0, /* src_mask */
1919 0xffff, /* dst_mask */
1920 TRUE), /* pcrel_offset */
1922 /* A 16 bit relative relocation without overflow. */
1923 HOWTO (R_PPC64_REL16_LO, /* type */
1924 0, /* rightshift */
1925 1, /* size (0 = byte, 1 = short, 2 = long) */
1926 16, /* bitsize */
1927 TRUE, /* pc_relative */
1928 0, /* bitpos */
1929 complain_overflow_dont,/* complain_on_overflow */
1930 bfd_elf_generic_reloc, /* special_function */
1931 "R_PPC64_REL16_LO", /* name */
1932 FALSE, /* partial_inplace */
1933 0, /* src_mask */
1934 0xffff, /* dst_mask */
1935 TRUE), /* pcrel_offset */
1937 /* The high order 16 bits of a relative address. */
1938 HOWTO (R_PPC64_REL16_HI, /* type */
1939 16, /* rightshift */
1940 1, /* size (0 = byte, 1 = short, 2 = long) */
1941 16, /* bitsize */
1942 TRUE, /* pc_relative */
1943 0, /* bitpos */
1944 complain_overflow_dont, /* complain_on_overflow */
1945 bfd_elf_generic_reloc, /* special_function */
1946 "R_PPC64_REL16_HI", /* name */
1947 FALSE, /* partial_inplace */
1948 0, /* src_mask */
1949 0xffff, /* dst_mask */
1950 TRUE), /* pcrel_offset */
1952 /* The high order 16 bits of a relative address, plus 1 if the contents of
1953 the low 16 bits, treated as a signed number, is negative. */
1954 HOWTO (R_PPC64_REL16_HA, /* type */
1955 16, /* rightshift */
1956 1, /* size (0 = byte, 1 = short, 2 = long) */
1957 16, /* bitsize */
1958 TRUE, /* pc_relative */
1959 0, /* bitpos */
1960 complain_overflow_dont, /* complain_on_overflow */
1961 ppc64_elf_ha_reloc, /* special_function */
1962 "R_PPC64_REL16_HA", /* name */
1963 FALSE, /* partial_inplace */
1964 0, /* src_mask */
1965 0xffff, /* dst_mask */
1966 TRUE), /* pcrel_offset */
1968 /* GNU extension to record C++ vtable hierarchy. */
1969 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1970 0, /* rightshift */
1971 0, /* size (0 = byte, 1 = short, 2 = long) */
1972 0, /* bitsize */
1973 FALSE, /* pc_relative */
1974 0, /* bitpos */
1975 complain_overflow_dont, /* complain_on_overflow */
1976 NULL, /* special_function */
1977 "R_PPC64_GNU_VTINHERIT", /* name */
1978 FALSE, /* partial_inplace */
1979 0, /* src_mask */
1980 0, /* dst_mask */
1981 FALSE), /* pcrel_offset */
1983 /* GNU extension to record C++ vtable member usage. */
1984 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1985 0, /* rightshift */
1986 0, /* size (0 = byte, 1 = short, 2 = long) */
1987 0, /* bitsize */
1988 FALSE, /* pc_relative */
1989 0, /* bitpos */
1990 complain_overflow_dont, /* complain_on_overflow */
1991 NULL, /* special_function */
1992 "R_PPC64_GNU_VTENTRY", /* name */
1993 FALSE, /* partial_inplace */
1994 0, /* src_mask */
1995 0, /* dst_mask */
1996 FALSE), /* pcrel_offset */
2000 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2001 be done. */
2003 static void
2004 ppc_howto_init (void)
2006 unsigned int i, type;
2008 for (i = 0;
2009 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2010 i++)
2012 type = ppc64_elf_howto_raw[i].type;
2013 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2014 / sizeof (ppc64_elf_howto_table[0])));
2015 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2019 static reloc_howto_type *
2020 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2021 bfd_reloc_code_real_type code)
2023 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2025 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2026 /* Initialize howto table if needed. */
2027 ppc_howto_init ();
2029 switch (code)
2031 default:
2032 return NULL;
2034 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2035 break;
2036 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2037 break;
2038 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2039 break;
2040 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2041 break;
2042 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2043 break;
2044 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2045 break;
2046 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2047 break;
2048 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2049 break;
2050 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2051 break;
2052 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2053 break;
2054 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2055 break;
2056 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2057 break;
2058 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2059 break;
2060 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2061 break;
2062 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2063 break;
2064 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2065 break;
2066 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2067 break;
2068 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2069 break;
2070 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2071 break;
2072 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2073 break;
2074 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2075 break;
2076 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2077 break;
2078 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2079 break;
2080 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2081 break;
2082 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2083 break;
2084 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2085 break;
2086 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2087 break;
2088 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2089 break;
2090 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2091 break;
2092 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2093 break;
2094 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2095 break;
2096 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2097 break;
2098 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2099 break;
2100 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2101 break;
2102 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2103 break;
2104 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2105 break;
2106 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2107 break;
2108 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2109 break;
2110 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2111 break;
2112 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2113 break;
2114 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2115 break;
2116 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2117 break;
2118 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2119 break;
2120 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2121 break;
2122 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2123 break;
2124 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2125 break;
2126 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2127 break;
2128 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2129 break;
2130 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2131 break;
2132 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2133 break;
2134 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2135 break;
2136 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2137 break;
2138 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2139 break;
2140 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2141 break;
2142 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2143 break;
2144 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2145 break;
2146 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2147 break;
2148 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2149 break;
2150 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2151 break;
2152 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2153 break;
2154 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2155 break;
2156 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2157 break;
2158 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2159 break;
2160 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2161 break;
2162 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2163 break;
2164 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2165 break;
2166 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2167 break;
2168 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2169 break;
2170 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2171 break;
2172 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2173 break;
2174 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2175 break;
2176 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2177 break;
2178 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2179 break;
2180 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2181 break;
2182 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2183 break;
2184 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2185 break;
2186 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2187 break;
2188 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2189 break;
2190 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2191 break;
2192 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2193 break;
2194 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2195 break;
2196 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2197 break;
2198 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2199 break;
2200 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2201 break;
2202 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2203 break;
2204 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2205 break;
2206 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2207 break;
2208 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2209 break;
2210 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2211 break;
2212 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2213 break;
2214 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2215 break;
2216 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2217 break;
2218 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2219 break;
2220 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2221 break;
2222 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2223 break;
2224 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2225 break;
2226 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2227 break;
2228 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2229 break;
2230 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2231 break;
2232 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2233 break;
2234 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2235 break;
2236 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2237 break;
2238 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2239 break;
2240 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2241 break;
2242 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2243 break;
2244 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2245 break;
2246 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2247 break;
2250 return ppc64_elf_howto_table[r];
2253 static reloc_howto_type *
2254 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2255 const char *r_name)
2257 unsigned int i;
2259 for (i = 0;
2260 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2261 i++)
2262 if (ppc64_elf_howto_raw[i].name != NULL
2263 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2264 return &ppc64_elf_howto_raw[i];
2266 return NULL;
2269 /* Set the howto pointer for a PowerPC ELF reloc. */
2271 static void
2272 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2273 Elf_Internal_Rela *dst)
2275 unsigned int type;
2277 /* Initialize howto table if needed. */
2278 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2279 ppc_howto_init ();
2281 type = ELF64_R_TYPE (dst->r_info);
2282 if (type >= (sizeof (ppc64_elf_howto_table)
2283 / sizeof (ppc64_elf_howto_table[0])))
2285 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2286 abfd, (int) type);
2287 type = R_PPC64_NONE;
2289 cache_ptr->howto = ppc64_elf_howto_table[type];
2292 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2294 static bfd_reloc_status_type
2295 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2296 void *data, asection *input_section,
2297 bfd *output_bfd, char **error_message)
2299 /* If this is a relocatable link (output_bfd test tells us), just
2300 call the generic function. Any adjustment will be done at final
2301 link time. */
2302 if (output_bfd != NULL)
2303 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2304 input_section, output_bfd, error_message);
2306 /* Adjust the addend for sign extension of the low 16 bits.
2307 We won't actually be using the low 16 bits, so trashing them
2308 doesn't matter. */
2309 reloc_entry->addend += 0x8000;
2310 return bfd_reloc_continue;
2313 static bfd_reloc_status_type
2314 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2315 void *data, asection *input_section,
2316 bfd *output_bfd, char **error_message)
2318 if (output_bfd != NULL)
2319 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2320 input_section, output_bfd, error_message);
2322 if (strcmp (symbol->section->name, ".opd") == 0
2323 && (symbol->section->owner->flags & DYNAMIC) == 0)
2325 bfd_vma dest = opd_entry_value (symbol->section,
2326 symbol->value + reloc_entry->addend,
2327 NULL, NULL);
2328 if (dest != (bfd_vma) -1)
2329 reloc_entry->addend = dest - (symbol->value
2330 + symbol->section->output_section->vma
2331 + symbol->section->output_offset);
2333 return bfd_reloc_continue;
2336 static bfd_reloc_status_type
2337 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2338 void *data, asection *input_section,
2339 bfd *output_bfd, char **error_message)
2341 long insn;
2342 enum elf_ppc64_reloc_type r_type;
2343 bfd_size_type octets;
2344 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
2345 bfd_boolean is_power4 = FALSE;
2347 /* If this is a relocatable link (output_bfd test tells us), just
2348 call the generic function. Any adjustment will be done at final
2349 link time. */
2350 if (output_bfd != NULL)
2351 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2352 input_section, output_bfd, error_message);
2354 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2355 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2356 insn &= ~(0x01 << 21);
2357 r_type = reloc_entry->howto->type;
2358 if (r_type == R_PPC64_ADDR14_BRTAKEN
2359 || r_type == R_PPC64_REL14_BRTAKEN)
2360 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2362 if (is_power4)
2364 /* Set 'a' bit. This is 0b00010 in BO field for branch
2365 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2366 for branch on CTR insns (BO == 1a00t or 1a01t). */
2367 if ((insn & (0x14 << 21)) == (0x04 << 21))
2368 insn |= 0x02 << 21;
2369 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2370 insn |= 0x08 << 21;
2371 else
2372 goto out;
2374 else
2376 bfd_vma target = 0;
2377 bfd_vma from;
2379 if (!bfd_is_com_section (symbol->section))
2380 target = symbol->value;
2381 target += symbol->section->output_section->vma;
2382 target += symbol->section->output_offset;
2383 target += reloc_entry->addend;
2385 from = (reloc_entry->address
2386 + input_section->output_offset
2387 + input_section->output_section->vma);
2389 /* Invert 'y' bit if not the default. */
2390 if ((bfd_signed_vma) (target - from) < 0)
2391 insn ^= 0x01 << 21;
2393 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2394 out:
2395 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2396 input_section, output_bfd, error_message);
2399 static bfd_reloc_status_type
2400 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2401 void *data, asection *input_section,
2402 bfd *output_bfd, char **error_message)
2404 /* If this is a relocatable link (output_bfd test tells us), just
2405 call the generic function. Any adjustment will be done at final
2406 link time. */
2407 if (output_bfd != NULL)
2408 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2409 input_section, output_bfd, error_message);
2411 /* Subtract the symbol section base address. */
2412 reloc_entry->addend -= symbol->section->output_section->vma;
2413 return bfd_reloc_continue;
2416 static bfd_reloc_status_type
2417 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2418 void *data, asection *input_section,
2419 bfd *output_bfd, char **error_message)
2421 /* If this is a relocatable link (output_bfd test tells us), just
2422 call the generic function. Any adjustment will be done at final
2423 link time. */
2424 if (output_bfd != NULL)
2425 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2426 input_section, output_bfd, error_message);
2428 /* Subtract the symbol section base address. */
2429 reloc_entry->addend -= symbol->section->output_section->vma;
2431 /* Adjust the addend for sign extension of the low 16 bits. */
2432 reloc_entry->addend += 0x8000;
2433 return bfd_reloc_continue;
2436 static bfd_reloc_status_type
2437 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2438 void *data, asection *input_section,
2439 bfd *output_bfd, char **error_message)
2441 bfd_vma TOCstart;
2443 /* If this is a relocatable link (output_bfd test tells us), just
2444 call the generic function. Any adjustment will be done at final
2445 link time. */
2446 if (output_bfd != NULL)
2447 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2448 input_section, output_bfd, error_message);
2450 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2451 if (TOCstart == 0)
2452 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2454 /* Subtract the TOC base address. */
2455 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2456 return bfd_reloc_continue;
2459 static bfd_reloc_status_type
2460 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2461 void *data, asection *input_section,
2462 bfd *output_bfd, char **error_message)
2464 bfd_vma TOCstart;
2466 /* If this is a relocatable link (output_bfd test tells us), just
2467 call the generic function. Any adjustment will be done at final
2468 link time. */
2469 if (output_bfd != NULL)
2470 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2471 input_section, output_bfd, error_message);
2473 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2474 if (TOCstart == 0)
2475 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2477 /* Subtract the TOC base address. */
2478 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2480 /* Adjust the addend for sign extension of the low 16 bits. */
2481 reloc_entry->addend += 0x8000;
2482 return bfd_reloc_continue;
2485 static bfd_reloc_status_type
2486 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2487 void *data, asection *input_section,
2488 bfd *output_bfd, char **error_message)
2490 bfd_vma TOCstart;
2491 bfd_size_type octets;
2493 /* If this is a relocatable link (output_bfd test tells us), just
2494 call the generic function. Any adjustment will be done at final
2495 link time. */
2496 if (output_bfd != NULL)
2497 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2498 input_section, output_bfd, error_message);
2500 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2501 if (TOCstart == 0)
2502 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2504 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2505 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2506 return bfd_reloc_ok;
2509 static bfd_reloc_status_type
2510 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2511 void *data, asection *input_section,
2512 bfd *output_bfd, char **error_message)
2514 /* If this is a relocatable link (output_bfd test tells us), just
2515 call the generic function. Any adjustment will be done at final
2516 link time. */
2517 if (output_bfd != NULL)
2518 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2519 input_section, output_bfd, error_message);
2521 if (error_message != NULL)
2523 static char buf[60];
2524 sprintf (buf, "generic linker can't handle %s",
2525 reloc_entry->howto->name);
2526 *error_message = buf;
2528 return bfd_reloc_dangerous;
2531 /* Track GOT entries needed for a given symbol. We might need more
2532 than one got entry per symbol. */
2533 struct got_entry
2535 struct got_entry *next;
2537 /* The symbol addend that we'll be placing in the GOT. */
2538 bfd_vma addend;
2540 /* Unlike other ELF targets, we use separate GOT entries for the same
2541 symbol referenced from different input files. This is to support
2542 automatic multiple TOC/GOT sections, where the TOC base can vary
2543 from one input file to another. After partitioning into TOC groups
2544 we merge entries within the group.
2546 Point to the BFD owning this GOT entry. */
2547 bfd *owner;
2549 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2550 TLS_TPREL or TLS_DTPREL for tls entries. */
2551 unsigned char tls_type;
2553 /* Non-zero if got.ent points to real entry. */
2554 unsigned char is_indirect;
2556 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2557 union
2559 bfd_signed_vma refcount;
2560 bfd_vma offset;
2561 struct got_entry *ent;
2562 } got;
2565 /* The same for PLT. */
2566 struct plt_entry
2568 struct plt_entry *next;
2570 bfd_vma addend;
2572 union
2574 bfd_signed_vma refcount;
2575 bfd_vma offset;
2576 } plt;
2579 struct ppc64_elf_obj_tdata
2581 struct elf_obj_tdata elf;
2583 /* Shortcuts to dynamic linker sections. */
2584 asection *got;
2585 asection *relgot;
2587 /* Used during garbage collection. We attach global symbols defined
2588 on removed .opd entries to this section so that the sym is removed. */
2589 asection *deleted_section;
2591 /* TLS local dynamic got entry handling. Support for multiple GOT
2592 sections means we potentially need one of these for each input bfd. */
2593 struct got_entry tlsld_got;
2595 /* A copy of relocs before they are modified for --emit-relocs. */
2596 Elf_Internal_Rela *opd_relocs;
2598 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2599 the reloc to be in the range -32768 to 32767. */
2600 unsigned int has_small_toc_reloc;
2603 #define ppc64_elf_tdata(bfd) \
2604 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2606 #define ppc64_tlsld_got(bfd) \
2607 (&ppc64_elf_tdata (bfd)->tlsld_got)
2609 #define is_ppc64_elf(bfd) \
2610 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2611 && elf_object_id (bfd) == PPC64_ELF_DATA)
2613 /* Override the generic function because we store some extras. */
2615 static bfd_boolean
2616 ppc64_elf_mkobject (bfd *abfd)
2618 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2619 PPC64_ELF_DATA);
2622 /* Fix bad default arch selected for a 64 bit input bfd when the
2623 default is 32 bit. */
2625 static bfd_boolean
2626 ppc64_elf_object_p (bfd *abfd)
2628 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2630 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2632 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2634 /* Relies on arch after 32 bit default being 64 bit default. */
2635 abfd->arch_info = abfd->arch_info->next;
2636 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2639 return TRUE;
2642 /* Support for core dump NOTE sections. */
2644 static bfd_boolean
2645 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2647 size_t offset, size;
2649 if (note->descsz != 504)
2650 return FALSE;
2652 /* pr_cursig */
2653 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2655 /* pr_pid */
2656 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
2658 /* pr_reg */
2659 offset = 112;
2660 size = 384;
2662 /* Make a ".reg/999" section. */
2663 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2664 size, note->descpos + offset);
2667 static bfd_boolean
2668 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2670 if (note->descsz != 136)
2671 return FALSE;
2673 elf_tdata (abfd)->core_program
2674 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2675 elf_tdata (abfd)->core_command
2676 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2678 return TRUE;
2681 static char *
2682 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2683 ...)
2685 switch (note_type)
2687 default:
2688 return NULL;
2690 case NT_PRPSINFO:
2692 char data[136];
2693 va_list ap;
2695 va_start (ap, note_type);
2696 memset (data, 0, 40);
2697 strncpy (data + 40, va_arg (ap, const char *), 16);
2698 strncpy (data + 56, va_arg (ap, const char *), 80);
2699 va_end (ap);
2700 return elfcore_write_note (abfd, buf, bufsiz,
2701 "CORE", note_type, data, sizeof (data));
2704 case NT_PRSTATUS:
2706 char data[504];
2707 va_list ap;
2708 long pid;
2709 int cursig;
2710 const void *greg;
2712 va_start (ap, note_type);
2713 memset (data, 0, 112);
2714 pid = va_arg (ap, long);
2715 bfd_put_32 (abfd, pid, data + 32);
2716 cursig = va_arg (ap, int);
2717 bfd_put_16 (abfd, cursig, data + 12);
2718 greg = va_arg (ap, const void *);
2719 memcpy (data + 112, greg, 384);
2720 memset (data + 496, 0, 8);
2721 va_end (ap);
2722 return elfcore_write_note (abfd, buf, bufsiz,
2723 "CORE", note_type, data, sizeof (data));
2728 /* Merge backend specific data from an object file to the output
2729 object file when linking. */
2731 static bfd_boolean
2732 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2734 /* Check if we have the same endianess. */
2735 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2736 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
2737 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2739 const char *msg;
2741 if (bfd_big_endian (ibfd))
2742 msg = _("%B: compiled for a big endian system "
2743 "and target is little endian");
2744 else
2745 msg = _("%B: compiled for a little endian system "
2746 "and target is big endian");
2748 (*_bfd_error_handler) (msg, ibfd);
2750 bfd_set_error (bfd_error_wrong_format);
2751 return FALSE;
2754 return TRUE;
2757 /* Add extra PPC sections. */
2759 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2761 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2762 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2763 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2764 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2765 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2766 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2767 { NULL, 0, 0, 0, 0 }
2770 enum _ppc64_sec_type {
2771 sec_normal = 0,
2772 sec_opd = 1,
2773 sec_toc = 2
2776 struct _ppc64_elf_section_data
2778 struct bfd_elf_section_data elf;
2780 union
2782 /* An array with one entry for each opd function descriptor. */
2783 struct _opd_sec_data
2785 /* Points to the function code section for local opd entries. */
2786 asection **func_sec;
2788 /* After editing .opd, adjust references to opd local syms. */
2789 long *adjust;
2790 } opd;
2792 /* An array for toc sections, indexed by offset/8. */
2793 struct _toc_sec_data
2795 /* Specifies the relocation symbol index used at a given toc offset. */
2796 unsigned *symndx;
2798 /* And the relocation addend. */
2799 bfd_vma *add;
2800 } toc;
2801 } u;
2803 enum _ppc64_sec_type sec_type:2;
2805 /* Flag set when small branches are detected. Used to
2806 select suitable defaults for the stub group size. */
2807 unsigned int has_14bit_branch:1;
2810 #define ppc64_elf_section_data(sec) \
2811 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2813 static bfd_boolean
2814 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2816 if (!sec->used_by_bfd)
2818 struct _ppc64_elf_section_data *sdata;
2819 bfd_size_type amt = sizeof (*sdata);
2821 sdata = bfd_zalloc (abfd, amt);
2822 if (sdata == NULL)
2823 return FALSE;
2824 sec->used_by_bfd = sdata;
2827 return _bfd_elf_new_section_hook (abfd, sec);
2830 static struct _opd_sec_data *
2831 get_opd_info (asection * sec)
2833 if (sec != NULL
2834 && ppc64_elf_section_data (sec) != NULL
2835 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2836 return &ppc64_elf_section_data (sec)->u.opd;
2837 return NULL;
2840 /* Parameters for the qsort hook. */
2841 static bfd_boolean synthetic_relocatable;
2843 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2845 static int
2846 compare_symbols (const void *ap, const void *bp)
2848 const asymbol *a = * (const asymbol **) ap;
2849 const asymbol *b = * (const asymbol **) bp;
2851 /* Section symbols first. */
2852 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2853 return -1;
2854 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2855 return 1;
2857 /* then .opd symbols. */
2858 if (strcmp (a->section->name, ".opd") == 0
2859 && strcmp (b->section->name, ".opd") != 0)
2860 return -1;
2861 if (strcmp (a->section->name, ".opd") != 0
2862 && strcmp (b->section->name, ".opd") == 0)
2863 return 1;
2865 /* then other code symbols. */
2866 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2867 == (SEC_CODE | SEC_ALLOC)
2868 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2869 != (SEC_CODE | SEC_ALLOC))
2870 return -1;
2872 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2873 != (SEC_CODE | SEC_ALLOC)
2874 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2875 == (SEC_CODE | SEC_ALLOC))
2876 return 1;
2878 if (synthetic_relocatable)
2880 if (a->section->id < b->section->id)
2881 return -1;
2883 if (a->section->id > b->section->id)
2884 return 1;
2887 if (a->value + a->section->vma < b->value + b->section->vma)
2888 return -1;
2890 if (a->value + a->section->vma > b->value + b->section->vma)
2891 return 1;
2893 /* For syms with the same value, prefer strong dynamic global function
2894 syms over other syms. */
2895 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2896 return -1;
2898 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2899 return 1;
2901 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2902 return -1;
2904 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2905 return 1;
2907 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2908 return -1;
2910 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2911 return 1;
2913 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2914 return -1;
2916 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2917 return 1;
2919 return 0;
2922 /* Search SYMS for a symbol of the given VALUE. */
2924 static asymbol *
2925 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
2927 long mid;
2929 if (id == -1)
2931 while (lo < hi)
2933 mid = (lo + hi) >> 1;
2934 if (syms[mid]->value + syms[mid]->section->vma < value)
2935 lo = mid + 1;
2936 else if (syms[mid]->value + syms[mid]->section->vma > value)
2937 hi = mid;
2938 else
2939 return syms[mid];
2942 else
2944 while (lo < hi)
2946 mid = (lo + hi) >> 1;
2947 if (syms[mid]->section->id < id)
2948 lo = mid + 1;
2949 else if (syms[mid]->section->id > id)
2950 hi = mid;
2951 else if (syms[mid]->value < value)
2952 lo = mid + 1;
2953 else if (syms[mid]->value > value)
2954 hi = mid;
2955 else
2956 return syms[mid];
2959 return NULL;
2962 static bfd_boolean
2963 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2965 bfd_vma vma = *(bfd_vma *) ptr;
2966 return ((section->flags & SEC_ALLOC) != 0
2967 && section->vma <= vma
2968 && vma < section->vma + section->size);
2971 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2972 entry syms. Also generate @plt symbols for the glink branch table. */
2974 static long
2975 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2976 long static_count, asymbol **static_syms,
2977 long dyn_count, asymbol **dyn_syms,
2978 asymbol **ret)
2980 asymbol *s;
2981 long i;
2982 long count;
2983 char *names;
2984 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2985 asection *opd;
2986 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2987 asymbol **syms;
2989 *ret = NULL;
2991 opd = bfd_get_section_by_name (abfd, ".opd");
2992 if (opd == NULL)
2993 return 0;
2995 symcount = static_count;
2996 if (!relocatable)
2997 symcount += dyn_count;
2998 if (symcount == 0)
2999 return 0;
3001 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3002 if (syms == NULL)
3003 return -1;
3005 if (!relocatable && static_count != 0 && dyn_count != 0)
3007 /* Use both symbol tables. */
3008 memcpy (syms, static_syms, static_count * sizeof (*syms));
3009 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3011 else if (!relocatable && static_count == 0)
3012 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3013 else
3014 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3016 synthetic_relocatable = relocatable;
3017 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3019 if (!relocatable && symcount > 1)
3021 long j;
3022 /* Trim duplicate syms, since we may have merged the normal and
3023 dynamic symbols. Actually, we only care about syms that have
3024 different values, so trim any with the same value. */
3025 for (i = 1, j = 1; i < symcount; ++i)
3026 if (syms[i - 1]->value + syms[i - 1]->section->vma
3027 != syms[i]->value + syms[i]->section->vma)
3028 syms[j++] = syms[i];
3029 symcount = j;
3032 i = 0;
3033 if (strcmp (syms[i]->section->name, ".opd") == 0)
3034 ++i;
3035 codesecsym = i;
3037 for (; i < symcount; ++i)
3038 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3039 != (SEC_CODE | SEC_ALLOC))
3040 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3041 break;
3042 codesecsymend = i;
3044 for (; i < symcount; ++i)
3045 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3046 break;
3047 secsymend = i;
3049 for (; i < symcount; ++i)
3050 if (strcmp (syms[i]->section->name, ".opd") != 0)
3051 break;
3052 opdsymend = i;
3054 for (; i < symcount; ++i)
3055 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3056 != (SEC_CODE | SEC_ALLOC))
3057 break;
3058 symcount = i;
3060 count = 0;
3062 if (relocatable)
3064 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3065 arelent *r;
3066 size_t size;
3067 long relcount;
3069 if (opdsymend == secsymend)
3070 goto done;
3072 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3073 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3074 if (relcount == 0)
3075 goto done;
3077 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3079 count = -1;
3080 goto done;
3083 size = 0;
3084 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3086 asymbol *sym;
3088 while (r < opd->relocation + relcount
3089 && r->address < syms[i]->value + opd->vma)
3090 ++r;
3092 if (r == opd->relocation + relcount)
3093 break;
3095 if (r->address != syms[i]->value + opd->vma)
3096 continue;
3098 if (r->howto->type != R_PPC64_ADDR64)
3099 continue;
3101 sym = *r->sym_ptr_ptr;
3102 if (!sym_exists_at (syms, opdsymend, symcount,
3103 sym->section->id, sym->value + r->addend))
3105 ++count;
3106 size += sizeof (asymbol);
3107 size += strlen (syms[i]->name) + 2;
3111 s = *ret = bfd_malloc (size);
3112 if (s == NULL)
3114 count = -1;
3115 goto done;
3118 names = (char *) (s + count);
3120 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3122 asymbol *sym;
3124 while (r < opd->relocation + relcount
3125 && r->address < syms[i]->value + opd->vma)
3126 ++r;
3128 if (r == opd->relocation + relcount)
3129 break;
3131 if (r->address != syms[i]->value + opd->vma)
3132 continue;
3134 if (r->howto->type != R_PPC64_ADDR64)
3135 continue;
3137 sym = *r->sym_ptr_ptr;
3138 if (!sym_exists_at (syms, opdsymend, symcount,
3139 sym->section->id, sym->value + r->addend))
3141 size_t len;
3143 *s = *syms[i];
3144 s->flags |= BSF_SYNTHETIC;
3145 s->section = sym->section;
3146 s->value = sym->value + r->addend;
3147 s->name = names;
3148 *names++ = '.';
3149 len = strlen (syms[i]->name);
3150 memcpy (names, syms[i]->name, len + 1);
3151 names += len + 1;
3152 /* Have udata.p point back to the original symbol this
3153 synthetic symbol was derived from. */
3154 s->udata.p = syms[i];
3155 s++;
3159 else
3161 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3162 bfd_byte *contents;
3163 size_t size;
3164 long plt_count = 0;
3165 bfd_vma glink_vma = 0, resolv_vma = 0;
3166 asection *dynamic, *glink = NULL, *relplt = NULL;
3167 arelent *p;
3169 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
3171 if (contents)
3173 free_contents_and_exit:
3174 free (contents);
3176 count = -1;
3177 goto done;
3180 size = 0;
3181 for (i = secsymend; i < opdsymend; ++i)
3183 bfd_vma ent;
3185 /* Ignore bogus symbols. */
3186 if (syms[i]->value > opd->size - 8)
3187 continue;
3189 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3190 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3192 ++count;
3193 size += sizeof (asymbol);
3194 size += strlen (syms[i]->name) + 2;
3198 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3199 if (dyn_count != 0
3200 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3202 bfd_byte *dynbuf, *extdyn, *extdynend;
3203 size_t extdynsize;
3204 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3206 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3207 goto free_contents_and_exit;
3209 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3210 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3212 extdyn = dynbuf;
3213 extdynend = extdyn + dynamic->size;
3214 for (; extdyn < extdynend; extdyn += extdynsize)
3216 Elf_Internal_Dyn dyn;
3217 (*swap_dyn_in) (abfd, extdyn, &dyn);
3219 if (dyn.d_tag == DT_NULL)
3220 break;
3222 if (dyn.d_tag == DT_PPC64_GLINK)
3224 /* The first glink stub starts at offset 32; see comment in
3225 ppc64_elf_finish_dynamic_sections. */
3226 glink_vma = dyn.d_un.d_val + 32;
3227 /* The .glink section usually does not survive the final
3228 link; search for the section (usually .text) where the
3229 glink stubs now reside. */
3230 glink = bfd_sections_find_if (abfd, section_covers_vma,
3231 &glink_vma);
3232 break;
3236 free (dynbuf);
3239 if (glink != NULL)
3241 /* Determine __glink trampoline by reading the relative branch
3242 from the first glink stub. */
3243 bfd_byte buf[4];
3244 if (bfd_get_section_contents (abfd, glink, buf,
3245 glink_vma + 4 - glink->vma, 4))
3247 unsigned int insn = bfd_get_32 (abfd, buf);
3248 insn ^= B_DOT;
3249 if ((insn & ~0x3fffffc) == 0)
3250 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3253 if (resolv_vma)
3254 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3256 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3257 if (relplt != NULL)
3259 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3260 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3261 goto free_contents_and_exit;
3263 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3264 size += plt_count * sizeof (asymbol);
3266 p = relplt->relocation;
3267 for (i = 0; i < plt_count; i++, p++)
3269 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3270 if (p->addend != 0)
3271 size += sizeof ("+0x") - 1 + 16;
3276 s = *ret = bfd_malloc (size);
3277 if (s == NULL)
3278 goto free_contents_and_exit;
3280 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3282 for (i = secsymend; i < opdsymend; ++i)
3284 bfd_vma ent;
3286 if (syms[i]->value > opd->size - 8)
3287 continue;
3289 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3290 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3292 long lo, hi;
3293 size_t len;
3294 asection *sec = abfd->sections;
3296 *s = *syms[i];
3297 lo = codesecsym;
3298 hi = codesecsymend;
3299 while (lo < hi)
3301 long mid = (lo + hi) >> 1;
3302 if (syms[mid]->section->vma < ent)
3303 lo = mid + 1;
3304 else if (syms[mid]->section->vma > ent)
3305 hi = mid;
3306 else
3308 sec = syms[mid]->section;
3309 break;
3313 if (lo >= hi && lo > codesecsym)
3314 sec = syms[lo - 1]->section;
3316 for (; sec != NULL; sec = sec->next)
3318 if (sec->vma > ent)
3319 break;
3320 if ((sec->flags & SEC_ALLOC) == 0
3321 || (sec->flags & SEC_LOAD) == 0)
3322 break;
3323 if ((sec->flags & SEC_CODE) != 0)
3324 s->section = sec;
3326 s->flags |= BSF_SYNTHETIC;
3327 s->value = ent - s->section->vma;
3328 s->name = names;
3329 *names++ = '.';
3330 len = strlen (syms[i]->name);
3331 memcpy (names, syms[i]->name, len + 1);
3332 names += len + 1;
3333 /* Have udata.p point back to the original symbol this
3334 synthetic symbol was derived from. */
3335 s->udata.p = syms[i];
3336 s++;
3339 free (contents);
3341 if (glink != NULL && relplt != NULL)
3343 if (resolv_vma)
3345 /* Add a symbol for the main glink trampoline. */
3346 memset (s, 0, sizeof *s);
3347 s->the_bfd = abfd;
3348 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3349 s->section = glink;
3350 s->value = resolv_vma - glink->vma;
3351 s->name = names;
3352 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3353 names += sizeof ("__glink_PLTresolve");
3354 s++;
3355 count++;
3358 /* FIXME: It would be very much nicer to put sym@plt on the
3359 stub rather than on the glink branch table entry. The
3360 objdump disassembler would then use a sensible symbol
3361 name on plt calls. The difficulty in doing so is
3362 a) finding the stubs, and,
3363 b) matching stubs against plt entries, and,
3364 c) there can be multiple stubs for a given plt entry.
3366 Solving (a) could be done by code scanning, but older
3367 ppc64 binaries used different stubs to current code.
3368 (b) is the tricky one since you need to known the toc
3369 pointer for at least one function that uses a pic stub to
3370 be able to calculate the plt address referenced.
3371 (c) means gdb would need to set multiple breakpoints (or
3372 find the glink branch itself) when setting breakpoints
3373 for pending shared library loads. */
3374 p = relplt->relocation;
3375 for (i = 0; i < plt_count; i++, p++)
3377 size_t len;
3379 *s = **p->sym_ptr_ptr;
3380 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3381 we are defining a symbol, ensure one of them is set. */
3382 if ((s->flags & BSF_LOCAL) == 0)
3383 s->flags |= BSF_GLOBAL;
3384 s->flags |= BSF_SYNTHETIC;
3385 s->section = glink;
3386 s->value = glink_vma - glink->vma;
3387 s->name = names;
3388 s->udata.p = NULL;
3389 len = strlen ((*p->sym_ptr_ptr)->name);
3390 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3391 names += len;
3392 if (p->addend != 0)
3394 memcpy (names, "+0x", sizeof ("+0x") - 1);
3395 names += sizeof ("+0x") - 1;
3396 bfd_sprintf_vma (abfd, names, p->addend);
3397 names += strlen (names);
3399 memcpy (names, "@plt", sizeof ("@plt"));
3400 names += sizeof ("@plt");
3401 s++;
3402 glink_vma += 8;
3403 if (i >= 0x8000)
3404 glink_vma += 4;
3406 count += plt_count;
3410 done:
3411 free (syms);
3412 return count;
3415 /* The following functions are specific to the ELF linker, while
3416 functions above are used generally. Those named ppc64_elf_* are
3417 called by the main ELF linker code. They appear in this file more
3418 or less in the order in which they are called. eg.
3419 ppc64_elf_check_relocs is called early in the link process,
3420 ppc64_elf_finish_dynamic_sections is one of the last functions
3421 called.
3423 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3424 functions have both a function code symbol and a function descriptor
3425 symbol. A call to foo in a relocatable object file looks like:
3427 . .text
3428 . x:
3429 . bl .foo
3430 . nop
3432 The function definition in another object file might be:
3434 . .section .opd
3435 . foo: .quad .foo
3436 . .quad .TOC.@tocbase
3437 . .quad 0
3439 . .text
3440 . .foo: blr
3442 When the linker resolves the call during a static link, the branch
3443 unsurprisingly just goes to .foo and the .opd information is unused.
3444 If the function definition is in a shared library, things are a little
3445 different: The call goes via a plt call stub, the opd information gets
3446 copied to the plt, and the linker patches the nop.
3448 . x:
3449 . bl .foo_stub
3450 . ld 2,40(1)
3453 . .foo_stub:
3454 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
3455 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
3456 . std 2,40(1) # this is the general idea
3457 . ld 11,0(12)
3458 . ld 2,8(12)
3459 . mtctr 11
3460 . ld 11,16(12)
3461 . bctr
3463 . .section .plt
3464 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3466 The "reloc ()" notation is supposed to indicate that the linker emits
3467 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3468 copying.
3470 What are the difficulties here? Well, firstly, the relocations
3471 examined by the linker in check_relocs are against the function code
3472 sym .foo, while the dynamic relocation in the plt is emitted against
3473 the function descriptor symbol, foo. Somewhere along the line, we need
3474 to carefully copy dynamic link information from one symbol to the other.
3475 Secondly, the generic part of the elf linker will make .foo a dynamic
3476 symbol as is normal for most other backends. We need foo dynamic
3477 instead, at least for an application final link. However, when
3478 creating a shared library containing foo, we need to have both symbols
3479 dynamic so that references to .foo are satisfied during the early
3480 stages of linking. Otherwise the linker might decide to pull in a
3481 definition from some other object, eg. a static library.
3483 Update: As of August 2004, we support a new convention. Function
3484 calls may use the function descriptor symbol, ie. "bl foo". This
3485 behaves exactly as "bl .foo". */
3487 /* The linker needs to keep track of the number of relocs that it
3488 decides to copy as dynamic relocs in check_relocs for each symbol.
3489 This is so that it can later discard them if they are found to be
3490 unnecessary. We store the information in a field extending the
3491 regular ELF linker hash table. */
3493 struct ppc_dyn_relocs
3495 struct ppc_dyn_relocs *next;
3497 /* The input section of the reloc. */
3498 asection *sec;
3500 /* Total number of relocs copied for the input section. */
3501 bfd_size_type count;
3503 /* Number of pc-relative relocs copied for the input section. */
3504 bfd_size_type pc_count;
3507 /* Of those relocs that might be copied as dynamic relocs, this function
3508 selects those that must be copied when linking a shared library,
3509 even when the symbol is local. */
3511 static int
3512 must_be_dyn_reloc (struct bfd_link_info *info,
3513 enum elf_ppc64_reloc_type r_type)
3515 switch (r_type)
3517 default:
3518 return 1;
3520 case R_PPC64_REL32:
3521 case R_PPC64_REL64:
3522 case R_PPC64_REL30:
3523 return 0;
3525 case R_PPC64_TPREL16:
3526 case R_PPC64_TPREL16_LO:
3527 case R_PPC64_TPREL16_HI:
3528 case R_PPC64_TPREL16_HA:
3529 case R_PPC64_TPREL16_DS:
3530 case R_PPC64_TPREL16_LO_DS:
3531 case R_PPC64_TPREL16_HIGHER:
3532 case R_PPC64_TPREL16_HIGHERA:
3533 case R_PPC64_TPREL16_HIGHEST:
3534 case R_PPC64_TPREL16_HIGHESTA:
3535 case R_PPC64_TPREL64:
3536 return !info->executable;
3540 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3541 copying dynamic variables from a shared lib into an app's dynbss
3542 section, and instead use a dynamic relocation to point into the
3543 shared lib. With code that gcc generates, it's vital that this be
3544 enabled; In the PowerPC64 ABI, the address of a function is actually
3545 the address of a function descriptor, which resides in the .opd
3546 section. gcc uses the descriptor directly rather than going via the
3547 GOT as some other ABI's do, which means that initialized function
3548 pointers must reference the descriptor. Thus, a function pointer
3549 initialized to the address of a function in a shared library will
3550 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3551 redefines the function descriptor symbol to point to the copy. This
3552 presents a problem as a plt entry for that function is also
3553 initialized from the function descriptor symbol and the copy reloc
3554 may not be initialized first. */
3555 #define ELIMINATE_COPY_RELOCS 1
3557 /* Section name for stubs is the associated section name plus this
3558 string. */
3559 #define STUB_SUFFIX ".stub"
3561 /* Linker stubs.
3562 ppc_stub_long_branch:
3563 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3564 destination, but a 24 bit branch in a stub section will reach.
3565 . b dest
3567 ppc_stub_plt_branch:
3568 Similar to the above, but a 24 bit branch in the stub section won't
3569 reach its destination.
3570 . addis %r12,%r2,xxx@toc@ha
3571 . ld %r11,xxx@toc@l(%r12)
3572 . mtctr %r11
3573 . bctr
3575 ppc_stub_plt_call:
3576 Used to call a function in a shared library. If it so happens that
3577 the plt entry referenced crosses a 64k boundary, then an extra
3578 "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
3579 . addis %r12,%r2,xxx@toc@ha
3580 . std %r2,40(%r1)
3581 . ld %r11,xxx+0@toc@l(%r12)
3582 . mtctr %r11
3583 . ld %r2,xxx+8@toc@l(%r12)
3584 . ld %r11,xxx+16@toc@l(%r12)
3585 . bctr
3587 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3588 code to adjust the value and save r2 to support multiple toc sections.
3589 A ppc_stub_long_branch with an r2 offset looks like:
3590 . std %r2,40(%r1)
3591 . addis %r2,%r2,off@ha
3592 . addi %r2,%r2,off@l
3593 . b dest
3595 A ppc_stub_plt_branch with an r2 offset looks like:
3596 . std %r2,40(%r1)
3597 . addis %r12,%r2,xxx@toc@ha
3598 . ld %r11,xxx@toc@l(%r12)
3599 . addis %r2,%r2,off@ha
3600 . addi %r2,%r2,off@l
3601 . mtctr %r11
3602 . bctr
3604 In cases where the "addis" instruction would add zero, the "addis" is
3605 omitted and following instructions modified slightly in some cases.
3608 enum ppc_stub_type {
3609 ppc_stub_none,
3610 ppc_stub_long_branch,
3611 ppc_stub_long_branch_r2off,
3612 ppc_stub_plt_branch,
3613 ppc_stub_plt_branch_r2off,
3614 ppc_stub_plt_call
3617 struct ppc_stub_hash_entry {
3619 /* Base hash table entry structure. */
3620 struct bfd_hash_entry root;
3622 enum ppc_stub_type stub_type;
3624 /* The stub section. */
3625 asection *stub_sec;
3627 /* Offset within stub_sec of the beginning of this stub. */
3628 bfd_vma stub_offset;
3630 /* Given the symbol's value and its section we can determine its final
3631 value when building the stubs (so the stub knows where to jump. */
3632 bfd_vma target_value;
3633 asection *target_section;
3635 /* The symbol table entry, if any, that this was derived from. */
3636 struct ppc_link_hash_entry *h;
3637 struct plt_entry *plt_ent;
3639 /* And the reloc addend that this was derived from. */
3640 bfd_vma addend;
3642 /* Where this stub is being called from, or, in the case of combined
3643 stub sections, the first input section in the group. */
3644 asection *id_sec;
3647 struct ppc_branch_hash_entry {
3649 /* Base hash table entry structure. */
3650 struct bfd_hash_entry root;
3652 /* Offset within branch lookup table. */
3653 unsigned int offset;
3655 /* Generation marker. */
3656 unsigned int iter;
3659 struct ppc_link_hash_entry
3661 struct elf_link_hash_entry elf;
3663 union {
3664 /* A pointer to the most recently used stub hash entry against this
3665 symbol. */
3666 struct ppc_stub_hash_entry *stub_cache;
3668 /* A pointer to the next symbol starting with a '.' */
3669 struct ppc_link_hash_entry *next_dot_sym;
3670 } u;
3672 /* Track dynamic relocs copied for this symbol. */
3673 struct ppc_dyn_relocs *dyn_relocs;
3675 /* Link between function code and descriptor symbols. */
3676 struct ppc_link_hash_entry *oh;
3678 /* Flag function code and descriptor symbols. */
3679 unsigned int is_func:1;
3680 unsigned int is_func_descriptor:1;
3681 unsigned int fake:1;
3683 /* Whether global opd/toc sym has been adjusted or not.
3684 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3685 should be set for all globals defined in any opd/toc section. */
3686 unsigned int adjust_done:1;
3688 /* Set if we twiddled this symbol to weak at some stage. */
3689 unsigned int was_undefined:1;
3691 /* Contexts in which symbol is used in the GOT (or TOC).
3692 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3693 corresponding relocs are encountered during check_relocs.
3694 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3695 indicate the corresponding GOT entry type is not needed.
3696 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3697 a TPREL one. We use a separate flag rather than setting TPREL
3698 just for convenience in distinguishing the two cases. */
3699 #define TLS_GD 1 /* GD reloc. */
3700 #define TLS_LD 2 /* LD reloc. */
3701 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
3702 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3703 #define TLS_TLS 16 /* Any TLS reloc. */
3704 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3705 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3706 #define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
3707 unsigned char tls_mask;
3710 /* ppc64 ELF linker hash table. */
3712 struct ppc_link_hash_table
3714 struct elf_link_hash_table elf;
3716 /* The stub hash table. */
3717 struct bfd_hash_table stub_hash_table;
3719 /* Another hash table for plt_branch stubs. */
3720 struct bfd_hash_table branch_hash_table;
3722 /* Linker stub bfd. */
3723 bfd *stub_bfd;
3725 /* Linker call-backs. */
3726 asection * (*add_stub_section) (const char *, asection *);
3727 void (*layout_sections_again) (void);
3729 /* Array to keep track of which stub sections have been created, and
3730 information on stub grouping. */
3731 struct map_stub {
3732 /* This is the section to which stubs in the group will be attached. */
3733 asection *link_sec;
3734 /* The stub section. */
3735 asection *stub_sec;
3736 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3737 bfd_vma toc_off;
3738 } *stub_group;
3740 /* Temp used when calculating TOC pointers. */
3741 bfd_vma toc_curr;
3742 bfd *toc_bfd;
3743 asection *toc_first_sec;
3745 /* Highest input section id. */
3746 int top_id;
3748 /* Highest output section index. */
3749 int top_index;
3751 /* Used when adding symbols. */
3752 struct ppc_link_hash_entry *dot_syms;
3754 /* List of input sections for each output section. */
3755 asection **input_list;
3757 /* Short-cuts to get to dynamic linker sections. */
3758 asection *got;
3759 asection *plt;
3760 asection *relplt;
3761 asection *iplt;
3762 asection *reliplt;
3763 asection *dynbss;
3764 asection *relbss;
3765 asection *glink;
3766 asection *sfpr;
3767 asection *brlt;
3768 asection *relbrlt;
3770 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3771 struct ppc_link_hash_entry *tls_get_addr;
3772 struct ppc_link_hash_entry *tls_get_addr_fd;
3774 /* The size of reliplt used by got entry relocs. */
3775 bfd_size_type got_reli_size;
3777 /* Statistics. */
3778 unsigned long stub_count[ppc_stub_plt_call];
3780 /* Number of stubs against global syms. */
3781 unsigned long stub_globals;
3783 /* Set if we should emit symbols for stubs. */
3784 unsigned int emit_stub_syms:1;
3786 /* Set if __tls_get_addr optimization should not be done. */
3787 unsigned int no_tls_get_addr_opt:1;
3789 /* Support for multiple toc sections. */
3790 unsigned int do_multi_toc:1;
3791 unsigned int multi_toc_needed:1;
3792 unsigned int second_toc_pass:1;
3793 unsigned int do_toc_opt:1;
3795 /* Set on error. */
3796 unsigned int stub_error:1;
3798 /* Temp used by ppc64_elf_process_dot_syms. */
3799 unsigned int twiddled_syms:1;
3801 /* Incremented every time we size stubs. */
3802 unsigned int stub_iteration;
3804 /* Small local sym cache. */
3805 struct sym_cache sym_cache;
3808 /* Rename some of the generic section flags to better document how they
3809 are used here. */
3811 /* Nonzero if this section has TLS related relocations. */
3812 #define has_tls_reloc sec_flg0
3814 /* Nonzero if this section has a call to __tls_get_addr. */
3815 #define has_tls_get_addr_call sec_flg1
3817 /* Nonzero if this section has any toc or got relocs. */
3818 #define has_toc_reloc sec_flg2
3820 /* Nonzero if this section has a call to another section that uses
3821 the toc or got. */
3822 #define makes_toc_func_call sec_flg3
3824 /* Recursion protection when determining above flag. */
3825 #define call_check_in_progress sec_flg4
3826 #define call_check_done sec_flg5
3828 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3830 #define ppc_hash_table(p) \
3831 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3832 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3834 #define ppc_stub_hash_lookup(table, string, create, copy) \
3835 ((struct ppc_stub_hash_entry *) \
3836 bfd_hash_lookup ((table), (string), (create), (copy)))
3838 #define ppc_branch_hash_lookup(table, string, create, copy) \
3839 ((struct ppc_branch_hash_entry *) \
3840 bfd_hash_lookup ((table), (string), (create), (copy)))
3842 /* Create an entry in the stub hash table. */
3844 static struct bfd_hash_entry *
3845 stub_hash_newfunc (struct bfd_hash_entry *entry,
3846 struct bfd_hash_table *table,
3847 const char *string)
3849 /* Allocate the structure if it has not already been allocated by a
3850 subclass. */
3851 if (entry == NULL)
3853 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3854 if (entry == NULL)
3855 return entry;
3858 /* Call the allocation method of the superclass. */
3859 entry = bfd_hash_newfunc (entry, table, string);
3860 if (entry != NULL)
3862 struct ppc_stub_hash_entry *eh;
3864 /* Initialize the local fields. */
3865 eh = (struct ppc_stub_hash_entry *) entry;
3866 eh->stub_type = ppc_stub_none;
3867 eh->stub_sec = NULL;
3868 eh->stub_offset = 0;
3869 eh->target_value = 0;
3870 eh->target_section = NULL;
3871 eh->h = NULL;
3872 eh->id_sec = NULL;
3875 return entry;
3878 /* Create an entry in the branch hash table. */
3880 static struct bfd_hash_entry *
3881 branch_hash_newfunc (struct bfd_hash_entry *entry,
3882 struct bfd_hash_table *table,
3883 const char *string)
3885 /* Allocate the structure if it has not already been allocated by a
3886 subclass. */
3887 if (entry == NULL)
3889 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3890 if (entry == NULL)
3891 return entry;
3894 /* Call the allocation method of the superclass. */
3895 entry = bfd_hash_newfunc (entry, table, string);
3896 if (entry != NULL)
3898 struct ppc_branch_hash_entry *eh;
3900 /* Initialize the local fields. */
3901 eh = (struct ppc_branch_hash_entry *) entry;
3902 eh->offset = 0;
3903 eh->iter = 0;
3906 return entry;
3909 /* Create an entry in a ppc64 ELF linker hash table. */
3911 static struct bfd_hash_entry *
3912 link_hash_newfunc (struct bfd_hash_entry *entry,
3913 struct bfd_hash_table *table,
3914 const char *string)
3916 /* Allocate the structure if it has not already been allocated by a
3917 subclass. */
3918 if (entry == NULL)
3920 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3921 if (entry == NULL)
3922 return entry;
3925 /* Call the allocation method of the superclass. */
3926 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3927 if (entry != NULL)
3929 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3931 memset (&eh->u.stub_cache, 0,
3932 (sizeof (struct ppc_link_hash_entry)
3933 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3935 /* When making function calls, old ABI code references function entry
3936 points (dot symbols), while new ABI code references the function
3937 descriptor symbol. We need to make any combination of reference and
3938 definition work together, without breaking archive linking.
3940 For a defined function "foo" and an undefined call to "bar":
3941 An old object defines "foo" and ".foo", references ".bar" (possibly
3942 "bar" too).
3943 A new object defines "foo" and references "bar".
3945 A new object thus has no problem with its undefined symbols being
3946 satisfied by definitions in an old object. On the other hand, the
3947 old object won't have ".bar" satisfied by a new object.
3949 Keep a list of newly added dot-symbols. */
3951 if (string[0] == '.')
3953 struct ppc_link_hash_table *htab;
3955 htab = (struct ppc_link_hash_table *) table;
3956 eh->u.next_dot_sym = htab->dot_syms;
3957 htab->dot_syms = eh;
3961 return entry;
3964 /* Create a ppc64 ELF linker hash table. */
3966 static struct bfd_link_hash_table *
3967 ppc64_elf_link_hash_table_create (bfd *abfd)
3969 struct ppc_link_hash_table *htab;
3970 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3972 htab = bfd_zmalloc (amt);
3973 if (htab == NULL)
3974 return NULL;
3976 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3977 sizeof (struct ppc_link_hash_entry),
3978 PPC64_ELF_DATA))
3980 free (htab);
3981 return NULL;
3984 /* Init the stub hash table too. */
3985 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3986 sizeof (struct ppc_stub_hash_entry)))
3987 return NULL;
3989 /* And the branch hash table. */
3990 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3991 sizeof (struct ppc_branch_hash_entry)))
3992 return NULL;
3994 /* Initializing two fields of the union is just cosmetic. We really
3995 only care about glist, but when compiled on a 32-bit host the
3996 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3997 debugger inspection of these fields look nicer. */
3998 htab->elf.init_got_refcount.refcount = 0;
3999 htab->elf.init_got_refcount.glist = NULL;
4000 htab->elf.init_plt_refcount.refcount = 0;
4001 htab->elf.init_plt_refcount.glist = NULL;
4002 htab->elf.init_got_offset.offset = 0;
4003 htab->elf.init_got_offset.glist = NULL;
4004 htab->elf.init_plt_offset.offset = 0;
4005 htab->elf.init_plt_offset.glist = NULL;
4007 return &htab->elf.root;
4010 /* Free the derived linker hash table. */
4012 static void
4013 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
4015 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
4017 bfd_hash_table_free (&ret->stub_hash_table);
4018 bfd_hash_table_free (&ret->branch_hash_table);
4019 _bfd_generic_link_hash_table_free (hash);
4022 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4024 void
4025 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4027 struct ppc_link_hash_table *htab;
4029 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4031 /* Always hook our dynamic sections into the first bfd, which is the
4032 linker created stub bfd. This ensures that the GOT header is at
4033 the start of the output TOC section. */
4034 htab = ppc_hash_table (info);
4035 if (htab == NULL)
4036 return;
4037 htab->stub_bfd = abfd;
4038 htab->elf.dynobj = abfd;
4041 /* Build a name for an entry in the stub hash table. */
4043 static char *
4044 ppc_stub_name (const asection *input_section,
4045 const asection *sym_sec,
4046 const struct ppc_link_hash_entry *h,
4047 const Elf_Internal_Rela *rel)
4049 char *stub_name;
4050 bfd_size_type len;
4052 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4053 offsets from a sym as a branch target? In fact, we could
4054 probably assume the addend is always zero. */
4055 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4057 if (h)
4059 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4060 stub_name = bfd_malloc (len);
4061 if (stub_name == NULL)
4062 return stub_name;
4064 sprintf (stub_name, "%08x.%s+%x",
4065 input_section->id & 0xffffffff,
4066 h->elf.root.root.string,
4067 (int) rel->r_addend & 0xffffffff);
4069 else
4071 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4072 stub_name = bfd_malloc (len);
4073 if (stub_name == NULL)
4074 return stub_name;
4076 sprintf (stub_name, "%08x.%x:%x+%x",
4077 input_section->id & 0xffffffff,
4078 sym_sec->id & 0xffffffff,
4079 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4080 (int) rel->r_addend & 0xffffffff);
4082 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4083 stub_name[len - 2] = 0;
4084 return stub_name;
4087 /* Look up an entry in the stub hash. Stub entries are cached because
4088 creating the stub name takes a bit of time. */
4090 static struct ppc_stub_hash_entry *
4091 ppc_get_stub_entry (const asection *input_section,
4092 const asection *sym_sec,
4093 struct ppc_link_hash_entry *h,
4094 const Elf_Internal_Rela *rel,
4095 struct ppc_link_hash_table *htab)
4097 struct ppc_stub_hash_entry *stub_entry;
4098 const asection *id_sec;
4100 /* If this input section is part of a group of sections sharing one
4101 stub section, then use the id of the first section in the group.
4102 Stub names need to include a section id, as there may well be
4103 more than one stub used to reach say, printf, and we need to
4104 distinguish between them. */
4105 id_sec = htab->stub_group[input_section->id].link_sec;
4107 if (h != NULL && h->u.stub_cache != NULL
4108 && h->u.stub_cache->h == h
4109 && h->u.stub_cache->id_sec == id_sec)
4111 stub_entry = h->u.stub_cache;
4113 else
4115 char *stub_name;
4117 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4118 if (stub_name == NULL)
4119 return NULL;
4121 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4122 stub_name, FALSE, FALSE);
4123 if (h != NULL)
4124 h->u.stub_cache = stub_entry;
4126 free (stub_name);
4129 return stub_entry;
4132 /* Add a new stub entry to the stub hash. Not all fields of the new
4133 stub entry are initialised. */
4135 static struct ppc_stub_hash_entry *
4136 ppc_add_stub (const char *stub_name,
4137 asection *section,
4138 struct ppc_link_hash_table *htab)
4140 asection *link_sec;
4141 asection *stub_sec;
4142 struct ppc_stub_hash_entry *stub_entry;
4144 link_sec = htab->stub_group[section->id].link_sec;
4145 stub_sec = htab->stub_group[section->id].stub_sec;
4146 if (stub_sec == NULL)
4148 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4149 if (stub_sec == NULL)
4151 size_t namelen;
4152 bfd_size_type len;
4153 char *s_name;
4155 namelen = strlen (link_sec->name);
4156 len = namelen + sizeof (STUB_SUFFIX);
4157 s_name = bfd_alloc (htab->stub_bfd, len);
4158 if (s_name == NULL)
4159 return NULL;
4161 memcpy (s_name, link_sec->name, namelen);
4162 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4163 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4164 if (stub_sec == NULL)
4165 return NULL;
4166 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4168 htab->stub_group[section->id].stub_sec = stub_sec;
4171 /* Enter this entry into the linker stub hash table. */
4172 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4173 TRUE, FALSE);
4174 if (stub_entry == NULL)
4176 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
4177 section->owner, stub_name);
4178 return NULL;
4181 stub_entry->stub_sec = stub_sec;
4182 stub_entry->stub_offset = 0;
4183 stub_entry->id_sec = link_sec;
4184 return stub_entry;
4187 /* Create sections for linker generated code. */
4189 static bfd_boolean
4190 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4192 struct ppc_link_hash_table *htab;
4193 flagword flags;
4195 htab = ppc_hash_table (info);
4196 if (htab == NULL)
4197 return FALSE;
4199 /* Create .sfpr for code to save and restore fp regs. */
4200 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4201 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4202 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4203 flags);
4204 if (htab->sfpr == NULL
4205 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4206 return FALSE;
4208 /* Create .glink for lazy dynamic linking support. */
4209 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4210 flags);
4211 if (htab->glink == NULL
4212 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4213 return FALSE;
4215 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4216 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4217 if (htab->iplt == NULL
4218 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4219 return FALSE;
4221 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4222 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4223 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4224 ".rela.iplt",
4225 flags);
4226 if (htab->reliplt == NULL
4227 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4228 return FALSE;
4230 /* Create branch lookup table for plt_branch stubs. */
4231 flags = (SEC_ALLOC | SEC_LOAD
4232 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4233 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4234 flags);
4235 if (htab->brlt == NULL
4236 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4237 return FALSE;
4239 if (!info->shared)
4240 return TRUE;
4242 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4243 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4244 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4245 ".rela.branch_lt",
4246 flags);
4247 if (htab->relbrlt == NULL
4248 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4249 return FALSE;
4251 return TRUE;
4254 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4255 not already done. */
4257 static bfd_boolean
4258 create_got_section (bfd *abfd, struct bfd_link_info *info)
4260 asection *got, *relgot;
4261 flagword flags;
4262 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4264 if (!is_ppc64_elf (abfd))
4265 return FALSE;
4266 if (htab == NULL)
4267 return FALSE;
4269 if (!htab->got)
4271 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4272 return FALSE;
4274 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4275 if (!htab->got)
4276 abort ();
4279 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4280 | SEC_LINKER_CREATED);
4282 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4283 if (!got
4284 || !bfd_set_section_alignment (abfd, got, 3))
4285 return FALSE;
4287 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4288 flags | SEC_READONLY);
4289 if (!relgot
4290 || ! bfd_set_section_alignment (abfd, relgot, 3))
4291 return FALSE;
4293 ppc64_elf_tdata (abfd)->got = got;
4294 ppc64_elf_tdata (abfd)->relgot = relgot;
4295 return TRUE;
4298 /* Create the dynamic sections, and set up shortcuts. */
4300 static bfd_boolean
4301 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4303 struct ppc_link_hash_table *htab;
4305 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4306 return FALSE;
4308 htab = ppc_hash_table (info);
4309 if (htab == NULL)
4310 return FALSE;
4312 if (!htab->got)
4313 htab->got = bfd_get_section_by_name (dynobj, ".got");
4314 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4315 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4316 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
4317 if (!info->shared)
4318 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
4320 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4321 || (!info->shared && !htab->relbss))
4322 abort ();
4324 return TRUE;
4327 /* Follow indirect and warning symbol links. */
4329 static inline struct bfd_link_hash_entry *
4330 follow_link (struct bfd_link_hash_entry *h)
4332 while (h->type == bfd_link_hash_indirect
4333 || h->type == bfd_link_hash_warning)
4334 h = h->u.i.link;
4335 return h;
4338 static inline struct elf_link_hash_entry *
4339 elf_follow_link (struct elf_link_hash_entry *h)
4341 return (struct elf_link_hash_entry *) follow_link (&h->root);
4344 static inline struct ppc_link_hash_entry *
4345 ppc_follow_link (struct ppc_link_hash_entry *h)
4347 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4350 /* Merge PLT info on FROM with that on TO. */
4352 static void
4353 move_plt_plist (struct ppc_link_hash_entry *from,
4354 struct ppc_link_hash_entry *to)
4356 if (from->elf.plt.plist != NULL)
4358 if (to->elf.plt.plist != NULL)
4360 struct plt_entry **entp;
4361 struct plt_entry *ent;
4363 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4365 struct plt_entry *dent;
4367 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4368 if (dent->addend == ent->addend)
4370 dent->plt.refcount += ent->plt.refcount;
4371 *entp = ent->next;
4372 break;
4374 if (dent == NULL)
4375 entp = &ent->next;
4377 *entp = to->elf.plt.plist;
4380 to->elf.plt.plist = from->elf.plt.plist;
4381 from->elf.plt.plist = NULL;
4385 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4387 static void
4388 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4389 struct elf_link_hash_entry *dir,
4390 struct elf_link_hash_entry *ind)
4392 struct ppc_link_hash_entry *edir, *eind;
4394 edir = (struct ppc_link_hash_entry *) dir;
4395 eind = (struct ppc_link_hash_entry *) ind;
4397 /* Copy over any dynamic relocs we may have on the indirect sym. */
4398 if (eind->dyn_relocs != NULL)
4400 if (edir->dyn_relocs != NULL)
4402 struct ppc_dyn_relocs **pp;
4403 struct ppc_dyn_relocs *p;
4405 /* Add reloc counts against the indirect sym to the direct sym
4406 list. Merge any entries against the same section. */
4407 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4409 struct ppc_dyn_relocs *q;
4411 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4412 if (q->sec == p->sec)
4414 q->pc_count += p->pc_count;
4415 q->count += p->count;
4416 *pp = p->next;
4417 break;
4419 if (q == NULL)
4420 pp = &p->next;
4422 *pp = edir->dyn_relocs;
4425 edir->dyn_relocs = eind->dyn_relocs;
4426 eind->dyn_relocs = NULL;
4429 edir->is_func |= eind->is_func;
4430 edir->is_func_descriptor |= eind->is_func_descriptor;
4431 edir->tls_mask |= eind->tls_mask;
4432 if (eind->oh != NULL)
4433 edir->oh = ppc_follow_link (eind->oh);
4435 /* If called to transfer flags for a weakdef during processing
4436 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4437 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4438 if (!(ELIMINATE_COPY_RELOCS
4439 && eind->elf.root.type != bfd_link_hash_indirect
4440 && edir->elf.dynamic_adjusted))
4441 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4443 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4444 edir->elf.ref_regular |= eind->elf.ref_regular;
4445 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4446 edir->elf.needs_plt |= eind->elf.needs_plt;
4448 /* If we were called to copy over info for a weak sym, that's all. */
4449 if (eind->elf.root.type != bfd_link_hash_indirect)
4450 return;
4452 /* Copy over got entries that we may have already seen to the
4453 symbol which just became indirect. */
4454 if (eind->elf.got.glist != NULL)
4456 if (edir->elf.got.glist != NULL)
4458 struct got_entry **entp;
4459 struct got_entry *ent;
4461 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4463 struct got_entry *dent;
4465 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4466 if (dent->addend == ent->addend
4467 && dent->owner == ent->owner
4468 && dent->tls_type == ent->tls_type)
4470 dent->got.refcount += ent->got.refcount;
4471 *entp = ent->next;
4472 break;
4474 if (dent == NULL)
4475 entp = &ent->next;
4477 *entp = edir->elf.got.glist;
4480 edir->elf.got.glist = eind->elf.got.glist;
4481 eind->elf.got.glist = NULL;
4484 /* And plt entries. */
4485 move_plt_plist (eind, edir);
4487 if (eind->elf.dynindx != -1)
4489 if (edir->elf.dynindx != -1)
4490 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4491 edir->elf.dynstr_index);
4492 edir->elf.dynindx = eind->elf.dynindx;
4493 edir->elf.dynstr_index = eind->elf.dynstr_index;
4494 eind->elf.dynindx = -1;
4495 eind->elf.dynstr_index = 0;
4499 /* Find the function descriptor hash entry from the given function code
4500 hash entry FH. Link the entries via their OH fields. */
4502 static struct ppc_link_hash_entry *
4503 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4505 struct ppc_link_hash_entry *fdh = fh->oh;
4507 if (fdh == NULL)
4509 const char *fd_name = fh->elf.root.root.string + 1;
4511 fdh = (struct ppc_link_hash_entry *)
4512 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4513 if (fdh == NULL)
4514 return fdh;
4516 fdh->is_func_descriptor = 1;
4517 fdh->oh = fh;
4518 fh->is_func = 1;
4519 fh->oh = fdh;
4522 return ppc_follow_link (fdh);
4525 /* Make a fake function descriptor sym for the code sym FH. */
4527 static struct ppc_link_hash_entry *
4528 make_fdh (struct bfd_link_info *info,
4529 struct ppc_link_hash_entry *fh)
4531 bfd *abfd;
4532 asymbol *newsym;
4533 struct bfd_link_hash_entry *bh;
4534 struct ppc_link_hash_entry *fdh;
4536 abfd = fh->elf.root.u.undef.abfd;
4537 newsym = bfd_make_empty_symbol (abfd);
4538 newsym->name = fh->elf.root.root.string + 1;
4539 newsym->section = bfd_und_section_ptr;
4540 newsym->value = 0;
4541 newsym->flags = BSF_WEAK;
4543 bh = NULL;
4544 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4545 newsym->flags, newsym->section,
4546 newsym->value, NULL, FALSE, FALSE,
4547 &bh))
4548 return NULL;
4550 fdh = (struct ppc_link_hash_entry *) bh;
4551 fdh->elf.non_elf = 0;
4552 fdh->fake = 1;
4553 fdh->is_func_descriptor = 1;
4554 fdh->oh = fh;
4555 fh->is_func = 1;
4556 fh->oh = fdh;
4557 return fdh;
4560 /* Fix function descriptor symbols defined in .opd sections to be
4561 function type. */
4563 static bfd_boolean
4564 ppc64_elf_add_symbol_hook (bfd *ibfd,
4565 struct bfd_link_info *info,
4566 Elf_Internal_Sym *isym,
4567 const char **name ATTRIBUTE_UNUSED,
4568 flagword *flags ATTRIBUTE_UNUSED,
4569 asection **sec,
4570 bfd_vma *value ATTRIBUTE_UNUSED)
4572 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4574 if ((ibfd->flags & DYNAMIC) == 0)
4575 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4577 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4579 else if (*sec != NULL
4580 && strcmp ((*sec)->name, ".opd") == 0)
4581 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4583 return TRUE;
4586 /* This function makes an old ABI object reference to ".bar" cause the
4587 inclusion of a new ABI object archive that defines "bar".
4588 NAME is a symbol defined in an archive. Return a symbol in the hash
4589 table that might be satisfied by the archive symbols. */
4591 static struct elf_link_hash_entry *
4592 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4593 struct bfd_link_info *info,
4594 const char *name)
4596 struct elf_link_hash_entry *h;
4597 char *dot_name;
4598 size_t len;
4600 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4601 if (h != NULL
4602 /* Don't return this sym if it is a fake function descriptor
4603 created by add_symbol_adjust. */
4604 && !(h->root.type == bfd_link_hash_undefweak
4605 && ((struct ppc_link_hash_entry *) h)->fake))
4606 return h;
4608 if (name[0] == '.')
4609 return h;
4611 len = strlen (name);
4612 dot_name = bfd_alloc (abfd, len + 2);
4613 if (dot_name == NULL)
4614 return (struct elf_link_hash_entry *) 0 - 1;
4615 dot_name[0] = '.';
4616 memcpy (dot_name + 1, name, len + 1);
4617 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4618 bfd_release (abfd, dot_name);
4619 return h;
4622 /* This function satisfies all old ABI object references to ".bar" if a
4623 new ABI object defines "bar". Well, at least, undefined dot symbols
4624 are made weak. This stops later archive searches from including an
4625 object if we already have a function descriptor definition. It also
4626 prevents the linker complaining about undefined symbols.
4627 We also check and correct mismatched symbol visibility here. The
4628 most restrictive visibility of the function descriptor and the
4629 function entry symbol is used. */
4631 static bfd_boolean
4632 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4634 struct ppc_link_hash_table *htab;
4635 struct ppc_link_hash_entry *fdh;
4637 if (eh->elf.root.type == bfd_link_hash_indirect)
4638 return TRUE;
4640 if (eh->elf.root.type == bfd_link_hash_warning)
4641 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4643 if (eh->elf.root.root.string[0] != '.')
4644 abort ();
4646 htab = ppc_hash_table (info);
4647 if (htab == NULL)
4648 return FALSE;
4650 fdh = lookup_fdh (eh, htab);
4651 if (fdh == NULL)
4653 if (!info->relocatable
4654 && (eh->elf.root.type == bfd_link_hash_undefined
4655 || eh->elf.root.type == bfd_link_hash_undefweak)
4656 && eh->elf.ref_regular)
4658 /* Make an undefweak function descriptor sym, which is enough to
4659 pull in an --as-needed shared lib, but won't cause link
4660 errors. Archives are handled elsewhere. */
4661 fdh = make_fdh (info, eh);
4662 if (fdh == NULL)
4663 return FALSE;
4664 fdh->elf.ref_regular = 1;
4667 else
4669 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4670 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4671 if (entry_vis < descr_vis)
4672 fdh->elf.other += entry_vis - descr_vis;
4673 else if (entry_vis > descr_vis)
4674 eh->elf.other += descr_vis - entry_vis;
4676 if ((fdh->elf.root.type == bfd_link_hash_defined
4677 || fdh->elf.root.type == bfd_link_hash_defweak)
4678 && eh->elf.root.type == bfd_link_hash_undefined)
4680 eh->elf.root.type = bfd_link_hash_undefweak;
4681 eh->was_undefined = 1;
4682 htab->twiddled_syms = 1;
4686 return TRUE;
4689 /* Process list of dot-symbols we made in link_hash_newfunc. */
4691 static bfd_boolean
4692 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
4694 struct ppc_link_hash_table *htab;
4695 struct ppc_link_hash_entry **p, *eh;
4697 if (!is_ppc64_elf (info->output_bfd))
4698 return TRUE;
4699 htab = ppc_hash_table (info);
4700 if (htab == NULL)
4701 return FALSE;
4703 if (is_ppc64_elf (ibfd))
4705 p = &htab->dot_syms;
4706 while ((eh = *p) != NULL)
4708 *p = NULL;
4709 if (!add_symbol_adjust (eh, info))
4710 return FALSE;
4711 p = &eh->u.next_dot_sym;
4715 /* Clear the list for non-ppc64 input files. */
4716 p = &htab->dot_syms;
4717 while ((eh = *p) != NULL)
4719 *p = NULL;
4720 p = &eh->u.next_dot_sym;
4723 /* We need to fix the undefs list for any syms we have twiddled to
4724 undef_weak. */
4725 if (htab->twiddled_syms)
4727 bfd_link_repair_undef_list (&htab->elf.root);
4728 htab->twiddled_syms = 0;
4730 return TRUE;
4733 /* Undo hash table changes when an --as-needed input file is determined
4734 not to be needed. */
4736 static bfd_boolean
4737 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4738 struct bfd_link_info *info)
4740 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4742 if (htab == NULL)
4743 return FALSE;
4745 htab->dot_syms = NULL;
4746 return TRUE;
4749 /* If --just-symbols against a final linked binary, then assume we need
4750 toc adjusting stubs when calling functions defined there. */
4752 static void
4753 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4755 if ((sec->flags & SEC_CODE) != 0
4756 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4757 && is_ppc64_elf (sec->owner))
4759 asection *got = bfd_get_section_by_name (sec->owner, ".got");
4760 if (got != NULL
4761 && got->size >= elf_backend_got_header_size
4762 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4763 sec->has_toc_reloc = 1;
4765 _bfd_elf_link_just_syms (sec, info);
4768 static struct plt_entry **
4769 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4770 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4772 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4773 struct plt_entry **local_plt;
4774 unsigned char *local_got_tls_masks;
4776 if (local_got_ents == NULL)
4778 bfd_size_type size = symtab_hdr->sh_info;
4780 size *= (sizeof (*local_got_ents)
4781 + sizeof (*local_plt)
4782 + sizeof (*local_got_tls_masks));
4783 local_got_ents = bfd_zalloc (abfd, size);
4784 if (local_got_ents == NULL)
4785 return NULL;
4786 elf_local_got_ents (abfd) = local_got_ents;
4789 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
4791 struct got_entry *ent;
4793 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4794 if (ent->addend == r_addend
4795 && ent->owner == abfd
4796 && ent->tls_type == tls_type)
4797 break;
4798 if (ent == NULL)
4800 bfd_size_type amt = sizeof (*ent);
4801 ent = bfd_alloc (abfd, amt);
4802 if (ent == NULL)
4803 return FALSE;
4804 ent->next = local_got_ents[r_symndx];
4805 ent->addend = r_addend;
4806 ent->owner = abfd;
4807 ent->tls_type = tls_type;
4808 ent->is_indirect = FALSE;
4809 ent->got.refcount = 0;
4810 local_got_ents[r_symndx] = ent;
4812 ent->got.refcount += 1;
4815 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4816 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4817 local_got_tls_masks[r_symndx] |= tls_type;
4819 return local_plt + r_symndx;
4822 static bfd_boolean
4823 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4825 struct plt_entry *ent;
4827 for (ent = *plist; ent != NULL; ent = ent->next)
4828 if (ent->addend == addend)
4829 break;
4830 if (ent == NULL)
4832 bfd_size_type amt = sizeof (*ent);
4833 ent = bfd_alloc (abfd, amt);
4834 if (ent == NULL)
4835 return FALSE;
4836 ent->next = *plist;
4837 ent->addend = addend;
4838 ent->plt.refcount = 0;
4839 *plist = ent;
4841 ent->plt.refcount += 1;
4842 return TRUE;
4845 static bfd_boolean
4846 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4848 return (r_type == R_PPC64_REL24
4849 || r_type == R_PPC64_REL14
4850 || r_type == R_PPC64_REL14_BRTAKEN
4851 || r_type == R_PPC64_REL14_BRNTAKEN
4852 || r_type == R_PPC64_ADDR24
4853 || r_type == R_PPC64_ADDR14
4854 || r_type == R_PPC64_ADDR14_BRTAKEN
4855 || r_type == R_PPC64_ADDR14_BRNTAKEN);
4858 /* Look through the relocs for a section during the first phase, and
4859 calculate needed space in the global offset table, procedure
4860 linkage table, and dynamic reloc sections. */
4862 static bfd_boolean
4863 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4864 asection *sec, const Elf_Internal_Rela *relocs)
4866 struct ppc_link_hash_table *htab;
4867 Elf_Internal_Shdr *symtab_hdr;
4868 struct elf_link_hash_entry **sym_hashes;
4869 const Elf_Internal_Rela *rel;
4870 const Elf_Internal_Rela *rel_end;
4871 asection *sreloc;
4872 asection **opd_sym_map;
4873 struct elf_link_hash_entry *tga, *dottga;
4875 if (info->relocatable)
4876 return TRUE;
4878 /* Don't do anything special with non-loaded, non-alloced sections.
4879 In particular, any relocs in such sections should not affect GOT
4880 and PLT reference counting (ie. we don't allow them to create GOT
4881 or PLT entries), there's no possibility or desire to optimize TLS
4882 relocs, and there's not much point in propagating relocs to shared
4883 libs that the dynamic linker won't relocate. */
4884 if ((sec->flags & SEC_ALLOC) == 0)
4885 return TRUE;
4887 BFD_ASSERT (is_ppc64_elf (abfd));
4889 htab = ppc_hash_table (info);
4890 if (htab == NULL)
4891 return FALSE;
4893 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4894 FALSE, FALSE, TRUE);
4895 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4896 FALSE, FALSE, TRUE);
4897 symtab_hdr = &elf_symtab_hdr (abfd);
4898 sym_hashes = elf_sym_hashes (abfd);
4899 sreloc = NULL;
4900 opd_sym_map = NULL;
4901 if (strcmp (sec->name, ".opd") == 0)
4903 /* Garbage collection needs some extra help with .opd sections.
4904 We don't want to necessarily keep everything referenced by
4905 relocs in .opd, as that would keep all functions. Instead,
4906 if we reference an .opd symbol (a function descriptor), we
4907 want to keep the function code symbol's section. This is
4908 easy for global symbols, but for local syms we need to keep
4909 information about the associated function section. */
4910 bfd_size_type amt;
4912 amt = sec->size * sizeof (*opd_sym_map) / 8;
4913 opd_sym_map = bfd_zalloc (abfd, amt);
4914 if (opd_sym_map == NULL)
4915 return FALSE;
4916 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
4917 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4918 ppc64_elf_section_data (sec)->sec_type = sec_opd;
4921 if (htab->sfpr == NULL
4922 && !create_linkage_sections (htab->elf.dynobj, info))
4923 return FALSE;
4925 rel_end = relocs + sec->reloc_count;
4926 for (rel = relocs; rel < rel_end; rel++)
4928 unsigned long r_symndx;
4929 struct elf_link_hash_entry *h;
4930 enum elf_ppc64_reloc_type r_type;
4931 int tls_type;
4932 struct _ppc64_elf_section_data *ppc64_sec;
4933 struct plt_entry **ifunc;
4935 r_symndx = ELF64_R_SYM (rel->r_info);
4936 if (r_symndx < symtab_hdr->sh_info)
4937 h = NULL;
4938 else
4940 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4941 h = elf_follow_link (h);
4944 tls_type = 0;
4945 ifunc = NULL;
4946 if (h != NULL)
4948 if (h->type == STT_GNU_IFUNC)
4950 h->needs_plt = 1;
4951 ifunc = &h->plt.plist;
4954 else
4956 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4957 abfd, r_symndx);
4958 if (isym == NULL)
4959 return FALSE;
4961 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4963 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4964 rel->r_addend, PLT_IFUNC);
4965 if (ifunc == NULL)
4966 return FALSE;
4969 r_type = ELF64_R_TYPE (rel->r_info);
4970 if (is_branch_reloc (r_type))
4972 if (h != NULL && (h == tga || h == dottga))
4974 if (rel != relocs
4975 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4976 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4977 /* We have a new-style __tls_get_addr call with a marker
4978 reloc. */
4980 else
4981 /* Mark this section as having an old-style call. */
4982 sec->has_tls_get_addr_call = 1;
4985 /* STT_GNU_IFUNC symbols must have a PLT entry. */
4986 if (ifunc != NULL
4987 && !update_plt_info (abfd, ifunc, rel->r_addend))
4988 return FALSE;
4991 switch (r_type)
4993 case R_PPC64_TLSGD:
4994 case R_PPC64_TLSLD:
4995 /* These special tls relocs tie a call to __tls_get_addr with
4996 its parameter symbol. */
4997 break;
4999 case R_PPC64_GOT_TLSLD16:
5000 case R_PPC64_GOT_TLSLD16_LO:
5001 case R_PPC64_GOT_TLSLD16_HI:
5002 case R_PPC64_GOT_TLSLD16_HA:
5003 tls_type = TLS_TLS | TLS_LD;
5004 goto dogottls;
5006 case R_PPC64_GOT_TLSGD16:
5007 case R_PPC64_GOT_TLSGD16_LO:
5008 case R_PPC64_GOT_TLSGD16_HI:
5009 case R_PPC64_GOT_TLSGD16_HA:
5010 tls_type = TLS_TLS | TLS_GD;
5011 goto dogottls;
5013 case R_PPC64_GOT_TPREL16_DS:
5014 case R_PPC64_GOT_TPREL16_LO_DS:
5015 case R_PPC64_GOT_TPREL16_HI:
5016 case R_PPC64_GOT_TPREL16_HA:
5017 if (!info->executable)
5018 info->flags |= DF_STATIC_TLS;
5019 tls_type = TLS_TLS | TLS_TPREL;
5020 goto dogottls;
5022 case R_PPC64_GOT_DTPREL16_DS:
5023 case R_PPC64_GOT_DTPREL16_LO_DS:
5024 case R_PPC64_GOT_DTPREL16_HI:
5025 case R_PPC64_GOT_DTPREL16_HA:
5026 tls_type = TLS_TLS | TLS_DTPREL;
5027 dogottls:
5028 sec->has_tls_reloc = 1;
5029 /* Fall thru */
5031 case R_PPC64_GOT16:
5032 case R_PPC64_GOT16_DS:
5033 case R_PPC64_GOT16_HA:
5034 case R_PPC64_GOT16_HI:
5035 case R_PPC64_GOT16_LO:
5036 case R_PPC64_GOT16_LO_DS:
5037 /* This symbol requires a global offset table entry. */
5038 sec->has_toc_reloc = 1;
5039 if (r_type == R_PPC64_GOT_TLSLD16
5040 || r_type == R_PPC64_GOT_TLSGD16
5041 || r_type == R_PPC64_GOT_TPREL16_DS
5042 || r_type == R_PPC64_GOT_DTPREL16_DS
5043 || r_type == R_PPC64_GOT16
5044 || r_type == R_PPC64_GOT16_DS)
5046 htab->do_multi_toc = 1;
5047 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5050 if (ppc64_elf_tdata (abfd)->got == NULL
5051 && !create_got_section (abfd, info))
5052 return FALSE;
5054 if (h != NULL)
5056 struct ppc_link_hash_entry *eh;
5057 struct got_entry *ent;
5059 eh = (struct ppc_link_hash_entry *) h;
5060 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5061 if (ent->addend == rel->r_addend
5062 && ent->owner == abfd
5063 && ent->tls_type == tls_type)
5064 break;
5065 if (ent == NULL)
5067 bfd_size_type amt = sizeof (*ent);
5068 ent = bfd_alloc (abfd, amt);
5069 if (ent == NULL)
5070 return FALSE;
5071 ent->next = eh->elf.got.glist;
5072 ent->addend = rel->r_addend;
5073 ent->owner = abfd;
5074 ent->tls_type = tls_type;
5075 ent->is_indirect = FALSE;
5076 ent->got.refcount = 0;
5077 eh->elf.got.glist = ent;
5079 ent->got.refcount += 1;
5080 eh->tls_mask |= tls_type;
5082 else
5083 /* This is a global offset table entry for a local symbol. */
5084 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5085 rel->r_addend, tls_type))
5086 return FALSE;
5087 break;
5089 case R_PPC64_PLT16_HA:
5090 case R_PPC64_PLT16_HI:
5091 case R_PPC64_PLT16_LO:
5092 case R_PPC64_PLT32:
5093 case R_PPC64_PLT64:
5094 /* This symbol requires a procedure linkage table entry. We
5095 actually build the entry in adjust_dynamic_symbol,
5096 because this might be a case of linking PIC code without
5097 linking in any dynamic objects, in which case we don't
5098 need to generate a procedure linkage table after all. */
5099 if (h == NULL)
5101 /* It does not make sense to have a procedure linkage
5102 table entry for a local symbol. */
5103 bfd_set_error (bfd_error_bad_value);
5104 return FALSE;
5106 else
5108 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5109 return FALSE;
5110 h->needs_plt = 1;
5111 if (h->root.root.string[0] == '.'
5112 && h->root.root.string[1] != '\0')
5113 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5115 break;
5117 /* The following relocations don't need to propagate the
5118 relocation if linking a shared object since they are
5119 section relative. */
5120 case R_PPC64_SECTOFF:
5121 case R_PPC64_SECTOFF_LO:
5122 case R_PPC64_SECTOFF_HI:
5123 case R_PPC64_SECTOFF_HA:
5124 case R_PPC64_SECTOFF_DS:
5125 case R_PPC64_SECTOFF_LO_DS:
5126 case R_PPC64_DTPREL16:
5127 case R_PPC64_DTPREL16_LO:
5128 case R_PPC64_DTPREL16_HI:
5129 case R_PPC64_DTPREL16_HA:
5130 case R_PPC64_DTPREL16_DS:
5131 case R_PPC64_DTPREL16_LO_DS:
5132 case R_PPC64_DTPREL16_HIGHER:
5133 case R_PPC64_DTPREL16_HIGHERA:
5134 case R_PPC64_DTPREL16_HIGHEST:
5135 case R_PPC64_DTPREL16_HIGHESTA:
5136 break;
5138 /* Nor do these. */
5139 case R_PPC64_REL16:
5140 case R_PPC64_REL16_LO:
5141 case R_PPC64_REL16_HI:
5142 case R_PPC64_REL16_HA:
5143 break;
5145 case R_PPC64_TOC16:
5146 case R_PPC64_TOC16_DS:
5147 htab->do_multi_toc = 1;
5148 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5149 case R_PPC64_TOC16_LO:
5150 case R_PPC64_TOC16_HI:
5151 case R_PPC64_TOC16_HA:
5152 case R_PPC64_TOC16_LO_DS:
5153 sec->has_toc_reloc = 1;
5154 break;
5156 /* This relocation describes the C++ object vtable hierarchy.
5157 Reconstruct it for later use during GC. */
5158 case R_PPC64_GNU_VTINHERIT:
5159 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5160 return FALSE;
5161 break;
5163 /* This relocation describes which C++ vtable entries are actually
5164 used. Record for later use during GC. */
5165 case R_PPC64_GNU_VTENTRY:
5166 BFD_ASSERT (h != NULL);
5167 if (h != NULL
5168 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5169 return FALSE;
5170 break;
5172 case R_PPC64_REL14:
5173 case R_PPC64_REL14_BRTAKEN:
5174 case R_PPC64_REL14_BRNTAKEN:
5176 asection *dest = NULL;
5178 /* Heuristic: If jumping outside our section, chances are
5179 we are going to need a stub. */
5180 if (h != NULL)
5182 /* If the sym is weak it may be overridden later, so
5183 don't assume we know where a weak sym lives. */
5184 if (h->root.type == bfd_link_hash_defined)
5185 dest = h->root.u.def.section;
5187 else
5189 Elf_Internal_Sym *isym;
5191 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5192 abfd, r_symndx);
5193 if (isym == NULL)
5194 return FALSE;
5196 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5199 if (dest != sec)
5200 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5202 /* Fall through. */
5204 case R_PPC64_REL24:
5205 if (h != NULL && ifunc == NULL)
5207 /* We may need a .plt entry if the function this reloc
5208 refers to is in a shared lib. */
5209 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5210 return FALSE;
5211 h->needs_plt = 1;
5212 if (h->root.root.string[0] == '.'
5213 && h->root.root.string[1] != '\0')
5214 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5215 if (h == tga || h == dottga)
5216 sec->has_tls_reloc = 1;
5218 break;
5220 case R_PPC64_TPREL64:
5221 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5222 if (!info->executable)
5223 info->flags |= DF_STATIC_TLS;
5224 goto dotlstoc;
5226 case R_PPC64_DTPMOD64:
5227 if (rel + 1 < rel_end
5228 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5229 && rel[1].r_offset == rel->r_offset + 8)
5230 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5231 else
5232 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5233 goto dotlstoc;
5235 case R_PPC64_DTPREL64:
5236 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5237 if (rel != relocs
5238 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5239 && rel[-1].r_offset == rel->r_offset - 8)
5240 /* This is the second reloc of a dtpmod, dtprel pair.
5241 Don't mark with TLS_DTPREL. */
5242 goto dodyn;
5244 dotlstoc:
5245 sec->has_tls_reloc = 1;
5246 if (h != NULL)
5248 struct ppc_link_hash_entry *eh;
5249 eh = (struct ppc_link_hash_entry *) h;
5250 eh->tls_mask |= tls_type;
5252 else
5253 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5254 rel->r_addend, tls_type))
5255 return FALSE;
5257 ppc64_sec = ppc64_elf_section_data (sec);
5258 if (ppc64_sec->sec_type != sec_toc)
5260 bfd_size_type amt;
5262 /* One extra to simplify get_tls_mask. */
5263 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5264 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5265 if (ppc64_sec->u.toc.symndx == NULL)
5266 return FALSE;
5267 amt = sec->size * sizeof (bfd_vma) / 8;
5268 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5269 if (ppc64_sec->u.toc.add == NULL)
5270 return FALSE;
5271 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5272 ppc64_sec->sec_type = sec_toc;
5274 BFD_ASSERT (rel->r_offset % 8 == 0);
5275 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5276 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5278 /* Mark the second slot of a GD or LD entry.
5279 -1 to indicate GD and -2 to indicate LD. */
5280 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5281 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5282 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5283 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5284 goto dodyn;
5286 case R_PPC64_TPREL16:
5287 case R_PPC64_TPREL16_LO:
5288 case R_PPC64_TPREL16_HI:
5289 case R_PPC64_TPREL16_HA:
5290 case R_PPC64_TPREL16_DS:
5291 case R_PPC64_TPREL16_LO_DS:
5292 case R_PPC64_TPREL16_HIGHER:
5293 case R_PPC64_TPREL16_HIGHERA:
5294 case R_PPC64_TPREL16_HIGHEST:
5295 case R_PPC64_TPREL16_HIGHESTA:
5296 if (info->shared)
5298 if (!info->executable)
5299 info->flags |= DF_STATIC_TLS;
5300 goto dodyn;
5302 break;
5304 case R_PPC64_ADDR64:
5305 if (opd_sym_map != NULL
5306 && rel + 1 < rel_end
5307 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5309 if (h != NULL)
5311 if (h->root.root.string[0] == '.'
5312 && h->root.root.string[1] != 0
5313 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5315 else
5316 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5318 else
5320 asection *s;
5321 Elf_Internal_Sym *isym;
5323 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5324 abfd, r_symndx);
5325 if (isym == NULL)
5326 return FALSE;
5328 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5329 if (s != NULL && s != sec)
5330 opd_sym_map[rel->r_offset / 8] = s;
5333 /* Fall through. */
5335 case R_PPC64_REL30:
5336 case R_PPC64_REL32:
5337 case R_PPC64_REL64:
5338 case R_PPC64_ADDR14:
5339 case R_PPC64_ADDR14_BRNTAKEN:
5340 case R_PPC64_ADDR14_BRTAKEN:
5341 case R_PPC64_ADDR16:
5342 case R_PPC64_ADDR16_DS:
5343 case R_PPC64_ADDR16_HA:
5344 case R_PPC64_ADDR16_HI:
5345 case R_PPC64_ADDR16_HIGHER:
5346 case R_PPC64_ADDR16_HIGHERA:
5347 case R_PPC64_ADDR16_HIGHEST:
5348 case R_PPC64_ADDR16_HIGHESTA:
5349 case R_PPC64_ADDR16_LO:
5350 case R_PPC64_ADDR16_LO_DS:
5351 case R_PPC64_ADDR24:
5352 case R_PPC64_ADDR32:
5353 case R_PPC64_UADDR16:
5354 case R_PPC64_UADDR32:
5355 case R_PPC64_UADDR64:
5356 case R_PPC64_TOC:
5357 if (h != NULL && !info->shared)
5358 /* We may need a copy reloc. */
5359 h->non_got_ref = 1;
5361 /* Don't propagate .opd relocs. */
5362 if (NO_OPD_RELOCS && opd_sym_map != NULL)
5363 break;
5365 /* If we are creating a shared library, and this is a reloc
5366 against a global symbol, or a non PC relative reloc
5367 against a local symbol, then we need to copy the reloc
5368 into the shared library. However, if we are linking with
5369 -Bsymbolic, we do not need to copy a reloc against a
5370 global symbol which is defined in an object we are
5371 including in the link (i.e., DEF_REGULAR is set). At
5372 this point we have not seen all the input files, so it is
5373 possible that DEF_REGULAR is not set now but will be set
5374 later (it is never cleared). In case of a weak definition,
5375 DEF_REGULAR may be cleared later by a strong definition in
5376 a shared library. We account for that possibility below by
5377 storing information in the dyn_relocs field of the hash
5378 table entry. A similar situation occurs when creating
5379 shared libraries and symbol visibility changes render the
5380 symbol local.
5382 If on the other hand, we are creating an executable, we
5383 may need to keep relocations for symbols satisfied by a
5384 dynamic library if we manage to avoid copy relocs for the
5385 symbol. */
5386 dodyn:
5387 if ((info->shared
5388 && (must_be_dyn_reloc (info, r_type)
5389 || (h != NULL
5390 && (! info->symbolic
5391 || h->root.type == bfd_link_hash_defweak
5392 || !h->def_regular))))
5393 || (ELIMINATE_COPY_RELOCS
5394 && !info->shared
5395 && h != NULL
5396 && (h->root.type == bfd_link_hash_defweak
5397 || !h->def_regular))
5398 || (!info->shared
5399 && ifunc != NULL))
5401 struct ppc_dyn_relocs *p;
5402 struct ppc_dyn_relocs **head;
5404 /* We must copy these reloc types into the output file.
5405 Create a reloc section in dynobj and make room for
5406 this reloc. */
5407 if (sreloc == NULL)
5409 sreloc = _bfd_elf_make_dynamic_reloc_section
5410 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5412 if (sreloc == NULL)
5413 return FALSE;
5416 /* If this is a global symbol, we count the number of
5417 relocations we need for this symbol. */
5418 if (h != NULL)
5420 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5422 else
5424 /* Track dynamic relocs needed for local syms too.
5425 We really need local syms available to do this
5426 easily. Oh well. */
5427 asection *s;
5428 void *vpp;
5429 Elf_Internal_Sym *isym;
5431 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5432 abfd, r_symndx);
5433 if (isym == NULL)
5434 return FALSE;
5436 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5437 if (s == NULL)
5438 s = sec;
5440 vpp = &elf_section_data (s)->local_dynrel;
5441 head = (struct ppc_dyn_relocs **) vpp;
5444 p = *head;
5445 if (p == NULL || p->sec != sec)
5447 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5448 if (p == NULL)
5449 return FALSE;
5450 p->next = *head;
5451 *head = p;
5452 p->sec = sec;
5453 p->count = 0;
5454 p->pc_count = 0;
5457 p->count += 1;
5458 if (!must_be_dyn_reloc (info, r_type))
5459 p->pc_count += 1;
5461 break;
5463 default:
5464 break;
5468 return TRUE;
5471 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5472 of the code entry point, and its section. */
5474 static bfd_vma
5475 opd_entry_value (asection *opd_sec,
5476 bfd_vma offset,
5477 asection **code_sec,
5478 bfd_vma *code_off)
5480 bfd *opd_bfd = opd_sec->owner;
5481 Elf_Internal_Rela *relocs;
5482 Elf_Internal_Rela *lo, *hi, *look;
5483 bfd_vma val;
5485 /* No relocs implies we are linking a --just-symbols object. */
5486 if (opd_sec->reloc_count == 0)
5488 char buf[8];
5490 if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
5491 return (bfd_vma) -1;
5493 val = bfd_get_64 (opd_bfd, buf);
5494 if (code_sec != NULL)
5496 asection *sec, *likely = NULL;
5497 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5498 if (sec->vma <= val
5499 && (sec->flags & SEC_LOAD) != 0
5500 && (sec->flags & SEC_ALLOC) != 0)
5501 likely = sec;
5502 if (likely != NULL)
5504 *code_sec = likely;
5505 if (code_off != NULL)
5506 *code_off = val - likely->vma;
5509 return val;
5512 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5514 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5515 if (relocs == NULL)
5516 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5518 /* Go find the opd reloc at the sym address. */
5519 lo = relocs;
5520 BFD_ASSERT (lo != NULL);
5521 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5522 val = (bfd_vma) -1;
5523 while (lo < hi)
5525 look = lo + (hi - lo) / 2;
5526 if (look->r_offset < offset)
5527 lo = look + 1;
5528 else if (look->r_offset > offset)
5529 hi = look;
5530 else
5532 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5534 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5535 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5537 unsigned long symndx = ELF64_R_SYM (look->r_info);
5538 asection *sec;
5540 if (symndx < symtab_hdr->sh_info)
5542 Elf_Internal_Sym *sym;
5544 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5545 if (sym == NULL)
5547 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5548 symtab_hdr->sh_info,
5549 0, NULL, NULL, NULL);
5550 if (sym == NULL)
5551 break;
5552 symtab_hdr->contents = (bfd_byte *) sym;
5555 sym += symndx;
5556 val = sym->st_value;
5557 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5558 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5560 else
5562 struct elf_link_hash_entry **sym_hashes;
5563 struct elf_link_hash_entry *rh;
5565 sym_hashes = elf_sym_hashes (opd_bfd);
5566 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5567 rh = elf_follow_link (rh);
5568 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5569 || rh->root.type == bfd_link_hash_defweak);
5570 val = rh->root.u.def.value;
5571 sec = rh->root.u.def.section;
5573 val += look->r_addend;
5574 if (code_off != NULL)
5575 *code_off = val;
5576 if (code_sec != NULL)
5577 *code_sec = sec;
5578 if (sec != NULL && sec->output_section != NULL)
5579 val += sec->output_section->vma + sec->output_offset;
5581 break;
5585 return val;
5588 /* Return true if symbol is defined in a regular object file. */
5590 static bfd_boolean
5591 is_static_defined (struct elf_link_hash_entry *h)
5593 return ((h->root.type == bfd_link_hash_defined
5594 || h->root.type == bfd_link_hash_defweak)
5595 && h->root.u.def.section != NULL
5596 && h->root.u.def.section->output_section != NULL);
5599 /* If FDH is a function descriptor symbol, return the associated code
5600 entry symbol if it is defined. Return NULL otherwise. */
5602 static struct ppc_link_hash_entry *
5603 defined_code_entry (struct ppc_link_hash_entry *fdh)
5605 if (fdh->is_func_descriptor)
5607 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5608 if (fh->elf.root.type == bfd_link_hash_defined
5609 || fh->elf.root.type == bfd_link_hash_defweak)
5610 return fh;
5612 return NULL;
5615 /* If FH is a function code entry symbol, return the associated
5616 function descriptor symbol if it is defined. Return NULL otherwise. */
5618 static struct ppc_link_hash_entry *
5619 defined_func_desc (struct ppc_link_hash_entry *fh)
5621 if (fh->oh != NULL
5622 && fh->oh->is_func_descriptor)
5624 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5625 if (fdh->elf.root.type == bfd_link_hash_defined
5626 || fdh->elf.root.type == bfd_link_hash_defweak)
5627 return fdh;
5629 return NULL;
5632 /* Mark all our entry sym sections, both opd and code section. */
5634 static void
5635 ppc64_elf_gc_keep (struct bfd_link_info *info)
5637 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5638 struct bfd_sym_chain *sym;
5640 if (htab == NULL)
5641 return;
5643 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5645 struct ppc_link_hash_entry *eh, *fh;
5646 asection *sec;
5648 eh = (struct ppc_link_hash_entry *)
5649 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5650 if (eh == NULL)
5651 continue;
5652 if (eh->elf.root.type != bfd_link_hash_defined
5653 && eh->elf.root.type != bfd_link_hash_defweak)
5654 continue;
5656 fh = defined_code_entry (eh);
5657 if (fh != NULL)
5659 sec = fh->elf.root.u.def.section;
5660 sec->flags |= SEC_KEEP;
5662 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5663 && opd_entry_value (eh->elf.root.u.def.section,
5664 eh->elf.root.u.def.value,
5665 &sec, NULL) != (bfd_vma) -1)
5666 sec->flags |= SEC_KEEP;
5668 sec = eh->elf.root.u.def.section;
5669 sec->flags |= SEC_KEEP;
5673 /* Mark sections containing dynamically referenced symbols. When
5674 building shared libraries, we must assume that any visible symbol is
5675 referenced. */
5677 static bfd_boolean
5678 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5680 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5681 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5682 struct ppc_link_hash_entry *fdh;
5684 if (eh->elf.root.type == bfd_link_hash_warning)
5685 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5687 /* Dynamic linking info is on the func descriptor sym. */
5688 fdh = defined_func_desc (eh);
5689 if (fdh != NULL)
5690 eh = fdh;
5692 if ((eh->elf.root.type == bfd_link_hash_defined
5693 || eh->elf.root.type == bfd_link_hash_defweak)
5694 && (eh->elf.ref_dynamic
5695 || (!info->executable
5696 && eh->elf.def_regular
5697 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5698 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
5700 asection *code_sec;
5701 struct ppc_link_hash_entry *fh;
5703 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5705 /* Function descriptor syms cause the associated
5706 function code sym section to be marked. */
5707 fh = defined_code_entry (eh);
5708 if (fh != NULL)
5710 code_sec = fh->elf.root.u.def.section;
5711 code_sec->flags |= SEC_KEEP;
5713 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5714 && opd_entry_value (eh->elf.root.u.def.section,
5715 eh->elf.root.u.def.value,
5716 &code_sec, NULL) != (bfd_vma) -1)
5717 code_sec->flags |= SEC_KEEP;
5720 return TRUE;
5723 /* Return the section that should be marked against GC for a given
5724 relocation. */
5726 static asection *
5727 ppc64_elf_gc_mark_hook (asection *sec,
5728 struct bfd_link_info *info,
5729 Elf_Internal_Rela *rel,
5730 struct elf_link_hash_entry *h,
5731 Elf_Internal_Sym *sym)
5733 asection *rsec;
5735 /* Syms return NULL if we're marking .opd, so we avoid marking all
5736 function sections, as all functions are referenced in .opd. */
5737 rsec = NULL;
5738 if (get_opd_info (sec) != NULL)
5739 return rsec;
5741 if (h != NULL)
5743 enum elf_ppc64_reloc_type r_type;
5744 struct ppc_link_hash_entry *eh, *fh, *fdh;
5746 r_type = ELF64_R_TYPE (rel->r_info);
5747 switch (r_type)
5749 case R_PPC64_GNU_VTINHERIT:
5750 case R_PPC64_GNU_VTENTRY:
5751 break;
5753 default:
5754 switch (h->root.type)
5756 case bfd_link_hash_defined:
5757 case bfd_link_hash_defweak:
5758 eh = (struct ppc_link_hash_entry *) h;
5759 fdh = defined_func_desc (eh);
5760 if (fdh != NULL)
5761 eh = fdh;
5763 /* Function descriptor syms cause the associated
5764 function code sym section to be marked. */
5765 fh = defined_code_entry (eh);
5766 if (fh != NULL)
5768 /* They also mark their opd section. */
5769 eh->elf.root.u.def.section->gc_mark = 1;
5771 rsec = fh->elf.root.u.def.section;
5773 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5774 && opd_entry_value (eh->elf.root.u.def.section,
5775 eh->elf.root.u.def.value,
5776 &rsec, NULL) != (bfd_vma) -1)
5777 eh->elf.root.u.def.section->gc_mark = 1;
5778 else
5779 rsec = h->root.u.def.section;
5780 break;
5782 case bfd_link_hash_common:
5783 rsec = h->root.u.c.p->section;
5784 break;
5786 default:
5787 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5791 else
5793 struct _opd_sec_data *opd;
5795 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5796 opd = get_opd_info (rsec);
5797 if (opd != NULL && opd->func_sec != NULL)
5799 rsec->gc_mark = 1;
5801 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
5805 return rsec;
5808 /* Update the .got, .plt. and dynamic reloc reference counts for the
5809 section being removed. */
5811 static bfd_boolean
5812 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5813 asection *sec, const Elf_Internal_Rela *relocs)
5815 struct ppc_link_hash_table *htab;
5816 Elf_Internal_Shdr *symtab_hdr;
5817 struct elf_link_hash_entry **sym_hashes;
5818 struct got_entry **local_got_ents;
5819 const Elf_Internal_Rela *rel, *relend;
5821 if (info->relocatable)
5822 return TRUE;
5824 if ((sec->flags & SEC_ALLOC) == 0)
5825 return TRUE;
5827 elf_section_data (sec)->local_dynrel = NULL;
5829 htab = ppc_hash_table (info);
5830 if (htab == NULL)
5831 return FALSE;
5833 symtab_hdr = &elf_symtab_hdr (abfd);
5834 sym_hashes = elf_sym_hashes (abfd);
5835 local_got_ents = elf_local_got_ents (abfd);
5837 relend = relocs + sec->reloc_count;
5838 for (rel = relocs; rel < relend; rel++)
5840 unsigned long r_symndx;
5841 enum elf_ppc64_reloc_type r_type;
5842 struct elf_link_hash_entry *h = NULL;
5843 unsigned char tls_type = 0;
5845 r_symndx = ELF64_R_SYM (rel->r_info);
5846 r_type = ELF64_R_TYPE (rel->r_info);
5847 if (r_symndx >= symtab_hdr->sh_info)
5849 struct ppc_link_hash_entry *eh;
5850 struct ppc_dyn_relocs **pp;
5851 struct ppc_dyn_relocs *p;
5853 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5854 h = elf_follow_link (h);
5855 eh = (struct ppc_link_hash_entry *) h;
5857 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5858 if (p->sec == sec)
5860 /* Everything must go for SEC. */
5861 *pp = p->next;
5862 break;
5866 if (is_branch_reloc (r_type))
5868 struct plt_entry **ifunc = NULL;
5869 if (h != NULL)
5871 if (h->type == STT_GNU_IFUNC)
5872 ifunc = &h->plt.plist;
5874 else if (local_got_ents != NULL)
5876 struct plt_entry **local_plt = (struct plt_entry **)
5877 (local_got_ents + symtab_hdr->sh_info);
5878 unsigned char *local_got_tls_masks = (unsigned char *)
5879 (local_plt + symtab_hdr->sh_info);
5880 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5881 ifunc = local_plt + r_symndx;
5883 if (ifunc != NULL)
5885 struct plt_entry *ent;
5887 for (ent = *ifunc; ent != NULL; ent = ent->next)
5888 if (ent->addend == rel->r_addend)
5889 break;
5890 if (ent == NULL)
5891 abort ();
5892 if (ent->plt.refcount > 0)
5893 ent->plt.refcount -= 1;
5894 continue;
5898 switch (r_type)
5900 case R_PPC64_GOT_TLSLD16:
5901 case R_PPC64_GOT_TLSLD16_LO:
5902 case R_PPC64_GOT_TLSLD16_HI:
5903 case R_PPC64_GOT_TLSLD16_HA:
5904 tls_type = TLS_TLS | TLS_LD;
5905 goto dogot;
5907 case R_PPC64_GOT_TLSGD16:
5908 case R_PPC64_GOT_TLSGD16_LO:
5909 case R_PPC64_GOT_TLSGD16_HI:
5910 case R_PPC64_GOT_TLSGD16_HA:
5911 tls_type = TLS_TLS | TLS_GD;
5912 goto dogot;
5914 case R_PPC64_GOT_TPREL16_DS:
5915 case R_PPC64_GOT_TPREL16_LO_DS:
5916 case R_PPC64_GOT_TPREL16_HI:
5917 case R_PPC64_GOT_TPREL16_HA:
5918 tls_type = TLS_TLS | TLS_TPREL;
5919 goto dogot;
5921 case R_PPC64_GOT_DTPREL16_DS:
5922 case R_PPC64_GOT_DTPREL16_LO_DS:
5923 case R_PPC64_GOT_DTPREL16_HI:
5924 case R_PPC64_GOT_DTPREL16_HA:
5925 tls_type = TLS_TLS | TLS_DTPREL;
5926 goto dogot;
5928 case R_PPC64_GOT16:
5929 case R_PPC64_GOT16_DS:
5930 case R_PPC64_GOT16_HA:
5931 case R_PPC64_GOT16_HI:
5932 case R_PPC64_GOT16_LO:
5933 case R_PPC64_GOT16_LO_DS:
5934 dogot:
5936 struct got_entry *ent;
5938 if (h != NULL)
5939 ent = h->got.glist;
5940 else
5941 ent = local_got_ents[r_symndx];
5943 for (; ent != NULL; ent = ent->next)
5944 if (ent->addend == rel->r_addend
5945 && ent->owner == abfd
5946 && ent->tls_type == tls_type)
5947 break;
5948 if (ent == NULL)
5949 abort ();
5950 if (ent->got.refcount > 0)
5951 ent->got.refcount -= 1;
5953 break;
5955 case R_PPC64_PLT16_HA:
5956 case R_PPC64_PLT16_HI:
5957 case R_PPC64_PLT16_LO:
5958 case R_PPC64_PLT32:
5959 case R_PPC64_PLT64:
5960 case R_PPC64_REL14:
5961 case R_PPC64_REL14_BRNTAKEN:
5962 case R_PPC64_REL14_BRTAKEN:
5963 case R_PPC64_REL24:
5964 if (h != NULL)
5966 struct plt_entry *ent;
5968 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5969 if (ent->addend == rel->r_addend)
5970 break;
5971 if (ent != NULL && ent->plt.refcount > 0)
5972 ent->plt.refcount -= 1;
5974 break;
5976 default:
5977 break;
5980 return TRUE;
5983 /* The maximum size of .sfpr. */
5984 #define SFPR_MAX (218*4)
5986 struct sfpr_def_parms
5988 const char name[12];
5989 unsigned char lo, hi;
5990 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5991 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5994 /* Auto-generate _save*, _rest* functions in .sfpr. */
5996 static bfd_boolean
5997 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5999 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6000 unsigned int i;
6001 size_t len = strlen (parm->name);
6002 bfd_boolean writing = FALSE;
6003 char sym[16];
6005 if (htab == NULL)
6006 return FALSE;
6008 memcpy (sym, parm->name, len);
6009 sym[len + 2] = 0;
6011 for (i = parm->lo; i <= parm->hi; i++)
6013 struct elf_link_hash_entry *h;
6015 sym[len + 0] = i / 10 + '0';
6016 sym[len + 1] = i % 10 + '0';
6017 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6018 if (h != NULL
6019 && !h->def_regular)
6021 h->root.type = bfd_link_hash_defined;
6022 h->root.u.def.section = htab->sfpr;
6023 h->root.u.def.value = htab->sfpr->size;
6024 h->type = STT_FUNC;
6025 h->def_regular = 1;
6026 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6027 writing = TRUE;
6028 if (htab->sfpr->contents == NULL)
6030 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6031 if (htab->sfpr->contents == NULL)
6032 return FALSE;
6035 if (writing)
6037 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6038 if (i != parm->hi)
6039 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6040 else
6041 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6042 htab->sfpr->size = p - htab->sfpr->contents;
6046 return TRUE;
6049 static bfd_byte *
6050 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6052 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6053 return p + 4;
6056 static bfd_byte *
6057 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6059 p = savegpr0 (abfd, p, r);
6060 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6061 p = p + 4;
6062 bfd_put_32 (abfd, BLR, p);
6063 return p + 4;
6066 static bfd_byte *
6067 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6069 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6070 return p + 4;
6073 static bfd_byte *
6074 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6076 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6077 p = p + 4;
6078 p = restgpr0 (abfd, p, r);
6079 bfd_put_32 (abfd, MTLR_R0, p);
6080 p = p + 4;
6081 if (r == 29)
6083 p = restgpr0 (abfd, p, 30);
6084 p = restgpr0 (abfd, p, 31);
6086 bfd_put_32 (abfd, BLR, p);
6087 return p + 4;
6090 static bfd_byte *
6091 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6093 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6094 return p + 4;
6097 static bfd_byte *
6098 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6100 p = savegpr1 (abfd, p, r);
6101 bfd_put_32 (abfd, BLR, p);
6102 return p + 4;
6105 static bfd_byte *
6106 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6108 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6109 return p + 4;
6112 static bfd_byte *
6113 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6115 p = restgpr1 (abfd, p, r);
6116 bfd_put_32 (abfd, BLR, p);
6117 return p + 4;
6120 static bfd_byte *
6121 savefpr (bfd *abfd, bfd_byte *p, int r)
6123 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6124 return p + 4;
6127 static bfd_byte *
6128 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6130 p = savefpr (abfd, p, r);
6131 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6132 p = p + 4;
6133 bfd_put_32 (abfd, BLR, p);
6134 return p + 4;
6137 static bfd_byte *
6138 restfpr (bfd *abfd, bfd_byte *p, int r)
6140 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6141 return p + 4;
6144 static bfd_byte *
6145 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6147 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6148 p = p + 4;
6149 p = restfpr (abfd, p, r);
6150 bfd_put_32 (abfd, MTLR_R0, p);
6151 p = p + 4;
6152 if (r == 29)
6154 p = restfpr (abfd, p, 30);
6155 p = restfpr (abfd, p, 31);
6157 bfd_put_32 (abfd, BLR, p);
6158 return p + 4;
6161 static bfd_byte *
6162 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6164 p = savefpr (abfd, p, r);
6165 bfd_put_32 (abfd, BLR, p);
6166 return p + 4;
6169 static bfd_byte *
6170 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6172 p = restfpr (abfd, p, r);
6173 bfd_put_32 (abfd, BLR, p);
6174 return p + 4;
6177 static bfd_byte *
6178 savevr (bfd *abfd, bfd_byte *p, int r)
6180 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6181 p = p + 4;
6182 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6183 return p + 4;
6186 static bfd_byte *
6187 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6189 p = savevr (abfd, p, r);
6190 bfd_put_32 (abfd, BLR, p);
6191 return p + 4;
6194 static bfd_byte *
6195 restvr (bfd *abfd, bfd_byte *p, int r)
6197 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6198 p = p + 4;
6199 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6200 return p + 4;
6203 static bfd_byte *
6204 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6206 p = restvr (abfd, p, r);
6207 bfd_put_32 (abfd, BLR, p);
6208 return p + 4;
6211 /* Called via elf_link_hash_traverse to transfer dynamic linking
6212 information on function code symbol entries to their corresponding
6213 function descriptor symbol entries. */
6215 static bfd_boolean
6216 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6218 struct bfd_link_info *info;
6219 struct ppc_link_hash_table *htab;
6220 struct plt_entry *ent;
6221 struct ppc_link_hash_entry *fh;
6222 struct ppc_link_hash_entry *fdh;
6223 bfd_boolean force_local;
6225 fh = (struct ppc_link_hash_entry *) h;
6226 if (fh->elf.root.type == bfd_link_hash_indirect)
6227 return TRUE;
6229 if (fh->elf.root.type == bfd_link_hash_warning)
6230 fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
6232 info = inf;
6233 htab = ppc_hash_table (info);
6234 if (htab == NULL)
6235 return FALSE;
6237 /* Resolve undefined references to dot-symbols as the value
6238 in the function descriptor, if we have one in a regular object.
6239 This is to satisfy cases like ".quad .foo". Calls to functions
6240 in dynamic objects are handled elsewhere. */
6241 if (fh->elf.root.type == bfd_link_hash_undefweak
6242 && fh->was_undefined
6243 && (fdh = defined_func_desc (fh)) != NULL
6244 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6245 && opd_entry_value (fdh->elf.root.u.def.section,
6246 fdh->elf.root.u.def.value,
6247 &fh->elf.root.u.def.section,
6248 &fh->elf.root.u.def.value) != (bfd_vma) -1)
6250 fh->elf.root.type = fdh->elf.root.type;
6251 fh->elf.forced_local = 1;
6252 fh->elf.def_regular = fdh->elf.def_regular;
6253 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6256 /* If this is a function code symbol, transfer dynamic linking
6257 information to the function descriptor symbol. */
6258 if (!fh->is_func)
6259 return TRUE;
6261 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6262 if (ent->plt.refcount > 0)
6263 break;
6264 if (ent == NULL
6265 || fh->elf.root.root.string[0] != '.'
6266 || fh->elf.root.root.string[1] == '\0')
6267 return TRUE;
6269 /* Find the corresponding function descriptor symbol. Create it
6270 as undefined if necessary. */
6272 fdh = lookup_fdh (fh, htab);
6273 if (fdh == NULL
6274 && !info->executable
6275 && (fh->elf.root.type == bfd_link_hash_undefined
6276 || fh->elf.root.type == bfd_link_hash_undefweak))
6278 fdh = make_fdh (info, fh);
6279 if (fdh == NULL)
6280 return FALSE;
6283 /* Fake function descriptors are made undefweak. If the function
6284 code symbol is strong undefined, make the fake sym the same.
6285 If the function code symbol is defined, then force the fake
6286 descriptor local; We can't support overriding of symbols in a
6287 shared library on a fake descriptor. */
6289 if (fdh != NULL
6290 && fdh->fake
6291 && fdh->elf.root.type == bfd_link_hash_undefweak)
6293 if (fh->elf.root.type == bfd_link_hash_undefined)
6295 fdh->elf.root.type = bfd_link_hash_undefined;
6296 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6298 else if (fh->elf.root.type == bfd_link_hash_defined
6299 || fh->elf.root.type == bfd_link_hash_defweak)
6301 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6305 if (fdh != NULL
6306 && !fdh->elf.forced_local
6307 && (!info->executable
6308 || fdh->elf.def_dynamic
6309 || fdh->elf.ref_dynamic
6310 || (fdh->elf.root.type == bfd_link_hash_undefweak
6311 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6313 if (fdh->elf.dynindx == -1)
6314 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6315 return FALSE;
6316 fdh->elf.ref_regular |= fh->elf.ref_regular;
6317 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6318 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6319 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6320 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6322 move_plt_plist (fh, fdh);
6323 fdh->elf.needs_plt = 1;
6325 fdh->is_func_descriptor = 1;
6326 fdh->oh = fh;
6327 fh->oh = fdh;
6330 /* Now that the info is on the function descriptor, clear the
6331 function code sym info. Any function code syms for which we
6332 don't have a definition in a regular file, we force local.
6333 This prevents a shared library from exporting syms that have
6334 been imported from another library. Function code syms that
6335 are really in the library we must leave global to prevent the
6336 linker dragging in a definition from a static library. */
6337 force_local = (!fh->elf.def_regular
6338 || fdh == NULL
6339 || !fdh->elf.def_regular
6340 || fdh->elf.forced_local);
6341 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6343 return TRUE;
6346 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6347 this hook to a) provide some gcc support functions, and b) transfer
6348 dynamic linking information gathered so far on function code symbol
6349 entries, to their corresponding function descriptor symbol entries. */
6351 static bfd_boolean
6352 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6353 struct bfd_link_info *info)
6355 struct ppc_link_hash_table *htab;
6356 unsigned int i;
6357 const struct sfpr_def_parms funcs[] =
6359 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6360 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6361 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6362 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6363 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6364 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6365 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6366 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6367 { "._savef", 14, 31, savefpr, savefpr1_tail },
6368 { "._restf", 14, 31, restfpr, restfpr1_tail },
6369 { "_savevr_", 20, 31, savevr, savevr_tail },
6370 { "_restvr_", 20, 31, restvr, restvr_tail }
6373 htab = ppc_hash_table (info);
6374 if (htab == NULL)
6375 return FALSE;
6377 if (htab->sfpr == NULL)
6378 /* We don't have any relocs. */
6379 return TRUE;
6381 /* Provide any missing _save* and _rest* functions. */
6382 htab->sfpr->size = 0;
6383 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6384 if (!sfpr_define (info, &funcs[i]))
6385 return FALSE;
6387 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6389 if (htab->sfpr->size == 0)
6390 htab->sfpr->flags |= SEC_EXCLUDE;
6392 return TRUE;
6395 /* Adjust a symbol defined by a dynamic object and referenced by a
6396 regular object. The current definition is in some section of the
6397 dynamic object, but we're not including those sections. We have to
6398 change the definition to something the rest of the link can
6399 understand. */
6401 static bfd_boolean
6402 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6403 struct elf_link_hash_entry *h)
6405 struct ppc_link_hash_table *htab;
6406 asection *s;
6408 htab = ppc_hash_table (info);
6409 if (htab == NULL)
6410 return FALSE;
6412 /* Deal with function syms. */
6413 if (h->type == STT_FUNC
6414 || h->type == STT_GNU_IFUNC
6415 || h->needs_plt)
6417 /* Clear procedure linkage table information for any symbol that
6418 won't need a .plt entry. */
6419 struct plt_entry *ent;
6420 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6421 if (ent->plt.refcount > 0)
6422 break;
6423 if (ent == NULL
6424 || (h->type != STT_GNU_IFUNC
6425 && (SYMBOL_CALLS_LOCAL (info, h)
6426 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6427 && h->root.type == bfd_link_hash_undefweak))))
6429 h->plt.plist = NULL;
6430 h->needs_plt = 0;
6433 else
6434 h->plt.plist = NULL;
6436 /* If this is a weak symbol, and there is a real definition, the
6437 processor independent code will have arranged for us to see the
6438 real definition first, and we can just use the same value. */
6439 if (h->u.weakdef != NULL)
6441 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6442 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6443 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6444 h->root.u.def.value = h->u.weakdef->root.u.def.value;
6445 if (ELIMINATE_COPY_RELOCS)
6446 h->non_got_ref = h->u.weakdef->non_got_ref;
6447 return TRUE;
6450 /* If we are creating a shared library, we must presume that the
6451 only references to the symbol are via the global offset table.
6452 For such cases we need not do anything here; the relocations will
6453 be handled correctly by relocate_section. */
6454 if (info->shared)
6455 return TRUE;
6457 /* If there are no references to this symbol that do not use the
6458 GOT, we don't need to generate a copy reloc. */
6459 if (!h->non_got_ref)
6460 return TRUE;
6462 /* Don't generate a copy reloc for symbols defined in the executable. */
6463 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6464 return TRUE;
6466 if (ELIMINATE_COPY_RELOCS)
6468 struct ppc_link_hash_entry * eh;
6469 struct ppc_dyn_relocs *p;
6471 eh = (struct ppc_link_hash_entry *) h;
6472 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6474 s = p->sec->output_section;
6475 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6476 break;
6479 /* If we didn't find any dynamic relocs in read-only sections, then
6480 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6481 if (p == NULL)
6483 h->non_got_ref = 0;
6484 return TRUE;
6488 if (h->plt.plist != NULL)
6490 /* We should never get here, but unfortunately there are versions
6491 of gcc out there that improperly (for this ABI) put initialized
6492 function pointers, vtable refs and suchlike in read-only
6493 sections. Allow them to proceed, but warn that this might
6494 break at runtime. */
6495 (*_bfd_error_handler)
6496 (_("copy reloc against `%s' requires lazy plt linking; "
6497 "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
6498 h->root.root.string);
6501 /* This is a reference to a symbol defined by a dynamic object which
6502 is not a function. */
6504 if (h->size == 0)
6506 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
6507 h->root.root.string);
6508 return TRUE;
6511 /* We must allocate the symbol in our .dynbss section, which will
6512 become part of the .bss section of the executable. There will be
6513 an entry for this symbol in the .dynsym section. The dynamic
6514 object will contain position independent code, so all references
6515 from the dynamic object to this symbol will go through the global
6516 offset table. The dynamic linker will use the .dynsym entry to
6517 determine the address it must put in the global offset table, so
6518 both the dynamic object and the regular object will refer to the
6519 same memory location for the variable. */
6521 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6522 to copy the initial value out of the dynamic object and into the
6523 runtime process image. We need to remember the offset into the
6524 .rela.bss section we are going to use. */
6525 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6527 htab->relbss->size += sizeof (Elf64_External_Rela);
6528 h->needs_copy = 1;
6531 s = htab->dynbss;
6533 return _bfd_elf_adjust_dynamic_copy (h, s);
6536 /* If given a function descriptor symbol, hide both the function code
6537 sym and the descriptor. */
6538 static void
6539 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6540 struct elf_link_hash_entry *h,
6541 bfd_boolean force_local)
6543 struct ppc_link_hash_entry *eh;
6544 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6546 eh = (struct ppc_link_hash_entry *) h;
6547 if (eh->is_func_descriptor)
6549 struct ppc_link_hash_entry *fh = eh->oh;
6551 if (fh == NULL)
6553 const char *p, *q;
6554 struct ppc_link_hash_table *htab;
6555 char save;
6557 /* We aren't supposed to use alloca in BFD because on
6558 systems which do not have alloca the version in libiberty
6559 calls xmalloc, which might cause the program to crash
6560 when it runs out of memory. This function doesn't have a
6561 return status, so there's no way to gracefully return an
6562 error. So cheat. We know that string[-1] can be safely
6563 accessed; It's either a string in an ELF string table,
6564 or allocated in an objalloc structure. */
6566 p = eh->elf.root.root.string - 1;
6567 save = *p;
6568 *(char *) p = '.';
6569 htab = ppc_hash_table (info);
6570 if (htab == NULL)
6571 return;
6573 fh = (struct ppc_link_hash_entry *)
6574 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6575 *(char *) p = save;
6577 /* Unfortunately, if it so happens that the string we were
6578 looking for was allocated immediately before this string,
6579 then we overwrote the string terminator. That's the only
6580 reason the lookup should fail. */
6581 if (fh == NULL)
6583 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6584 while (q >= eh->elf.root.root.string && *q == *p)
6585 --q, --p;
6586 if (q < eh->elf.root.root.string && *p == '.')
6587 fh = (struct ppc_link_hash_entry *)
6588 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6590 if (fh != NULL)
6592 eh->oh = fh;
6593 fh->oh = eh;
6596 if (fh != NULL)
6597 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6601 static bfd_boolean
6602 get_sym_h (struct elf_link_hash_entry **hp,
6603 Elf_Internal_Sym **symp,
6604 asection **symsecp,
6605 unsigned char **tls_maskp,
6606 Elf_Internal_Sym **locsymsp,
6607 unsigned long r_symndx,
6608 bfd *ibfd)
6610 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6612 if (r_symndx >= symtab_hdr->sh_info)
6614 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6615 struct elf_link_hash_entry *h;
6617 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6618 h = elf_follow_link (h);
6620 if (hp != NULL)
6621 *hp = h;
6623 if (symp != NULL)
6624 *symp = NULL;
6626 if (symsecp != NULL)
6628 asection *symsec = NULL;
6629 if (h->root.type == bfd_link_hash_defined
6630 || h->root.type == bfd_link_hash_defweak)
6631 symsec = h->root.u.def.section;
6632 *symsecp = symsec;
6635 if (tls_maskp != NULL)
6637 struct ppc_link_hash_entry *eh;
6639 eh = (struct ppc_link_hash_entry *) h;
6640 *tls_maskp = &eh->tls_mask;
6643 else
6645 Elf_Internal_Sym *sym;
6646 Elf_Internal_Sym *locsyms = *locsymsp;
6648 if (locsyms == NULL)
6650 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6651 if (locsyms == NULL)
6652 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6653 symtab_hdr->sh_info,
6654 0, NULL, NULL, NULL);
6655 if (locsyms == NULL)
6656 return FALSE;
6657 *locsymsp = locsyms;
6659 sym = locsyms + r_symndx;
6661 if (hp != NULL)
6662 *hp = NULL;
6664 if (symp != NULL)
6665 *symp = sym;
6667 if (symsecp != NULL)
6668 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6670 if (tls_maskp != NULL)
6672 struct got_entry **lgot_ents;
6673 unsigned char *tls_mask;
6675 tls_mask = NULL;
6676 lgot_ents = elf_local_got_ents (ibfd);
6677 if (lgot_ents != NULL)
6679 struct plt_entry **local_plt = (struct plt_entry **)
6680 (lgot_ents + symtab_hdr->sh_info);
6681 unsigned char *lgot_masks = (unsigned char *)
6682 (local_plt + symtab_hdr->sh_info);
6683 tls_mask = &lgot_masks[r_symndx];
6685 *tls_maskp = tls_mask;
6688 return TRUE;
6691 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6692 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6693 type suitable for optimization, and 1 otherwise. */
6695 static int
6696 get_tls_mask (unsigned char **tls_maskp,
6697 unsigned long *toc_symndx,
6698 bfd_vma *toc_addend,
6699 Elf_Internal_Sym **locsymsp,
6700 const Elf_Internal_Rela *rel,
6701 bfd *ibfd)
6703 unsigned long r_symndx;
6704 int next_r;
6705 struct elf_link_hash_entry *h;
6706 Elf_Internal_Sym *sym;
6707 asection *sec;
6708 bfd_vma off;
6710 r_symndx = ELF64_R_SYM (rel->r_info);
6711 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6712 return 0;
6714 if ((*tls_maskp != NULL && **tls_maskp != 0)
6715 || sec == NULL
6716 || ppc64_elf_section_data (sec) == NULL
6717 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6718 return 1;
6720 /* Look inside a TOC section too. */
6721 if (h != NULL)
6723 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6724 off = h->root.u.def.value;
6726 else
6727 off = sym->st_value;
6728 off += rel->r_addend;
6729 BFD_ASSERT (off % 8 == 0);
6730 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6731 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6732 if (toc_symndx != NULL)
6733 *toc_symndx = r_symndx;
6734 if (toc_addend != NULL)
6735 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6736 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6737 return 0;
6738 if ((h == NULL || is_static_defined (h))
6739 && (next_r == -1 || next_r == -2))
6740 return 1 - next_r;
6741 return 1;
6744 /* Adjust all global syms defined in opd sections. In gcc generated
6745 code for the old ABI, these will already have been done. */
6747 static bfd_boolean
6748 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6750 struct ppc_link_hash_entry *eh;
6751 asection *sym_sec;
6752 struct _opd_sec_data *opd;
6754 if (h->root.type == bfd_link_hash_indirect)
6755 return TRUE;
6757 if (h->root.type == bfd_link_hash_warning)
6758 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6760 if (h->root.type != bfd_link_hash_defined
6761 && h->root.type != bfd_link_hash_defweak)
6762 return TRUE;
6764 eh = (struct ppc_link_hash_entry *) h;
6765 if (eh->adjust_done)
6766 return TRUE;
6768 sym_sec = eh->elf.root.u.def.section;
6769 opd = get_opd_info (sym_sec);
6770 if (opd != NULL && opd->adjust != NULL)
6772 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
6773 if (adjust == -1)
6775 /* This entry has been deleted. */
6776 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6777 if (dsec == NULL)
6779 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6780 if (elf_discarded_section (dsec))
6782 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6783 break;
6786 eh->elf.root.u.def.value = 0;
6787 eh->elf.root.u.def.section = dsec;
6789 else
6790 eh->elf.root.u.def.value += adjust;
6791 eh->adjust_done = 1;
6793 return TRUE;
6796 /* Handles decrementing dynamic reloc counts for the reloc specified by
6797 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6798 have already been determined. */
6800 static bfd_boolean
6801 dec_dynrel_count (bfd_vma r_info,
6802 asection *sec,
6803 struct bfd_link_info *info,
6804 Elf_Internal_Sym **local_syms,
6805 struct elf_link_hash_entry *h,
6806 asection *sym_sec)
6808 enum elf_ppc64_reloc_type r_type;
6809 struct ppc_dyn_relocs *p;
6810 struct ppc_dyn_relocs **pp;
6812 /* Can this reloc be dynamic? This switch, and later tests here
6813 should be kept in sync with the code in check_relocs. */
6814 r_type = ELF64_R_TYPE (r_info);
6815 switch (r_type)
6817 default:
6818 return TRUE;
6820 case R_PPC64_TPREL16:
6821 case R_PPC64_TPREL16_LO:
6822 case R_PPC64_TPREL16_HI:
6823 case R_PPC64_TPREL16_HA:
6824 case R_PPC64_TPREL16_DS:
6825 case R_PPC64_TPREL16_LO_DS:
6826 case R_PPC64_TPREL16_HIGHER:
6827 case R_PPC64_TPREL16_HIGHERA:
6828 case R_PPC64_TPREL16_HIGHEST:
6829 case R_PPC64_TPREL16_HIGHESTA:
6830 if (!info->shared)
6831 return TRUE;
6833 case R_PPC64_TPREL64:
6834 case R_PPC64_DTPMOD64:
6835 case R_PPC64_DTPREL64:
6836 case R_PPC64_ADDR64:
6837 case R_PPC64_REL30:
6838 case R_PPC64_REL32:
6839 case R_PPC64_REL64:
6840 case R_PPC64_ADDR14:
6841 case R_PPC64_ADDR14_BRNTAKEN:
6842 case R_PPC64_ADDR14_BRTAKEN:
6843 case R_PPC64_ADDR16:
6844 case R_PPC64_ADDR16_DS:
6845 case R_PPC64_ADDR16_HA:
6846 case R_PPC64_ADDR16_HI:
6847 case R_PPC64_ADDR16_HIGHER:
6848 case R_PPC64_ADDR16_HIGHERA:
6849 case R_PPC64_ADDR16_HIGHEST:
6850 case R_PPC64_ADDR16_HIGHESTA:
6851 case R_PPC64_ADDR16_LO:
6852 case R_PPC64_ADDR16_LO_DS:
6853 case R_PPC64_ADDR24:
6854 case R_PPC64_ADDR32:
6855 case R_PPC64_UADDR16:
6856 case R_PPC64_UADDR32:
6857 case R_PPC64_UADDR64:
6858 case R_PPC64_TOC:
6859 break;
6862 if (local_syms != NULL)
6864 unsigned long r_symndx;
6865 Elf_Internal_Sym *sym;
6866 bfd *ibfd = sec->owner;
6868 r_symndx = ELF64_R_SYM (r_info);
6869 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6870 return FALSE;
6873 if ((info->shared
6874 && (must_be_dyn_reloc (info, r_type)
6875 || (h != NULL
6876 && (!info->symbolic
6877 || h->root.type == bfd_link_hash_defweak
6878 || !h->def_regular))))
6879 || (ELIMINATE_COPY_RELOCS
6880 && !info->shared
6881 && h != NULL
6882 && (h->root.type == bfd_link_hash_defweak
6883 || !h->def_regular)))
6885 else
6886 return TRUE;
6888 if (h != NULL)
6889 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6890 else
6892 if (sym_sec != NULL)
6894 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6895 pp = (struct ppc_dyn_relocs **) vpp;
6897 else
6899 void *vpp = &elf_section_data (sec)->local_dynrel;
6900 pp = (struct ppc_dyn_relocs **) vpp;
6903 /* elf_gc_sweep may have already removed all dyn relocs associated
6904 with local syms for a given section. Don't report a dynreloc
6905 miscount. */
6906 if (*pp == NULL)
6907 return TRUE;
6910 while ((p = *pp) != NULL)
6912 if (p->sec == sec)
6914 if (!must_be_dyn_reloc (info, r_type))
6915 p->pc_count -= 1;
6916 p->count -= 1;
6917 if (p->count == 0)
6918 *pp = p->next;
6919 return TRUE;
6921 pp = &p->next;
6924 (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
6925 sec->owner, sec);
6926 bfd_set_error (bfd_error_bad_value);
6927 return FALSE;
6930 /* Remove unused Official Procedure Descriptor entries. Currently we
6931 only remove those associated with functions in discarded link-once
6932 sections, or weakly defined functions that have been overridden. It
6933 would be possible to remove many more entries for statically linked
6934 applications. */
6936 bfd_boolean
6937 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
6939 bfd *ibfd;
6940 bfd_boolean some_edited = FALSE;
6941 asection *need_pad = NULL;
6943 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6945 asection *sec;
6946 Elf_Internal_Rela *relstart, *rel, *relend;
6947 Elf_Internal_Shdr *symtab_hdr;
6948 Elf_Internal_Sym *local_syms;
6949 bfd_vma offset;
6950 struct _opd_sec_data *opd;
6951 bfd_boolean need_edit, add_aux_fields;
6952 bfd_size_type cnt_16b = 0;
6954 if (!is_ppc64_elf (ibfd))
6955 continue;
6957 sec = bfd_get_section_by_name (ibfd, ".opd");
6958 if (sec == NULL || sec->size == 0)
6959 continue;
6961 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6962 continue;
6964 if (sec->output_section == bfd_abs_section_ptr)
6965 continue;
6967 /* Look through the section relocs. */
6968 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6969 continue;
6971 local_syms = NULL;
6972 symtab_hdr = &elf_symtab_hdr (ibfd);
6974 /* Read the relocations. */
6975 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6976 info->keep_memory);
6977 if (relstart == NULL)
6978 return FALSE;
6980 /* First run through the relocs to check they are sane, and to
6981 determine whether we need to edit this opd section. */
6982 need_edit = FALSE;
6983 need_pad = sec;
6984 offset = 0;
6985 relend = relstart + sec->reloc_count;
6986 for (rel = relstart; rel < relend; )
6988 enum elf_ppc64_reloc_type r_type;
6989 unsigned long r_symndx;
6990 asection *sym_sec;
6991 struct elf_link_hash_entry *h;
6992 Elf_Internal_Sym *sym;
6994 /* .opd contains a regular array of 16 or 24 byte entries. We're
6995 only interested in the reloc pointing to a function entry
6996 point. */
6997 if (rel->r_offset != offset
6998 || rel + 1 >= relend
6999 || (rel + 1)->r_offset != offset + 8)
7001 /* If someone messes with .opd alignment then after a
7002 "ld -r" we might have padding in the middle of .opd.
7003 Also, there's nothing to prevent someone putting
7004 something silly in .opd with the assembler. No .opd
7005 optimization for them! */
7006 broken_opd:
7007 (*_bfd_error_handler)
7008 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7009 need_edit = FALSE;
7010 break;
7013 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7014 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7016 (*_bfd_error_handler)
7017 (_("%B: unexpected reloc type %u in .opd section"),
7018 ibfd, r_type);
7019 need_edit = FALSE;
7020 break;
7023 r_symndx = ELF64_R_SYM (rel->r_info);
7024 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7025 r_symndx, ibfd))
7026 goto error_ret;
7028 if (sym_sec == NULL || sym_sec->owner == NULL)
7030 const char *sym_name;
7031 if (h != NULL)
7032 sym_name = h->root.root.string;
7033 else
7034 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7035 sym_sec);
7037 (*_bfd_error_handler)
7038 (_("%B: undefined sym `%s' in .opd section"),
7039 ibfd, sym_name);
7040 need_edit = FALSE;
7041 break;
7044 /* opd entries are always for functions defined in the
7045 current input bfd. If the symbol isn't defined in the
7046 input bfd, then we won't be using the function in this
7047 bfd; It must be defined in a linkonce section in another
7048 bfd, or is weak. It's also possible that we are
7049 discarding the function due to a linker script /DISCARD/,
7050 which we test for via the output_section. */
7051 if (sym_sec->owner != ibfd
7052 || sym_sec->output_section == bfd_abs_section_ptr)
7053 need_edit = TRUE;
7055 rel += 2;
7056 if (rel == relend
7057 || (rel + 1 == relend && rel->r_offset == offset + 16))
7059 if (sec->size == offset + 24)
7061 need_pad = NULL;
7062 break;
7064 if (rel == relend && sec->size == offset + 16)
7066 cnt_16b++;
7067 break;
7069 goto broken_opd;
7072 if (rel->r_offset == offset + 24)
7073 offset += 24;
7074 else if (rel->r_offset != offset + 16)
7075 goto broken_opd;
7076 else if (rel + 1 < relend
7077 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7078 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7080 offset += 16;
7081 cnt_16b++;
7083 else if (rel + 2 < relend
7084 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7085 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7087 offset += 24;
7088 rel += 1;
7090 else
7091 goto broken_opd;
7094 add_aux_fields = non_overlapping && cnt_16b > 0;
7096 if (need_edit || add_aux_fields)
7098 Elf_Internal_Rela *write_rel;
7099 Elf_Internal_Shdr *rel_hdr;
7100 bfd_byte *rptr, *wptr;
7101 bfd_byte *new_contents;
7102 bfd_boolean skip;
7103 long opd_ent_size;
7104 bfd_size_type amt;
7106 new_contents = NULL;
7107 amt = sec->size * sizeof (long) / 8;
7108 opd = &ppc64_elf_section_data (sec)->u.opd;
7109 opd->adjust = bfd_zalloc (sec->owner, amt);
7110 if (opd->adjust == NULL)
7111 return FALSE;
7112 ppc64_elf_section_data (sec)->sec_type = sec_opd;
7114 /* This seems a waste of time as input .opd sections are all
7115 zeros as generated by gcc, but I suppose there's no reason
7116 this will always be so. We might start putting something in
7117 the third word of .opd entries. */
7118 if ((sec->flags & SEC_IN_MEMORY) == 0)
7120 bfd_byte *loc;
7121 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7123 if (loc != NULL)
7124 free (loc);
7125 error_ret:
7126 if (local_syms != NULL
7127 && symtab_hdr->contents != (unsigned char *) local_syms)
7128 free (local_syms);
7129 if (elf_section_data (sec)->relocs != relstart)
7130 free (relstart);
7131 return FALSE;
7133 sec->contents = loc;
7134 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7137 elf_section_data (sec)->relocs = relstart;
7139 new_contents = sec->contents;
7140 if (add_aux_fields)
7142 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7143 if (new_contents == NULL)
7144 return FALSE;
7145 need_pad = FALSE;
7147 wptr = new_contents;
7148 rptr = sec->contents;
7150 write_rel = relstart;
7151 skip = FALSE;
7152 offset = 0;
7153 opd_ent_size = 0;
7154 for (rel = relstart; rel < relend; rel++)
7156 unsigned long r_symndx;
7157 asection *sym_sec;
7158 struct elf_link_hash_entry *h;
7159 Elf_Internal_Sym *sym;
7161 r_symndx = ELF64_R_SYM (rel->r_info);
7162 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7163 r_symndx, ibfd))
7164 goto error_ret;
7166 if (rel->r_offset == offset)
7168 struct ppc_link_hash_entry *fdh = NULL;
7170 /* See if the .opd entry is full 24 byte or
7171 16 byte (with fd_aux entry overlapped with next
7172 fd_func). */
7173 opd_ent_size = 24;
7174 if ((rel + 2 == relend && sec->size == offset + 16)
7175 || (rel + 3 < relend
7176 && rel[2].r_offset == offset + 16
7177 && rel[3].r_offset == offset + 24
7178 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7179 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7180 opd_ent_size = 16;
7182 if (h != NULL
7183 && h->root.root.string[0] == '.')
7185 struct ppc_link_hash_table *htab;
7187 htab = ppc_hash_table (info);
7188 if (htab != NULL)
7189 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7190 htab);
7191 if (fdh != NULL
7192 && fdh->elf.root.type != bfd_link_hash_defined
7193 && fdh->elf.root.type != bfd_link_hash_defweak)
7194 fdh = NULL;
7197 skip = (sym_sec->owner != ibfd
7198 || sym_sec->output_section == bfd_abs_section_ptr);
7199 if (skip)
7201 if (fdh != NULL && sym_sec->owner == ibfd)
7203 /* Arrange for the function descriptor sym
7204 to be dropped. */
7205 fdh->elf.root.u.def.value = 0;
7206 fdh->elf.root.u.def.section = sym_sec;
7208 opd->adjust[rel->r_offset / 8] = -1;
7210 else
7212 /* We'll be keeping this opd entry. */
7214 if (fdh != NULL)
7216 /* Redefine the function descriptor symbol to
7217 this location in the opd section. It is
7218 necessary to update the value here rather
7219 than using an array of adjustments as we do
7220 for local symbols, because various places
7221 in the generic ELF code use the value
7222 stored in u.def.value. */
7223 fdh->elf.root.u.def.value = wptr - new_contents;
7224 fdh->adjust_done = 1;
7227 /* Local syms are a bit tricky. We could
7228 tweak them as they can be cached, but
7229 we'd need to look through the local syms
7230 for the function descriptor sym which we
7231 don't have at the moment. So keep an
7232 array of adjustments. */
7233 opd->adjust[rel->r_offset / 8]
7234 = (wptr - new_contents) - (rptr - sec->contents);
7236 if (wptr != rptr)
7237 memcpy (wptr, rptr, opd_ent_size);
7238 wptr += opd_ent_size;
7239 if (add_aux_fields && opd_ent_size == 16)
7241 memset (wptr, '\0', 8);
7242 wptr += 8;
7245 rptr += opd_ent_size;
7246 offset += opd_ent_size;
7249 if (skip)
7251 if (!NO_OPD_RELOCS
7252 && !info->relocatable
7253 && !dec_dynrel_count (rel->r_info, sec, info,
7254 NULL, h, sym_sec))
7255 goto error_ret;
7257 else
7259 /* We need to adjust any reloc offsets to point to the
7260 new opd entries. While we're at it, we may as well
7261 remove redundant relocs. */
7262 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
7263 if (write_rel != rel)
7264 memcpy (write_rel, rel, sizeof (*rel));
7265 ++write_rel;
7269 sec->size = wptr - new_contents;
7270 sec->reloc_count = write_rel - relstart;
7271 if (add_aux_fields)
7273 free (sec->contents);
7274 sec->contents = new_contents;
7277 /* Fudge the header size too, as this is used later in
7278 elf_bfd_final_link if we are emitting relocs. */
7279 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7280 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7281 some_edited = TRUE;
7283 else if (elf_section_data (sec)->relocs != relstart)
7284 free (relstart);
7286 if (local_syms != NULL
7287 && symtab_hdr->contents != (unsigned char *) local_syms)
7289 if (!info->keep_memory)
7290 free (local_syms);
7291 else
7292 symtab_hdr->contents = (unsigned char *) local_syms;
7296 if (some_edited)
7297 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7299 /* If we are doing a final link and the last .opd entry is just 16 byte
7300 long, add a 8 byte padding after it. */
7301 if (need_pad != NULL && !info->relocatable)
7303 bfd_byte *p;
7305 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7307 BFD_ASSERT (need_pad->size > 0);
7309 p = bfd_malloc (need_pad->size + 8);
7310 if (p == NULL)
7311 return FALSE;
7313 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7314 p, 0, need_pad->size))
7315 return FALSE;
7317 need_pad->contents = p;
7318 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7320 else
7322 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7323 if (p == NULL)
7324 return FALSE;
7326 need_pad->contents = p;
7329 memset (need_pad->contents + need_pad->size, 0, 8);
7330 need_pad->size += 8;
7333 return TRUE;
7336 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7338 asection *
7339 ppc64_elf_tls_setup (struct bfd_link_info *info,
7340 int no_tls_get_addr_opt,
7341 int *no_multi_toc)
7343 struct ppc_link_hash_table *htab;
7345 htab = ppc_hash_table (info);
7346 if (htab == NULL)
7347 return NULL;
7349 if (*no_multi_toc)
7350 htab->do_multi_toc = 0;
7351 else if (!htab->do_multi_toc)
7352 *no_multi_toc = 1;
7354 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7355 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7356 FALSE, FALSE, TRUE));
7357 /* Move dynamic linking info to the function descriptor sym. */
7358 if (htab->tls_get_addr != NULL)
7359 func_desc_adjust (&htab->tls_get_addr->elf, info);
7360 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7361 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7362 FALSE, FALSE, TRUE));
7363 if (!no_tls_get_addr_opt)
7365 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7367 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7368 FALSE, FALSE, TRUE);
7369 if (opt != NULL)
7370 func_desc_adjust (opt, info);
7371 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7372 FALSE, FALSE, TRUE);
7373 if (opt_fd != NULL
7374 && (opt_fd->root.type == bfd_link_hash_defined
7375 || opt_fd->root.type == bfd_link_hash_defweak))
7377 /* If glibc supports an optimized __tls_get_addr call stub,
7378 signalled by the presence of __tls_get_addr_opt, and we'll
7379 be calling __tls_get_addr via a plt call stub, then
7380 make __tls_get_addr point to __tls_get_addr_opt. */
7381 tga_fd = &htab->tls_get_addr_fd->elf;
7382 if (htab->elf.dynamic_sections_created
7383 && tga_fd != NULL
7384 && (tga_fd->type == STT_FUNC
7385 || tga_fd->needs_plt)
7386 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7387 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7388 && tga_fd->root.type == bfd_link_hash_undefweak)))
7390 struct plt_entry *ent;
7392 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7393 if (ent->plt.refcount > 0)
7394 break;
7395 if (ent != NULL)
7397 tga_fd->root.type = bfd_link_hash_indirect;
7398 tga_fd->root.u.i.link = &opt_fd->root;
7399 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7400 if (opt_fd->dynindx != -1)
7402 /* Use __tls_get_addr_opt in dynamic relocations. */
7403 opt_fd->dynindx = -1;
7404 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7405 opt_fd->dynstr_index);
7406 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7407 return NULL;
7409 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7410 tga = &htab->tls_get_addr->elf;
7411 if (opt != NULL && tga != NULL)
7413 tga->root.type = bfd_link_hash_indirect;
7414 tga->root.u.i.link = &opt->root;
7415 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7416 _bfd_elf_link_hash_hide_symbol (info, opt,
7417 tga->forced_local);
7418 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7420 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7421 htab->tls_get_addr_fd->is_func_descriptor = 1;
7422 if (htab->tls_get_addr != NULL)
7424 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7425 htab->tls_get_addr->is_func = 1;
7430 else
7431 no_tls_get_addr_opt = TRUE;
7433 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
7434 return _bfd_elf_tls_setup (info->output_bfd, info);
7437 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7438 HASH1 or HASH2. */
7440 static bfd_boolean
7441 branch_reloc_hash_match (const bfd *ibfd,
7442 const Elf_Internal_Rela *rel,
7443 const struct ppc_link_hash_entry *hash1,
7444 const struct ppc_link_hash_entry *hash2)
7446 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7447 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7448 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7450 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7452 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7453 struct elf_link_hash_entry *h;
7455 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7456 h = elf_follow_link (h);
7457 if (h == &hash1->elf || h == &hash2->elf)
7458 return TRUE;
7460 return FALSE;
7463 /* Run through all the TLS relocs looking for optimization
7464 opportunities. The linker has been hacked (see ppc64elf.em) to do
7465 a preliminary section layout so that we know the TLS segment
7466 offsets. We can't optimize earlier because some optimizations need
7467 to know the tp offset, and we need to optimize before allocating
7468 dynamic relocations. */
7470 bfd_boolean
7471 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7473 bfd *ibfd;
7474 asection *sec;
7475 struct ppc_link_hash_table *htab;
7476 int pass;
7478 if (info->relocatable || !info->executable)
7479 return TRUE;
7481 htab = ppc_hash_table (info);
7482 if (htab == NULL)
7483 return FALSE;
7485 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7487 Elf_Internal_Sym *locsyms = NULL;
7488 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7489 unsigned char *toc_ref = NULL;
7491 /* Look at all the sections for this file. Make two passes over
7492 the relocs. On the first pass, mark toc entries involved
7493 with tls relocs, and check that tls relocs involved in
7494 setting up a tls_get_addr call are indeed followed by such a
7495 call. If they are not, exclude them from the optimizations
7496 done on the second pass. */
7497 for (pass = 0; pass < 2; ++pass)
7498 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7499 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7501 Elf_Internal_Rela *relstart, *rel, *relend;
7503 /* Read the relocations. */
7504 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7505 info->keep_memory);
7506 if (relstart == NULL)
7507 return FALSE;
7509 relend = relstart + sec->reloc_count;
7510 for (rel = relstart; rel < relend; rel++)
7512 enum elf_ppc64_reloc_type r_type;
7513 unsigned long r_symndx;
7514 struct elf_link_hash_entry *h;
7515 Elf_Internal_Sym *sym;
7516 asection *sym_sec;
7517 unsigned char *tls_mask;
7518 unsigned char tls_set, tls_clear, tls_type = 0;
7519 bfd_vma value;
7520 bfd_boolean ok_tprel, is_local;
7521 long toc_ref_index = 0;
7522 int expecting_tls_get_addr = 0;
7524 r_symndx = ELF64_R_SYM (rel->r_info);
7525 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7526 r_symndx, ibfd))
7528 err_free_rel:
7529 if (elf_section_data (sec)->relocs != relstart)
7530 free (relstart);
7531 if (toc_ref != NULL)
7532 free (toc_ref);
7533 if (locsyms != NULL
7534 && (elf_symtab_hdr (ibfd).contents
7535 != (unsigned char *) locsyms))
7536 free (locsyms);
7537 return FALSE;
7540 if (h != NULL)
7542 if (h->root.type == bfd_link_hash_defined
7543 || h->root.type == bfd_link_hash_defweak)
7544 value = h->root.u.def.value;
7545 else if (h->root.type == bfd_link_hash_undefweak)
7546 value = 0;
7547 else
7548 continue;
7550 else
7551 /* Symbols referenced by TLS relocs must be of type
7552 STT_TLS. So no need for .opd local sym adjust. */
7553 value = sym->st_value;
7555 ok_tprel = FALSE;
7556 is_local = FALSE;
7557 if (h == NULL
7558 || !h->def_dynamic)
7560 is_local = TRUE;
7561 if (h != NULL
7562 && h->root.type == bfd_link_hash_undefweak)
7563 ok_tprel = TRUE;
7564 else
7566 value += sym_sec->output_offset;
7567 value += sym_sec->output_section->vma;
7568 value -= htab->elf.tls_sec->vma;
7569 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7570 < (bfd_vma) 1 << 32);
7574 r_type = ELF64_R_TYPE (rel->r_info);
7575 switch (r_type)
7577 case R_PPC64_GOT_TLSLD16:
7578 case R_PPC64_GOT_TLSLD16_LO:
7579 expecting_tls_get_addr = 1;
7580 /* Fall thru */
7582 case R_PPC64_GOT_TLSLD16_HI:
7583 case R_PPC64_GOT_TLSLD16_HA:
7584 /* These relocs should never be against a symbol
7585 defined in a shared lib. Leave them alone if
7586 that turns out to be the case. */
7587 if (!is_local)
7588 continue;
7590 /* LD -> LE */
7591 tls_set = 0;
7592 tls_clear = TLS_LD;
7593 tls_type = TLS_TLS | TLS_LD;
7594 break;
7596 case R_PPC64_GOT_TLSGD16:
7597 case R_PPC64_GOT_TLSGD16_LO:
7598 expecting_tls_get_addr = 1;
7599 /* Fall thru */
7601 case R_PPC64_GOT_TLSGD16_HI:
7602 case R_PPC64_GOT_TLSGD16_HA:
7603 if (ok_tprel)
7604 /* GD -> LE */
7605 tls_set = 0;
7606 else
7607 /* GD -> IE */
7608 tls_set = TLS_TLS | TLS_TPRELGD;
7609 tls_clear = TLS_GD;
7610 tls_type = TLS_TLS | TLS_GD;
7611 break;
7613 case R_PPC64_GOT_TPREL16_DS:
7614 case R_PPC64_GOT_TPREL16_LO_DS:
7615 case R_PPC64_GOT_TPREL16_HI:
7616 case R_PPC64_GOT_TPREL16_HA:
7617 if (ok_tprel)
7619 /* IE -> LE */
7620 tls_set = 0;
7621 tls_clear = TLS_TPREL;
7622 tls_type = TLS_TLS | TLS_TPREL;
7623 break;
7625 continue;
7627 case R_PPC64_TOC16:
7628 case R_PPC64_TOC16_LO:
7629 case R_PPC64_TLS:
7630 case R_PPC64_TLSGD:
7631 case R_PPC64_TLSLD:
7632 if (sym_sec == NULL || sym_sec != toc)
7633 continue;
7635 /* Mark this toc entry as referenced by a TLS
7636 code sequence. We can do that now in the
7637 case of R_PPC64_TLS, and after checking for
7638 tls_get_addr for the TOC16 relocs. */
7639 if (toc_ref == NULL)
7641 toc_ref = bfd_zmalloc (toc->size / 8);
7642 if (toc_ref == NULL)
7643 goto err_free_rel;
7645 if (h != NULL)
7646 value = h->root.u.def.value;
7647 else
7648 value = sym->st_value;
7649 value += rel->r_addend;
7650 BFD_ASSERT (value < toc->size && value % 8 == 0);
7651 toc_ref_index = value / 8;
7652 if (r_type == R_PPC64_TLS
7653 || r_type == R_PPC64_TLSGD
7654 || r_type == R_PPC64_TLSLD)
7656 toc_ref[toc_ref_index] = 1;
7657 continue;
7660 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7661 continue;
7663 tls_set = 0;
7664 tls_clear = 0;
7665 expecting_tls_get_addr = 2;
7666 break;
7668 case R_PPC64_TPREL64:
7669 if (pass == 0
7670 || sec != toc
7671 || toc_ref == NULL
7672 || !toc_ref[rel->r_offset / 8])
7673 continue;
7674 if (ok_tprel)
7676 /* IE -> LE */
7677 tls_set = TLS_EXPLICIT;
7678 tls_clear = TLS_TPREL;
7679 break;
7681 continue;
7683 case R_PPC64_DTPMOD64:
7684 if (pass == 0
7685 || sec != toc
7686 || toc_ref == NULL
7687 || !toc_ref[rel->r_offset / 8])
7688 continue;
7689 if (rel + 1 < relend
7690 && (rel[1].r_info
7691 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7692 && rel[1].r_offset == rel->r_offset + 8)
7694 if (ok_tprel)
7695 /* GD -> LE */
7696 tls_set = TLS_EXPLICIT | TLS_GD;
7697 else
7698 /* GD -> IE */
7699 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7700 tls_clear = TLS_GD;
7702 else
7704 if (!is_local)
7705 continue;
7707 /* LD -> LE */
7708 tls_set = TLS_EXPLICIT;
7709 tls_clear = TLS_LD;
7711 break;
7713 default:
7714 continue;
7717 if (pass == 0)
7719 if (!expecting_tls_get_addr
7720 || !sec->has_tls_get_addr_call)
7721 continue;
7723 if (rel + 1 < relend
7724 && branch_reloc_hash_match (ibfd, rel + 1,
7725 htab->tls_get_addr,
7726 htab->tls_get_addr_fd))
7728 if (expecting_tls_get_addr == 2)
7730 /* Check for toc tls entries. */
7731 unsigned char *toc_tls;
7732 int retval;
7734 retval = get_tls_mask (&toc_tls, NULL, NULL,
7735 &locsyms,
7736 rel, ibfd);
7737 if (retval == 0)
7738 goto err_free_rel;
7739 if (retval > 1 && toc_tls != NULL)
7740 toc_ref[toc_ref_index] = 1;
7742 continue;
7745 if (expecting_tls_get_addr != 1)
7746 continue;
7748 /* Uh oh, we didn't find the expected call. We
7749 could just mark this symbol to exclude it
7750 from tls optimization but it's safer to skip
7751 the entire section. */
7752 sec->has_tls_reloc = 0;
7753 break;
7756 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
7758 struct plt_entry *ent;
7759 for (ent = htab->tls_get_addr->elf.plt.plist;
7760 ent != NULL;
7761 ent = ent->next)
7762 if (ent->addend == 0)
7764 if (ent->plt.refcount > 0)
7766 ent->plt.refcount -= 1;
7767 expecting_tls_get_addr = 0;
7769 break;
7773 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
7775 struct plt_entry *ent;
7776 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7777 ent != NULL;
7778 ent = ent->next)
7779 if (ent->addend == 0)
7781 if (ent->plt.refcount > 0)
7782 ent->plt.refcount -= 1;
7783 break;
7787 if (tls_clear == 0)
7788 continue;
7790 if ((tls_set & TLS_EXPLICIT) == 0)
7792 struct got_entry *ent;
7794 /* Adjust got entry for this reloc. */
7795 if (h != NULL)
7796 ent = h->got.glist;
7797 else
7798 ent = elf_local_got_ents (ibfd)[r_symndx];
7800 for (; ent != NULL; ent = ent->next)
7801 if (ent->addend == rel->r_addend
7802 && ent->owner == ibfd
7803 && ent->tls_type == tls_type)
7804 break;
7805 if (ent == NULL)
7806 abort ();
7808 if (tls_set == 0)
7810 /* We managed to get rid of a got entry. */
7811 if (ent->got.refcount > 0)
7812 ent->got.refcount -= 1;
7815 else
7817 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7818 we'll lose one or two dyn relocs. */
7819 if (!dec_dynrel_count (rel->r_info, sec, info,
7820 NULL, h, sym_sec))
7821 return FALSE;
7823 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7825 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7826 NULL, h, sym_sec))
7827 return FALSE;
7831 *tls_mask |= tls_set;
7832 *tls_mask &= ~tls_clear;
7835 if (elf_section_data (sec)->relocs != relstart)
7836 free (relstart);
7839 if (toc_ref != NULL)
7840 free (toc_ref);
7842 if (locsyms != NULL
7843 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7845 if (!info->keep_memory)
7846 free (locsyms);
7847 else
7848 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7851 return TRUE;
7854 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7855 the values of any global symbols in a toc section that has been
7856 edited. Globals in toc sections should be a rarity, so this function
7857 sets a flag if any are found in toc sections other than the one just
7858 edited, so that futher hash table traversals can be avoided. */
7860 struct adjust_toc_info
7862 asection *toc;
7863 unsigned long *skip;
7864 bfd_boolean global_toc_syms;
7867 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7869 static bfd_boolean
7870 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7872 struct ppc_link_hash_entry *eh;
7873 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7874 unsigned long i;
7876 if (h->root.type == bfd_link_hash_indirect)
7877 return TRUE;
7879 if (h->root.type == bfd_link_hash_warning)
7880 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7882 if (h->root.type != bfd_link_hash_defined
7883 && h->root.type != bfd_link_hash_defweak)
7884 return TRUE;
7886 eh = (struct ppc_link_hash_entry *) h;
7887 if (eh->adjust_done)
7888 return TRUE;
7890 if (eh->elf.root.u.def.section == toc_inf->toc)
7892 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7893 i = toc_inf->toc->rawsize >> 3;
7894 else
7895 i = eh->elf.root.u.def.value >> 3;
7897 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
7899 (*_bfd_error_handler)
7900 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7902 ++i;
7903 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
7904 eh->elf.root.u.def.value = (bfd_vma) i << 3;
7907 eh->elf.root.u.def.value -= toc_inf->skip[i];
7908 eh->adjust_done = 1;
7910 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7911 toc_inf->global_toc_syms = TRUE;
7913 return TRUE;
7916 /* Examine all relocs referencing .toc sections in order to remove
7917 unused .toc entries. */
7919 bfd_boolean
7920 ppc64_elf_edit_toc (struct bfd_link_info *info)
7922 bfd *ibfd;
7923 struct adjust_toc_info toc_inf;
7924 struct ppc_link_hash_table *htab = ppc_hash_table (info);
7926 htab->do_toc_opt = 1;
7927 toc_inf.global_toc_syms = TRUE;
7928 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7930 asection *toc, *sec;
7931 Elf_Internal_Shdr *symtab_hdr;
7932 Elf_Internal_Sym *local_syms;
7933 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
7934 unsigned long *skip, *drop;
7935 unsigned char *used;
7936 unsigned char *keep, last, some_unused;
7938 if (!is_ppc64_elf (ibfd))
7939 continue;
7941 toc = bfd_get_section_by_name (ibfd, ".toc");
7942 if (toc == NULL
7943 || toc->size == 0
7944 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7945 || elf_discarded_section (toc))
7946 continue;
7948 toc_relocs = NULL;
7949 local_syms = NULL;
7950 symtab_hdr = &elf_symtab_hdr (ibfd);
7952 /* Look at sections dropped from the final link. */
7953 skip = NULL;
7954 relstart = NULL;
7955 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7957 if (sec->reloc_count == 0
7958 || !elf_discarded_section (sec)
7959 || get_opd_info (sec)
7960 || (sec->flags & SEC_ALLOC) == 0
7961 || (sec->flags & SEC_DEBUGGING) != 0)
7962 continue;
7964 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7965 if (relstart == NULL)
7966 goto error_ret;
7968 /* Run through the relocs to see which toc entries might be
7969 unused. */
7970 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7972 enum elf_ppc64_reloc_type r_type;
7973 unsigned long r_symndx;
7974 asection *sym_sec;
7975 struct elf_link_hash_entry *h;
7976 Elf_Internal_Sym *sym;
7977 bfd_vma val;
7979 r_type = ELF64_R_TYPE (rel->r_info);
7980 switch (r_type)
7982 default:
7983 continue;
7985 case R_PPC64_TOC16:
7986 case R_PPC64_TOC16_LO:
7987 case R_PPC64_TOC16_HI:
7988 case R_PPC64_TOC16_HA:
7989 case R_PPC64_TOC16_DS:
7990 case R_PPC64_TOC16_LO_DS:
7991 break;
7994 r_symndx = ELF64_R_SYM (rel->r_info);
7995 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7996 r_symndx, ibfd))
7997 goto error_ret;
7999 if (sym_sec != toc)
8000 continue;
8002 if (h != NULL)
8003 val = h->root.u.def.value;
8004 else
8005 val = sym->st_value;
8006 val += rel->r_addend;
8008 if (val >= toc->size)
8009 continue;
8011 /* Anything in the toc ought to be aligned to 8 bytes.
8012 If not, don't mark as unused. */
8013 if (val & 7)
8014 continue;
8016 if (skip == NULL)
8018 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8019 if (skip == NULL)
8020 goto error_ret;
8023 skip[val >> 3] = ref_from_discarded;
8026 if (elf_section_data (sec)->relocs != relstart)
8027 free (relstart);
8030 /* For largetoc loads of address constants, we can convert
8031 . addis rx,2,addr@got@ha
8032 . ld ry,addr@got@l(rx)
8034 . addis rx,2,addr@toc@ha
8035 . addi ry,rx,addr@toc@l
8036 when addr is within 2G of the toc pointer. This then means
8037 that the word storing "addr" in the toc is no longer needed. */
8039 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8040 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8041 && toc->reloc_count != 0)
8043 /* Read toc relocs. */
8044 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8045 info->keep_memory);
8046 if (toc_relocs == NULL)
8047 goto error_ret;
8049 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8051 enum elf_ppc64_reloc_type r_type;
8052 unsigned long r_symndx;
8053 asection *sym_sec;
8054 struct elf_link_hash_entry *h;
8055 Elf_Internal_Sym *sym;
8056 bfd_vma val, addr;
8058 r_type = ELF64_R_TYPE (rel->r_info);
8059 if (r_type != R_PPC64_ADDR64)
8060 continue;
8062 r_symndx = ELF64_R_SYM (rel->r_info);
8063 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8064 r_symndx, ibfd))
8065 goto error_ret;
8067 if (sym_sec == NULL
8068 || elf_discarded_section (sym_sec))
8069 continue;
8071 if (!SYMBOL_CALLS_LOCAL (info, h))
8072 continue;
8074 if (h != NULL)
8076 if (h->type == STT_GNU_IFUNC)
8077 continue;
8078 val = h->root.u.def.value;
8080 else
8082 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8083 continue;
8084 val = sym->st_value;
8086 val += rel->r_addend;
8087 val += sym_sec->output_section->vma + sym_sec->output_offset;
8089 /* We don't yet know the exact toc pointer value, but we
8090 know it will be somewhere in the toc section. Don't
8091 optimize if the difference from any possible toc
8092 pointer is outside [ff..f80008000, 7fff7fff]. */
8093 addr = toc->output_section->vma + TOC_BASE_OFF;
8094 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8095 continue;
8097 addr = toc->output_section->vma + toc->output_section->rawsize;
8098 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8099 continue;
8101 if (skip == NULL)
8103 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8104 if (skip == NULL)
8105 goto error_ret;
8108 skip[rel->r_offset >> 3]
8109 |= can_optimize | ((rel - toc_relocs) << 2);
8113 if (skip == NULL)
8114 continue;
8116 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8117 if (used == NULL)
8119 error_ret:
8120 if (local_syms != NULL
8121 && symtab_hdr->contents != (unsigned char *) local_syms)
8122 free (local_syms);
8123 if (sec != NULL
8124 && relstart != NULL
8125 && elf_section_data (sec)->relocs != relstart)
8126 free (relstart);
8127 if (toc_relocs != NULL
8128 && elf_section_data (toc)->relocs != toc_relocs)
8129 free (toc_relocs);
8130 if (skip != NULL)
8131 free (skip);
8132 return FALSE;
8135 /* Now check all kept sections that might reference the toc.
8136 Check the toc itself last. */
8137 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8138 : ibfd->sections);
8139 sec != NULL;
8140 sec = (sec == toc ? NULL
8141 : sec->next == NULL ? toc
8142 : sec->next == toc && toc->next ? toc->next
8143 : sec->next))
8145 int repeat;
8147 if (sec->reloc_count == 0
8148 || elf_discarded_section (sec)
8149 || get_opd_info (sec)
8150 || (sec->flags & SEC_ALLOC) == 0
8151 || (sec->flags & SEC_DEBUGGING) != 0)
8152 continue;
8154 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8155 info->keep_memory);
8156 if (relstart == NULL)
8157 goto error_ret;
8159 /* Mark toc entries referenced as used. */
8160 repeat = 0;
8162 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8164 enum elf_ppc64_reloc_type r_type;
8165 unsigned long r_symndx;
8166 asection *sym_sec;
8167 struct elf_link_hash_entry *h;
8168 Elf_Internal_Sym *sym;
8169 bfd_vma val;
8171 r_type = ELF64_R_TYPE (rel->r_info);
8172 switch (r_type)
8174 case R_PPC64_TOC16:
8175 case R_PPC64_TOC16_LO:
8176 case R_PPC64_TOC16_HI:
8177 case R_PPC64_TOC16_HA:
8178 case R_PPC64_TOC16_DS:
8179 case R_PPC64_TOC16_LO_DS:
8180 /* In case we're taking addresses of toc entries. */
8181 case R_PPC64_ADDR64:
8182 break;
8184 default:
8185 continue;
8188 r_symndx = ELF64_R_SYM (rel->r_info);
8189 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8190 r_symndx, ibfd))
8192 free (used);
8193 goto error_ret;
8196 if (sym_sec != toc)
8197 continue;
8199 if (h != NULL)
8200 val = h->root.u.def.value;
8201 else
8202 val = sym->st_value;
8203 val += rel->r_addend;
8205 if (val >= toc->size)
8206 continue;
8208 if ((skip[val >> 3] & can_optimize) != 0)
8210 bfd_vma off;
8211 unsigned char opc;
8213 switch (r_type)
8215 case R_PPC64_TOC16_HA:
8216 break;
8218 case R_PPC64_TOC16_LO_DS:
8219 off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8220 if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
8221 return FALSE;
8222 if ((opc & (0x3f << 2)) == (58u << 2))
8223 break;
8224 /* Fall thru */
8226 default:
8227 /* Wrong sort of reloc, or not a ld. We may
8228 as well clear ref_from_discarded too. */
8229 skip[val >> 3] = 0;
8233 /* For the toc section, we only mark as used if
8234 this entry itself isn't unused. */
8235 if (sec == toc
8236 && !used[val >> 3]
8237 && (used[rel->r_offset >> 3]
8238 || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
8239 /* Do all the relocs again, to catch reference
8240 chains. */
8241 repeat = 1;
8243 used[val >> 3] = 1;
8245 while (repeat);
8247 if (elf_section_data (sec)->relocs != relstart)
8248 free (relstart);
8251 /* Merge the used and skip arrays. Assume that TOC
8252 doublewords not appearing as either used or unused belong
8253 to to an entry more than one doubleword in size. */
8254 for (drop = skip, keep = used, last = 0, some_unused = 0;
8255 drop < skip + (toc->size + 7) / 8;
8256 ++drop, ++keep)
8258 if (*keep)
8260 *drop &= ~ref_from_discarded;
8261 if ((*drop & can_optimize) != 0)
8262 some_unused = 1;
8263 last = 0;
8265 else if (*drop)
8267 some_unused = 1;
8268 last = ref_from_discarded;
8270 else
8271 *drop = last;
8274 free (used);
8276 if (some_unused)
8278 bfd_byte *contents, *src;
8279 unsigned long off;
8280 Elf_Internal_Sym *sym;
8281 bfd_boolean local_toc_syms = FALSE;
8283 /* Shuffle the toc contents, and at the same time convert the
8284 skip array from booleans into offsets. */
8285 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8286 goto error_ret;
8288 elf_section_data (toc)->this_hdr.contents = contents;
8290 for (src = contents, off = 0, drop = skip;
8291 src < contents + toc->size;
8292 src += 8, ++drop)
8294 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8295 off += 8;
8296 else if (off != 0)
8298 *drop = off;
8299 memcpy (src - off, src, 8);
8302 *drop = off;
8303 toc->rawsize = toc->size;
8304 toc->size = src - contents - off;
8306 /* Adjust addends for relocs against the toc section sym,
8307 and optimize any accesses we can. */
8308 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8310 if (sec->reloc_count == 0
8311 || elf_discarded_section (sec))
8312 continue;
8314 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8315 info->keep_memory);
8316 if (relstart == NULL)
8317 goto error_ret;
8319 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8321 enum elf_ppc64_reloc_type r_type;
8322 unsigned long r_symndx;
8323 asection *sym_sec;
8324 struct elf_link_hash_entry *h;
8325 bfd_vma val;
8327 r_type = ELF64_R_TYPE (rel->r_info);
8328 switch (r_type)
8330 default:
8331 continue;
8333 case R_PPC64_TOC16:
8334 case R_PPC64_TOC16_LO:
8335 case R_PPC64_TOC16_HI:
8336 case R_PPC64_TOC16_HA:
8337 case R_PPC64_TOC16_DS:
8338 case R_PPC64_TOC16_LO_DS:
8339 case R_PPC64_ADDR64:
8340 break;
8343 r_symndx = ELF64_R_SYM (rel->r_info);
8344 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8345 r_symndx, ibfd))
8346 goto error_ret;
8348 if (sym_sec != toc)
8349 continue;
8351 if (h != NULL)
8352 val = h->root.u.def.value;
8353 else
8355 val = sym->st_value;
8356 if (val != 0)
8357 local_toc_syms = TRUE;
8360 val += rel->r_addend;
8362 if (val > toc->rawsize)
8363 val = toc->rawsize;
8364 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8365 continue;
8366 else if ((skip[val >> 3] & can_optimize) != 0)
8368 Elf_Internal_Rela *tocrel
8369 = toc_relocs + (skip[val >> 3] >> 2);
8370 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8372 switch (r_type)
8374 case R_PPC64_TOC16_HA:
8375 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8376 break;
8378 case R_PPC64_TOC16_LO_DS:
8379 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8380 break;
8382 default:
8383 abort ();
8385 rel->r_addend = tocrel->r_addend;
8386 elf_section_data (sec)->relocs = relstart;
8387 continue;
8390 if (h != NULL || sym->st_value != 0)
8391 continue;
8393 rel->r_addend -= skip[val >> 3];
8394 elf_section_data (sec)->relocs = relstart;
8397 if (elf_section_data (sec)->relocs != relstart)
8398 free (relstart);
8401 /* We shouldn't have local or global symbols defined in the TOC,
8402 but handle them anyway. */
8403 if (local_syms != NULL)
8404 for (sym = local_syms;
8405 sym < local_syms + symtab_hdr->sh_info;
8406 ++sym)
8407 if (sym->st_value != 0
8408 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8410 unsigned long i;
8412 if (sym->st_value > toc->rawsize)
8413 i = toc->rawsize >> 3;
8414 else
8415 i = sym->st_value >> 3;
8417 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8419 if (local_toc_syms)
8420 (*_bfd_error_handler)
8421 (_("%s defined on removed toc entry"),
8422 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8424 ++i;
8425 while ((skip[i] & (ref_from_discarded | can_optimize)));
8426 sym->st_value = (bfd_vma) i << 3;
8429 sym->st_value -= skip[i];
8430 symtab_hdr->contents = (unsigned char *) local_syms;
8433 /* Adjust any global syms defined in this toc input section. */
8434 if (toc_inf.global_toc_syms)
8436 toc_inf.toc = toc;
8437 toc_inf.skip = skip;
8438 toc_inf.global_toc_syms = FALSE;
8439 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8440 &toc_inf);
8443 if (toc->reloc_count != 0)
8445 Elf_Internal_Shdr *rel_hdr;
8446 Elf_Internal_Rela *wrel;
8447 bfd_size_type sz;
8449 /* Remove unused toc relocs, and adjust those we keep. */
8450 if (toc_relocs == NULL)
8451 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8452 info->keep_memory);
8453 if (toc_relocs == NULL)
8454 goto error_ret;
8456 wrel = toc_relocs;
8457 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8458 if ((skip[rel->r_offset >> 3]
8459 & (ref_from_discarded | can_optimize)) == 0)
8461 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8462 wrel->r_info = rel->r_info;
8463 wrel->r_addend = rel->r_addend;
8464 ++wrel;
8466 else if (!dec_dynrel_count (rel->r_info, toc, info,
8467 &local_syms, NULL, NULL))
8468 goto error_ret;
8470 elf_section_data (toc)->relocs = toc_relocs;
8471 toc->reloc_count = wrel - toc_relocs;
8472 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8473 sz = rel_hdr->sh_entsize;
8474 rel_hdr->sh_size = toc->reloc_count * sz;
8477 else if (toc_relocs != NULL
8478 && elf_section_data (toc)->relocs != toc_relocs)
8479 free (toc_relocs);
8481 if (local_syms != NULL
8482 && symtab_hdr->contents != (unsigned char *) local_syms)
8484 if (!info->keep_memory)
8485 free (local_syms);
8486 else
8487 symtab_hdr->contents = (unsigned char *) local_syms;
8489 free (skip);
8492 return TRUE;
8495 /* Return true iff input section I references the TOC using
8496 instructions limited to +/-32k offsets. */
8498 bfd_boolean
8499 ppc64_elf_has_small_toc_reloc (asection *i)
8501 return (is_ppc64_elf (i->owner)
8502 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8505 /* Allocate space for one GOT entry. */
8507 static void
8508 allocate_got (struct elf_link_hash_entry *h,
8509 struct bfd_link_info *info,
8510 struct got_entry *gent)
8512 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8513 bfd_boolean dyn;
8514 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8515 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8516 ? 16 : 8);
8517 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8518 ? 2 : 1) * sizeof (Elf64_External_Rela);
8519 asection *got = ppc64_elf_tdata (gent->owner)->got;
8521 gent->got.offset = got->size;
8522 got->size += entsize;
8524 dyn = htab->elf.dynamic_sections_created;
8525 if ((info->shared
8526 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8527 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8528 || h->root.type != bfd_link_hash_undefweak))
8530 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8531 relgot->size += rentsize;
8533 else if (h->type == STT_GNU_IFUNC)
8535 asection *relgot = htab->reliplt;
8536 relgot->size += rentsize;
8537 htab->got_reli_size += rentsize;
8541 /* This function merges got entries in the same toc group. */
8543 static void
8544 merge_got_entries (struct got_entry **pent)
8546 struct got_entry *ent, *ent2;
8548 for (ent = *pent; ent != NULL; ent = ent->next)
8549 if (!ent->is_indirect)
8550 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8551 if (!ent2->is_indirect
8552 && ent2->addend == ent->addend
8553 && ent2->tls_type == ent->tls_type
8554 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8556 ent2->is_indirect = TRUE;
8557 ent2->got.ent = ent;
8561 /* Allocate space in .plt, .got and associated reloc sections for
8562 dynamic relocs. */
8564 static bfd_boolean
8565 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8567 struct bfd_link_info *info;
8568 struct ppc_link_hash_table *htab;
8569 asection *s;
8570 struct ppc_link_hash_entry *eh;
8571 struct ppc_dyn_relocs *p;
8572 struct got_entry **pgent, *gent;
8574 if (h->root.type == bfd_link_hash_indirect)
8575 return TRUE;
8577 if (h->root.type == bfd_link_hash_warning)
8578 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8580 info = (struct bfd_link_info *) inf;
8581 htab = ppc_hash_table (info);
8582 if (htab == NULL)
8583 return FALSE;
8585 if ((htab->elf.dynamic_sections_created
8586 && h->dynindx != -1
8587 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8588 || h->type == STT_GNU_IFUNC)
8590 struct plt_entry *pent;
8591 bfd_boolean doneone = FALSE;
8592 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8593 if (pent->plt.refcount > 0)
8595 if (!htab->elf.dynamic_sections_created
8596 || h->dynindx == -1)
8598 s = htab->iplt;
8599 pent->plt.offset = s->size;
8600 s->size += PLT_ENTRY_SIZE;
8601 s = htab->reliplt;
8603 else
8605 /* If this is the first .plt entry, make room for the special
8606 first entry. */
8607 s = htab->plt;
8608 if (s->size == 0)
8609 s->size += PLT_INITIAL_ENTRY_SIZE;
8611 pent->plt.offset = s->size;
8613 /* Make room for this entry. */
8614 s->size += PLT_ENTRY_SIZE;
8616 /* Make room for the .glink code. */
8617 s = htab->glink;
8618 if (s->size == 0)
8619 s->size += GLINK_CALL_STUB_SIZE;
8620 /* We need bigger stubs past index 32767. */
8621 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8622 s->size += 4;
8623 s->size += 2*4;
8625 /* We also need to make an entry in the .rela.plt section. */
8626 s = htab->relplt;
8628 s->size += sizeof (Elf64_External_Rela);
8629 doneone = TRUE;
8631 else
8632 pent->plt.offset = (bfd_vma) -1;
8633 if (!doneone)
8635 h->plt.plist = NULL;
8636 h->needs_plt = 0;
8639 else
8641 h->plt.plist = NULL;
8642 h->needs_plt = 0;
8645 eh = (struct ppc_link_hash_entry *) h;
8646 /* Run through the TLS GD got entries first if we're changing them
8647 to TPREL. */
8648 if ((eh->tls_mask & TLS_TPRELGD) != 0)
8649 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8650 if (gent->got.refcount > 0
8651 && (gent->tls_type & TLS_GD) != 0)
8653 /* This was a GD entry that has been converted to TPREL. If
8654 there happens to be a TPREL entry we can use that one. */
8655 struct got_entry *ent;
8656 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8657 if (ent->got.refcount > 0
8658 && (ent->tls_type & TLS_TPREL) != 0
8659 && ent->addend == gent->addend
8660 && ent->owner == gent->owner)
8662 gent->got.refcount = 0;
8663 break;
8666 /* If not, then we'll be using our own TPREL entry. */
8667 if (gent->got.refcount != 0)
8668 gent->tls_type = TLS_TLS | TLS_TPREL;
8671 /* Remove any list entry that won't generate a word in the GOT before
8672 we call merge_got_entries. Otherwise we risk merging to empty
8673 entries. */
8674 pgent = &h->got.glist;
8675 while ((gent = *pgent) != NULL)
8676 if (gent->got.refcount > 0)
8678 if ((gent->tls_type & TLS_LD) != 0
8679 && !h->def_dynamic)
8681 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8682 *pgent = gent->next;
8684 else
8685 pgent = &gent->next;
8687 else
8688 *pgent = gent->next;
8690 if (!htab->do_multi_toc)
8691 merge_got_entries (&h->got.glist);
8693 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8694 if (!gent->is_indirect)
8696 /* Make sure this symbol is output as a dynamic symbol.
8697 Undefined weak syms won't yet be marked as dynamic,
8698 nor will all TLS symbols. */
8699 if (h->dynindx == -1
8700 && !h->forced_local
8701 && h->type != STT_GNU_IFUNC
8702 && htab->elf.dynamic_sections_created)
8704 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8705 return FALSE;
8708 if (!is_ppc64_elf (gent->owner))
8709 abort ();
8711 allocate_got (h, info, gent);
8714 if (eh->dyn_relocs == NULL
8715 || (!htab->elf.dynamic_sections_created
8716 && h->type != STT_GNU_IFUNC))
8717 return TRUE;
8719 /* In the shared -Bsymbolic case, discard space allocated for
8720 dynamic pc-relative relocs against symbols which turn out to be
8721 defined in regular objects. For the normal shared case, discard
8722 space for relocs that have become local due to symbol visibility
8723 changes. */
8725 if (info->shared)
8727 /* Relocs that use pc_count are those that appear on a call insn,
8728 or certain REL relocs (see must_be_dyn_reloc) that can be
8729 generated via assembly. We want calls to protected symbols to
8730 resolve directly to the function rather than going via the plt.
8731 If people want function pointer comparisons to work as expected
8732 then they should avoid writing weird assembly. */
8733 if (SYMBOL_CALLS_LOCAL (info, h))
8735 struct ppc_dyn_relocs **pp;
8737 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8739 p->count -= p->pc_count;
8740 p->pc_count = 0;
8741 if (p->count == 0)
8742 *pp = p->next;
8743 else
8744 pp = &p->next;
8748 /* Also discard relocs on undefined weak syms with non-default
8749 visibility. */
8750 if (eh->dyn_relocs != NULL
8751 && h->root.type == bfd_link_hash_undefweak)
8753 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8754 eh->dyn_relocs = NULL;
8756 /* Make sure this symbol is output as a dynamic symbol.
8757 Undefined weak syms won't yet be marked as dynamic. */
8758 else if (h->dynindx == -1
8759 && !h->forced_local)
8761 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8762 return FALSE;
8766 else if (h->type == STT_GNU_IFUNC)
8768 if (!h->non_got_ref)
8769 eh->dyn_relocs = NULL;
8771 else if (ELIMINATE_COPY_RELOCS)
8773 /* For the non-shared case, discard space for relocs against
8774 symbols which turn out to need copy relocs or are not
8775 dynamic. */
8777 if (!h->non_got_ref
8778 && !h->def_regular)
8780 /* Make sure this symbol is output as a dynamic symbol.
8781 Undefined weak syms won't yet be marked as dynamic. */
8782 if (h->dynindx == -1
8783 && !h->forced_local)
8785 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8786 return FALSE;
8789 /* If that succeeded, we know we'll be keeping all the
8790 relocs. */
8791 if (h->dynindx != -1)
8792 goto keep;
8795 eh->dyn_relocs = NULL;
8797 keep: ;
8800 /* Finally, allocate space. */
8801 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8803 asection *sreloc = elf_section_data (p->sec)->sreloc;
8804 if (!htab->elf.dynamic_sections_created)
8805 sreloc = htab->reliplt;
8806 sreloc->size += p->count * sizeof (Elf64_External_Rela);
8809 return TRUE;
8812 /* Find any dynamic relocs that apply to read-only sections. */
8814 static bfd_boolean
8815 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8817 struct ppc_link_hash_entry *eh;
8818 struct ppc_dyn_relocs *p;
8820 if (h->root.type == bfd_link_hash_warning)
8821 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8823 eh = (struct ppc_link_hash_entry *) h;
8824 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8826 asection *s = p->sec->output_section;
8828 if (s != NULL && (s->flags & SEC_READONLY) != 0)
8830 struct bfd_link_info *info = inf;
8832 info->flags |= DF_TEXTREL;
8834 /* Not an error, just cut short the traversal. */
8835 return FALSE;
8838 return TRUE;
8841 /* Set the sizes of the dynamic sections. */
8843 static bfd_boolean
8844 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
8845 struct bfd_link_info *info)
8847 struct ppc_link_hash_table *htab;
8848 bfd *dynobj;
8849 asection *s;
8850 bfd_boolean relocs;
8851 bfd *ibfd;
8852 struct got_entry *first_tlsld;
8854 htab = ppc_hash_table (info);
8855 if (htab == NULL)
8856 return FALSE;
8858 dynobj = htab->elf.dynobj;
8859 if (dynobj == NULL)
8860 abort ();
8862 if (htab->elf.dynamic_sections_created)
8864 /* Set the contents of the .interp section to the interpreter. */
8865 if (info->executable)
8867 s = bfd_get_section_by_name (dynobj, ".interp");
8868 if (s == NULL)
8869 abort ();
8870 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
8871 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
8875 /* Set up .got offsets for local syms, and space for local dynamic
8876 relocs. */
8877 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8879 struct got_entry **lgot_ents;
8880 struct got_entry **end_lgot_ents;
8881 struct plt_entry **local_plt;
8882 struct plt_entry **end_local_plt;
8883 unsigned char *lgot_masks;
8884 bfd_size_type locsymcount;
8885 Elf_Internal_Shdr *symtab_hdr;
8886 asection *srel;
8888 if (!is_ppc64_elf (ibfd))
8889 continue;
8891 for (s = ibfd->sections; s != NULL; s = s->next)
8893 struct ppc_dyn_relocs *p;
8895 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
8897 if (!bfd_is_abs_section (p->sec)
8898 && bfd_is_abs_section (p->sec->output_section))
8900 /* Input section has been discarded, either because
8901 it is a copy of a linkonce section or due to
8902 linker script /DISCARD/, so we'll be discarding
8903 the relocs too. */
8905 else if (p->count != 0)
8907 srel = elf_section_data (p->sec)->sreloc;
8908 if (!htab->elf.dynamic_sections_created)
8909 srel = htab->reliplt;
8910 srel->size += p->count * sizeof (Elf64_External_Rela);
8911 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
8912 info->flags |= DF_TEXTREL;
8917 lgot_ents = elf_local_got_ents (ibfd);
8918 if (!lgot_ents)
8919 continue;
8921 symtab_hdr = &elf_symtab_hdr (ibfd);
8922 locsymcount = symtab_hdr->sh_info;
8923 end_lgot_ents = lgot_ents + locsymcount;
8924 local_plt = (struct plt_entry **) end_lgot_ents;
8925 end_local_plt = local_plt + locsymcount;
8926 lgot_masks = (unsigned char *) end_local_plt;
8927 s = ppc64_elf_tdata (ibfd)->got;
8928 srel = ppc64_elf_tdata (ibfd)->relgot;
8929 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
8931 struct got_entry **pent, *ent;
8933 pent = lgot_ents;
8934 while ((ent = *pent) != NULL)
8935 if (ent->got.refcount > 0)
8937 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
8939 ppc64_tlsld_got (ibfd)->got.refcount += 1;
8940 *pent = ent->next;
8942 else
8944 unsigned int num = 1;
8945 ent->got.offset = s->size;
8946 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
8947 num = 2;
8948 s->size += num * 8;
8949 if (info->shared)
8950 srel->size += num * sizeof (Elf64_External_Rela);
8951 else if ((*lgot_masks & PLT_IFUNC) != 0)
8953 htab->reliplt->size
8954 += num * sizeof (Elf64_External_Rela);
8955 htab->got_reli_size
8956 += num * sizeof (Elf64_External_Rela);
8958 pent = &ent->next;
8961 else
8962 *pent = ent->next;
8965 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
8966 for (; local_plt < end_local_plt; ++local_plt)
8968 struct plt_entry *ent;
8970 for (ent = *local_plt; ent != NULL; ent = ent->next)
8971 if (ent->plt.refcount > 0)
8973 s = htab->iplt;
8974 ent->plt.offset = s->size;
8975 s->size += PLT_ENTRY_SIZE;
8977 htab->reliplt->size += sizeof (Elf64_External_Rela);
8979 else
8980 ent->plt.offset = (bfd_vma) -1;
8984 /* Allocate global sym .plt and .got entries, and space for global
8985 sym dynamic relocs. */
8986 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
8988 first_tlsld = NULL;
8989 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8991 struct got_entry *ent;
8993 if (!is_ppc64_elf (ibfd))
8994 continue;
8996 ent = ppc64_tlsld_got (ibfd);
8997 if (ent->got.refcount > 0)
8999 if (!htab->do_multi_toc && first_tlsld != NULL)
9001 ent->is_indirect = TRUE;
9002 ent->got.ent = first_tlsld;
9004 else
9006 if (first_tlsld == NULL)
9007 first_tlsld = ent;
9008 s = ppc64_elf_tdata (ibfd)->got;
9009 ent->got.offset = s->size;
9010 ent->owner = ibfd;
9011 s->size += 16;
9012 if (info->shared)
9014 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9015 srel->size += sizeof (Elf64_External_Rela);
9019 else
9020 ent->got.offset = (bfd_vma) -1;
9023 /* We now have determined the sizes of the various dynamic sections.
9024 Allocate memory for them. */
9025 relocs = FALSE;
9026 for (s = dynobj->sections; s != NULL; s = s->next)
9028 if ((s->flags & SEC_LINKER_CREATED) == 0)
9029 continue;
9031 if (s == htab->brlt || s == htab->relbrlt)
9032 /* These haven't been allocated yet; don't strip. */
9033 continue;
9034 else if (s == htab->got
9035 || s == htab->plt
9036 || s == htab->iplt
9037 || s == htab->glink
9038 || s == htab->dynbss)
9040 /* Strip this section if we don't need it; see the
9041 comment below. */
9043 else if (CONST_STRNEQ (s->name, ".rela"))
9045 if (s->size != 0)
9047 if (s != htab->relplt)
9048 relocs = TRUE;
9050 /* We use the reloc_count field as a counter if we need
9051 to copy relocs into the output file. */
9052 s->reloc_count = 0;
9055 else
9057 /* It's not one of our sections, so don't allocate space. */
9058 continue;
9061 if (s->size == 0)
9063 /* If we don't need this section, strip it from the
9064 output file. This is mostly to handle .rela.bss and
9065 .rela.plt. We must create both sections in
9066 create_dynamic_sections, because they must be created
9067 before the linker maps input sections to output
9068 sections. The linker does that before
9069 adjust_dynamic_symbol is called, and it is that
9070 function which decides whether anything needs to go
9071 into these sections. */
9072 s->flags |= SEC_EXCLUDE;
9073 continue;
9076 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9077 continue;
9079 /* Allocate memory for the section contents. We use bfd_zalloc
9080 here in case unused entries are not reclaimed before the
9081 section's contents are written out. This should not happen,
9082 but this way if it does we get a R_PPC64_NONE reloc in .rela
9083 sections instead of garbage.
9084 We also rely on the section contents being zero when writing
9085 the GOT. */
9086 s->contents = bfd_zalloc (dynobj, s->size);
9087 if (s->contents == NULL)
9088 return FALSE;
9091 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9093 if (!is_ppc64_elf (ibfd))
9094 continue;
9096 s = ppc64_elf_tdata (ibfd)->got;
9097 if (s != NULL && s != htab->got)
9099 if (s->size == 0)
9100 s->flags |= SEC_EXCLUDE;
9101 else
9103 s->contents = bfd_zalloc (ibfd, s->size);
9104 if (s->contents == NULL)
9105 return FALSE;
9108 s = ppc64_elf_tdata (ibfd)->relgot;
9109 if (s != NULL)
9111 if (s->size == 0)
9112 s->flags |= SEC_EXCLUDE;
9113 else
9115 s->contents = bfd_zalloc (ibfd, s->size);
9116 if (s->contents == NULL)
9117 return FALSE;
9118 relocs = TRUE;
9119 s->reloc_count = 0;
9124 if (htab->elf.dynamic_sections_created)
9126 /* Add some entries to the .dynamic section. We fill in the
9127 values later, in ppc64_elf_finish_dynamic_sections, but we
9128 must add the entries now so that we get the correct size for
9129 the .dynamic section. The DT_DEBUG entry is filled in by the
9130 dynamic linker and used by the debugger. */
9131 #define add_dynamic_entry(TAG, VAL) \
9132 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9134 if (info->executable)
9136 if (!add_dynamic_entry (DT_DEBUG, 0))
9137 return FALSE;
9140 if (htab->plt != NULL && htab->plt->size != 0)
9142 if (!add_dynamic_entry (DT_PLTGOT, 0)
9143 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9144 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9145 || !add_dynamic_entry (DT_JMPREL, 0)
9146 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9147 return FALSE;
9150 if (NO_OPD_RELOCS)
9152 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9153 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9154 return FALSE;
9157 if (!htab->no_tls_get_addr_opt
9158 && htab->tls_get_addr_fd != NULL
9159 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9160 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9161 return FALSE;
9163 if (relocs)
9165 if (!add_dynamic_entry (DT_RELA, 0)
9166 || !add_dynamic_entry (DT_RELASZ, 0)
9167 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9168 return FALSE;
9170 /* If any dynamic relocs apply to a read-only section,
9171 then we need a DT_TEXTREL entry. */
9172 if ((info->flags & DF_TEXTREL) == 0)
9173 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
9175 if ((info->flags & DF_TEXTREL) != 0)
9177 if (!add_dynamic_entry (DT_TEXTREL, 0))
9178 return FALSE;
9182 #undef add_dynamic_entry
9184 return TRUE;
9187 /* Determine the type of stub needed, if any, for a call. */
9189 static inline enum ppc_stub_type
9190 ppc_type_of_stub (asection *input_sec,
9191 const Elf_Internal_Rela *rel,
9192 struct ppc_link_hash_entry **hash,
9193 struct plt_entry **plt_ent,
9194 bfd_vma destination)
9196 struct ppc_link_hash_entry *h = *hash;
9197 bfd_vma location;
9198 bfd_vma branch_offset;
9199 bfd_vma max_branch_offset;
9200 enum elf_ppc64_reloc_type r_type;
9202 if (h != NULL)
9204 struct plt_entry *ent;
9205 struct ppc_link_hash_entry *fdh = h;
9206 if (h->oh != NULL
9207 && h->oh->is_func_descriptor)
9209 fdh = ppc_follow_link (h->oh);
9210 *hash = fdh;
9213 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9214 if (ent->addend == rel->r_addend
9215 && ent->plt.offset != (bfd_vma) -1)
9217 *plt_ent = ent;
9218 return ppc_stub_plt_call;
9221 /* Here, we know we don't have a plt entry. If we don't have a
9222 either a defined function descriptor or a defined entry symbol
9223 in a regular object file, then it is pointless trying to make
9224 any other type of stub. */
9225 if (!is_static_defined (&fdh->elf)
9226 && !is_static_defined (&h->elf))
9227 return ppc_stub_none;
9229 else if (elf_local_got_ents (input_sec->owner) != NULL)
9231 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9232 struct plt_entry **local_plt = (struct plt_entry **)
9233 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9234 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9236 if (local_plt[r_symndx] != NULL)
9238 struct plt_entry *ent;
9240 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9241 if (ent->addend == rel->r_addend
9242 && ent->plt.offset != (bfd_vma) -1)
9244 *plt_ent = ent;
9245 return ppc_stub_plt_call;
9250 /* Determine where the call point is. */
9251 location = (input_sec->output_offset
9252 + input_sec->output_section->vma
9253 + rel->r_offset);
9255 branch_offset = destination - location;
9256 r_type = ELF64_R_TYPE (rel->r_info);
9258 /* Determine if a long branch stub is needed. */
9259 max_branch_offset = 1 << 25;
9260 if (r_type != R_PPC64_REL24)
9261 max_branch_offset = 1 << 15;
9263 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9264 /* We need a stub. Figure out whether a long_branch or plt_branch
9265 is needed later. */
9266 return ppc_stub_long_branch;
9268 return ppc_stub_none;
9271 /* Build a .plt call stub. */
9273 static inline bfd_byte *
9274 build_plt_stub (bfd *obfd, bfd_byte *p, int offset, Elf_Internal_Rela *r)
9276 #define PPC_LO(v) ((v) & 0xffff)
9277 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9278 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9280 if (PPC_HA (offset) != 0)
9282 if (r != NULL)
9284 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9285 r[1].r_offset = r[0].r_offset + 8;
9286 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9287 r[1].r_addend = r[0].r_addend;
9288 if (PPC_HA (offset + 16) != PPC_HA (offset))
9290 r[2].r_offset = r[1].r_offset + 4;
9291 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9292 r[2].r_addend = r[0].r_addend;
9294 else
9296 r[2].r_offset = r[1].r_offset + 8;
9297 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9298 r[2].r_addend = r[0].r_addend + 8;
9299 r[3].r_offset = r[2].r_offset + 4;
9300 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9301 r[3].r_addend = r[0].r_addend + 16;
9304 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9305 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9306 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
9307 if (PPC_HA (offset + 16) != PPC_HA (offset))
9309 bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9310 offset = 0;
9312 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9313 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p), p += 4;
9314 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
9315 bfd_put_32 (obfd, BCTR, p), p += 4;
9317 else
9319 if (r != NULL)
9321 r[0].r_offset += 4;
9322 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9323 if (PPC_HA (offset + 16) != PPC_HA (offset))
9325 r[1].r_offset = r[0].r_offset + 4;
9326 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9327 r[1].r_addend = r[0].r_addend;
9329 else
9331 r[1].r_offset = r[0].r_offset + 8;
9332 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9333 r[1].r_addend = r[0].r_addend + 16;
9334 r[2].r_offset = r[1].r_offset + 4;
9335 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9336 r[2].r_addend = r[0].r_addend + 8;
9339 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9340 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p), p += 4;
9341 if (PPC_HA (offset + 16) != PPC_HA (offset))
9343 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9344 offset = 0;
9346 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9347 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9348 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9349 bfd_put_32 (obfd, BCTR, p), p += 4;
9351 return p;
9354 /* Build a special .plt call stub for __tls_get_addr. */
9356 #define LD_R11_0R3 0xe9630000
9357 #define LD_R12_0R3 0xe9830000
9358 #define MR_R0_R3 0x7c601b78
9359 #define CMPDI_R11_0 0x2c2b0000
9360 #define ADD_R3_R12_R13 0x7c6c6a14
9361 #define BEQLR 0x4d820020
9362 #define MR_R3_R0 0x7c030378
9363 #define MFLR_R11 0x7d6802a6
9364 #define STD_R11_0R1 0xf9610000
9365 #define BCTRL 0x4e800421
9366 #define LD_R11_0R1 0xe9610000
9367 #define LD_R2_0R1 0xe8410000
9368 #define MTLR_R11 0x7d6803a6
9370 static inline bfd_byte *
9371 build_tls_get_addr_stub (bfd *obfd, bfd_byte *p, int offset,
9372 Elf_Internal_Rela *r)
9374 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9375 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9376 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9377 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9378 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9379 bfd_put_32 (obfd, BEQLR, p), p += 4;
9380 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9381 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9382 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
9384 if (r != NULL)
9385 r[0].r_offset += 9 * 4;
9386 p = build_plt_stub (obfd, p, offset, r);
9387 bfd_put_32 (obfd, BCTRL, p - 4);
9389 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
9390 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
9391 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9392 bfd_put_32 (obfd, BLR, p), p += 4;
9394 return p;
9397 static Elf_Internal_Rela *
9398 get_relocs (asection *sec, int count)
9400 Elf_Internal_Rela *relocs;
9401 struct bfd_elf_section_data *elfsec_data;
9403 elfsec_data = elf_section_data (sec);
9404 relocs = elfsec_data->relocs;
9405 if (relocs == NULL)
9407 bfd_size_type relsize;
9408 relsize = sec->reloc_count * sizeof (*relocs);
9409 relocs = bfd_alloc (sec->owner, relsize);
9410 if (relocs == NULL)
9411 return NULL;
9412 elfsec_data->relocs = relocs;
9413 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9414 sizeof (Elf_Internal_Shdr));
9415 if (elfsec_data->rela.hdr == NULL)
9416 return NULL;
9417 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9418 * sizeof (Elf64_External_Rela));
9419 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
9420 sec->reloc_count = 0;
9422 relocs += sec->reloc_count;
9423 sec->reloc_count += count;
9424 return relocs;
9427 static bfd_vma
9428 get_r2off (struct ppc_link_hash_table *htab,
9429 struct ppc_stub_hash_entry *stub_entry)
9431 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
9433 if (r2off == 0)
9435 /* Support linking -R objects. Get the toc pointer from the
9436 opd entry. */
9437 char buf[8];
9438 asection *opd = stub_entry->h->elf.root.u.def.section;
9439 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
9441 if (strcmp (opd->name, ".opd") != 0
9442 || opd->reloc_count != 0)
9444 (*_bfd_error_handler) (_("cannot find opd entry toc for %s"),
9445 stub_entry->h->elf.root.root.string);
9446 bfd_set_error (bfd_error_bad_value);
9447 return 0;
9449 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
9450 return 0;
9451 r2off = bfd_get_64 (opd->owner, buf);
9452 r2off -= elf_gp (stub_entry->id_sec->output_section->owner);
9454 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
9455 return r2off;
9458 static bfd_boolean
9459 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9461 struct ppc_stub_hash_entry *stub_entry;
9462 struct ppc_branch_hash_entry *br_entry;
9463 struct bfd_link_info *info;
9464 struct ppc_link_hash_table *htab;
9465 bfd_byte *loc;
9466 bfd_byte *p;
9467 bfd_vma dest, off;
9468 int size;
9469 Elf_Internal_Rela *r;
9470 asection *plt;
9472 /* Massage our args to the form they really have. */
9473 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9474 info = in_arg;
9476 htab = ppc_hash_table (info);
9477 if (htab == NULL)
9478 return FALSE;
9480 /* Make a note of the offset within the stubs for this entry. */
9481 stub_entry->stub_offset = stub_entry->stub_sec->size;
9482 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
9484 htab->stub_count[stub_entry->stub_type - 1] += 1;
9485 switch (stub_entry->stub_type)
9487 case ppc_stub_long_branch:
9488 case ppc_stub_long_branch_r2off:
9489 /* Branches are relative. This is where we are going to. */
9490 off = dest = (stub_entry->target_value
9491 + stub_entry->target_section->output_offset
9492 + stub_entry->target_section->output_section->vma);
9494 /* And this is where we are coming from. */
9495 off -= (stub_entry->stub_offset
9496 + stub_entry->stub_sec->output_offset
9497 + stub_entry->stub_sec->output_section->vma);
9499 size = 4;
9500 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9502 bfd_vma r2off = get_r2off (htab, stub_entry);
9504 if (r2off == 0)
9506 htab->stub_error = TRUE;
9507 return FALSE;
9509 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9510 loc += 4;
9511 size = 12;
9512 if (PPC_HA (r2off) != 0)
9514 size = 16;
9515 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9516 loc += 4;
9518 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9519 loc += 4;
9520 off -= size - 4;
9522 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
9524 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9526 (*_bfd_error_handler) (_("long branch stub `%s' offset overflow"),
9527 stub_entry->root.string);
9528 htab->stub_error = TRUE;
9529 return FALSE;
9532 if (info->emitrelocations)
9534 r = get_relocs (stub_entry->stub_sec, 1);
9535 if (r == NULL)
9536 return FALSE;
9537 r->r_offset = loc - stub_entry->stub_sec->contents;
9538 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9539 r->r_addend = dest;
9540 if (stub_entry->h != NULL)
9542 struct elf_link_hash_entry **hashes;
9543 unsigned long symndx;
9544 struct ppc_link_hash_entry *h;
9546 hashes = elf_sym_hashes (htab->stub_bfd);
9547 if (hashes == NULL)
9549 bfd_size_type hsize;
9551 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9552 hashes = bfd_zalloc (htab->stub_bfd, hsize);
9553 if (hashes == NULL)
9554 return FALSE;
9555 elf_sym_hashes (htab->stub_bfd) = hashes;
9556 htab->stub_globals = 1;
9558 symndx = htab->stub_globals++;
9559 h = stub_entry->h;
9560 hashes[symndx] = &h->elf;
9561 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9562 if (h->oh != NULL && h->oh->is_func)
9563 h = ppc_follow_link (h->oh);
9564 if (h->elf.root.u.def.section != stub_entry->target_section)
9565 /* H is an opd symbol. The addend must be zero. */
9566 r->r_addend = 0;
9567 else
9569 off = (h->elf.root.u.def.value
9570 + h->elf.root.u.def.section->output_offset
9571 + h->elf.root.u.def.section->output_section->vma);
9572 r->r_addend -= off;
9576 break;
9578 case ppc_stub_plt_branch:
9579 case ppc_stub_plt_branch_r2off:
9580 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9581 stub_entry->root.string + 9,
9582 FALSE, FALSE);
9583 if (br_entry == NULL)
9585 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
9586 stub_entry->root.string);
9587 htab->stub_error = TRUE;
9588 return FALSE;
9591 dest = (stub_entry->target_value
9592 + stub_entry->target_section->output_offset
9593 + stub_entry->target_section->output_section->vma);
9595 bfd_put_64 (htab->brlt->owner, dest,
9596 htab->brlt->contents + br_entry->offset);
9598 if (br_entry->iter == htab->stub_iteration)
9600 br_entry->iter = 0;
9602 if (htab->relbrlt != NULL)
9604 /* Create a reloc for the branch lookup table entry. */
9605 Elf_Internal_Rela rela;
9606 bfd_byte *rl;
9608 rela.r_offset = (br_entry->offset
9609 + htab->brlt->output_offset
9610 + htab->brlt->output_section->vma);
9611 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9612 rela.r_addend = dest;
9614 rl = htab->relbrlt->contents;
9615 rl += (htab->relbrlt->reloc_count++
9616 * sizeof (Elf64_External_Rela));
9617 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9619 else if (info->emitrelocations)
9621 r = get_relocs (htab->brlt, 1);
9622 if (r == NULL)
9623 return FALSE;
9624 /* brlt, being SEC_LINKER_CREATED does not go through the
9625 normal reloc processing. Symbols and offsets are not
9626 translated from input file to output file form, so
9627 set up the offset per the output file. */
9628 r->r_offset = (br_entry->offset
9629 + htab->brlt->output_offset
9630 + htab->brlt->output_section->vma);
9631 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9632 r->r_addend = dest;
9636 dest = (br_entry->offset
9637 + htab->brlt->output_offset
9638 + htab->brlt->output_section->vma);
9640 off = (dest
9641 - elf_gp (htab->brlt->output_section->owner)
9642 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9644 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
9646 (*_bfd_error_handler)
9647 (_("linkage table error against `%s'"),
9648 stub_entry->root.string);
9649 bfd_set_error (bfd_error_bad_value);
9650 htab->stub_error = TRUE;
9651 return FALSE;
9654 if (info->emitrelocations)
9656 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
9657 if (r == NULL)
9658 return FALSE;
9659 r[0].r_offset = loc - stub_entry->stub_sec->contents;
9660 if (bfd_big_endian (info->output_bfd))
9661 r[0].r_offset += 2;
9662 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
9663 r[0].r_offset += 4;
9664 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9665 r[0].r_addend = dest;
9666 if (PPC_HA (off) != 0)
9668 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9669 r[1].r_offset = r[0].r_offset + 4;
9670 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9671 r[1].r_addend = r[0].r_addend;
9675 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9677 if (PPC_HA (off) != 0)
9679 size = 16;
9680 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
9681 loc += 4;
9682 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
9684 else
9686 size = 12;
9687 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
9690 else
9692 bfd_vma r2off = get_r2off (htab, stub_entry);
9694 if (r2off == 0)
9696 htab->stub_error = TRUE;
9697 return FALSE;
9700 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9701 loc += 4;
9702 size = 20;
9703 if (PPC_HA (off) != 0)
9705 size += 4;
9706 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
9707 loc += 4;
9708 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
9709 loc += 4;
9711 else
9713 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
9714 loc += 4;
9717 if (PPC_HA (r2off) != 0)
9719 size += 4;
9720 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9721 loc += 4;
9723 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9725 loc += 4;
9726 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
9727 loc += 4;
9728 bfd_put_32 (htab->stub_bfd, BCTR, loc);
9729 break;
9731 case ppc_stub_plt_call:
9732 if (stub_entry->h != NULL
9733 && stub_entry->h->is_func_descriptor
9734 && stub_entry->h->oh != NULL)
9736 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
9738 /* If the old-ABI "dot-symbol" is undefined make it weak so
9739 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
9740 FIXME: We used to define the symbol on one of the call
9741 stubs instead, which is why we test symbol section id
9742 against htab->top_id in various places. Likely all
9743 these checks could now disappear. */
9744 if (fh->elf.root.type == bfd_link_hash_undefined)
9745 fh->elf.root.type = bfd_link_hash_undefweak;
9748 /* Now build the stub. */
9749 dest = stub_entry->plt_ent->plt.offset & ~1;
9750 if (dest >= (bfd_vma) -2)
9751 abort ();
9753 plt = htab->plt;
9754 if (!htab->elf.dynamic_sections_created
9755 || stub_entry->h == NULL
9756 || stub_entry->h->elf.dynindx == -1)
9757 plt = htab->iplt;
9759 dest += plt->output_offset + plt->output_section->vma;
9761 if (stub_entry->h == NULL
9762 && (stub_entry->plt_ent->plt.offset & 1) == 0)
9764 Elf_Internal_Rela rela;
9765 bfd_byte *rl;
9767 rela.r_offset = dest;
9768 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
9769 rela.r_addend = (stub_entry->target_value
9770 + stub_entry->target_section->output_offset
9771 + stub_entry->target_section->output_section->vma);
9773 rl = (htab->reliplt->contents
9774 + (htab->reliplt->reloc_count++
9775 * sizeof (Elf64_External_Rela)));
9776 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
9777 stub_entry->plt_ent->plt.offset |= 1;
9780 off = (dest
9781 - elf_gp (plt->output_section->owner)
9782 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9784 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
9786 (*_bfd_error_handler)
9787 (_("linkage table error against `%s'"),
9788 stub_entry->h != NULL
9789 ? stub_entry->h->elf.root.root.string
9790 : "<local sym>");
9791 bfd_set_error (bfd_error_bad_value);
9792 htab->stub_error = TRUE;
9793 return FALSE;
9796 r = NULL;
9797 if (info->emitrelocations)
9799 r = get_relocs (stub_entry->stub_sec,
9800 (2 + (PPC_HA (off) != 0)
9801 + (PPC_HA (off + 16) == PPC_HA (off))));
9802 if (r == NULL)
9803 return FALSE;
9804 r[0].r_offset = loc - stub_entry->stub_sec->contents;
9805 if (bfd_big_endian (info->output_bfd))
9806 r[0].r_offset += 2;
9807 r[0].r_addend = dest;
9809 if (stub_entry->h != NULL
9810 && (stub_entry->h == htab->tls_get_addr_fd
9811 || stub_entry->h == htab->tls_get_addr)
9812 && !htab->no_tls_get_addr_opt)
9813 p = build_tls_get_addr_stub (htab->stub_bfd, loc, off, r);
9814 else
9815 p = build_plt_stub (htab->stub_bfd, loc, off, r);
9816 size = p - loc;
9817 break;
9819 default:
9820 BFD_FAIL ();
9821 return FALSE;
9824 stub_entry->stub_sec->size += size;
9826 if (htab->emit_stub_syms)
9828 struct elf_link_hash_entry *h;
9829 size_t len1, len2;
9830 char *name;
9831 const char *const stub_str[] = { "long_branch",
9832 "long_branch_r2off",
9833 "plt_branch",
9834 "plt_branch_r2off",
9835 "plt_call" };
9837 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
9838 len2 = strlen (stub_entry->root.string);
9839 name = bfd_malloc (len1 + len2 + 2);
9840 if (name == NULL)
9841 return FALSE;
9842 memcpy (name, stub_entry->root.string, 9);
9843 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
9844 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
9845 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
9846 if (h == NULL)
9847 return FALSE;
9848 if (h->root.type == bfd_link_hash_new)
9850 h->root.type = bfd_link_hash_defined;
9851 h->root.u.def.section = stub_entry->stub_sec;
9852 h->root.u.def.value = stub_entry->stub_offset;
9853 h->ref_regular = 1;
9854 h->def_regular = 1;
9855 h->ref_regular_nonweak = 1;
9856 h->forced_local = 1;
9857 h->non_elf = 0;
9861 return TRUE;
9864 /* As above, but don't actually build the stub. Just bump offset so
9865 we know stub section sizes, and select plt_branch stubs where
9866 long_branch stubs won't do. */
9868 static bfd_boolean
9869 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9871 struct ppc_stub_hash_entry *stub_entry;
9872 struct bfd_link_info *info;
9873 struct ppc_link_hash_table *htab;
9874 bfd_vma off;
9875 int size;
9877 /* Massage our args to the form they really have. */
9878 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9879 info = in_arg;
9881 htab = ppc_hash_table (info);
9882 if (htab == NULL)
9883 return FALSE;
9885 if (stub_entry->stub_type == ppc_stub_plt_call)
9887 asection *plt;
9888 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
9889 if (off >= (bfd_vma) -2)
9890 abort ();
9891 plt = htab->plt;
9892 if (!htab->elf.dynamic_sections_created
9893 || stub_entry->h == NULL
9894 || stub_entry->h->elf.dynindx == -1)
9895 plt = htab->iplt;
9896 off += (plt->output_offset
9897 + plt->output_section->vma
9898 - elf_gp (plt->output_section->owner)
9899 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9901 size = PLT_CALL_STUB_SIZE;
9902 if (PPC_HA (off) == 0)
9903 size -= 4;
9904 if (PPC_HA (off + 16) != PPC_HA (off))
9905 size += 4;
9906 if (stub_entry->h != NULL
9907 && (stub_entry->h == htab->tls_get_addr_fd
9908 || stub_entry->h == htab->tls_get_addr)
9909 && !htab->no_tls_get_addr_opt)
9910 size += 13 * 4;
9911 if (info->emitrelocations)
9913 stub_entry->stub_sec->reloc_count
9914 += 2 + (PPC_HA (off) != 0) + (PPC_HA (off + 16) == PPC_HA (off));
9915 stub_entry->stub_sec->flags |= SEC_RELOC;
9918 else
9920 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
9921 variants. */
9922 bfd_vma r2off = 0;
9924 off = (stub_entry->target_value
9925 + stub_entry->target_section->output_offset
9926 + stub_entry->target_section->output_section->vma);
9927 off -= (stub_entry->stub_sec->size
9928 + stub_entry->stub_sec->output_offset
9929 + stub_entry->stub_sec->output_section->vma);
9931 /* Reset the stub type from the plt variant in case we now
9932 can reach with a shorter stub. */
9933 if (stub_entry->stub_type >= ppc_stub_plt_branch)
9934 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
9936 size = 4;
9937 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9939 r2off = get_r2off (htab, stub_entry);
9940 if (r2off == 0)
9942 htab->stub_error = TRUE;
9943 return FALSE;
9945 size = 12;
9946 if (PPC_HA (r2off) != 0)
9947 size = 16;
9948 off -= size - 4;
9951 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
9952 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9954 struct ppc_branch_hash_entry *br_entry;
9956 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9957 stub_entry->root.string + 9,
9958 TRUE, FALSE);
9959 if (br_entry == NULL)
9961 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
9962 stub_entry->root.string);
9963 htab->stub_error = TRUE;
9964 return FALSE;
9967 if (br_entry->iter != htab->stub_iteration)
9969 br_entry->iter = htab->stub_iteration;
9970 br_entry->offset = htab->brlt->size;
9971 htab->brlt->size += 8;
9973 if (htab->relbrlt != NULL)
9974 htab->relbrlt->size += sizeof (Elf64_External_Rela);
9975 else if (info->emitrelocations)
9977 htab->brlt->reloc_count += 1;
9978 htab->brlt->flags |= SEC_RELOC;
9982 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
9983 off = (br_entry->offset
9984 + htab->brlt->output_offset
9985 + htab->brlt->output_section->vma
9986 - elf_gp (htab->brlt->output_section->owner)
9987 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9989 if (info->emitrelocations)
9991 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
9992 stub_entry->stub_sec->flags |= SEC_RELOC;
9995 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9997 size = 12;
9998 if (PPC_HA (off) != 0)
9999 size = 16;
10001 else
10003 size = 20;
10004 if (PPC_HA (off) != 0)
10005 size += 4;
10007 if (PPC_HA (r2off) != 0)
10008 size += 4;
10011 else if (info->emitrelocations)
10013 stub_entry->stub_sec->reloc_count += 1;
10014 stub_entry->stub_sec->flags |= SEC_RELOC;
10018 stub_entry->stub_sec->size += size;
10019 return TRUE;
10022 /* Set up various things so that we can make a list of input sections
10023 for each output section included in the link. Returns -1 on error,
10024 0 when no stubs will be needed, and 1 on success. */
10027 ppc64_elf_setup_section_lists
10028 (struct bfd_link_info *info,
10029 asection *(*add_stub_section) (const char *, asection *),
10030 void (*layout_sections_again) (void))
10032 bfd *input_bfd;
10033 int top_id, top_index, id;
10034 asection *section;
10035 asection **input_list;
10036 bfd_size_type amt;
10037 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10039 if (htab == NULL)
10040 return -1;
10041 /* Stash our params away. */
10042 htab->add_stub_section = add_stub_section;
10043 htab->layout_sections_again = layout_sections_again;
10045 if (htab->brlt == NULL)
10046 return 0;
10048 /* Find the top input section id. */
10049 for (input_bfd = info->input_bfds, top_id = 3;
10050 input_bfd != NULL;
10051 input_bfd = input_bfd->link_next)
10053 for (section = input_bfd->sections;
10054 section != NULL;
10055 section = section->next)
10057 if (top_id < section->id)
10058 top_id = section->id;
10062 htab->top_id = top_id;
10063 amt = sizeof (struct map_stub) * (top_id + 1);
10064 htab->stub_group = bfd_zmalloc (amt);
10065 if (htab->stub_group == NULL)
10066 return -1;
10068 /* Set toc_off for com, und, abs and ind sections. */
10069 for (id = 0; id < 3; id++)
10070 htab->stub_group[id].toc_off = TOC_BASE_OFF;
10072 /* We can't use output_bfd->section_count here to find the top output
10073 section index as some sections may have been removed, and
10074 strip_excluded_output_sections doesn't renumber the indices. */
10075 for (section = info->output_bfd->sections, top_index = 0;
10076 section != NULL;
10077 section = section->next)
10079 if (top_index < section->index)
10080 top_index = section->index;
10083 htab->top_index = top_index;
10084 amt = sizeof (asection *) * (top_index + 1);
10085 input_list = bfd_zmalloc (amt);
10086 htab->input_list = input_list;
10087 if (input_list == NULL)
10088 return -1;
10090 return 1;
10093 /* Set up for first pass at multitoc partitioning. */
10095 void
10096 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10098 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10100 elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10101 htab->toc_curr = elf_gp (info->output_bfd);
10102 htab->toc_bfd = NULL;
10103 htab->toc_first_sec = NULL;
10106 /* The linker repeatedly calls this function for each TOC input section
10107 and linker generated GOT section. Group input bfds such that the toc
10108 within a group is less than 64k in size. */
10110 bfd_boolean
10111 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10113 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10114 bfd_vma addr, off, limit;
10116 if (htab == NULL)
10117 return FALSE;
10119 if (!htab->second_toc_pass)
10121 /* Keep track of the first .toc or .got section for this input bfd. */
10122 if (htab->toc_bfd != isec->owner)
10124 htab->toc_bfd = isec->owner;
10125 htab->toc_first_sec = isec;
10128 addr = isec->output_offset + isec->output_section->vma;
10129 off = addr - htab->toc_curr;
10130 limit = 0x80008000;
10131 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10132 limit = 0x10000;
10133 if (off + isec->size > limit)
10135 addr = (htab->toc_first_sec->output_offset
10136 + htab->toc_first_sec->output_section->vma);
10137 htab->toc_curr = addr;
10140 /* toc_curr is the base address of this toc group. Set elf_gp
10141 for the input section to be the offset relative to the
10142 output toc base plus 0x8000. Making the input elf_gp an
10143 offset allows us to move the toc as a whole without
10144 recalculating input elf_gp. */
10145 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10146 off += TOC_BASE_OFF;
10148 /* Die if someone uses a linker script that doesn't keep input
10149 file .toc and .got together. */
10150 if (elf_gp (isec->owner) != 0
10151 && elf_gp (isec->owner) != off)
10152 return FALSE;
10154 elf_gp (isec->owner) = off;
10155 return TRUE;
10158 /* During the second pass toc_first_sec points to the start of
10159 a toc group, and toc_curr is used to track the old elf_gp.
10160 We use toc_bfd to ensure we only look at each bfd once. */
10161 if (htab->toc_bfd == isec->owner)
10162 return TRUE;
10163 htab->toc_bfd = isec->owner;
10165 if (htab->toc_first_sec == NULL
10166 || htab->toc_curr != elf_gp (isec->owner))
10168 htab->toc_curr = elf_gp (isec->owner);
10169 htab->toc_first_sec = isec;
10171 addr = (htab->toc_first_sec->output_offset
10172 + htab->toc_first_sec->output_section->vma);
10173 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10174 elf_gp (isec->owner) = off;
10176 return TRUE;
10179 /* Called via elf_link_hash_traverse to merge GOT entries for global
10180 symbol H. */
10182 static bfd_boolean
10183 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10185 if (h->root.type == bfd_link_hash_indirect)
10186 return TRUE;
10188 if (h->root.type == bfd_link_hash_warning)
10189 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10191 merge_got_entries (&h->got.glist);
10193 return TRUE;
10196 /* Called via elf_link_hash_traverse to allocate GOT entries for global
10197 symbol H. */
10199 static bfd_boolean
10200 reallocate_got (struct elf_link_hash_entry *h, void *inf)
10202 struct got_entry *gent;
10204 if (h->root.type == bfd_link_hash_indirect)
10205 return TRUE;
10207 if (h->root.type == bfd_link_hash_warning)
10208 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10210 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10211 if (!gent->is_indirect)
10212 allocate_got (h, (struct bfd_link_info *) inf, gent);
10213 return TRUE;
10216 /* Called on the first multitoc pass after the last call to
10217 ppc64_elf_next_toc_section. This function removes duplicate GOT
10218 entries. */
10220 bfd_boolean
10221 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
10223 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10224 struct bfd *ibfd, *ibfd2;
10225 bfd_boolean done_something;
10227 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
10229 if (!htab->do_multi_toc)
10230 return FALSE;
10232 /* Merge global sym got entries within a toc group. */
10233 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10235 /* And tlsld_got. */
10236 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10238 struct got_entry *ent, *ent2;
10240 if (!is_ppc64_elf (ibfd))
10241 continue;
10243 ent = ppc64_tlsld_got (ibfd);
10244 if (!ent->is_indirect
10245 && ent->got.offset != (bfd_vma) -1)
10247 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10249 if (!is_ppc64_elf (ibfd2))
10250 continue;
10252 ent2 = ppc64_tlsld_got (ibfd2);
10253 if (!ent2->is_indirect
10254 && ent2->got.offset != (bfd_vma) -1
10255 && elf_gp (ibfd2) == elf_gp (ibfd))
10257 ent2->is_indirect = TRUE;
10258 ent2->got.ent = ent;
10264 /* Zap sizes of got sections. */
10265 htab->reliplt->rawsize = htab->reliplt->size;
10266 htab->reliplt->size -= htab->got_reli_size;
10267 htab->got_reli_size = 0;
10269 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10271 asection *got, *relgot;
10273 if (!is_ppc64_elf (ibfd))
10274 continue;
10276 got = ppc64_elf_tdata (ibfd)->got;
10277 if (got != NULL)
10279 got->rawsize = got->size;
10280 got->size = 0;
10281 relgot = ppc64_elf_tdata (ibfd)->relgot;
10282 relgot->rawsize = relgot->size;
10283 relgot->size = 0;
10287 /* Now reallocate the got, local syms first. We don't need to
10288 allocate section contents again since we never increase size. */
10289 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10291 struct got_entry **lgot_ents;
10292 struct got_entry **end_lgot_ents;
10293 struct plt_entry **local_plt;
10294 struct plt_entry **end_local_plt;
10295 unsigned char *lgot_masks;
10296 bfd_size_type locsymcount;
10297 Elf_Internal_Shdr *symtab_hdr;
10298 asection *s, *srel;
10300 if (!is_ppc64_elf (ibfd))
10301 continue;
10303 lgot_ents = elf_local_got_ents (ibfd);
10304 if (!lgot_ents)
10305 continue;
10307 symtab_hdr = &elf_symtab_hdr (ibfd);
10308 locsymcount = symtab_hdr->sh_info;
10309 end_lgot_ents = lgot_ents + locsymcount;
10310 local_plt = (struct plt_entry **) end_lgot_ents;
10311 end_local_plt = local_plt + locsymcount;
10312 lgot_masks = (unsigned char *) end_local_plt;
10313 s = ppc64_elf_tdata (ibfd)->got;
10314 srel = ppc64_elf_tdata (ibfd)->relgot;
10315 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10317 struct got_entry *ent;
10319 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
10321 unsigned int num = 1;
10322 ent->got.offset = s->size;
10323 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10324 num = 2;
10325 s->size += num * 8;
10326 if (info->shared)
10327 srel->size += num * sizeof (Elf64_External_Rela);
10328 else if ((*lgot_masks & PLT_IFUNC) != 0)
10330 htab->reliplt->size
10331 += num * sizeof (Elf64_External_Rela);
10332 htab->got_reli_size
10333 += num * sizeof (Elf64_External_Rela);
10339 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10341 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10343 struct got_entry *ent;
10345 if (!is_ppc64_elf (ibfd))
10346 continue;
10348 ent = ppc64_tlsld_got (ibfd);
10349 if (!ent->is_indirect
10350 && ent->got.offset != (bfd_vma) -1)
10352 asection *s = ppc64_elf_tdata (ibfd)->got;
10353 ent->got.offset = s->size;
10354 s->size += 16;
10355 if (info->shared)
10357 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10358 srel->size += sizeof (Elf64_External_Rela);
10363 done_something = htab->reliplt->rawsize != htab->reliplt->size;
10364 if (!done_something)
10365 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10367 asection *got;
10369 if (!is_ppc64_elf (ibfd))
10370 continue;
10372 got = ppc64_elf_tdata (ibfd)->got;
10373 if (got != NULL)
10375 done_something = got->rawsize != got->size;
10376 if (done_something)
10377 break;
10381 if (done_something)
10382 (*htab->layout_sections_again) ();
10384 /* Set up for second pass over toc sections to recalculate elf_gp
10385 on input sections. */
10386 htab->toc_bfd = NULL;
10387 htab->toc_first_sec = NULL;
10388 htab->second_toc_pass = TRUE;
10389 return done_something;
10392 /* Called after second pass of multitoc partitioning. */
10394 void
10395 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10397 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10399 /* After the second pass, toc_curr tracks the TOC offset used
10400 for code sections below in ppc64_elf_next_input_section. */
10401 htab->toc_curr = TOC_BASE_OFF;
10404 /* No toc references were found in ISEC. If the code in ISEC makes no
10405 calls, then there's no need to use toc adjusting stubs when branching
10406 into ISEC. Actually, indirect calls from ISEC are OK as they will
10407 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
10408 needed, and 2 if a cyclical call-graph was found but no other reason
10409 for a stub was detected. If called from the top level, a return of
10410 2 means the same as a return of 0. */
10412 static int
10413 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
10415 int ret;
10417 /* Mark this section as checked. */
10418 isec->call_check_done = 1;
10420 /* We know none of our code bearing sections will need toc stubs. */
10421 if ((isec->flags & SEC_LINKER_CREATED) != 0)
10422 return 0;
10424 if (isec->size == 0)
10425 return 0;
10427 if (isec->output_section == NULL)
10428 return 0;
10430 ret = 0;
10431 if (isec->reloc_count != 0)
10433 Elf_Internal_Rela *relstart, *rel;
10434 Elf_Internal_Sym *local_syms;
10435 struct ppc_link_hash_table *htab;
10437 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10438 info->keep_memory);
10439 if (relstart == NULL)
10440 return -1;
10442 /* Look for branches to outside of this section. */
10443 local_syms = NULL;
10444 htab = ppc_hash_table (info);
10445 if (htab == NULL)
10446 return -1;
10448 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
10450 enum elf_ppc64_reloc_type r_type;
10451 unsigned long r_symndx;
10452 struct elf_link_hash_entry *h;
10453 struct ppc_link_hash_entry *eh;
10454 Elf_Internal_Sym *sym;
10455 asection *sym_sec;
10456 struct _opd_sec_data *opd;
10457 bfd_vma sym_value;
10458 bfd_vma dest;
10460 r_type = ELF64_R_TYPE (rel->r_info);
10461 if (r_type != R_PPC64_REL24
10462 && r_type != R_PPC64_REL14
10463 && r_type != R_PPC64_REL14_BRTAKEN
10464 && r_type != R_PPC64_REL14_BRNTAKEN)
10465 continue;
10467 r_symndx = ELF64_R_SYM (rel->r_info);
10468 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10469 isec->owner))
10471 ret = -1;
10472 break;
10475 /* Calls to dynamic lib functions go through a plt call stub
10476 that uses r2. */
10477 eh = (struct ppc_link_hash_entry *) h;
10478 if (eh != NULL
10479 && (eh->elf.plt.plist != NULL
10480 || (eh->oh != NULL
10481 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10483 ret = 1;
10484 break;
10487 if (sym_sec == NULL)
10488 /* Ignore other undefined symbols. */
10489 continue;
10491 /* Assume branches to other sections not included in the
10492 link need stubs too, to cover -R and absolute syms. */
10493 if (sym_sec->output_section == NULL)
10495 ret = 1;
10496 break;
10499 if (h == NULL)
10500 sym_value = sym->st_value;
10501 else
10503 if (h->root.type != bfd_link_hash_defined
10504 && h->root.type != bfd_link_hash_defweak)
10505 abort ();
10506 sym_value = h->root.u.def.value;
10508 sym_value += rel->r_addend;
10510 /* If this branch reloc uses an opd sym, find the code section. */
10511 opd = get_opd_info (sym_sec);
10512 if (opd != NULL)
10514 if (h == NULL && opd->adjust != NULL)
10516 long adjust;
10518 adjust = opd->adjust[sym->st_value / 8];
10519 if (adjust == -1)
10520 /* Assume deleted functions won't ever be called. */
10521 continue;
10522 sym_value += adjust;
10525 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10526 if (dest == (bfd_vma) -1)
10527 continue;
10529 else
10530 dest = (sym_value
10531 + sym_sec->output_offset
10532 + sym_sec->output_section->vma);
10534 /* Ignore branch to self. */
10535 if (sym_sec == isec)
10536 continue;
10538 /* If the called function uses the toc, we need a stub. */
10539 if (sym_sec->has_toc_reloc
10540 || sym_sec->makes_toc_func_call)
10542 ret = 1;
10543 break;
10546 /* Assume any branch that needs a long branch stub might in fact
10547 need a plt_branch stub. A plt_branch stub uses r2. */
10548 else if (dest - (isec->output_offset
10549 + isec->output_section->vma
10550 + rel->r_offset) + (1 << 25) >= (2 << 25))
10552 ret = 1;
10553 break;
10556 /* If calling back to a section in the process of being
10557 tested, we can't say for sure that no toc adjusting stubs
10558 are needed, so don't return zero. */
10559 else if (sym_sec->call_check_in_progress)
10560 ret = 2;
10562 /* Branches to another section that itself doesn't have any TOC
10563 references are OK. Recursively call ourselves to check. */
10564 else if (!sym_sec->call_check_done)
10566 int recur;
10568 /* Mark current section as indeterminate, so that other
10569 sections that call back to current won't be marked as
10570 known. */
10571 isec->call_check_in_progress = 1;
10572 recur = toc_adjusting_stub_needed (info, sym_sec);
10573 isec->call_check_in_progress = 0;
10575 if (recur != 0)
10577 ret = recur;
10578 if (recur != 2)
10579 break;
10584 if (local_syms != NULL
10585 && (elf_symtab_hdr (isec->owner).contents
10586 != (unsigned char *) local_syms))
10587 free (local_syms);
10588 if (elf_section_data (isec)->relocs != relstart)
10589 free (relstart);
10592 if ((ret & 1) == 0
10593 && isec->map_head.s != NULL
10594 && (strcmp (isec->output_section->name, ".init") == 0
10595 || strcmp (isec->output_section->name, ".fini") == 0))
10597 if (isec->map_head.s->has_toc_reloc
10598 || isec->map_head.s->makes_toc_func_call)
10599 ret = 1;
10600 else if (!isec->map_head.s->call_check_done)
10602 int recur;
10603 isec->call_check_in_progress = 1;
10604 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10605 isec->call_check_in_progress = 0;
10606 if (recur != 0)
10607 ret = recur;
10611 if (ret == 1)
10612 isec->makes_toc_func_call = 1;
10614 return ret;
10617 /* The linker repeatedly calls this function for each input section,
10618 in the order that input sections are linked into output sections.
10619 Build lists of input sections to determine groupings between which
10620 we may insert linker stubs. */
10622 bfd_boolean
10623 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
10625 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10627 if (htab == NULL)
10628 return FALSE;
10630 if ((isec->output_section->flags & SEC_CODE) != 0
10631 && isec->output_section->index <= htab->top_index)
10633 asection **list = htab->input_list + isec->output_section->index;
10634 /* Steal the link_sec pointer for our list. */
10635 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
10636 /* This happens to make the list in reverse order,
10637 which is what we want. */
10638 PREV_SEC (isec) = *list;
10639 *list = isec;
10642 if (htab->multi_toc_needed)
10644 /* If a code section has a function that uses the TOC then we need
10645 to use the right TOC (obviously). Also, make sure that .opd gets
10646 the correct TOC value for R_PPC64_TOC relocs that don't have or
10647 can't find their function symbol (shouldn't ever happen now).
10648 Also specially treat .fixup for the linux kernel. .fixup
10649 contains branches, but only back to the function that hit an
10650 exception. */
10651 if (isec->has_toc_reloc
10652 || (isec->flags & SEC_CODE) == 0
10653 || strcmp (isec->name, ".fixup") == 0)
10655 if (elf_gp (isec->owner) != 0)
10656 htab->toc_curr = elf_gp (isec->owner);
10658 else
10660 if (!isec->call_check_done
10661 && toc_adjusting_stub_needed (info, isec) < 0)
10662 return FALSE;
10663 /* If we make a local call from this section, ie. a branch
10664 without a following nop, then we have no place to put a
10665 toc restoring insn. We must use the same toc group as
10666 the callee.
10667 Testing makes_toc_func_call actually tests for *any*
10668 calls to functions that need a good toc pointer. A more
10669 precise test would be better, as this one will set
10670 incorrect values for pasted .init/.fini fragments.
10671 (Fixed later in check_pasted_section.) */
10672 if (isec->makes_toc_func_call
10673 && elf_gp (isec->owner) != 0)
10674 htab->toc_curr = elf_gp (isec->owner);
10678 /* Functions that don't use the TOC can belong in any TOC group.
10679 Use the last TOC base. */
10680 htab->stub_group[isec->id].toc_off = htab->toc_curr;
10681 return TRUE;
10684 /* Check that all .init and .fini sections use the same toc, if they
10685 have toc relocs. */
10687 static bfd_boolean
10688 check_pasted_section (struct bfd_link_info *info, const char *name)
10690 asection *o = bfd_get_section_by_name (info->output_bfd, name);
10692 if (o != NULL)
10694 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10695 bfd_vma toc_off = 0;
10696 asection *i;
10698 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10699 if (i->has_toc_reloc)
10701 if (toc_off == 0)
10702 toc_off = htab->stub_group[i->id].toc_off;
10703 else if (toc_off != htab->stub_group[i->id].toc_off)
10704 return FALSE;
10707 if (toc_off == 0)
10708 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10709 if (i->makes_toc_func_call)
10711 toc_off = htab->stub_group[i->id].toc_off;
10712 break;
10715 /* Make sure the whole pasted function uses the same toc offset. */
10716 if (toc_off != 0)
10717 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10718 htab->stub_group[i->id].toc_off = toc_off;
10720 return TRUE;
10723 bfd_boolean
10724 ppc64_elf_check_init_fini (struct bfd_link_info *info)
10726 return (check_pasted_section (info, ".init")
10727 & check_pasted_section (info, ".fini"));
10730 /* See whether we can group stub sections together. Grouping stub
10731 sections may result in fewer stubs. More importantly, we need to
10732 put all .init* and .fini* stubs at the beginning of the .init or
10733 .fini output sections respectively, because glibc splits the
10734 _init and _fini functions into multiple parts. Putting a stub in
10735 the middle of a function is not a good idea. */
10737 static void
10738 group_sections (struct ppc_link_hash_table *htab,
10739 bfd_size_type stub_group_size,
10740 bfd_boolean stubs_always_before_branch)
10742 asection **list;
10743 bfd_size_type stub14_group_size;
10744 bfd_boolean suppress_size_errors;
10746 suppress_size_errors = FALSE;
10747 stub14_group_size = stub_group_size;
10748 if (stub_group_size == 1)
10750 /* Default values. */
10751 if (stubs_always_before_branch)
10753 stub_group_size = 0x1e00000;
10754 stub14_group_size = 0x7800;
10756 else
10758 stub_group_size = 0x1c00000;
10759 stub14_group_size = 0x7000;
10761 suppress_size_errors = TRUE;
10764 list = htab->input_list + htab->top_index;
10767 asection *tail = *list;
10768 while (tail != NULL)
10770 asection *curr;
10771 asection *prev;
10772 bfd_size_type total;
10773 bfd_boolean big_sec;
10774 bfd_vma curr_toc;
10776 curr = tail;
10777 total = tail->size;
10778 big_sec = total > (ppc64_elf_section_data (tail) != NULL
10779 && ppc64_elf_section_data (tail)->has_14bit_branch
10780 ? stub14_group_size : stub_group_size);
10781 if (big_sec && !suppress_size_errors)
10782 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
10783 tail->owner, tail);
10784 curr_toc = htab->stub_group[tail->id].toc_off;
10786 while ((prev = PREV_SEC (curr)) != NULL
10787 && ((total += curr->output_offset - prev->output_offset)
10788 < (ppc64_elf_section_data (prev) != NULL
10789 && ppc64_elf_section_data (prev)->has_14bit_branch
10790 ? stub14_group_size : stub_group_size))
10791 && htab->stub_group[prev->id].toc_off == curr_toc)
10792 curr = prev;
10794 /* OK, the size from the start of CURR to the end is less
10795 than stub_group_size and thus can be handled by one stub
10796 section. (or the tail section is itself larger than
10797 stub_group_size, in which case we may be toast.) We
10798 should really be keeping track of the total size of stubs
10799 added here, as stubs contribute to the final output
10800 section size. That's a little tricky, and this way will
10801 only break if stubs added make the total size more than
10802 2^25, ie. for the default stub_group_size, if stubs total
10803 more than 2097152 bytes, or nearly 75000 plt call stubs. */
10806 prev = PREV_SEC (tail);
10807 /* Set up this stub group. */
10808 htab->stub_group[tail->id].link_sec = curr;
10810 while (tail != curr && (tail = prev) != NULL);
10812 /* But wait, there's more! Input sections up to stub_group_size
10813 bytes before the stub section can be handled by it too.
10814 Don't do this if we have a really large section after the
10815 stubs, as adding more stubs increases the chance that
10816 branches may not reach into the stub section. */
10817 if (!stubs_always_before_branch && !big_sec)
10819 total = 0;
10820 while (prev != NULL
10821 && ((total += tail->output_offset - prev->output_offset)
10822 < (ppc64_elf_section_data (prev) != NULL
10823 && ppc64_elf_section_data (prev)->has_14bit_branch
10824 ? stub14_group_size : stub_group_size))
10825 && htab->stub_group[prev->id].toc_off == curr_toc)
10827 tail = prev;
10828 prev = PREV_SEC (tail);
10829 htab->stub_group[tail->id].link_sec = curr;
10832 tail = prev;
10835 while (list-- != htab->input_list);
10836 free (htab->input_list);
10837 #undef PREV_SEC
10840 /* Determine and set the size of the stub section for a final link.
10842 The basic idea here is to examine all the relocations looking for
10843 PC-relative calls to a target that is unreachable with a "bl"
10844 instruction. */
10846 bfd_boolean
10847 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size)
10849 bfd_size_type stub_group_size;
10850 bfd_boolean stubs_always_before_branch;
10851 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10853 if (htab == NULL)
10854 return FALSE;
10856 stubs_always_before_branch = group_size < 0;
10857 if (group_size < 0)
10858 stub_group_size = -group_size;
10859 else
10860 stub_group_size = group_size;
10862 group_sections (htab, stub_group_size, stubs_always_before_branch);
10864 while (1)
10866 bfd *input_bfd;
10867 unsigned int bfd_indx;
10868 asection *stub_sec;
10870 htab->stub_iteration += 1;
10872 for (input_bfd = info->input_bfds, bfd_indx = 0;
10873 input_bfd != NULL;
10874 input_bfd = input_bfd->link_next, bfd_indx++)
10876 Elf_Internal_Shdr *symtab_hdr;
10877 asection *section;
10878 Elf_Internal_Sym *local_syms = NULL;
10880 if (!is_ppc64_elf (input_bfd))
10881 continue;
10883 /* We'll need the symbol table in a second. */
10884 symtab_hdr = &elf_symtab_hdr (input_bfd);
10885 if (symtab_hdr->sh_info == 0)
10886 continue;
10888 /* Walk over each section attached to the input bfd. */
10889 for (section = input_bfd->sections;
10890 section != NULL;
10891 section = section->next)
10893 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
10895 /* If there aren't any relocs, then there's nothing more
10896 to do. */
10897 if ((section->flags & SEC_RELOC) == 0
10898 || (section->flags & SEC_ALLOC) == 0
10899 || (section->flags & SEC_LOAD) == 0
10900 || (section->flags & SEC_CODE) == 0
10901 || section->reloc_count == 0)
10902 continue;
10904 /* If this section is a link-once section that will be
10905 discarded, then don't create any stubs. */
10906 if (section->output_section == NULL
10907 || section->output_section->owner != info->output_bfd)
10908 continue;
10910 /* Get the relocs. */
10911 internal_relocs
10912 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
10913 info->keep_memory);
10914 if (internal_relocs == NULL)
10915 goto error_ret_free_local;
10917 /* Now examine each relocation. */
10918 irela = internal_relocs;
10919 irelaend = irela + section->reloc_count;
10920 for (; irela < irelaend; irela++)
10922 enum elf_ppc64_reloc_type r_type;
10923 unsigned int r_indx;
10924 enum ppc_stub_type stub_type;
10925 struct ppc_stub_hash_entry *stub_entry;
10926 asection *sym_sec, *code_sec;
10927 bfd_vma sym_value, code_value;
10928 bfd_vma destination;
10929 bfd_boolean ok_dest;
10930 struct ppc_link_hash_entry *hash;
10931 struct ppc_link_hash_entry *fdh;
10932 struct elf_link_hash_entry *h;
10933 Elf_Internal_Sym *sym;
10934 char *stub_name;
10935 const asection *id_sec;
10936 struct _opd_sec_data *opd;
10937 struct plt_entry *plt_ent;
10939 r_type = ELF64_R_TYPE (irela->r_info);
10940 r_indx = ELF64_R_SYM (irela->r_info);
10942 if (r_type >= R_PPC64_max)
10944 bfd_set_error (bfd_error_bad_value);
10945 goto error_ret_free_internal;
10948 /* Only look for stubs on branch instructions. */
10949 if (r_type != R_PPC64_REL24
10950 && r_type != R_PPC64_REL14
10951 && r_type != R_PPC64_REL14_BRTAKEN
10952 && r_type != R_PPC64_REL14_BRNTAKEN)
10953 continue;
10955 /* Now determine the call target, its name, value,
10956 section. */
10957 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
10958 r_indx, input_bfd))
10959 goto error_ret_free_internal;
10960 hash = (struct ppc_link_hash_entry *) h;
10962 ok_dest = FALSE;
10963 fdh = NULL;
10964 sym_value = 0;
10965 if (hash == NULL)
10967 sym_value = sym->st_value;
10968 ok_dest = TRUE;
10970 else if (hash->elf.root.type == bfd_link_hash_defined
10971 || hash->elf.root.type == bfd_link_hash_defweak)
10973 sym_value = hash->elf.root.u.def.value;
10974 if (sym_sec->output_section != NULL)
10975 ok_dest = TRUE;
10977 else if (hash->elf.root.type == bfd_link_hash_undefweak
10978 || hash->elf.root.type == bfd_link_hash_undefined)
10980 /* Recognise an old ABI func code entry sym, and
10981 use the func descriptor sym instead if it is
10982 defined. */
10983 if (hash->elf.root.root.string[0] == '.'
10984 && (fdh = lookup_fdh (hash, htab)) != NULL)
10986 if (fdh->elf.root.type == bfd_link_hash_defined
10987 || fdh->elf.root.type == bfd_link_hash_defweak)
10989 sym_sec = fdh->elf.root.u.def.section;
10990 sym_value = fdh->elf.root.u.def.value;
10991 if (sym_sec->output_section != NULL)
10992 ok_dest = TRUE;
10994 else
10995 fdh = NULL;
10998 else
11000 bfd_set_error (bfd_error_bad_value);
11001 goto error_ret_free_internal;
11004 destination = 0;
11005 if (ok_dest)
11007 sym_value += irela->r_addend;
11008 destination = (sym_value
11009 + sym_sec->output_offset
11010 + sym_sec->output_section->vma);
11013 code_sec = sym_sec;
11014 code_value = sym_value;
11015 opd = get_opd_info (sym_sec);
11016 if (opd != NULL)
11018 bfd_vma dest;
11020 if (hash == NULL && opd->adjust != NULL)
11022 long adjust = opd->adjust[sym_value / 8];
11023 if (adjust == -1)
11024 continue;
11025 code_value += adjust;
11026 sym_value += adjust;
11028 dest = opd_entry_value (sym_sec, sym_value,
11029 &code_sec, &code_value);
11030 if (dest != (bfd_vma) -1)
11032 destination = dest;
11033 if (fdh != NULL)
11035 /* Fixup old ABI sym to point at code
11036 entry. */
11037 hash->elf.root.type = bfd_link_hash_defweak;
11038 hash->elf.root.u.def.section = code_sec;
11039 hash->elf.root.u.def.value = code_value;
11044 /* Determine what (if any) linker stub is needed. */
11045 plt_ent = NULL;
11046 stub_type = ppc_type_of_stub (section, irela, &hash,
11047 &plt_ent, destination);
11049 if (stub_type != ppc_stub_plt_call)
11051 /* Check whether we need a TOC adjusting stub.
11052 Since the linker pastes together pieces from
11053 different object files when creating the
11054 _init and _fini functions, it may be that a
11055 call to what looks like a local sym is in
11056 fact a call needing a TOC adjustment. */
11057 if (code_sec != NULL
11058 && code_sec->output_section != NULL
11059 && (htab->stub_group[code_sec->id].toc_off
11060 != htab->stub_group[section->id].toc_off)
11061 && (code_sec->has_toc_reloc
11062 || code_sec->makes_toc_func_call))
11063 stub_type = ppc_stub_long_branch_r2off;
11066 if (stub_type == ppc_stub_none)
11067 continue;
11069 /* __tls_get_addr calls might be eliminated. */
11070 if (stub_type != ppc_stub_plt_call
11071 && hash != NULL
11072 && (hash == htab->tls_get_addr
11073 || hash == htab->tls_get_addr_fd)
11074 && section->has_tls_reloc
11075 && irela != internal_relocs)
11077 /* Get tls info. */
11078 unsigned char *tls_mask;
11080 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
11081 irela - 1, input_bfd))
11082 goto error_ret_free_internal;
11083 if (*tls_mask != 0)
11084 continue;
11087 /* Support for grouping stub sections. */
11088 id_sec = htab->stub_group[section->id].link_sec;
11090 /* Get the name of this stub. */
11091 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11092 if (!stub_name)
11093 goto error_ret_free_internal;
11095 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
11096 stub_name, FALSE, FALSE);
11097 if (stub_entry != NULL)
11099 /* The proper stub has already been created. */
11100 free (stub_name);
11101 continue;
11104 stub_entry = ppc_add_stub (stub_name, section, htab);
11105 if (stub_entry == NULL)
11107 free (stub_name);
11108 error_ret_free_internal:
11109 if (elf_section_data (section)->relocs == NULL)
11110 free (internal_relocs);
11111 error_ret_free_local:
11112 if (local_syms != NULL
11113 && (symtab_hdr->contents
11114 != (unsigned char *) local_syms))
11115 free (local_syms);
11116 return FALSE;
11119 stub_entry->stub_type = stub_type;
11120 if (stub_type != ppc_stub_plt_call)
11122 stub_entry->target_value = code_value;
11123 stub_entry->target_section = code_sec;
11125 else
11127 stub_entry->target_value = sym_value;
11128 stub_entry->target_section = sym_sec;
11130 stub_entry->h = hash;
11131 stub_entry->plt_ent = plt_ent;
11132 stub_entry->addend = irela->r_addend;
11134 if (stub_entry->h != NULL)
11135 htab->stub_globals += 1;
11138 /* We're done with the internal relocs, free them. */
11139 if (elf_section_data (section)->relocs != internal_relocs)
11140 free (internal_relocs);
11143 if (local_syms != NULL
11144 && symtab_hdr->contents != (unsigned char *) local_syms)
11146 if (!info->keep_memory)
11147 free (local_syms);
11148 else
11149 symtab_hdr->contents = (unsigned char *) local_syms;
11153 /* We may have added some stubs. Find out the new size of the
11154 stub sections. */
11155 for (stub_sec = htab->stub_bfd->sections;
11156 stub_sec != NULL;
11157 stub_sec = stub_sec->next)
11158 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11160 stub_sec->rawsize = stub_sec->size;
11161 stub_sec->size = 0;
11162 stub_sec->reloc_count = 0;
11163 stub_sec->flags &= ~SEC_RELOC;
11166 htab->brlt->size = 0;
11167 htab->brlt->reloc_count = 0;
11168 htab->brlt->flags &= ~SEC_RELOC;
11169 if (htab->relbrlt != NULL)
11170 htab->relbrlt->size = 0;
11172 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
11174 if (info->emitrelocations
11175 && htab->glink != NULL && htab->glink->size != 0)
11177 htab->glink->reloc_count = 1;
11178 htab->glink->flags |= SEC_RELOC;
11181 for (stub_sec = htab->stub_bfd->sections;
11182 stub_sec != NULL;
11183 stub_sec = stub_sec->next)
11184 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11185 && stub_sec->rawsize != stub_sec->size)
11186 break;
11188 /* Exit from this loop when no stubs have been added, and no stubs
11189 have changed size. */
11190 if (stub_sec == NULL)
11191 break;
11193 /* Ask the linker to do its stuff. */
11194 (*htab->layout_sections_again) ();
11197 /* It would be nice to strip htab->brlt from the output if the
11198 section is empty, but it's too late. If we strip sections here,
11199 the dynamic symbol table is corrupted since the section symbol
11200 for the stripped section isn't written. */
11202 return TRUE;
11205 /* Called after we have determined section placement. If sections
11206 move, we'll be called again. Provide a value for TOCstart. */
11208 bfd_vma
11209 ppc64_elf_toc (bfd *obfd)
11211 asection *s;
11212 bfd_vma TOCstart;
11214 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11215 order. The TOC starts where the first of these sections starts. */
11216 s = bfd_get_section_by_name (obfd, ".got");
11217 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11218 s = bfd_get_section_by_name (obfd, ".toc");
11219 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11220 s = bfd_get_section_by_name (obfd, ".tocbss");
11221 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11222 s = bfd_get_section_by_name (obfd, ".plt");
11223 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11225 /* This may happen for
11226 o references to TOC base (SYM@toc / TOC[tc0]) without a
11227 .toc directive
11228 o bad linker script
11229 o --gc-sections and empty TOC sections
11231 FIXME: Warn user? */
11233 /* Look for a likely section. We probably won't even be
11234 using TOCstart. */
11235 for (s = obfd->sections; s != NULL; s = s->next)
11236 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11237 | SEC_EXCLUDE))
11238 == (SEC_ALLOC | SEC_SMALL_DATA))
11239 break;
11240 if (s == NULL)
11241 for (s = obfd->sections; s != NULL; s = s->next)
11242 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
11243 == (SEC_ALLOC | SEC_SMALL_DATA))
11244 break;
11245 if (s == NULL)
11246 for (s = obfd->sections; s != NULL; s = s->next)
11247 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11248 == SEC_ALLOC)
11249 break;
11250 if (s == NULL)
11251 for (s = obfd->sections; s != NULL; s = s->next)
11252 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
11253 break;
11256 TOCstart = 0;
11257 if (s != NULL)
11258 TOCstart = s->output_section->vma + s->output_offset;
11260 return TOCstart;
11263 /* Build all the stubs associated with the current output file.
11264 The stubs are kept in a hash table attached to the main linker
11265 hash table. This function is called via gldelf64ppc_finish. */
11267 bfd_boolean
11268 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11269 struct bfd_link_info *info,
11270 char **stats)
11272 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11273 asection *stub_sec;
11274 bfd_byte *p;
11275 int stub_sec_count = 0;
11277 if (htab == NULL)
11278 return FALSE;
11280 htab->emit_stub_syms = emit_stub_syms;
11282 /* Allocate memory to hold the linker stubs. */
11283 for (stub_sec = htab->stub_bfd->sections;
11284 stub_sec != NULL;
11285 stub_sec = stub_sec->next)
11286 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11287 && stub_sec->size != 0)
11289 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11290 if (stub_sec->contents == NULL)
11291 return FALSE;
11292 /* We want to check that built size is the same as calculated
11293 size. rawsize is a convenient location to use. */
11294 stub_sec->rawsize = stub_sec->size;
11295 stub_sec->size = 0;
11298 if (htab->glink != NULL && htab->glink->size != 0)
11300 unsigned int indx;
11301 bfd_vma plt0;
11303 /* Build the .glink plt call stub. */
11304 if (htab->emit_stub_syms)
11306 struct elf_link_hash_entry *h;
11307 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11308 TRUE, FALSE, FALSE);
11309 if (h == NULL)
11310 return FALSE;
11311 if (h->root.type == bfd_link_hash_new)
11313 h->root.type = bfd_link_hash_defined;
11314 h->root.u.def.section = htab->glink;
11315 h->root.u.def.value = 8;
11316 h->ref_regular = 1;
11317 h->def_regular = 1;
11318 h->ref_regular_nonweak = 1;
11319 h->forced_local = 1;
11320 h->non_elf = 0;
11323 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11324 if (info->emitrelocations)
11326 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11327 if (r == NULL)
11328 return FALSE;
11329 r->r_offset = (htab->glink->output_offset
11330 + htab->glink->output_section->vma);
11331 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11332 r->r_addend = plt0;
11334 p = htab->glink->contents;
11335 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
11336 bfd_put_64 (htab->glink->owner, plt0, p);
11337 p += 8;
11338 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
11339 p += 4;
11340 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
11341 p += 4;
11342 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
11343 p += 4;
11344 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
11345 p += 4;
11346 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
11347 p += 4;
11348 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
11349 p += 4;
11350 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
11351 p += 4;
11352 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
11353 p += 4;
11354 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
11355 p += 4;
11356 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
11357 p += 4;
11358 bfd_put_32 (htab->glink->owner, BCTR, p);
11359 p += 4;
11360 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11362 bfd_put_32 (htab->glink->owner, NOP, p);
11363 p += 4;
11366 /* Build the .glink lazy link call stubs. */
11367 indx = 0;
11368 while (p < htab->glink->contents + htab->glink->size)
11370 if (indx < 0x8000)
11372 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
11373 p += 4;
11375 else
11377 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
11378 p += 4;
11379 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
11380 p += 4;
11382 bfd_put_32 (htab->glink->owner,
11383 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
11384 indx++;
11385 p += 4;
11387 htab->glink->rawsize = p - htab->glink->contents;
11390 if (htab->brlt->size != 0)
11392 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
11393 htab->brlt->size);
11394 if (htab->brlt->contents == NULL)
11395 return FALSE;
11397 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
11399 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
11400 htab->relbrlt->size);
11401 if (htab->relbrlt->contents == NULL)
11402 return FALSE;
11405 /* Build the stubs as directed by the stub hash table. */
11406 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
11408 if (htab->relbrlt != NULL)
11409 htab->relbrlt->reloc_count = 0;
11411 for (stub_sec = htab->stub_bfd->sections;
11412 stub_sec != NULL;
11413 stub_sec = stub_sec->next)
11414 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11416 stub_sec_count += 1;
11417 if (stub_sec->rawsize != stub_sec->size)
11418 break;
11421 if (stub_sec != NULL
11422 || htab->glink->rawsize != htab->glink->size)
11424 htab->stub_error = TRUE;
11425 (*_bfd_error_handler) (_("stubs don't match calculated size"));
11428 if (htab->stub_error)
11429 return FALSE;
11431 if (stats != NULL)
11433 *stats = bfd_malloc (500);
11434 if (*stats == NULL)
11435 return FALSE;
11437 sprintf (*stats, _("linker stubs in %u group%s\n"
11438 " branch %lu\n"
11439 " toc adjust %lu\n"
11440 " long branch %lu\n"
11441 " long toc adj %lu\n"
11442 " plt call %lu"),
11443 stub_sec_count,
11444 stub_sec_count == 1 ? "" : "s",
11445 htab->stub_count[ppc_stub_long_branch - 1],
11446 htab->stub_count[ppc_stub_long_branch_r2off - 1],
11447 htab->stub_count[ppc_stub_plt_branch - 1],
11448 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
11449 htab->stub_count[ppc_stub_plt_call - 1]);
11451 return TRUE;
11454 /* This function undoes the changes made by add_symbol_adjust. */
11456 static bfd_boolean
11457 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11459 struct ppc_link_hash_entry *eh;
11461 if (h->root.type == bfd_link_hash_indirect)
11462 return TRUE;
11464 if (h->root.type == bfd_link_hash_warning)
11465 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11467 eh = (struct ppc_link_hash_entry *) h;
11468 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
11469 return TRUE;
11471 eh->elf.root.type = bfd_link_hash_undefined;
11472 return TRUE;
11475 void
11476 ppc64_elf_restore_symbols (struct bfd_link_info *info)
11478 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11480 if (htab != NULL)
11481 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
11484 /* What to do when ld finds relocations against symbols defined in
11485 discarded sections. */
11487 static unsigned int
11488 ppc64_elf_action_discarded (asection *sec)
11490 if (strcmp (".opd", sec->name) == 0)
11491 return 0;
11493 if (strcmp (".toc", sec->name) == 0)
11494 return 0;
11496 if (strcmp (".toc1", sec->name) == 0)
11497 return 0;
11499 return _bfd_elf_default_action_discarded (sec);
11502 /* REL points to a low-part reloc on a largetoc instruction sequence.
11503 Find the matching high-part reloc instruction and verify that it
11504 is addis REG,x,imm. If so, set *REG to x and return a pointer to
11505 the high-part reloc. */
11507 static const Elf_Internal_Rela *
11508 ha_reloc_match (const Elf_Internal_Rela *relocs,
11509 const Elf_Internal_Rela *rel,
11510 unsigned int *reg,
11511 bfd_boolean match_addend,
11512 const bfd *input_bfd,
11513 const bfd_byte *contents)
11515 enum elf_ppc64_reloc_type r_type, r_type_ha;
11516 bfd_vma r_info_ha, r_addend;
11518 r_type = ELF64_R_TYPE (rel->r_info);
11519 switch (r_type)
11521 case R_PPC64_GOT_TLSLD16_LO:
11522 case R_PPC64_GOT_TLSGD16_LO:
11523 case R_PPC64_GOT_TPREL16_LO_DS:
11524 case R_PPC64_GOT_DTPREL16_LO_DS:
11525 case R_PPC64_GOT16_LO:
11526 case R_PPC64_TOC16_LO:
11527 r_type_ha = r_type + 2;
11528 break;
11529 case R_PPC64_GOT16_LO_DS:
11530 r_type_ha = R_PPC64_GOT16_HA;
11531 break;
11532 case R_PPC64_TOC16_LO_DS:
11533 r_type_ha = R_PPC64_TOC16_HA;
11534 break;
11535 default:
11536 abort ();
11538 r_info_ha = ELF64_R_INFO (ELF64_R_SYM (rel->r_info), r_type_ha);
11539 r_addend = rel->r_addend;
11541 while (--rel >= relocs)
11542 if (rel->r_info == r_info_ha
11543 && (!match_addend
11544 || rel->r_addend == r_addend))
11546 const bfd_byte *p = contents + (rel->r_offset & ~3);
11547 unsigned int insn = bfd_get_32 (input_bfd, p);
11548 if ((insn & (0x3f << 26)) == (15u << 26) /* addis rt,x,imm */
11549 && (insn & (0x1f << 21)) == (*reg << 21))
11551 *reg = (insn >> 16) & 0x1f;
11552 return rel;
11554 break;
11556 return NULL;
11559 /* The RELOCATE_SECTION function is called by the ELF backend linker
11560 to handle the relocations for a section.
11562 The relocs are always passed as Rela structures; if the section
11563 actually uses Rel structures, the r_addend field will always be
11564 zero.
11566 This function is responsible for adjust the section contents as
11567 necessary, and (if using Rela relocs and generating a
11568 relocatable output file) adjusting the reloc addend as
11569 necessary.
11571 This function does not have to worry about setting the reloc
11572 address or the reloc symbol index.
11574 LOCAL_SYMS is a pointer to the swapped in local symbols.
11576 LOCAL_SECTIONS is an array giving the section in the input file
11577 corresponding to the st_shndx field of each local symbol.
11579 The global hash table entry for the global symbols can be found
11580 via elf_sym_hashes (input_bfd).
11582 When generating relocatable output, this function must handle
11583 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
11584 going to be the section symbol corresponding to the output
11585 section, which means that the addend must be adjusted
11586 accordingly. */
11588 static bfd_boolean
11589 ppc64_elf_relocate_section (bfd *output_bfd,
11590 struct bfd_link_info *info,
11591 bfd *input_bfd,
11592 asection *input_section,
11593 bfd_byte *contents,
11594 Elf_Internal_Rela *relocs,
11595 Elf_Internal_Sym *local_syms,
11596 asection **local_sections)
11598 struct ppc_link_hash_table *htab;
11599 Elf_Internal_Shdr *symtab_hdr;
11600 struct elf_link_hash_entry **sym_hashes;
11601 Elf_Internal_Rela *rel;
11602 Elf_Internal_Rela *relend;
11603 Elf_Internal_Rela outrel;
11604 bfd_byte *loc;
11605 struct got_entry **local_got_ents;
11606 unsigned char *ha_opt;
11607 bfd_vma TOCstart;
11608 bfd_boolean no_ha_opt;
11609 bfd_boolean ret = TRUE;
11610 bfd_boolean is_opd;
11611 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
11612 bfd_boolean is_power4 = FALSE;
11613 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
11615 /* Initialize howto table if needed. */
11616 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
11617 ppc_howto_init ();
11619 htab = ppc_hash_table (info);
11620 if (htab == NULL)
11621 return FALSE;
11623 /* Don't relocate stub sections. */
11624 if (input_section->owner == htab->stub_bfd)
11625 return TRUE;
11627 BFD_ASSERT (is_ppc64_elf (input_bfd));
11629 local_got_ents = elf_local_got_ents (input_bfd);
11630 TOCstart = elf_gp (output_bfd);
11631 symtab_hdr = &elf_symtab_hdr (input_bfd);
11632 sym_hashes = elf_sym_hashes (input_bfd);
11633 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
11634 ha_opt = NULL;
11635 no_ha_opt = FALSE;
11637 rel = relocs;
11638 relend = relocs + input_section->reloc_count;
11639 for (; rel < relend; rel++)
11641 enum elf_ppc64_reloc_type r_type;
11642 bfd_vma addend, orig_addend;
11643 bfd_reloc_status_type r;
11644 Elf_Internal_Sym *sym;
11645 asection *sec;
11646 struct elf_link_hash_entry *h_elf;
11647 struct ppc_link_hash_entry *h;
11648 struct ppc_link_hash_entry *fdh;
11649 const char *sym_name;
11650 unsigned long r_symndx, toc_symndx;
11651 bfd_vma toc_addend;
11652 unsigned char tls_mask, tls_gd, tls_type;
11653 unsigned char sym_type;
11654 bfd_vma relocation;
11655 bfd_boolean unresolved_reloc;
11656 bfd_boolean warned;
11657 unsigned int insn;
11658 unsigned int mask;
11659 struct ppc_stub_hash_entry *stub_entry;
11660 bfd_vma max_br_offset;
11661 bfd_vma from;
11663 r_type = ELF64_R_TYPE (rel->r_info);
11664 r_symndx = ELF64_R_SYM (rel->r_info);
11666 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
11667 symbol of the previous ADDR64 reloc. The symbol gives us the
11668 proper TOC base to use. */
11669 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
11670 && rel != relocs
11671 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
11672 && is_opd)
11673 r_symndx = ELF64_R_SYM (rel[-1].r_info);
11675 sym = NULL;
11676 sec = NULL;
11677 h_elf = NULL;
11678 sym_name = NULL;
11679 unresolved_reloc = FALSE;
11680 warned = FALSE;
11681 orig_addend = rel->r_addend;
11683 if (r_symndx < symtab_hdr->sh_info)
11685 /* It's a local symbol. */
11686 struct _opd_sec_data *opd;
11688 sym = local_syms + r_symndx;
11689 sec = local_sections[r_symndx];
11690 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
11691 sym_type = ELF64_ST_TYPE (sym->st_info);
11692 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
11693 opd = get_opd_info (sec);
11694 if (opd != NULL && opd->adjust != NULL)
11696 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
11697 if (adjust == -1)
11698 relocation = 0;
11699 else
11701 /* If this is a relocation against the opd section sym
11702 and we have edited .opd, adjust the reloc addend so
11703 that ld -r and ld --emit-relocs output is correct.
11704 If it is a reloc against some other .opd symbol,
11705 then the symbol value will be adjusted later. */
11706 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11707 rel->r_addend += adjust;
11708 else
11709 relocation += adjust;
11713 else
11715 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
11716 r_symndx, symtab_hdr, sym_hashes,
11717 h_elf, sec, relocation,
11718 unresolved_reloc, warned);
11719 sym_name = h_elf->root.root.string;
11720 sym_type = h_elf->type;
11722 h = (struct ppc_link_hash_entry *) h_elf;
11724 if (sec != NULL && elf_discarded_section (sec))
11725 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
11726 rel, relend,
11727 ppc64_elf_howto_table[r_type],
11728 contents);
11730 if (info->relocatable)
11731 continue;
11733 /* TLS optimizations. Replace instruction sequences and relocs
11734 based on information we collected in tls_optimize. We edit
11735 RELOCS so that --emit-relocs will output something sensible
11736 for the final instruction stream. */
11737 tls_mask = 0;
11738 tls_gd = 0;
11739 toc_symndx = 0;
11740 if (h != NULL)
11741 tls_mask = h->tls_mask;
11742 else if (local_got_ents != NULL)
11744 struct plt_entry **local_plt = (struct plt_entry **)
11745 (local_got_ents + symtab_hdr->sh_info);
11746 unsigned char *lgot_masks = (unsigned char *)
11747 (local_plt + symtab_hdr->sh_info);
11748 tls_mask = lgot_masks[r_symndx];
11750 if (tls_mask == 0
11751 && (r_type == R_PPC64_TLS
11752 || r_type == R_PPC64_TLSGD
11753 || r_type == R_PPC64_TLSLD))
11755 /* Check for toc tls entries. */
11756 unsigned char *toc_tls;
11758 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11759 &local_syms, rel, input_bfd))
11760 return FALSE;
11762 if (toc_tls)
11763 tls_mask = *toc_tls;
11766 /* Check that tls relocs are used with tls syms, and non-tls
11767 relocs are used with non-tls syms. */
11768 if (r_symndx != STN_UNDEF
11769 && r_type != R_PPC64_NONE
11770 && (h == NULL
11771 || h->elf.root.type == bfd_link_hash_defined
11772 || h->elf.root.type == bfd_link_hash_defweak)
11773 && (IS_PPC64_TLS_RELOC (r_type)
11774 != (sym_type == STT_TLS
11775 || (sym_type == STT_SECTION
11776 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
11778 if (tls_mask != 0
11779 && (r_type == R_PPC64_TLS
11780 || r_type == R_PPC64_TLSGD
11781 || r_type == R_PPC64_TLSLD))
11782 /* R_PPC64_TLS is OK against a symbol in the TOC. */
11784 else
11785 (*_bfd_error_handler)
11786 (!IS_PPC64_TLS_RELOC (r_type)
11787 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
11788 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s"),
11789 input_bfd,
11790 input_section,
11791 (long) rel->r_offset,
11792 ppc64_elf_howto_table[r_type]->name,
11793 sym_name);
11796 /* Ensure reloc mapping code below stays sane. */
11797 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
11798 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
11799 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
11800 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
11801 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
11802 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
11803 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
11804 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
11805 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
11806 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
11807 abort ();
11809 switch (r_type)
11811 default:
11812 break;
11814 case R_PPC64_LO_DS_OPT:
11815 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
11816 if ((insn & (0x3f << 26)) != 58u << 26)
11817 abort ();
11818 insn += (14u << 26) - (58u << 26);
11819 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
11820 r_type = R_PPC64_TOC16_LO;
11821 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11822 break;
11824 case R_PPC64_TOC16:
11825 case R_PPC64_TOC16_LO:
11826 case R_PPC64_TOC16_DS:
11827 case R_PPC64_TOC16_LO_DS:
11829 /* Check for toc tls entries. */
11830 unsigned char *toc_tls;
11831 int retval;
11833 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11834 &local_syms, rel, input_bfd);
11835 if (retval == 0)
11836 return FALSE;
11838 if (toc_tls)
11840 tls_mask = *toc_tls;
11841 if (r_type == R_PPC64_TOC16_DS
11842 || r_type == R_PPC64_TOC16_LO_DS)
11844 if (tls_mask != 0
11845 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
11846 goto toctprel;
11848 else
11850 /* If we found a GD reloc pair, then we might be
11851 doing a GD->IE transition. */
11852 if (retval == 2)
11854 tls_gd = TLS_TPRELGD;
11855 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11856 goto tls_ldgd_opt;
11858 else if (retval == 3)
11860 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11861 goto tls_ldgd_opt;
11866 break;
11868 case R_PPC64_GOT_TPREL16_HI:
11869 case R_PPC64_GOT_TPREL16_HA:
11870 if (tls_mask != 0
11871 && (tls_mask & TLS_TPREL) == 0)
11873 rel->r_offset -= d_offset;
11874 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
11875 r_type = R_PPC64_NONE;
11876 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11878 break;
11880 case R_PPC64_GOT_TPREL16_DS:
11881 case R_PPC64_GOT_TPREL16_LO_DS:
11882 if (tls_mask != 0
11883 && (tls_mask & TLS_TPREL) == 0)
11885 toctprel:
11886 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
11887 insn &= 31 << 21;
11888 insn |= 0x3c0d0000; /* addis 0,13,0 */
11889 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
11890 r_type = R_PPC64_TPREL16_HA;
11891 if (toc_symndx != 0)
11893 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
11894 rel->r_addend = toc_addend;
11895 /* We changed the symbol. Start over in order to
11896 get h, sym, sec etc. right. */
11897 rel--;
11898 continue;
11900 else
11901 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11903 break;
11905 case R_PPC64_TLS:
11906 if (tls_mask != 0
11907 && (tls_mask & TLS_TPREL) == 0)
11909 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
11910 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
11911 if (insn == 0)
11912 abort ();
11913 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
11914 /* Was PPC64_TLS which sits on insn boundary, now
11915 PPC64_TPREL16_LO which is at low-order half-word. */
11916 rel->r_offset += d_offset;
11917 r_type = R_PPC64_TPREL16_LO;
11918 if (toc_symndx != 0)
11920 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
11921 rel->r_addend = toc_addend;
11922 /* We changed the symbol. Start over in order to
11923 get h, sym, sec etc. right. */
11924 rel--;
11925 continue;
11927 else
11928 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11930 break;
11932 case R_PPC64_GOT_TLSGD16_HI:
11933 case R_PPC64_GOT_TLSGD16_HA:
11934 tls_gd = TLS_TPRELGD;
11935 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11936 goto tls_gdld_hi;
11937 break;
11939 case R_PPC64_GOT_TLSLD16_HI:
11940 case R_PPC64_GOT_TLSLD16_HA:
11941 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11943 tls_gdld_hi:
11944 if ((tls_mask & tls_gd) != 0)
11945 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11946 + R_PPC64_GOT_TPREL16_DS);
11947 else
11949 rel->r_offset -= d_offset;
11950 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
11951 r_type = R_PPC64_NONE;
11953 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11955 break;
11957 case R_PPC64_GOT_TLSGD16:
11958 case R_PPC64_GOT_TLSGD16_LO:
11959 tls_gd = TLS_TPRELGD;
11960 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11961 goto tls_ldgd_opt;
11962 break;
11964 case R_PPC64_GOT_TLSLD16:
11965 case R_PPC64_GOT_TLSLD16_LO:
11966 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11968 unsigned int insn1, insn2, insn3;
11969 bfd_vma offset;
11971 tls_ldgd_opt:
11972 offset = (bfd_vma) -1;
11973 /* If not using the newer R_PPC64_TLSGD/LD to mark
11974 __tls_get_addr calls, we must trust that the call
11975 stays with its arg setup insns, ie. that the next
11976 reloc is the __tls_get_addr call associated with
11977 the current reloc. Edit both insns. */
11978 if (input_section->has_tls_get_addr_call
11979 && rel + 1 < relend
11980 && branch_reloc_hash_match (input_bfd, rel + 1,
11981 htab->tls_get_addr,
11982 htab->tls_get_addr_fd))
11983 offset = rel[1].r_offset;
11984 if ((tls_mask & tls_gd) != 0)
11986 /* IE */
11987 insn1 = bfd_get_32 (output_bfd,
11988 contents + rel->r_offset - d_offset);
11989 insn1 &= (1 << 26) - (1 << 2);
11990 insn1 |= 58 << 26; /* ld */
11991 insn2 = 0x7c636a14; /* add 3,3,13 */
11992 if (offset != (bfd_vma) -1)
11993 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
11994 if ((tls_mask & TLS_EXPLICIT) == 0)
11995 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11996 + R_PPC64_GOT_TPREL16_DS);
11997 else
11998 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
11999 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12001 else
12003 /* LE */
12004 insn1 = 0x3c6d0000; /* addis 3,13,0 */
12005 insn2 = 0x38630000; /* addi 3,3,0 */
12006 if (tls_gd == 0)
12008 /* Was an LD reloc. */
12009 if (toc_symndx)
12010 sec = local_sections[toc_symndx];
12011 for (r_symndx = 0;
12012 r_symndx < symtab_hdr->sh_info;
12013 r_symndx++)
12014 if (local_sections[r_symndx] == sec)
12015 break;
12016 if (r_symndx >= symtab_hdr->sh_info)
12017 r_symndx = STN_UNDEF;
12018 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12019 if (r_symndx != STN_UNDEF)
12020 rel->r_addend -= (local_syms[r_symndx].st_value
12021 + sec->output_offset
12022 + sec->output_section->vma);
12024 else if (toc_symndx != 0)
12026 r_symndx = toc_symndx;
12027 rel->r_addend = toc_addend;
12029 r_type = R_PPC64_TPREL16_HA;
12030 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12031 if (offset != (bfd_vma) -1)
12033 rel[1].r_info = ELF64_R_INFO (r_symndx,
12034 R_PPC64_TPREL16_LO);
12035 rel[1].r_offset = offset + d_offset;
12036 rel[1].r_addend = rel->r_addend;
12039 bfd_put_32 (output_bfd, insn1,
12040 contents + rel->r_offset - d_offset);
12041 if (offset != (bfd_vma) -1)
12043 insn3 = bfd_get_32 (output_bfd,
12044 contents + offset + 4);
12045 if (insn3 == NOP
12046 || insn3 == CROR_151515 || insn3 == CROR_313131)
12048 rel[1].r_offset += 4;
12049 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12050 insn2 = NOP;
12052 bfd_put_32 (output_bfd, insn2, contents + offset);
12054 if ((tls_mask & tls_gd) == 0
12055 && (tls_gd == 0 || toc_symndx != 0))
12057 /* We changed the symbol. Start over in order
12058 to get h, sym, sec etc. right. */
12059 rel--;
12060 continue;
12063 break;
12065 case R_PPC64_TLSGD:
12066 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12068 unsigned int insn2, insn3;
12069 bfd_vma offset = rel->r_offset;
12071 if ((tls_mask & TLS_TPRELGD) != 0)
12073 /* IE */
12074 r_type = R_PPC64_NONE;
12075 insn2 = 0x7c636a14; /* add 3,3,13 */
12077 else
12079 /* LE */
12080 if (toc_symndx != 0)
12082 r_symndx = toc_symndx;
12083 rel->r_addend = toc_addend;
12085 r_type = R_PPC64_TPREL16_LO;
12086 rel->r_offset = offset + d_offset;
12087 insn2 = 0x38630000; /* addi 3,3,0 */
12089 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12090 /* Zap the reloc on the _tls_get_addr call too. */
12091 BFD_ASSERT (offset == rel[1].r_offset);
12092 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12093 insn3 = bfd_get_32 (output_bfd,
12094 contents + offset + 4);
12095 if (insn3 == NOP
12096 || insn3 == CROR_151515 || insn3 == CROR_313131)
12098 rel->r_offset += 4;
12099 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12100 insn2 = NOP;
12102 bfd_put_32 (output_bfd, insn2, contents + offset);
12103 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
12105 rel--;
12106 continue;
12109 break;
12111 case R_PPC64_TLSLD:
12112 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12114 unsigned int insn2, insn3;
12115 bfd_vma offset = rel->r_offset;
12117 if (toc_symndx)
12118 sec = local_sections[toc_symndx];
12119 for (r_symndx = 0;
12120 r_symndx < symtab_hdr->sh_info;
12121 r_symndx++)
12122 if (local_sections[r_symndx] == sec)
12123 break;
12124 if (r_symndx >= symtab_hdr->sh_info)
12125 r_symndx = STN_UNDEF;
12126 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12127 if (r_symndx != STN_UNDEF)
12128 rel->r_addend -= (local_syms[r_symndx].st_value
12129 + sec->output_offset
12130 + sec->output_section->vma);
12132 r_type = R_PPC64_TPREL16_LO;
12133 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12134 rel->r_offset = offset + d_offset;
12135 /* Zap the reloc on the _tls_get_addr call too. */
12136 BFD_ASSERT (offset == rel[1].r_offset);
12137 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12138 insn2 = 0x38630000; /* addi 3,3,0 */
12139 insn3 = bfd_get_32 (output_bfd,
12140 contents + offset + 4);
12141 if (insn3 == NOP
12142 || insn3 == CROR_151515 || insn3 == CROR_313131)
12144 rel->r_offset += 4;
12145 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12146 insn2 = NOP;
12148 bfd_put_32 (output_bfd, insn2, contents + offset);
12149 rel--;
12150 continue;
12152 break;
12154 case R_PPC64_DTPMOD64:
12155 if (rel + 1 < relend
12156 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12157 && rel[1].r_offset == rel->r_offset + 8)
12159 if ((tls_mask & TLS_GD) == 0)
12161 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12162 if ((tls_mask & TLS_TPRELGD) != 0)
12163 r_type = R_PPC64_TPREL64;
12164 else
12166 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12167 r_type = R_PPC64_NONE;
12169 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12172 else
12174 if ((tls_mask & TLS_LD) == 0)
12176 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12177 r_type = R_PPC64_NONE;
12178 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12181 break;
12183 case R_PPC64_TPREL64:
12184 if ((tls_mask & TLS_TPREL) == 0)
12186 r_type = R_PPC64_NONE;
12187 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12189 break;
12192 /* Handle other relocations that tweak non-addend part of insn. */
12193 insn = 0;
12194 max_br_offset = 1 << 25;
12195 addend = rel->r_addend;
12196 switch (r_type)
12198 default:
12199 break;
12201 /* Branch taken prediction relocations. */
12202 case R_PPC64_ADDR14_BRTAKEN:
12203 case R_PPC64_REL14_BRTAKEN:
12204 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
12205 /* Fall thru. */
12207 /* Branch not taken prediction relocations. */
12208 case R_PPC64_ADDR14_BRNTAKEN:
12209 case R_PPC64_REL14_BRNTAKEN:
12210 insn |= bfd_get_32 (output_bfd,
12211 contents + rel->r_offset) & ~(0x01 << 21);
12212 /* Fall thru. */
12214 case R_PPC64_REL14:
12215 max_br_offset = 1 << 15;
12216 /* Fall thru. */
12218 case R_PPC64_REL24:
12219 /* Calls to functions with a different TOC, such as calls to
12220 shared objects, need to alter the TOC pointer. This is
12221 done using a linkage stub. A REL24 branching to these
12222 linkage stubs needs to be followed by a nop, as the nop
12223 will be replaced with an instruction to restore the TOC
12224 base pointer. */
12225 fdh = h;
12226 if (h != NULL
12227 && h->oh != NULL
12228 && h->oh->is_func_descriptor)
12229 fdh = ppc_follow_link (h->oh);
12230 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12231 if (stub_entry != NULL
12232 && (stub_entry->stub_type == ppc_stub_plt_call
12233 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12234 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
12236 bfd_boolean can_plt_call = FALSE;
12238 if (rel->r_offset + 8 <= input_section->size)
12240 unsigned long nop;
12241 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12242 if (nop == NOP
12243 || nop == CROR_151515 || nop == CROR_313131)
12245 if (h != NULL
12246 && (h == htab->tls_get_addr_fd
12247 || h == htab->tls_get_addr)
12248 && !htab->no_tls_get_addr_opt)
12250 /* Special stub used, leave nop alone. */
12252 else
12253 bfd_put_32 (input_bfd, LD_R2_40R1,
12254 contents + rel->r_offset + 4);
12255 can_plt_call = TRUE;
12259 if (!can_plt_call)
12261 if (stub_entry->stub_type == ppc_stub_plt_call)
12263 /* If this is a plain branch rather than a branch
12264 and link, don't require a nop. However, don't
12265 allow tail calls in a shared library as they
12266 will result in r2 being corrupted. */
12267 unsigned long br;
12268 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
12269 if (info->executable && (br & 1) == 0)
12270 can_plt_call = TRUE;
12271 else
12272 stub_entry = NULL;
12274 else if (h != NULL
12275 && strcmp (h->elf.root.root.string,
12276 ".__libc_start_main") == 0)
12278 /* Allow crt1 branch to go via a toc adjusting stub. */
12279 can_plt_call = TRUE;
12281 else
12283 if (strcmp (input_section->output_section->name,
12284 ".init") == 0
12285 || strcmp (input_section->output_section->name,
12286 ".fini") == 0)
12287 (*_bfd_error_handler)
12288 (_("%B(%A+0x%lx): automatic multiple TOCs "
12289 "not supported using your crt files; "
12290 "recompile with -mminimal-toc or upgrade gcc"),
12291 input_bfd,
12292 input_section,
12293 (long) rel->r_offset);
12294 else
12295 (*_bfd_error_handler)
12296 (_("%B(%A+0x%lx): sibling call optimization to `%s' "
12297 "does not allow automatic multiple TOCs; "
12298 "recompile with -mminimal-toc or "
12299 "-fno-optimize-sibling-calls, "
12300 "or make `%s' extern"),
12301 input_bfd,
12302 input_section,
12303 (long) rel->r_offset,
12304 sym_name,
12305 sym_name);
12306 bfd_set_error (bfd_error_bad_value);
12307 ret = FALSE;
12311 if (can_plt_call
12312 && stub_entry->stub_type == ppc_stub_plt_call)
12313 unresolved_reloc = FALSE;
12316 if ((stub_entry == NULL
12317 || stub_entry->stub_type == ppc_stub_long_branch
12318 || stub_entry->stub_type == ppc_stub_plt_branch)
12319 && get_opd_info (sec) != NULL)
12321 /* The branch destination is the value of the opd entry. */
12322 bfd_vma off = (relocation + addend
12323 - sec->output_section->vma
12324 - sec->output_offset);
12325 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12326 if (dest != (bfd_vma) -1)
12328 relocation = dest;
12329 addend = 0;
12333 /* If the branch is out of reach we ought to have a long
12334 branch stub. */
12335 from = (rel->r_offset
12336 + input_section->output_offset
12337 + input_section->output_section->vma);
12339 if (stub_entry != NULL
12340 && (stub_entry->stub_type == ppc_stub_long_branch
12341 || stub_entry->stub_type == ppc_stub_plt_branch)
12342 && (r_type == R_PPC64_ADDR14_BRTAKEN
12343 || r_type == R_PPC64_ADDR14_BRNTAKEN
12344 || (relocation + addend - from + max_br_offset
12345 < 2 * max_br_offset)))
12346 /* Don't use the stub if this branch is in range. */
12347 stub_entry = NULL;
12349 if (stub_entry != NULL)
12351 /* Munge up the value and addend so that we call the stub
12352 rather than the procedure directly. */
12353 relocation = (stub_entry->stub_offset
12354 + stub_entry->stub_sec->output_offset
12355 + stub_entry->stub_sec->output_section->vma);
12356 addend = 0;
12359 if (insn != 0)
12361 if (is_power4)
12363 /* Set 'a' bit. This is 0b00010 in BO field for branch
12364 on CR(BI) insns (BO == 001at or 011at), and 0b01000
12365 for branch on CTR insns (BO == 1a00t or 1a01t). */
12366 if ((insn & (0x14 << 21)) == (0x04 << 21))
12367 insn |= 0x02 << 21;
12368 else if ((insn & (0x14 << 21)) == (0x10 << 21))
12369 insn |= 0x08 << 21;
12370 else
12371 break;
12373 else
12375 /* Invert 'y' bit if not the default. */
12376 if ((bfd_signed_vma) (relocation + addend - from) < 0)
12377 insn ^= 0x01 << 21;
12380 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12383 /* NOP out calls to undefined weak functions.
12384 We can thus call a weak function without first
12385 checking whether the function is defined. */
12386 else if (h != NULL
12387 && h->elf.root.type == bfd_link_hash_undefweak
12388 && h->elf.dynindx == -1
12389 && r_type == R_PPC64_REL24
12390 && relocation == 0
12391 && addend == 0)
12393 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12394 continue;
12396 break;
12399 /* Set `addend'. */
12400 tls_type = 0;
12401 switch (r_type)
12403 default:
12404 (*_bfd_error_handler)
12405 (_("%B: unknown relocation type %d for symbol %s"),
12406 input_bfd, (int) r_type, sym_name);
12408 bfd_set_error (bfd_error_bad_value);
12409 ret = FALSE;
12410 continue;
12412 case R_PPC64_NONE:
12413 case R_PPC64_TLS:
12414 case R_PPC64_TLSGD:
12415 case R_PPC64_TLSLD:
12416 case R_PPC64_GNU_VTINHERIT:
12417 case R_PPC64_GNU_VTENTRY:
12418 continue;
12420 /* GOT16 relocations. Like an ADDR16 using the symbol's
12421 address in the GOT as relocation value instead of the
12422 symbol's value itself. Also, create a GOT entry for the
12423 symbol and put the symbol value there. */
12424 case R_PPC64_GOT_TLSGD16:
12425 case R_PPC64_GOT_TLSGD16_LO:
12426 case R_PPC64_GOT_TLSGD16_HI:
12427 case R_PPC64_GOT_TLSGD16_HA:
12428 tls_type = TLS_TLS | TLS_GD;
12429 goto dogot;
12431 case R_PPC64_GOT_TLSLD16:
12432 case R_PPC64_GOT_TLSLD16_LO:
12433 case R_PPC64_GOT_TLSLD16_HI:
12434 case R_PPC64_GOT_TLSLD16_HA:
12435 tls_type = TLS_TLS | TLS_LD;
12436 goto dogot;
12438 case R_PPC64_GOT_TPREL16_DS:
12439 case R_PPC64_GOT_TPREL16_LO_DS:
12440 case R_PPC64_GOT_TPREL16_HI:
12441 case R_PPC64_GOT_TPREL16_HA:
12442 tls_type = TLS_TLS | TLS_TPREL;
12443 goto dogot;
12445 case R_PPC64_GOT_DTPREL16_DS:
12446 case R_PPC64_GOT_DTPREL16_LO_DS:
12447 case R_PPC64_GOT_DTPREL16_HI:
12448 case R_PPC64_GOT_DTPREL16_HA:
12449 tls_type = TLS_TLS | TLS_DTPREL;
12450 goto dogot;
12452 case R_PPC64_GOT16:
12453 case R_PPC64_GOT16_LO:
12454 case R_PPC64_GOT16_HI:
12455 case R_PPC64_GOT16_HA:
12456 case R_PPC64_GOT16_DS:
12457 case R_PPC64_GOT16_LO_DS:
12458 dogot:
12460 /* Relocation is to the entry for this symbol in the global
12461 offset table. */
12462 asection *got;
12463 bfd_vma *offp;
12464 bfd_vma off;
12465 unsigned long indx = 0;
12466 struct got_entry *ent;
12468 if (tls_type == (TLS_TLS | TLS_LD)
12469 && (h == NULL
12470 || !h->elf.def_dynamic))
12471 ent = ppc64_tlsld_got (input_bfd);
12472 else
12475 if (h != NULL)
12477 bfd_boolean dyn = htab->elf.dynamic_sections_created;
12478 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
12479 &h->elf)
12480 || (info->shared
12481 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
12482 /* This is actually a static link, or it is a
12483 -Bsymbolic link and the symbol is defined
12484 locally, or the symbol was forced to be local
12485 because of a version file. */
12487 else
12489 indx = h->elf.dynindx;
12490 unresolved_reloc = FALSE;
12492 ent = h->elf.got.glist;
12494 else
12496 if (local_got_ents == NULL)
12497 abort ();
12498 ent = local_got_ents[r_symndx];
12501 for (; ent != NULL; ent = ent->next)
12502 if (ent->addend == orig_addend
12503 && ent->owner == input_bfd
12504 && ent->tls_type == tls_type)
12505 break;
12508 if (ent == NULL)
12509 abort ();
12510 if (ent->is_indirect)
12511 ent = ent->got.ent;
12512 offp = &ent->got.offset;
12513 got = ppc64_elf_tdata (ent->owner)->got;
12514 if (got == NULL)
12515 abort ();
12517 /* The offset must always be a multiple of 8. We use the
12518 least significant bit to record whether we have already
12519 processed this entry. */
12520 off = *offp;
12521 if ((off & 1) != 0)
12522 off &= ~1;
12523 else
12525 /* Generate relocs for the dynamic linker, except in
12526 the case of TLSLD where we'll use one entry per
12527 module. */
12528 asection *relgot;
12529 bfd_boolean ifunc;
12531 *offp = off | 1;
12532 relgot = NULL;
12533 ifunc = (h != NULL
12534 ? h->elf.type == STT_GNU_IFUNC
12535 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
12536 if ((info->shared || indx != 0)
12537 && (h == NULL
12538 || (tls_type == (TLS_TLS | TLS_LD)
12539 && !h->elf.def_dynamic)
12540 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12541 || h->elf.root.type != bfd_link_hash_undefweak))
12542 relgot = ppc64_elf_tdata (ent->owner)->relgot;
12543 else if (ifunc)
12544 relgot = htab->reliplt;
12545 if (relgot != NULL)
12547 outrel.r_offset = (got->output_section->vma
12548 + got->output_offset
12549 + off);
12550 outrel.r_addend = addend;
12551 if (tls_type & (TLS_LD | TLS_GD))
12553 outrel.r_addend = 0;
12554 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
12555 if (tls_type == (TLS_TLS | TLS_GD))
12557 loc = relgot->contents;
12558 loc += (relgot->reloc_count++
12559 * sizeof (Elf64_External_Rela));
12560 bfd_elf64_swap_reloca_out (output_bfd,
12561 &outrel, loc);
12562 outrel.r_offset += 8;
12563 outrel.r_addend = addend;
12564 outrel.r_info
12565 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
12568 else if (tls_type == (TLS_TLS | TLS_DTPREL))
12569 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
12570 else if (tls_type == (TLS_TLS | TLS_TPREL))
12571 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
12572 else if (indx != 0)
12573 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
12574 else
12576 if (ifunc)
12577 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12578 else
12579 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12581 /* Write the .got section contents for the sake
12582 of prelink. */
12583 loc = got->contents + off;
12584 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
12585 loc);
12588 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
12590 outrel.r_addend += relocation;
12591 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
12592 outrel.r_addend -= htab->elf.tls_sec->vma;
12594 loc = relgot->contents;
12595 loc += (relgot->reloc_count++
12596 * sizeof (Elf64_External_Rela));
12597 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12600 /* Init the .got section contents here if we're not
12601 emitting a reloc. */
12602 else
12604 relocation += addend;
12605 if (tls_type == (TLS_TLS | TLS_LD))
12606 relocation = 1;
12607 else if (tls_type != 0)
12609 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
12610 if (tls_type == (TLS_TLS | TLS_TPREL))
12611 relocation += DTP_OFFSET - TP_OFFSET;
12613 if (tls_type == (TLS_TLS | TLS_GD))
12615 bfd_put_64 (output_bfd, relocation,
12616 got->contents + off + 8);
12617 relocation = 1;
12621 bfd_put_64 (output_bfd, relocation,
12622 got->contents + off);
12626 if (off >= (bfd_vma) -2)
12627 abort ();
12629 relocation = got->output_section->vma + got->output_offset + off;
12630 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
12632 break;
12634 case R_PPC64_PLT16_HA:
12635 case R_PPC64_PLT16_HI:
12636 case R_PPC64_PLT16_LO:
12637 case R_PPC64_PLT32:
12638 case R_PPC64_PLT64:
12639 /* Relocation is to the entry for this symbol in the
12640 procedure linkage table. */
12642 /* Resolve a PLT reloc against a local symbol directly,
12643 without using the procedure linkage table. */
12644 if (h == NULL)
12645 break;
12647 /* It's possible that we didn't make a PLT entry for this
12648 symbol. This happens when statically linking PIC code,
12649 or when using -Bsymbolic. Go find a match if there is a
12650 PLT entry. */
12651 if (htab->plt != NULL)
12653 struct plt_entry *ent;
12654 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
12655 if (ent->addend == orig_addend
12656 && ent->plt.offset != (bfd_vma) -1)
12658 relocation = (htab->plt->output_section->vma
12659 + htab->plt->output_offset
12660 + ent->plt.offset);
12661 unresolved_reloc = FALSE;
12664 break;
12666 case R_PPC64_TOC:
12667 /* Relocation value is TOC base. */
12668 relocation = TOCstart;
12669 if (r_symndx == STN_UNDEF)
12670 relocation += htab->stub_group[input_section->id].toc_off;
12671 else if (unresolved_reloc)
12673 else if (sec != NULL && sec->id <= htab->top_id)
12674 relocation += htab->stub_group[sec->id].toc_off;
12675 else
12676 unresolved_reloc = TRUE;
12677 goto dodyn;
12679 /* TOC16 relocs. We want the offset relative to the TOC base,
12680 which is the address of the start of the TOC plus 0x8000.
12681 The TOC consists of sections .got, .toc, .tocbss, and .plt,
12682 in this order. */
12683 case R_PPC64_TOC16:
12684 case R_PPC64_TOC16_LO:
12685 case R_PPC64_TOC16_HI:
12686 case R_PPC64_TOC16_DS:
12687 case R_PPC64_TOC16_LO_DS:
12688 case R_PPC64_TOC16_HA:
12689 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
12690 break;
12692 /* Relocate against the beginning of the section. */
12693 case R_PPC64_SECTOFF:
12694 case R_PPC64_SECTOFF_LO:
12695 case R_PPC64_SECTOFF_HI:
12696 case R_PPC64_SECTOFF_DS:
12697 case R_PPC64_SECTOFF_LO_DS:
12698 case R_PPC64_SECTOFF_HA:
12699 if (sec != NULL)
12700 addend -= sec->output_section->vma;
12701 break;
12703 case R_PPC64_REL16:
12704 case R_PPC64_REL16_LO:
12705 case R_PPC64_REL16_HI:
12706 case R_PPC64_REL16_HA:
12707 break;
12709 case R_PPC64_REL14:
12710 case R_PPC64_REL14_BRNTAKEN:
12711 case R_PPC64_REL14_BRTAKEN:
12712 case R_PPC64_REL24:
12713 break;
12715 case R_PPC64_TPREL16:
12716 case R_PPC64_TPREL16_LO:
12717 case R_PPC64_TPREL16_HI:
12718 case R_PPC64_TPREL16_HA:
12719 case R_PPC64_TPREL16_DS:
12720 case R_PPC64_TPREL16_LO_DS:
12721 case R_PPC64_TPREL16_HIGHER:
12722 case R_PPC64_TPREL16_HIGHERA:
12723 case R_PPC64_TPREL16_HIGHEST:
12724 case R_PPC64_TPREL16_HIGHESTA:
12725 if (h != NULL
12726 && h->elf.root.type == bfd_link_hash_undefweak
12727 && h->elf.dynindx == -1)
12729 /* Make this relocation against an undefined weak symbol
12730 resolve to zero. This is really just a tweak, since
12731 code using weak externs ought to check that they are
12732 defined before using them. */
12733 bfd_byte *p = contents + rel->r_offset - d_offset;
12735 insn = bfd_get_32 (output_bfd, p);
12736 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
12737 if (insn != 0)
12738 bfd_put_32 (output_bfd, insn, p);
12739 break;
12741 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
12742 if (info->shared)
12743 /* The TPREL16 relocs shouldn't really be used in shared
12744 libs as they will result in DT_TEXTREL being set, but
12745 support them anyway. */
12746 goto dodyn;
12747 break;
12749 case R_PPC64_DTPREL16:
12750 case R_PPC64_DTPREL16_LO:
12751 case R_PPC64_DTPREL16_HI:
12752 case R_PPC64_DTPREL16_HA:
12753 case R_PPC64_DTPREL16_DS:
12754 case R_PPC64_DTPREL16_LO_DS:
12755 case R_PPC64_DTPREL16_HIGHER:
12756 case R_PPC64_DTPREL16_HIGHERA:
12757 case R_PPC64_DTPREL16_HIGHEST:
12758 case R_PPC64_DTPREL16_HIGHESTA:
12759 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
12760 break;
12762 case R_PPC64_DTPMOD64:
12763 relocation = 1;
12764 addend = 0;
12765 goto dodyn;
12767 case R_PPC64_TPREL64:
12768 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
12769 goto dodyn;
12771 case R_PPC64_DTPREL64:
12772 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
12773 /* Fall thru */
12775 /* Relocations that may need to be propagated if this is a
12776 dynamic object. */
12777 case R_PPC64_REL30:
12778 case R_PPC64_REL32:
12779 case R_PPC64_REL64:
12780 case R_PPC64_ADDR14:
12781 case R_PPC64_ADDR14_BRNTAKEN:
12782 case R_PPC64_ADDR14_BRTAKEN:
12783 case R_PPC64_ADDR16:
12784 case R_PPC64_ADDR16_DS:
12785 case R_PPC64_ADDR16_HA:
12786 case R_PPC64_ADDR16_HI:
12787 case R_PPC64_ADDR16_HIGHER:
12788 case R_PPC64_ADDR16_HIGHERA:
12789 case R_PPC64_ADDR16_HIGHEST:
12790 case R_PPC64_ADDR16_HIGHESTA:
12791 case R_PPC64_ADDR16_LO:
12792 case R_PPC64_ADDR16_LO_DS:
12793 case R_PPC64_ADDR24:
12794 case R_PPC64_ADDR32:
12795 case R_PPC64_ADDR64:
12796 case R_PPC64_UADDR16:
12797 case R_PPC64_UADDR32:
12798 case R_PPC64_UADDR64:
12799 dodyn:
12800 if ((input_section->flags & SEC_ALLOC) == 0)
12801 break;
12803 if (NO_OPD_RELOCS && is_opd)
12804 break;
12806 if ((info->shared
12807 && (h == NULL
12808 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12809 || h->elf.root.type != bfd_link_hash_undefweak)
12810 && (must_be_dyn_reloc (info, r_type)
12811 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
12812 || (ELIMINATE_COPY_RELOCS
12813 && !info->shared
12814 && h != NULL
12815 && h->elf.dynindx != -1
12816 && !h->elf.non_got_ref
12817 && !h->elf.def_regular)
12818 || (!info->shared
12819 && (h != NULL
12820 ? h->elf.type == STT_GNU_IFUNC
12821 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
12823 bfd_boolean skip, relocate;
12824 asection *sreloc;
12825 bfd_vma out_off;
12827 /* When generating a dynamic object, these relocations
12828 are copied into the output file to be resolved at run
12829 time. */
12831 skip = FALSE;
12832 relocate = FALSE;
12834 out_off = _bfd_elf_section_offset (output_bfd, info,
12835 input_section, rel->r_offset);
12836 if (out_off == (bfd_vma) -1)
12837 skip = TRUE;
12838 else if (out_off == (bfd_vma) -2)
12839 skip = TRUE, relocate = TRUE;
12840 out_off += (input_section->output_section->vma
12841 + input_section->output_offset);
12842 outrel.r_offset = out_off;
12843 outrel.r_addend = rel->r_addend;
12845 /* Optimize unaligned reloc use. */
12846 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
12847 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
12848 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
12849 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
12850 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
12851 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
12852 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
12853 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
12854 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
12856 if (skip)
12857 memset (&outrel, 0, sizeof outrel);
12858 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
12859 && !is_opd
12860 && r_type != R_PPC64_TOC)
12861 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
12862 else
12864 /* This symbol is local, or marked to become local,
12865 or this is an opd section reloc which must point
12866 at a local function. */
12867 outrel.r_addend += relocation;
12868 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
12870 if (is_opd && h != NULL)
12872 /* Lie about opd entries. This case occurs
12873 when building shared libraries and we
12874 reference a function in another shared
12875 lib. The same thing happens for a weak
12876 definition in an application that's
12877 overridden by a strong definition in a
12878 shared lib. (I believe this is a generic
12879 bug in binutils handling of weak syms.)
12880 In these cases we won't use the opd
12881 entry in this lib. */
12882 unresolved_reloc = FALSE;
12884 if (!is_opd
12885 && r_type == R_PPC64_ADDR64
12886 && (h != NULL
12887 ? h->elf.type == STT_GNU_IFUNC
12888 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
12889 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12890 else
12892 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12894 /* We need to relocate .opd contents for ld.so.
12895 Prelink also wants simple and consistent rules
12896 for relocs. This make all RELATIVE relocs have
12897 *r_offset equal to r_addend. */
12898 relocate = TRUE;
12901 else
12903 long indx = 0;
12905 if (h != NULL
12906 ? h->elf.type == STT_GNU_IFUNC
12907 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12909 (*_bfd_error_handler)
12910 (_("%B(%A+0x%lx): relocation %s for indirect "
12911 "function %s unsupported"),
12912 input_bfd,
12913 input_section,
12914 (long) rel->r_offset,
12915 ppc64_elf_howto_table[r_type]->name,
12916 sym_name);
12917 ret = FALSE;
12919 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
12921 else if (sec == NULL || sec->owner == NULL)
12923 bfd_set_error (bfd_error_bad_value);
12924 return FALSE;
12926 else
12928 asection *osec;
12930 osec = sec->output_section;
12931 indx = elf_section_data (osec)->dynindx;
12933 if (indx == 0)
12935 if ((osec->flags & SEC_READONLY) == 0
12936 && htab->elf.data_index_section != NULL)
12937 osec = htab->elf.data_index_section;
12938 else
12939 osec = htab->elf.text_index_section;
12940 indx = elf_section_data (osec)->dynindx;
12942 BFD_ASSERT (indx != 0);
12944 /* We are turning this relocation into one
12945 against a section symbol, so subtract out
12946 the output section's address but not the
12947 offset of the input section in the output
12948 section. */
12949 outrel.r_addend -= osec->vma;
12952 outrel.r_info = ELF64_R_INFO (indx, r_type);
12956 sreloc = elf_section_data (input_section)->sreloc;
12957 if (!htab->elf.dynamic_sections_created)
12958 sreloc = htab->reliplt;
12959 if (sreloc == NULL)
12960 abort ();
12962 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
12963 >= sreloc->size)
12964 abort ();
12965 loc = sreloc->contents;
12966 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
12967 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12969 /* If this reloc is against an external symbol, it will
12970 be computed at runtime, so there's no need to do
12971 anything now. However, for the sake of prelink ensure
12972 that the section contents are a known value. */
12973 if (! relocate)
12975 unresolved_reloc = FALSE;
12976 /* The value chosen here is quite arbitrary as ld.so
12977 ignores section contents except for the special
12978 case of .opd where the contents might be accessed
12979 before relocation. Choose zero, as that won't
12980 cause reloc overflow. */
12981 relocation = 0;
12982 addend = 0;
12983 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
12984 to improve backward compatibility with older
12985 versions of ld. */
12986 if (r_type == R_PPC64_ADDR64)
12987 addend = outrel.r_addend;
12988 /* Adjust pc_relative relocs to have zero in *r_offset. */
12989 else if (ppc64_elf_howto_table[r_type]->pc_relative)
12990 addend = (input_section->output_section->vma
12991 + input_section->output_offset
12992 + rel->r_offset);
12995 break;
12997 case R_PPC64_COPY:
12998 case R_PPC64_GLOB_DAT:
12999 case R_PPC64_JMP_SLOT:
13000 case R_PPC64_JMP_IREL:
13001 case R_PPC64_RELATIVE:
13002 /* We shouldn't ever see these dynamic relocs in relocatable
13003 files. */
13004 /* Fall through. */
13006 case R_PPC64_PLTGOT16:
13007 case R_PPC64_PLTGOT16_DS:
13008 case R_PPC64_PLTGOT16_HA:
13009 case R_PPC64_PLTGOT16_HI:
13010 case R_PPC64_PLTGOT16_LO:
13011 case R_PPC64_PLTGOT16_LO_DS:
13012 case R_PPC64_PLTREL32:
13013 case R_PPC64_PLTREL64:
13014 /* These ones haven't been implemented yet. */
13016 (*_bfd_error_handler)
13017 (_("%B: relocation %s is not supported for symbol %s."),
13018 input_bfd,
13019 ppc64_elf_howto_table[r_type]->name, sym_name);
13021 bfd_set_error (bfd_error_invalid_operation);
13022 ret = FALSE;
13023 continue;
13026 /* Multi-instruction sequences that access the TOC can be
13027 optimized, eg. addis ra,r2,0; addi rb,ra,x;
13028 to nop; addi rb,r2,x; */
13029 switch (r_type)
13031 default:
13032 break;
13034 case R_PPC64_GOT_TLSLD16_HI:
13035 case R_PPC64_GOT_TLSGD16_HI:
13036 case R_PPC64_GOT_TPREL16_HI:
13037 case R_PPC64_GOT_DTPREL16_HI:
13038 case R_PPC64_GOT16_HI:
13039 case R_PPC64_TOC16_HI:
13040 /* These relocs would only be useful if building up an
13041 offset to later add to r2, perhaps in an indexed
13042 addressing mode instruction. Don't try to optimize.
13043 Unfortunately, the possibility of someone building up an
13044 offset like this or even with the HA relocs, means that
13045 we need to check the high insn when optimizing the low
13046 insn. */
13047 break;
13049 case R_PPC64_GOT_TLSLD16_HA:
13050 case R_PPC64_GOT_TLSGD16_HA:
13051 case R_PPC64_GOT_TPREL16_HA:
13052 case R_PPC64_GOT_DTPREL16_HA:
13053 case R_PPC64_GOT16_HA:
13054 case R_PPC64_TOC16_HA:
13055 /* nop is done later. */
13056 break;
13058 case R_PPC64_GOT_TLSLD16_LO:
13059 case R_PPC64_GOT_TLSGD16_LO:
13060 case R_PPC64_GOT_TPREL16_LO_DS:
13061 case R_PPC64_GOT_DTPREL16_LO_DS:
13062 case R_PPC64_GOT16_LO:
13063 case R_PPC64_GOT16_LO_DS:
13064 case R_PPC64_TOC16_LO:
13065 case R_PPC64_TOC16_LO_DS:
13066 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000)
13068 bfd_byte *p = contents + (rel->r_offset & ~3);
13069 insn = bfd_get_32 (input_bfd, p);
13070 if ((insn & (0x3f << 26)) == 14u << 26 /* addi */
13071 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
13072 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
13073 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
13074 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
13075 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
13076 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
13077 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
13078 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
13079 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
13080 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
13081 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
13082 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
13083 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
13084 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
13085 && (insn & 3) != 1)
13086 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
13087 && ((insn & 3) == 0 || (insn & 3) == 3)))
13089 unsigned int reg = (insn >> 16) & 0x1f;
13090 const Elf_Internal_Rela *ha;
13091 bfd_boolean match_addend;
13093 match_addend = (sym != NULL
13094 && ELF_ST_TYPE (sym->st_info) == STT_SECTION);
13095 ha = ha_reloc_match (relocs, rel, &reg, match_addend,
13096 input_bfd, contents);
13097 if (ha != NULL)
13099 insn &= ~(0x1f << 16);
13100 insn |= reg << 16;
13101 bfd_put_32 (input_bfd, insn, p);
13102 if (ha_opt == NULL)
13104 ha_opt = bfd_zmalloc (input_section->reloc_count);
13105 if (ha_opt == NULL)
13106 return FALSE;
13108 ha_opt[ha - relocs] = 1;
13110 else
13111 /* If we don't find a matching high part insn,
13112 something is fishy. Refuse to nop any high
13113 part insn in this section. */
13114 no_ha_opt = TRUE;
13117 break;
13120 /* Do any further special processing. */
13121 switch (r_type)
13123 default:
13124 break;
13126 case R_PPC64_ADDR16_HA:
13127 case R_PPC64_REL16_HA:
13128 case R_PPC64_ADDR16_HIGHERA:
13129 case R_PPC64_ADDR16_HIGHESTA:
13130 case R_PPC64_TOC16_HA:
13131 case R_PPC64_SECTOFF_HA:
13132 case R_PPC64_TPREL16_HA:
13133 case R_PPC64_DTPREL16_HA:
13134 case R_PPC64_TPREL16_HIGHER:
13135 case R_PPC64_TPREL16_HIGHERA:
13136 case R_PPC64_TPREL16_HIGHEST:
13137 case R_PPC64_TPREL16_HIGHESTA:
13138 case R_PPC64_DTPREL16_HIGHER:
13139 case R_PPC64_DTPREL16_HIGHERA:
13140 case R_PPC64_DTPREL16_HIGHEST:
13141 case R_PPC64_DTPREL16_HIGHESTA:
13142 /* It's just possible that this symbol is a weak symbol
13143 that's not actually defined anywhere. In that case,
13144 'sec' would be NULL, and we should leave the symbol
13145 alone (it will be set to zero elsewhere in the link). */
13146 if (sec == NULL)
13147 break;
13148 /* Fall thru */
13150 case R_PPC64_GOT16_HA:
13151 case R_PPC64_PLTGOT16_HA:
13152 case R_PPC64_PLT16_HA:
13153 case R_PPC64_GOT_TLSGD16_HA:
13154 case R_PPC64_GOT_TLSLD16_HA:
13155 case R_PPC64_GOT_TPREL16_HA:
13156 case R_PPC64_GOT_DTPREL16_HA:
13157 /* Add 0x10000 if sign bit in 0:15 is set.
13158 Bits 0:15 are not used. */
13159 addend += 0x8000;
13160 break;
13162 case R_PPC64_ADDR16_DS:
13163 case R_PPC64_ADDR16_LO_DS:
13164 case R_PPC64_GOT16_DS:
13165 case R_PPC64_GOT16_LO_DS:
13166 case R_PPC64_PLT16_LO_DS:
13167 case R_PPC64_SECTOFF_DS:
13168 case R_PPC64_SECTOFF_LO_DS:
13169 case R_PPC64_TOC16_DS:
13170 case R_PPC64_TOC16_LO_DS:
13171 case R_PPC64_PLTGOT16_DS:
13172 case R_PPC64_PLTGOT16_LO_DS:
13173 case R_PPC64_GOT_TPREL16_DS:
13174 case R_PPC64_GOT_TPREL16_LO_DS:
13175 case R_PPC64_GOT_DTPREL16_DS:
13176 case R_PPC64_GOT_DTPREL16_LO_DS:
13177 case R_PPC64_TPREL16_DS:
13178 case R_PPC64_TPREL16_LO_DS:
13179 case R_PPC64_DTPREL16_DS:
13180 case R_PPC64_DTPREL16_LO_DS:
13181 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13182 mask = 3;
13183 /* If this reloc is against an lq insn, then the value must be
13184 a multiple of 16. This is somewhat of a hack, but the
13185 "correct" way to do this by defining _DQ forms of all the
13186 _DS relocs bloats all reloc switches in this file. It
13187 doesn't seem to make much sense to use any of these relocs
13188 in data, so testing the insn should be safe. */
13189 if ((insn & (0x3f << 26)) == (56u << 26))
13190 mask = 15;
13191 if (((relocation + addend) & mask) != 0)
13193 (*_bfd_error_handler)
13194 (_("%B(%A+0x%lx): error: %s not a multiple of %u"),
13195 input_bfd, input_section, (long) rel->r_offset,
13196 ppc64_elf_howto_table[r_type]->name,
13197 mask + 1);
13198 bfd_set_error (bfd_error_bad_value);
13199 ret = FALSE;
13200 continue;
13202 break;
13205 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13206 because such sections are not SEC_ALLOC and thus ld.so will
13207 not process them. */
13208 if (unresolved_reloc
13209 && !((input_section->flags & SEC_DEBUGGING) != 0
13210 && h->elf.def_dynamic))
13212 (*_bfd_error_handler)
13213 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
13214 input_bfd,
13215 input_section,
13216 (long) rel->r_offset,
13217 ppc64_elf_howto_table[(int) r_type]->name,
13218 h->elf.root.root.string);
13219 ret = FALSE;
13222 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
13223 input_bfd,
13224 input_section,
13225 contents,
13226 rel->r_offset,
13227 relocation,
13228 addend);
13230 if (r != bfd_reloc_ok)
13232 if (sym_name == NULL)
13233 sym_name = "(null)";
13234 if (r == bfd_reloc_overflow)
13236 if (warned)
13237 continue;
13238 if (h != NULL
13239 && h->elf.root.type == bfd_link_hash_undefweak
13240 && ppc64_elf_howto_table[r_type]->pc_relative)
13242 /* Assume this is a call protected by other code that
13243 detects the symbol is undefined. If this is the case,
13244 we can safely ignore the overflow. If not, the
13245 program is hosed anyway, and a little warning isn't
13246 going to help. */
13248 continue;
13251 if (!((*info->callbacks->reloc_overflow)
13252 (info, (h ? &h->elf.root : NULL), sym_name,
13253 ppc64_elf_howto_table[r_type]->name,
13254 orig_addend, input_bfd, input_section, rel->r_offset)))
13255 return FALSE;
13257 else
13259 (*_bfd_error_handler)
13260 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
13261 input_bfd,
13262 input_section,
13263 (long) rel->r_offset,
13264 ppc64_elf_howto_table[r_type]->name,
13265 sym_name,
13266 (int) r);
13267 ret = FALSE;
13272 if (ha_opt != NULL)
13274 if (!no_ha_opt)
13276 unsigned char *opt = ha_opt;
13277 rel = relocs;
13278 relend = relocs + input_section->reloc_count;
13279 for (; rel < relend; opt++, rel++)
13280 if (*opt != 0)
13282 bfd_byte *p = contents + (rel->r_offset & ~3);
13283 bfd_put_32 (input_bfd, NOP, p);
13286 free (ha_opt);
13289 /* If we're emitting relocations, then shortly after this function
13290 returns, reloc offsets and addends for this section will be
13291 adjusted. Worse, reloc symbol indices will be for the output
13292 file rather than the input. Save a copy of the relocs for
13293 opd_entry_value. */
13294 if (is_opd && (info->emitrelocations || info->relocatable))
13296 bfd_size_type amt;
13297 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13298 rel = bfd_alloc (input_bfd, amt);
13299 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13300 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13301 if (rel == NULL)
13302 return FALSE;
13303 memcpy (rel, relocs, amt);
13305 return ret;
13308 /* Adjust the value of any local symbols in opd sections. */
13310 static int
13311 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13312 const char *name ATTRIBUTE_UNUSED,
13313 Elf_Internal_Sym *elfsym,
13314 asection *input_sec,
13315 struct elf_link_hash_entry *h)
13317 struct _opd_sec_data *opd;
13318 long adjust;
13319 bfd_vma value;
13321 if (h != NULL)
13322 return 1;
13324 opd = get_opd_info (input_sec);
13325 if (opd == NULL || opd->adjust == NULL)
13326 return 1;
13328 value = elfsym->st_value - input_sec->output_offset;
13329 if (!info->relocatable)
13330 value -= input_sec->output_section->vma;
13332 adjust = opd->adjust[value / 8];
13333 if (adjust == -1)
13334 return 2;
13336 elfsym->st_value += adjust;
13337 return 1;
13340 /* Finish up dynamic symbol handling. We set the contents of various
13341 dynamic sections here. */
13343 static bfd_boolean
13344 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13345 struct bfd_link_info *info,
13346 struct elf_link_hash_entry *h,
13347 Elf_Internal_Sym *sym)
13349 struct ppc_link_hash_table *htab;
13350 struct plt_entry *ent;
13351 Elf_Internal_Rela rela;
13352 bfd_byte *loc;
13354 htab = ppc_hash_table (info);
13355 if (htab == NULL)
13356 return FALSE;
13358 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13359 if (ent->plt.offset != (bfd_vma) -1)
13361 /* This symbol has an entry in the procedure linkage
13362 table. Set it up. */
13363 if (!htab->elf.dynamic_sections_created
13364 || h->dynindx == -1)
13366 BFD_ASSERT (h->type == STT_GNU_IFUNC
13367 && h->def_regular
13368 && (h->root.type == bfd_link_hash_defined
13369 || h->root.type == bfd_link_hash_defweak));
13370 rela.r_offset = (htab->iplt->output_section->vma
13371 + htab->iplt->output_offset
13372 + ent->plt.offset);
13373 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13374 rela.r_addend = (h->root.u.def.value
13375 + h->root.u.def.section->output_offset
13376 + h->root.u.def.section->output_section->vma
13377 + ent->addend);
13378 loc = (htab->reliplt->contents
13379 + (htab->reliplt->reloc_count++
13380 * sizeof (Elf64_External_Rela)));
13382 else
13384 rela.r_offset = (htab->plt->output_section->vma
13385 + htab->plt->output_offset
13386 + ent->plt.offset);
13387 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13388 rela.r_addend = ent->addend;
13389 loc = (htab->relplt->contents
13390 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13391 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
13393 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13396 if (h->needs_copy)
13398 /* This symbol needs a copy reloc. Set it up. */
13400 if (h->dynindx == -1
13401 || (h->root.type != bfd_link_hash_defined
13402 && h->root.type != bfd_link_hash_defweak)
13403 || htab->relbss == NULL)
13404 abort ();
13406 rela.r_offset = (h->root.u.def.value
13407 + h->root.u.def.section->output_section->vma
13408 + h->root.u.def.section->output_offset);
13409 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13410 rela.r_addend = 0;
13411 loc = htab->relbss->contents;
13412 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
13413 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13416 /* Mark some specially defined symbols as absolute. */
13417 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13418 sym->st_shndx = SHN_ABS;
13420 return TRUE;
13423 /* Used to decide how to sort relocs in an optimal manner for the
13424 dynamic linker, before writing them out. */
13426 static enum elf_reloc_type_class
13427 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
13429 enum elf_ppc64_reloc_type r_type;
13431 r_type = ELF64_R_TYPE (rela->r_info);
13432 switch (r_type)
13434 case R_PPC64_RELATIVE:
13435 return reloc_class_relative;
13436 case R_PPC64_JMP_SLOT:
13437 return reloc_class_plt;
13438 case R_PPC64_COPY:
13439 return reloc_class_copy;
13440 default:
13441 return reloc_class_normal;
13445 /* Finish up the dynamic sections. */
13447 static bfd_boolean
13448 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13449 struct bfd_link_info *info)
13451 struct ppc_link_hash_table *htab;
13452 bfd *dynobj;
13453 asection *sdyn;
13455 htab = ppc_hash_table (info);
13456 if (htab == NULL)
13457 return FALSE;
13459 dynobj = htab->elf.dynobj;
13460 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13462 if (htab->elf.dynamic_sections_created)
13464 Elf64_External_Dyn *dyncon, *dynconend;
13466 if (sdyn == NULL || htab->got == NULL)
13467 abort ();
13469 dyncon = (Elf64_External_Dyn *) sdyn->contents;
13470 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
13471 for (; dyncon < dynconend; dyncon++)
13473 Elf_Internal_Dyn dyn;
13474 asection *s;
13476 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13478 switch (dyn.d_tag)
13480 default:
13481 continue;
13483 case DT_PPC64_GLINK:
13484 s = htab->glink;
13485 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13486 /* We stupidly defined DT_PPC64_GLINK to be the start
13487 of glink rather than the first entry point, which is
13488 what ld.so needs, and now have a bigger stub to
13489 support automatic multiple TOCs. */
13490 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
13491 break;
13493 case DT_PPC64_OPD:
13494 s = bfd_get_section_by_name (output_bfd, ".opd");
13495 if (s == NULL)
13496 continue;
13497 dyn.d_un.d_ptr = s->vma;
13498 break;
13500 case DT_PPC64_OPDSZ:
13501 s = bfd_get_section_by_name (output_bfd, ".opd");
13502 if (s == NULL)
13503 continue;
13504 dyn.d_un.d_val = s->size;
13505 break;
13507 case DT_PLTGOT:
13508 s = htab->plt;
13509 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13510 break;
13512 case DT_JMPREL:
13513 s = htab->relplt;
13514 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13515 break;
13517 case DT_PLTRELSZ:
13518 dyn.d_un.d_val = htab->relplt->size;
13519 break;
13521 case DT_RELASZ:
13522 /* Don't count procedure linkage table relocs in the
13523 overall reloc count. */
13524 s = htab->relplt;
13525 if (s == NULL)
13526 continue;
13527 dyn.d_un.d_val -= s->size;
13528 break;
13530 case DT_RELA:
13531 /* We may not be using the standard ELF linker script.
13532 If .rela.plt is the first .rela section, we adjust
13533 DT_RELA to not include it. */
13534 s = htab->relplt;
13535 if (s == NULL)
13536 continue;
13537 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
13538 continue;
13539 dyn.d_un.d_ptr += s->size;
13540 break;
13543 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
13547 if (htab->got != NULL && htab->got->size != 0)
13549 /* Fill in the first entry in the global offset table.
13550 We use it to hold the link-time TOCbase. */
13551 bfd_put_64 (output_bfd,
13552 elf_gp (output_bfd) + TOC_BASE_OFF,
13553 htab->got->contents);
13555 /* Set .got entry size. */
13556 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
13559 if (htab->plt != NULL && htab->plt->size != 0)
13561 /* Set .plt entry size. */
13562 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
13563 = PLT_ENTRY_SIZE;
13566 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
13567 brlt ourselves if emitrelocations. */
13568 if (htab->brlt != NULL
13569 && htab->brlt->reloc_count != 0
13570 && !_bfd_elf_link_output_relocs (output_bfd,
13571 htab->brlt,
13572 elf_section_data (htab->brlt)->rela.hdr,
13573 elf_section_data (htab->brlt)->relocs,
13574 NULL))
13575 return FALSE;
13577 if (htab->glink != NULL
13578 && htab->glink->reloc_count != 0
13579 && !_bfd_elf_link_output_relocs (output_bfd,
13580 htab->glink,
13581 elf_section_data (htab->glink)->rela.hdr,
13582 elf_section_data (htab->glink)->relocs,
13583 NULL))
13584 return FALSE;
13586 /* We need to handle writing out multiple GOT sections ourselves,
13587 since we didn't add them to DYNOBJ. We know dynobj is the first
13588 bfd. */
13589 while ((dynobj = dynobj->link_next) != NULL)
13591 asection *s;
13593 if (!is_ppc64_elf (dynobj))
13594 continue;
13596 s = ppc64_elf_tdata (dynobj)->got;
13597 if (s != NULL
13598 && s->size != 0
13599 && s->output_section != bfd_abs_section_ptr
13600 && !bfd_set_section_contents (output_bfd, s->output_section,
13601 s->contents, s->output_offset,
13602 s->size))
13603 return FALSE;
13604 s = ppc64_elf_tdata (dynobj)->relgot;
13605 if (s != NULL
13606 && s->size != 0
13607 && s->output_section != bfd_abs_section_ptr
13608 && !bfd_set_section_contents (output_bfd, s->output_section,
13609 s->contents, s->output_offset,
13610 s->size))
13611 return FALSE;
13614 return TRUE;
13617 #include "elf64-target.h"