Mention bug #.
[binutils.git] / bfd / elf64-ppc.c
blob35f7cdf6348d7bc83a26a504b2451684676a1203
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
3 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 <amodra@bigpond.net.au>
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 2 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. */
24 /* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
28 #include "bfd.h"
29 #include "sysdep.h"
30 #include "bfdlink.h"
31 #include "libbfd.h"
32 #include "elf-bfd.h"
33 #include "elf/ppc64.h"
34 #include "elf64-ppc.h"
36 static bfd_reloc_status_type ppc64_elf_ha_reloc
37 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
38 static bfd_reloc_status_type ppc64_elf_branch_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_toc_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc64_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_vma opd_entry_value
55 (asection *, bfd_vma, asection **, bfd_vma *);
57 #define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
58 #define TARGET_LITTLE_NAME "elf64-powerpcle"
59 #define TARGET_BIG_SYM bfd_elf64_powerpc_vec
60 #define TARGET_BIG_NAME "elf64-powerpc"
61 #define ELF_ARCH bfd_arch_powerpc
62 #define ELF_MACHINE_CODE EM_PPC64
63 #define ELF_MAXPAGESIZE 0x10000
64 #define ELF_COMMONPAGESIZE 0x1000
65 #define elf_info_to_howto ppc64_elf_info_to_howto
67 #define elf_backend_want_got_sym 0
68 #define elf_backend_want_plt_sym 0
69 #define elf_backend_plt_alignment 3
70 #define elf_backend_plt_not_loaded 1
71 #define elf_backend_got_header_size 8
72 #define elf_backend_can_gc_sections 1
73 #define elf_backend_can_refcount 1
74 #define elf_backend_rela_normal 1
76 #define bfd_elf64_mkobject ppc64_elf_mkobject
77 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
78 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
79 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
80 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
81 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
82 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
84 #define elf_backend_object_p ppc64_elf_object_p
85 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
86 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
87 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
88 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
89 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
90 #define elf_backend_check_directives ppc64_elf_check_directives
91 #define elf_backend_as_needed_cleanup ppc64_elf_as_needed_cleanup
92 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
93 #define elf_backend_check_relocs ppc64_elf_check_relocs
94 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
95 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
96 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
97 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
98 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
99 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
100 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
101 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
102 #define elf_backend_action_discarded ppc64_elf_action_discarded
103 #define elf_backend_relocate_section ppc64_elf_relocate_section
104 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
105 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
106 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
107 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
108 #define elf_backend_special_sections ppc64_elf_special_sections
110 /* The name of the dynamic interpreter. This is put in the .interp
111 section. */
112 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
114 /* The size in bytes of an entry in the procedure linkage table. */
115 #define PLT_ENTRY_SIZE 24
117 /* The initial size of the plt reserved for the dynamic linker. */
118 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
120 /* TOC base pointers offset from start of TOC. */
121 #define TOC_BASE_OFF 0x8000
123 /* Offset of tp and dtp pointers from start of TLS block. */
124 #define TP_OFFSET 0x7000
125 #define DTP_OFFSET 0x8000
127 /* .plt call stub instructions. The normal stub is like this, but
128 sometimes the .plt entry crosses a 64k boundary and we need to
129 insert an addis to adjust r12. */
130 #define PLT_CALL_STUB_SIZE (7*4)
131 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
132 #define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
133 #define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
134 #define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
135 #define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
136 /* ld %r11,xxx+16@l(%r12) */
137 #define BCTR 0x4e800420 /* bctr */
140 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,off@ha */
141 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
142 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
144 #define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
146 /* glink call stub instructions. We enter with the index in R0. */
147 #define GLINK_CALL_STUB_SIZE (16*4)
148 /* 0: */
149 /* .quad plt0-1f */
150 /* __glink: */
151 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
152 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
153 /* 1: */
154 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
155 #define LD_R2_M16R11 0xe84bfff0 /* ld %2,(0b-1b)(%11) */
156 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
157 #define ADD_R12_R2_R11 0x7d825a14 /* add %12,%2,%11 */
158 /* ld %11,0(%12) */
159 /* ld %2,8(%12) */
160 /* mtctr %11 */
161 /* ld %11,16(%12) */
162 /* bctr */
164 /* Pad with this. */
165 #define NOP 0x60000000
167 /* Some other nops. */
168 #define CROR_151515 0x4def7b82
169 #define CROR_313131 0x4ffffb82
171 /* .glink entries for the first 32k functions are two instructions. */
172 #define LI_R0_0 0x38000000 /* li %r0,0 */
173 #define B_DOT 0x48000000 /* b . */
175 /* After that, we need two instructions to load the index, followed by
176 a branch. */
177 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
178 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
180 /* Instructions used by the save and restore reg functions. */
181 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
182 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
183 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
184 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
185 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
186 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
187 #define LI_R12_0 0x39800000 /* li %r12,0 */
188 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
189 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
190 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
191 #define BLR 0x4e800020 /* blr */
193 /* Since .opd is an array of descriptors and each entry will end up
194 with identical R_PPC64_RELATIVE relocs, there is really no need to
195 propagate .opd relocs; The dynamic linker should be taught to
196 relocate .opd without reloc entries. */
197 #ifndef NO_OPD_RELOCS
198 #define NO_OPD_RELOCS 0
199 #endif
201 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
203 /* Relocation HOWTO's. */
204 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
206 static reloc_howto_type ppc64_elf_howto_raw[] = {
207 /* This reloc does nothing. */
208 HOWTO (R_PPC64_NONE, /* type */
209 0, /* rightshift */
210 2, /* size (0 = byte, 1 = short, 2 = long) */
211 32, /* bitsize */
212 FALSE, /* pc_relative */
213 0, /* bitpos */
214 complain_overflow_dont, /* complain_on_overflow */
215 bfd_elf_generic_reloc, /* special_function */
216 "R_PPC64_NONE", /* name */
217 FALSE, /* partial_inplace */
218 0, /* src_mask */
219 0, /* dst_mask */
220 FALSE), /* pcrel_offset */
222 /* A standard 32 bit relocation. */
223 HOWTO (R_PPC64_ADDR32, /* type */
224 0, /* rightshift */
225 2, /* size (0 = byte, 1 = short, 2 = long) */
226 32, /* bitsize */
227 FALSE, /* pc_relative */
228 0, /* bitpos */
229 complain_overflow_bitfield, /* complain_on_overflow */
230 bfd_elf_generic_reloc, /* special_function */
231 "R_PPC64_ADDR32", /* name */
232 FALSE, /* partial_inplace */
233 0, /* src_mask */
234 0xffffffff, /* dst_mask */
235 FALSE), /* pcrel_offset */
237 /* An absolute 26 bit branch; the lower two bits must be zero.
238 FIXME: we don't check that, we just clear them. */
239 HOWTO (R_PPC64_ADDR24, /* type */
240 0, /* rightshift */
241 2, /* size (0 = byte, 1 = short, 2 = long) */
242 26, /* bitsize */
243 FALSE, /* pc_relative */
244 0, /* bitpos */
245 complain_overflow_bitfield, /* complain_on_overflow */
246 bfd_elf_generic_reloc, /* special_function */
247 "R_PPC64_ADDR24", /* name */
248 FALSE, /* partial_inplace */
249 0, /* src_mask */
250 0x03fffffc, /* dst_mask */
251 FALSE), /* pcrel_offset */
253 /* A standard 16 bit relocation. */
254 HOWTO (R_PPC64_ADDR16, /* type */
255 0, /* rightshift */
256 1, /* size (0 = byte, 1 = short, 2 = long) */
257 16, /* bitsize */
258 FALSE, /* pc_relative */
259 0, /* bitpos */
260 complain_overflow_bitfield, /* complain_on_overflow */
261 bfd_elf_generic_reloc, /* special_function */
262 "R_PPC64_ADDR16", /* name */
263 FALSE, /* partial_inplace */
264 0, /* src_mask */
265 0xffff, /* dst_mask */
266 FALSE), /* pcrel_offset */
268 /* A 16 bit relocation without overflow. */
269 HOWTO (R_PPC64_ADDR16_LO, /* type */
270 0, /* rightshift */
271 1, /* size (0 = byte, 1 = short, 2 = long) */
272 16, /* bitsize */
273 FALSE, /* pc_relative */
274 0, /* bitpos */
275 complain_overflow_dont,/* complain_on_overflow */
276 bfd_elf_generic_reloc, /* special_function */
277 "R_PPC64_ADDR16_LO", /* name */
278 FALSE, /* partial_inplace */
279 0, /* src_mask */
280 0xffff, /* dst_mask */
281 FALSE), /* pcrel_offset */
283 /* Bits 16-31 of an address. */
284 HOWTO (R_PPC64_ADDR16_HI, /* type */
285 16, /* rightshift */
286 1, /* size (0 = byte, 1 = short, 2 = long) */
287 16, /* bitsize */
288 FALSE, /* pc_relative */
289 0, /* bitpos */
290 complain_overflow_dont, /* complain_on_overflow */
291 bfd_elf_generic_reloc, /* special_function */
292 "R_PPC64_ADDR16_HI", /* name */
293 FALSE, /* partial_inplace */
294 0, /* src_mask */
295 0xffff, /* dst_mask */
296 FALSE), /* pcrel_offset */
298 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
299 bits, treated as a signed number, is negative. */
300 HOWTO (R_PPC64_ADDR16_HA, /* type */
301 16, /* rightshift */
302 1, /* size (0 = byte, 1 = short, 2 = long) */
303 16, /* bitsize */
304 FALSE, /* pc_relative */
305 0, /* bitpos */
306 complain_overflow_dont, /* complain_on_overflow */
307 ppc64_elf_ha_reloc, /* special_function */
308 "R_PPC64_ADDR16_HA", /* name */
309 FALSE, /* partial_inplace */
310 0, /* src_mask */
311 0xffff, /* dst_mask */
312 FALSE), /* pcrel_offset */
314 /* An absolute 16 bit branch; the lower two bits must be zero.
315 FIXME: we don't check that, we just clear them. */
316 HOWTO (R_PPC64_ADDR14, /* type */
317 0, /* rightshift */
318 2, /* size (0 = byte, 1 = short, 2 = long) */
319 16, /* bitsize */
320 FALSE, /* pc_relative */
321 0, /* bitpos */
322 complain_overflow_bitfield, /* complain_on_overflow */
323 ppc64_elf_branch_reloc, /* special_function */
324 "R_PPC64_ADDR14", /* name */
325 FALSE, /* partial_inplace */
326 0, /* src_mask */
327 0x0000fffc, /* dst_mask */
328 FALSE), /* pcrel_offset */
330 /* An absolute 16 bit branch, for which bit 10 should be set to
331 indicate that the branch is expected to be taken. The lower two
332 bits must be zero. */
333 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
334 0, /* rightshift */
335 2, /* size (0 = byte, 1 = short, 2 = long) */
336 16, /* bitsize */
337 FALSE, /* pc_relative */
338 0, /* bitpos */
339 complain_overflow_bitfield, /* complain_on_overflow */
340 ppc64_elf_brtaken_reloc, /* special_function */
341 "R_PPC64_ADDR14_BRTAKEN",/* name */
342 FALSE, /* partial_inplace */
343 0, /* src_mask */
344 0x0000fffc, /* dst_mask */
345 FALSE), /* pcrel_offset */
347 /* An absolute 16 bit branch, for which bit 10 should be set to
348 indicate that the branch is not expected to be taken. The lower
349 two bits must be zero. */
350 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
351 0, /* rightshift */
352 2, /* size (0 = byte, 1 = short, 2 = long) */
353 16, /* bitsize */
354 FALSE, /* pc_relative */
355 0, /* bitpos */
356 complain_overflow_bitfield, /* complain_on_overflow */
357 ppc64_elf_brtaken_reloc, /* special_function */
358 "R_PPC64_ADDR14_BRNTAKEN",/* name */
359 FALSE, /* partial_inplace */
360 0, /* src_mask */
361 0x0000fffc, /* dst_mask */
362 FALSE), /* pcrel_offset */
364 /* A relative 26 bit branch; the lower two bits must be zero. */
365 HOWTO (R_PPC64_REL24, /* type */
366 0, /* rightshift */
367 2, /* size (0 = byte, 1 = short, 2 = long) */
368 26, /* bitsize */
369 TRUE, /* pc_relative */
370 0, /* bitpos */
371 complain_overflow_signed, /* complain_on_overflow */
372 ppc64_elf_branch_reloc, /* special_function */
373 "R_PPC64_REL24", /* name */
374 FALSE, /* partial_inplace */
375 0, /* src_mask */
376 0x03fffffc, /* dst_mask */
377 TRUE), /* pcrel_offset */
379 /* A relative 16 bit branch; the lower two bits must be zero. */
380 HOWTO (R_PPC64_REL14, /* type */
381 0, /* rightshift */
382 2, /* size (0 = byte, 1 = short, 2 = long) */
383 16, /* bitsize */
384 TRUE, /* pc_relative */
385 0, /* bitpos */
386 complain_overflow_signed, /* complain_on_overflow */
387 ppc64_elf_branch_reloc, /* special_function */
388 "R_PPC64_REL14", /* name */
389 FALSE, /* partial_inplace */
390 0, /* src_mask */
391 0x0000fffc, /* dst_mask */
392 TRUE), /* pcrel_offset */
394 /* A relative 16 bit branch. Bit 10 should be set to indicate that
395 the branch is expected to be taken. The lower two bits must be
396 zero. */
397 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
398 0, /* rightshift */
399 2, /* size (0 = byte, 1 = short, 2 = long) */
400 16, /* bitsize */
401 TRUE, /* pc_relative */
402 0, /* bitpos */
403 complain_overflow_signed, /* complain_on_overflow */
404 ppc64_elf_brtaken_reloc, /* special_function */
405 "R_PPC64_REL14_BRTAKEN", /* name */
406 FALSE, /* partial_inplace */
407 0, /* src_mask */
408 0x0000fffc, /* dst_mask */
409 TRUE), /* pcrel_offset */
411 /* A relative 16 bit branch. Bit 10 should be set to indicate that
412 the branch is not expected to be taken. The lower two bits must
413 be zero. */
414 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
415 0, /* rightshift */
416 2, /* size (0 = byte, 1 = short, 2 = long) */
417 16, /* bitsize */
418 TRUE, /* pc_relative */
419 0, /* bitpos */
420 complain_overflow_signed, /* complain_on_overflow */
421 ppc64_elf_brtaken_reloc, /* special_function */
422 "R_PPC64_REL14_BRNTAKEN",/* name */
423 FALSE, /* partial_inplace */
424 0, /* src_mask */
425 0x0000fffc, /* dst_mask */
426 TRUE), /* pcrel_offset */
428 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
429 symbol. */
430 HOWTO (R_PPC64_GOT16, /* type */
431 0, /* rightshift */
432 1, /* size (0 = byte, 1 = short, 2 = long) */
433 16, /* bitsize */
434 FALSE, /* pc_relative */
435 0, /* bitpos */
436 complain_overflow_signed, /* complain_on_overflow */
437 ppc64_elf_unhandled_reloc, /* special_function */
438 "R_PPC64_GOT16", /* name */
439 FALSE, /* partial_inplace */
440 0, /* src_mask */
441 0xffff, /* dst_mask */
442 FALSE), /* pcrel_offset */
444 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
445 the symbol. */
446 HOWTO (R_PPC64_GOT16_LO, /* type */
447 0, /* rightshift */
448 1, /* size (0 = byte, 1 = short, 2 = long) */
449 16, /* bitsize */
450 FALSE, /* pc_relative */
451 0, /* bitpos */
452 complain_overflow_dont, /* complain_on_overflow */
453 ppc64_elf_unhandled_reloc, /* special_function */
454 "R_PPC64_GOT16_LO", /* name */
455 FALSE, /* partial_inplace */
456 0, /* src_mask */
457 0xffff, /* dst_mask */
458 FALSE), /* pcrel_offset */
460 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
461 the symbol. */
462 HOWTO (R_PPC64_GOT16_HI, /* type */
463 16, /* rightshift */
464 1, /* size (0 = byte, 1 = short, 2 = long) */
465 16, /* bitsize */
466 FALSE, /* pc_relative */
467 0, /* bitpos */
468 complain_overflow_dont,/* complain_on_overflow */
469 ppc64_elf_unhandled_reloc, /* special_function */
470 "R_PPC64_GOT16_HI", /* name */
471 FALSE, /* partial_inplace */
472 0, /* src_mask */
473 0xffff, /* dst_mask */
474 FALSE), /* pcrel_offset */
476 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
477 the symbol. */
478 HOWTO (R_PPC64_GOT16_HA, /* type */
479 16, /* rightshift */
480 1, /* size (0 = byte, 1 = short, 2 = long) */
481 16, /* bitsize */
482 FALSE, /* pc_relative */
483 0, /* bitpos */
484 complain_overflow_dont,/* complain_on_overflow */
485 ppc64_elf_unhandled_reloc, /* special_function */
486 "R_PPC64_GOT16_HA", /* name */
487 FALSE, /* partial_inplace */
488 0, /* src_mask */
489 0xffff, /* dst_mask */
490 FALSE), /* pcrel_offset */
492 /* This is used only by the dynamic linker. The symbol should exist
493 both in the object being run and in some shared library. The
494 dynamic linker copies the data addressed by the symbol from the
495 shared library into the object, because the object being
496 run has to have the data at some particular address. */
497 HOWTO (R_PPC64_COPY, /* type */
498 0, /* rightshift */
499 0, /* this one is variable size */
500 0, /* bitsize */
501 FALSE, /* pc_relative */
502 0, /* bitpos */
503 complain_overflow_dont, /* complain_on_overflow */
504 ppc64_elf_unhandled_reloc, /* special_function */
505 "R_PPC64_COPY", /* name */
506 FALSE, /* partial_inplace */
507 0, /* src_mask */
508 0, /* dst_mask */
509 FALSE), /* pcrel_offset */
511 /* Like R_PPC64_ADDR64, but used when setting global offset table
512 entries. */
513 HOWTO (R_PPC64_GLOB_DAT, /* type */
514 0, /* rightshift */
515 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
516 64, /* bitsize */
517 FALSE, /* pc_relative */
518 0, /* bitpos */
519 complain_overflow_dont, /* complain_on_overflow */
520 ppc64_elf_unhandled_reloc, /* special_function */
521 "R_PPC64_GLOB_DAT", /* name */
522 FALSE, /* partial_inplace */
523 0, /* src_mask */
524 ONES (64), /* dst_mask */
525 FALSE), /* pcrel_offset */
527 /* Created by the link editor. Marks a procedure linkage table
528 entry for a symbol. */
529 HOWTO (R_PPC64_JMP_SLOT, /* type */
530 0, /* rightshift */
531 0, /* size (0 = byte, 1 = short, 2 = long) */
532 0, /* bitsize */
533 FALSE, /* pc_relative */
534 0, /* bitpos */
535 complain_overflow_dont, /* complain_on_overflow */
536 ppc64_elf_unhandled_reloc, /* special_function */
537 "R_PPC64_JMP_SLOT", /* name */
538 FALSE, /* partial_inplace */
539 0, /* src_mask */
540 0, /* dst_mask */
541 FALSE), /* pcrel_offset */
543 /* Used only by the dynamic linker. When the object is run, this
544 doubleword64 is set to the load address of the object, plus the
545 addend. */
546 HOWTO (R_PPC64_RELATIVE, /* type */
547 0, /* rightshift */
548 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
549 64, /* bitsize */
550 FALSE, /* pc_relative */
551 0, /* bitpos */
552 complain_overflow_dont, /* complain_on_overflow */
553 bfd_elf_generic_reloc, /* special_function */
554 "R_PPC64_RELATIVE", /* name */
555 FALSE, /* partial_inplace */
556 0, /* src_mask */
557 ONES (64), /* dst_mask */
558 FALSE), /* pcrel_offset */
560 /* Like R_PPC64_ADDR32, but may be unaligned. */
561 HOWTO (R_PPC64_UADDR32, /* type */
562 0, /* rightshift */
563 2, /* size (0 = byte, 1 = short, 2 = long) */
564 32, /* bitsize */
565 FALSE, /* pc_relative */
566 0, /* bitpos */
567 complain_overflow_bitfield, /* complain_on_overflow */
568 bfd_elf_generic_reloc, /* special_function */
569 "R_PPC64_UADDR32", /* name */
570 FALSE, /* partial_inplace */
571 0, /* src_mask */
572 0xffffffff, /* dst_mask */
573 FALSE), /* pcrel_offset */
575 /* Like R_PPC64_ADDR16, but may be unaligned. */
576 HOWTO (R_PPC64_UADDR16, /* type */
577 0, /* rightshift */
578 1, /* size (0 = byte, 1 = short, 2 = long) */
579 16, /* bitsize */
580 FALSE, /* pc_relative */
581 0, /* bitpos */
582 complain_overflow_bitfield, /* complain_on_overflow */
583 bfd_elf_generic_reloc, /* special_function */
584 "R_PPC64_UADDR16", /* name */
585 FALSE, /* partial_inplace */
586 0, /* src_mask */
587 0xffff, /* dst_mask */
588 FALSE), /* pcrel_offset */
590 /* 32-bit PC relative. */
591 HOWTO (R_PPC64_REL32, /* type */
592 0, /* rightshift */
593 2, /* size (0 = byte, 1 = short, 2 = long) */
594 32, /* bitsize */
595 TRUE, /* pc_relative */
596 0, /* bitpos */
597 /* FIXME: Verify. Was complain_overflow_bitfield. */
598 complain_overflow_signed, /* complain_on_overflow */
599 bfd_elf_generic_reloc, /* special_function */
600 "R_PPC64_REL32", /* name */
601 FALSE, /* partial_inplace */
602 0, /* src_mask */
603 0xffffffff, /* dst_mask */
604 TRUE), /* pcrel_offset */
606 /* 32-bit relocation to the symbol's procedure linkage table. */
607 HOWTO (R_PPC64_PLT32, /* type */
608 0, /* rightshift */
609 2, /* size (0 = byte, 1 = short, 2 = long) */
610 32, /* bitsize */
611 FALSE, /* pc_relative */
612 0, /* bitpos */
613 complain_overflow_bitfield, /* complain_on_overflow */
614 ppc64_elf_unhandled_reloc, /* special_function */
615 "R_PPC64_PLT32", /* name */
616 FALSE, /* partial_inplace */
617 0, /* src_mask */
618 0xffffffff, /* dst_mask */
619 FALSE), /* pcrel_offset */
621 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
622 FIXME: R_PPC64_PLTREL32 not supported. */
623 HOWTO (R_PPC64_PLTREL32, /* type */
624 0, /* rightshift */
625 2, /* size (0 = byte, 1 = short, 2 = long) */
626 32, /* bitsize */
627 TRUE, /* pc_relative */
628 0, /* bitpos */
629 complain_overflow_signed, /* complain_on_overflow */
630 bfd_elf_generic_reloc, /* special_function */
631 "R_PPC64_PLTREL32", /* name */
632 FALSE, /* partial_inplace */
633 0, /* src_mask */
634 0xffffffff, /* dst_mask */
635 TRUE), /* pcrel_offset */
637 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
638 the symbol. */
639 HOWTO (R_PPC64_PLT16_LO, /* type */
640 0, /* rightshift */
641 1, /* size (0 = byte, 1 = short, 2 = long) */
642 16, /* bitsize */
643 FALSE, /* pc_relative */
644 0, /* bitpos */
645 complain_overflow_dont, /* complain_on_overflow */
646 ppc64_elf_unhandled_reloc, /* special_function */
647 "R_PPC64_PLT16_LO", /* name */
648 FALSE, /* partial_inplace */
649 0, /* src_mask */
650 0xffff, /* dst_mask */
651 FALSE), /* pcrel_offset */
653 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
654 the symbol. */
655 HOWTO (R_PPC64_PLT16_HI, /* type */
656 16, /* rightshift */
657 1, /* size (0 = byte, 1 = short, 2 = long) */
658 16, /* bitsize */
659 FALSE, /* pc_relative */
660 0, /* bitpos */
661 complain_overflow_dont, /* complain_on_overflow */
662 ppc64_elf_unhandled_reloc, /* special_function */
663 "R_PPC64_PLT16_HI", /* name */
664 FALSE, /* partial_inplace */
665 0, /* src_mask */
666 0xffff, /* dst_mask */
667 FALSE), /* pcrel_offset */
669 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
670 the symbol. */
671 HOWTO (R_PPC64_PLT16_HA, /* type */
672 16, /* rightshift */
673 1, /* size (0 = byte, 1 = short, 2 = long) */
674 16, /* bitsize */
675 FALSE, /* pc_relative */
676 0, /* bitpos */
677 complain_overflow_dont, /* complain_on_overflow */
678 ppc64_elf_unhandled_reloc, /* special_function */
679 "R_PPC64_PLT16_HA", /* name */
680 FALSE, /* partial_inplace */
681 0, /* src_mask */
682 0xffff, /* dst_mask */
683 FALSE), /* pcrel_offset */
685 /* 16-bit section relative relocation. */
686 HOWTO (R_PPC64_SECTOFF, /* type */
687 0, /* rightshift */
688 1, /* size (0 = byte, 1 = short, 2 = long) */
689 16, /* bitsize */
690 FALSE, /* pc_relative */
691 0, /* bitpos */
692 complain_overflow_bitfield, /* complain_on_overflow */
693 ppc64_elf_sectoff_reloc, /* special_function */
694 "R_PPC64_SECTOFF", /* name */
695 FALSE, /* partial_inplace */
696 0, /* src_mask */
697 0xffff, /* dst_mask */
698 FALSE), /* pcrel_offset */
700 /* Like R_PPC64_SECTOFF, but no overflow warning. */
701 HOWTO (R_PPC64_SECTOFF_LO, /* type */
702 0, /* rightshift */
703 1, /* size (0 = byte, 1 = short, 2 = long) */
704 16, /* bitsize */
705 FALSE, /* pc_relative */
706 0, /* bitpos */
707 complain_overflow_dont, /* complain_on_overflow */
708 ppc64_elf_sectoff_reloc, /* special_function */
709 "R_PPC64_SECTOFF_LO", /* name */
710 FALSE, /* partial_inplace */
711 0, /* src_mask */
712 0xffff, /* dst_mask */
713 FALSE), /* pcrel_offset */
715 /* 16-bit upper half section relative relocation. */
716 HOWTO (R_PPC64_SECTOFF_HI, /* type */
717 16, /* rightshift */
718 1, /* size (0 = byte, 1 = short, 2 = long) */
719 16, /* bitsize */
720 FALSE, /* pc_relative */
721 0, /* bitpos */
722 complain_overflow_dont, /* complain_on_overflow */
723 ppc64_elf_sectoff_reloc, /* special_function */
724 "R_PPC64_SECTOFF_HI", /* name */
725 FALSE, /* partial_inplace */
726 0, /* src_mask */
727 0xffff, /* dst_mask */
728 FALSE), /* pcrel_offset */
730 /* 16-bit upper half adjusted section relative relocation. */
731 HOWTO (R_PPC64_SECTOFF_HA, /* type */
732 16, /* rightshift */
733 1, /* size (0 = byte, 1 = short, 2 = long) */
734 16, /* bitsize */
735 FALSE, /* pc_relative */
736 0, /* bitpos */
737 complain_overflow_dont, /* complain_on_overflow */
738 ppc64_elf_sectoff_ha_reloc, /* special_function */
739 "R_PPC64_SECTOFF_HA", /* name */
740 FALSE, /* partial_inplace */
741 0, /* src_mask */
742 0xffff, /* dst_mask */
743 FALSE), /* pcrel_offset */
745 /* Like R_PPC64_REL24 without touching the two least significant bits. */
746 HOWTO (R_PPC64_REL30, /* type */
747 2, /* rightshift */
748 2, /* size (0 = byte, 1 = short, 2 = long) */
749 30, /* bitsize */
750 TRUE, /* pc_relative */
751 0, /* bitpos */
752 complain_overflow_dont, /* complain_on_overflow */
753 bfd_elf_generic_reloc, /* special_function */
754 "R_PPC64_REL30", /* name */
755 FALSE, /* partial_inplace */
756 0, /* src_mask */
757 0xfffffffc, /* dst_mask */
758 TRUE), /* pcrel_offset */
760 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
762 /* A standard 64-bit relocation. */
763 HOWTO (R_PPC64_ADDR64, /* type */
764 0, /* rightshift */
765 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
766 64, /* bitsize */
767 FALSE, /* pc_relative */
768 0, /* bitpos */
769 complain_overflow_dont, /* complain_on_overflow */
770 bfd_elf_generic_reloc, /* special_function */
771 "R_PPC64_ADDR64", /* name */
772 FALSE, /* partial_inplace */
773 0, /* src_mask */
774 ONES (64), /* dst_mask */
775 FALSE), /* pcrel_offset */
777 /* The bits 32-47 of an address. */
778 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
779 32, /* rightshift */
780 1, /* size (0 = byte, 1 = short, 2 = long) */
781 16, /* bitsize */
782 FALSE, /* pc_relative */
783 0, /* bitpos */
784 complain_overflow_dont, /* complain_on_overflow */
785 bfd_elf_generic_reloc, /* special_function */
786 "R_PPC64_ADDR16_HIGHER", /* name */
787 FALSE, /* partial_inplace */
788 0, /* src_mask */
789 0xffff, /* dst_mask */
790 FALSE), /* pcrel_offset */
792 /* The bits 32-47 of an address, plus 1 if the contents of the low
793 16 bits, treated as a signed number, is negative. */
794 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
795 32, /* rightshift */
796 1, /* size (0 = byte, 1 = short, 2 = long) */
797 16, /* bitsize */
798 FALSE, /* pc_relative */
799 0, /* bitpos */
800 complain_overflow_dont, /* complain_on_overflow */
801 ppc64_elf_ha_reloc, /* special_function */
802 "R_PPC64_ADDR16_HIGHERA", /* name */
803 FALSE, /* partial_inplace */
804 0, /* src_mask */
805 0xffff, /* dst_mask */
806 FALSE), /* pcrel_offset */
808 /* The bits 48-63 of an address. */
809 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
810 48, /* rightshift */
811 1, /* size (0 = byte, 1 = short, 2 = long) */
812 16, /* bitsize */
813 FALSE, /* pc_relative */
814 0, /* bitpos */
815 complain_overflow_dont, /* complain_on_overflow */
816 bfd_elf_generic_reloc, /* special_function */
817 "R_PPC64_ADDR16_HIGHEST", /* name */
818 FALSE, /* partial_inplace */
819 0, /* src_mask */
820 0xffff, /* dst_mask */
821 FALSE), /* pcrel_offset */
823 /* The bits 48-63 of an address, plus 1 if the contents of the low
824 16 bits, treated as a signed number, is negative. */
825 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
826 48, /* rightshift */
827 1, /* size (0 = byte, 1 = short, 2 = long) */
828 16, /* bitsize */
829 FALSE, /* pc_relative */
830 0, /* bitpos */
831 complain_overflow_dont, /* complain_on_overflow */
832 ppc64_elf_ha_reloc, /* special_function */
833 "R_PPC64_ADDR16_HIGHESTA", /* name */
834 FALSE, /* partial_inplace */
835 0, /* src_mask */
836 0xffff, /* dst_mask */
837 FALSE), /* pcrel_offset */
839 /* Like ADDR64, but may be unaligned. */
840 HOWTO (R_PPC64_UADDR64, /* type */
841 0, /* rightshift */
842 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
843 64, /* bitsize */
844 FALSE, /* pc_relative */
845 0, /* bitpos */
846 complain_overflow_dont, /* complain_on_overflow */
847 bfd_elf_generic_reloc, /* special_function */
848 "R_PPC64_UADDR64", /* name */
849 FALSE, /* partial_inplace */
850 0, /* src_mask */
851 ONES (64), /* dst_mask */
852 FALSE), /* pcrel_offset */
854 /* 64-bit relative relocation. */
855 HOWTO (R_PPC64_REL64, /* type */
856 0, /* rightshift */
857 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
858 64, /* bitsize */
859 TRUE, /* pc_relative */
860 0, /* bitpos */
861 complain_overflow_dont, /* complain_on_overflow */
862 bfd_elf_generic_reloc, /* special_function */
863 "R_PPC64_REL64", /* name */
864 FALSE, /* partial_inplace */
865 0, /* src_mask */
866 ONES (64), /* dst_mask */
867 TRUE), /* pcrel_offset */
869 /* 64-bit relocation to the symbol's procedure linkage table. */
870 HOWTO (R_PPC64_PLT64, /* type */
871 0, /* rightshift */
872 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
873 64, /* bitsize */
874 FALSE, /* pc_relative */
875 0, /* bitpos */
876 complain_overflow_dont, /* complain_on_overflow */
877 ppc64_elf_unhandled_reloc, /* special_function */
878 "R_PPC64_PLT64", /* name */
879 FALSE, /* partial_inplace */
880 0, /* src_mask */
881 ONES (64), /* dst_mask */
882 FALSE), /* pcrel_offset */
884 /* 64-bit PC relative relocation to the symbol's procedure linkage
885 table. */
886 /* FIXME: R_PPC64_PLTREL64 not supported. */
887 HOWTO (R_PPC64_PLTREL64, /* type */
888 0, /* rightshift */
889 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
890 64, /* bitsize */
891 TRUE, /* pc_relative */
892 0, /* bitpos */
893 complain_overflow_dont, /* complain_on_overflow */
894 ppc64_elf_unhandled_reloc, /* special_function */
895 "R_PPC64_PLTREL64", /* name */
896 FALSE, /* partial_inplace */
897 0, /* src_mask */
898 ONES (64), /* dst_mask */
899 TRUE), /* pcrel_offset */
901 /* 16 bit TOC-relative relocation. */
903 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
904 HOWTO (R_PPC64_TOC16, /* type */
905 0, /* rightshift */
906 1, /* size (0 = byte, 1 = short, 2 = long) */
907 16, /* bitsize */
908 FALSE, /* pc_relative */
909 0, /* bitpos */
910 complain_overflow_signed, /* complain_on_overflow */
911 ppc64_elf_toc_reloc, /* special_function */
912 "R_PPC64_TOC16", /* name */
913 FALSE, /* partial_inplace */
914 0, /* src_mask */
915 0xffff, /* dst_mask */
916 FALSE), /* pcrel_offset */
918 /* 16 bit TOC-relative relocation without overflow. */
920 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
921 HOWTO (R_PPC64_TOC16_LO, /* type */
922 0, /* rightshift */
923 1, /* size (0 = byte, 1 = short, 2 = long) */
924 16, /* bitsize */
925 FALSE, /* pc_relative */
926 0, /* bitpos */
927 complain_overflow_dont, /* complain_on_overflow */
928 ppc64_elf_toc_reloc, /* special_function */
929 "R_PPC64_TOC16_LO", /* name */
930 FALSE, /* partial_inplace */
931 0, /* src_mask */
932 0xffff, /* dst_mask */
933 FALSE), /* pcrel_offset */
935 /* 16 bit TOC-relative relocation, high 16 bits. */
937 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
938 HOWTO (R_PPC64_TOC16_HI, /* type */
939 16, /* rightshift */
940 1, /* size (0 = byte, 1 = short, 2 = long) */
941 16, /* bitsize */
942 FALSE, /* pc_relative */
943 0, /* bitpos */
944 complain_overflow_dont, /* complain_on_overflow */
945 ppc64_elf_toc_reloc, /* special_function */
946 "R_PPC64_TOC16_HI", /* name */
947 FALSE, /* partial_inplace */
948 0, /* src_mask */
949 0xffff, /* dst_mask */
950 FALSE), /* pcrel_offset */
952 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
953 contents of the low 16 bits, treated as a signed number, is
954 negative. */
956 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
957 HOWTO (R_PPC64_TOC16_HA, /* type */
958 16, /* rightshift */
959 1, /* size (0 = byte, 1 = short, 2 = long) */
960 16, /* bitsize */
961 FALSE, /* pc_relative */
962 0, /* bitpos */
963 complain_overflow_dont, /* complain_on_overflow */
964 ppc64_elf_toc_ha_reloc, /* special_function */
965 "R_PPC64_TOC16_HA", /* name */
966 FALSE, /* partial_inplace */
967 0, /* src_mask */
968 0xffff, /* dst_mask */
969 FALSE), /* pcrel_offset */
971 /* 64-bit relocation; insert value of TOC base (.TOC.). */
973 /* R_PPC64_TOC 51 doubleword64 .TOC. */
974 HOWTO (R_PPC64_TOC, /* type */
975 0, /* rightshift */
976 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
977 64, /* bitsize */
978 FALSE, /* pc_relative */
979 0, /* bitpos */
980 complain_overflow_bitfield, /* complain_on_overflow */
981 ppc64_elf_toc64_reloc, /* special_function */
982 "R_PPC64_TOC", /* name */
983 FALSE, /* partial_inplace */
984 0, /* src_mask */
985 ONES (64), /* dst_mask */
986 FALSE), /* pcrel_offset */
988 /* Like R_PPC64_GOT16, but also informs the link editor that the
989 value to relocate may (!) refer to a PLT entry which the link
990 editor (a) may replace with the symbol value. If the link editor
991 is unable to fully resolve the symbol, it may (b) create a PLT
992 entry and store the address to the new PLT entry in the GOT.
993 This permits lazy resolution of function symbols at run time.
994 The link editor may also skip all of this and just (c) emit a
995 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
996 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
997 HOWTO (R_PPC64_PLTGOT16, /* type */
998 0, /* rightshift */
999 1, /* size (0 = byte, 1 = short, 2 = long) */
1000 16, /* bitsize */
1001 FALSE, /* pc_relative */
1002 0, /* bitpos */
1003 complain_overflow_signed, /* complain_on_overflow */
1004 ppc64_elf_unhandled_reloc, /* special_function */
1005 "R_PPC64_PLTGOT16", /* name */
1006 FALSE, /* partial_inplace */
1007 0, /* src_mask */
1008 0xffff, /* dst_mask */
1009 FALSE), /* pcrel_offset */
1011 /* Like R_PPC64_PLTGOT16, but without overflow. */
1012 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1013 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1014 0, /* rightshift */
1015 1, /* size (0 = byte, 1 = short, 2 = long) */
1016 16, /* bitsize */
1017 FALSE, /* pc_relative */
1018 0, /* bitpos */
1019 complain_overflow_dont, /* complain_on_overflow */
1020 ppc64_elf_unhandled_reloc, /* special_function */
1021 "R_PPC64_PLTGOT16_LO", /* name */
1022 FALSE, /* partial_inplace */
1023 0, /* src_mask */
1024 0xffff, /* dst_mask */
1025 FALSE), /* pcrel_offset */
1027 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1028 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1029 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1030 16, /* rightshift */
1031 1, /* size (0 = byte, 1 = short, 2 = long) */
1032 16, /* bitsize */
1033 FALSE, /* pc_relative */
1034 0, /* bitpos */
1035 complain_overflow_dont, /* complain_on_overflow */
1036 ppc64_elf_unhandled_reloc, /* special_function */
1037 "R_PPC64_PLTGOT16_HI", /* name */
1038 FALSE, /* partial_inplace */
1039 0, /* src_mask */
1040 0xffff, /* dst_mask */
1041 FALSE), /* pcrel_offset */
1043 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1044 1 if the contents of the low 16 bits, treated as a signed number,
1045 is negative. */
1046 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1047 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1048 16, /* rightshift */
1049 1, /* size (0 = byte, 1 = short, 2 = long) */
1050 16, /* bitsize */
1051 FALSE, /* pc_relative */
1052 0, /* bitpos */
1053 complain_overflow_dont,/* complain_on_overflow */
1054 ppc64_elf_unhandled_reloc, /* special_function */
1055 "R_PPC64_PLTGOT16_HA", /* name */
1056 FALSE, /* partial_inplace */
1057 0, /* src_mask */
1058 0xffff, /* dst_mask */
1059 FALSE), /* pcrel_offset */
1061 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1062 HOWTO (R_PPC64_ADDR16_DS, /* type */
1063 0, /* rightshift */
1064 1, /* size (0 = byte, 1 = short, 2 = long) */
1065 16, /* bitsize */
1066 FALSE, /* pc_relative */
1067 0, /* bitpos */
1068 complain_overflow_bitfield, /* complain_on_overflow */
1069 bfd_elf_generic_reloc, /* special_function */
1070 "R_PPC64_ADDR16_DS", /* name */
1071 FALSE, /* partial_inplace */
1072 0, /* src_mask */
1073 0xfffc, /* dst_mask */
1074 FALSE), /* pcrel_offset */
1076 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1077 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1078 0, /* rightshift */
1079 1, /* size (0 = byte, 1 = short, 2 = long) */
1080 16, /* bitsize */
1081 FALSE, /* pc_relative */
1082 0, /* bitpos */
1083 complain_overflow_dont,/* complain_on_overflow */
1084 bfd_elf_generic_reloc, /* special_function */
1085 "R_PPC64_ADDR16_LO_DS",/* name */
1086 FALSE, /* partial_inplace */
1087 0, /* src_mask */
1088 0xfffc, /* dst_mask */
1089 FALSE), /* pcrel_offset */
1091 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1092 HOWTO (R_PPC64_GOT16_DS, /* type */
1093 0, /* rightshift */
1094 1, /* size (0 = byte, 1 = short, 2 = long) */
1095 16, /* bitsize */
1096 FALSE, /* pc_relative */
1097 0, /* bitpos */
1098 complain_overflow_signed, /* complain_on_overflow */
1099 ppc64_elf_unhandled_reloc, /* special_function */
1100 "R_PPC64_GOT16_DS", /* name */
1101 FALSE, /* partial_inplace */
1102 0, /* src_mask */
1103 0xfffc, /* dst_mask */
1104 FALSE), /* pcrel_offset */
1106 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1107 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1108 0, /* rightshift */
1109 1, /* size (0 = byte, 1 = short, 2 = long) */
1110 16, /* bitsize */
1111 FALSE, /* pc_relative */
1112 0, /* bitpos */
1113 complain_overflow_dont, /* complain_on_overflow */
1114 ppc64_elf_unhandled_reloc, /* special_function */
1115 "R_PPC64_GOT16_LO_DS", /* name */
1116 FALSE, /* partial_inplace */
1117 0, /* src_mask */
1118 0xfffc, /* dst_mask */
1119 FALSE), /* pcrel_offset */
1121 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1122 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1123 0, /* rightshift */
1124 1, /* size (0 = byte, 1 = short, 2 = long) */
1125 16, /* bitsize */
1126 FALSE, /* pc_relative */
1127 0, /* bitpos */
1128 complain_overflow_dont, /* complain_on_overflow */
1129 ppc64_elf_unhandled_reloc, /* special_function */
1130 "R_PPC64_PLT16_LO_DS", /* name */
1131 FALSE, /* partial_inplace */
1132 0, /* src_mask */
1133 0xfffc, /* dst_mask */
1134 FALSE), /* pcrel_offset */
1136 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1137 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1138 0, /* rightshift */
1139 1, /* size (0 = byte, 1 = short, 2 = long) */
1140 16, /* bitsize */
1141 FALSE, /* pc_relative */
1142 0, /* bitpos */
1143 complain_overflow_bitfield, /* complain_on_overflow */
1144 ppc64_elf_sectoff_reloc, /* special_function */
1145 "R_PPC64_SECTOFF_DS", /* name */
1146 FALSE, /* partial_inplace */
1147 0, /* src_mask */
1148 0xfffc, /* dst_mask */
1149 FALSE), /* pcrel_offset */
1151 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1152 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1153 0, /* rightshift */
1154 1, /* size (0 = byte, 1 = short, 2 = long) */
1155 16, /* bitsize */
1156 FALSE, /* pc_relative */
1157 0, /* bitpos */
1158 complain_overflow_dont, /* complain_on_overflow */
1159 ppc64_elf_sectoff_reloc, /* special_function */
1160 "R_PPC64_SECTOFF_LO_DS",/* name */
1161 FALSE, /* partial_inplace */
1162 0, /* src_mask */
1163 0xfffc, /* dst_mask */
1164 FALSE), /* pcrel_offset */
1166 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1167 HOWTO (R_PPC64_TOC16_DS, /* type */
1168 0, /* rightshift */
1169 1, /* size (0 = byte, 1 = short, 2 = long) */
1170 16, /* bitsize */
1171 FALSE, /* pc_relative */
1172 0, /* bitpos */
1173 complain_overflow_signed, /* complain_on_overflow */
1174 ppc64_elf_toc_reloc, /* special_function */
1175 "R_PPC64_TOC16_DS", /* name */
1176 FALSE, /* partial_inplace */
1177 0, /* src_mask */
1178 0xfffc, /* dst_mask */
1179 FALSE), /* pcrel_offset */
1181 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1182 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1183 0, /* rightshift */
1184 1, /* size (0 = byte, 1 = short, 2 = long) */
1185 16, /* bitsize */
1186 FALSE, /* pc_relative */
1187 0, /* bitpos */
1188 complain_overflow_dont, /* complain_on_overflow */
1189 ppc64_elf_toc_reloc, /* special_function */
1190 "R_PPC64_TOC16_LO_DS", /* name */
1191 FALSE, /* partial_inplace */
1192 0, /* src_mask */
1193 0xfffc, /* dst_mask */
1194 FALSE), /* pcrel_offset */
1196 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1197 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1198 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1199 0, /* rightshift */
1200 1, /* size (0 = byte, 1 = short, 2 = long) */
1201 16, /* bitsize */
1202 FALSE, /* pc_relative */
1203 0, /* bitpos */
1204 complain_overflow_signed, /* complain_on_overflow */
1205 ppc64_elf_unhandled_reloc, /* special_function */
1206 "R_PPC64_PLTGOT16_DS", /* name */
1207 FALSE, /* partial_inplace */
1208 0, /* src_mask */
1209 0xfffc, /* dst_mask */
1210 FALSE), /* pcrel_offset */
1212 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1213 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1214 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1215 0, /* rightshift */
1216 1, /* size (0 = byte, 1 = short, 2 = long) */
1217 16, /* bitsize */
1218 FALSE, /* pc_relative */
1219 0, /* bitpos */
1220 complain_overflow_dont, /* complain_on_overflow */
1221 ppc64_elf_unhandled_reloc, /* special_function */
1222 "R_PPC64_PLTGOT16_LO_DS",/* name */
1223 FALSE, /* partial_inplace */
1224 0, /* src_mask */
1225 0xfffc, /* dst_mask */
1226 FALSE), /* pcrel_offset */
1228 /* Marker reloc for TLS. */
1229 HOWTO (R_PPC64_TLS,
1230 0, /* rightshift */
1231 2, /* size (0 = byte, 1 = short, 2 = long) */
1232 32, /* bitsize */
1233 FALSE, /* pc_relative */
1234 0, /* bitpos */
1235 complain_overflow_dont, /* complain_on_overflow */
1236 bfd_elf_generic_reloc, /* special_function */
1237 "R_PPC64_TLS", /* name */
1238 FALSE, /* partial_inplace */
1239 0, /* src_mask */
1240 0, /* dst_mask */
1241 FALSE), /* pcrel_offset */
1243 /* Computes the load module index of the load module that contains the
1244 definition of its TLS sym. */
1245 HOWTO (R_PPC64_DTPMOD64,
1246 0, /* rightshift */
1247 4, /* size (0 = byte, 1 = short, 2 = long) */
1248 64, /* bitsize */
1249 FALSE, /* pc_relative */
1250 0, /* bitpos */
1251 complain_overflow_dont, /* complain_on_overflow */
1252 ppc64_elf_unhandled_reloc, /* special_function */
1253 "R_PPC64_DTPMOD64", /* name */
1254 FALSE, /* partial_inplace */
1255 0, /* src_mask */
1256 ONES (64), /* dst_mask */
1257 FALSE), /* pcrel_offset */
1259 /* Computes a dtv-relative displacement, the difference between the value
1260 of sym+add and the base address of the thread-local storage block that
1261 contains the definition of sym, minus 0x8000. */
1262 HOWTO (R_PPC64_DTPREL64,
1263 0, /* rightshift */
1264 4, /* size (0 = byte, 1 = short, 2 = long) */
1265 64, /* bitsize */
1266 FALSE, /* pc_relative */
1267 0, /* bitpos */
1268 complain_overflow_dont, /* complain_on_overflow */
1269 ppc64_elf_unhandled_reloc, /* special_function */
1270 "R_PPC64_DTPREL64", /* name */
1271 FALSE, /* partial_inplace */
1272 0, /* src_mask */
1273 ONES (64), /* dst_mask */
1274 FALSE), /* pcrel_offset */
1276 /* A 16 bit dtprel reloc. */
1277 HOWTO (R_PPC64_DTPREL16,
1278 0, /* rightshift */
1279 1, /* size (0 = byte, 1 = short, 2 = long) */
1280 16, /* bitsize */
1281 FALSE, /* pc_relative */
1282 0, /* bitpos */
1283 complain_overflow_signed, /* complain_on_overflow */
1284 ppc64_elf_unhandled_reloc, /* special_function */
1285 "R_PPC64_DTPREL16", /* name */
1286 FALSE, /* partial_inplace */
1287 0, /* src_mask */
1288 0xffff, /* dst_mask */
1289 FALSE), /* pcrel_offset */
1291 /* Like DTPREL16, but no overflow. */
1292 HOWTO (R_PPC64_DTPREL16_LO,
1293 0, /* rightshift */
1294 1, /* size (0 = byte, 1 = short, 2 = long) */
1295 16, /* bitsize */
1296 FALSE, /* pc_relative */
1297 0, /* bitpos */
1298 complain_overflow_dont, /* complain_on_overflow */
1299 ppc64_elf_unhandled_reloc, /* special_function */
1300 "R_PPC64_DTPREL16_LO", /* name */
1301 FALSE, /* partial_inplace */
1302 0, /* src_mask */
1303 0xffff, /* dst_mask */
1304 FALSE), /* pcrel_offset */
1306 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1307 HOWTO (R_PPC64_DTPREL16_HI,
1308 16, /* rightshift */
1309 1, /* size (0 = byte, 1 = short, 2 = long) */
1310 16, /* bitsize */
1311 FALSE, /* pc_relative */
1312 0, /* bitpos */
1313 complain_overflow_dont, /* complain_on_overflow */
1314 ppc64_elf_unhandled_reloc, /* special_function */
1315 "R_PPC64_DTPREL16_HI", /* name */
1316 FALSE, /* partial_inplace */
1317 0, /* src_mask */
1318 0xffff, /* dst_mask */
1319 FALSE), /* pcrel_offset */
1321 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1322 HOWTO (R_PPC64_DTPREL16_HA,
1323 16, /* rightshift */
1324 1, /* size (0 = byte, 1 = short, 2 = long) */
1325 16, /* bitsize */
1326 FALSE, /* pc_relative */
1327 0, /* bitpos */
1328 complain_overflow_dont, /* complain_on_overflow */
1329 ppc64_elf_unhandled_reloc, /* special_function */
1330 "R_PPC64_DTPREL16_HA", /* name */
1331 FALSE, /* partial_inplace */
1332 0, /* src_mask */
1333 0xffff, /* dst_mask */
1334 FALSE), /* pcrel_offset */
1336 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1337 HOWTO (R_PPC64_DTPREL16_HIGHER,
1338 32, /* rightshift */
1339 1, /* size (0 = byte, 1 = short, 2 = long) */
1340 16, /* bitsize */
1341 FALSE, /* pc_relative */
1342 0, /* bitpos */
1343 complain_overflow_dont, /* complain_on_overflow */
1344 ppc64_elf_unhandled_reloc, /* special_function */
1345 "R_PPC64_DTPREL16_HIGHER", /* name */
1346 FALSE, /* partial_inplace */
1347 0, /* src_mask */
1348 0xffff, /* dst_mask */
1349 FALSE), /* pcrel_offset */
1351 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1352 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1353 32, /* rightshift */
1354 1, /* size (0 = byte, 1 = short, 2 = long) */
1355 16, /* bitsize */
1356 FALSE, /* pc_relative */
1357 0, /* bitpos */
1358 complain_overflow_dont, /* complain_on_overflow */
1359 ppc64_elf_unhandled_reloc, /* special_function */
1360 "R_PPC64_DTPREL16_HIGHERA", /* name */
1361 FALSE, /* partial_inplace */
1362 0, /* src_mask */
1363 0xffff, /* dst_mask */
1364 FALSE), /* pcrel_offset */
1366 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1367 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1368 48, /* rightshift */
1369 1, /* size (0 = byte, 1 = short, 2 = long) */
1370 16, /* bitsize */
1371 FALSE, /* pc_relative */
1372 0, /* bitpos */
1373 complain_overflow_dont, /* complain_on_overflow */
1374 ppc64_elf_unhandled_reloc, /* special_function */
1375 "R_PPC64_DTPREL16_HIGHEST", /* name */
1376 FALSE, /* partial_inplace */
1377 0, /* src_mask */
1378 0xffff, /* dst_mask */
1379 FALSE), /* pcrel_offset */
1381 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1382 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1383 48, /* rightshift */
1384 1, /* size (0 = byte, 1 = short, 2 = long) */
1385 16, /* bitsize */
1386 FALSE, /* pc_relative */
1387 0, /* bitpos */
1388 complain_overflow_dont, /* complain_on_overflow */
1389 ppc64_elf_unhandled_reloc, /* special_function */
1390 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1391 FALSE, /* partial_inplace */
1392 0, /* src_mask */
1393 0xffff, /* dst_mask */
1394 FALSE), /* pcrel_offset */
1396 /* Like DTPREL16, but for insns with a DS field. */
1397 HOWTO (R_PPC64_DTPREL16_DS,
1398 0, /* rightshift */
1399 1, /* size (0 = byte, 1 = short, 2 = long) */
1400 16, /* bitsize */
1401 FALSE, /* pc_relative */
1402 0, /* bitpos */
1403 complain_overflow_signed, /* complain_on_overflow */
1404 ppc64_elf_unhandled_reloc, /* special_function */
1405 "R_PPC64_DTPREL16_DS", /* name */
1406 FALSE, /* partial_inplace */
1407 0, /* src_mask */
1408 0xfffc, /* dst_mask */
1409 FALSE), /* pcrel_offset */
1411 /* Like DTPREL16_DS, but no overflow. */
1412 HOWTO (R_PPC64_DTPREL16_LO_DS,
1413 0, /* rightshift */
1414 1, /* size (0 = byte, 1 = short, 2 = long) */
1415 16, /* bitsize */
1416 FALSE, /* pc_relative */
1417 0, /* bitpos */
1418 complain_overflow_dont, /* complain_on_overflow */
1419 ppc64_elf_unhandled_reloc, /* special_function */
1420 "R_PPC64_DTPREL16_LO_DS", /* name */
1421 FALSE, /* partial_inplace */
1422 0, /* src_mask */
1423 0xfffc, /* dst_mask */
1424 FALSE), /* pcrel_offset */
1426 /* Computes a tp-relative displacement, the difference between the value of
1427 sym+add and the value of the thread pointer (r13). */
1428 HOWTO (R_PPC64_TPREL64,
1429 0, /* rightshift */
1430 4, /* size (0 = byte, 1 = short, 2 = long) */
1431 64, /* bitsize */
1432 FALSE, /* pc_relative */
1433 0, /* bitpos */
1434 complain_overflow_dont, /* complain_on_overflow */
1435 ppc64_elf_unhandled_reloc, /* special_function */
1436 "R_PPC64_TPREL64", /* name */
1437 FALSE, /* partial_inplace */
1438 0, /* src_mask */
1439 ONES (64), /* dst_mask */
1440 FALSE), /* pcrel_offset */
1442 /* A 16 bit tprel reloc. */
1443 HOWTO (R_PPC64_TPREL16,
1444 0, /* rightshift */
1445 1, /* size (0 = byte, 1 = short, 2 = long) */
1446 16, /* bitsize */
1447 FALSE, /* pc_relative */
1448 0, /* bitpos */
1449 complain_overflow_signed, /* complain_on_overflow */
1450 ppc64_elf_unhandled_reloc, /* special_function */
1451 "R_PPC64_TPREL16", /* name */
1452 FALSE, /* partial_inplace */
1453 0, /* src_mask */
1454 0xffff, /* dst_mask */
1455 FALSE), /* pcrel_offset */
1457 /* Like TPREL16, but no overflow. */
1458 HOWTO (R_PPC64_TPREL16_LO,
1459 0, /* rightshift */
1460 1, /* size (0 = byte, 1 = short, 2 = long) */
1461 16, /* bitsize */
1462 FALSE, /* pc_relative */
1463 0, /* bitpos */
1464 complain_overflow_dont, /* complain_on_overflow */
1465 ppc64_elf_unhandled_reloc, /* special_function */
1466 "R_PPC64_TPREL16_LO", /* name */
1467 FALSE, /* partial_inplace */
1468 0, /* src_mask */
1469 0xffff, /* dst_mask */
1470 FALSE), /* pcrel_offset */
1472 /* Like TPREL16_LO, but next higher group of 16 bits. */
1473 HOWTO (R_PPC64_TPREL16_HI,
1474 16, /* rightshift */
1475 1, /* size (0 = byte, 1 = short, 2 = long) */
1476 16, /* bitsize */
1477 FALSE, /* pc_relative */
1478 0, /* bitpos */
1479 complain_overflow_dont, /* complain_on_overflow */
1480 ppc64_elf_unhandled_reloc, /* special_function */
1481 "R_PPC64_TPREL16_HI", /* name */
1482 FALSE, /* partial_inplace */
1483 0, /* src_mask */
1484 0xffff, /* dst_mask */
1485 FALSE), /* pcrel_offset */
1487 /* Like TPREL16_HI, but adjust for low 16 bits. */
1488 HOWTO (R_PPC64_TPREL16_HA,
1489 16, /* rightshift */
1490 1, /* size (0 = byte, 1 = short, 2 = long) */
1491 16, /* bitsize */
1492 FALSE, /* pc_relative */
1493 0, /* bitpos */
1494 complain_overflow_dont, /* complain_on_overflow */
1495 ppc64_elf_unhandled_reloc, /* special_function */
1496 "R_PPC64_TPREL16_HA", /* name */
1497 FALSE, /* partial_inplace */
1498 0, /* src_mask */
1499 0xffff, /* dst_mask */
1500 FALSE), /* pcrel_offset */
1502 /* Like TPREL16_HI, but next higher group of 16 bits. */
1503 HOWTO (R_PPC64_TPREL16_HIGHER,
1504 32, /* rightshift */
1505 1, /* size (0 = byte, 1 = short, 2 = long) */
1506 16, /* bitsize */
1507 FALSE, /* pc_relative */
1508 0, /* bitpos */
1509 complain_overflow_dont, /* complain_on_overflow */
1510 ppc64_elf_unhandled_reloc, /* special_function */
1511 "R_PPC64_TPREL16_HIGHER", /* name */
1512 FALSE, /* partial_inplace */
1513 0, /* src_mask */
1514 0xffff, /* dst_mask */
1515 FALSE), /* pcrel_offset */
1517 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1518 HOWTO (R_PPC64_TPREL16_HIGHERA,
1519 32, /* rightshift */
1520 1, /* size (0 = byte, 1 = short, 2 = long) */
1521 16, /* bitsize */
1522 FALSE, /* pc_relative */
1523 0, /* bitpos */
1524 complain_overflow_dont, /* complain_on_overflow */
1525 ppc64_elf_unhandled_reloc, /* special_function */
1526 "R_PPC64_TPREL16_HIGHERA", /* name */
1527 FALSE, /* partial_inplace */
1528 0, /* src_mask */
1529 0xffff, /* dst_mask */
1530 FALSE), /* pcrel_offset */
1532 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1533 HOWTO (R_PPC64_TPREL16_HIGHEST,
1534 48, /* rightshift */
1535 1, /* size (0 = byte, 1 = short, 2 = long) */
1536 16, /* bitsize */
1537 FALSE, /* pc_relative */
1538 0, /* bitpos */
1539 complain_overflow_dont, /* complain_on_overflow */
1540 ppc64_elf_unhandled_reloc, /* special_function */
1541 "R_PPC64_TPREL16_HIGHEST", /* name */
1542 FALSE, /* partial_inplace */
1543 0, /* src_mask */
1544 0xffff, /* dst_mask */
1545 FALSE), /* pcrel_offset */
1547 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1548 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1549 48, /* rightshift */
1550 1, /* size (0 = byte, 1 = short, 2 = long) */
1551 16, /* bitsize */
1552 FALSE, /* pc_relative */
1553 0, /* bitpos */
1554 complain_overflow_dont, /* complain_on_overflow */
1555 ppc64_elf_unhandled_reloc, /* special_function */
1556 "R_PPC64_TPREL16_HIGHESTA", /* name */
1557 FALSE, /* partial_inplace */
1558 0, /* src_mask */
1559 0xffff, /* dst_mask */
1560 FALSE), /* pcrel_offset */
1562 /* Like TPREL16, but for insns with a DS field. */
1563 HOWTO (R_PPC64_TPREL16_DS,
1564 0, /* rightshift */
1565 1, /* size (0 = byte, 1 = short, 2 = long) */
1566 16, /* bitsize */
1567 FALSE, /* pc_relative */
1568 0, /* bitpos */
1569 complain_overflow_signed, /* complain_on_overflow */
1570 ppc64_elf_unhandled_reloc, /* special_function */
1571 "R_PPC64_TPREL16_DS", /* name */
1572 FALSE, /* partial_inplace */
1573 0, /* src_mask */
1574 0xfffc, /* dst_mask */
1575 FALSE), /* pcrel_offset */
1577 /* Like TPREL16_DS, but no overflow. */
1578 HOWTO (R_PPC64_TPREL16_LO_DS,
1579 0, /* rightshift */
1580 1, /* size (0 = byte, 1 = short, 2 = long) */
1581 16, /* bitsize */
1582 FALSE, /* pc_relative */
1583 0, /* bitpos */
1584 complain_overflow_dont, /* complain_on_overflow */
1585 ppc64_elf_unhandled_reloc, /* special_function */
1586 "R_PPC64_TPREL16_LO_DS", /* name */
1587 FALSE, /* partial_inplace */
1588 0, /* src_mask */
1589 0xfffc, /* dst_mask */
1590 FALSE), /* pcrel_offset */
1592 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1593 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1594 to the first entry relative to the TOC base (r2). */
1595 HOWTO (R_PPC64_GOT_TLSGD16,
1596 0, /* rightshift */
1597 1, /* size (0 = byte, 1 = short, 2 = long) */
1598 16, /* bitsize */
1599 FALSE, /* pc_relative */
1600 0, /* bitpos */
1601 complain_overflow_signed, /* complain_on_overflow */
1602 ppc64_elf_unhandled_reloc, /* special_function */
1603 "R_PPC64_GOT_TLSGD16", /* name */
1604 FALSE, /* partial_inplace */
1605 0, /* src_mask */
1606 0xffff, /* dst_mask */
1607 FALSE), /* pcrel_offset */
1609 /* Like GOT_TLSGD16, but no overflow. */
1610 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1611 0, /* rightshift */
1612 1, /* size (0 = byte, 1 = short, 2 = long) */
1613 16, /* bitsize */
1614 FALSE, /* pc_relative */
1615 0, /* bitpos */
1616 complain_overflow_dont, /* complain_on_overflow */
1617 ppc64_elf_unhandled_reloc, /* special_function */
1618 "R_PPC64_GOT_TLSGD16_LO", /* name */
1619 FALSE, /* partial_inplace */
1620 0, /* src_mask */
1621 0xffff, /* dst_mask */
1622 FALSE), /* pcrel_offset */
1624 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1625 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1626 16, /* rightshift */
1627 1, /* size (0 = byte, 1 = short, 2 = long) */
1628 16, /* bitsize */
1629 FALSE, /* pc_relative */
1630 0, /* bitpos */
1631 complain_overflow_dont, /* complain_on_overflow */
1632 ppc64_elf_unhandled_reloc, /* special_function */
1633 "R_PPC64_GOT_TLSGD16_HI", /* name */
1634 FALSE, /* partial_inplace */
1635 0, /* src_mask */
1636 0xffff, /* dst_mask */
1637 FALSE), /* pcrel_offset */
1639 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1640 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1641 16, /* rightshift */
1642 1, /* size (0 = byte, 1 = short, 2 = long) */
1643 16, /* bitsize */
1644 FALSE, /* pc_relative */
1645 0, /* bitpos */
1646 complain_overflow_dont, /* complain_on_overflow */
1647 ppc64_elf_unhandled_reloc, /* special_function */
1648 "R_PPC64_GOT_TLSGD16_HA", /* name */
1649 FALSE, /* partial_inplace */
1650 0, /* src_mask */
1651 0xffff, /* dst_mask */
1652 FALSE), /* pcrel_offset */
1654 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1655 with values (sym+add)@dtpmod and zero, and computes the offset to the
1656 first entry relative to the TOC base (r2). */
1657 HOWTO (R_PPC64_GOT_TLSLD16,
1658 0, /* rightshift */
1659 1, /* size (0 = byte, 1 = short, 2 = long) */
1660 16, /* bitsize */
1661 FALSE, /* pc_relative */
1662 0, /* bitpos */
1663 complain_overflow_signed, /* complain_on_overflow */
1664 ppc64_elf_unhandled_reloc, /* special_function */
1665 "R_PPC64_GOT_TLSLD16", /* name */
1666 FALSE, /* partial_inplace */
1667 0, /* src_mask */
1668 0xffff, /* dst_mask */
1669 FALSE), /* pcrel_offset */
1671 /* Like GOT_TLSLD16, but no overflow. */
1672 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1673 0, /* rightshift */
1674 1, /* size (0 = byte, 1 = short, 2 = long) */
1675 16, /* bitsize */
1676 FALSE, /* pc_relative */
1677 0, /* bitpos */
1678 complain_overflow_dont, /* complain_on_overflow */
1679 ppc64_elf_unhandled_reloc, /* special_function */
1680 "R_PPC64_GOT_TLSLD16_LO", /* name */
1681 FALSE, /* partial_inplace */
1682 0, /* src_mask */
1683 0xffff, /* dst_mask */
1684 FALSE), /* pcrel_offset */
1686 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1687 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1688 16, /* rightshift */
1689 1, /* size (0 = byte, 1 = short, 2 = long) */
1690 16, /* bitsize */
1691 FALSE, /* pc_relative */
1692 0, /* bitpos */
1693 complain_overflow_dont, /* complain_on_overflow */
1694 ppc64_elf_unhandled_reloc, /* special_function */
1695 "R_PPC64_GOT_TLSLD16_HI", /* name */
1696 FALSE, /* partial_inplace */
1697 0, /* src_mask */
1698 0xffff, /* dst_mask */
1699 FALSE), /* pcrel_offset */
1701 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1702 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1703 16, /* rightshift */
1704 1, /* size (0 = byte, 1 = short, 2 = long) */
1705 16, /* bitsize */
1706 FALSE, /* pc_relative */
1707 0, /* bitpos */
1708 complain_overflow_dont, /* complain_on_overflow */
1709 ppc64_elf_unhandled_reloc, /* special_function */
1710 "R_PPC64_GOT_TLSLD16_HA", /* name */
1711 FALSE, /* partial_inplace */
1712 0, /* src_mask */
1713 0xffff, /* dst_mask */
1714 FALSE), /* pcrel_offset */
1716 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1717 the offset to the entry relative to the TOC base (r2). */
1718 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1719 0, /* rightshift */
1720 1, /* size (0 = byte, 1 = short, 2 = long) */
1721 16, /* bitsize */
1722 FALSE, /* pc_relative */
1723 0, /* bitpos */
1724 complain_overflow_signed, /* complain_on_overflow */
1725 ppc64_elf_unhandled_reloc, /* special_function */
1726 "R_PPC64_GOT_DTPREL16_DS", /* name */
1727 FALSE, /* partial_inplace */
1728 0, /* src_mask */
1729 0xfffc, /* dst_mask */
1730 FALSE), /* pcrel_offset */
1732 /* Like GOT_DTPREL16_DS, but no overflow. */
1733 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1734 0, /* rightshift */
1735 1, /* size (0 = byte, 1 = short, 2 = long) */
1736 16, /* bitsize */
1737 FALSE, /* pc_relative */
1738 0, /* bitpos */
1739 complain_overflow_dont, /* complain_on_overflow */
1740 ppc64_elf_unhandled_reloc, /* special_function */
1741 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1742 FALSE, /* partial_inplace */
1743 0, /* src_mask */
1744 0xfffc, /* dst_mask */
1745 FALSE), /* pcrel_offset */
1747 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1748 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1749 16, /* rightshift */
1750 1, /* size (0 = byte, 1 = short, 2 = long) */
1751 16, /* bitsize */
1752 FALSE, /* pc_relative */
1753 0, /* bitpos */
1754 complain_overflow_dont, /* complain_on_overflow */
1755 ppc64_elf_unhandled_reloc, /* special_function */
1756 "R_PPC64_GOT_DTPREL16_HI", /* name */
1757 FALSE, /* partial_inplace */
1758 0, /* src_mask */
1759 0xffff, /* dst_mask */
1760 FALSE), /* pcrel_offset */
1762 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1763 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1764 16, /* rightshift */
1765 1, /* size (0 = byte, 1 = short, 2 = long) */
1766 16, /* bitsize */
1767 FALSE, /* pc_relative */
1768 0, /* bitpos */
1769 complain_overflow_dont, /* complain_on_overflow */
1770 ppc64_elf_unhandled_reloc, /* special_function */
1771 "R_PPC64_GOT_DTPREL16_HA", /* name */
1772 FALSE, /* partial_inplace */
1773 0, /* src_mask */
1774 0xffff, /* dst_mask */
1775 FALSE), /* pcrel_offset */
1777 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1778 offset to the entry relative to the TOC base (r2). */
1779 HOWTO (R_PPC64_GOT_TPREL16_DS,
1780 0, /* rightshift */
1781 1, /* size (0 = byte, 1 = short, 2 = long) */
1782 16, /* bitsize */
1783 FALSE, /* pc_relative */
1784 0, /* bitpos */
1785 complain_overflow_signed, /* complain_on_overflow */
1786 ppc64_elf_unhandled_reloc, /* special_function */
1787 "R_PPC64_GOT_TPREL16_DS", /* name */
1788 FALSE, /* partial_inplace */
1789 0, /* src_mask */
1790 0xfffc, /* dst_mask */
1791 FALSE), /* pcrel_offset */
1793 /* Like GOT_TPREL16_DS, but no overflow. */
1794 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1795 0, /* rightshift */
1796 1, /* size (0 = byte, 1 = short, 2 = long) */
1797 16, /* bitsize */
1798 FALSE, /* pc_relative */
1799 0, /* bitpos */
1800 complain_overflow_dont, /* complain_on_overflow */
1801 ppc64_elf_unhandled_reloc, /* special_function */
1802 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1803 FALSE, /* partial_inplace */
1804 0, /* src_mask */
1805 0xfffc, /* dst_mask */
1806 FALSE), /* pcrel_offset */
1808 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1809 HOWTO (R_PPC64_GOT_TPREL16_HI,
1810 16, /* rightshift */
1811 1, /* size (0 = byte, 1 = short, 2 = long) */
1812 16, /* bitsize */
1813 FALSE, /* pc_relative */
1814 0, /* bitpos */
1815 complain_overflow_dont, /* complain_on_overflow */
1816 ppc64_elf_unhandled_reloc, /* special_function */
1817 "R_PPC64_GOT_TPREL16_HI", /* name */
1818 FALSE, /* partial_inplace */
1819 0, /* src_mask */
1820 0xffff, /* dst_mask */
1821 FALSE), /* pcrel_offset */
1823 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1824 HOWTO (R_PPC64_GOT_TPREL16_HA,
1825 16, /* rightshift */
1826 1, /* size (0 = byte, 1 = short, 2 = long) */
1827 16, /* bitsize */
1828 FALSE, /* pc_relative */
1829 0, /* bitpos */
1830 complain_overflow_dont, /* complain_on_overflow */
1831 ppc64_elf_unhandled_reloc, /* special_function */
1832 "R_PPC64_GOT_TPREL16_HA", /* name */
1833 FALSE, /* partial_inplace */
1834 0, /* src_mask */
1835 0xffff, /* dst_mask */
1836 FALSE), /* pcrel_offset */
1838 /* GNU extension to record C++ vtable hierarchy. */
1839 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1840 0, /* rightshift */
1841 0, /* size (0 = byte, 1 = short, 2 = long) */
1842 0, /* bitsize */
1843 FALSE, /* pc_relative */
1844 0, /* bitpos */
1845 complain_overflow_dont, /* complain_on_overflow */
1846 NULL, /* special_function */
1847 "R_PPC64_GNU_VTINHERIT", /* name */
1848 FALSE, /* partial_inplace */
1849 0, /* src_mask */
1850 0, /* dst_mask */
1851 FALSE), /* pcrel_offset */
1853 /* GNU extension to record C++ vtable member usage. */
1854 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1855 0, /* rightshift */
1856 0, /* size (0 = byte, 1 = short, 2 = long) */
1857 0, /* bitsize */
1858 FALSE, /* pc_relative */
1859 0, /* bitpos */
1860 complain_overflow_dont, /* complain_on_overflow */
1861 NULL, /* special_function */
1862 "R_PPC64_GNU_VTENTRY", /* name */
1863 FALSE, /* partial_inplace */
1864 0, /* src_mask */
1865 0, /* dst_mask */
1866 FALSE), /* pcrel_offset */
1870 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1871 be done. */
1873 static void
1874 ppc_howto_init (void)
1876 unsigned int i, type;
1878 for (i = 0;
1879 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1880 i++)
1882 type = ppc64_elf_howto_raw[i].type;
1883 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1884 / sizeof (ppc64_elf_howto_table[0])));
1885 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1889 static reloc_howto_type *
1890 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1891 bfd_reloc_code_real_type code)
1893 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1895 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1896 /* Initialize howto table if needed. */
1897 ppc_howto_init ();
1899 switch (code)
1901 default:
1902 return NULL;
1904 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1905 break;
1906 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1907 break;
1908 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1909 break;
1910 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1911 break;
1912 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1913 break;
1914 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1915 break;
1916 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1917 break;
1918 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1919 break;
1920 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1921 break;
1922 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1923 break;
1924 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1925 break;
1926 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1927 break;
1928 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1929 break;
1930 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1931 break;
1932 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1933 break;
1934 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1935 break;
1936 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1937 break;
1938 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1939 break;
1940 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1941 break;
1942 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1943 break;
1944 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1945 break;
1946 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1947 break;
1948 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1949 break;
1950 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1951 break;
1952 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1953 break;
1954 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1955 break;
1956 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1957 break;
1958 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1959 break;
1960 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1961 break;
1962 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1963 break;
1964 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1965 break;
1966 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1967 break;
1968 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1969 break;
1970 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1971 break;
1972 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1973 break;
1974 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1975 break;
1976 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1977 break;
1978 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1979 break;
1980 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1981 break;
1982 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1983 break;
1984 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1985 break;
1986 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1987 break;
1988 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1989 break;
1990 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1991 break;
1992 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1993 break;
1994 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1995 break;
1996 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1997 break;
1998 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1999 break;
2000 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2001 break;
2002 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2003 break;
2004 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2005 break;
2006 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2007 break;
2008 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2009 break;
2010 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2011 break;
2012 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2013 break;
2014 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2015 break;
2016 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2017 break;
2018 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2019 break;
2020 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2021 break;
2022 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2023 break;
2024 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2025 break;
2026 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2027 break;
2028 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2029 break;
2030 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2031 break;
2032 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2033 break;
2034 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2035 break;
2036 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2037 break;
2038 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2039 break;
2040 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2041 break;
2042 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2043 break;
2044 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2045 break;
2046 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2047 break;
2048 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2049 break;
2050 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2051 break;
2052 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2053 break;
2054 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2055 break;
2056 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2057 break;
2058 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2059 break;
2060 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2061 break;
2062 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2063 break;
2064 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2065 break;
2066 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2067 break;
2068 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2069 break;
2070 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2071 break;
2072 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2073 break;
2074 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2075 break;
2076 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2077 break;
2078 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2079 break;
2080 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2081 break;
2082 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2083 break;
2084 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2085 break;
2086 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2087 break;
2088 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2089 break;
2090 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2091 break;
2092 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2093 break;
2094 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2095 break;
2096 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2097 break;
2098 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2099 break;
2100 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2101 break;
2102 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2103 break;
2104 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2105 break;
2108 return ppc64_elf_howto_table[r];
2111 /* Set the howto pointer for a PowerPC ELF reloc. */
2113 static void
2114 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2115 Elf_Internal_Rela *dst)
2117 unsigned int type;
2119 /* Initialize howto table if needed. */
2120 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2121 ppc_howto_init ();
2123 type = ELF64_R_TYPE (dst->r_info);
2124 if (type >= (sizeof (ppc64_elf_howto_table)
2125 / sizeof (ppc64_elf_howto_table[0])))
2127 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2128 abfd, (int) type);
2129 type = R_PPC64_NONE;
2131 cache_ptr->howto = ppc64_elf_howto_table[type];
2134 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2136 static bfd_reloc_status_type
2137 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2138 void *data, asection *input_section,
2139 bfd *output_bfd, char **error_message)
2141 /* If this is a relocatable link (output_bfd test tells us), just
2142 call the generic function. Any adjustment will be done at final
2143 link time. */
2144 if (output_bfd != NULL)
2145 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2146 input_section, output_bfd, error_message);
2148 /* Adjust the addend for sign extension of the low 16 bits.
2149 We won't actually be using the low 16 bits, so trashing them
2150 doesn't matter. */
2151 reloc_entry->addend += 0x8000;
2152 return bfd_reloc_continue;
2155 static bfd_reloc_status_type
2156 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2157 void *data, asection *input_section,
2158 bfd *output_bfd, char **error_message)
2160 if (output_bfd != NULL)
2161 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2162 input_section, output_bfd, error_message);
2164 if (strcmp (symbol->section->name, ".opd") == 0
2165 && (symbol->section->owner->flags & DYNAMIC) == 0)
2167 bfd_vma dest = opd_entry_value (symbol->section,
2168 symbol->value + reloc_entry->addend,
2169 NULL, NULL);
2170 if (dest != (bfd_vma) -1)
2171 reloc_entry->addend = dest - (symbol->value
2172 + symbol->section->output_section->vma
2173 + symbol->section->output_offset);
2175 return bfd_reloc_continue;
2178 static bfd_reloc_status_type
2179 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2180 void *data, asection *input_section,
2181 bfd *output_bfd, char **error_message)
2183 long insn;
2184 enum elf_ppc64_reloc_type r_type;
2185 bfd_size_type octets;
2186 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
2187 bfd_boolean is_power4 = FALSE;
2189 /* If this is a relocatable link (output_bfd test tells us), just
2190 call the generic function. Any adjustment will be done at final
2191 link time. */
2192 if (output_bfd != NULL)
2193 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2194 input_section, output_bfd, error_message);
2196 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2197 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2198 insn &= ~(0x01 << 21);
2199 r_type = reloc_entry->howto->type;
2200 if (r_type == R_PPC64_ADDR14_BRTAKEN
2201 || r_type == R_PPC64_REL14_BRTAKEN)
2202 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2204 if (is_power4)
2206 /* Set 'a' bit. This is 0b00010 in BO field for branch
2207 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2208 for branch on CTR insns (BO == 1a00t or 1a01t). */
2209 if ((insn & (0x14 << 21)) == (0x04 << 21))
2210 insn |= 0x02 << 21;
2211 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2212 insn |= 0x08 << 21;
2213 else
2214 goto out;
2216 else
2218 bfd_vma target = 0;
2219 bfd_vma from;
2221 if (!bfd_is_com_section (symbol->section))
2222 target = symbol->value;
2223 target += symbol->section->output_section->vma;
2224 target += symbol->section->output_offset;
2225 target += reloc_entry->addend;
2227 from = (reloc_entry->address
2228 + input_section->output_offset
2229 + input_section->output_section->vma);
2231 /* Invert 'y' bit if not the default. */
2232 if ((bfd_signed_vma) (target - from) < 0)
2233 insn ^= 0x01 << 21;
2235 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2236 out:
2237 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2238 input_section, output_bfd, error_message);
2241 static bfd_reloc_status_type
2242 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2243 void *data, asection *input_section,
2244 bfd *output_bfd, char **error_message)
2246 /* If this is a relocatable link (output_bfd test tells us), just
2247 call the generic function. Any adjustment will be done at final
2248 link time. */
2249 if (output_bfd != NULL)
2250 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2251 input_section, output_bfd, error_message);
2253 /* Subtract the symbol section base address. */
2254 reloc_entry->addend -= symbol->section->output_section->vma;
2255 return bfd_reloc_continue;
2258 static bfd_reloc_status_type
2259 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2260 void *data, asection *input_section,
2261 bfd *output_bfd, char **error_message)
2263 /* If this is a relocatable link (output_bfd test tells us), just
2264 call the generic function. Any adjustment will be done at final
2265 link time. */
2266 if (output_bfd != NULL)
2267 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2268 input_section, output_bfd, error_message);
2270 /* Subtract the symbol section base address. */
2271 reloc_entry->addend -= symbol->section->output_section->vma;
2273 /* Adjust the addend for sign extension of the low 16 bits. */
2274 reloc_entry->addend += 0x8000;
2275 return bfd_reloc_continue;
2278 static bfd_reloc_status_type
2279 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2280 void *data, asection *input_section,
2281 bfd *output_bfd, char **error_message)
2283 bfd_vma TOCstart;
2285 /* If this is a relocatable link (output_bfd test tells us), just
2286 call the generic function. Any adjustment will be done at final
2287 link time. */
2288 if (output_bfd != NULL)
2289 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2290 input_section, output_bfd, error_message);
2292 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2293 if (TOCstart == 0)
2294 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2296 /* Subtract the TOC base address. */
2297 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2298 return bfd_reloc_continue;
2301 static bfd_reloc_status_type
2302 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2303 void *data, asection *input_section,
2304 bfd *output_bfd, char **error_message)
2306 bfd_vma TOCstart;
2308 /* If this is a relocatable link (output_bfd test tells us), just
2309 call the generic function. Any adjustment will be done at final
2310 link time. */
2311 if (output_bfd != NULL)
2312 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2313 input_section, output_bfd, error_message);
2315 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2316 if (TOCstart == 0)
2317 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2319 /* Subtract the TOC base address. */
2320 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2322 /* Adjust the addend for sign extension of the low 16 bits. */
2323 reloc_entry->addend += 0x8000;
2324 return bfd_reloc_continue;
2327 static bfd_reloc_status_type
2328 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2329 void *data, asection *input_section,
2330 bfd *output_bfd, char **error_message)
2332 bfd_vma TOCstart;
2333 bfd_size_type octets;
2335 /* If this is a relocatable link (output_bfd test tells us), just
2336 call the generic function. Any adjustment will be done at final
2337 link time. */
2338 if (output_bfd != NULL)
2339 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2340 input_section, output_bfd, error_message);
2342 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2343 if (TOCstart == 0)
2344 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2346 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2347 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2348 return bfd_reloc_ok;
2351 static bfd_reloc_status_type
2352 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2353 void *data, asection *input_section,
2354 bfd *output_bfd, char **error_message)
2356 /* If this is a relocatable link (output_bfd test tells us), just
2357 call the generic function. Any adjustment will be done at final
2358 link time. */
2359 if (output_bfd != NULL)
2360 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2361 input_section, output_bfd, error_message);
2363 if (error_message != NULL)
2365 static char buf[60];
2366 sprintf (buf, "generic linker can't handle %s",
2367 reloc_entry->howto->name);
2368 *error_message = buf;
2370 return bfd_reloc_dangerous;
2373 struct ppc64_elf_obj_tdata
2375 struct elf_obj_tdata elf;
2377 /* Shortcuts to dynamic linker sections. */
2378 asection *got;
2379 asection *relgot;
2381 /* Used during garbage collection. We attach global symbols defined
2382 on removed .opd entries to this section so that the sym is removed. */
2383 asection *deleted_section;
2385 /* TLS local dynamic got entry handling. Suppose for multiple GOT
2386 sections means we potentially need one of these for each input bfd. */
2387 union {
2388 bfd_signed_vma refcount;
2389 bfd_vma offset;
2390 } tlsld_got;
2392 /* A copy of relocs before they are modified for --emit-relocs. */
2393 Elf_Internal_Rela *opd_relocs;
2396 #define ppc64_elf_tdata(bfd) \
2397 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2399 #define ppc64_tlsld_got(bfd) \
2400 (&ppc64_elf_tdata (bfd)->tlsld_got)
2402 /* Override the generic function because we store some extras. */
2404 static bfd_boolean
2405 ppc64_elf_mkobject (bfd *abfd)
2407 if (abfd->tdata.any == NULL)
2409 bfd_size_type amt = sizeof (struct ppc64_elf_obj_tdata);
2410 abfd->tdata.any = bfd_zalloc (abfd, amt);
2411 if (abfd->tdata.any == NULL)
2412 return FALSE;
2414 return bfd_elf_mkobject (abfd);
2417 /* Return 1 if target is one of ours. */
2419 static bfd_boolean
2420 is_ppc64_elf_target (const struct bfd_target *targ)
2422 extern const bfd_target bfd_elf64_powerpc_vec;
2423 extern const bfd_target bfd_elf64_powerpcle_vec;
2425 return targ == &bfd_elf64_powerpc_vec || targ == &bfd_elf64_powerpcle_vec;
2428 /* Fix bad default arch selected for a 64 bit input bfd when the
2429 default is 32 bit. */
2431 static bfd_boolean
2432 ppc64_elf_object_p (bfd *abfd)
2434 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2436 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2438 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2440 /* Relies on arch after 32 bit default being 64 bit default. */
2441 abfd->arch_info = abfd->arch_info->next;
2442 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2445 return TRUE;
2448 /* Support for core dump NOTE sections. */
2450 static bfd_boolean
2451 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2453 size_t offset, size;
2455 if (note->descsz != 504)
2456 return FALSE;
2458 /* pr_cursig */
2459 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2461 /* pr_pid */
2462 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 32);
2464 /* pr_reg */
2465 offset = 112;
2466 size = 384;
2468 /* Make a ".reg/999" section. */
2469 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2470 size, note->descpos + offset);
2473 static bfd_boolean
2474 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2476 if (note->descsz != 136)
2477 return FALSE;
2479 elf_tdata (abfd)->core_program
2480 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2481 elf_tdata (abfd)->core_command
2482 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2484 return TRUE;
2487 /* Merge backend specific data from an object file to the output
2488 object file when linking. */
2490 static bfd_boolean
2491 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2493 /* Check if we have the same endianess. */
2494 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2495 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
2496 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2498 const char *msg;
2500 if (bfd_big_endian (ibfd))
2501 msg = _("%B: compiled for a big endian system "
2502 "and target is little endian");
2503 else
2504 msg = _("%B: compiled for a little endian system "
2505 "and target is big endian");
2507 (*_bfd_error_handler) (msg, ibfd);
2509 bfd_set_error (bfd_error_wrong_format);
2510 return FALSE;
2513 return TRUE;
2516 /* Add extra PPC sections. */
2518 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2520 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2521 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2522 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2523 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2524 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2525 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2526 { NULL, 0, 0, 0, 0 }
2529 enum _ppc64_sec_type {
2530 sec_normal = 0,
2531 sec_opd = 1,
2532 sec_toc = 2
2535 struct _ppc64_elf_section_data
2537 struct bfd_elf_section_data elf;
2539 /* An array with one entry for each opd function descriptor. */
2540 union
2542 /* Points to the function code section for local opd entries. */
2543 asection **opd_func_sec;
2544 /* After editing .opd, adjust references to opd local syms. */
2545 long *opd_adjust;
2547 /* An array for toc sections, indexed by offset/8.
2548 Specifies the relocation symbol index used at a given toc offset. */
2549 unsigned *t_symndx;
2550 } u;
2552 enum _ppc64_sec_type sec_type:2;
2554 /* Flag set when small branches are detected. Used to
2555 select suitable defaults for the stub group size. */
2556 unsigned int has_14bit_branch:1;
2559 #define ppc64_elf_section_data(sec) \
2560 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2562 static bfd_boolean
2563 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2565 if (!sec->used_by_bfd)
2567 struct _ppc64_elf_section_data *sdata;
2568 bfd_size_type amt = sizeof (*sdata);
2570 sdata = bfd_zalloc (abfd, amt);
2571 if (sdata == NULL)
2572 return FALSE;
2573 sec->used_by_bfd = sdata;
2576 return _bfd_elf_new_section_hook (abfd, sec);
2579 static void *
2580 get_opd_info (asection * sec)
2582 if (sec != NULL
2583 && ppc64_elf_section_data (sec) != NULL
2584 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2585 return ppc64_elf_section_data (sec)->u.opd_adjust;
2586 return NULL;
2589 /* Parameters for the qsort hook. */
2590 static asection *synthetic_opd;
2591 static bfd_boolean synthetic_relocatable;
2593 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2595 static int
2596 compare_symbols (const void *ap, const void *bp)
2598 const asymbol *a = * (const asymbol **) ap;
2599 const asymbol *b = * (const asymbol **) bp;
2601 /* Section symbols first. */
2602 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2603 return -1;
2604 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2605 return 1;
2607 /* then .opd symbols. */
2608 if (a->section == synthetic_opd && b->section != synthetic_opd)
2609 return -1;
2610 if (a->section != synthetic_opd && b->section == synthetic_opd)
2611 return 1;
2613 /* then other code symbols. */
2614 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2615 == (SEC_CODE | SEC_ALLOC)
2616 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2617 != (SEC_CODE | SEC_ALLOC))
2618 return -1;
2620 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2621 != (SEC_CODE | SEC_ALLOC)
2622 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2623 == (SEC_CODE | SEC_ALLOC))
2624 return 1;
2626 if (synthetic_relocatable)
2628 if (a->section->id < b->section->id)
2629 return -1;
2631 if (a->section->id > b->section->id)
2632 return 1;
2635 if (a->value + a->section->vma < b->value + b->section->vma)
2636 return -1;
2638 if (a->value + a->section->vma > b->value + b->section->vma)
2639 return 1;
2641 /* For syms with the same value, prefer strong dynamic global function
2642 syms over other syms. */
2643 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2644 return -1;
2646 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2647 return 1;
2649 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2650 return -1;
2652 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2653 return 1;
2655 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2656 return -1;
2658 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2659 return 1;
2661 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2662 return -1;
2664 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2665 return 1;
2667 return 0;
2670 /* Search SYMS for a symbol of the given VALUE. */
2672 static asymbol *
2673 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
2675 long mid;
2677 if (id == -1)
2679 while (lo < hi)
2681 mid = (lo + hi) >> 1;
2682 if (syms[mid]->value + syms[mid]->section->vma < value)
2683 lo = mid + 1;
2684 else if (syms[mid]->value + syms[mid]->section->vma > value)
2685 hi = mid;
2686 else
2687 return syms[mid];
2690 else
2692 while (lo < hi)
2694 mid = (lo + hi) >> 1;
2695 if (syms[mid]->section->id < id)
2696 lo = mid + 1;
2697 else if (syms[mid]->section->id > id)
2698 hi = mid;
2699 else if (syms[mid]->value < value)
2700 lo = mid + 1;
2701 else if (syms[mid]->value > value)
2702 hi = mid;
2703 else
2704 return syms[mid];
2707 return NULL;
2710 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2711 entry syms. */
2713 static long
2714 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2715 long static_count, asymbol **static_syms,
2716 long dyn_count, asymbol **dyn_syms,
2717 asymbol **ret)
2719 asymbol *s;
2720 long i;
2721 long count;
2722 char *names;
2723 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2724 asection *opd;
2725 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2726 asymbol **syms;
2728 *ret = NULL;
2730 opd = bfd_get_section_by_name (abfd, ".opd");
2731 if (opd == NULL)
2732 return 0;
2734 symcount = static_count;
2735 if (!relocatable)
2736 symcount += dyn_count;
2737 if (symcount == 0)
2738 return 0;
2740 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2741 if (syms == NULL)
2742 return -1;
2744 if (!relocatable && static_count != 0 && dyn_count != 0)
2746 /* Use both symbol tables. */
2747 memcpy (syms, static_syms, static_count * sizeof (*syms));
2748 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
2750 else if (!relocatable && static_count == 0)
2751 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2752 else
2753 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2755 synthetic_opd = opd;
2756 synthetic_relocatable = relocatable;
2757 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2759 if (!relocatable && symcount > 1)
2761 long j;
2762 /* Trim duplicate syms, since we may have merged the normal and
2763 dynamic symbols. Actually, we only care about syms that have
2764 different values, so trim any with the same value. */
2765 for (i = 1, j = 1; i < symcount; ++i)
2766 if (syms[i - 1]->value + syms[i - 1]->section->vma
2767 != syms[i]->value + syms[i]->section->vma)
2768 syms[j++] = syms[i];
2769 symcount = j;
2772 i = 0;
2773 if (syms[i]->section == opd)
2774 ++i;
2775 codesecsym = i;
2777 for (; i < symcount; ++i)
2778 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2779 != (SEC_CODE | SEC_ALLOC))
2780 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2781 break;
2782 codesecsymend = i;
2784 for (; i < symcount; ++i)
2785 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2786 break;
2787 secsymend = i;
2789 for (; i < symcount; ++i)
2790 if (syms[i]->section != opd)
2791 break;
2792 opdsymend = i;
2794 for (; i < symcount; ++i)
2795 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2796 != (SEC_CODE | SEC_ALLOC))
2797 break;
2798 symcount = i;
2800 count = 0;
2801 if (opdsymend == secsymend)
2802 goto done;
2804 if (relocatable)
2806 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2807 arelent *r;
2808 size_t size;
2809 long relcount;
2811 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2812 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2813 if (relcount == 0)
2814 goto done;
2816 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2818 count = -1;
2819 goto done;
2822 size = 0;
2823 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2825 asymbol *sym;
2827 while (r < opd->relocation + relcount
2828 && r->address < syms[i]->value + opd->vma)
2829 ++r;
2831 if (r == opd->relocation + relcount)
2832 break;
2834 if (r->address != syms[i]->value + opd->vma)
2835 continue;
2837 if (r->howto->type != R_PPC64_ADDR64)
2838 continue;
2840 sym = *r->sym_ptr_ptr;
2841 if (!sym_exists_at (syms, opdsymend, symcount,
2842 sym->section->id, sym->value + r->addend))
2844 ++count;
2845 size += sizeof (asymbol);
2846 size += strlen (syms[i]->name) + 2;
2850 s = *ret = bfd_malloc (size);
2851 if (s == NULL)
2853 count = -1;
2854 goto done;
2857 names = (char *) (s + count);
2859 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2861 asymbol *sym;
2863 while (r < opd->relocation + relcount
2864 && r->address < syms[i]->value + opd->vma)
2865 ++r;
2867 if (r == opd->relocation + relcount)
2868 break;
2870 if (r->address != syms[i]->value + opd->vma)
2871 continue;
2873 if (r->howto->type != R_PPC64_ADDR64)
2874 continue;
2876 sym = *r->sym_ptr_ptr;
2877 if (!sym_exists_at (syms, opdsymend, symcount,
2878 sym->section->id, sym->value + r->addend))
2880 size_t len;
2882 *s = *syms[i];
2883 s->section = sym->section;
2884 s->value = sym->value + r->addend;
2885 s->name = names;
2886 *names++ = '.';
2887 len = strlen (syms[i]->name);
2888 memcpy (names, syms[i]->name, len + 1);
2889 names += len + 1;
2890 s++;
2894 else
2896 bfd_byte *contents;
2897 size_t size;
2899 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
2901 if (contents)
2903 free_contents_and_exit:
2904 free (contents);
2906 count = -1;
2907 goto done;
2910 size = 0;
2911 for (i = secsymend; i < opdsymend; ++i)
2913 bfd_vma ent;
2915 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2916 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2918 ++count;
2919 size += sizeof (asymbol);
2920 size += strlen (syms[i]->name) + 2;
2924 s = *ret = bfd_malloc (size);
2925 if (s == NULL)
2926 goto free_contents_and_exit;
2928 names = (char *) (s + count);
2930 for (i = secsymend; i < opdsymend; ++i)
2932 bfd_vma ent;
2934 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2935 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2937 long lo, hi;
2938 size_t len;
2939 asection *sec = abfd->sections;
2941 *s = *syms[i];
2942 lo = codesecsym;
2943 hi = codesecsymend;
2944 while (lo < hi)
2946 long mid = (lo + hi) >> 1;
2947 if (syms[mid]->section->vma < ent)
2948 lo = mid + 1;
2949 else if (syms[mid]->section->vma > ent)
2950 hi = mid;
2951 else
2953 sec = syms[mid]->section;
2954 break;
2958 if (lo >= hi && lo > codesecsym)
2959 sec = syms[lo - 1]->section;
2961 for (; sec != NULL; sec = sec->next)
2963 if (sec->vma > ent)
2964 break;
2965 if ((sec->flags & SEC_ALLOC) == 0
2966 || (sec->flags & SEC_LOAD) == 0)
2967 break;
2968 if ((sec->flags & SEC_CODE) != 0)
2969 s->section = sec;
2971 s->value = ent - s->section->vma;
2972 s->name = names;
2973 *names++ = '.';
2974 len = strlen (syms[i]->name);
2975 memcpy (names, syms[i]->name, len + 1);
2976 names += len + 1;
2977 s++;
2980 free (contents);
2983 done:
2984 free (syms);
2985 return count;
2988 /* The following functions are specific to the ELF linker, while
2989 functions above are used generally. Those named ppc64_elf_* are
2990 called by the main ELF linker code. They appear in this file more
2991 or less in the order in which they are called. eg.
2992 ppc64_elf_check_relocs is called early in the link process,
2993 ppc64_elf_finish_dynamic_sections is one of the last functions
2994 called.
2996 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2997 functions have both a function code symbol and a function descriptor
2998 symbol. A call to foo in a relocatable object file looks like:
3000 . .text
3001 . x:
3002 . bl .foo
3003 . nop
3005 The function definition in another object file might be:
3007 . .section .opd
3008 . foo: .quad .foo
3009 . .quad .TOC.@tocbase
3010 . .quad 0
3012 . .text
3013 . .foo: blr
3015 When the linker resolves the call during a static link, the branch
3016 unsurprisingly just goes to .foo and the .opd information is unused.
3017 If the function definition is in a shared library, things are a little
3018 different: The call goes via a plt call stub, the opd information gets
3019 copied to the plt, and the linker patches the nop.
3021 . x:
3022 . bl .foo_stub
3023 . ld 2,40(1)
3026 . .foo_stub:
3027 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
3028 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
3029 . std 2,40(1) # this is the general idea
3030 . ld 11,0(12)
3031 . ld 2,8(12)
3032 . mtctr 11
3033 . ld 11,16(12)
3034 . bctr
3036 . .section .plt
3037 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3039 The "reloc ()" notation is supposed to indicate that the linker emits
3040 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3041 copying.
3043 What are the difficulties here? Well, firstly, the relocations
3044 examined by the linker in check_relocs are against the function code
3045 sym .foo, while the dynamic relocation in the plt is emitted against
3046 the function descriptor symbol, foo. Somewhere along the line, we need
3047 to carefully copy dynamic link information from one symbol to the other.
3048 Secondly, the generic part of the elf linker will make .foo a dynamic
3049 symbol as is normal for most other backends. We need foo dynamic
3050 instead, at least for an application final link. However, when
3051 creating a shared library containing foo, we need to have both symbols
3052 dynamic so that references to .foo are satisfied during the early
3053 stages of linking. Otherwise the linker might decide to pull in a
3054 definition from some other object, eg. a static library.
3056 Update: As of August 2004, we support a new convention. Function
3057 calls may use the function descriptor symbol, ie. "bl foo". This
3058 behaves exactly as "bl .foo". */
3060 /* The linker needs to keep track of the number of relocs that it
3061 decides to copy as dynamic relocs in check_relocs for each symbol.
3062 This is so that it can later discard them if they are found to be
3063 unnecessary. We store the information in a field extending the
3064 regular ELF linker hash table. */
3066 struct ppc_dyn_relocs
3068 struct ppc_dyn_relocs *next;
3070 /* The input section of the reloc. */
3071 asection *sec;
3073 /* Total number of relocs copied for the input section. */
3074 bfd_size_type count;
3076 /* Number of pc-relative relocs copied for the input section. */
3077 bfd_size_type pc_count;
3080 /* Track GOT entries needed for a given symbol. We might need more
3081 than one got entry per symbol. */
3082 struct got_entry
3084 struct got_entry *next;
3086 /* The symbol addend that we'll be placing in the GOT. */
3087 bfd_vma addend;
3089 /* Unlike other ELF targets, we use separate GOT entries for the same
3090 symbol referenced from different input files. This is to support
3091 automatic multiple TOC/GOT sections, where the TOC base can vary
3092 from one input file to another.
3094 Point to the BFD owning this GOT entry. */
3095 bfd *owner;
3097 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
3098 TLS_TPREL or TLS_DTPREL for tls entries. */
3099 char tls_type;
3101 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
3102 union
3104 bfd_signed_vma refcount;
3105 bfd_vma offset;
3106 } got;
3109 /* The same for PLT. */
3110 struct plt_entry
3112 struct plt_entry *next;
3114 bfd_vma addend;
3116 union
3118 bfd_signed_vma refcount;
3119 bfd_vma offset;
3120 } plt;
3123 /* Of those relocs that might be copied as dynamic relocs, this macro
3124 selects those that must be copied when linking a shared library,
3125 even when the symbol is local. */
3127 #define MUST_BE_DYN_RELOC(RTYPE) \
3128 ((RTYPE) != R_PPC64_REL32 \
3129 && (RTYPE) != R_PPC64_REL64 \
3130 && (RTYPE) != R_PPC64_REL30)
3132 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3133 copying dynamic variables from a shared lib into an app's dynbss
3134 section, and instead use a dynamic relocation to point into the
3135 shared lib. With code that gcc generates, it's vital that this be
3136 enabled; In the PowerPC64 ABI, the address of a function is actually
3137 the address of a function descriptor, which resides in the .opd
3138 section. gcc uses the descriptor directly rather than going via the
3139 GOT as some other ABI's do, which means that initialized function
3140 pointers must reference the descriptor. Thus, a function pointer
3141 initialized to the address of a function in a shared library will
3142 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3143 redefines the function descriptor symbol to point to the copy. This
3144 presents a problem as a plt entry for that function is also
3145 initialized from the function descriptor symbol and the copy reloc
3146 may not be initialized first. */
3147 #define ELIMINATE_COPY_RELOCS 1
3149 /* Section name for stubs is the associated section name plus this
3150 string. */
3151 #define STUB_SUFFIX ".stub"
3153 /* Linker stubs.
3154 ppc_stub_long_branch:
3155 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3156 destination, but a 24 bit branch in a stub section will reach.
3157 . b dest
3159 ppc_stub_plt_branch:
3160 Similar to the above, but a 24 bit branch in the stub section won't
3161 reach its destination.
3162 . addis %r12,%r2,xxx@toc@ha
3163 . ld %r11,xxx@toc@l(%r12)
3164 . mtctr %r11
3165 . bctr
3167 ppc_stub_plt_call:
3168 Used to call a function in a shared library. If it so happens that
3169 the plt entry referenced crosses a 64k boundary, then an extra
3170 "addis %r12,%r12,1" will be inserted before the load at xxx+8 or
3171 xxx+16 as appropriate.
3172 . addis %r12,%r2,xxx@toc@ha
3173 . std %r2,40(%r1)
3174 . ld %r11,xxx+0@toc@l(%r12)
3175 . ld %r2,xxx+8@toc@l(%r12)
3176 . mtctr %r11
3177 . ld %r11,xxx+16@toc@l(%r12)
3178 . bctr
3180 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3181 code to adjust the value and save r2 to support multiple toc sections.
3182 A ppc_stub_long_branch with an r2 offset looks like:
3183 . std %r2,40(%r1)
3184 . addis %r2,%r2,off@ha
3185 . addi %r2,%r2,off@l
3186 . b dest
3188 A ppc_stub_plt_branch with an r2 offset looks like:
3189 . std %r2,40(%r1)
3190 . addis %r12,%r2,xxx@toc@ha
3191 . ld %r11,xxx@toc@l(%r12)
3192 . addis %r2,%r2,off@ha
3193 . addi %r2,%r2,off@l
3194 . mtctr %r11
3195 . bctr
3198 enum ppc_stub_type {
3199 ppc_stub_none,
3200 ppc_stub_long_branch,
3201 ppc_stub_long_branch_r2off,
3202 ppc_stub_plt_branch,
3203 ppc_stub_plt_branch_r2off,
3204 ppc_stub_plt_call
3207 struct ppc_stub_hash_entry {
3209 /* Base hash table entry structure. */
3210 struct bfd_hash_entry root;
3212 enum ppc_stub_type stub_type;
3214 /* The stub section. */
3215 asection *stub_sec;
3217 /* Offset within stub_sec of the beginning of this stub. */
3218 bfd_vma stub_offset;
3220 /* Given the symbol's value and its section we can determine its final
3221 value when building the stubs (so the stub knows where to jump. */
3222 bfd_vma target_value;
3223 asection *target_section;
3225 /* The symbol table entry, if any, that this was derived from. */
3226 struct ppc_link_hash_entry *h;
3228 /* And the reloc addend that this was derived from. */
3229 bfd_vma addend;
3231 /* Where this stub is being called from, or, in the case of combined
3232 stub sections, the first input section in the group. */
3233 asection *id_sec;
3236 struct ppc_branch_hash_entry {
3238 /* Base hash table entry structure. */
3239 struct bfd_hash_entry root;
3241 /* Offset within branch lookup table. */
3242 unsigned int offset;
3244 /* Generation marker. */
3245 unsigned int iter;
3248 struct ppc_link_hash_entry
3250 struct elf_link_hash_entry elf;
3252 union {
3253 /* A pointer to the most recently used stub hash entry against this
3254 symbol. */
3255 struct ppc_stub_hash_entry *stub_cache;
3257 /* A pointer to the next symbol starting with a '.' */
3258 struct ppc_link_hash_entry *next_dot_sym;
3259 } u;
3261 /* Track dynamic relocs copied for this symbol. */
3262 struct ppc_dyn_relocs *dyn_relocs;
3264 /* Link between function code and descriptor symbols. */
3265 struct ppc_link_hash_entry *oh;
3267 /* Flag function code and descriptor symbols. */
3268 unsigned int is_func:1;
3269 unsigned int is_func_descriptor:1;
3270 unsigned int fake:1;
3272 /* Whether global opd/toc sym has been adjusted or not.
3273 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3274 should be set for all globals defined in any opd/toc section. */
3275 unsigned int adjust_done:1;
3277 /* Set if we twiddled this symbol to weak at some stage. */
3278 unsigned int was_undefined:1;
3280 /* Contexts in which symbol is used in the GOT (or TOC).
3281 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3282 corresponding relocs are encountered during check_relocs.
3283 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3284 indicate the corresponding GOT entry type is not needed.
3285 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3286 a TPREL one. We use a separate flag rather than setting TPREL
3287 just for convenience in distinguishing the two cases. */
3288 #define TLS_GD 1 /* GD reloc. */
3289 #define TLS_LD 2 /* LD reloc. */
3290 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
3291 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3292 #define TLS_TLS 16 /* Any TLS reloc. */
3293 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3294 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3295 char tls_mask;
3298 /* ppc64 ELF linker hash table. */
3300 struct ppc_link_hash_table
3302 struct elf_link_hash_table elf;
3304 /* The stub hash table. */
3305 struct bfd_hash_table stub_hash_table;
3307 /* Another hash table for plt_branch stubs. */
3308 struct bfd_hash_table branch_hash_table;
3310 /* Linker stub bfd. */
3311 bfd *stub_bfd;
3313 /* Linker call-backs. */
3314 asection * (*add_stub_section) (const char *, asection *);
3315 void (*layout_sections_again) (void);
3317 /* Array to keep track of which stub sections have been created, and
3318 information on stub grouping. */
3319 struct map_stub {
3320 /* This is the section to which stubs in the group will be attached. */
3321 asection *link_sec;
3322 /* The stub section. */
3323 asection *stub_sec;
3324 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3325 bfd_vma toc_off;
3326 } *stub_group;
3328 /* Temp used when calculating TOC pointers. */
3329 bfd_vma toc_curr;
3331 /* Highest input section id. */
3332 int top_id;
3334 /* Highest output section index. */
3335 int top_index;
3337 /* Used when adding symbols. */
3338 struct ppc_link_hash_entry *dot_syms;
3340 /* List of input sections for each output section. */
3341 asection **input_list;
3343 /* Short-cuts to get to dynamic linker sections. */
3344 asection *got;
3345 asection *plt;
3346 asection *relplt;
3347 asection *dynbss;
3348 asection *relbss;
3349 asection *glink;
3350 asection *sfpr;
3351 asection *brlt;
3352 asection *relbrlt;
3354 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3355 struct ppc_link_hash_entry *tls_get_addr;
3356 struct ppc_link_hash_entry *tls_get_addr_fd;
3358 /* Statistics. */
3359 unsigned long stub_count[ppc_stub_plt_call];
3361 /* Number of stubs against global syms. */
3362 unsigned long stub_globals;
3364 /* Set if we should emit symbols for stubs. */
3365 unsigned int emit_stub_syms:1;
3367 /* Support for multiple toc sections. */
3368 unsigned int no_multi_toc:1;
3369 unsigned int multi_toc_needed:1;
3371 /* Set on error. */
3372 unsigned int stub_error:1;
3374 /* Temp used by ppc64_elf_check_directives. */
3375 unsigned int twiddled_syms:1;
3377 /* Incremented every time we size stubs. */
3378 unsigned int stub_iteration;
3380 /* Small local sym to section mapping cache. */
3381 struct sym_sec_cache sym_sec;
3384 /* Rename some of the generic section flags to better document how they
3385 are used here. */
3386 #define has_toc_reloc has_gp_reloc
3387 #define makes_toc_func_call need_finalize_relax
3388 #define call_check_in_progress reloc_done
3390 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3392 #define ppc_hash_table(p) \
3393 ((struct ppc_link_hash_table *) ((p)->hash))
3395 #define ppc_stub_hash_lookup(table, string, create, copy) \
3396 ((struct ppc_stub_hash_entry *) \
3397 bfd_hash_lookup ((table), (string), (create), (copy)))
3399 #define ppc_branch_hash_lookup(table, string, create, copy) \
3400 ((struct ppc_branch_hash_entry *) \
3401 bfd_hash_lookup ((table), (string), (create), (copy)))
3403 /* Create an entry in the stub hash table. */
3405 static struct bfd_hash_entry *
3406 stub_hash_newfunc (struct bfd_hash_entry *entry,
3407 struct bfd_hash_table *table,
3408 const char *string)
3410 /* Allocate the structure if it has not already been allocated by a
3411 subclass. */
3412 if (entry == NULL)
3414 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3415 if (entry == NULL)
3416 return entry;
3419 /* Call the allocation method of the superclass. */
3420 entry = bfd_hash_newfunc (entry, table, string);
3421 if (entry != NULL)
3423 struct ppc_stub_hash_entry *eh;
3425 /* Initialize the local fields. */
3426 eh = (struct ppc_stub_hash_entry *) entry;
3427 eh->stub_type = ppc_stub_none;
3428 eh->stub_sec = NULL;
3429 eh->stub_offset = 0;
3430 eh->target_value = 0;
3431 eh->target_section = NULL;
3432 eh->h = NULL;
3433 eh->id_sec = NULL;
3436 return entry;
3439 /* Create an entry in the branch hash table. */
3441 static struct bfd_hash_entry *
3442 branch_hash_newfunc (struct bfd_hash_entry *entry,
3443 struct bfd_hash_table *table,
3444 const char *string)
3446 /* Allocate the structure if it has not already been allocated by a
3447 subclass. */
3448 if (entry == NULL)
3450 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3451 if (entry == NULL)
3452 return entry;
3455 /* Call the allocation method of the superclass. */
3456 entry = bfd_hash_newfunc (entry, table, string);
3457 if (entry != NULL)
3459 struct ppc_branch_hash_entry *eh;
3461 /* Initialize the local fields. */
3462 eh = (struct ppc_branch_hash_entry *) entry;
3463 eh->offset = 0;
3464 eh->iter = 0;
3467 return entry;
3470 /* Create an entry in a ppc64 ELF linker hash table. */
3472 static struct bfd_hash_entry *
3473 link_hash_newfunc (struct bfd_hash_entry *entry,
3474 struct bfd_hash_table *table,
3475 const char *string)
3477 /* Allocate the structure if it has not already been allocated by a
3478 subclass. */
3479 if (entry == NULL)
3481 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3482 if (entry == NULL)
3483 return entry;
3486 /* Call the allocation method of the superclass. */
3487 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3488 if (entry != NULL)
3490 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3492 memset (&eh->u.stub_cache, 0,
3493 (sizeof (struct ppc_link_hash_entry)
3494 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3496 /* When making function calls, old ABI code references function entry
3497 points (dot symbols), while new ABI code references the function
3498 descriptor symbol. We need to make any combination of reference and
3499 definition work together, without breaking archive linking.
3501 For a defined function "foo" and an undefined call to "bar":
3502 An old object defines "foo" and ".foo", references ".bar" (possibly
3503 "bar" too).
3504 A new object defines "foo" and references "bar".
3506 A new object thus has no problem with its undefined symbols being
3507 satisfied by definitions in an old object. On the other hand, the
3508 old object won't have ".bar" satisfied by a new object.
3510 Keep a list of newly added dot-symbols. */
3512 if (string[0] == '.')
3514 struct ppc_link_hash_table *htab;
3516 htab = (struct ppc_link_hash_table *) table;
3517 eh->u.next_dot_sym = htab->dot_syms;
3518 htab->dot_syms = eh;
3522 return entry;
3525 /* Create a ppc64 ELF linker hash table. */
3527 static struct bfd_link_hash_table *
3528 ppc64_elf_link_hash_table_create (bfd *abfd)
3530 struct ppc_link_hash_table *htab;
3531 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3533 htab = bfd_zmalloc (amt);
3534 if (htab == NULL)
3535 return NULL;
3537 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3538 sizeof (struct ppc_link_hash_entry)))
3540 free (htab);
3541 return NULL;
3544 /* Init the stub hash table too. */
3545 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3546 sizeof (struct ppc_stub_hash_entry)))
3547 return NULL;
3549 /* And the branch hash table. */
3550 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3551 sizeof (struct ppc_branch_hash_entry)))
3552 return NULL;
3554 /* Initializing two fields of the union is just cosmetic. We really
3555 only care about glist, but when compiled on a 32-bit host the
3556 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3557 debugger inspection of these fields look nicer. */
3558 htab->elf.init_got_refcount.refcount = 0;
3559 htab->elf.init_got_refcount.glist = NULL;
3560 htab->elf.init_plt_refcount.refcount = 0;
3561 htab->elf.init_plt_refcount.glist = NULL;
3562 htab->elf.init_got_offset.offset = 0;
3563 htab->elf.init_got_offset.glist = NULL;
3564 htab->elf.init_plt_offset.offset = 0;
3565 htab->elf.init_plt_offset.glist = NULL;
3567 return &htab->elf.root;
3570 /* Free the derived linker hash table. */
3572 static void
3573 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
3575 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
3577 bfd_hash_table_free (&ret->stub_hash_table);
3578 bfd_hash_table_free (&ret->branch_hash_table);
3579 _bfd_generic_link_hash_table_free (hash);
3582 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3584 void
3585 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
3587 struct ppc_link_hash_table *htab;
3589 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
3591 /* Always hook our dynamic sections into the first bfd, which is the
3592 linker created stub bfd. This ensures that the GOT header is at
3593 the start of the output TOC section. */
3594 htab = ppc_hash_table (info);
3595 htab->stub_bfd = abfd;
3596 htab->elf.dynobj = abfd;
3599 /* Build a name for an entry in the stub hash table. */
3601 static char *
3602 ppc_stub_name (const asection *input_section,
3603 const asection *sym_sec,
3604 const struct ppc_link_hash_entry *h,
3605 const Elf_Internal_Rela *rel)
3607 char *stub_name;
3608 bfd_size_type len;
3610 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3611 offsets from a sym as a branch target? In fact, we could
3612 probably assume the addend is always zero. */
3613 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3615 if (h)
3617 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3618 stub_name = bfd_malloc (len);
3619 if (stub_name == NULL)
3620 return stub_name;
3622 sprintf (stub_name, "%08x.%s+%x",
3623 input_section->id & 0xffffffff,
3624 h->elf.root.root.string,
3625 (int) rel->r_addend & 0xffffffff);
3627 else
3629 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3630 stub_name = bfd_malloc (len);
3631 if (stub_name == NULL)
3632 return stub_name;
3634 sprintf (stub_name, "%08x.%x:%x+%x",
3635 input_section->id & 0xffffffff,
3636 sym_sec->id & 0xffffffff,
3637 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3638 (int) rel->r_addend & 0xffffffff);
3640 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3641 stub_name[len - 2] = 0;
3642 return stub_name;
3645 /* Look up an entry in the stub hash. Stub entries are cached because
3646 creating the stub name takes a bit of time. */
3648 static struct ppc_stub_hash_entry *
3649 ppc_get_stub_entry (const asection *input_section,
3650 const asection *sym_sec,
3651 struct ppc_link_hash_entry *h,
3652 const Elf_Internal_Rela *rel,
3653 struct ppc_link_hash_table *htab)
3655 struct ppc_stub_hash_entry *stub_entry;
3656 const asection *id_sec;
3658 /* If this input section is part of a group of sections sharing one
3659 stub section, then use the id of the first section in the group.
3660 Stub names need to include a section id, as there may well be
3661 more than one stub used to reach say, printf, and we need to
3662 distinguish between them. */
3663 id_sec = htab->stub_group[input_section->id].link_sec;
3665 if (h != NULL && h->u.stub_cache != NULL
3666 && h->u.stub_cache->h == h
3667 && h->u.stub_cache->id_sec == id_sec)
3669 stub_entry = h->u.stub_cache;
3671 else
3673 char *stub_name;
3675 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
3676 if (stub_name == NULL)
3677 return NULL;
3679 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3680 stub_name, FALSE, FALSE);
3681 if (h != NULL)
3682 h->u.stub_cache = stub_entry;
3684 free (stub_name);
3687 return stub_entry;
3690 /* Add a new stub entry to the stub hash. Not all fields of the new
3691 stub entry are initialised. */
3693 static struct ppc_stub_hash_entry *
3694 ppc_add_stub (const char *stub_name,
3695 asection *section,
3696 struct ppc_link_hash_table *htab)
3698 asection *link_sec;
3699 asection *stub_sec;
3700 struct ppc_stub_hash_entry *stub_entry;
3702 link_sec = htab->stub_group[section->id].link_sec;
3703 stub_sec = htab->stub_group[section->id].stub_sec;
3704 if (stub_sec == NULL)
3706 stub_sec = htab->stub_group[link_sec->id].stub_sec;
3707 if (stub_sec == NULL)
3709 size_t namelen;
3710 bfd_size_type len;
3711 char *s_name;
3713 namelen = strlen (link_sec->name);
3714 len = namelen + sizeof (STUB_SUFFIX);
3715 s_name = bfd_alloc (htab->stub_bfd, len);
3716 if (s_name == NULL)
3717 return NULL;
3719 memcpy (s_name, link_sec->name, namelen);
3720 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3721 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3722 if (stub_sec == NULL)
3723 return NULL;
3724 htab->stub_group[link_sec->id].stub_sec = stub_sec;
3726 htab->stub_group[section->id].stub_sec = stub_sec;
3729 /* Enter this entry into the linker stub hash table. */
3730 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3731 TRUE, FALSE);
3732 if (stub_entry == NULL)
3734 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
3735 section->owner, stub_name);
3736 return NULL;
3739 stub_entry->stub_sec = stub_sec;
3740 stub_entry->stub_offset = 0;
3741 stub_entry->id_sec = link_sec;
3742 return stub_entry;
3745 /* Create sections for linker generated code. */
3747 static bfd_boolean
3748 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3750 struct ppc_link_hash_table *htab;
3751 flagword flags;
3753 htab = ppc_hash_table (info);
3755 /* Create .sfpr for code to save and restore fp regs. */
3756 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3757 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3758 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3759 flags);
3760 if (htab->sfpr == NULL
3761 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3762 return FALSE;
3764 /* Create .glink for lazy dynamic linking support. */
3765 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3766 flags);
3767 if (htab->glink == NULL
3768 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
3769 return FALSE;
3771 /* Create branch lookup table for plt_branch stubs. */
3772 if (info->shared)
3774 flags = (SEC_ALLOC | SEC_LOAD
3775 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3776 htab->brlt
3777 = bfd_make_section_anyway_with_flags (dynobj, ".data.rel.ro.brlt",
3778 flags);
3780 else
3782 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3783 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3784 htab->brlt
3785 = bfd_make_section_anyway_with_flags (dynobj, ".rodata.brlt", flags);
3788 if (htab->brlt == NULL
3789 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
3790 return FALSE;
3792 if (info->shared)
3794 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3795 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3796 htab->relbrlt
3797 = bfd_make_section_anyway_with_flags (dynobj, ".rela.data.rel.ro.brlt",
3798 flags);
3800 else if (info->emitrelocations)
3802 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3803 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3804 htab->relbrlt
3805 = bfd_make_section_anyway_with_flags (dynobj, ".rela.rodata.brlt",
3806 flags);
3808 else
3809 return TRUE;
3811 if (!htab->relbrlt
3812 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3813 return FALSE;
3815 return TRUE;
3818 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3819 not already done. */
3821 static bfd_boolean
3822 create_got_section (bfd *abfd, struct bfd_link_info *info)
3824 asection *got, *relgot;
3825 flagword flags;
3826 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3828 if (!htab->got)
3830 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
3831 return FALSE;
3833 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
3834 if (!htab->got)
3835 abort ();
3838 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3839 | SEC_LINKER_CREATED);
3841 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3842 if (!got
3843 || !bfd_set_section_alignment (abfd, got, 3))
3844 return FALSE;
3846 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3847 flags | SEC_READONLY);
3848 if (!relgot
3849 || ! bfd_set_section_alignment (abfd, relgot, 3))
3850 return FALSE;
3852 ppc64_elf_tdata (abfd)->got = got;
3853 ppc64_elf_tdata (abfd)->relgot = relgot;
3854 return TRUE;
3857 /* Create the dynamic sections, and set up shortcuts. */
3859 static bfd_boolean
3860 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3862 struct ppc_link_hash_table *htab;
3864 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3865 return FALSE;
3867 htab = ppc_hash_table (info);
3868 if (!htab->got)
3869 htab->got = bfd_get_section_by_name (dynobj, ".got");
3870 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
3871 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
3872 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
3873 if (!info->shared)
3874 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
3876 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
3877 || (!info->shared && !htab->relbss))
3878 abort ();
3880 return TRUE;
3883 /* Merge PLT info on FROM with that on TO. */
3885 static void
3886 move_plt_plist (struct ppc_link_hash_entry *from,
3887 struct ppc_link_hash_entry *to)
3889 if (from->elf.plt.plist != NULL)
3891 if (to->elf.plt.plist != NULL)
3893 struct plt_entry **entp;
3894 struct plt_entry *ent;
3896 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3898 struct plt_entry *dent;
3900 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3901 if (dent->addend == ent->addend)
3903 dent->plt.refcount += ent->plt.refcount;
3904 *entp = ent->next;
3905 break;
3907 if (dent == NULL)
3908 entp = &ent->next;
3910 *entp = to->elf.plt.plist;
3913 to->elf.plt.plist = from->elf.plt.plist;
3914 from->elf.plt.plist = NULL;
3918 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3920 static void
3921 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3922 struct elf_link_hash_entry *dir,
3923 struct elf_link_hash_entry *ind)
3925 struct ppc_link_hash_entry *edir, *eind;
3927 edir = (struct ppc_link_hash_entry *) dir;
3928 eind = (struct ppc_link_hash_entry *) ind;
3930 /* Copy over any dynamic relocs we may have on the indirect sym. */
3931 if (eind->dyn_relocs != NULL)
3933 if (edir->dyn_relocs != NULL)
3935 struct ppc_dyn_relocs **pp;
3936 struct ppc_dyn_relocs *p;
3938 /* Add reloc counts against the indirect sym to the direct sym
3939 list. Merge any entries against the same section. */
3940 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3942 struct ppc_dyn_relocs *q;
3944 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3945 if (q->sec == p->sec)
3947 q->pc_count += p->pc_count;
3948 q->count += p->count;
3949 *pp = p->next;
3950 break;
3952 if (q == NULL)
3953 pp = &p->next;
3955 *pp = edir->dyn_relocs;
3958 edir->dyn_relocs = eind->dyn_relocs;
3959 eind->dyn_relocs = NULL;
3962 edir->is_func |= eind->is_func;
3963 edir->is_func_descriptor |= eind->is_func_descriptor;
3964 edir->tls_mask |= eind->tls_mask;
3966 /* If called to transfer flags for a weakdef during processing
3967 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
3968 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
3969 if (!(ELIMINATE_COPY_RELOCS
3970 && eind->elf.root.type != bfd_link_hash_indirect
3971 && edir->elf.dynamic_adjusted))
3972 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3974 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3975 edir->elf.ref_regular |= eind->elf.ref_regular;
3976 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3977 edir->elf.needs_plt |= eind->elf.needs_plt;
3979 /* If we were called to copy over info for a weak sym, that's all. */
3980 if (eind->elf.root.type != bfd_link_hash_indirect)
3981 return;
3983 /* Copy over got entries that we may have already seen to the
3984 symbol which just became indirect. */
3985 if (eind->elf.got.glist != NULL)
3987 if (edir->elf.got.glist != NULL)
3989 struct got_entry **entp;
3990 struct got_entry *ent;
3992 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3994 struct got_entry *dent;
3996 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3997 if (dent->addend == ent->addend
3998 && dent->owner == ent->owner
3999 && dent->tls_type == ent->tls_type)
4001 dent->got.refcount += ent->got.refcount;
4002 *entp = ent->next;
4003 break;
4005 if (dent == NULL)
4006 entp = &ent->next;
4008 *entp = edir->elf.got.glist;
4011 edir->elf.got.glist = eind->elf.got.glist;
4012 eind->elf.got.glist = NULL;
4015 /* And plt entries. */
4016 move_plt_plist (eind, edir);
4018 if (eind->elf.dynindx != -1)
4020 if (edir->elf.dynindx != -1)
4021 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4022 edir->elf.dynstr_index);
4023 edir->elf.dynindx = eind->elf.dynindx;
4024 edir->elf.dynstr_index = eind->elf.dynstr_index;
4025 eind->elf.dynindx = -1;
4026 eind->elf.dynstr_index = 0;
4030 /* Find the function descriptor hash entry from the given function code
4031 hash entry FH. Link the entries via their OH fields. */
4033 static struct ppc_link_hash_entry *
4034 get_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4036 struct ppc_link_hash_entry *fdh = fh->oh;
4038 if (fdh == NULL)
4040 const char *fd_name = fh->elf.root.root.string + 1;
4042 fdh = (struct ppc_link_hash_entry *)
4043 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4044 if (fdh != NULL)
4046 fdh->is_func_descriptor = 1;
4047 fdh->oh = fh;
4048 fh->is_func = 1;
4049 fh->oh = fdh;
4053 return fdh;
4056 /* Make a fake function descriptor sym for the code sym FH. */
4058 static struct ppc_link_hash_entry *
4059 make_fdh (struct bfd_link_info *info,
4060 struct ppc_link_hash_entry *fh)
4062 bfd *abfd;
4063 asymbol *newsym;
4064 struct bfd_link_hash_entry *bh;
4065 struct ppc_link_hash_entry *fdh;
4067 abfd = fh->elf.root.u.undef.abfd;
4068 newsym = bfd_make_empty_symbol (abfd);
4069 newsym->name = fh->elf.root.root.string + 1;
4070 newsym->section = bfd_und_section_ptr;
4071 newsym->value = 0;
4072 newsym->flags = BSF_WEAK;
4074 bh = NULL;
4075 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4076 newsym->flags, newsym->section,
4077 newsym->value, NULL, FALSE, FALSE,
4078 &bh))
4079 return NULL;
4081 fdh = (struct ppc_link_hash_entry *) bh;
4082 fdh->elf.non_elf = 0;
4083 fdh->fake = 1;
4084 fdh->is_func_descriptor = 1;
4085 fdh->oh = fh;
4086 fh->is_func = 1;
4087 fh->oh = fdh;
4088 return fdh;
4091 /* Fix function descriptor symbols defined in .opd sections to be
4092 function type. */
4094 static bfd_boolean
4095 ppc64_elf_add_symbol_hook (bfd *ibfd ATTRIBUTE_UNUSED,
4096 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4097 Elf_Internal_Sym *isym,
4098 const char **name ATTRIBUTE_UNUSED,
4099 flagword *flags ATTRIBUTE_UNUSED,
4100 asection **sec,
4101 bfd_vma *value ATTRIBUTE_UNUSED)
4103 if (*sec != NULL
4104 && strcmp (bfd_get_section_name (ibfd, *sec), ".opd") == 0)
4105 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4107 return TRUE;
4110 /* This function makes an old ABI object reference to ".bar" cause the
4111 inclusion of a new ABI object archive that defines "bar".
4112 NAME is a symbol defined in an archive. Return a symbol in the hash
4113 table that might be satisfied by the archive symbols. */
4115 static struct elf_link_hash_entry *
4116 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4117 struct bfd_link_info *info,
4118 const char *name)
4120 struct elf_link_hash_entry *h;
4121 char *dot_name;
4122 size_t len;
4124 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4125 if (h != NULL
4126 /* Don't return this sym if it is a fake function descriptor
4127 created by add_symbol_adjust. */
4128 && !(h->root.type == bfd_link_hash_undefweak
4129 && ((struct ppc_link_hash_entry *) h)->fake))
4130 return h;
4132 if (name[0] == '.')
4133 return h;
4135 len = strlen (name);
4136 dot_name = bfd_alloc (abfd, len + 2);
4137 if (dot_name == NULL)
4138 return (struct elf_link_hash_entry *) 0 - 1;
4139 dot_name[0] = '.';
4140 memcpy (dot_name + 1, name, len + 1);
4141 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4142 bfd_release (abfd, dot_name);
4143 return h;
4146 /* This function satisfies all old ABI object references to ".bar" if a
4147 new ABI object defines "bar". Well, at least, undefined dot symbols
4148 are made weak. This stops later archive searches from including an
4149 object if we already have a function descriptor definition. It also
4150 prevents the linker complaining about undefined symbols.
4151 We also check and correct mismatched symbol visibility here. The
4152 most restrictive visibility of the function descriptor and the
4153 function entry symbol is used. */
4155 static bfd_boolean
4156 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4158 struct ppc_link_hash_table *htab;
4159 struct ppc_link_hash_entry *fdh;
4161 if (eh->elf.root.type == bfd_link_hash_indirect)
4162 return TRUE;
4164 if (eh->elf.root.type == bfd_link_hash_warning)
4165 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4167 if (eh->elf.root.root.string[0] != '.')
4168 abort ();
4170 htab = ppc_hash_table (info);
4171 fdh = get_fdh (eh, htab);
4172 if (fdh == NULL
4173 && !info->relocatable
4174 && (eh->elf.root.type == bfd_link_hash_undefined
4175 || eh->elf.root.type == bfd_link_hash_undefweak)
4176 && eh->elf.ref_regular)
4178 /* Make an undefweak function descriptor sym, which is enough to
4179 pull in an --as-needed shared lib, but won't cause link
4180 errors. Archives are handled elsewhere. */
4181 fdh = make_fdh (info, eh);
4182 if (fdh == NULL)
4183 return FALSE;
4184 else
4185 fdh->elf.ref_regular = 1;
4187 else if (fdh != NULL)
4189 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4190 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4191 if (entry_vis < descr_vis)
4192 fdh->elf.other += entry_vis - descr_vis;
4193 else if (entry_vis > descr_vis)
4194 eh->elf.other += descr_vis - entry_vis;
4196 if ((fdh->elf.root.type == bfd_link_hash_defined
4197 || fdh->elf.root.type == bfd_link_hash_defweak)
4198 && eh->elf.root.type == bfd_link_hash_undefined)
4200 eh->elf.root.type = bfd_link_hash_undefweak;
4201 eh->was_undefined = 1;
4202 htab->twiddled_syms = 1;
4206 return TRUE;
4209 /* Process list of dot-symbols we made in link_hash_newfunc. */
4211 static bfd_boolean
4212 ppc64_elf_check_directives (bfd *ibfd, struct bfd_link_info *info)
4214 struct ppc_link_hash_table *htab;
4215 struct ppc_link_hash_entry **p, *eh;
4217 htab = ppc_hash_table (info);
4218 if (!is_ppc64_elf_target (htab->elf.root.creator))
4219 return TRUE;
4221 if (is_ppc64_elf_target (ibfd->xvec))
4223 p = &htab->dot_syms;
4224 while ((eh = *p) != NULL)
4226 *p = NULL;
4227 if (!add_symbol_adjust (eh, info))
4228 return FALSE;
4229 p = &eh->u.next_dot_sym;
4233 /* Clear the list for non-ppc64 input files. */
4234 p = &htab->dot_syms;
4235 while ((eh = *p) != NULL)
4237 *p = NULL;
4238 p = &eh->u.next_dot_sym;
4241 /* We need to fix the undefs list for any syms we have twiddled to
4242 undef_weak. */
4243 if (htab->twiddled_syms)
4245 bfd_link_repair_undef_list (&htab->elf.root);
4246 htab->twiddled_syms = 0;
4248 return TRUE;
4251 /* Undo hash table changes when an --as-needed input file is determined
4252 not to be needed. */
4254 static bfd_boolean
4255 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4256 struct bfd_link_info *info)
4258 ppc_hash_table (info)->dot_syms = NULL;
4259 return TRUE;
4262 static bfd_boolean
4263 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4264 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4266 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4267 char *local_got_tls_masks;
4269 if (local_got_ents == NULL)
4271 bfd_size_type size = symtab_hdr->sh_info;
4273 size *= sizeof (*local_got_ents) + sizeof (*local_got_tls_masks);
4274 local_got_ents = bfd_zalloc (abfd, size);
4275 if (local_got_ents == NULL)
4276 return FALSE;
4277 elf_local_got_ents (abfd) = local_got_ents;
4280 if ((tls_type & TLS_EXPLICIT) == 0)
4282 struct got_entry *ent;
4284 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4285 if (ent->addend == r_addend
4286 && ent->owner == abfd
4287 && ent->tls_type == tls_type)
4288 break;
4289 if (ent == NULL)
4291 bfd_size_type amt = sizeof (*ent);
4292 ent = bfd_alloc (abfd, amt);
4293 if (ent == NULL)
4294 return FALSE;
4295 ent->next = local_got_ents[r_symndx];
4296 ent->addend = r_addend;
4297 ent->owner = abfd;
4298 ent->tls_type = tls_type;
4299 ent->got.refcount = 0;
4300 local_got_ents[r_symndx] = ent;
4302 ent->got.refcount += 1;
4305 local_got_tls_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
4306 local_got_tls_masks[r_symndx] |= tls_type;
4307 return TRUE;
4310 static bfd_boolean
4311 update_plt_info (bfd *abfd, struct ppc_link_hash_entry *eh, bfd_vma addend)
4313 struct plt_entry *ent;
4315 for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
4316 if (ent->addend == addend)
4317 break;
4318 if (ent == NULL)
4320 bfd_size_type amt = sizeof (*ent);
4321 ent = bfd_alloc (abfd, amt);
4322 if (ent == NULL)
4323 return FALSE;
4324 ent->next = eh->elf.plt.plist;
4325 ent->addend = addend;
4326 ent->plt.refcount = 0;
4327 eh->elf.plt.plist = ent;
4329 ent->plt.refcount += 1;
4330 eh->elf.needs_plt = 1;
4331 if (eh->elf.root.root.string[0] == '.'
4332 && eh->elf.root.root.string[1] != '\0')
4333 eh->is_func = 1;
4334 return TRUE;
4337 /* Look through the relocs for a section during the first phase, and
4338 calculate needed space in the global offset table, procedure
4339 linkage table, and dynamic reloc sections. */
4341 static bfd_boolean
4342 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4343 asection *sec, const Elf_Internal_Rela *relocs)
4345 struct ppc_link_hash_table *htab;
4346 Elf_Internal_Shdr *symtab_hdr;
4347 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4348 const Elf_Internal_Rela *rel;
4349 const Elf_Internal_Rela *rel_end;
4350 asection *sreloc;
4351 asection **opd_sym_map;
4353 if (info->relocatable)
4354 return TRUE;
4356 /* Don't do anything special with non-loaded, non-alloced sections.
4357 In particular, any relocs in such sections should not affect GOT
4358 and PLT reference counting (ie. we don't allow them to create GOT
4359 or PLT entries), there's no possibility or desire to optimize TLS
4360 relocs, and there's not much point in propagating relocs to shared
4361 libs that the dynamic linker won't relocate. */
4362 if ((sec->flags & SEC_ALLOC) == 0)
4363 return TRUE;
4365 htab = ppc_hash_table (info);
4366 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4368 sym_hashes = elf_sym_hashes (abfd);
4369 sym_hashes_end = (sym_hashes
4370 + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
4371 - symtab_hdr->sh_info);
4373 sreloc = NULL;
4374 opd_sym_map = NULL;
4375 if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
4377 /* Garbage collection needs some extra help with .opd sections.
4378 We don't want to necessarily keep everything referenced by
4379 relocs in .opd, as that would keep all functions. Instead,
4380 if we reference an .opd symbol (a function descriptor), we
4381 want to keep the function code symbol's section. This is
4382 easy for global symbols, but for local syms we need to keep
4383 information about the associated function section. Later, if
4384 edit_opd deletes entries, we'll use this array to adjust
4385 local syms in .opd. */
4386 union opd_info {
4387 asection *func_section;
4388 long entry_adjust;
4390 bfd_size_type amt;
4392 amt = sec->size * sizeof (union opd_info) / 8;
4393 opd_sym_map = bfd_zalloc (abfd, amt);
4394 if (opd_sym_map == NULL)
4395 return FALSE;
4396 ppc64_elf_section_data (sec)->u.opd_func_sec = opd_sym_map;
4397 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4398 ppc64_elf_section_data (sec)->sec_type = sec_opd;
4401 if (htab->sfpr == NULL
4402 && !create_linkage_sections (htab->elf.dynobj, info))
4403 return FALSE;
4405 rel_end = relocs + sec->reloc_count;
4406 for (rel = relocs; rel < rel_end; rel++)
4408 unsigned long r_symndx;
4409 struct elf_link_hash_entry *h;
4410 enum elf_ppc64_reloc_type r_type;
4411 int tls_type = 0;
4412 struct _ppc64_elf_section_data *ppc64_sec;
4414 r_symndx = ELF64_R_SYM (rel->r_info);
4415 if (r_symndx < symtab_hdr->sh_info)
4416 h = NULL;
4417 else
4419 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4420 while (h->root.type == bfd_link_hash_indirect
4421 || h->root.type == bfd_link_hash_warning)
4422 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4425 r_type = ELF64_R_TYPE (rel->r_info);
4426 switch (r_type)
4428 case R_PPC64_GOT_TLSLD16:
4429 case R_PPC64_GOT_TLSLD16_LO:
4430 case R_PPC64_GOT_TLSLD16_HI:
4431 case R_PPC64_GOT_TLSLD16_HA:
4432 ppc64_tlsld_got (abfd)->refcount += 1;
4433 tls_type = TLS_TLS | TLS_LD;
4434 goto dogottls;
4436 case R_PPC64_GOT_TLSGD16:
4437 case R_PPC64_GOT_TLSGD16_LO:
4438 case R_PPC64_GOT_TLSGD16_HI:
4439 case R_PPC64_GOT_TLSGD16_HA:
4440 tls_type = TLS_TLS | TLS_GD;
4441 goto dogottls;
4443 case R_PPC64_GOT_TPREL16_DS:
4444 case R_PPC64_GOT_TPREL16_LO_DS:
4445 case R_PPC64_GOT_TPREL16_HI:
4446 case R_PPC64_GOT_TPREL16_HA:
4447 if (info->shared)
4448 info->flags |= DF_STATIC_TLS;
4449 tls_type = TLS_TLS | TLS_TPREL;
4450 goto dogottls;
4452 case R_PPC64_GOT_DTPREL16_DS:
4453 case R_PPC64_GOT_DTPREL16_LO_DS:
4454 case R_PPC64_GOT_DTPREL16_HI:
4455 case R_PPC64_GOT_DTPREL16_HA:
4456 tls_type = TLS_TLS | TLS_DTPREL;
4457 dogottls:
4458 sec->has_tls_reloc = 1;
4459 /* Fall thru */
4461 case R_PPC64_GOT16:
4462 case R_PPC64_GOT16_DS:
4463 case R_PPC64_GOT16_HA:
4464 case R_PPC64_GOT16_HI:
4465 case R_PPC64_GOT16_LO:
4466 case R_PPC64_GOT16_LO_DS:
4467 /* This symbol requires a global offset table entry. */
4468 sec->has_toc_reloc = 1;
4469 if (ppc64_elf_tdata (abfd)->got == NULL
4470 && !create_got_section (abfd, info))
4471 return FALSE;
4473 if (h != NULL)
4475 struct ppc_link_hash_entry *eh;
4476 struct got_entry *ent;
4478 eh = (struct ppc_link_hash_entry *) h;
4479 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4480 if (ent->addend == rel->r_addend
4481 && ent->owner == abfd
4482 && ent->tls_type == tls_type)
4483 break;
4484 if (ent == NULL)
4486 bfd_size_type amt = sizeof (*ent);
4487 ent = bfd_alloc (abfd, amt);
4488 if (ent == NULL)
4489 return FALSE;
4490 ent->next = eh->elf.got.glist;
4491 ent->addend = rel->r_addend;
4492 ent->owner = abfd;
4493 ent->tls_type = tls_type;
4494 ent->got.refcount = 0;
4495 eh->elf.got.glist = ent;
4497 ent->got.refcount += 1;
4498 eh->tls_mask |= tls_type;
4500 else
4501 /* This is a global offset table entry for a local symbol. */
4502 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4503 rel->r_addend, tls_type))
4504 return FALSE;
4505 break;
4507 case R_PPC64_PLT16_HA:
4508 case R_PPC64_PLT16_HI:
4509 case R_PPC64_PLT16_LO:
4510 case R_PPC64_PLT32:
4511 case R_PPC64_PLT64:
4512 /* This symbol requires a procedure linkage table entry. We
4513 actually build the entry in adjust_dynamic_symbol,
4514 because this might be a case of linking PIC code without
4515 linking in any dynamic objects, in which case we don't
4516 need to generate a procedure linkage table after all. */
4517 if (h == NULL)
4519 /* It does not make sense to have a procedure linkage
4520 table entry for a local symbol. */
4521 bfd_set_error (bfd_error_bad_value);
4522 return FALSE;
4524 else
4525 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4526 rel->r_addend))
4527 return FALSE;
4528 break;
4530 /* The following relocations don't need to propagate the
4531 relocation if linking a shared object since they are
4532 section relative. */
4533 case R_PPC64_SECTOFF:
4534 case R_PPC64_SECTOFF_LO:
4535 case R_PPC64_SECTOFF_HI:
4536 case R_PPC64_SECTOFF_HA:
4537 case R_PPC64_SECTOFF_DS:
4538 case R_PPC64_SECTOFF_LO_DS:
4539 case R_PPC64_DTPREL16:
4540 case R_PPC64_DTPREL16_LO:
4541 case R_PPC64_DTPREL16_HI:
4542 case R_PPC64_DTPREL16_HA:
4543 case R_PPC64_DTPREL16_DS:
4544 case R_PPC64_DTPREL16_LO_DS:
4545 case R_PPC64_DTPREL16_HIGHER:
4546 case R_PPC64_DTPREL16_HIGHERA:
4547 case R_PPC64_DTPREL16_HIGHEST:
4548 case R_PPC64_DTPREL16_HIGHESTA:
4549 break;
4551 /* Nor do these. */
4552 case R_PPC64_TOC16:
4553 case R_PPC64_TOC16_LO:
4554 case R_PPC64_TOC16_HI:
4555 case R_PPC64_TOC16_HA:
4556 case R_PPC64_TOC16_DS:
4557 case R_PPC64_TOC16_LO_DS:
4558 sec->has_toc_reloc = 1;
4559 break;
4561 /* This relocation describes the C++ object vtable hierarchy.
4562 Reconstruct it for later use during GC. */
4563 case R_PPC64_GNU_VTINHERIT:
4564 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4565 return FALSE;
4566 break;
4568 /* This relocation describes which C++ vtable entries are actually
4569 used. Record for later use during GC. */
4570 case R_PPC64_GNU_VTENTRY:
4571 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4572 return FALSE;
4573 break;
4575 case R_PPC64_REL14:
4576 case R_PPC64_REL14_BRTAKEN:
4577 case R_PPC64_REL14_BRNTAKEN:
4579 asection *dest = NULL;
4581 /* Heuristic: If jumping outside our section, chances are
4582 we are going to need a stub. */
4583 if (h != NULL)
4585 /* If the sym is weak it may be overridden later, so
4586 don't assume we know where a weak sym lives. */
4587 if (h->root.type == bfd_link_hash_defined)
4588 dest = h->root.u.def.section;
4590 else
4591 dest = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4592 sec, r_symndx);
4593 if (dest != sec)
4594 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4596 /* Fall through. */
4598 case R_PPC64_REL24:
4599 if (h != NULL)
4601 /* We may need a .plt entry if the function this reloc
4602 refers to is in a shared lib. */
4603 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4604 rel->r_addend))
4605 return FALSE;
4606 if (h == &htab->tls_get_addr->elf
4607 || h == &htab->tls_get_addr_fd->elf)
4608 sec->has_tls_reloc = 1;
4609 else if (htab->tls_get_addr == NULL
4610 && CONST_STRNEQ (h->root.root.string, ".__tls_get_addr")
4611 && (h->root.root.string[15] == 0
4612 || h->root.root.string[15] == '@'))
4614 htab->tls_get_addr = (struct ppc_link_hash_entry *) h;
4615 sec->has_tls_reloc = 1;
4617 else if (htab->tls_get_addr_fd == NULL
4618 && CONST_STRNEQ (h->root.root.string, "__tls_get_addr")
4619 && (h->root.root.string[14] == 0
4620 || h->root.root.string[14] == '@'))
4622 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) h;
4623 sec->has_tls_reloc = 1;
4626 break;
4628 case R_PPC64_TPREL64:
4629 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4630 if (info->shared)
4631 info->flags |= DF_STATIC_TLS;
4632 goto dotlstoc;
4634 case R_PPC64_DTPMOD64:
4635 if (rel + 1 < rel_end
4636 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4637 && rel[1].r_offset == rel->r_offset + 8)
4638 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4639 else
4640 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4641 goto dotlstoc;
4643 case R_PPC64_DTPREL64:
4644 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4645 if (rel != relocs
4646 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4647 && rel[-1].r_offset == rel->r_offset - 8)
4648 /* This is the second reloc of a dtpmod, dtprel pair.
4649 Don't mark with TLS_DTPREL. */
4650 goto dodyn;
4652 dotlstoc:
4653 sec->has_tls_reloc = 1;
4654 if (h != NULL)
4656 struct ppc_link_hash_entry *eh;
4657 eh = (struct ppc_link_hash_entry *) h;
4658 eh->tls_mask |= tls_type;
4660 else
4661 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4662 rel->r_addend, tls_type))
4663 return FALSE;
4665 ppc64_sec = ppc64_elf_section_data (sec);
4666 if (ppc64_sec->sec_type != sec_toc)
4668 /* One extra to simplify get_tls_mask. */
4669 bfd_size_type amt = sec->size * sizeof (unsigned) / 8 + 1;
4670 ppc64_sec->u.t_symndx = bfd_zalloc (abfd, amt);
4671 if (ppc64_sec->u.t_symndx == NULL)
4672 return FALSE;
4673 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4674 ppc64_sec->sec_type = sec_toc;
4676 BFD_ASSERT (rel->r_offset % 8 == 0);
4677 ppc64_sec->u.t_symndx[rel->r_offset / 8] = r_symndx;
4679 /* Mark the second slot of a GD or LD entry.
4680 -1 to indicate GD and -2 to indicate LD. */
4681 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4682 ppc64_sec->u.t_symndx[rel->r_offset / 8 + 1] = -1;
4683 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4684 ppc64_sec->u.t_symndx[rel->r_offset / 8 + 1] = -2;
4685 goto dodyn;
4687 case R_PPC64_TPREL16:
4688 case R_PPC64_TPREL16_LO:
4689 case R_PPC64_TPREL16_HI:
4690 case R_PPC64_TPREL16_HA:
4691 case R_PPC64_TPREL16_DS:
4692 case R_PPC64_TPREL16_LO_DS:
4693 case R_PPC64_TPREL16_HIGHER:
4694 case R_PPC64_TPREL16_HIGHERA:
4695 case R_PPC64_TPREL16_HIGHEST:
4696 case R_PPC64_TPREL16_HIGHESTA:
4697 if (info->shared)
4699 info->flags |= DF_STATIC_TLS;
4700 goto dodyn;
4702 break;
4704 case R_PPC64_ADDR64:
4705 if (opd_sym_map != NULL
4706 && rel + 1 < rel_end
4707 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
4709 if (h != NULL)
4711 if (h->root.root.string[0] == '.'
4712 && h->root.root.string[1] != 0
4713 && get_fdh ((struct ppc_link_hash_entry *) h, htab))
4715 else
4716 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4718 else
4720 asection *s;
4722 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
4723 r_symndx);
4724 if (s == NULL)
4725 return FALSE;
4726 else if (s != sec)
4727 opd_sym_map[rel->r_offset / 8] = s;
4730 /* Fall through. */
4732 case R_PPC64_REL30:
4733 case R_PPC64_REL32:
4734 case R_PPC64_REL64:
4735 case R_PPC64_ADDR14:
4736 case R_PPC64_ADDR14_BRNTAKEN:
4737 case R_PPC64_ADDR14_BRTAKEN:
4738 case R_PPC64_ADDR16:
4739 case R_PPC64_ADDR16_DS:
4740 case R_PPC64_ADDR16_HA:
4741 case R_PPC64_ADDR16_HI:
4742 case R_PPC64_ADDR16_HIGHER:
4743 case R_PPC64_ADDR16_HIGHERA:
4744 case R_PPC64_ADDR16_HIGHEST:
4745 case R_PPC64_ADDR16_HIGHESTA:
4746 case R_PPC64_ADDR16_LO:
4747 case R_PPC64_ADDR16_LO_DS:
4748 case R_PPC64_ADDR24:
4749 case R_PPC64_ADDR32:
4750 case R_PPC64_UADDR16:
4751 case R_PPC64_UADDR32:
4752 case R_PPC64_UADDR64:
4753 case R_PPC64_TOC:
4754 if (h != NULL && !info->shared)
4755 /* We may need a copy reloc. */
4756 h->non_got_ref = 1;
4758 /* Don't propagate .opd relocs. */
4759 if (NO_OPD_RELOCS && opd_sym_map != NULL)
4760 break;
4762 /* If we are creating a shared library, and this is a reloc
4763 against a global symbol, or a non PC relative reloc
4764 against a local symbol, then we need to copy the reloc
4765 into the shared library. However, if we are linking with
4766 -Bsymbolic, we do not need to copy a reloc against a
4767 global symbol which is defined in an object we are
4768 including in the link (i.e., DEF_REGULAR is set). At
4769 this point we have not seen all the input files, so it is
4770 possible that DEF_REGULAR is not set now but will be set
4771 later (it is never cleared). In case of a weak definition,
4772 DEF_REGULAR may be cleared later by a strong definition in
4773 a shared library. We account for that possibility below by
4774 storing information in the dyn_relocs field of the hash
4775 table entry. A similar situation occurs when creating
4776 shared libraries and symbol visibility changes render the
4777 symbol local.
4779 If on the other hand, we are creating an executable, we
4780 may need to keep relocations for symbols satisfied by a
4781 dynamic library if we manage to avoid copy relocs for the
4782 symbol. */
4783 dodyn:
4784 if ((info->shared
4785 && (MUST_BE_DYN_RELOC (r_type)
4786 || (h != NULL
4787 && (! info->symbolic
4788 || h->root.type == bfd_link_hash_defweak
4789 || !h->def_regular))))
4790 || (ELIMINATE_COPY_RELOCS
4791 && !info->shared
4792 && h != NULL
4793 && (h->root.type == bfd_link_hash_defweak
4794 || !h->def_regular)))
4796 struct ppc_dyn_relocs *p;
4797 struct ppc_dyn_relocs **head;
4799 /* We must copy these reloc types into the output file.
4800 Create a reloc section in dynobj and make room for
4801 this reloc. */
4802 if (sreloc == NULL)
4804 const char *name;
4805 bfd *dynobj;
4807 name = (bfd_elf_string_from_elf_section
4808 (abfd,
4809 elf_elfheader (abfd)->e_shstrndx,
4810 elf_section_data (sec)->rel_hdr.sh_name));
4811 if (name == NULL)
4812 return FALSE;
4814 if (! CONST_STRNEQ (name, ".rela")
4815 || strcmp (bfd_get_section_name (abfd, sec),
4816 name + 5) != 0)
4818 (*_bfd_error_handler)
4819 (_("%B: bad relocation section name `%s\'"),
4820 abfd, name);
4821 bfd_set_error (bfd_error_bad_value);
4824 dynobj = htab->elf.dynobj;
4825 sreloc = bfd_get_section_by_name (dynobj, name);
4826 if (sreloc == NULL)
4828 flagword flags;
4830 flags = (SEC_HAS_CONTENTS | SEC_READONLY
4831 | SEC_IN_MEMORY | SEC_LINKER_CREATED
4832 | SEC_ALLOC | SEC_LOAD);
4833 sreloc = bfd_make_section_with_flags (dynobj,
4834 name,
4835 flags);
4836 if (sreloc == NULL
4837 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
4838 return FALSE;
4840 elf_section_data (sec)->sreloc = sreloc;
4843 /* If this is a global symbol, we count the number of
4844 relocations we need for this symbol. */
4845 if (h != NULL)
4847 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
4849 else
4851 /* Track dynamic relocs needed for local syms too.
4852 We really need local syms available to do this
4853 easily. Oh well. */
4855 asection *s;
4856 void *vpp;
4858 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4859 sec, r_symndx);
4860 if (s == NULL)
4861 return FALSE;
4863 vpp = &elf_section_data (s)->local_dynrel;
4864 head = (struct ppc_dyn_relocs **) vpp;
4867 p = *head;
4868 if (p == NULL || p->sec != sec)
4870 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4871 if (p == NULL)
4872 return FALSE;
4873 p->next = *head;
4874 *head = p;
4875 p->sec = sec;
4876 p->count = 0;
4877 p->pc_count = 0;
4880 p->count += 1;
4881 if (!MUST_BE_DYN_RELOC (r_type))
4882 p->pc_count += 1;
4884 break;
4886 default:
4887 break;
4891 return TRUE;
4894 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
4895 of the code entry point, and its section. */
4897 static bfd_vma
4898 opd_entry_value (asection *opd_sec,
4899 bfd_vma offset,
4900 asection **code_sec,
4901 bfd_vma *code_off)
4903 bfd *opd_bfd = opd_sec->owner;
4904 Elf_Internal_Rela *relocs;
4905 Elf_Internal_Rela *lo, *hi, *look;
4906 bfd_vma val;
4908 /* No relocs implies we are linking a --just-symbols object. */
4909 if (opd_sec->reloc_count == 0)
4911 bfd_vma val;
4913 if (!bfd_get_section_contents (opd_bfd, opd_sec, &val, offset, 8))
4914 return (bfd_vma) -1;
4916 if (code_sec != NULL)
4918 asection *sec, *likely = NULL;
4919 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
4920 if (sec->vma <= val
4921 && (sec->flags & SEC_LOAD) != 0
4922 && (sec->flags & SEC_ALLOC) != 0)
4923 likely = sec;
4924 if (likely != NULL)
4926 *code_sec = likely;
4927 if (code_off != NULL)
4928 *code_off = val - likely->vma;
4931 return val;
4934 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
4935 if (relocs == NULL)
4936 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
4938 /* Go find the opd reloc at the sym address. */
4939 lo = relocs;
4940 BFD_ASSERT (lo != NULL);
4941 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
4942 val = (bfd_vma) -1;
4943 while (lo < hi)
4945 look = lo + (hi - lo) / 2;
4946 if (look->r_offset < offset)
4947 lo = look + 1;
4948 else if (look->r_offset > offset)
4949 hi = look;
4950 else
4952 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (opd_bfd)->symtab_hdr;
4953 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
4954 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
4956 unsigned long symndx = ELF64_R_SYM (look->r_info);
4957 asection *sec;
4959 if (symndx < symtab_hdr->sh_info)
4961 Elf_Internal_Sym *sym;
4963 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
4964 if (sym == NULL)
4966 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
4967 symtab_hdr->sh_info,
4968 0, NULL, NULL, NULL);
4969 if (sym == NULL)
4970 break;
4971 symtab_hdr->contents = (bfd_byte *) sym;
4974 sym += symndx;
4975 val = sym->st_value;
4976 sec = NULL;
4977 if ((sym->st_shndx != SHN_UNDEF
4978 && sym->st_shndx < SHN_LORESERVE)
4979 || sym->st_shndx > SHN_HIRESERVE)
4980 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
4981 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
4983 else
4985 struct elf_link_hash_entry **sym_hashes;
4986 struct elf_link_hash_entry *rh;
4988 sym_hashes = elf_sym_hashes (opd_bfd);
4989 rh = sym_hashes[symndx - symtab_hdr->sh_info];
4990 while (rh->root.type == bfd_link_hash_indirect
4991 || rh->root.type == bfd_link_hash_warning)
4992 rh = ((struct elf_link_hash_entry *) rh->root.u.i.link);
4993 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
4994 || rh->root.type == bfd_link_hash_defweak);
4995 val = rh->root.u.def.value;
4996 sec = rh->root.u.def.section;
4998 val += look->r_addend;
4999 if (code_off != NULL)
5000 *code_off = val;
5001 if (code_sec != NULL)
5002 *code_sec = sec;
5003 if (sec != NULL && sec->output_section != NULL)
5004 val += sec->output_section->vma + sec->output_offset;
5006 break;
5010 return val;
5013 /* Mark sections containing dynamically referenced symbols. When
5014 building shared libraries, we must assume that any visible symbol is
5015 referenced. */
5017 static bfd_boolean
5018 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5020 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5021 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5023 if (eh->elf.root.type == bfd_link_hash_warning)
5024 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5026 /* Dynamic linking info is on the func descriptor sym. */
5027 if (eh->oh != NULL
5028 && eh->oh->is_func_descriptor
5029 && (eh->oh->elf.root.type == bfd_link_hash_defined
5030 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5031 eh = eh->oh;
5033 if ((eh->elf.root.type == bfd_link_hash_defined
5034 || eh->elf.root.type == bfd_link_hash_defweak)
5035 && (eh->elf.ref_dynamic
5036 || (!info->executable
5037 && eh->elf.def_regular
5038 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5039 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
5041 asection *code_sec;
5043 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5045 /* Function descriptor syms cause the associated
5046 function code sym section to be marked. */
5047 if (eh->is_func_descriptor
5048 && (eh->oh->elf.root.type == bfd_link_hash_defined
5049 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5050 eh->oh->elf.root.u.def.section->flags |= SEC_KEEP;
5051 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5052 && opd_entry_value (eh->elf.root.u.def.section,
5053 eh->elf.root.u.def.value,
5054 &code_sec, NULL) != (bfd_vma) -1)
5055 code_sec->flags |= SEC_KEEP;
5058 return TRUE;
5061 /* Return the section that should be marked against GC for a given
5062 relocation. */
5064 static asection *
5065 ppc64_elf_gc_mark_hook (asection *sec,
5066 struct bfd_link_info *info,
5067 Elf_Internal_Rela *rel,
5068 struct elf_link_hash_entry *h,
5069 Elf_Internal_Sym *sym)
5071 asection *rsec;
5073 /* First mark all our entry sym sections. */
5074 if (info->gc_sym_list != NULL)
5076 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5077 struct bfd_sym_chain *sym = info->gc_sym_list;
5079 info->gc_sym_list = NULL;
5080 for (; sym != NULL; sym = sym->next)
5082 struct ppc_link_hash_entry *eh;
5084 eh = (struct ppc_link_hash_entry *)
5085 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
5086 if (eh == NULL)
5087 continue;
5088 if (eh->elf.root.type != bfd_link_hash_defined
5089 && eh->elf.root.type != bfd_link_hash_defweak)
5090 continue;
5092 if (eh->is_func_descriptor
5093 && (eh->oh->elf.root.type == bfd_link_hash_defined
5094 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5095 rsec = eh->oh->elf.root.u.def.section;
5096 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5097 && opd_entry_value (eh->elf.root.u.def.section,
5098 eh->elf.root.u.def.value,
5099 &rsec, NULL) != (bfd_vma) -1)
5101 else
5102 continue;
5104 if (!rsec->gc_mark)
5105 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5107 rsec = eh->elf.root.u.def.section;
5108 if (!rsec->gc_mark)
5109 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5113 /* Syms return NULL if we're marking .opd, so we avoid marking all
5114 function sections, as all functions are referenced in .opd. */
5115 rsec = NULL;
5116 if (get_opd_info (sec) != NULL)
5117 return rsec;
5119 if (h != NULL)
5121 enum elf_ppc64_reloc_type r_type;
5122 struct ppc_link_hash_entry *eh;
5124 r_type = ELF64_R_TYPE (rel->r_info);
5125 switch (r_type)
5127 case R_PPC64_GNU_VTINHERIT:
5128 case R_PPC64_GNU_VTENTRY:
5129 break;
5131 default:
5132 switch (h->root.type)
5134 case bfd_link_hash_defined:
5135 case bfd_link_hash_defweak:
5136 eh = (struct ppc_link_hash_entry *) h;
5137 if (eh->oh != NULL
5138 && eh->oh->is_func_descriptor
5139 && (eh->oh->elf.root.type == bfd_link_hash_defined
5140 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5141 eh = eh->oh;
5143 /* Function descriptor syms cause the associated
5144 function code sym section to be marked. */
5145 if (eh->is_func_descriptor
5146 && (eh->oh->elf.root.type == bfd_link_hash_defined
5147 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5149 /* They also mark their opd section. */
5150 if (!eh->elf.root.u.def.section->gc_mark)
5151 _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5152 ppc64_elf_gc_mark_hook);
5154 rsec = eh->oh->elf.root.u.def.section;
5156 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5157 && opd_entry_value (eh->elf.root.u.def.section,
5158 eh->elf.root.u.def.value,
5159 &rsec, NULL) != (bfd_vma) -1)
5161 if (!eh->elf.root.u.def.section->gc_mark)
5162 _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5163 ppc64_elf_gc_mark_hook);
5165 else
5166 rsec = h->root.u.def.section;
5167 break;
5169 case bfd_link_hash_common:
5170 rsec = h->root.u.c.p->section;
5171 break;
5173 default:
5174 break;
5178 else
5180 asection **opd_sym_section;
5182 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5183 opd_sym_section = get_opd_info (rsec);
5184 if (opd_sym_section != NULL)
5186 if (!rsec->gc_mark)
5187 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5189 rsec = opd_sym_section[(sym->st_value + rel->r_addend) / 8];
5193 return rsec;
5196 /* Update the .got, .plt. and dynamic reloc reference counts for the
5197 section being removed. */
5199 static bfd_boolean
5200 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5201 asection *sec, const Elf_Internal_Rela *relocs)
5203 struct ppc_link_hash_table *htab;
5204 Elf_Internal_Shdr *symtab_hdr;
5205 struct elf_link_hash_entry **sym_hashes;
5206 struct got_entry **local_got_ents;
5207 const Elf_Internal_Rela *rel, *relend;
5209 if ((sec->flags & SEC_ALLOC) == 0)
5210 return TRUE;
5212 elf_section_data (sec)->local_dynrel = NULL;
5214 htab = ppc_hash_table (info);
5215 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5216 sym_hashes = elf_sym_hashes (abfd);
5217 local_got_ents = elf_local_got_ents (abfd);
5219 relend = relocs + sec->reloc_count;
5220 for (rel = relocs; rel < relend; rel++)
5222 unsigned long r_symndx;
5223 enum elf_ppc64_reloc_type r_type;
5224 struct elf_link_hash_entry *h = NULL;
5225 char tls_type = 0;
5227 r_symndx = ELF64_R_SYM (rel->r_info);
5228 r_type = ELF64_R_TYPE (rel->r_info);
5229 if (r_symndx >= symtab_hdr->sh_info)
5231 struct ppc_link_hash_entry *eh;
5232 struct ppc_dyn_relocs **pp;
5233 struct ppc_dyn_relocs *p;
5235 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5236 while (h->root.type == bfd_link_hash_indirect
5237 || h->root.type == bfd_link_hash_warning)
5238 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5239 eh = (struct ppc_link_hash_entry *) h;
5241 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5242 if (p->sec == sec)
5244 /* Everything must go for SEC. */
5245 *pp = p->next;
5246 break;
5250 switch (r_type)
5252 case R_PPC64_GOT_TLSLD16:
5253 case R_PPC64_GOT_TLSLD16_LO:
5254 case R_PPC64_GOT_TLSLD16_HI:
5255 case R_PPC64_GOT_TLSLD16_HA:
5256 ppc64_tlsld_got (abfd)->refcount -= 1;
5257 tls_type = TLS_TLS | TLS_LD;
5258 goto dogot;
5260 case R_PPC64_GOT_TLSGD16:
5261 case R_PPC64_GOT_TLSGD16_LO:
5262 case R_PPC64_GOT_TLSGD16_HI:
5263 case R_PPC64_GOT_TLSGD16_HA:
5264 tls_type = TLS_TLS | TLS_GD;
5265 goto dogot;
5267 case R_PPC64_GOT_TPREL16_DS:
5268 case R_PPC64_GOT_TPREL16_LO_DS:
5269 case R_PPC64_GOT_TPREL16_HI:
5270 case R_PPC64_GOT_TPREL16_HA:
5271 tls_type = TLS_TLS | TLS_TPREL;
5272 goto dogot;
5274 case R_PPC64_GOT_DTPREL16_DS:
5275 case R_PPC64_GOT_DTPREL16_LO_DS:
5276 case R_PPC64_GOT_DTPREL16_HI:
5277 case R_PPC64_GOT_DTPREL16_HA:
5278 tls_type = TLS_TLS | TLS_DTPREL;
5279 goto dogot;
5281 case R_PPC64_GOT16:
5282 case R_PPC64_GOT16_DS:
5283 case R_PPC64_GOT16_HA:
5284 case R_PPC64_GOT16_HI:
5285 case R_PPC64_GOT16_LO:
5286 case R_PPC64_GOT16_LO_DS:
5287 dogot:
5289 struct got_entry *ent;
5291 if (h != NULL)
5292 ent = h->got.glist;
5293 else
5294 ent = local_got_ents[r_symndx];
5296 for (; ent != NULL; ent = ent->next)
5297 if (ent->addend == rel->r_addend
5298 && ent->owner == abfd
5299 && ent->tls_type == tls_type)
5300 break;
5301 if (ent == NULL)
5302 abort ();
5303 if (ent->got.refcount > 0)
5304 ent->got.refcount -= 1;
5306 break;
5308 case R_PPC64_PLT16_HA:
5309 case R_PPC64_PLT16_HI:
5310 case R_PPC64_PLT16_LO:
5311 case R_PPC64_PLT32:
5312 case R_PPC64_PLT64:
5313 case R_PPC64_REL14:
5314 case R_PPC64_REL14_BRNTAKEN:
5315 case R_PPC64_REL14_BRTAKEN:
5316 case R_PPC64_REL24:
5317 if (h != NULL)
5319 struct plt_entry *ent;
5321 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5322 if (ent->addend == rel->r_addend)
5323 break;
5324 if (ent == NULL)
5325 abort ();
5326 if (ent->plt.refcount > 0)
5327 ent->plt.refcount -= 1;
5329 break;
5331 default:
5332 break;
5335 return TRUE;
5338 /* The maximum size of .sfpr. */
5339 #define SFPR_MAX (218*4)
5341 struct sfpr_def_parms
5343 const char name[12];
5344 unsigned char lo, hi;
5345 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5346 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5349 /* Auto-generate _save*, _rest* functions in .sfpr. */
5351 static unsigned int
5352 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5354 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5355 unsigned int i;
5356 size_t len = strlen (parm->name);
5357 bfd_boolean writing = FALSE;
5358 char sym[16];
5360 memcpy (sym, parm->name, len);
5361 sym[len + 2] = 0;
5363 for (i = parm->lo; i <= parm->hi; i++)
5365 struct elf_link_hash_entry *h;
5367 sym[len + 0] = i / 10 + '0';
5368 sym[len + 1] = i % 10 + '0';
5369 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
5370 if (h != NULL
5371 && !h->def_regular)
5373 h->root.type = bfd_link_hash_defined;
5374 h->root.u.def.section = htab->sfpr;
5375 h->root.u.def.value = htab->sfpr->size;
5376 h->type = STT_FUNC;
5377 h->def_regular = 1;
5378 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
5379 writing = TRUE;
5380 if (htab->sfpr->contents == NULL)
5382 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5383 if (htab->sfpr->contents == NULL)
5384 return FALSE;
5387 if (writing)
5389 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5390 if (i != parm->hi)
5391 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5392 else
5393 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5394 htab->sfpr->size = p - htab->sfpr->contents;
5398 return TRUE;
5401 static bfd_byte *
5402 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5404 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5405 return p + 4;
5408 static bfd_byte *
5409 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5411 p = savegpr0 (abfd, p, r);
5412 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5413 p = p + 4;
5414 bfd_put_32 (abfd, BLR, p);
5415 return p + 4;
5418 static bfd_byte *
5419 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5421 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5422 return p + 4;
5425 static bfd_byte *
5426 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5428 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5429 p = p + 4;
5430 p = restgpr0 (abfd, p, r);
5431 bfd_put_32 (abfd, MTLR_R0, p);
5432 p = p + 4;
5433 if (r == 29)
5435 p = restgpr0 (abfd, p, 30);
5436 p = restgpr0 (abfd, p, 31);
5438 bfd_put_32 (abfd, BLR, p);
5439 return p + 4;
5442 static bfd_byte *
5443 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5445 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5446 return p + 4;
5449 static bfd_byte *
5450 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5452 p = savegpr1 (abfd, p, r);
5453 bfd_put_32 (abfd, BLR, p);
5454 return p + 4;
5457 static bfd_byte *
5458 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5460 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5461 return p + 4;
5464 static bfd_byte *
5465 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5467 p = restgpr1 (abfd, p, r);
5468 bfd_put_32 (abfd, BLR, p);
5469 return p + 4;
5472 static bfd_byte *
5473 savefpr (bfd *abfd, bfd_byte *p, int r)
5475 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5476 return p + 4;
5479 static bfd_byte *
5480 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5482 p = savefpr (abfd, p, r);
5483 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5484 p = p + 4;
5485 bfd_put_32 (abfd, BLR, p);
5486 return p + 4;
5489 static bfd_byte *
5490 restfpr (bfd *abfd, bfd_byte *p, int r)
5492 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5493 return p + 4;
5496 static bfd_byte *
5497 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5499 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5500 p = p + 4;
5501 p = restfpr (abfd, p, r);
5502 bfd_put_32 (abfd, MTLR_R0, p);
5503 p = p + 4;
5504 if (r == 29)
5506 p = restfpr (abfd, p, 30);
5507 p = restfpr (abfd, p, 31);
5509 bfd_put_32 (abfd, BLR, p);
5510 return p + 4;
5513 static bfd_byte *
5514 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5516 p = savefpr (abfd, p, r);
5517 bfd_put_32 (abfd, BLR, p);
5518 return p + 4;
5521 static bfd_byte *
5522 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5524 p = restfpr (abfd, p, r);
5525 bfd_put_32 (abfd, BLR, p);
5526 return p + 4;
5529 static bfd_byte *
5530 savevr (bfd *abfd, bfd_byte *p, int r)
5532 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5533 p = p + 4;
5534 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5535 return p + 4;
5538 static bfd_byte *
5539 savevr_tail (bfd *abfd, bfd_byte *p, int r)
5541 p = savevr (abfd, p, r);
5542 bfd_put_32 (abfd, BLR, p);
5543 return p + 4;
5546 static bfd_byte *
5547 restvr (bfd *abfd, bfd_byte *p, int r)
5549 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5550 p = p + 4;
5551 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5552 return p + 4;
5555 static bfd_byte *
5556 restvr_tail (bfd *abfd, bfd_byte *p, int r)
5558 p = restvr (abfd, p, r);
5559 bfd_put_32 (abfd, BLR, p);
5560 return p + 4;
5563 /* Called via elf_link_hash_traverse to transfer dynamic linking
5564 information on function code symbol entries to their corresponding
5565 function descriptor symbol entries. */
5567 static bfd_boolean
5568 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5570 struct bfd_link_info *info;
5571 struct ppc_link_hash_table *htab;
5572 struct plt_entry *ent;
5573 struct ppc_link_hash_entry *fh;
5574 struct ppc_link_hash_entry *fdh;
5575 bfd_boolean force_local;
5577 fh = (struct ppc_link_hash_entry *) h;
5578 if (fh->elf.root.type == bfd_link_hash_indirect)
5579 return TRUE;
5581 if (fh->elf.root.type == bfd_link_hash_warning)
5582 fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
5584 info = inf;
5585 htab = ppc_hash_table (info);
5587 /* Resolve undefined references to dot-symbols as the value
5588 in the function descriptor, if we have one in a regular object.
5589 This is to satisfy cases like ".quad .foo". Calls to functions
5590 in dynamic objects are handled elsewhere. */
5591 if (fh->elf.root.type == bfd_link_hash_undefweak
5592 && fh->was_undefined
5593 && (fh->oh->elf.root.type == bfd_link_hash_defined
5594 || fh->oh->elf.root.type == bfd_link_hash_defweak)
5595 && get_opd_info (fh->oh->elf.root.u.def.section) != NULL
5596 && opd_entry_value (fh->oh->elf.root.u.def.section,
5597 fh->oh->elf.root.u.def.value,
5598 &fh->elf.root.u.def.section,
5599 &fh->elf.root.u.def.value) != (bfd_vma) -1)
5601 fh->elf.root.type = fh->oh->elf.root.type;
5602 fh->elf.forced_local = 1;
5605 /* If this is a function code symbol, transfer dynamic linking
5606 information to the function descriptor symbol. */
5607 if (!fh->is_func)
5608 return TRUE;
5610 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5611 if (ent->plt.refcount > 0)
5612 break;
5613 if (ent == NULL
5614 || fh->elf.root.root.string[0] != '.'
5615 || fh->elf.root.root.string[1] == '\0')
5616 return TRUE;
5618 /* Find the corresponding function descriptor symbol. Create it
5619 as undefined if necessary. */
5621 fdh = get_fdh (fh, htab);
5622 if (fdh != NULL)
5623 while (fdh->elf.root.type == bfd_link_hash_indirect
5624 || fdh->elf.root.type == bfd_link_hash_warning)
5625 fdh = (struct ppc_link_hash_entry *) fdh->elf.root.u.i.link;
5627 if (fdh == NULL
5628 && info->shared
5629 && (fh->elf.root.type == bfd_link_hash_undefined
5630 || fh->elf.root.type == bfd_link_hash_undefweak))
5632 fdh = make_fdh (info, fh);
5633 if (fdh == NULL)
5634 return FALSE;
5637 /* Fake function descriptors are made undefweak. If the function
5638 code symbol is strong undefined, make the fake sym the same.
5639 If the function code symbol is defined, then force the fake
5640 descriptor local; We can't support overriding of symbols in a
5641 shared library on a fake descriptor. */
5643 if (fdh != NULL
5644 && fdh->fake
5645 && fdh->elf.root.type == bfd_link_hash_undefweak)
5647 if (fh->elf.root.type == bfd_link_hash_undefined)
5649 fdh->elf.root.type = bfd_link_hash_undefined;
5650 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
5652 else if (fh->elf.root.type == bfd_link_hash_defined
5653 || fh->elf.root.type == bfd_link_hash_defweak)
5655 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
5659 if (fdh != NULL
5660 && !fdh->elf.forced_local
5661 && (info->shared
5662 || fdh->elf.def_dynamic
5663 || fdh->elf.ref_dynamic
5664 || (fdh->elf.root.type == bfd_link_hash_undefweak
5665 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
5667 if (fdh->elf.dynindx == -1)
5668 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5669 return FALSE;
5670 fdh->elf.ref_regular |= fh->elf.ref_regular;
5671 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5672 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5673 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
5674 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
5676 move_plt_plist (fh, fdh);
5677 fdh->elf.needs_plt = 1;
5679 fdh->is_func_descriptor = 1;
5680 fdh->oh = fh;
5681 fh->oh = fdh;
5684 /* Now that the info is on the function descriptor, clear the
5685 function code sym info. Any function code syms for which we
5686 don't have a definition in a regular file, we force local.
5687 This prevents a shared library from exporting syms that have
5688 been imported from another library. Function code syms that
5689 are really in the library we must leave global to prevent the
5690 linker dragging in a definition from a static library. */
5691 force_local = (!fh->elf.def_regular
5692 || fdh == NULL
5693 || !fdh->elf.def_regular
5694 || fdh->elf.forced_local);
5695 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5697 return TRUE;
5700 /* Called near the start of bfd_elf_size_dynamic_sections. We use
5701 this hook to a) provide some gcc support functions, and b) transfer
5702 dynamic linking information gathered so far on function code symbol
5703 entries, to their corresponding function descriptor symbol entries. */
5705 static bfd_boolean
5706 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5707 struct bfd_link_info *info)
5709 struct ppc_link_hash_table *htab;
5710 unsigned int i;
5711 const struct sfpr_def_parms funcs[] =
5713 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5714 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5715 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5716 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5717 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5718 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5719 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5720 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5721 { "._savef", 14, 31, savefpr, savefpr1_tail },
5722 { "._restf", 14, 31, restfpr, restfpr1_tail },
5723 { "_savevr_", 20, 31, savevr, savevr_tail },
5724 { "_restvr_", 20, 31, restvr, restvr_tail }
5727 htab = ppc_hash_table (info);
5728 if (htab->sfpr == NULL)
5729 /* We don't have any relocs. */
5730 return TRUE;
5732 /* Provide any missing _save* and _rest* functions. */
5733 htab->sfpr->size = 0;
5734 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
5735 if (!sfpr_define (info, &funcs[i]))
5736 return FALSE;
5738 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5740 if (htab->sfpr->size == 0)
5741 htab->sfpr->flags |= SEC_EXCLUDE;
5743 return TRUE;
5746 /* Adjust a symbol defined by a dynamic object and referenced by a
5747 regular object. The current definition is in some section of the
5748 dynamic object, but we're not including those sections. We have to
5749 change the definition to something the rest of the link can
5750 understand. */
5752 static bfd_boolean
5753 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
5754 struct elf_link_hash_entry *h)
5756 struct ppc_link_hash_table *htab;
5757 asection *s;
5758 unsigned int power_of_two;
5760 htab = ppc_hash_table (info);
5762 /* Deal with function syms. */
5763 if (h->type == STT_FUNC
5764 || h->needs_plt)
5766 /* Clear procedure linkage table information for any symbol that
5767 won't need a .plt entry. */
5768 struct plt_entry *ent;
5769 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5770 if (ent->plt.refcount > 0)
5771 break;
5772 if (ent == NULL
5773 || SYMBOL_CALLS_LOCAL (info, h)
5774 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5775 && h->root.type == bfd_link_hash_undefweak))
5777 h->plt.plist = NULL;
5778 h->needs_plt = 0;
5781 else
5782 h->plt.plist = NULL;
5784 /* If this is a weak symbol, and there is a real definition, the
5785 processor independent code will have arranged for us to see the
5786 real definition first, and we can just use the same value. */
5787 if (h->u.weakdef != NULL)
5789 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
5790 || h->u.weakdef->root.type == bfd_link_hash_defweak);
5791 h->root.u.def.section = h->u.weakdef->root.u.def.section;
5792 h->root.u.def.value = h->u.weakdef->root.u.def.value;
5793 if (ELIMINATE_COPY_RELOCS)
5794 h->non_got_ref = h->u.weakdef->non_got_ref;
5795 return TRUE;
5798 /* If we are creating a shared library, we must presume that the
5799 only references to the symbol are via the global offset table.
5800 For such cases we need not do anything here; the relocations will
5801 be handled correctly by relocate_section. */
5802 if (info->shared)
5803 return TRUE;
5805 /* If there are no references to this symbol that do not use the
5806 GOT, we don't need to generate a copy reloc. */
5807 if (!h->non_got_ref)
5808 return TRUE;
5810 if (ELIMINATE_COPY_RELOCS)
5812 struct ppc_link_hash_entry * eh;
5813 struct ppc_dyn_relocs *p;
5815 eh = (struct ppc_link_hash_entry *) h;
5816 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5818 s = p->sec->output_section;
5819 if (s != NULL && (s->flags & SEC_READONLY) != 0)
5820 break;
5823 /* If we didn't find any dynamic relocs in read-only sections, then
5824 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
5825 if (p == NULL)
5827 h->non_got_ref = 0;
5828 return TRUE;
5832 if (h->plt.plist != NULL)
5834 /* We should never get here, but unfortunately there are versions
5835 of gcc out there that improperly (for this ABI) put initialized
5836 function pointers, vtable refs and suchlike in read-only
5837 sections. Allow them to proceed, but warn that this might
5838 break at runtime. */
5839 (*_bfd_error_handler)
5840 (_("copy reloc against `%s' requires lazy plt linking; "
5841 "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
5842 h->root.root.string);
5845 /* This is a reference to a symbol defined by a dynamic object which
5846 is not a function. */
5848 if (h->size == 0)
5850 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
5851 h->root.root.string);
5852 return TRUE;
5855 /* We must allocate the symbol in our .dynbss section, which will
5856 become part of the .bss section of the executable. There will be
5857 an entry for this symbol in the .dynsym section. The dynamic
5858 object will contain position independent code, so all references
5859 from the dynamic object to this symbol will go through the global
5860 offset table. The dynamic linker will use the .dynsym entry to
5861 determine the address it must put in the global offset table, so
5862 both the dynamic object and the regular object will refer to the
5863 same memory location for the variable. */
5865 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
5866 to copy the initial value out of the dynamic object and into the
5867 runtime process image. We need to remember the offset into the
5868 .rela.bss section we are going to use. */
5869 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5871 htab->relbss->size += sizeof (Elf64_External_Rela);
5872 h->needs_copy = 1;
5875 /* We need to figure out the alignment required for this symbol. I
5876 have no idea how ELF linkers handle this. */
5877 power_of_two = bfd_log2 (h->size);
5878 if (power_of_two > 4)
5879 power_of_two = 4;
5881 /* Apply the required alignment. */
5882 s = htab->dynbss;
5883 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
5884 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
5886 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
5887 return FALSE;
5890 /* Define the symbol as being at this point in the section. */
5891 h->root.u.def.section = s;
5892 h->root.u.def.value = s->size;
5894 /* Increment the section size to make room for the symbol. */
5895 s->size += h->size;
5897 return TRUE;
5900 /* If given a function descriptor symbol, hide both the function code
5901 sym and the descriptor. */
5902 static void
5903 ppc64_elf_hide_symbol (struct bfd_link_info *info,
5904 struct elf_link_hash_entry *h,
5905 bfd_boolean force_local)
5907 struct ppc_link_hash_entry *eh;
5908 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
5910 eh = (struct ppc_link_hash_entry *) h;
5911 if (eh->is_func_descriptor)
5913 struct ppc_link_hash_entry *fh = eh->oh;
5915 if (fh == NULL)
5917 const char *p, *q;
5918 struct ppc_link_hash_table *htab;
5919 char save;
5921 /* We aren't supposed to use alloca in BFD because on
5922 systems which do not have alloca the version in libiberty
5923 calls xmalloc, which might cause the program to crash
5924 when it runs out of memory. This function doesn't have a
5925 return status, so there's no way to gracefully return an
5926 error. So cheat. We know that string[-1] can be safely
5927 accessed; It's either a string in an ELF string table,
5928 or allocated in an objalloc structure. */
5930 p = eh->elf.root.root.string - 1;
5931 save = *p;
5932 *(char *) p = '.';
5933 htab = ppc_hash_table (info);
5934 fh = (struct ppc_link_hash_entry *)
5935 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
5936 *(char *) p = save;
5938 /* Unfortunately, if it so happens that the string we were
5939 looking for was allocated immediately before this string,
5940 then we overwrote the string terminator. That's the only
5941 reason the lookup should fail. */
5942 if (fh == NULL)
5944 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
5945 while (q >= eh->elf.root.root.string && *q == *p)
5946 --q, --p;
5947 if (q < eh->elf.root.root.string && *p == '.')
5948 fh = (struct ppc_link_hash_entry *)
5949 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
5951 if (fh != NULL)
5953 eh->oh = fh;
5954 fh->oh = eh;
5957 if (fh != NULL)
5958 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5962 static bfd_boolean
5963 get_sym_h (struct elf_link_hash_entry **hp,
5964 Elf_Internal_Sym **symp,
5965 asection **symsecp,
5966 char **tls_maskp,
5967 Elf_Internal_Sym **locsymsp,
5968 unsigned long r_symndx,
5969 bfd *ibfd)
5971 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5973 if (r_symndx >= symtab_hdr->sh_info)
5975 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
5976 struct elf_link_hash_entry *h;
5978 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5979 while (h->root.type == bfd_link_hash_indirect
5980 || h->root.type == bfd_link_hash_warning)
5981 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5983 if (hp != NULL)
5984 *hp = h;
5986 if (symp != NULL)
5987 *symp = NULL;
5989 if (symsecp != NULL)
5991 asection *symsec = NULL;
5992 if (h->root.type == bfd_link_hash_defined
5993 || h->root.type == bfd_link_hash_defweak)
5994 symsec = h->root.u.def.section;
5995 *symsecp = symsec;
5998 if (tls_maskp != NULL)
6000 struct ppc_link_hash_entry *eh;
6002 eh = (struct ppc_link_hash_entry *) h;
6003 *tls_maskp = &eh->tls_mask;
6006 else
6008 Elf_Internal_Sym *sym;
6009 Elf_Internal_Sym *locsyms = *locsymsp;
6011 if (locsyms == NULL)
6013 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6014 if (locsyms == NULL)
6015 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6016 symtab_hdr->sh_info,
6017 0, NULL, NULL, NULL);
6018 if (locsyms == NULL)
6019 return FALSE;
6020 *locsymsp = locsyms;
6022 sym = locsyms + r_symndx;
6024 if (hp != NULL)
6025 *hp = NULL;
6027 if (symp != NULL)
6028 *symp = sym;
6030 if (symsecp != NULL)
6032 asection *symsec = NULL;
6033 if ((sym->st_shndx != SHN_UNDEF
6034 && sym->st_shndx < SHN_LORESERVE)
6035 || sym->st_shndx > SHN_HIRESERVE)
6036 symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6037 *symsecp = symsec;
6040 if (tls_maskp != NULL)
6042 struct got_entry **lgot_ents;
6043 char *tls_mask;
6045 tls_mask = NULL;
6046 lgot_ents = elf_local_got_ents (ibfd);
6047 if (lgot_ents != NULL)
6049 char *lgot_masks = (char *) (lgot_ents + symtab_hdr->sh_info);
6050 tls_mask = &lgot_masks[r_symndx];
6052 *tls_maskp = tls_mask;
6055 return TRUE;
6058 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6059 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6060 type suitable for optimization, and 1 otherwise. */
6062 static int
6063 get_tls_mask (char **tls_maskp, unsigned long *toc_symndx,
6064 Elf_Internal_Sym **locsymsp,
6065 const Elf_Internal_Rela *rel, bfd *ibfd)
6067 unsigned long r_symndx;
6068 int next_r;
6069 struct elf_link_hash_entry *h;
6070 Elf_Internal_Sym *sym;
6071 asection *sec;
6072 bfd_vma off;
6074 r_symndx = ELF64_R_SYM (rel->r_info);
6075 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6076 return 0;
6078 if ((*tls_maskp != NULL && **tls_maskp != 0)
6079 || sec == NULL
6080 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6081 return 1;
6083 /* Look inside a TOC section too. */
6084 if (h != NULL)
6086 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6087 off = h->root.u.def.value;
6089 else
6090 off = sym->st_value;
6091 off += rel->r_addend;
6092 BFD_ASSERT (off % 8 == 0);
6093 r_symndx = ppc64_elf_section_data (sec)->u.t_symndx[off / 8];
6094 next_r = ppc64_elf_section_data (sec)->u.t_symndx[off / 8 + 1];
6095 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6096 return 0;
6097 if (toc_symndx != NULL)
6098 *toc_symndx = r_symndx;
6099 if ((h == NULL
6100 || ((h->root.type == bfd_link_hash_defined
6101 || h->root.type == bfd_link_hash_defweak)
6102 && !h->def_dynamic))
6103 && (next_r == -1 || next_r == -2))
6104 return 1 - next_r;
6105 return 1;
6108 /* Adjust all global syms defined in opd sections. In gcc generated
6109 code for the old ABI, these will already have been done. */
6111 static bfd_boolean
6112 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6114 struct ppc_link_hash_entry *eh;
6115 asection *sym_sec;
6116 long *opd_adjust;
6118 if (h->root.type == bfd_link_hash_indirect)
6119 return TRUE;
6121 if (h->root.type == bfd_link_hash_warning)
6122 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6124 if (h->root.type != bfd_link_hash_defined
6125 && h->root.type != bfd_link_hash_defweak)
6126 return TRUE;
6128 eh = (struct ppc_link_hash_entry *) h;
6129 if (eh->adjust_done)
6130 return TRUE;
6132 sym_sec = eh->elf.root.u.def.section;
6133 opd_adjust = get_opd_info (sym_sec);
6134 if (opd_adjust != NULL)
6136 long adjust = opd_adjust[eh->elf.root.u.def.value / 8];
6137 if (adjust == -1)
6139 /* This entry has been deleted. */
6140 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6141 if (dsec == NULL)
6143 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6144 if (elf_discarded_section (dsec))
6146 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6147 break;
6150 eh->elf.root.u.def.value = 0;
6151 eh->elf.root.u.def.section = dsec;
6153 else
6154 eh->elf.root.u.def.value += adjust;
6155 eh->adjust_done = 1;
6157 return TRUE;
6160 /* Handles decrementing dynamic reloc counts for the reloc specified by
6161 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6162 have already been determined. */
6164 static bfd_boolean
6165 dec_dynrel_count (bfd_vma r_info,
6166 asection *sec,
6167 struct bfd_link_info *info,
6168 Elf_Internal_Sym **local_syms,
6169 struct elf_link_hash_entry *h,
6170 asection *sym_sec)
6172 enum elf_ppc64_reloc_type r_type;
6173 struct ppc_dyn_relocs *p;
6174 struct ppc_dyn_relocs **pp;
6176 /* Can this reloc be dynamic? This switch, and later tests here
6177 should be kept in sync with the code in check_relocs. */
6178 r_type = ELF64_R_TYPE (r_info);
6179 switch (r_type)
6181 default:
6182 return TRUE;
6184 case R_PPC64_TPREL16:
6185 case R_PPC64_TPREL16_LO:
6186 case R_PPC64_TPREL16_HI:
6187 case R_PPC64_TPREL16_HA:
6188 case R_PPC64_TPREL16_DS:
6189 case R_PPC64_TPREL16_LO_DS:
6190 case R_PPC64_TPREL16_HIGHER:
6191 case R_PPC64_TPREL16_HIGHERA:
6192 case R_PPC64_TPREL16_HIGHEST:
6193 case R_PPC64_TPREL16_HIGHESTA:
6194 if (!info->shared)
6195 return TRUE;
6197 case R_PPC64_TPREL64:
6198 case R_PPC64_DTPMOD64:
6199 case R_PPC64_DTPREL64:
6200 case R_PPC64_ADDR64:
6201 case R_PPC64_REL30:
6202 case R_PPC64_REL32:
6203 case R_PPC64_REL64:
6204 case R_PPC64_ADDR14:
6205 case R_PPC64_ADDR14_BRNTAKEN:
6206 case R_PPC64_ADDR14_BRTAKEN:
6207 case R_PPC64_ADDR16:
6208 case R_PPC64_ADDR16_DS:
6209 case R_PPC64_ADDR16_HA:
6210 case R_PPC64_ADDR16_HI:
6211 case R_PPC64_ADDR16_HIGHER:
6212 case R_PPC64_ADDR16_HIGHERA:
6213 case R_PPC64_ADDR16_HIGHEST:
6214 case R_PPC64_ADDR16_HIGHESTA:
6215 case R_PPC64_ADDR16_LO:
6216 case R_PPC64_ADDR16_LO_DS:
6217 case R_PPC64_ADDR24:
6218 case R_PPC64_ADDR32:
6219 case R_PPC64_UADDR16:
6220 case R_PPC64_UADDR32:
6221 case R_PPC64_UADDR64:
6222 case R_PPC64_TOC:
6223 break;
6226 if (local_syms != NULL)
6228 unsigned long r_symndx;
6229 Elf_Internal_Sym *sym;
6230 bfd *ibfd = sec->owner;
6232 r_symndx = ELF64_R_SYM (r_info);
6233 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6234 return FALSE;
6237 if ((info->shared
6238 && (MUST_BE_DYN_RELOC (r_type)
6239 || (h != NULL
6240 && (!info->symbolic
6241 || h->root.type == bfd_link_hash_defweak
6242 || !h->def_regular))))
6243 || (ELIMINATE_COPY_RELOCS
6244 && !info->shared
6245 && h != NULL
6246 && (h->root.type == bfd_link_hash_defweak
6247 || !h->def_regular)))
6249 else
6250 return TRUE;
6252 if (h != NULL)
6253 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6254 else
6256 if (sym_sec != NULL)
6258 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6259 pp = (struct ppc_dyn_relocs **) vpp;
6261 else
6263 void *vpp = &elf_section_data (sec)->local_dynrel;
6264 pp = (struct ppc_dyn_relocs **) vpp;
6267 /* elf_gc_sweep may have already removed all dyn relocs associated
6268 with local syms for a given section. Don't report a dynreloc
6269 miscount. */
6270 if (*pp == NULL)
6271 return TRUE;
6274 while ((p = *pp) != NULL)
6276 if (p->sec == sec)
6278 if (!MUST_BE_DYN_RELOC (r_type))
6279 p->pc_count -= 1;
6280 p->count -= 1;
6281 if (p->count == 0)
6282 *pp = p->next;
6283 return TRUE;
6285 pp = &p->next;
6288 (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
6289 sec->owner, sec);
6290 bfd_set_error (bfd_error_bad_value);
6291 return FALSE;
6294 /* Remove unused Official Procedure Descriptor entries. Currently we
6295 only remove those associated with functions in discarded link-once
6296 sections, or weakly defined functions that have been overridden. It
6297 would be possible to remove many more entries for statically linked
6298 applications. */
6300 bfd_boolean
6301 ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info,
6302 bfd_boolean no_opd_opt,
6303 bfd_boolean non_overlapping)
6305 bfd *ibfd;
6306 bfd_boolean some_edited = FALSE;
6307 asection *need_pad = NULL;
6309 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6311 asection *sec;
6312 Elf_Internal_Rela *relstart, *rel, *relend;
6313 Elf_Internal_Shdr *symtab_hdr;
6314 Elf_Internal_Sym *local_syms;
6315 struct elf_link_hash_entry **sym_hashes;
6316 bfd_vma offset;
6317 bfd_size_type amt;
6318 long *opd_adjust;
6319 bfd_boolean need_edit, add_aux_fields;
6320 bfd_size_type cnt_16b = 0;
6322 sec = bfd_get_section_by_name (ibfd, ".opd");
6323 if (sec == NULL || sec->size == 0)
6324 continue;
6326 amt = sec->size * sizeof (long) / 8;
6327 opd_adjust = get_opd_info (sec);
6328 if (opd_adjust == NULL)
6330 /* check_relocs hasn't been called. Must be a ld -r link
6331 or --just-symbols object. */
6332 opd_adjust = bfd_alloc (obfd, amt);
6333 if (opd_adjust == NULL)
6334 return FALSE;
6335 ppc64_elf_section_data (sec)->u.opd_adjust = opd_adjust;
6336 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
6337 ppc64_elf_section_data (sec)->sec_type = sec_opd;
6339 memset (opd_adjust, 0, amt);
6341 if (no_opd_opt)
6342 continue;
6344 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6345 continue;
6347 if (sec->output_section == bfd_abs_section_ptr)
6348 continue;
6350 /* Look through the section relocs. */
6351 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6352 continue;
6354 local_syms = NULL;
6355 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
6356 sym_hashes = elf_sym_hashes (ibfd);
6358 /* Read the relocations. */
6359 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6360 info->keep_memory);
6361 if (relstart == NULL)
6362 return FALSE;
6364 /* First run through the relocs to check they are sane, and to
6365 determine whether we need to edit this opd section. */
6366 need_edit = FALSE;
6367 need_pad = sec;
6368 offset = 0;
6369 relend = relstart + sec->reloc_count;
6370 for (rel = relstart; rel < relend; )
6372 enum elf_ppc64_reloc_type r_type;
6373 unsigned long r_symndx;
6374 asection *sym_sec;
6375 struct elf_link_hash_entry *h;
6376 Elf_Internal_Sym *sym;
6378 /* .opd contains a regular array of 16 or 24 byte entries. We're
6379 only interested in the reloc pointing to a function entry
6380 point. */
6381 if (rel->r_offset != offset
6382 || rel + 1 >= relend
6383 || (rel + 1)->r_offset != offset + 8)
6385 /* If someone messes with .opd alignment then after a
6386 "ld -r" we might have padding in the middle of .opd.
6387 Also, there's nothing to prevent someone putting
6388 something silly in .opd with the assembler. No .opd
6389 optimization for them! */
6390 broken_opd:
6391 (*_bfd_error_handler)
6392 (_("%B: .opd is not a regular array of opd entries"), ibfd);
6393 need_edit = FALSE;
6394 break;
6397 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6398 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6400 (*_bfd_error_handler)
6401 (_("%B: unexpected reloc type %u in .opd section"),
6402 ibfd, r_type);
6403 need_edit = FALSE;
6404 break;
6407 r_symndx = ELF64_R_SYM (rel->r_info);
6408 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6409 r_symndx, ibfd))
6410 goto error_ret;
6412 if (sym_sec == NULL || sym_sec->owner == NULL)
6414 const char *sym_name;
6415 if (h != NULL)
6416 sym_name = h->root.root.string;
6417 else
6418 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6419 sym_sec);
6421 (*_bfd_error_handler)
6422 (_("%B: undefined sym `%s' in .opd section"),
6423 ibfd, sym_name);
6424 need_edit = FALSE;
6425 break;
6428 /* opd entries are always for functions defined in the
6429 current input bfd. If the symbol isn't defined in the
6430 input bfd, then we won't be using the function in this
6431 bfd; It must be defined in a linkonce section in another
6432 bfd, or is weak. It's also possible that we are
6433 discarding the function due to a linker script /DISCARD/,
6434 which we test for via the output_section. */
6435 if (sym_sec->owner != ibfd
6436 || sym_sec->output_section == bfd_abs_section_ptr)
6437 need_edit = TRUE;
6439 rel += 2;
6440 if (rel == relend
6441 || (rel + 1 == relend && rel->r_offset == offset + 16))
6443 if (sec->size == offset + 24)
6445 need_pad = NULL;
6446 break;
6448 if (rel == relend && sec->size == offset + 16)
6450 cnt_16b++;
6451 break;
6453 goto broken_opd;
6456 if (rel->r_offset == offset + 24)
6457 offset += 24;
6458 else if (rel->r_offset != offset + 16)
6459 goto broken_opd;
6460 else if (rel + 1 < relend
6461 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6462 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6464 offset += 16;
6465 cnt_16b++;
6467 else if (rel + 2 < relend
6468 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
6469 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
6471 offset += 24;
6472 rel += 1;
6474 else
6475 goto broken_opd;
6478 add_aux_fields = non_overlapping && cnt_16b > 0;
6480 if (need_edit || add_aux_fields)
6482 Elf_Internal_Rela *write_rel;
6483 bfd_byte *rptr, *wptr;
6484 bfd_byte *new_contents = NULL;
6485 bfd_boolean skip;
6486 long opd_ent_size;
6488 /* This seems a waste of time as input .opd sections are all
6489 zeros as generated by gcc, but I suppose there's no reason
6490 this will always be so. We might start putting something in
6491 the third word of .opd entries. */
6492 if ((sec->flags & SEC_IN_MEMORY) == 0)
6494 bfd_byte *loc;
6495 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6497 if (loc != NULL)
6498 free (loc);
6499 error_ret:
6500 if (local_syms != NULL
6501 && symtab_hdr->contents != (unsigned char *) local_syms)
6502 free (local_syms);
6503 if (elf_section_data (sec)->relocs != relstart)
6504 free (relstart);
6505 return FALSE;
6507 sec->contents = loc;
6508 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6511 elf_section_data (sec)->relocs = relstart;
6513 new_contents = sec->contents;
6514 if (add_aux_fields)
6516 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6517 if (new_contents == NULL)
6518 return FALSE;
6519 need_pad = FALSE;
6521 wptr = new_contents;
6522 rptr = sec->contents;
6524 write_rel = relstart;
6525 skip = FALSE;
6526 offset = 0;
6527 opd_ent_size = 0;
6528 for (rel = relstart; rel < relend; rel++)
6530 unsigned long r_symndx;
6531 asection *sym_sec;
6532 struct elf_link_hash_entry *h;
6533 Elf_Internal_Sym *sym;
6535 r_symndx = ELF64_R_SYM (rel->r_info);
6536 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6537 r_symndx, ibfd))
6538 goto error_ret;
6540 if (rel->r_offset == offset)
6542 struct ppc_link_hash_entry *fdh = NULL;
6544 /* See if the .opd entry is full 24 byte or
6545 16 byte (with fd_aux entry overlapped with next
6546 fd_func). */
6547 opd_ent_size = 24;
6548 if ((rel + 2 == relend && sec->size == offset + 16)
6549 || (rel + 3 < relend
6550 && rel[2].r_offset == offset + 16
6551 && rel[3].r_offset == offset + 24
6552 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
6553 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
6554 opd_ent_size = 16;
6556 if (h != NULL
6557 && h->root.root.string[0] == '.')
6559 fdh = get_fdh ((struct ppc_link_hash_entry *) h,
6560 ppc_hash_table (info));
6561 if (fdh != NULL
6562 && fdh->elf.root.type != bfd_link_hash_defined
6563 && fdh->elf.root.type != bfd_link_hash_defweak)
6564 fdh = NULL;
6567 skip = (sym_sec->owner != ibfd
6568 || sym_sec->output_section == bfd_abs_section_ptr);
6569 if (skip)
6571 if (fdh != NULL && sym_sec->owner == ibfd)
6573 /* Arrange for the function descriptor sym
6574 to be dropped. */
6575 fdh->elf.root.u.def.value = 0;
6576 fdh->elf.root.u.def.section = sym_sec;
6578 opd_adjust[rel->r_offset / 8] = -1;
6580 else
6582 /* We'll be keeping this opd entry. */
6584 if (fdh != NULL)
6586 /* Redefine the function descriptor symbol to
6587 this location in the opd section. It is
6588 necessary to update the value here rather
6589 than using an array of adjustments as we do
6590 for local symbols, because various places
6591 in the generic ELF code use the value
6592 stored in u.def.value. */
6593 fdh->elf.root.u.def.value = wptr - new_contents;
6594 fdh->adjust_done = 1;
6597 /* Local syms are a bit tricky. We could
6598 tweak them as they can be cached, but
6599 we'd need to look through the local syms
6600 for the function descriptor sym which we
6601 don't have at the moment. So keep an
6602 array of adjustments. */
6603 opd_adjust[rel->r_offset / 8]
6604 = (wptr - new_contents) - (rptr - sec->contents);
6606 if (wptr != rptr)
6607 memcpy (wptr, rptr, opd_ent_size);
6608 wptr += opd_ent_size;
6609 if (add_aux_fields && opd_ent_size == 16)
6611 memset (wptr, '\0', 8);
6612 wptr += 8;
6615 rptr += opd_ent_size;
6616 offset += opd_ent_size;
6619 if (skip)
6621 if (!NO_OPD_RELOCS
6622 && !info->relocatable
6623 && !dec_dynrel_count (rel->r_info, sec, info,
6624 NULL, h, sym_sec))
6625 goto error_ret;
6627 else
6629 /* We need to adjust any reloc offsets to point to the
6630 new opd entries. While we're at it, we may as well
6631 remove redundant relocs. */
6632 rel->r_offset += opd_adjust[(offset - opd_ent_size) / 8];
6633 if (write_rel != rel)
6634 memcpy (write_rel, rel, sizeof (*rel));
6635 ++write_rel;
6639 sec->size = wptr - new_contents;
6640 sec->reloc_count = write_rel - relstart;
6641 if (add_aux_fields)
6643 free (sec->contents);
6644 sec->contents = new_contents;
6647 /* Fudge the header size too, as this is used later in
6648 elf_bfd_final_link if we are emitting relocs. */
6649 elf_section_data (sec)->rel_hdr.sh_size
6650 = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
6651 BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
6652 some_edited = TRUE;
6654 else if (elf_section_data (sec)->relocs != relstart)
6655 free (relstart);
6657 if (local_syms != NULL
6658 && symtab_hdr->contents != (unsigned char *) local_syms)
6660 if (!info->keep_memory)
6661 free (local_syms);
6662 else
6663 symtab_hdr->contents = (unsigned char *) local_syms;
6667 if (some_edited)
6668 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
6670 /* If we are doing a final link and the last .opd entry is just 16 byte
6671 long, add a 8 byte padding after it. */
6672 if (need_pad != NULL && !info->relocatable)
6674 bfd_byte *p;
6676 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
6678 BFD_ASSERT (need_pad->size > 0);
6680 p = bfd_malloc (need_pad->size + 8);
6681 if (p == NULL)
6682 return FALSE;
6684 if (! bfd_get_section_contents (need_pad->owner, need_pad,
6685 p, 0, need_pad->size))
6686 return FALSE;
6688 need_pad->contents = p;
6689 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6691 else
6693 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
6694 if (p == NULL)
6695 return FALSE;
6697 need_pad->contents = p;
6700 memset (need_pad->contents + need_pad->size, 0, 8);
6701 need_pad->size += 8;
6704 return TRUE;
6707 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
6709 asection *
6710 ppc64_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
6712 struct ppc_link_hash_table *htab;
6714 htab = ppc_hash_table (info);
6715 if (htab->tls_get_addr != NULL)
6717 struct ppc_link_hash_entry *h = htab->tls_get_addr;
6719 while (h->elf.root.type == bfd_link_hash_indirect
6720 || h->elf.root.type == bfd_link_hash_warning)
6721 h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
6723 htab->tls_get_addr = h;
6725 if (htab->tls_get_addr_fd == NULL
6726 && h->oh != NULL
6727 && h->oh->is_func_descriptor
6728 && (h->oh->elf.root.type == bfd_link_hash_defined
6729 || h->oh->elf.root.type == bfd_link_hash_defweak))
6730 htab->tls_get_addr_fd = h->oh;
6733 if (htab->tls_get_addr_fd != NULL)
6735 struct ppc_link_hash_entry *h = htab->tls_get_addr_fd;
6737 while (h->elf.root.type == bfd_link_hash_indirect
6738 || h->elf.root.type == bfd_link_hash_warning)
6739 h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
6741 htab->tls_get_addr_fd = h;
6744 return _bfd_elf_tls_setup (obfd, info);
6747 /* Run through all the TLS relocs looking for optimization
6748 opportunities. The linker has been hacked (see ppc64elf.em) to do
6749 a preliminary section layout so that we know the TLS segment
6750 offsets. We can't optimize earlier because some optimizations need
6751 to know the tp offset, and we need to optimize before allocating
6752 dynamic relocations. */
6754 bfd_boolean
6755 ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6757 bfd *ibfd;
6758 asection *sec;
6759 struct ppc_link_hash_table *htab;
6761 if (info->relocatable || info->shared)
6762 return TRUE;
6764 htab = ppc_hash_table (info);
6765 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6767 Elf_Internal_Sym *locsyms = NULL;
6768 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
6769 unsigned char *toc_ref = NULL;
6771 /* Look at all the sections for this file, with TOC last. */
6772 for (sec = (ibfd->sections == toc && toc && toc->next ? toc->next
6773 : ibfd->sections);
6774 sec != NULL;
6775 sec = (sec == toc ? NULL
6776 : sec->next == NULL ? toc
6777 : sec->next == toc && toc->next ? toc->next
6778 : sec->next))
6779 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
6781 Elf_Internal_Rela *relstart, *rel, *relend;
6782 int expecting_tls_get_addr;
6783 long toc_ref_index = 0;
6785 /* Read the relocations. */
6786 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6787 info->keep_memory);
6788 if (relstart == NULL)
6789 return FALSE;
6791 expecting_tls_get_addr = 0;
6792 relend = relstart + sec->reloc_count;
6793 for (rel = relstart; rel < relend; rel++)
6795 enum elf_ppc64_reloc_type r_type;
6796 unsigned long r_symndx;
6797 struct elf_link_hash_entry *h;
6798 Elf_Internal_Sym *sym;
6799 asection *sym_sec;
6800 char *tls_mask;
6801 char tls_set, tls_clear, tls_type = 0;
6802 bfd_vma value;
6803 bfd_boolean ok_tprel, is_local;
6805 r_symndx = ELF64_R_SYM (rel->r_info);
6806 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
6807 r_symndx, ibfd))
6809 err_free_rel:
6810 if (elf_section_data (sec)->relocs != relstart)
6811 free (relstart);
6812 if (toc_ref != NULL)
6813 free (toc_ref);
6814 if (locsyms != NULL
6815 && (elf_tdata (ibfd)->symtab_hdr.contents
6816 != (unsigned char *) locsyms))
6817 free (locsyms);
6818 return FALSE;
6821 if (h != NULL)
6823 if (h->root.type != bfd_link_hash_defined
6824 && h->root.type != bfd_link_hash_defweak)
6825 continue;
6826 value = h->root.u.def.value;
6828 else
6829 /* Symbols referenced by TLS relocs must be of type
6830 STT_TLS. So no need for .opd local sym adjust. */
6831 value = sym->st_value;
6833 ok_tprel = FALSE;
6834 is_local = FALSE;
6835 if (h == NULL
6836 || !h->def_dynamic)
6838 is_local = TRUE;
6839 value += sym_sec->output_offset;
6840 value += sym_sec->output_section->vma;
6841 value -= htab->elf.tls_sec->vma;
6842 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
6843 < (bfd_vma) 1 << 32);
6846 r_type = ELF64_R_TYPE (rel->r_info);
6847 switch (r_type)
6849 case R_PPC64_GOT_TLSLD16:
6850 case R_PPC64_GOT_TLSLD16_LO:
6851 case R_PPC64_GOT_TLSLD16_HI:
6852 case R_PPC64_GOT_TLSLD16_HA:
6853 /* These relocs should never be against a symbol
6854 defined in a shared lib. Leave them alone if
6855 that turns out to be the case. */
6856 ppc64_tlsld_got (ibfd)->refcount -= 1;
6857 if (!is_local)
6858 continue;
6860 /* LD -> LE */
6861 tls_set = 0;
6862 tls_clear = TLS_LD;
6863 tls_type = TLS_TLS | TLS_LD;
6864 expecting_tls_get_addr = 1;
6865 break;
6867 case R_PPC64_GOT_TLSGD16:
6868 case R_PPC64_GOT_TLSGD16_LO:
6869 case R_PPC64_GOT_TLSGD16_HI:
6870 case R_PPC64_GOT_TLSGD16_HA:
6871 if (ok_tprel)
6872 /* GD -> LE */
6873 tls_set = 0;
6874 else
6875 /* GD -> IE */
6876 tls_set = TLS_TLS | TLS_TPRELGD;
6877 tls_clear = TLS_GD;
6878 tls_type = TLS_TLS | TLS_GD;
6879 expecting_tls_get_addr = 1;
6880 break;
6882 case R_PPC64_GOT_TPREL16_DS:
6883 case R_PPC64_GOT_TPREL16_LO_DS:
6884 case R_PPC64_GOT_TPREL16_HI:
6885 case R_PPC64_GOT_TPREL16_HA:
6886 expecting_tls_get_addr = 0;
6887 if (ok_tprel)
6889 /* IE -> LE */
6890 tls_set = 0;
6891 tls_clear = TLS_TPREL;
6892 tls_type = TLS_TLS | TLS_TPREL;
6893 break;
6895 else
6896 continue;
6898 case R_PPC64_REL14:
6899 case R_PPC64_REL14_BRTAKEN:
6900 case R_PPC64_REL14_BRNTAKEN:
6901 case R_PPC64_REL24:
6902 if (h != NULL
6903 && (h == &htab->tls_get_addr->elf
6904 || h == &htab->tls_get_addr_fd->elf))
6906 if (!expecting_tls_get_addr
6907 && rel != relstart
6908 && ((ELF64_R_TYPE (rel[-1].r_info)
6909 == R_PPC64_TOC16)
6910 || (ELF64_R_TYPE (rel[-1].r_info)
6911 == R_PPC64_TOC16_LO)))
6913 /* Check for toc tls entries. */
6914 char *toc_tls;
6915 int retval;
6917 retval = get_tls_mask (&toc_tls, NULL, &locsyms,
6918 rel - 1, ibfd);
6919 if (retval == 0)
6920 goto err_free_rel;
6921 if (retval > 1 && toc_tls != NULL)
6923 expecting_tls_get_addr = 1;
6924 if (toc_ref != NULL)
6925 toc_ref[toc_ref_index] = 1;
6929 if (expecting_tls_get_addr)
6931 struct plt_entry *ent;
6932 for (ent = h->plt.plist; ent; ent = ent->next)
6933 if (ent->addend == 0)
6935 if (ent->plt.refcount > 0)
6936 ent->plt.refcount -= 1;
6937 break;
6941 expecting_tls_get_addr = 0;
6942 continue;
6944 case R_PPC64_TOC16:
6945 case R_PPC64_TOC16_LO:
6946 case R_PPC64_TLS:
6947 expecting_tls_get_addr = 0;
6948 if (sym_sec == toc && toc != NULL)
6950 /* Mark this toc entry as referenced by a TLS
6951 code sequence. We can do that now in the
6952 case of R_PPC64_TLS, and after checking for
6953 tls_get_addr for the TOC16 relocs. */
6954 if (toc_ref == NULL)
6956 toc_ref = bfd_zmalloc (toc->size / 8);
6957 if (toc_ref == NULL)
6958 goto err_free_rel;
6960 if (h != NULL)
6961 value = h->root.u.def.value;
6962 else
6963 value = sym->st_value;
6964 value += rel->r_addend;
6965 BFD_ASSERT (value < toc->size && value % 8 == 0);
6966 toc_ref_index = value / 8;
6967 if (r_type == R_PPC64_TLS)
6968 toc_ref[toc_ref_index] = 1;
6970 continue;
6972 case R_PPC64_TPREL64:
6973 expecting_tls_get_addr = 0;
6974 if (sec != toc
6975 || toc_ref == NULL
6976 || !toc_ref[rel->r_offset / 8])
6977 continue;
6978 if (ok_tprel)
6980 /* IE -> LE */
6981 tls_set = TLS_EXPLICIT;
6982 tls_clear = TLS_TPREL;
6983 break;
6985 else
6986 continue;
6988 case R_PPC64_DTPMOD64:
6989 expecting_tls_get_addr = 0;
6990 if (sec != toc
6991 || toc_ref == NULL
6992 || !toc_ref[rel->r_offset / 8])
6993 continue;
6994 if (rel + 1 < relend
6995 && (rel[1].r_info
6996 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
6997 && rel[1].r_offset == rel->r_offset + 8)
6999 if (ok_tprel)
7000 /* GD -> LE */
7001 tls_set = TLS_EXPLICIT | TLS_GD;
7002 else
7003 /* GD -> IE */
7004 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7005 tls_clear = TLS_GD;
7007 else
7009 if (!is_local)
7010 continue;
7012 /* LD -> LE */
7013 tls_set = TLS_EXPLICIT;
7014 tls_clear = TLS_LD;
7016 break;
7018 default:
7019 expecting_tls_get_addr = 0;
7020 continue;
7023 if ((tls_set & TLS_EXPLICIT) == 0)
7025 struct got_entry *ent;
7027 /* Adjust got entry for this reloc. */
7028 if (h != NULL)
7029 ent = h->got.glist;
7030 else
7031 ent = elf_local_got_ents (ibfd)[r_symndx];
7033 for (; ent != NULL; ent = ent->next)
7034 if (ent->addend == rel->r_addend
7035 && ent->owner == ibfd
7036 && ent->tls_type == tls_type)
7037 break;
7038 if (ent == NULL)
7039 abort ();
7041 if (tls_set == 0)
7043 /* We managed to get rid of a got entry. */
7044 if (ent->got.refcount > 0)
7045 ent->got.refcount -= 1;
7048 else
7050 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7051 we'll lose one or two dyn relocs. */
7052 if (!dec_dynrel_count (rel->r_info, sec, info,
7053 NULL, h, sym_sec))
7054 return FALSE;
7056 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7058 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7059 NULL, h, sym_sec))
7060 return FALSE;
7064 *tls_mask |= tls_set;
7065 *tls_mask &= ~tls_clear;
7068 if (elf_section_data (sec)->relocs != relstart)
7069 free (relstart);
7072 if (toc_ref != NULL)
7073 free (toc_ref);
7075 if (locsyms != NULL
7076 && (elf_tdata (ibfd)->symtab_hdr.contents
7077 != (unsigned char *) locsyms))
7079 if (!info->keep_memory)
7080 free (locsyms);
7081 else
7082 elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
7085 return TRUE;
7088 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7089 the values of any global symbols in a toc section that has been
7090 edited. Globals in toc sections should be a rarity, so this function
7091 sets a flag if any are found in toc sections other than the one just
7092 edited, so that futher hash table traversals can be avoided. */
7094 struct adjust_toc_info
7096 asection *toc;
7097 unsigned long *skip;
7098 bfd_boolean global_toc_syms;
7101 static bfd_boolean
7102 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7104 struct ppc_link_hash_entry *eh;
7105 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7107 if (h->root.type == bfd_link_hash_indirect)
7108 return TRUE;
7110 if (h->root.type == bfd_link_hash_warning)
7111 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7113 if (h->root.type != bfd_link_hash_defined
7114 && h->root.type != bfd_link_hash_defweak)
7115 return TRUE;
7117 eh = (struct ppc_link_hash_entry *) h;
7118 if (eh->adjust_done)
7119 return TRUE;
7121 if (eh->elf.root.u.def.section == toc_inf->toc)
7123 unsigned long skip = toc_inf->skip[eh->elf.root.u.def.value >> 3];
7124 if (skip != (unsigned long) -1)
7125 eh->elf.root.u.def.value -= skip;
7126 else
7128 (*_bfd_error_handler)
7129 (_("%s defined in removed toc entry"), eh->elf.root.root.string);
7130 eh->elf.root.u.def.section = &bfd_abs_section;
7131 eh->elf.root.u.def.value = 0;
7133 eh->adjust_done = 1;
7135 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7136 toc_inf->global_toc_syms = TRUE;
7138 return TRUE;
7141 /* Examine all relocs referencing .toc sections in order to remove
7142 unused .toc entries. */
7144 bfd_boolean
7145 ppc64_elf_edit_toc (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
7147 bfd *ibfd;
7148 struct adjust_toc_info toc_inf;
7150 toc_inf.global_toc_syms = TRUE;
7151 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7153 asection *toc, *sec;
7154 Elf_Internal_Shdr *symtab_hdr;
7155 Elf_Internal_Sym *local_syms;
7156 struct elf_link_hash_entry **sym_hashes;
7157 Elf_Internal_Rela *relstart, *rel;
7158 unsigned long *skip, *drop;
7159 unsigned char *used;
7160 unsigned char *keep, last, some_unused;
7162 toc = bfd_get_section_by_name (ibfd, ".toc");
7163 if (toc == NULL
7164 || toc->size == 0
7165 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7166 || elf_discarded_section (toc))
7167 continue;
7169 local_syms = NULL;
7170 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7171 sym_hashes = elf_sym_hashes (ibfd);
7173 /* Look at sections dropped from the final link. */
7174 skip = NULL;
7175 relstart = NULL;
7176 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7178 if (sec->reloc_count == 0
7179 || !elf_discarded_section (sec)
7180 || get_opd_info (sec)
7181 || (sec->flags & SEC_ALLOC) == 0
7182 || (sec->flags & SEC_DEBUGGING) != 0)
7183 continue;
7185 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7186 if (relstart == NULL)
7187 goto error_ret;
7189 /* Run through the relocs to see which toc entries might be
7190 unused. */
7191 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7193 enum elf_ppc64_reloc_type r_type;
7194 unsigned long r_symndx;
7195 asection *sym_sec;
7196 struct elf_link_hash_entry *h;
7197 Elf_Internal_Sym *sym;
7198 bfd_vma val;
7200 r_type = ELF64_R_TYPE (rel->r_info);
7201 switch (r_type)
7203 default:
7204 continue;
7206 case R_PPC64_TOC16:
7207 case R_PPC64_TOC16_LO:
7208 case R_PPC64_TOC16_HI:
7209 case R_PPC64_TOC16_HA:
7210 case R_PPC64_TOC16_DS:
7211 case R_PPC64_TOC16_LO_DS:
7212 break;
7215 r_symndx = ELF64_R_SYM (rel->r_info);
7216 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7217 r_symndx, ibfd))
7218 goto error_ret;
7220 if (sym_sec != toc)
7221 continue;
7223 if (h != NULL)
7224 val = h->root.u.def.value;
7225 else
7226 val = sym->st_value;
7227 val += rel->r_addend;
7229 if (val >= toc->size)
7230 continue;
7232 /* Anything in the toc ought to be aligned to 8 bytes.
7233 If not, don't mark as unused. */
7234 if (val & 7)
7235 continue;
7237 if (skip == NULL)
7239 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 7) / 8);
7240 if (skip == NULL)
7241 goto error_ret;
7244 skip[val >> 3] = 1;
7247 if (elf_section_data (sec)->relocs != relstart)
7248 free (relstart);
7251 if (skip == NULL)
7252 continue;
7254 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
7255 if (used == NULL)
7257 error_ret:
7258 if (local_syms != NULL
7259 && symtab_hdr->contents != (unsigned char *) local_syms)
7260 free (local_syms);
7261 if (sec != NULL
7262 && relstart != NULL
7263 && elf_section_data (sec)->relocs != relstart)
7264 free (relstart);
7265 if (skip != NULL)
7266 free (skip);
7267 return FALSE;
7270 /* Now check all kept sections that might reference the toc.
7271 Check the toc itself last. */
7272 for (sec = (ibfd->sections == toc && toc->next ? toc->next
7273 : ibfd->sections);
7274 sec != NULL;
7275 sec = (sec == toc ? NULL
7276 : sec->next == NULL ? toc
7277 : sec->next == toc && toc->next ? toc->next
7278 : sec->next))
7280 int repeat;
7282 if (sec->reloc_count == 0
7283 || elf_discarded_section (sec)
7284 || get_opd_info (sec)
7285 || (sec->flags & SEC_ALLOC) == 0
7286 || (sec->flags & SEC_DEBUGGING) != 0)
7287 continue;
7289 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, TRUE);
7290 if (relstart == NULL)
7291 goto error_ret;
7293 /* Mark toc entries referenced as used. */
7294 repeat = 0;
7296 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7298 enum elf_ppc64_reloc_type r_type;
7299 unsigned long r_symndx;
7300 asection *sym_sec;
7301 struct elf_link_hash_entry *h;
7302 Elf_Internal_Sym *sym;
7303 bfd_vma val;
7305 r_type = ELF64_R_TYPE (rel->r_info);
7306 switch (r_type)
7308 case R_PPC64_TOC16:
7309 case R_PPC64_TOC16_LO:
7310 case R_PPC64_TOC16_HI:
7311 case R_PPC64_TOC16_HA:
7312 case R_PPC64_TOC16_DS:
7313 case R_PPC64_TOC16_LO_DS:
7314 /* In case we're taking addresses of toc entries. */
7315 case R_PPC64_ADDR64:
7316 break;
7318 default:
7319 continue;
7322 r_symndx = ELF64_R_SYM (rel->r_info);
7323 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7324 r_symndx, ibfd))
7326 free (used);
7327 goto error_ret;
7330 if (sym_sec != toc)
7331 continue;
7333 if (h != NULL)
7334 val = h->root.u.def.value;
7335 else
7336 val = sym->st_value;
7337 val += rel->r_addend;
7339 if (val >= toc->size)
7340 continue;
7342 /* For the toc section, we only mark as used if
7343 this entry itself isn't unused. */
7344 if (sec == toc
7345 && !used[val >> 3]
7346 && (used[rel->r_offset >> 3]
7347 || !skip[rel->r_offset >> 3]))
7348 /* Do all the relocs again, to catch reference
7349 chains. */
7350 repeat = 1;
7352 used[val >> 3] = 1;
7354 while (repeat);
7357 /* Merge the used and skip arrays. Assume that TOC
7358 doublewords not appearing as either used or unused belong
7359 to to an entry more than one doubleword in size. */
7360 for (drop = skip, keep = used, last = 0, some_unused = 0;
7361 drop < skip + (toc->size + 7) / 8;
7362 ++drop, ++keep)
7364 if (*keep)
7366 *drop = 0;
7367 last = 0;
7369 else if (*drop)
7371 some_unused = 1;
7372 last = 1;
7374 else
7375 *drop = last;
7378 free (used);
7380 if (some_unused)
7382 bfd_byte *contents, *src;
7383 unsigned long off;
7385 /* Shuffle the toc contents, and at the same time convert the
7386 skip array from booleans into offsets. */
7387 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
7388 goto error_ret;
7390 elf_section_data (toc)->this_hdr.contents = contents;
7392 for (src = contents, off = 0, drop = skip;
7393 src < contents + toc->size;
7394 src += 8, ++drop)
7396 if (*drop)
7398 *drop = (unsigned long) -1;
7399 off += 8;
7401 else if (off != 0)
7403 *drop = off;
7404 memcpy (src - off, src, 8);
7407 toc->rawsize = toc->size;
7408 toc->size = src - contents - off;
7410 if (toc->reloc_count != 0)
7412 Elf_Internal_Rela *wrel;
7413 bfd_size_type sz;
7415 /* Read toc relocs. */
7416 relstart = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
7417 TRUE);
7418 if (relstart == NULL)
7419 goto error_ret;
7421 /* Remove unused toc relocs, and adjust those we keep. */
7422 wrel = relstart;
7423 for (rel = relstart; rel < relstart + toc->reloc_count; ++rel)
7424 if (skip[rel->r_offset >> 3] != (unsigned long) -1)
7426 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
7427 wrel->r_info = rel->r_info;
7428 wrel->r_addend = rel->r_addend;
7429 ++wrel;
7431 else if (!dec_dynrel_count (rel->r_info, toc, info,
7432 &local_syms, NULL, NULL))
7433 goto error_ret;
7435 toc->reloc_count = wrel - relstart;
7436 sz = elf_section_data (toc)->rel_hdr.sh_entsize;
7437 elf_section_data (toc)->rel_hdr.sh_size = toc->reloc_count * sz;
7438 BFD_ASSERT (elf_section_data (toc)->rel_hdr2 == NULL);
7441 /* Adjust addends for relocs against the toc section sym. */
7442 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7444 if (sec->reloc_count == 0
7445 || elf_discarded_section (sec))
7446 continue;
7448 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7449 TRUE);
7450 if (relstart == NULL)
7451 goto error_ret;
7453 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7455 enum elf_ppc64_reloc_type r_type;
7456 unsigned long r_symndx;
7457 asection *sym_sec;
7458 struct elf_link_hash_entry *h;
7459 Elf_Internal_Sym *sym;
7461 r_type = ELF64_R_TYPE (rel->r_info);
7462 switch (r_type)
7464 default:
7465 continue;
7467 case R_PPC64_TOC16:
7468 case R_PPC64_TOC16_LO:
7469 case R_PPC64_TOC16_HI:
7470 case R_PPC64_TOC16_HA:
7471 case R_PPC64_TOC16_DS:
7472 case R_PPC64_TOC16_LO_DS:
7473 case R_PPC64_ADDR64:
7474 break;
7477 r_symndx = ELF64_R_SYM (rel->r_info);
7478 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7479 r_symndx, ibfd))
7480 goto error_ret;
7482 if (sym_sec != toc || h != NULL || sym->st_value != 0)
7483 continue;
7485 rel->r_addend -= skip[rel->r_addend >> 3];
7489 /* We shouldn't have local or global symbols defined in the TOC,
7490 but handle them anyway. */
7491 if (local_syms != NULL)
7493 Elf_Internal_Sym *sym;
7495 for (sym = local_syms;
7496 sym < local_syms + symtab_hdr->sh_info;
7497 ++sym)
7498 if (sym->st_shndx != SHN_UNDEF
7499 && (sym->st_shndx < SHN_LORESERVE
7500 || sym->st_shndx > SHN_HIRESERVE)
7501 && sym->st_value != 0
7502 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
7504 if (skip[sym->st_value >> 3] != (unsigned long) -1)
7505 sym->st_value -= skip[sym->st_value >> 3];
7506 else
7508 (*_bfd_error_handler)
7509 (_("%s defined in removed toc entry"),
7510 bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7511 NULL));
7512 sym->st_value = 0;
7513 sym->st_shndx = SHN_ABS;
7515 symtab_hdr->contents = (unsigned char *) local_syms;
7519 /* Finally, adjust any global syms defined in the toc. */
7520 if (toc_inf.global_toc_syms)
7522 toc_inf.toc = toc;
7523 toc_inf.skip = skip;
7524 toc_inf.global_toc_syms = FALSE;
7525 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
7526 &toc_inf);
7530 if (local_syms != NULL
7531 && symtab_hdr->contents != (unsigned char *) local_syms)
7533 if (!info->keep_memory)
7534 free (local_syms);
7535 else
7536 symtab_hdr->contents = (unsigned char *) local_syms;
7538 free (skip);
7541 return TRUE;
7544 /* Allocate space in .plt, .got and associated reloc sections for
7545 dynamic relocs. */
7547 static bfd_boolean
7548 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7550 struct bfd_link_info *info;
7551 struct ppc_link_hash_table *htab;
7552 asection *s;
7553 struct ppc_link_hash_entry *eh;
7554 struct ppc_dyn_relocs *p;
7555 struct got_entry *gent;
7557 if (h->root.type == bfd_link_hash_indirect)
7558 return TRUE;
7560 if (h->root.type == bfd_link_hash_warning)
7561 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7563 info = (struct bfd_link_info *) inf;
7564 htab = ppc_hash_table (info);
7566 if (htab->elf.dynamic_sections_created
7567 && h->dynindx != -1
7568 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
7570 struct plt_entry *pent;
7571 bfd_boolean doneone = FALSE;
7572 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7573 if (pent->plt.refcount > 0)
7575 /* If this is the first .plt entry, make room for the special
7576 first entry. */
7577 s = htab->plt;
7578 if (s->size == 0)
7579 s->size += PLT_INITIAL_ENTRY_SIZE;
7581 pent->plt.offset = s->size;
7583 /* Make room for this entry. */
7584 s->size += PLT_ENTRY_SIZE;
7586 /* Make room for the .glink code. */
7587 s = htab->glink;
7588 if (s->size == 0)
7589 s->size += GLINK_CALL_STUB_SIZE;
7590 /* We need bigger stubs past index 32767. */
7591 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
7592 s->size += 4;
7593 s->size += 2*4;
7595 /* We also need to make an entry in the .rela.plt section. */
7596 s = htab->relplt;
7597 s->size += sizeof (Elf64_External_Rela);
7598 doneone = TRUE;
7600 else
7601 pent->plt.offset = (bfd_vma) -1;
7602 if (!doneone)
7604 h->plt.plist = NULL;
7605 h->needs_plt = 0;
7608 else
7610 h->plt.plist = NULL;
7611 h->needs_plt = 0;
7614 eh = (struct ppc_link_hash_entry *) h;
7615 /* Run through the TLS GD got entries first if we're changing them
7616 to TPREL. */
7617 if ((eh->tls_mask & TLS_TPRELGD) != 0)
7618 for (gent = h->got.glist; gent != NULL; gent = gent->next)
7619 if (gent->got.refcount > 0
7620 && (gent->tls_type & TLS_GD) != 0)
7622 /* This was a GD entry that has been converted to TPREL. If
7623 there happens to be a TPREL entry we can use that one. */
7624 struct got_entry *ent;
7625 for (ent = h->got.glist; ent != NULL; ent = ent->next)
7626 if (ent->got.refcount > 0
7627 && (ent->tls_type & TLS_TPREL) != 0
7628 && ent->addend == gent->addend
7629 && ent->owner == gent->owner)
7631 gent->got.refcount = 0;
7632 break;
7635 /* If not, then we'll be using our own TPREL entry. */
7636 if (gent->got.refcount != 0)
7637 gent->tls_type = TLS_TLS | TLS_TPREL;
7640 for (gent = h->got.glist; gent != NULL; gent = gent->next)
7641 if (gent->got.refcount > 0)
7643 bfd_boolean dyn;
7645 /* Make sure this symbol is output as a dynamic symbol.
7646 Undefined weak syms won't yet be marked as dynamic,
7647 nor will all TLS symbols. */
7648 if (h->dynindx == -1
7649 && !h->forced_local)
7651 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7652 return FALSE;
7655 if ((gent->tls_type & TLS_LD) != 0
7656 && !h->def_dynamic)
7658 gent->got.offset = ppc64_tlsld_got (gent->owner)->offset;
7659 continue;
7662 s = ppc64_elf_tdata (gent->owner)->got;
7663 gent->got.offset = s->size;
7664 s->size
7665 += (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)) ? 16 : 8;
7666 dyn = htab->elf.dynamic_sections_created;
7667 if ((info->shared
7668 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
7669 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7670 || h->root.type != bfd_link_hash_undefweak))
7671 ppc64_elf_tdata (gent->owner)->relgot->size
7672 += (gent->tls_type & eh->tls_mask & TLS_GD
7673 ? 2 * sizeof (Elf64_External_Rela)
7674 : sizeof (Elf64_External_Rela));
7676 else
7677 gent->got.offset = (bfd_vma) -1;
7679 if (eh->dyn_relocs == NULL)
7680 return TRUE;
7682 /* In the shared -Bsymbolic case, discard space allocated for
7683 dynamic pc-relative relocs against symbols which turn out to be
7684 defined in regular objects. For the normal shared case, discard
7685 space for relocs that have become local due to symbol visibility
7686 changes. */
7688 if (info->shared)
7690 /* Relocs that use pc_count are those that appear on a call insn,
7691 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
7692 generated via assembly. We want calls to protected symbols to
7693 resolve directly to the function rather than going via the plt.
7694 If people want function pointer comparisons to work as expected
7695 then they should avoid writing weird assembly. */
7696 if (SYMBOL_CALLS_LOCAL (info, h))
7698 struct ppc_dyn_relocs **pp;
7700 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
7702 p->count -= p->pc_count;
7703 p->pc_count = 0;
7704 if (p->count == 0)
7705 *pp = p->next;
7706 else
7707 pp = &p->next;
7711 /* Also discard relocs on undefined weak syms with non-default
7712 visibility. */
7713 if (eh->dyn_relocs != NULL
7714 && h->root.type == bfd_link_hash_undefweak)
7716 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
7717 eh->dyn_relocs = NULL;
7719 /* Make sure this symbol is output as a dynamic symbol.
7720 Undefined weak syms won't yet be marked as dynamic. */
7721 else if (h->dynindx == -1
7722 && !h->forced_local)
7724 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7725 return FALSE;
7729 else if (ELIMINATE_COPY_RELOCS)
7731 /* For the non-shared case, discard space for relocs against
7732 symbols which turn out to need copy relocs or are not
7733 dynamic. */
7735 if (!h->non_got_ref
7736 && h->def_dynamic
7737 && !h->def_regular)
7739 /* Make sure this symbol is output as a dynamic symbol.
7740 Undefined weak syms won't yet be marked as dynamic. */
7741 if (h->dynindx == -1
7742 && !h->forced_local)
7744 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7745 return FALSE;
7748 /* If that succeeded, we know we'll be keeping all the
7749 relocs. */
7750 if (h->dynindx != -1)
7751 goto keep;
7754 eh->dyn_relocs = NULL;
7756 keep: ;
7759 /* Finally, allocate space. */
7760 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7762 asection *sreloc = elf_section_data (p->sec)->sreloc;
7763 sreloc->size += p->count * sizeof (Elf64_External_Rela);
7766 return TRUE;
7769 /* Find any dynamic relocs that apply to read-only sections. */
7771 static bfd_boolean
7772 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7774 struct ppc_link_hash_entry *eh;
7775 struct ppc_dyn_relocs *p;
7777 if (h->root.type == bfd_link_hash_warning)
7778 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7780 eh = (struct ppc_link_hash_entry *) h;
7781 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7783 asection *s = p->sec->output_section;
7785 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7787 struct bfd_link_info *info = inf;
7789 info->flags |= DF_TEXTREL;
7791 /* Not an error, just cut short the traversal. */
7792 return FALSE;
7795 return TRUE;
7798 /* Set the sizes of the dynamic sections. */
7800 static bfd_boolean
7801 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
7802 struct bfd_link_info *info)
7804 struct ppc_link_hash_table *htab;
7805 bfd *dynobj;
7806 asection *s;
7807 bfd_boolean relocs;
7808 bfd *ibfd;
7810 htab = ppc_hash_table (info);
7811 dynobj = htab->elf.dynobj;
7812 if (dynobj == NULL)
7813 abort ();
7815 if (htab->elf.dynamic_sections_created)
7817 /* Set the contents of the .interp section to the interpreter. */
7818 if (info->executable)
7820 s = bfd_get_section_by_name (dynobj, ".interp");
7821 if (s == NULL)
7822 abort ();
7823 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
7824 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
7828 /* Set up .got offsets for local syms, and space for local dynamic
7829 relocs. */
7830 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7832 struct got_entry **lgot_ents;
7833 struct got_entry **end_lgot_ents;
7834 char *lgot_masks;
7835 bfd_size_type locsymcount;
7836 Elf_Internal_Shdr *symtab_hdr;
7837 asection *srel;
7839 if (!is_ppc64_elf_target (ibfd->xvec))
7840 continue;
7842 if (ppc64_tlsld_got (ibfd)->refcount > 0)
7844 s = ppc64_elf_tdata (ibfd)->got;
7845 ppc64_tlsld_got (ibfd)->offset = s->size;
7846 s->size += 16;
7847 if (info->shared)
7849 srel = ppc64_elf_tdata (ibfd)->relgot;
7850 srel->size += sizeof (Elf64_External_Rela);
7853 else
7854 ppc64_tlsld_got (ibfd)->offset = (bfd_vma) -1;
7856 for (s = ibfd->sections; s != NULL; s = s->next)
7858 struct ppc_dyn_relocs *p;
7860 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
7862 if (!bfd_is_abs_section (p->sec)
7863 && bfd_is_abs_section (p->sec->output_section))
7865 /* Input section has been discarded, either because
7866 it is a copy of a linkonce section or due to
7867 linker script /DISCARD/, so we'll be discarding
7868 the relocs too. */
7870 else if (p->count != 0)
7872 srel = elf_section_data (p->sec)->sreloc;
7873 srel->size += p->count * sizeof (Elf64_External_Rela);
7874 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
7875 info->flags |= DF_TEXTREL;
7880 lgot_ents = elf_local_got_ents (ibfd);
7881 if (!lgot_ents)
7882 continue;
7884 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7885 locsymcount = symtab_hdr->sh_info;
7886 end_lgot_ents = lgot_ents + locsymcount;
7887 lgot_masks = (char *) end_lgot_ents;
7888 s = ppc64_elf_tdata (ibfd)->got;
7889 srel = ppc64_elf_tdata (ibfd)->relgot;
7890 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
7892 struct got_entry *ent;
7894 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
7895 if (ent->got.refcount > 0)
7897 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
7899 if (ppc64_tlsld_got (ibfd)->offset == (bfd_vma) -1)
7901 ppc64_tlsld_got (ibfd)->offset = s->size;
7902 s->size += 16;
7903 if (info->shared)
7904 srel->size += sizeof (Elf64_External_Rela);
7906 ent->got.offset = ppc64_tlsld_got (ibfd)->offset;
7908 else
7910 ent->got.offset = s->size;
7911 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
7913 s->size += 16;
7914 if (info->shared)
7915 srel->size += 2 * sizeof (Elf64_External_Rela);
7917 else
7919 s->size += 8;
7920 if (info->shared)
7921 srel->size += sizeof (Elf64_External_Rela);
7925 else
7926 ent->got.offset = (bfd_vma) -1;
7930 /* Allocate global sym .plt and .got entries, and space for global
7931 sym dynamic relocs. */
7932 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
7934 /* We now have determined the sizes of the various dynamic sections.
7935 Allocate memory for them. */
7936 relocs = FALSE;
7937 for (s = dynobj->sections; s != NULL; s = s->next)
7939 if ((s->flags & SEC_LINKER_CREATED) == 0)
7940 continue;
7942 if (s == htab->brlt || s == htab->relbrlt)
7943 /* These haven't been allocated yet; don't strip. */
7944 continue;
7945 else if (s == htab->got
7946 || s == htab->plt
7947 || s == htab->glink
7948 || s == htab->dynbss)
7950 /* Strip this section if we don't need it; see the
7951 comment below. */
7953 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
7955 if (s->size != 0)
7957 if (s != htab->relplt)
7958 relocs = TRUE;
7960 /* We use the reloc_count field as a counter if we need
7961 to copy relocs into the output file. */
7962 s->reloc_count = 0;
7965 else
7967 /* It's not one of our sections, so don't allocate space. */
7968 continue;
7971 if (s->size == 0)
7973 /* If we don't need this section, strip it from the
7974 output file. This is mostly to handle .rela.bss and
7975 .rela.plt. We must create both sections in
7976 create_dynamic_sections, because they must be created
7977 before the linker maps input sections to output
7978 sections. The linker does that before
7979 adjust_dynamic_symbol is called, and it is that
7980 function which decides whether anything needs to go
7981 into these sections. */
7982 s->flags |= SEC_EXCLUDE;
7983 continue;
7986 if ((s->flags & SEC_HAS_CONTENTS) == 0)
7987 continue;
7989 /* Allocate memory for the section contents. We use bfd_zalloc
7990 here in case unused entries are not reclaimed before the
7991 section's contents are written out. This should not happen,
7992 but this way if it does we get a R_PPC64_NONE reloc in .rela
7993 sections instead of garbage.
7994 We also rely on the section contents being zero when writing
7995 the GOT. */
7996 s->contents = bfd_zalloc (dynobj, s->size);
7997 if (s->contents == NULL)
7998 return FALSE;
8001 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8003 if (!is_ppc64_elf_target (ibfd->xvec))
8004 continue;
8006 s = ppc64_elf_tdata (ibfd)->got;
8007 if (s != NULL && s != htab->got)
8009 if (s->size == 0)
8010 s->flags |= SEC_EXCLUDE;
8011 else
8013 s->contents = bfd_zalloc (ibfd, s->size);
8014 if (s->contents == NULL)
8015 return FALSE;
8018 s = ppc64_elf_tdata (ibfd)->relgot;
8019 if (s != NULL)
8021 if (s->size == 0)
8022 s->flags |= SEC_EXCLUDE;
8023 else
8025 s->contents = bfd_zalloc (ibfd, s->size);
8026 if (s->contents == NULL)
8027 return FALSE;
8028 relocs = TRUE;
8029 s->reloc_count = 0;
8034 if (htab->elf.dynamic_sections_created)
8036 /* Add some entries to the .dynamic section. We fill in the
8037 values later, in ppc64_elf_finish_dynamic_sections, but we
8038 must add the entries now so that we get the correct size for
8039 the .dynamic section. The DT_DEBUG entry is filled in by the
8040 dynamic linker and used by the debugger. */
8041 #define add_dynamic_entry(TAG, VAL) \
8042 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
8044 if (info->executable)
8046 if (!add_dynamic_entry (DT_DEBUG, 0))
8047 return FALSE;
8050 if (htab->plt != NULL && htab->plt->size != 0)
8052 if (!add_dynamic_entry (DT_PLTGOT, 0)
8053 || !add_dynamic_entry (DT_PLTRELSZ, 0)
8054 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
8055 || !add_dynamic_entry (DT_JMPREL, 0)
8056 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
8057 return FALSE;
8060 if (NO_OPD_RELOCS)
8062 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
8063 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
8064 return FALSE;
8067 if (relocs)
8069 if (!add_dynamic_entry (DT_RELA, 0)
8070 || !add_dynamic_entry (DT_RELASZ, 0)
8071 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
8072 return FALSE;
8074 /* If any dynamic relocs apply to a read-only section,
8075 then we need a DT_TEXTREL entry. */
8076 if ((info->flags & DF_TEXTREL) == 0)
8077 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
8079 if ((info->flags & DF_TEXTREL) != 0)
8081 if (!add_dynamic_entry (DT_TEXTREL, 0))
8082 return FALSE;
8086 #undef add_dynamic_entry
8088 return TRUE;
8091 /* Determine the type of stub needed, if any, for a call. */
8093 static inline enum ppc_stub_type
8094 ppc_type_of_stub (asection *input_sec,
8095 const Elf_Internal_Rela *rel,
8096 struct ppc_link_hash_entry **hash,
8097 bfd_vma destination)
8099 struct ppc_link_hash_entry *h = *hash;
8100 bfd_vma location;
8101 bfd_vma branch_offset;
8102 bfd_vma max_branch_offset;
8103 enum elf_ppc64_reloc_type r_type;
8105 if (h != NULL)
8107 struct ppc_link_hash_entry *fdh = h;
8108 if (fdh->oh != NULL
8109 && fdh->oh->is_func_descriptor)
8110 fdh = fdh->oh;
8112 if (fdh->elf.dynindx != -1)
8114 struct plt_entry *ent;
8116 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
8117 if (ent->addend == rel->r_addend
8118 && ent->plt.offset != (bfd_vma) -1)
8120 *hash = fdh;
8121 return ppc_stub_plt_call;
8125 /* Here, we know we don't have a plt entry. If we don't have a
8126 either a defined function descriptor or a defined entry symbol
8127 in a regular object file, then it is pointless trying to make
8128 any other type of stub. */
8129 if (!((fdh->elf.root.type == bfd_link_hash_defined
8130 || fdh->elf.root.type == bfd_link_hash_defweak)
8131 && fdh->elf.root.u.def.section->output_section != NULL)
8132 && !((h->elf.root.type == bfd_link_hash_defined
8133 || h->elf.root.type == bfd_link_hash_defweak)
8134 && h->elf.root.u.def.section->output_section != NULL))
8135 return ppc_stub_none;
8138 /* Determine where the call point is. */
8139 location = (input_sec->output_offset
8140 + input_sec->output_section->vma
8141 + rel->r_offset);
8143 branch_offset = destination - location;
8144 r_type = ELF64_R_TYPE (rel->r_info);
8146 /* Determine if a long branch stub is needed. */
8147 max_branch_offset = 1 << 25;
8148 if (r_type != R_PPC64_REL24)
8149 max_branch_offset = 1 << 15;
8151 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
8152 /* We need a stub. Figure out whether a long_branch or plt_branch
8153 is needed later. */
8154 return ppc_stub_long_branch;
8156 return ppc_stub_none;
8159 /* Build a .plt call stub. */
8161 static inline bfd_byte *
8162 build_plt_stub (bfd *obfd, bfd_byte *p, int offset)
8164 #define PPC_LO(v) ((v) & 0xffff)
8165 #define PPC_HI(v) (((v) >> 16) & 0xffff)
8166 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
8168 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
8169 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
8170 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
8171 if (PPC_HA (offset + 8) != PPC_HA (offset))
8172 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
8173 offset += 8;
8174 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p), p += 4;
8175 if (PPC_HA (offset + 8) != PPC_HA (offset))
8176 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
8177 offset += 8;
8178 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
8179 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
8180 bfd_put_32 (obfd, BCTR, p), p += 4;
8181 return p;
8184 static bfd_boolean
8185 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
8187 struct ppc_stub_hash_entry *stub_entry;
8188 struct ppc_branch_hash_entry *br_entry;
8189 struct bfd_link_info *info;
8190 struct ppc_link_hash_table *htab;
8191 bfd_byte *loc;
8192 bfd_byte *p;
8193 unsigned int indx;
8194 struct plt_entry *ent;
8195 bfd_vma dest, off;
8196 int size;
8198 /* Massage our args to the form they really have. */
8199 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
8200 info = in_arg;
8202 htab = ppc_hash_table (info);
8204 /* Make a note of the offset within the stubs for this entry. */
8205 stub_entry->stub_offset = stub_entry->stub_sec->size;
8206 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
8208 htab->stub_count[stub_entry->stub_type - 1] += 1;
8209 switch (stub_entry->stub_type)
8211 case ppc_stub_long_branch:
8212 case ppc_stub_long_branch_r2off:
8213 /* Branches are relative. This is where we are going to. */
8214 off = dest = (stub_entry->target_value
8215 + stub_entry->target_section->output_offset
8216 + stub_entry->target_section->output_section->vma);
8218 /* And this is where we are coming from. */
8219 off -= (stub_entry->stub_offset
8220 + stub_entry->stub_sec->output_offset
8221 + stub_entry->stub_sec->output_section->vma);
8223 if (stub_entry->stub_type != ppc_stub_long_branch_r2off)
8224 size = 4;
8225 else
8227 bfd_vma r2off;
8229 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8230 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8231 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
8232 loc += 4;
8233 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
8234 loc += 4;
8235 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
8236 loc += 4;
8237 off -= 12;
8238 size = 16;
8240 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
8242 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8244 (*_bfd_error_handler) (_("long branch stub `%s' offset overflow"),
8245 stub_entry->root.string);
8246 htab->stub_error = TRUE;
8247 return FALSE;
8250 if (info->emitrelocations)
8252 Elf_Internal_Rela *relocs, *r;
8253 struct bfd_elf_section_data *elfsec_data;
8255 elfsec_data = elf_section_data (stub_entry->stub_sec);
8256 relocs = elfsec_data->relocs;
8257 if (relocs == NULL)
8259 bfd_size_type relsize;
8260 relsize = stub_entry->stub_sec->reloc_count * sizeof (*relocs);
8261 relocs = bfd_alloc (htab->stub_bfd, relsize);
8262 if (relocs == NULL)
8263 return FALSE;
8264 elfsec_data->relocs = relocs;
8265 elfsec_data->rel_hdr.sh_size = relsize;
8266 elfsec_data->rel_hdr.sh_entsize = 24;
8267 stub_entry->stub_sec->reloc_count = 0;
8269 r = relocs + stub_entry->stub_sec->reloc_count;
8270 stub_entry->stub_sec->reloc_count += 1;
8271 r->r_offset = loc - stub_entry->stub_sec->contents;
8272 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
8273 r->r_addend = dest;
8274 if (stub_entry->h != NULL)
8276 struct elf_link_hash_entry **hashes;
8277 unsigned long symndx;
8278 struct ppc_link_hash_entry *h;
8280 hashes = elf_sym_hashes (htab->stub_bfd);
8281 if (hashes == NULL)
8283 bfd_size_type hsize;
8285 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
8286 hashes = bfd_zalloc (htab->stub_bfd, hsize);
8287 if (hashes == NULL)
8288 return FALSE;
8289 elf_sym_hashes (htab->stub_bfd) = hashes;
8290 htab->stub_globals = 1;
8292 symndx = htab->stub_globals++;
8293 h = stub_entry->h;
8294 hashes[symndx] = &h->elf;
8295 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
8296 if (h->oh != NULL && h->oh->is_func)
8297 h = h->oh;
8298 if (h->elf.root.u.def.section != stub_entry->target_section)
8299 /* H is an opd symbol. The addend must be zero. */
8300 r->r_addend = 0;
8301 else
8303 off = (h->elf.root.u.def.value
8304 + h->elf.root.u.def.section->output_offset
8305 + h->elf.root.u.def.section->output_section->vma);
8306 r->r_addend -= off;
8310 break;
8312 case ppc_stub_plt_branch:
8313 case ppc_stub_plt_branch_r2off:
8314 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8315 stub_entry->root.string + 9,
8316 FALSE, FALSE);
8317 if (br_entry == NULL)
8319 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
8320 stub_entry->root.string);
8321 htab->stub_error = TRUE;
8322 return FALSE;
8325 off = (stub_entry->target_value
8326 + stub_entry->target_section->output_offset
8327 + stub_entry->target_section->output_section->vma);
8329 bfd_put_64 (htab->brlt->owner, off,
8330 htab->brlt->contents + br_entry->offset);
8332 if (htab->relbrlt != NULL)
8334 /* Create a reloc for the branch lookup table entry. */
8335 Elf_Internal_Rela rela;
8336 bfd_byte *rl;
8338 rela.r_offset = (br_entry->offset
8339 + htab->brlt->output_offset
8340 + htab->brlt->output_section->vma);
8341 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
8342 rela.r_addend = off;
8344 rl = htab->relbrlt->contents;
8345 rl += htab->relbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
8346 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
8349 off = (br_entry->offset
8350 + htab->brlt->output_offset
8351 + htab->brlt->output_section->vma
8352 - elf_gp (htab->brlt->output_section->owner)
8353 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8355 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
8357 (*_bfd_error_handler)
8358 (_("linkage table error against `%s'"),
8359 stub_entry->root.string);
8360 bfd_set_error (bfd_error_bad_value);
8361 htab->stub_error = TRUE;
8362 return FALSE;
8365 indx = off;
8366 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
8368 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
8369 loc += 4;
8370 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
8371 size = 16;
8373 else
8375 bfd_vma r2off;
8377 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8378 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8379 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
8380 loc += 4;
8381 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
8382 loc += 4;
8383 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
8384 loc += 4;
8385 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
8386 loc += 4;
8387 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
8388 size = 28;
8390 loc += 4;
8391 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
8392 loc += 4;
8393 bfd_put_32 (htab->stub_bfd, BCTR, loc);
8394 break;
8396 case ppc_stub_plt_call:
8397 /* Do the best we can for shared libraries built without
8398 exporting ".foo" for each "foo". This can happen when symbol
8399 versioning scripts strip all bar a subset of symbols. */
8400 if (stub_entry->h->oh != NULL
8401 && stub_entry->h->oh->elf.root.type != bfd_link_hash_defined
8402 && stub_entry->h->oh->elf.root.type != bfd_link_hash_defweak)
8404 /* Point the symbol at the stub. There may be multiple stubs,
8405 we don't really care; The main thing is to make this sym
8406 defined somewhere. Maybe defining the symbol in the stub
8407 section is a silly idea. If we didn't do this, htab->top_id
8408 could disappear. */
8409 stub_entry->h->oh->elf.root.type = bfd_link_hash_defined;
8410 stub_entry->h->oh->elf.root.u.def.section = stub_entry->stub_sec;
8411 stub_entry->h->oh->elf.root.u.def.value = stub_entry->stub_offset;
8414 /* Now build the stub. */
8415 off = (bfd_vma) -1;
8416 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8417 if (ent->addend == stub_entry->addend)
8419 off = ent->plt.offset;
8420 break;
8422 if (off >= (bfd_vma) -2)
8423 abort ();
8425 off &= ~ (bfd_vma) 1;
8426 off += (htab->plt->output_offset
8427 + htab->plt->output_section->vma
8428 - elf_gp (htab->plt->output_section->owner)
8429 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8431 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
8433 (*_bfd_error_handler)
8434 (_("linkage table error against `%s'"),
8435 stub_entry->h->elf.root.root.string);
8436 bfd_set_error (bfd_error_bad_value);
8437 htab->stub_error = TRUE;
8438 return FALSE;
8441 p = build_plt_stub (htab->stub_bfd, loc, off);
8442 size = p - loc;
8443 break;
8445 default:
8446 BFD_FAIL ();
8447 return FALSE;
8450 stub_entry->stub_sec->size += size;
8452 if (htab->emit_stub_syms)
8454 struct elf_link_hash_entry *h;
8455 size_t len1, len2;
8456 char *name;
8457 const char *const stub_str[] = { "long_branch",
8458 "long_branch_r2off",
8459 "plt_branch",
8460 "plt_branch_r2off",
8461 "plt_call" };
8463 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
8464 len2 = strlen (stub_entry->root.string);
8465 name = bfd_malloc (len1 + len2 + 2);
8466 if (name == NULL)
8467 return FALSE;
8468 memcpy (name, stub_entry->root.string, 9);
8469 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
8470 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
8471 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
8472 if (h == NULL)
8473 return FALSE;
8474 if (h->root.type == bfd_link_hash_new)
8476 h->root.type = bfd_link_hash_defined;
8477 h->root.u.def.section = stub_entry->stub_sec;
8478 h->root.u.def.value = stub_entry->stub_offset;
8479 h->ref_regular = 1;
8480 h->def_regular = 1;
8481 h->ref_regular_nonweak = 1;
8482 h->forced_local = 1;
8483 h->non_elf = 0;
8487 return TRUE;
8490 /* As above, but don't actually build the stub. Just bump offset so
8491 we know stub section sizes, and select plt_branch stubs where
8492 long_branch stubs won't do. */
8494 static bfd_boolean
8495 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
8497 struct ppc_stub_hash_entry *stub_entry;
8498 struct bfd_link_info *info;
8499 struct ppc_link_hash_table *htab;
8500 bfd_vma off;
8501 int size;
8503 /* Massage our args to the form they really have. */
8504 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
8505 info = in_arg;
8507 htab = ppc_hash_table (info);
8509 if (stub_entry->stub_type == ppc_stub_plt_call)
8511 struct plt_entry *ent;
8512 off = (bfd_vma) -1;
8513 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8514 if (ent->addend == stub_entry->addend)
8516 off = ent->plt.offset & ~(bfd_vma) 1;
8517 break;
8519 if (off >= (bfd_vma) -2)
8520 abort ();
8521 off += (htab->plt->output_offset
8522 + htab->plt->output_section->vma
8523 - elf_gp (htab->plt->output_section->owner)
8524 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8526 size = PLT_CALL_STUB_SIZE;
8527 if (PPC_HA (off + 16) != PPC_HA (off))
8528 size += 4;
8530 else
8532 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
8533 variants. */
8534 off = (stub_entry->target_value
8535 + stub_entry->target_section->output_offset
8536 + stub_entry->target_section->output_section->vma);
8537 off -= (stub_entry->stub_sec->size
8538 + stub_entry->stub_sec->output_offset
8539 + stub_entry->stub_sec->output_section->vma);
8541 /* Reset the stub type from the plt variant in case we now
8542 can reach with a shorter stub. */
8543 if (stub_entry->stub_type >= ppc_stub_plt_branch)
8544 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
8546 size = 4;
8547 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
8549 off -= 12;
8550 size = 16;
8553 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
8554 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8556 struct ppc_branch_hash_entry *br_entry;
8558 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8559 stub_entry->root.string + 9,
8560 TRUE, FALSE);
8561 if (br_entry == NULL)
8563 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
8564 stub_entry->root.string);
8565 htab->stub_error = TRUE;
8566 return FALSE;
8569 if (br_entry->iter != htab->stub_iteration)
8571 br_entry->iter = htab->stub_iteration;
8572 br_entry->offset = htab->brlt->size;
8573 htab->brlt->size += 8;
8575 if (htab->relbrlt != NULL)
8576 htab->relbrlt->size += sizeof (Elf64_External_Rela);
8579 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
8580 size = 16;
8581 if (stub_entry->stub_type != ppc_stub_plt_branch)
8582 size = 28;
8585 if (info->emitrelocations
8586 && (stub_entry->stub_type == ppc_stub_long_branch
8587 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
8588 stub_entry->stub_sec->reloc_count += 1;
8591 stub_entry->stub_sec->size += size;
8592 return TRUE;
8595 /* Set up various things so that we can make a list of input sections
8596 for each output section included in the link. Returns -1 on error,
8597 0 when no stubs will be needed, and 1 on success. */
8600 ppc64_elf_setup_section_lists (bfd *output_bfd,
8601 struct bfd_link_info *info,
8602 int no_multi_toc)
8604 bfd *input_bfd;
8605 int top_id, top_index, id;
8606 asection *section;
8607 asection **input_list;
8608 bfd_size_type amt;
8609 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8611 htab->no_multi_toc = no_multi_toc;
8613 if (htab->brlt == NULL)
8614 return 0;
8616 /* Find the top input section id. */
8617 for (input_bfd = info->input_bfds, top_id = 3;
8618 input_bfd != NULL;
8619 input_bfd = input_bfd->link_next)
8621 for (section = input_bfd->sections;
8622 section != NULL;
8623 section = section->next)
8625 if (top_id < section->id)
8626 top_id = section->id;
8630 htab->top_id = top_id;
8631 amt = sizeof (struct map_stub) * (top_id + 1);
8632 htab->stub_group = bfd_zmalloc (amt);
8633 if (htab->stub_group == NULL)
8634 return -1;
8636 /* Set toc_off for com, und, abs and ind sections. */
8637 for (id = 0; id < 3; id++)
8638 htab->stub_group[id].toc_off = TOC_BASE_OFF;
8640 elf_gp (output_bfd) = htab->toc_curr = ppc64_elf_toc (output_bfd);
8642 /* We can't use output_bfd->section_count here to find the top output
8643 section index as some sections may have been removed, and
8644 strip_excluded_output_sections doesn't renumber the indices. */
8645 for (section = output_bfd->sections, top_index = 0;
8646 section != NULL;
8647 section = section->next)
8649 if (top_index < section->index)
8650 top_index = section->index;
8653 htab->top_index = top_index;
8654 amt = sizeof (asection *) * (top_index + 1);
8655 input_list = bfd_zmalloc (amt);
8656 htab->input_list = input_list;
8657 if (input_list == NULL)
8658 return -1;
8660 return 1;
8663 /* The linker repeatedly calls this function for each TOC input section
8664 and linker generated GOT section. Group input bfds such that the toc
8665 within a group is less than 64k in size. Will break with cute linker
8666 scripts that play games with dot in the output toc section. */
8668 void
8669 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
8671 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8673 if (!htab->no_multi_toc)
8675 bfd_vma addr = isec->output_offset + isec->output_section->vma;
8676 bfd_vma off = addr - htab->toc_curr;
8678 if (off + isec->size > 0x10000)
8679 htab->toc_curr = addr;
8681 elf_gp (isec->owner) = (htab->toc_curr
8682 - elf_gp (isec->output_section->owner)
8683 + TOC_BASE_OFF);
8687 /* Called after the last call to the above function. */
8689 void
8690 ppc64_elf_reinit_toc (bfd *output_bfd, struct bfd_link_info *info)
8692 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8694 htab->multi_toc_needed = htab->toc_curr != elf_gp (output_bfd);
8696 /* toc_curr tracks the TOC offset used for code sections below in
8697 ppc64_elf_next_input_section. Start off at 0x8000. */
8698 htab->toc_curr = TOC_BASE_OFF;
8701 /* No toc references were found in ISEC. If the code in ISEC makes no
8702 calls, then there's no need to use toc adjusting stubs when branching
8703 into ISEC. Actually, indirect calls from ISEC are OK as they will
8704 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
8705 needed, and 2 if a cyclical call-graph was found but no other reason
8706 for a stub was detected. If called from the top level, a return of
8707 2 means the same as a return of 0. */
8709 static int
8710 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
8712 Elf_Internal_Rela *relstart, *rel;
8713 Elf_Internal_Sym *local_syms;
8714 int ret;
8715 struct ppc_link_hash_table *htab;
8717 /* We know none of our code bearing sections will need toc stubs. */
8718 if ((isec->flags & SEC_LINKER_CREATED) != 0)
8719 return 0;
8721 if (isec->size == 0)
8722 return 0;
8724 if (isec->output_section == NULL)
8725 return 0;
8727 /* Hack for linux kernel. .fixup contains branches, but only back to
8728 the function that hit an exception. */
8729 if (strcmp (isec->name, ".fixup") == 0)
8730 return 0;
8732 if (isec->reloc_count == 0)
8733 return 0;
8735 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
8736 info->keep_memory);
8737 if (relstart == NULL)
8738 return -1;
8740 /* Look for branches to outside of this section. */
8741 local_syms = NULL;
8742 ret = 0;
8743 htab = ppc_hash_table (info);
8744 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
8746 enum elf_ppc64_reloc_type r_type;
8747 unsigned long r_symndx;
8748 struct elf_link_hash_entry *h;
8749 Elf_Internal_Sym *sym;
8750 asection *sym_sec;
8751 long *opd_adjust;
8752 bfd_vma sym_value;
8753 bfd_vma dest;
8755 r_type = ELF64_R_TYPE (rel->r_info);
8756 if (r_type != R_PPC64_REL24
8757 && r_type != R_PPC64_REL14
8758 && r_type != R_PPC64_REL14_BRTAKEN
8759 && r_type != R_PPC64_REL14_BRNTAKEN)
8760 continue;
8762 r_symndx = ELF64_R_SYM (rel->r_info);
8763 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
8764 isec->owner))
8766 ret = -1;
8767 break;
8770 /* Calls to dynamic lib functions go through a plt call stub
8771 that uses r2. Branches to undefined symbols might be a call
8772 using old-style dot symbols that can be satisfied by a plt
8773 call into a new-style dynamic library. */
8774 if (sym_sec == NULL)
8776 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8777 if (eh != NULL
8778 && eh->oh != NULL
8779 && eh->oh->elf.plt.plist != NULL)
8781 ret = 1;
8782 break;
8785 /* Ignore other undefined symbols. */
8786 continue;
8789 /* Assume branches to other sections not included in the link need
8790 stubs too, to cover -R and absolute syms. */
8791 if (sym_sec->output_section == NULL)
8793 ret = 1;
8794 break;
8797 if (h == NULL)
8798 sym_value = sym->st_value;
8799 else
8801 if (h->root.type != bfd_link_hash_defined
8802 && h->root.type != bfd_link_hash_defweak)
8803 abort ();
8804 sym_value = h->root.u.def.value;
8806 sym_value += rel->r_addend;
8808 /* If this branch reloc uses an opd sym, find the code section. */
8809 opd_adjust = get_opd_info (sym_sec);
8810 if (opd_adjust != NULL)
8812 if (h == NULL)
8814 long adjust;
8816 adjust = opd_adjust[sym->st_value / 8];
8817 if (adjust == -1)
8818 /* Assume deleted functions won't ever be called. */
8819 continue;
8820 sym_value += adjust;
8823 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
8824 if (dest == (bfd_vma) -1)
8825 continue;
8827 else
8828 dest = (sym_value
8829 + sym_sec->output_offset
8830 + sym_sec->output_section->vma);
8832 /* Ignore branch to self. */
8833 if (sym_sec == isec)
8834 continue;
8836 /* If the called function uses the toc, we need a stub. */
8837 if (sym_sec->has_toc_reloc
8838 || sym_sec->makes_toc_func_call)
8840 ret = 1;
8841 break;
8844 /* Assume any branch that needs a long branch stub might in fact
8845 need a plt_branch stub. A plt_branch stub uses r2. */
8846 else if (dest - (isec->output_offset
8847 + isec->output_section->vma
8848 + rel->r_offset) + (1 << 25) >= (2 << 25))
8850 ret = 1;
8851 break;
8854 /* If calling back to a section in the process of being tested, we
8855 can't say for sure that no toc adjusting stubs are needed, so
8856 don't return zero. */
8857 else if (sym_sec->call_check_in_progress)
8858 ret = 2;
8860 /* Branches to another section that itself doesn't have any TOC
8861 references are OK. Recursively call ourselves to check. */
8862 else if (sym_sec->id <= htab->top_id
8863 && htab->stub_group[sym_sec->id].toc_off == 0)
8865 int recur;
8867 /* Mark current section as indeterminate, so that other
8868 sections that call back to current won't be marked as
8869 known. */
8870 isec->call_check_in_progress = 1;
8871 recur = toc_adjusting_stub_needed (info, sym_sec);
8872 isec->call_check_in_progress = 0;
8874 if (recur < 0)
8876 /* An error. Exit. */
8877 ret = -1;
8878 break;
8880 else if (recur <= 1)
8882 /* Known result. Mark as checked and set section flag. */
8883 htab->stub_group[sym_sec->id].toc_off = 1;
8884 if (recur != 0)
8886 sym_sec->makes_toc_func_call = 1;
8887 ret = 1;
8888 break;
8891 else
8893 /* Unknown result. Continue checking. */
8894 ret = 2;
8899 if (local_syms != NULL
8900 && (elf_tdata (isec->owner)->symtab_hdr.contents
8901 != (unsigned char *) local_syms))
8902 free (local_syms);
8903 if (elf_section_data (isec)->relocs != relstart)
8904 free (relstart);
8906 return ret;
8909 /* The linker repeatedly calls this function for each input section,
8910 in the order that input sections are linked into output sections.
8911 Build lists of input sections to determine groupings between which
8912 we may insert linker stubs. */
8914 bfd_boolean
8915 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
8917 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8919 if ((isec->output_section->flags & SEC_CODE) != 0
8920 && isec->output_section->index <= htab->top_index)
8922 asection **list = htab->input_list + isec->output_section->index;
8923 /* Steal the link_sec pointer for our list. */
8924 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
8925 /* This happens to make the list in reverse order,
8926 which is what we want. */
8927 PREV_SEC (isec) = *list;
8928 *list = isec;
8931 if (htab->multi_toc_needed)
8933 /* If a code section has a function that uses the TOC then we need
8934 to use the right TOC (obviously). Also, make sure that .opd gets
8935 the correct TOC value for R_PPC64_TOC relocs that don't have or
8936 can't find their function symbol (shouldn't ever happen now). */
8937 if (isec->has_toc_reloc || (isec->flags & SEC_CODE) == 0)
8939 if (elf_gp (isec->owner) != 0)
8940 htab->toc_curr = elf_gp (isec->owner);
8942 else if (htab->stub_group[isec->id].toc_off == 0)
8944 int ret = toc_adjusting_stub_needed (info, isec);
8945 if (ret < 0)
8946 return FALSE;
8947 else
8948 isec->makes_toc_func_call = ret & 1;
8952 /* Functions that don't use the TOC can belong in any TOC group.
8953 Use the last TOC base. This happens to make _init and _fini
8954 pasting work. */
8955 htab->stub_group[isec->id].toc_off = htab->toc_curr;
8956 return TRUE;
8959 /* See whether we can group stub sections together. Grouping stub
8960 sections may result in fewer stubs. More importantly, we need to
8961 put all .init* and .fini* stubs at the beginning of the .init or
8962 .fini output sections respectively, because glibc splits the
8963 _init and _fini functions into multiple parts. Putting a stub in
8964 the middle of a function is not a good idea. */
8966 static void
8967 group_sections (struct ppc_link_hash_table *htab,
8968 bfd_size_type stub_group_size,
8969 bfd_boolean stubs_always_before_branch)
8971 asection **list;
8972 bfd_size_type stub14_group_size;
8973 bfd_boolean suppress_size_errors;
8975 suppress_size_errors = FALSE;
8976 stub14_group_size = stub_group_size;
8977 if (stub_group_size == 1)
8979 /* Default values. */
8980 if (stubs_always_before_branch)
8982 stub_group_size = 0x1e00000;
8983 stub14_group_size = 0x7800;
8985 else
8987 stub_group_size = 0x1c00000;
8988 stub14_group_size = 0x7000;
8990 suppress_size_errors = TRUE;
8993 list = htab->input_list + htab->top_index;
8996 asection *tail = *list;
8997 while (tail != NULL)
8999 asection *curr;
9000 asection *prev;
9001 bfd_size_type total;
9002 bfd_boolean big_sec;
9003 bfd_vma curr_toc;
9005 curr = tail;
9006 total = tail->size;
9007 big_sec = total > (ppc64_elf_section_data (tail)->has_14bit_branch
9008 ? stub14_group_size : stub_group_size);
9009 if (big_sec && !suppress_size_errors)
9010 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
9011 tail->owner, tail);
9012 curr_toc = htab->stub_group[tail->id].toc_off;
9014 while ((prev = PREV_SEC (curr)) != NULL
9015 && ((total += curr->output_offset - prev->output_offset)
9016 < (ppc64_elf_section_data (prev)->has_14bit_branch
9017 ? stub14_group_size : stub_group_size))
9018 && htab->stub_group[prev->id].toc_off == curr_toc)
9019 curr = prev;
9021 /* OK, the size from the start of CURR to the end is less
9022 than stub_group_size and thus can be handled by one stub
9023 section. (or the tail section is itself larger than
9024 stub_group_size, in which case we may be toast.) We
9025 should really be keeping track of the total size of stubs
9026 added here, as stubs contribute to the final output
9027 section size. That's a little tricky, and this way will
9028 only break if stubs added make the total size more than
9029 2^25, ie. for the default stub_group_size, if stubs total
9030 more than 2097152 bytes, or nearly 75000 plt call stubs. */
9033 prev = PREV_SEC (tail);
9034 /* Set up this stub group. */
9035 htab->stub_group[tail->id].link_sec = curr;
9037 while (tail != curr && (tail = prev) != NULL);
9039 /* But wait, there's more! Input sections up to stub_group_size
9040 bytes before the stub section can be handled by it too.
9041 Don't do this if we have a really large section after the
9042 stubs, as adding more stubs increases the chance that
9043 branches may not reach into the stub section. */
9044 if (!stubs_always_before_branch && !big_sec)
9046 total = 0;
9047 while (prev != NULL
9048 && ((total += tail->output_offset - prev->output_offset)
9049 < (ppc64_elf_section_data (prev)->has_14bit_branch
9050 ? stub14_group_size : stub_group_size))
9051 && htab->stub_group[prev->id].toc_off == curr_toc)
9053 tail = prev;
9054 prev = PREV_SEC (tail);
9055 htab->stub_group[tail->id].link_sec = curr;
9058 tail = prev;
9061 while (list-- != htab->input_list);
9062 free (htab->input_list);
9063 #undef PREV_SEC
9066 /* Determine and set the size of the stub section for a final link.
9068 The basic idea here is to examine all the relocations looking for
9069 PC-relative calls to a target that is unreachable with a "bl"
9070 instruction. */
9072 bfd_boolean
9073 ppc64_elf_size_stubs (bfd *output_bfd,
9074 struct bfd_link_info *info,
9075 bfd_signed_vma group_size,
9076 asection *(*add_stub_section) (const char *, asection *),
9077 void (*layout_sections_again) (void))
9079 bfd_size_type stub_group_size;
9080 bfd_boolean stubs_always_before_branch;
9081 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9083 /* Stash our params away. */
9084 htab->add_stub_section = add_stub_section;
9085 htab->layout_sections_again = layout_sections_again;
9086 stubs_always_before_branch = group_size < 0;
9087 if (group_size < 0)
9088 stub_group_size = -group_size;
9089 else
9090 stub_group_size = group_size;
9092 group_sections (htab, stub_group_size, stubs_always_before_branch);
9094 while (1)
9096 bfd *input_bfd;
9097 unsigned int bfd_indx;
9098 asection *stub_sec;
9100 htab->stub_iteration += 1;
9102 for (input_bfd = info->input_bfds, bfd_indx = 0;
9103 input_bfd != NULL;
9104 input_bfd = input_bfd->link_next, bfd_indx++)
9106 Elf_Internal_Shdr *symtab_hdr;
9107 asection *section;
9108 Elf_Internal_Sym *local_syms = NULL;
9110 if (!is_ppc64_elf_target (input_bfd->xvec))
9111 continue;
9113 /* We'll need the symbol table in a second. */
9114 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9115 if (symtab_hdr->sh_info == 0)
9116 continue;
9118 /* Walk over each section attached to the input bfd. */
9119 for (section = input_bfd->sections;
9120 section != NULL;
9121 section = section->next)
9123 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
9125 /* If there aren't any relocs, then there's nothing more
9126 to do. */
9127 if ((section->flags & SEC_RELOC) == 0
9128 || (section->flags & SEC_ALLOC) == 0
9129 || (section->flags & SEC_LOAD) == 0
9130 || (section->flags & SEC_CODE) == 0
9131 || section->reloc_count == 0)
9132 continue;
9134 /* If this section is a link-once section that will be
9135 discarded, then don't create any stubs. */
9136 if (section->output_section == NULL
9137 || section->output_section->owner != output_bfd)
9138 continue;
9140 /* Get the relocs. */
9141 internal_relocs
9142 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
9143 info->keep_memory);
9144 if (internal_relocs == NULL)
9145 goto error_ret_free_local;
9147 /* Now examine each relocation. */
9148 irela = internal_relocs;
9149 irelaend = irela + section->reloc_count;
9150 for (; irela < irelaend; irela++)
9152 enum elf_ppc64_reloc_type r_type;
9153 unsigned int r_indx;
9154 enum ppc_stub_type stub_type;
9155 struct ppc_stub_hash_entry *stub_entry;
9156 asection *sym_sec, *code_sec;
9157 bfd_vma sym_value;
9158 bfd_vma destination;
9159 bfd_boolean ok_dest;
9160 struct ppc_link_hash_entry *hash;
9161 struct ppc_link_hash_entry *fdh;
9162 struct elf_link_hash_entry *h;
9163 Elf_Internal_Sym *sym;
9164 char *stub_name;
9165 const asection *id_sec;
9166 long *opd_adjust;
9168 r_type = ELF64_R_TYPE (irela->r_info);
9169 r_indx = ELF64_R_SYM (irela->r_info);
9171 if (r_type >= R_PPC64_max)
9173 bfd_set_error (bfd_error_bad_value);
9174 goto error_ret_free_internal;
9177 /* Only look for stubs on branch instructions. */
9178 if (r_type != R_PPC64_REL24
9179 && r_type != R_PPC64_REL14
9180 && r_type != R_PPC64_REL14_BRTAKEN
9181 && r_type != R_PPC64_REL14_BRNTAKEN)
9182 continue;
9184 /* Now determine the call target, its name, value,
9185 section. */
9186 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9187 r_indx, input_bfd))
9188 goto error_ret_free_internal;
9189 hash = (struct ppc_link_hash_entry *) h;
9191 ok_dest = FALSE;
9192 fdh = NULL;
9193 sym_value = 0;
9194 if (hash == NULL)
9196 sym_value = sym->st_value;
9197 ok_dest = TRUE;
9199 else if (hash->elf.root.type == bfd_link_hash_defined
9200 || hash->elf.root.type == bfd_link_hash_defweak)
9202 sym_value = hash->elf.root.u.def.value;
9203 if (sym_sec->output_section != NULL)
9204 ok_dest = TRUE;
9206 else if (hash->elf.root.type == bfd_link_hash_undefweak
9207 || hash->elf.root.type == bfd_link_hash_undefined)
9209 /* Recognise an old ABI func code entry sym, and
9210 use the func descriptor sym instead if it is
9211 defined. */
9212 if (hash->elf.root.root.string[0] == '.'
9213 && (fdh = get_fdh (hash, htab)) != NULL)
9215 if (fdh->elf.root.type == bfd_link_hash_defined
9216 || fdh->elf.root.type == bfd_link_hash_defweak)
9218 sym_sec = fdh->elf.root.u.def.section;
9219 sym_value = fdh->elf.root.u.def.value;
9220 if (sym_sec->output_section != NULL)
9221 ok_dest = TRUE;
9223 else
9224 fdh = NULL;
9227 else
9229 bfd_set_error (bfd_error_bad_value);
9230 goto error_ret_free_internal;
9233 destination = 0;
9234 if (ok_dest)
9236 sym_value += irela->r_addend;
9237 destination = (sym_value
9238 + sym_sec->output_offset
9239 + sym_sec->output_section->vma);
9242 code_sec = sym_sec;
9243 opd_adjust = get_opd_info (sym_sec);
9244 if (opd_adjust != NULL)
9246 bfd_vma dest;
9248 if (hash == NULL)
9250 long adjust = opd_adjust[sym_value / 8];
9251 if (adjust == -1)
9252 continue;
9253 sym_value += adjust;
9255 dest = opd_entry_value (sym_sec, sym_value,
9256 &code_sec, &sym_value);
9257 if (dest != (bfd_vma) -1)
9259 destination = dest;
9260 if (fdh != NULL)
9262 /* Fixup old ABI sym to point at code
9263 entry. */
9264 hash->elf.root.type = bfd_link_hash_defweak;
9265 hash->elf.root.u.def.section = code_sec;
9266 hash->elf.root.u.def.value = sym_value;
9271 /* Determine what (if any) linker stub is needed. */
9272 stub_type = ppc_type_of_stub (section, irela, &hash,
9273 destination);
9275 if (stub_type != ppc_stub_plt_call)
9277 /* Check whether we need a TOC adjusting stub.
9278 Since the linker pastes together pieces from
9279 different object files when creating the
9280 _init and _fini functions, it may be that a
9281 call to what looks like a local sym is in
9282 fact a call needing a TOC adjustment. */
9283 if (code_sec != NULL
9284 && code_sec->output_section != NULL
9285 && (htab->stub_group[code_sec->id].toc_off
9286 != htab->stub_group[section->id].toc_off)
9287 && (code_sec->has_toc_reloc
9288 || code_sec->makes_toc_func_call))
9289 stub_type = ppc_stub_long_branch_r2off;
9292 if (stub_type == ppc_stub_none)
9293 continue;
9295 /* __tls_get_addr calls might be eliminated. */
9296 if (stub_type != ppc_stub_plt_call
9297 && hash != NULL
9298 && (hash == htab->tls_get_addr
9299 || hash == htab->tls_get_addr_fd)
9300 && section->has_tls_reloc
9301 && irela != internal_relocs)
9303 /* Get tls info. */
9304 char *tls_mask;
9306 if (!get_tls_mask (&tls_mask, NULL, &local_syms,
9307 irela - 1, input_bfd))
9308 goto error_ret_free_internal;
9309 if (*tls_mask != 0)
9310 continue;
9313 /* Support for grouping stub sections. */
9314 id_sec = htab->stub_group[section->id].link_sec;
9316 /* Get the name of this stub. */
9317 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
9318 if (!stub_name)
9319 goto error_ret_free_internal;
9321 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
9322 stub_name, FALSE, FALSE);
9323 if (stub_entry != NULL)
9325 /* The proper stub has already been created. */
9326 free (stub_name);
9327 continue;
9330 stub_entry = ppc_add_stub (stub_name, section, htab);
9331 if (stub_entry == NULL)
9333 free (stub_name);
9334 error_ret_free_internal:
9335 if (elf_section_data (section)->relocs == NULL)
9336 free (internal_relocs);
9337 error_ret_free_local:
9338 if (local_syms != NULL
9339 && (symtab_hdr->contents
9340 != (unsigned char *) local_syms))
9341 free (local_syms);
9342 return FALSE;
9345 stub_entry->stub_type = stub_type;
9346 stub_entry->target_value = sym_value;
9347 stub_entry->target_section = code_sec;
9348 stub_entry->h = hash;
9349 stub_entry->addend = irela->r_addend;
9351 if (stub_entry->h != NULL)
9352 htab->stub_globals += 1;
9355 /* We're done with the internal relocs, free them. */
9356 if (elf_section_data (section)->relocs != internal_relocs)
9357 free (internal_relocs);
9360 if (local_syms != NULL
9361 && symtab_hdr->contents != (unsigned char *) local_syms)
9363 if (!info->keep_memory)
9364 free (local_syms);
9365 else
9366 symtab_hdr->contents = (unsigned char *) local_syms;
9370 /* We may have added some stubs. Find out the new size of the
9371 stub sections. */
9372 for (stub_sec = htab->stub_bfd->sections;
9373 stub_sec != NULL;
9374 stub_sec = stub_sec->next)
9375 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
9377 stub_sec->rawsize = stub_sec->size;
9378 stub_sec->size = 0;
9379 stub_sec->reloc_count = 0;
9382 htab->brlt->size = 0;
9383 if (htab->relbrlt != NULL)
9384 htab->relbrlt->size = 0;
9386 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
9388 for (stub_sec = htab->stub_bfd->sections;
9389 stub_sec != NULL;
9390 stub_sec = stub_sec->next)
9391 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9392 && stub_sec->rawsize != stub_sec->size)
9393 break;
9395 /* Exit from this loop when no stubs have been added, and no stubs
9396 have changed size. */
9397 if (stub_sec == NULL)
9398 break;
9400 /* Ask the linker to do its stuff. */
9401 (*htab->layout_sections_again) ();
9404 /* It would be nice to strip htab->brlt from the output if the
9405 section is empty, but it's too late. If we strip sections here,
9406 the dynamic symbol table is corrupted since the section symbol
9407 for the stripped section isn't written. */
9409 return TRUE;
9412 /* Called after we have determined section placement. If sections
9413 move, we'll be called again. Provide a value for TOCstart. */
9415 bfd_vma
9416 ppc64_elf_toc (bfd *obfd)
9418 asection *s;
9419 bfd_vma TOCstart;
9421 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
9422 order. The TOC starts where the first of these sections starts. */
9423 s = bfd_get_section_by_name (obfd, ".got");
9424 if (s == NULL)
9425 s = bfd_get_section_by_name (obfd, ".toc");
9426 if (s == NULL)
9427 s = bfd_get_section_by_name (obfd, ".tocbss");
9428 if (s == NULL)
9429 s = bfd_get_section_by_name (obfd, ".plt");
9430 if (s == NULL)
9432 /* This may happen for
9433 o references to TOC base (SYM@toc / TOC[tc0]) without a
9434 .toc directive
9435 o bad linker script
9436 o --gc-sections and empty TOC sections
9438 FIXME: Warn user? */
9440 /* Look for a likely section. We probably won't even be
9441 using TOCstart. */
9442 for (s = obfd->sections; s != NULL; s = s->next)
9443 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
9444 == (SEC_ALLOC | SEC_SMALL_DATA))
9445 break;
9446 if (s == NULL)
9447 for (s = obfd->sections; s != NULL; s = s->next)
9448 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
9449 == (SEC_ALLOC | SEC_SMALL_DATA))
9450 break;
9451 if (s == NULL)
9452 for (s = obfd->sections; s != NULL; s = s->next)
9453 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
9454 break;
9455 if (s == NULL)
9456 for (s = obfd->sections; s != NULL; s = s->next)
9457 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
9458 break;
9461 TOCstart = 0;
9462 if (s != NULL)
9463 TOCstart = s->output_section->vma + s->output_offset;
9465 return TOCstart;
9468 /* Build all the stubs associated with the current output file.
9469 The stubs are kept in a hash table attached to the main linker
9470 hash table. This function is called via gldelf64ppc_finish. */
9472 bfd_boolean
9473 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
9474 struct bfd_link_info *info,
9475 char **stats)
9477 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9478 asection *stub_sec;
9479 bfd_byte *p;
9480 int stub_sec_count = 0;
9482 htab->emit_stub_syms = emit_stub_syms;
9484 /* Allocate memory to hold the linker stubs. */
9485 for (stub_sec = htab->stub_bfd->sections;
9486 stub_sec != NULL;
9487 stub_sec = stub_sec->next)
9488 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9489 && stub_sec->size != 0)
9491 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
9492 if (stub_sec->contents == NULL)
9493 return FALSE;
9494 /* We want to check that built size is the same as calculated
9495 size. rawsize is a convenient location to use. */
9496 stub_sec->rawsize = stub_sec->size;
9497 stub_sec->size = 0;
9500 if (htab->glink != NULL && htab->glink->size != 0)
9502 unsigned int indx;
9503 bfd_vma plt0;
9505 /* Build the .glink plt call stub. */
9506 if (htab->emit_stub_syms)
9508 struct elf_link_hash_entry *h;
9509 h = elf_link_hash_lookup (&htab->elf, "__glink", TRUE, FALSE, FALSE);
9510 if (h == NULL)
9511 return FALSE;
9512 if (h->root.type == bfd_link_hash_new)
9514 h->root.type = bfd_link_hash_defined;
9515 h->root.u.def.section = htab->glink;
9516 h->root.u.def.value = 8;
9517 h->ref_regular = 1;
9518 h->def_regular = 1;
9519 h->ref_regular_nonweak = 1;
9520 h->forced_local = 1;
9521 h->non_elf = 0;
9524 p = htab->glink->contents;
9525 plt0 = (htab->plt->output_section->vma
9526 + htab->plt->output_offset
9527 - (htab->glink->output_section->vma
9528 + htab->glink->output_offset
9529 + 16));
9530 bfd_put_64 (htab->glink->owner, plt0, p);
9531 p += 8;
9532 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
9533 p += 4;
9534 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
9535 p += 4;
9536 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
9537 p += 4;
9538 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
9539 p += 4;
9540 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
9541 p += 4;
9542 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
9543 p += 4;
9544 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
9545 p += 4;
9546 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
9547 p += 4;
9548 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
9549 p += 4;
9550 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
9551 p += 4;
9552 bfd_put_32 (htab->glink->owner, BCTR, p);
9553 p += 4;
9554 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
9556 bfd_put_32 (htab->glink->owner, NOP, p);
9557 p += 4;
9560 /* Build the .glink lazy link call stubs. */
9561 indx = 0;
9562 while (p < htab->glink->contents + htab->glink->size)
9564 if (indx < 0x8000)
9566 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9567 p += 4;
9569 else
9571 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9572 p += 4;
9573 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9574 p += 4;
9576 bfd_put_32 (htab->glink->owner,
9577 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
9578 indx++;
9579 p += 4;
9581 htab->glink->rawsize = p - htab->glink->contents;
9584 if (htab->brlt->size != 0)
9586 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
9587 htab->brlt->size);
9588 if (htab->brlt->contents == NULL)
9589 return FALSE;
9591 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
9593 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
9594 htab->relbrlt->size);
9595 if (htab->relbrlt->contents == NULL)
9596 return FALSE;
9599 /* Build the stubs as directed by the stub hash table. */
9600 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
9602 if (htab->relbrlt != NULL)
9603 htab->relbrlt->reloc_count = 0;
9605 for (stub_sec = htab->stub_bfd->sections;
9606 stub_sec != NULL;
9607 stub_sec = stub_sec->next)
9608 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
9610 stub_sec_count += 1;
9611 if (stub_sec->rawsize != stub_sec->size)
9612 break;
9615 if (stub_sec != NULL
9616 || htab->glink->rawsize != htab->glink->size)
9618 htab->stub_error = TRUE;
9619 (*_bfd_error_handler) (_("stubs don't match calculated size"));
9622 if (htab->stub_error)
9623 return FALSE;
9625 if (stats != NULL)
9627 *stats = bfd_malloc (500);
9628 if (*stats == NULL)
9629 return FALSE;
9631 sprintf (*stats, _("linker stubs in %u group%s\n"
9632 " branch %lu\n"
9633 " toc adjust %lu\n"
9634 " long branch %lu\n"
9635 " long toc adj %lu\n"
9636 " plt call %lu"),
9637 stub_sec_count,
9638 stub_sec_count == 1 ? "" : "s",
9639 htab->stub_count[ppc_stub_long_branch - 1],
9640 htab->stub_count[ppc_stub_long_branch_r2off - 1],
9641 htab->stub_count[ppc_stub_plt_branch - 1],
9642 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
9643 htab->stub_count[ppc_stub_plt_call - 1]);
9645 return TRUE;
9648 /* This function undoes the changes made by add_symbol_adjust. */
9650 static bfd_boolean
9651 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
9653 struct ppc_link_hash_entry *eh;
9655 if (h->root.type == bfd_link_hash_indirect)
9656 return TRUE;
9658 if (h->root.type == bfd_link_hash_warning)
9659 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9661 eh = (struct ppc_link_hash_entry *) h;
9662 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
9663 return TRUE;
9665 eh->elf.root.type = bfd_link_hash_undefined;
9666 return TRUE;
9669 void
9670 ppc64_elf_restore_symbols (struct bfd_link_info *info)
9672 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9673 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
9676 /* What to do when ld finds relocations against symbols defined in
9677 discarded sections. */
9679 static unsigned int
9680 ppc64_elf_action_discarded (asection *sec)
9682 if (strcmp (".opd", sec->name) == 0)
9683 return 0;
9685 if (strcmp (".toc", sec->name) == 0)
9686 return 0;
9688 if (strcmp (".toc1", sec->name) == 0)
9689 return 0;
9691 return _bfd_elf_default_action_discarded (sec);
9694 /* The RELOCATE_SECTION function is called by the ELF backend linker
9695 to handle the relocations for a section.
9697 The relocs are always passed as Rela structures; if the section
9698 actually uses Rel structures, the r_addend field will always be
9699 zero.
9701 This function is responsible for adjust the section contents as
9702 necessary, and (if using Rela relocs and generating a
9703 relocatable output file) adjusting the reloc addend as
9704 necessary.
9706 This function does not have to worry about setting the reloc
9707 address or the reloc symbol index.
9709 LOCAL_SYMS is a pointer to the swapped in local symbols.
9711 LOCAL_SECTIONS is an array giving the section in the input file
9712 corresponding to the st_shndx field of each local symbol.
9714 The global hash table entry for the global symbols can be found
9715 via elf_sym_hashes (input_bfd).
9717 When generating relocatable output, this function must handle
9718 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
9719 going to be the section symbol corresponding to the output
9720 section, which means that the addend must be adjusted
9721 accordingly. */
9723 static bfd_boolean
9724 ppc64_elf_relocate_section (bfd *output_bfd,
9725 struct bfd_link_info *info,
9726 bfd *input_bfd,
9727 asection *input_section,
9728 bfd_byte *contents,
9729 Elf_Internal_Rela *relocs,
9730 Elf_Internal_Sym *local_syms,
9731 asection **local_sections)
9733 struct ppc_link_hash_table *htab;
9734 Elf_Internal_Shdr *symtab_hdr;
9735 struct elf_link_hash_entry **sym_hashes;
9736 Elf_Internal_Rela *rel;
9737 Elf_Internal_Rela *relend;
9738 Elf_Internal_Rela outrel;
9739 bfd_byte *loc;
9740 struct got_entry **local_got_ents;
9741 bfd_vma TOCstart;
9742 bfd_boolean ret = TRUE;
9743 bfd_boolean is_opd;
9744 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
9745 bfd_boolean is_power4 = FALSE;
9746 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
9748 /* Initialize howto table if needed. */
9749 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9750 ppc_howto_init ();
9752 htab = ppc_hash_table (info);
9754 /* Don't relocate stub sections. */
9755 if (input_section->owner == htab->stub_bfd)
9756 return TRUE;
9758 local_got_ents = elf_local_got_ents (input_bfd);
9759 TOCstart = elf_gp (output_bfd);
9760 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9761 sym_hashes = elf_sym_hashes (input_bfd);
9762 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
9764 rel = relocs;
9765 relend = relocs + input_section->reloc_count;
9766 for (; rel < relend; rel++)
9768 enum elf_ppc64_reloc_type r_type;
9769 bfd_vma addend, orig_addend;
9770 bfd_reloc_status_type r;
9771 Elf_Internal_Sym *sym;
9772 asection *sec;
9773 struct elf_link_hash_entry *h_elf;
9774 struct ppc_link_hash_entry *h;
9775 struct ppc_link_hash_entry *fdh;
9776 const char *sym_name;
9777 unsigned long r_symndx, toc_symndx;
9778 char tls_mask, tls_gd, tls_type;
9779 char sym_type;
9780 bfd_vma relocation;
9781 bfd_boolean unresolved_reloc;
9782 bfd_boolean warned;
9783 unsigned long insn, mask;
9784 struct ppc_stub_hash_entry *stub_entry;
9785 bfd_vma max_br_offset;
9786 bfd_vma from;
9788 r_type = ELF64_R_TYPE (rel->r_info);
9789 r_symndx = ELF64_R_SYM (rel->r_info);
9791 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
9792 symbol of the previous ADDR64 reloc. The symbol gives us the
9793 proper TOC base to use. */
9794 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
9795 && rel != relocs
9796 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
9797 && is_opd)
9798 r_symndx = ELF64_R_SYM (rel[-1].r_info);
9800 sym = NULL;
9801 sec = NULL;
9802 h_elf = NULL;
9803 sym_name = NULL;
9804 unresolved_reloc = FALSE;
9805 warned = FALSE;
9806 orig_addend = rel->r_addend;
9808 if (r_symndx < symtab_hdr->sh_info)
9810 /* It's a local symbol. */
9811 long *opd_adjust;
9813 sym = local_syms + r_symndx;
9814 sec = local_sections[r_symndx];
9815 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
9816 sym_type = ELF64_ST_TYPE (sym->st_info);
9817 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9818 opd_adjust = get_opd_info (sec);
9819 if (opd_adjust != NULL)
9821 long adjust = opd_adjust[(sym->st_value + rel->r_addend) / 8];
9822 if (adjust == -1)
9823 relocation = 0;
9824 else
9826 /* If this is a relocation against the opd section sym
9827 and we have edited .opd, adjust the reloc addend so
9828 that ld -r and ld --emit-relocs output is correct.
9829 If it is a reloc against some other .opd symbol,
9830 then the symbol value will be adjusted later. */
9831 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9832 rel->r_addend += adjust;
9833 else
9834 relocation += adjust;
9837 if (info->relocatable)
9838 continue;
9840 else
9842 if (info->relocatable)
9843 continue;
9844 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
9845 r_symndx, symtab_hdr, sym_hashes,
9846 h_elf, sec, relocation,
9847 unresolved_reloc, warned);
9848 sym_name = h_elf->root.root.string;
9849 sym_type = h_elf->type;
9851 h = (struct ppc_link_hash_entry *) h_elf;
9853 /* TLS optimizations. Replace instruction sequences and relocs
9854 based on information we collected in tls_optimize. We edit
9855 RELOCS so that --emit-relocs will output something sensible
9856 for the final instruction stream. */
9857 tls_mask = 0;
9858 tls_gd = 0;
9859 toc_symndx = 0;
9860 if (IS_PPC64_TLS_RELOC (r_type))
9862 if (h != NULL)
9863 tls_mask = h->tls_mask;
9864 else if (local_got_ents != NULL)
9866 char *lgot_masks;
9867 lgot_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
9868 tls_mask = lgot_masks[r_symndx];
9870 if (tls_mask == 0 && r_type == R_PPC64_TLS)
9872 /* Check for toc tls entries. */
9873 char *toc_tls;
9875 if (!get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
9876 rel, input_bfd))
9877 return FALSE;
9879 if (toc_tls)
9880 tls_mask = *toc_tls;
9884 /* Check that tls relocs are used with tls syms, and non-tls
9885 relocs are used with non-tls syms. */
9886 if (r_symndx != 0
9887 && r_type != R_PPC64_NONE
9888 && (h == NULL
9889 || h->elf.root.type == bfd_link_hash_defined
9890 || h->elf.root.type == bfd_link_hash_defweak)
9891 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
9893 if (r_type == R_PPC64_TLS && tls_mask != 0)
9894 /* R_PPC64_TLS is OK against a symbol in the TOC. */
9896 else
9897 (*_bfd_error_handler)
9898 (sym_type == STT_TLS
9899 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
9900 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s"),
9901 input_bfd,
9902 input_section,
9903 (long) rel->r_offset,
9904 ppc64_elf_howto_table[r_type]->name,
9905 sym_name);
9908 /* Ensure reloc mapping code below stays sane. */
9909 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
9910 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
9911 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
9912 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
9913 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
9914 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
9915 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
9916 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
9917 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
9918 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
9919 abort ();
9921 switch (r_type)
9923 default:
9924 break;
9926 case R_PPC64_TOC16:
9927 case R_PPC64_TOC16_LO:
9928 case R_PPC64_TOC16_DS:
9929 case R_PPC64_TOC16_LO_DS:
9931 /* Check for toc tls entries. */
9932 char *toc_tls;
9933 int retval;
9935 retval = get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
9936 rel, input_bfd);
9937 if (retval == 0)
9938 return FALSE;
9940 if (toc_tls)
9942 tls_mask = *toc_tls;
9943 if (r_type == R_PPC64_TOC16_DS
9944 || r_type == R_PPC64_TOC16_LO_DS)
9946 if (tls_mask != 0
9947 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
9948 goto toctprel;
9950 else
9952 /* If we found a GD reloc pair, then we might be
9953 doing a GD->IE transition. */
9954 if (retval == 2)
9956 tls_gd = TLS_TPRELGD;
9957 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
9958 goto tls_get_addr_check;
9960 else if (retval == 3)
9962 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
9963 goto tls_get_addr_check;
9968 break;
9970 case R_PPC64_GOT_TPREL16_DS:
9971 case R_PPC64_GOT_TPREL16_LO_DS:
9972 if (tls_mask != 0
9973 && (tls_mask & TLS_TPREL) == 0)
9975 toctprel:
9976 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
9977 insn &= 31 << 21;
9978 insn |= 0x3c0d0000; /* addis 0,13,0 */
9979 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
9980 r_type = R_PPC64_TPREL16_HA;
9981 if (toc_symndx != 0)
9983 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
9984 /* We changed the symbol. Start over in order to
9985 get h, sym, sec etc. right. */
9986 rel--;
9987 continue;
9989 else
9990 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
9992 break;
9994 case R_PPC64_TLS:
9995 if (tls_mask != 0
9996 && (tls_mask & TLS_TPREL) == 0)
9998 bfd_vma rtra;
9999 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
10000 if ((insn & ((0x3f << 26) | (31 << 11)))
10001 == ((31 << 26) | (13 << 11)))
10002 rtra = insn & ((1 << 26) - (1 << 16));
10003 else if ((insn & ((0x3f << 26) | (31 << 16)))
10004 == ((31 << 26) | (13 << 16)))
10005 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
10006 else
10007 abort ();
10008 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
10009 /* add -> addi. */
10010 insn = 14 << 26;
10011 else if ((insn & (31 << 1)) == 23 << 1
10012 && ((insn & (31 << 6)) < 14 << 6
10013 || ((insn & (31 << 6)) >= 16 << 6
10014 && (insn & (31 << 6)) < 24 << 6)))
10015 /* load and store indexed -> dform. */
10016 insn = (32 | ((insn >> 6) & 31)) << 26;
10017 else if ((insn & (31 << 1)) == 21 << 1
10018 && (insn & (0x1a << 6)) == 0)
10019 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
10020 insn = (((58 | ((insn >> 6) & 4)) << 26)
10021 | ((insn >> 6) & 1));
10022 else if ((insn & (31 << 1)) == 21 << 1
10023 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
10024 /* lwax -> lwa. */
10025 insn = (58 << 26) | 2;
10026 else
10027 abort ();
10028 insn |= rtra;
10029 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
10030 /* Was PPC64_TLS which sits on insn boundary, now
10031 PPC64_TPREL16_LO which is at low-order half-word. */
10032 rel->r_offset += d_offset;
10033 r_type = R_PPC64_TPREL16_LO;
10034 if (toc_symndx != 0)
10036 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
10037 /* We changed the symbol. Start over in order to
10038 get h, sym, sec etc. right. */
10039 rel--;
10040 continue;
10042 else
10043 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10045 break;
10047 case R_PPC64_GOT_TLSGD16_HI:
10048 case R_PPC64_GOT_TLSGD16_HA:
10049 tls_gd = TLS_TPRELGD;
10050 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
10051 goto tls_gdld_hi;
10052 break;
10054 case R_PPC64_GOT_TLSLD16_HI:
10055 case R_PPC64_GOT_TLSLD16_HA:
10056 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
10058 tls_gdld_hi:
10059 if ((tls_mask & tls_gd) != 0)
10060 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
10061 + R_PPC64_GOT_TPREL16_DS);
10062 else
10064 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
10065 rel->r_offset -= d_offset;
10066 r_type = R_PPC64_NONE;
10068 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10070 break;
10072 case R_PPC64_GOT_TLSGD16:
10073 case R_PPC64_GOT_TLSGD16_LO:
10074 tls_gd = TLS_TPRELGD;
10075 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
10076 goto tls_get_addr_check;
10077 break;
10079 case R_PPC64_GOT_TLSLD16:
10080 case R_PPC64_GOT_TLSLD16_LO:
10081 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
10083 tls_get_addr_check:
10084 if (rel + 1 < relend)
10086 enum elf_ppc64_reloc_type r_type2;
10087 unsigned long r_symndx2;
10088 struct elf_link_hash_entry *h2;
10089 bfd_vma insn1, insn2, insn3;
10090 bfd_vma offset;
10092 /* The next instruction should be a call to
10093 __tls_get_addr. Peek at the reloc to be sure. */
10094 r_type2 = ELF64_R_TYPE (rel[1].r_info);
10095 r_symndx2 = ELF64_R_SYM (rel[1].r_info);
10096 if (r_symndx2 < symtab_hdr->sh_info
10097 || (r_type2 != R_PPC64_REL14
10098 && r_type2 != R_PPC64_REL14_BRTAKEN
10099 && r_type2 != R_PPC64_REL14_BRNTAKEN
10100 && r_type2 != R_PPC64_REL24))
10101 break;
10103 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
10104 while (h2->root.type == bfd_link_hash_indirect
10105 || h2->root.type == bfd_link_hash_warning)
10106 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
10107 if (h2 == NULL || (h2 != &htab->tls_get_addr->elf
10108 && h2 != &htab->tls_get_addr_fd->elf))
10109 break;
10111 /* OK, it checks out. Replace the call. */
10112 offset = rel[1].r_offset;
10113 insn1 = bfd_get_32 (output_bfd,
10114 contents + rel->r_offset - d_offset);
10115 insn3 = bfd_get_32 (output_bfd,
10116 contents + offset + 4);
10117 if ((tls_mask & tls_gd) != 0)
10119 /* IE */
10120 insn1 &= (1 << 26) - (1 << 2);
10121 insn1 |= 58 << 26; /* ld */
10122 insn2 = 0x7c636a14; /* add 3,3,13 */
10123 rel[1].r_info = ELF64_R_INFO (r_symndx2, R_PPC64_NONE);
10124 if ((tls_mask & TLS_EXPLICIT) == 0)
10125 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
10126 + R_PPC64_GOT_TPREL16_DS);
10127 else
10128 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
10129 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10131 else
10133 /* LE */
10134 insn1 = 0x3c6d0000; /* addis 3,13,0 */
10135 insn2 = 0x38630000; /* addi 3,3,0 */
10136 if (tls_gd == 0)
10138 /* Was an LD reloc. */
10139 r_symndx = 0;
10140 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
10141 rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
10143 else if (toc_symndx != 0)
10144 r_symndx = toc_symndx;
10145 r_type = R_PPC64_TPREL16_HA;
10146 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10147 rel[1].r_info = ELF64_R_INFO (r_symndx,
10148 R_PPC64_TPREL16_LO);
10149 rel[1].r_offset += d_offset;
10151 if (insn3 == NOP
10152 || insn3 == CROR_151515 || insn3 == CROR_313131)
10154 insn3 = insn2;
10155 insn2 = NOP;
10156 rel[1].r_offset += 4;
10158 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - d_offset);
10159 bfd_put_32 (output_bfd, insn2, contents + offset);
10160 bfd_put_32 (output_bfd, insn3, contents + offset + 4);
10161 if (tls_gd == 0 || toc_symndx != 0)
10163 /* We changed the symbol. Start over in order
10164 to get h, sym, sec etc. right. */
10165 rel--;
10166 continue;
10170 break;
10172 case R_PPC64_DTPMOD64:
10173 if (rel + 1 < relend
10174 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
10175 && rel[1].r_offset == rel->r_offset + 8)
10177 if ((tls_mask & TLS_GD) == 0)
10179 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
10180 if ((tls_mask & TLS_TPRELGD) != 0)
10181 r_type = R_PPC64_TPREL64;
10182 else
10184 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
10185 r_type = R_PPC64_NONE;
10187 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10190 else
10192 if ((tls_mask & TLS_LD) == 0)
10194 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
10195 r_type = R_PPC64_NONE;
10196 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10199 break;
10201 case R_PPC64_TPREL64:
10202 if ((tls_mask & TLS_TPREL) == 0)
10204 r_type = R_PPC64_NONE;
10205 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10207 break;
10210 /* Handle other relocations that tweak non-addend part of insn. */
10211 insn = 0;
10212 max_br_offset = 1 << 25;
10213 addend = rel->r_addend;
10214 switch (r_type)
10216 default:
10217 break;
10219 /* Branch taken prediction relocations. */
10220 case R_PPC64_ADDR14_BRTAKEN:
10221 case R_PPC64_REL14_BRTAKEN:
10222 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
10223 /* Fall thru. */
10225 /* Branch not taken prediction relocations. */
10226 case R_PPC64_ADDR14_BRNTAKEN:
10227 case R_PPC64_REL14_BRNTAKEN:
10228 insn |= bfd_get_32 (output_bfd,
10229 contents + rel->r_offset) & ~(0x01 << 21);
10230 /* Fall thru. */
10232 case R_PPC64_REL14:
10233 max_br_offset = 1 << 15;
10234 /* Fall thru. */
10236 case R_PPC64_REL24:
10237 /* Calls to functions with a different TOC, such as calls to
10238 shared objects, need to alter the TOC pointer. This is
10239 done using a linkage stub. A REL24 branching to these
10240 linkage stubs needs to be followed by a nop, as the nop
10241 will be replaced with an instruction to restore the TOC
10242 base pointer. */
10243 stub_entry = NULL;
10244 fdh = h;
10245 if (((h != NULL
10246 && (((fdh = h->oh) != NULL
10247 && fdh->elf.plt.plist != NULL)
10248 || (fdh = h)->elf.plt.plist != NULL))
10249 || (sec != NULL
10250 && sec->output_section != NULL
10251 && sec->id <= htab->top_id
10252 && (htab->stub_group[sec->id].toc_off
10253 != htab->stub_group[input_section->id].toc_off)))
10254 && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
10255 rel, htab)) != NULL
10256 && (stub_entry->stub_type == ppc_stub_plt_call
10257 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
10258 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
10260 bfd_boolean can_plt_call = FALSE;
10262 if (rel->r_offset + 8 <= input_section->size)
10264 unsigned long nop;
10265 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
10266 if (nop == NOP
10267 || nop == CROR_151515 || nop == CROR_313131)
10269 bfd_put_32 (input_bfd, LD_R2_40R1,
10270 contents + rel->r_offset + 4);
10271 can_plt_call = TRUE;
10275 if (!can_plt_call)
10277 if (stub_entry->stub_type == ppc_stub_plt_call)
10279 /* If this is a plain branch rather than a branch
10280 and link, don't require a nop. However, don't
10281 allow tail calls in a shared library as they
10282 will result in r2 being corrupted. */
10283 unsigned long br;
10284 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
10285 if (info->executable && (br & 1) == 0)
10286 can_plt_call = TRUE;
10287 else
10288 stub_entry = NULL;
10290 else if (h != NULL
10291 && strcmp (h->elf.root.root.string,
10292 ".__libc_start_main") == 0)
10294 /* Allow crt1 branch to go via a toc adjusting stub. */
10295 can_plt_call = TRUE;
10297 else
10299 if (strcmp (input_section->output_section->name,
10300 ".init") == 0
10301 || strcmp (input_section->output_section->name,
10302 ".fini") == 0)
10303 (*_bfd_error_handler)
10304 (_("%B(%A+0x%lx): automatic multiple TOCs "
10305 "not supported using your crt files; "
10306 "recompile with -mminimal-toc or upgrade gcc"),
10307 input_bfd,
10308 input_section,
10309 (long) rel->r_offset);
10310 else
10311 (*_bfd_error_handler)
10312 (_("%B(%A+0x%lx): sibling call optimization to `%s' "
10313 "does not allow automatic multiple TOCs; "
10314 "recompile with -mminimal-toc or "
10315 "-fno-optimize-sibling-calls, "
10316 "or make `%s' extern"),
10317 input_bfd,
10318 input_section,
10319 (long) rel->r_offset,
10320 sym_name,
10321 sym_name);
10322 bfd_set_error (bfd_error_bad_value);
10323 ret = FALSE;
10327 if (can_plt_call
10328 && stub_entry->stub_type == ppc_stub_plt_call)
10329 unresolved_reloc = FALSE;
10332 if (stub_entry == NULL
10333 && get_opd_info (sec) != NULL)
10335 /* The branch destination is the value of the opd entry. */
10336 bfd_vma off = (relocation + addend
10337 - sec->output_section->vma
10338 - sec->output_offset);
10339 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
10340 if (dest != (bfd_vma) -1)
10342 relocation = dest;
10343 addend = 0;
10347 /* If the branch is out of reach we ought to have a long
10348 branch stub. */
10349 from = (rel->r_offset
10350 + input_section->output_offset
10351 + input_section->output_section->vma);
10353 if (stub_entry == NULL
10354 && (relocation + addend - from + max_br_offset
10355 >= 2 * max_br_offset)
10356 && r_type != R_PPC64_ADDR14_BRTAKEN
10357 && r_type != R_PPC64_ADDR14_BRNTAKEN)
10358 stub_entry = ppc_get_stub_entry (input_section, sec, h, rel,
10359 htab);
10361 if (stub_entry != NULL)
10363 /* Munge up the value and addend so that we call the stub
10364 rather than the procedure directly. */
10365 relocation = (stub_entry->stub_offset
10366 + stub_entry->stub_sec->output_offset
10367 + stub_entry->stub_sec->output_section->vma);
10368 addend = 0;
10371 if (insn != 0)
10373 if (is_power4)
10375 /* Set 'a' bit. This is 0b00010 in BO field for branch
10376 on CR(BI) insns (BO == 001at or 011at), and 0b01000
10377 for branch on CTR insns (BO == 1a00t or 1a01t). */
10378 if ((insn & (0x14 << 21)) == (0x04 << 21))
10379 insn |= 0x02 << 21;
10380 else if ((insn & (0x14 << 21)) == (0x10 << 21))
10381 insn |= 0x08 << 21;
10382 else
10383 break;
10385 else
10387 /* Invert 'y' bit if not the default. */
10388 if ((bfd_signed_vma) (relocation + addend - from) < 0)
10389 insn ^= 0x01 << 21;
10392 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
10395 /* NOP out calls to undefined weak functions.
10396 We can thus call a weak function without first
10397 checking whether the function is defined. */
10398 else if (h != NULL
10399 && h->elf.root.type == bfd_link_hash_undefweak
10400 && r_type == R_PPC64_REL24
10401 && relocation == 0
10402 && addend == 0)
10404 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
10405 continue;
10407 break;
10410 /* Set `addend'. */
10411 tls_type = 0;
10412 switch (r_type)
10414 default:
10415 (*_bfd_error_handler)
10416 (_("%B: unknown relocation type %d for symbol %s"),
10417 input_bfd, (int) r_type, sym_name);
10419 bfd_set_error (bfd_error_bad_value);
10420 ret = FALSE;
10421 continue;
10423 case R_PPC64_NONE:
10424 case R_PPC64_TLS:
10425 case R_PPC64_GNU_VTINHERIT:
10426 case R_PPC64_GNU_VTENTRY:
10427 continue;
10429 /* GOT16 relocations. Like an ADDR16 using the symbol's
10430 address in the GOT as relocation value instead of the
10431 symbol's value itself. Also, create a GOT entry for the
10432 symbol and put the symbol value there. */
10433 case R_PPC64_GOT_TLSGD16:
10434 case R_PPC64_GOT_TLSGD16_LO:
10435 case R_PPC64_GOT_TLSGD16_HI:
10436 case R_PPC64_GOT_TLSGD16_HA:
10437 tls_type = TLS_TLS | TLS_GD;
10438 goto dogot;
10440 case R_PPC64_GOT_TLSLD16:
10441 case R_PPC64_GOT_TLSLD16_LO:
10442 case R_PPC64_GOT_TLSLD16_HI:
10443 case R_PPC64_GOT_TLSLD16_HA:
10444 tls_type = TLS_TLS | TLS_LD;
10445 goto dogot;
10447 case R_PPC64_GOT_TPREL16_DS:
10448 case R_PPC64_GOT_TPREL16_LO_DS:
10449 case R_PPC64_GOT_TPREL16_HI:
10450 case R_PPC64_GOT_TPREL16_HA:
10451 tls_type = TLS_TLS | TLS_TPREL;
10452 goto dogot;
10454 case R_PPC64_GOT_DTPREL16_DS:
10455 case R_PPC64_GOT_DTPREL16_LO_DS:
10456 case R_PPC64_GOT_DTPREL16_HI:
10457 case R_PPC64_GOT_DTPREL16_HA:
10458 tls_type = TLS_TLS | TLS_DTPREL;
10459 goto dogot;
10461 case R_PPC64_GOT16:
10462 case R_PPC64_GOT16_LO:
10463 case R_PPC64_GOT16_HI:
10464 case R_PPC64_GOT16_HA:
10465 case R_PPC64_GOT16_DS:
10466 case R_PPC64_GOT16_LO_DS:
10467 dogot:
10469 /* Relocation is to the entry for this symbol in the global
10470 offset table. */
10471 asection *got;
10472 bfd_vma *offp;
10473 bfd_vma off;
10474 unsigned long indx = 0;
10476 if (tls_type == (TLS_TLS | TLS_LD)
10477 && (h == NULL
10478 || !h->elf.def_dynamic))
10479 offp = &ppc64_tlsld_got (input_bfd)->offset;
10480 else
10482 struct got_entry *ent;
10484 if (h != NULL)
10486 bfd_boolean dyn = htab->elf.dynamic_sections_created;
10487 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
10488 &h->elf)
10489 || (info->shared
10490 && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
10491 /* This is actually a static link, or it is a
10492 -Bsymbolic link and the symbol is defined
10493 locally, or the symbol was forced to be local
10494 because of a version file. */
10496 else
10498 indx = h->elf.dynindx;
10499 unresolved_reloc = FALSE;
10501 ent = h->elf.got.glist;
10503 else
10505 if (local_got_ents == NULL)
10506 abort ();
10507 ent = local_got_ents[r_symndx];
10510 for (; ent != NULL; ent = ent->next)
10511 if (ent->addend == orig_addend
10512 && ent->owner == input_bfd
10513 && ent->tls_type == tls_type)
10514 break;
10515 if (ent == NULL)
10516 abort ();
10517 offp = &ent->got.offset;
10520 got = ppc64_elf_tdata (input_bfd)->got;
10521 if (got == NULL)
10522 abort ();
10524 /* The offset must always be a multiple of 8. We use the
10525 least significant bit to record whether we have already
10526 processed this entry. */
10527 off = *offp;
10528 if ((off & 1) != 0)
10529 off &= ~1;
10530 else
10532 /* Generate relocs for the dynamic linker, except in
10533 the case of TLSLD where we'll use one entry per
10534 module. */
10535 asection *relgot = ppc64_elf_tdata (input_bfd)->relgot;
10537 *offp = off | 1;
10538 if ((info->shared || indx != 0)
10539 && (h == NULL
10540 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10541 || h->elf.root.type != bfd_link_hash_undefweak))
10543 outrel.r_offset = (got->output_section->vma
10544 + got->output_offset
10545 + off);
10546 outrel.r_addend = addend;
10547 if (tls_type & (TLS_LD | TLS_GD))
10549 outrel.r_addend = 0;
10550 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
10551 if (tls_type == (TLS_TLS | TLS_GD))
10553 loc = relgot->contents;
10554 loc += (relgot->reloc_count++
10555 * sizeof (Elf64_External_Rela));
10556 bfd_elf64_swap_reloca_out (output_bfd,
10557 &outrel, loc);
10558 outrel.r_offset += 8;
10559 outrel.r_addend = addend;
10560 outrel.r_info
10561 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
10564 else if (tls_type == (TLS_TLS | TLS_DTPREL))
10565 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
10566 else if (tls_type == (TLS_TLS | TLS_TPREL))
10567 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
10568 else if (indx == 0)
10570 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_RELATIVE);
10572 /* Write the .got section contents for the sake
10573 of prelink. */
10574 loc = got->contents + off;
10575 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
10576 loc);
10578 else
10579 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
10581 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
10583 outrel.r_addend += relocation;
10584 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
10585 outrel.r_addend -= htab->elf.tls_sec->vma;
10587 loc = relgot->contents;
10588 loc += (relgot->reloc_count++
10589 * sizeof (Elf64_External_Rela));
10590 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10593 /* Init the .got section contents here if we're not
10594 emitting a reloc. */
10595 else
10597 relocation += addend;
10598 if (tls_type == (TLS_TLS | TLS_LD))
10599 relocation = 1;
10600 else if (tls_type != 0)
10602 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
10603 if (tls_type == (TLS_TLS | TLS_TPREL))
10604 relocation += DTP_OFFSET - TP_OFFSET;
10606 if (tls_type == (TLS_TLS | TLS_GD))
10608 bfd_put_64 (output_bfd, relocation,
10609 got->contents + off + 8);
10610 relocation = 1;
10614 bfd_put_64 (output_bfd, relocation,
10615 got->contents + off);
10619 if (off >= (bfd_vma) -2)
10620 abort ();
10622 relocation = got->output_offset + off;
10624 /* TOC base (r2) is TOC start plus 0x8000. */
10625 addend = -TOC_BASE_OFF;
10627 break;
10629 case R_PPC64_PLT16_HA:
10630 case R_PPC64_PLT16_HI:
10631 case R_PPC64_PLT16_LO:
10632 case R_PPC64_PLT32:
10633 case R_PPC64_PLT64:
10634 /* Relocation is to the entry for this symbol in the
10635 procedure linkage table. */
10637 /* Resolve a PLT reloc against a local symbol directly,
10638 without using the procedure linkage table. */
10639 if (h == NULL)
10640 break;
10642 /* It's possible that we didn't make a PLT entry for this
10643 symbol. This happens when statically linking PIC code,
10644 or when using -Bsymbolic. Go find a match if there is a
10645 PLT entry. */
10646 if (htab->plt != NULL)
10648 struct plt_entry *ent;
10649 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
10650 if (ent->addend == orig_addend
10651 && ent->plt.offset != (bfd_vma) -1)
10653 relocation = (htab->plt->output_section->vma
10654 + htab->plt->output_offset
10655 + ent->plt.offset);
10656 unresolved_reloc = FALSE;
10659 break;
10661 case R_PPC64_TOC:
10662 /* Relocation value is TOC base. */
10663 relocation = TOCstart;
10664 if (r_symndx == 0)
10665 relocation += htab->stub_group[input_section->id].toc_off;
10666 else if (unresolved_reloc)
10668 else if (sec != NULL && sec->id <= htab->top_id)
10669 relocation += htab->stub_group[sec->id].toc_off;
10670 else
10671 unresolved_reloc = TRUE;
10672 goto dodyn2;
10674 /* TOC16 relocs. We want the offset relative to the TOC base,
10675 which is the address of the start of the TOC plus 0x8000.
10676 The TOC consists of sections .got, .toc, .tocbss, and .plt,
10677 in this order. */
10678 case R_PPC64_TOC16:
10679 case R_PPC64_TOC16_LO:
10680 case R_PPC64_TOC16_HI:
10681 case R_PPC64_TOC16_DS:
10682 case R_PPC64_TOC16_LO_DS:
10683 case R_PPC64_TOC16_HA:
10684 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
10685 break;
10687 /* Relocate against the beginning of the section. */
10688 case R_PPC64_SECTOFF:
10689 case R_PPC64_SECTOFF_LO:
10690 case R_PPC64_SECTOFF_HI:
10691 case R_PPC64_SECTOFF_DS:
10692 case R_PPC64_SECTOFF_LO_DS:
10693 case R_PPC64_SECTOFF_HA:
10694 if (sec != NULL)
10695 addend -= sec->output_section->vma;
10696 break;
10698 case R_PPC64_REL14:
10699 case R_PPC64_REL14_BRNTAKEN:
10700 case R_PPC64_REL14_BRTAKEN:
10701 case R_PPC64_REL24:
10702 break;
10704 case R_PPC64_TPREL16:
10705 case R_PPC64_TPREL16_LO:
10706 case R_PPC64_TPREL16_HI:
10707 case R_PPC64_TPREL16_HA:
10708 case R_PPC64_TPREL16_DS:
10709 case R_PPC64_TPREL16_LO_DS:
10710 case R_PPC64_TPREL16_HIGHER:
10711 case R_PPC64_TPREL16_HIGHERA:
10712 case R_PPC64_TPREL16_HIGHEST:
10713 case R_PPC64_TPREL16_HIGHESTA:
10714 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
10715 if (info->shared)
10716 /* The TPREL16 relocs shouldn't really be used in shared
10717 libs as they will result in DT_TEXTREL being set, but
10718 support them anyway. */
10719 goto dodyn;
10720 break;
10722 case R_PPC64_DTPREL16:
10723 case R_PPC64_DTPREL16_LO:
10724 case R_PPC64_DTPREL16_HI:
10725 case R_PPC64_DTPREL16_HA:
10726 case R_PPC64_DTPREL16_DS:
10727 case R_PPC64_DTPREL16_LO_DS:
10728 case R_PPC64_DTPREL16_HIGHER:
10729 case R_PPC64_DTPREL16_HIGHERA:
10730 case R_PPC64_DTPREL16_HIGHEST:
10731 case R_PPC64_DTPREL16_HIGHESTA:
10732 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
10733 break;
10735 case R_PPC64_DTPMOD64:
10736 relocation = 1;
10737 addend = 0;
10738 goto dodyn;
10740 case R_PPC64_TPREL64:
10741 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
10742 goto dodyn;
10744 case R_PPC64_DTPREL64:
10745 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
10746 /* Fall thru */
10748 /* Relocations that may need to be propagated if this is a
10749 dynamic object. */
10750 case R_PPC64_REL30:
10751 case R_PPC64_REL32:
10752 case R_PPC64_REL64:
10753 case R_PPC64_ADDR14:
10754 case R_PPC64_ADDR14_BRNTAKEN:
10755 case R_PPC64_ADDR14_BRTAKEN:
10756 case R_PPC64_ADDR16:
10757 case R_PPC64_ADDR16_DS:
10758 case R_PPC64_ADDR16_HA:
10759 case R_PPC64_ADDR16_HI:
10760 case R_PPC64_ADDR16_HIGHER:
10761 case R_PPC64_ADDR16_HIGHERA:
10762 case R_PPC64_ADDR16_HIGHEST:
10763 case R_PPC64_ADDR16_HIGHESTA:
10764 case R_PPC64_ADDR16_LO:
10765 case R_PPC64_ADDR16_LO_DS:
10766 case R_PPC64_ADDR24:
10767 case R_PPC64_ADDR32:
10768 case R_PPC64_ADDR64:
10769 case R_PPC64_UADDR16:
10770 case R_PPC64_UADDR32:
10771 case R_PPC64_UADDR64:
10772 /* r_symndx will be zero only for relocs against symbols
10773 from removed linkonce sections, or sections discarded by
10774 a linker script. */
10775 dodyn:
10776 if (r_symndx == 0)
10778 _bfd_clear_contents (ppc64_elf_howto_table[r_type], input_bfd,
10779 contents + rel->r_offset);
10780 break;
10782 /* Fall thru. */
10784 dodyn2:
10785 if ((input_section->flags & SEC_ALLOC) == 0)
10786 break;
10788 if (NO_OPD_RELOCS && is_opd)
10789 break;
10791 if ((info->shared
10792 && (h == NULL
10793 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10794 || h->elf.root.type != bfd_link_hash_undefweak)
10795 && (MUST_BE_DYN_RELOC (r_type)
10796 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
10797 || (ELIMINATE_COPY_RELOCS
10798 && !info->shared
10799 && h != NULL
10800 && h->elf.dynindx != -1
10801 && !h->elf.non_got_ref
10802 && h->elf.def_dynamic
10803 && !h->elf.def_regular))
10805 Elf_Internal_Rela outrel;
10806 bfd_boolean skip, relocate;
10807 asection *sreloc;
10808 bfd_byte *loc;
10809 bfd_vma out_off;
10811 /* When generating a dynamic object, these relocations
10812 are copied into the output file to be resolved at run
10813 time. */
10815 skip = FALSE;
10816 relocate = FALSE;
10818 out_off = _bfd_elf_section_offset (output_bfd, info,
10819 input_section, rel->r_offset);
10820 if (out_off == (bfd_vma) -1)
10821 skip = TRUE;
10822 else if (out_off == (bfd_vma) -2)
10823 skip = TRUE, relocate = TRUE;
10824 out_off += (input_section->output_section->vma
10825 + input_section->output_offset);
10826 outrel.r_offset = out_off;
10827 outrel.r_addend = rel->r_addend;
10829 /* Optimize unaligned reloc use. */
10830 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
10831 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
10832 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
10833 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
10834 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
10835 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
10836 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
10837 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
10838 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
10840 if (skip)
10841 memset (&outrel, 0, sizeof outrel);
10842 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
10843 && !is_opd
10844 && r_type != R_PPC64_TOC)
10845 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
10846 else
10848 /* This symbol is local, or marked to become local,
10849 or this is an opd section reloc which must point
10850 at a local function. */
10851 outrel.r_addend += relocation;
10852 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
10854 if (is_opd && h != NULL)
10856 /* Lie about opd entries. This case occurs
10857 when building shared libraries and we
10858 reference a function in another shared
10859 lib. The same thing happens for a weak
10860 definition in an application that's
10861 overridden by a strong definition in a
10862 shared lib. (I believe this is a generic
10863 bug in binutils handling of weak syms.)
10864 In these cases we won't use the opd
10865 entry in this lib. */
10866 unresolved_reloc = FALSE;
10868 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10870 /* We need to relocate .opd contents for ld.so.
10871 Prelink also wants simple and consistent rules
10872 for relocs. This make all RELATIVE relocs have
10873 *r_offset equal to r_addend. */
10874 relocate = TRUE;
10876 else
10878 long indx = 0;
10880 if (bfd_is_abs_section (sec))
10882 else if (sec == NULL || sec->owner == NULL)
10884 bfd_set_error (bfd_error_bad_value);
10885 return FALSE;
10887 else
10889 asection *osec;
10891 osec = sec->output_section;
10892 indx = elf_section_data (osec)->dynindx;
10894 if (indx == 0)
10896 if ((osec->flags & SEC_READONLY) == 0
10897 && htab->elf.data_index_section != NULL)
10898 osec = htab->elf.data_index_section;
10899 else
10900 osec = htab->elf.text_index_section;
10901 indx = elf_section_data (osec)->dynindx;
10903 BFD_ASSERT (indx != 0);
10905 /* We are turning this relocation into one
10906 against a section symbol, so subtract out
10907 the output section's address but not the
10908 offset of the input section in the output
10909 section. */
10910 outrel.r_addend -= osec->vma;
10913 outrel.r_info = ELF64_R_INFO (indx, r_type);
10917 sreloc = elf_section_data (input_section)->sreloc;
10918 if (sreloc == NULL)
10919 abort ();
10921 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
10922 >= sreloc->size)
10923 abort ();
10924 loc = sreloc->contents;
10925 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
10926 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10928 /* If this reloc is against an external symbol, it will
10929 be computed at runtime, so there's no need to do
10930 anything now. However, for the sake of prelink ensure
10931 that the section contents are a known value. */
10932 if (! relocate)
10934 unresolved_reloc = FALSE;
10935 /* The value chosen here is quite arbitrary as ld.so
10936 ignores section contents except for the special
10937 case of .opd where the contents might be accessed
10938 before relocation. Choose zero, as that won't
10939 cause reloc overflow. */
10940 relocation = 0;
10941 addend = 0;
10942 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
10943 to improve backward compatibility with older
10944 versions of ld. */
10945 if (r_type == R_PPC64_ADDR64)
10946 addend = outrel.r_addend;
10947 /* Adjust pc_relative relocs to have zero in *r_offset. */
10948 else if (ppc64_elf_howto_table[r_type]->pc_relative)
10949 addend = (input_section->output_section->vma
10950 + input_section->output_offset
10951 + rel->r_offset);
10954 break;
10956 case R_PPC64_COPY:
10957 case R_PPC64_GLOB_DAT:
10958 case R_PPC64_JMP_SLOT:
10959 case R_PPC64_RELATIVE:
10960 /* We shouldn't ever see these dynamic relocs in relocatable
10961 files. */
10962 /* Fall through. */
10964 case R_PPC64_PLTGOT16:
10965 case R_PPC64_PLTGOT16_DS:
10966 case R_PPC64_PLTGOT16_HA:
10967 case R_PPC64_PLTGOT16_HI:
10968 case R_PPC64_PLTGOT16_LO:
10969 case R_PPC64_PLTGOT16_LO_DS:
10970 case R_PPC64_PLTREL32:
10971 case R_PPC64_PLTREL64:
10972 /* These ones haven't been implemented yet. */
10974 (*_bfd_error_handler)
10975 (_("%B: relocation %s is not supported for symbol %s."),
10976 input_bfd,
10977 ppc64_elf_howto_table[r_type]->name, sym_name);
10979 bfd_set_error (bfd_error_invalid_operation);
10980 ret = FALSE;
10981 continue;
10984 /* Do any further special processing. */
10985 switch (r_type)
10987 default:
10988 break;
10990 case R_PPC64_ADDR16_HA:
10991 case R_PPC64_ADDR16_HIGHERA:
10992 case R_PPC64_ADDR16_HIGHESTA:
10993 case R_PPC64_TOC16_HA:
10994 case R_PPC64_SECTOFF_HA:
10995 case R_PPC64_TPREL16_HA:
10996 case R_PPC64_DTPREL16_HA:
10997 case R_PPC64_TPREL16_HIGHER:
10998 case R_PPC64_TPREL16_HIGHERA:
10999 case R_PPC64_TPREL16_HIGHEST:
11000 case R_PPC64_TPREL16_HIGHESTA:
11001 case R_PPC64_DTPREL16_HIGHER:
11002 case R_PPC64_DTPREL16_HIGHERA:
11003 case R_PPC64_DTPREL16_HIGHEST:
11004 case R_PPC64_DTPREL16_HIGHESTA:
11005 /* It's just possible that this symbol is a weak symbol
11006 that's not actually defined anywhere. In that case,
11007 'sec' would be NULL, and we should leave the symbol
11008 alone (it will be set to zero elsewhere in the link). */
11009 if (sec == NULL)
11010 break;
11011 /* Fall thru */
11013 case R_PPC64_GOT16_HA:
11014 case R_PPC64_PLTGOT16_HA:
11015 case R_PPC64_PLT16_HA:
11016 case R_PPC64_GOT_TLSGD16_HA:
11017 case R_PPC64_GOT_TLSLD16_HA:
11018 case R_PPC64_GOT_TPREL16_HA:
11019 case R_PPC64_GOT_DTPREL16_HA:
11020 /* Add 0x10000 if sign bit in 0:15 is set.
11021 Bits 0:15 are not used. */
11022 addend += 0x8000;
11023 break;
11025 case R_PPC64_ADDR16_DS:
11026 case R_PPC64_ADDR16_LO_DS:
11027 case R_PPC64_GOT16_DS:
11028 case R_PPC64_GOT16_LO_DS:
11029 case R_PPC64_PLT16_LO_DS:
11030 case R_PPC64_SECTOFF_DS:
11031 case R_PPC64_SECTOFF_LO_DS:
11032 case R_PPC64_TOC16_DS:
11033 case R_PPC64_TOC16_LO_DS:
11034 case R_PPC64_PLTGOT16_DS:
11035 case R_PPC64_PLTGOT16_LO_DS:
11036 case R_PPC64_GOT_TPREL16_DS:
11037 case R_PPC64_GOT_TPREL16_LO_DS:
11038 case R_PPC64_GOT_DTPREL16_DS:
11039 case R_PPC64_GOT_DTPREL16_LO_DS:
11040 case R_PPC64_TPREL16_DS:
11041 case R_PPC64_TPREL16_LO_DS:
11042 case R_PPC64_DTPREL16_DS:
11043 case R_PPC64_DTPREL16_LO_DS:
11044 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
11045 mask = 3;
11046 /* If this reloc is against an lq insn, then the value must be
11047 a multiple of 16. This is somewhat of a hack, but the
11048 "correct" way to do this by defining _DQ forms of all the
11049 _DS relocs bloats all reloc switches in this file. It
11050 doesn't seem to make much sense to use any of these relocs
11051 in data, so testing the insn should be safe. */
11052 if ((insn & (0x3f << 26)) == (56u << 26))
11053 mask = 15;
11054 if (((relocation + addend) & mask) != 0)
11056 (*_bfd_error_handler)
11057 (_("%B: error: relocation %s not a multiple of %d"),
11058 input_bfd,
11059 ppc64_elf_howto_table[r_type]->name,
11060 mask + 1);
11061 bfd_set_error (bfd_error_bad_value);
11062 ret = FALSE;
11063 continue;
11065 break;
11068 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
11069 because such sections are not SEC_ALLOC and thus ld.so will
11070 not process them. */
11071 if (unresolved_reloc
11072 && !((input_section->flags & SEC_DEBUGGING) != 0
11073 && h->elf.def_dynamic))
11075 (*_bfd_error_handler)
11076 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
11077 input_bfd,
11078 input_section,
11079 (long) rel->r_offset,
11080 ppc64_elf_howto_table[(int) r_type]->name,
11081 h->elf.root.root.string);
11082 ret = FALSE;
11085 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
11086 input_bfd,
11087 input_section,
11088 contents,
11089 rel->r_offset,
11090 relocation,
11091 addend);
11093 if (r != bfd_reloc_ok)
11095 if (sym_name == NULL)
11096 sym_name = "(null)";
11097 if (r == bfd_reloc_overflow)
11099 if (warned)
11100 continue;
11101 if (h != NULL
11102 && h->elf.root.type == bfd_link_hash_undefweak
11103 && ppc64_elf_howto_table[r_type]->pc_relative)
11105 /* Assume this is a call protected by other code that
11106 detects the symbol is undefined. If this is the case,
11107 we can safely ignore the overflow. If not, the
11108 program is hosed anyway, and a little warning isn't
11109 going to help. */
11111 continue;
11114 if (!((*info->callbacks->reloc_overflow)
11115 (info, (h ? &h->elf.root : NULL), sym_name,
11116 ppc64_elf_howto_table[r_type]->name,
11117 orig_addend, input_bfd, input_section, rel->r_offset)))
11118 return FALSE;
11120 else
11122 (*_bfd_error_handler)
11123 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
11124 input_bfd,
11125 input_section,
11126 (long) rel->r_offset,
11127 ppc64_elf_howto_table[r_type]->name,
11128 sym_name,
11129 (int) r);
11130 ret = FALSE;
11135 /* If we're emitting relocations, then shortly after this function
11136 returns, reloc offsets and addends for this section will be
11137 adjusted. Worse, reloc symbol indices will be for the output
11138 file rather than the input. Save a copy of the relocs for
11139 opd_entry_value. */
11140 if (is_opd && (info->emitrelocations || info->relocatable))
11142 bfd_size_type amt;
11143 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
11144 rel = bfd_alloc (input_bfd, amt);
11145 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
11146 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
11147 if (rel == NULL)
11148 return FALSE;
11149 memcpy (rel, relocs, amt);
11151 return ret;
11154 /* Adjust the value of any local symbols in opd sections. */
11156 static bfd_boolean
11157 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
11158 const char *name ATTRIBUTE_UNUSED,
11159 Elf_Internal_Sym *elfsym,
11160 asection *input_sec,
11161 struct elf_link_hash_entry *h)
11163 long *opd_adjust, adjust;
11164 bfd_vma value;
11166 if (h != NULL)
11167 return TRUE;
11169 opd_adjust = get_opd_info (input_sec);
11170 if (opd_adjust == NULL)
11171 return TRUE;
11173 value = elfsym->st_value - input_sec->output_offset;
11174 if (!info->relocatable)
11175 value -= input_sec->output_section->vma;
11177 adjust = opd_adjust[value / 8];
11178 if (adjust == -1)
11179 elfsym->st_value = 0;
11180 else
11181 elfsym->st_value += adjust;
11182 return TRUE;
11185 /* Finish up dynamic symbol handling. We set the contents of various
11186 dynamic sections here. */
11188 static bfd_boolean
11189 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
11190 struct bfd_link_info *info,
11191 struct elf_link_hash_entry *h,
11192 Elf_Internal_Sym *sym)
11194 struct ppc_link_hash_table *htab;
11195 struct plt_entry *ent;
11196 Elf_Internal_Rela rela;
11197 bfd_byte *loc;
11199 htab = ppc_hash_table (info);
11201 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
11202 if (ent->plt.offset != (bfd_vma) -1)
11204 /* This symbol has an entry in the procedure linkage
11205 table. Set it up. */
11207 if (htab->plt == NULL
11208 || htab->relplt == NULL
11209 || htab->glink == NULL)
11210 abort ();
11212 /* Create a JMP_SLOT reloc to inform the dynamic linker to
11213 fill in the PLT entry. */
11214 rela.r_offset = (htab->plt->output_section->vma
11215 + htab->plt->output_offset
11216 + ent->plt.offset);
11217 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
11218 rela.r_addend = ent->addend;
11220 loc = htab->relplt->contents;
11221 loc += ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
11222 * sizeof (Elf64_External_Rela));
11223 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
11226 if (h->needs_copy)
11228 Elf_Internal_Rela rela;
11229 bfd_byte *loc;
11231 /* This symbol needs a copy reloc. Set it up. */
11233 if (h->dynindx == -1
11234 || (h->root.type != bfd_link_hash_defined
11235 && h->root.type != bfd_link_hash_defweak)
11236 || htab->relbss == NULL)
11237 abort ();
11239 rela.r_offset = (h->root.u.def.value
11240 + h->root.u.def.section->output_section->vma
11241 + h->root.u.def.section->output_offset);
11242 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
11243 rela.r_addend = 0;
11244 loc = htab->relbss->contents;
11245 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
11246 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
11249 /* Mark some specially defined symbols as absolute. */
11250 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
11251 sym->st_shndx = SHN_ABS;
11253 return TRUE;
11256 /* Used to decide how to sort relocs in an optimal manner for the
11257 dynamic linker, before writing them out. */
11259 static enum elf_reloc_type_class
11260 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
11262 enum elf_ppc64_reloc_type r_type;
11264 r_type = ELF64_R_TYPE (rela->r_info);
11265 switch (r_type)
11267 case R_PPC64_RELATIVE:
11268 return reloc_class_relative;
11269 case R_PPC64_JMP_SLOT:
11270 return reloc_class_plt;
11271 case R_PPC64_COPY:
11272 return reloc_class_copy;
11273 default:
11274 return reloc_class_normal;
11278 /* Finish up the dynamic sections. */
11280 static bfd_boolean
11281 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
11282 struct bfd_link_info *info)
11284 struct ppc_link_hash_table *htab;
11285 bfd *dynobj;
11286 asection *sdyn;
11288 htab = ppc_hash_table (info);
11289 dynobj = htab->elf.dynobj;
11290 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
11292 if (htab->elf.dynamic_sections_created)
11294 Elf64_External_Dyn *dyncon, *dynconend;
11296 if (sdyn == NULL || htab->got == NULL)
11297 abort ();
11299 dyncon = (Elf64_External_Dyn *) sdyn->contents;
11300 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
11301 for (; dyncon < dynconend; dyncon++)
11303 Elf_Internal_Dyn dyn;
11304 asection *s;
11306 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
11308 switch (dyn.d_tag)
11310 default:
11311 continue;
11313 case DT_PPC64_GLINK:
11314 s = htab->glink;
11315 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11316 /* We stupidly defined DT_PPC64_GLINK to be the start
11317 of glink rather than the first entry point, which is
11318 what ld.so needs, and now have a bigger stub to
11319 support automatic multiple TOCs. */
11320 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
11321 break;
11323 case DT_PPC64_OPD:
11324 s = bfd_get_section_by_name (output_bfd, ".opd");
11325 if (s == NULL)
11326 continue;
11327 dyn.d_un.d_ptr = s->vma;
11328 break;
11330 case DT_PPC64_OPDSZ:
11331 s = bfd_get_section_by_name (output_bfd, ".opd");
11332 if (s == NULL)
11333 continue;
11334 dyn.d_un.d_val = s->size;
11335 break;
11337 case DT_PLTGOT:
11338 s = htab->plt;
11339 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11340 break;
11342 case DT_JMPREL:
11343 s = htab->relplt;
11344 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11345 break;
11347 case DT_PLTRELSZ:
11348 dyn.d_un.d_val = htab->relplt->size;
11349 break;
11351 case DT_RELASZ:
11352 /* Don't count procedure linkage table relocs in the
11353 overall reloc count. */
11354 s = htab->relplt;
11355 if (s == NULL)
11356 continue;
11357 dyn.d_un.d_val -= s->size;
11358 break;
11360 case DT_RELA:
11361 /* We may not be using the standard ELF linker script.
11362 If .rela.plt is the first .rela section, we adjust
11363 DT_RELA to not include it. */
11364 s = htab->relplt;
11365 if (s == NULL)
11366 continue;
11367 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
11368 continue;
11369 dyn.d_un.d_ptr += s->size;
11370 break;
11373 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
11377 if (htab->got != NULL && htab->got->size != 0)
11379 /* Fill in the first entry in the global offset table.
11380 We use it to hold the link-time TOCbase. */
11381 bfd_put_64 (output_bfd,
11382 elf_gp (output_bfd) + TOC_BASE_OFF,
11383 htab->got->contents);
11385 /* Set .got entry size. */
11386 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
11389 if (htab->plt != NULL && htab->plt->size != 0)
11391 /* Set .plt entry size. */
11392 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
11393 = PLT_ENTRY_SIZE;
11396 /* We need to handle writing out multiple GOT sections ourselves,
11397 since we didn't add them to DYNOBJ. We know dynobj is the first
11398 bfd. */
11399 while ((dynobj = dynobj->link_next) != NULL)
11401 asection *s;
11403 if (!is_ppc64_elf_target (dynobj->xvec))
11404 continue;
11406 s = ppc64_elf_tdata (dynobj)->got;
11407 if (s != NULL
11408 && s->size != 0
11409 && s->output_section != bfd_abs_section_ptr
11410 && !bfd_set_section_contents (output_bfd, s->output_section,
11411 s->contents, s->output_offset,
11412 s->size))
11413 return FALSE;
11414 s = ppc64_elf_tdata (dynobj)->relgot;
11415 if (s != NULL
11416 && s->size != 0
11417 && s->output_section != bfd_abs_section_ptr
11418 && !bfd_set_section_contents (output_bfd, s->output_section,
11419 s->contents, s->output_offset,
11420 s->size))
11421 return FALSE;
11424 return TRUE;
11427 #include "elf64-target.h"