2006-01-27 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / bfd / elf64-ppc.c
blob3a807ebb27c0fbc2e4af683f1cf7f69442fdbf35
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_info_to_howto ppc64_elf_info_to_howto
66 #define elf_backend_want_got_sym 0
67 #define elf_backend_want_plt_sym 0
68 #define elf_backend_plt_alignment 3
69 #define elf_backend_plt_not_loaded 1
70 #define elf_backend_got_header_size 8
71 #define elf_backend_can_gc_sections 1
72 #define elf_backend_can_refcount 1
73 #define elf_backend_rela_normal 1
75 #define bfd_elf64_mkobject ppc64_elf_mkobject
76 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
77 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
78 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
79 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
80 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
81 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
83 #define elf_backend_object_p ppc64_elf_object_p
84 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
85 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
86 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
87 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
88 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
89 #define elf_backend_check_directives ppc64_elf_check_directives
90 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
91 #define elf_backend_check_relocs ppc64_elf_check_relocs
92 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
93 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
94 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
95 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
96 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
97 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
98 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
99 #define elf_backend_action_discarded ppc64_elf_action_discarded
100 #define elf_backend_relocate_section ppc64_elf_relocate_section
101 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
102 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
103 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
104 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
105 #define elf_backend_special_sections ppc64_elf_special_sections
107 /* The name of the dynamic interpreter. This is put in the .interp
108 section. */
109 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
111 /* The size in bytes of an entry in the procedure linkage table. */
112 #define PLT_ENTRY_SIZE 24
114 /* The initial size of the plt reserved for the dynamic linker. */
115 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
117 /* TOC base pointers offset from start of TOC. */
118 #define TOC_BASE_OFF 0x8000
120 /* Offset of tp and dtp pointers from start of TLS block. */
121 #define TP_OFFSET 0x7000
122 #define DTP_OFFSET 0x8000
124 /* .plt call stub instructions. The normal stub is like this, but
125 sometimes the .plt entry crosses a 64k boundary and we need to
126 insert an addis to adjust r12. */
127 #define PLT_CALL_STUB_SIZE (7*4)
128 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
129 #define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
130 #define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
131 #define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
132 #define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
133 /* ld %r11,xxx+16@l(%r12) */
134 #define BCTR 0x4e800420 /* bctr */
137 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
138 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
140 #define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
142 /* glink call stub instructions. We enter with the index in R0, and the
143 address of glink entry in CTR. From that, we can calculate PLT0. */
144 #define GLINK_CALL_STUB_SIZE (16*4)
145 #define MFCTR_R12 0x7d8902a6 /* mfctr %r12 */
146 #define SLDI_R11_R0_3 0x780b1f24 /* sldi %r11,%r0,3 */
147 #define ADDIC_R2_R0_32K 0x34408000 /* addic. %r2,%r0,-32768 */
148 #define SUB_R12_R12_R11 0x7d8b6050 /* sub %r12,%r12,%r11 */
149 #define SRADI_R2_R2_63 0x7c42fe76 /* sradi %r2,%r2,63 */
150 #define SLDI_R11_R0_2 0x780b1764 /* sldi %r11,%r0,2 */
151 #define AND_R2_R2_R11 0x7c425838 /* and %r2,%r2,%r11 */
152 /* sub %r12,%r12,%r11 */
153 #define ADD_R12_R12_R2 0x7d8c1214 /* add %r12,%r12,%r2 */
154 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
155 /* ld %r11,xxx@l(%r12) */
156 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,xxx@l */
157 /* ld %r2,8(%r12) */
158 /* mtctr %r11 */
159 /* ld %r11,16(%r12) */
160 /* bctr */
162 /* Pad with this. */
163 #define NOP 0x60000000
165 /* Some other nops. */
166 #define CROR_151515 0x4def7b82
167 #define CROR_313131 0x4ffffb82
169 /* .glink entries for the first 32k functions are two instructions. */
170 #define LI_R0_0 0x38000000 /* li %r0,0 */
171 #define B_DOT 0x48000000 /* b . */
173 /* After that, we need two instructions to load the index, followed by
174 a branch. */
175 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
176 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
178 /* Instructions used by the save and restore reg functions. */
179 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
180 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
181 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
182 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
183 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
184 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
185 #define LI_R12_0 0x39800000 /* li %r12,0 */
186 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
187 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
188 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
189 #define BLR 0x4e800020 /* blr */
191 /* Since .opd is an array of descriptors and each entry will end up
192 with identical R_PPC64_RELATIVE relocs, there is really no need to
193 propagate .opd relocs; The dynamic linker should be taught to
194 relocate .opd without reloc entries. */
195 #ifndef NO_OPD_RELOCS
196 #define NO_OPD_RELOCS 0
197 #endif
199 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
201 /* Relocation HOWTO's. */
202 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
204 static reloc_howto_type ppc64_elf_howto_raw[] = {
205 /* This reloc does nothing. */
206 HOWTO (R_PPC64_NONE, /* type */
207 0, /* rightshift */
208 2, /* size (0 = byte, 1 = short, 2 = long) */
209 32, /* bitsize */
210 FALSE, /* pc_relative */
211 0, /* bitpos */
212 complain_overflow_dont, /* complain_on_overflow */
213 bfd_elf_generic_reloc, /* special_function */
214 "R_PPC64_NONE", /* name */
215 FALSE, /* partial_inplace */
216 0, /* src_mask */
217 0, /* dst_mask */
218 FALSE), /* pcrel_offset */
220 /* A standard 32 bit relocation. */
221 HOWTO (R_PPC64_ADDR32, /* type */
222 0, /* rightshift */
223 2, /* size (0 = byte, 1 = short, 2 = long) */
224 32, /* bitsize */
225 FALSE, /* pc_relative */
226 0, /* bitpos */
227 complain_overflow_bitfield, /* complain_on_overflow */
228 bfd_elf_generic_reloc, /* special_function */
229 "R_PPC64_ADDR32", /* name */
230 FALSE, /* partial_inplace */
231 0, /* src_mask */
232 0xffffffff, /* dst_mask */
233 FALSE), /* pcrel_offset */
235 /* An absolute 26 bit branch; the lower two bits must be zero.
236 FIXME: we don't check that, we just clear them. */
237 HOWTO (R_PPC64_ADDR24, /* type */
238 0, /* rightshift */
239 2, /* size (0 = byte, 1 = short, 2 = long) */
240 26, /* bitsize */
241 FALSE, /* pc_relative */
242 0, /* bitpos */
243 complain_overflow_bitfield, /* complain_on_overflow */
244 bfd_elf_generic_reloc, /* special_function */
245 "R_PPC64_ADDR24", /* name */
246 FALSE, /* partial_inplace */
247 0, /* src_mask */
248 0x03fffffc, /* dst_mask */
249 FALSE), /* pcrel_offset */
251 /* A standard 16 bit relocation. */
252 HOWTO (R_PPC64_ADDR16, /* type */
253 0, /* rightshift */
254 1, /* size (0 = byte, 1 = short, 2 = long) */
255 16, /* bitsize */
256 FALSE, /* pc_relative */
257 0, /* bitpos */
258 complain_overflow_bitfield, /* complain_on_overflow */
259 bfd_elf_generic_reloc, /* special_function */
260 "R_PPC64_ADDR16", /* name */
261 FALSE, /* partial_inplace */
262 0, /* src_mask */
263 0xffff, /* dst_mask */
264 FALSE), /* pcrel_offset */
266 /* A 16 bit relocation without overflow. */
267 HOWTO (R_PPC64_ADDR16_LO, /* type */
268 0, /* rightshift */
269 1, /* size (0 = byte, 1 = short, 2 = long) */
270 16, /* bitsize */
271 FALSE, /* pc_relative */
272 0, /* bitpos */
273 complain_overflow_dont,/* complain_on_overflow */
274 bfd_elf_generic_reloc, /* special_function */
275 "R_PPC64_ADDR16_LO", /* name */
276 FALSE, /* partial_inplace */
277 0, /* src_mask */
278 0xffff, /* dst_mask */
279 FALSE), /* pcrel_offset */
281 /* Bits 16-31 of an address. */
282 HOWTO (R_PPC64_ADDR16_HI, /* type */
283 16, /* rightshift */
284 1, /* size (0 = byte, 1 = short, 2 = long) */
285 16, /* bitsize */
286 FALSE, /* pc_relative */
287 0, /* bitpos */
288 complain_overflow_dont, /* complain_on_overflow */
289 bfd_elf_generic_reloc, /* special_function */
290 "R_PPC64_ADDR16_HI", /* name */
291 FALSE, /* partial_inplace */
292 0, /* src_mask */
293 0xffff, /* dst_mask */
294 FALSE), /* pcrel_offset */
296 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
297 bits, treated as a signed number, is negative. */
298 HOWTO (R_PPC64_ADDR16_HA, /* type */
299 16, /* rightshift */
300 1, /* size (0 = byte, 1 = short, 2 = long) */
301 16, /* bitsize */
302 FALSE, /* pc_relative */
303 0, /* bitpos */
304 complain_overflow_dont, /* complain_on_overflow */
305 ppc64_elf_ha_reloc, /* special_function */
306 "R_PPC64_ADDR16_HA", /* name */
307 FALSE, /* partial_inplace */
308 0, /* src_mask */
309 0xffff, /* dst_mask */
310 FALSE), /* pcrel_offset */
312 /* An absolute 16 bit branch; the lower two bits must be zero.
313 FIXME: we don't check that, we just clear them. */
314 HOWTO (R_PPC64_ADDR14, /* type */
315 0, /* rightshift */
316 2, /* size (0 = byte, 1 = short, 2 = long) */
317 16, /* bitsize */
318 FALSE, /* pc_relative */
319 0, /* bitpos */
320 complain_overflow_bitfield, /* complain_on_overflow */
321 ppc64_elf_branch_reloc, /* special_function */
322 "R_PPC64_ADDR14", /* name */
323 FALSE, /* partial_inplace */
324 0, /* src_mask */
325 0x0000fffc, /* dst_mask */
326 FALSE), /* pcrel_offset */
328 /* An absolute 16 bit branch, for which bit 10 should be set to
329 indicate that the branch is expected to be taken. The lower two
330 bits must be zero. */
331 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
332 0, /* rightshift */
333 2, /* size (0 = byte, 1 = short, 2 = long) */
334 16, /* bitsize */
335 FALSE, /* pc_relative */
336 0, /* bitpos */
337 complain_overflow_bitfield, /* complain_on_overflow */
338 ppc64_elf_brtaken_reloc, /* special_function */
339 "R_PPC64_ADDR14_BRTAKEN",/* name */
340 FALSE, /* partial_inplace */
341 0, /* src_mask */
342 0x0000fffc, /* dst_mask */
343 FALSE), /* pcrel_offset */
345 /* An absolute 16 bit branch, for which bit 10 should be set to
346 indicate that the branch is not expected to be taken. The lower
347 two bits must be zero. */
348 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
349 0, /* rightshift */
350 2, /* size (0 = byte, 1 = short, 2 = long) */
351 16, /* bitsize */
352 FALSE, /* pc_relative */
353 0, /* bitpos */
354 complain_overflow_bitfield, /* complain_on_overflow */
355 ppc64_elf_brtaken_reloc, /* special_function */
356 "R_PPC64_ADDR14_BRNTAKEN",/* name */
357 FALSE, /* partial_inplace */
358 0, /* src_mask */
359 0x0000fffc, /* dst_mask */
360 FALSE), /* pcrel_offset */
362 /* A relative 26 bit branch; the lower two bits must be zero. */
363 HOWTO (R_PPC64_REL24, /* type */
364 0, /* rightshift */
365 2, /* size (0 = byte, 1 = short, 2 = long) */
366 26, /* bitsize */
367 TRUE, /* pc_relative */
368 0, /* bitpos */
369 complain_overflow_signed, /* complain_on_overflow */
370 ppc64_elf_branch_reloc, /* special_function */
371 "R_PPC64_REL24", /* name */
372 FALSE, /* partial_inplace */
373 0, /* src_mask */
374 0x03fffffc, /* dst_mask */
375 TRUE), /* pcrel_offset */
377 /* A relative 16 bit branch; the lower two bits must be zero. */
378 HOWTO (R_PPC64_REL14, /* type */
379 0, /* rightshift */
380 2, /* size (0 = byte, 1 = short, 2 = long) */
381 16, /* bitsize */
382 TRUE, /* pc_relative */
383 0, /* bitpos */
384 complain_overflow_signed, /* complain_on_overflow */
385 ppc64_elf_branch_reloc, /* special_function */
386 "R_PPC64_REL14", /* name */
387 FALSE, /* partial_inplace */
388 0, /* src_mask */
389 0x0000fffc, /* dst_mask */
390 TRUE), /* pcrel_offset */
392 /* A relative 16 bit branch. Bit 10 should be set to indicate that
393 the branch is expected to be taken. The lower two bits must be
394 zero. */
395 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
396 0, /* rightshift */
397 2, /* size (0 = byte, 1 = short, 2 = long) */
398 16, /* bitsize */
399 TRUE, /* pc_relative */
400 0, /* bitpos */
401 complain_overflow_signed, /* complain_on_overflow */
402 ppc64_elf_brtaken_reloc, /* special_function */
403 "R_PPC64_REL14_BRTAKEN", /* name */
404 FALSE, /* partial_inplace */
405 0, /* src_mask */
406 0x0000fffc, /* dst_mask */
407 TRUE), /* pcrel_offset */
409 /* A relative 16 bit branch. Bit 10 should be set to indicate that
410 the branch is not expected to be taken. The lower two bits must
411 be zero. */
412 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
413 0, /* rightshift */
414 2, /* size (0 = byte, 1 = short, 2 = long) */
415 16, /* bitsize */
416 TRUE, /* pc_relative */
417 0, /* bitpos */
418 complain_overflow_signed, /* complain_on_overflow */
419 ppc64_elf_brtaken_reloc, /* special_function */
420 "R_PPC64_REL14_BRNTAKEN",/* name */
421 FALSE, /* partial_inplace */
422 0, /* src_mask */
423 0x0000fffc, /* dst_mask */
424 TRUE), /* pcrel_offset */
426 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
427 symbol. */
428 HOWTO (R_PPC64_GOT16, /* type */
429 0, /* rightshift */
430 1, /* size (0 = byte, 1 = short, 2 = long) */
431 16, /* bitsize */
432 FALSE, /* pc_relative */
433 0, /* bitpos */
434 complain_overflow_signed, /* complain_on_overflow */
435 ppc64_elf_unhandled_reloc, /* special_function */
436 "R_PPC64_GOT16", /* name */
437 FALSE, /* partial_inplace */
438 0, /* src_mask */
439 0xffff, /* dst_mask */
440 FALSE), /* pcrel_offset */
442 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
443 the symbol. */
444 HOWTO (R_PPC64_GOT16_LO, /* type */
445 0, /* rightshift */
446 1, /* size (0 = byte, 1 = short, 2 = long) */
447 16, /* bitsize */
448 FALSE, /* pc_relative */
449 0, /* bitpos */
450 complain_overflow_dont, /* complain_on_overflow */
451 ppc64_elf_unhandled_reloc, /* special_function */
452 "R_PPC64_GOT16_LO", /* name */
453 FALSE, /* partial_inplace */
454 0, /* src_mask */
455 0xffff, /* dst_mask */
456 FALSE), /* pcrel_offset */
458 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
459 the symbol. */
460 HOWTO (R_PPC64_GOT16_HI, /* type */
461 16, /* rightshift */
462 1, /* size (0 = byte, 1 = short, 2 = long) */
463 16, /* bitsize */
464 FALSE, /* pc_relative */
465 0, /* bitpos */
466 complain_overflow_dont,/* complain_on_overflow */
467 ppc64_elf_unhandled_reloc, /* special_function */
468 "R_PPC64_GOT16_HI", /* name */
469 FALSE, /* partial_inplace */
470 0, /* src_mask */
471 0xffff, /* dst_mask */
472 FALSE), /* pcrel_offset */
474 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
475 the symbol. */
476 HOWTO (R_PPC64_GOT16_HA, /* type */
477 16, /* rightshift */
478 1, /* size (0 = byte, 1 = short, 2 = long) */
479 16, /* bitsize */
480 FALSE, /* pc_relative */
481 0, /* bitpos */
482 complain_overflow_dont,/* complain_on_overflow */
483 ppc64_elf_unhandled_reloc, /* special_function */
484 "R_PPC64_GOT16_HA", /* name */
485 FALSE, /* partial_inplace */
486 0, /* src_mask */
487 0xffff, /* dst_mask */
488 FALSE), /* pcrel_offset */
490 /* This is used only by the dynamic linker. The symbol should exist
491 both in the object being run and in some shared library. The
492 dynamic linker copies the data addressed by the symbol from the
493 shared library into the object, because the object being
494 run has to have the data at some particular address. */
495 HOWTO (R_PPC64_COPY, /* type */
496 0, /* rightshift */
497 0, /* this one is variable size */
498 0, /* bitsize */
499 FALSE, /* pc_relative */
500 0, /* bitpos */
501 complain_overflow_dont, /* complain_on_overflow */
502 ppc64_elf_unhandled_reloc, /* special_function */
503 "R_PPC64_COPY", /* name */
504 FALSE, /* partial_inplace */
505 0, /* src_mask */
506 0, /* dst_mask */
507 FALSE), /* pcrel_offset */
509 /* Like R_PPC64_ADDR64, but used when setting global offset table
510 entries. */
511 HOWTO (R_PPC64_GLOB_DAT, /* type */
512 0, /* rightshift */
513 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
514 64, /* bitsize */
515 FALSE, /* pc_relative */
516 0, /* bitpos */
517 complain_overflow_dont, /* complain_on_overflow */
518 ppc64_elf_unhandled_reloc, /* special_function */
519 "R_PPC64_GLOB_DAT", /* name */
520 FALSE, /* partial_inplace */
521 0, /* src_mask */
522 ONES (64), /* dst_mask */
523 FALSE), /* pcrel_offset */
525 /* Created by the link editor. Marks a procedure linkage table
526 entry for a symbol. */
527 HOWTO (R_PPC64_JMP_SLOT, /* type */
528 0, /* rightshift */
529 0, /* size (0 = byte, 1 = short, 2 = long) */
530 0, /* bitsize */
531 FALSE, /* pc_relative */
532 0, /* bitpos */
533 complain_overflow_dont, /* complain_on_overflow */
534 ppc64_elf_unhandled_reloc, /* special_function */
535 "R_PPC64_JMP_SLOT", /* name */
536 FALSE, /* partial_inplace */
537 0, /* src_mask */
538 0, /* dst_mask */
539 FALSE), /* pcrel_offset */
541 /* Used only by the dynamic linker. When the object is run, this
542 doubleword64 is set to the load address of the object, plus the
543 addend. */
544 HOWTO (R_PPC64_RELATIVE, /* type */
545 0, /* rightshift */
546 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
547 64, /* bitsize */
548 FALSE, /* pc_relative */
549 0, /* bitpos */
550 complain_overflow_dont, /* complain_on_overflow */
551 bfd_elf_generic_reloc, /* special_function */
552 "R_PPC64_RELATIVE", /* name */
553 FALSE, /* partial_inplace */
554 0, /* src_mask */
555 ONES (64), /* dst_mask */
556 FALSE), /* pcrel_offset */
558 /* Like R_PPC64_ADDR32, but may be unaligned. */
559 HOWTO (R_PPC64_UADDR32, /* type */
560 0, /* rightshift */
561 2, /* size (0 = byte, 1 = short, 2 = long) */
562 32, /* bitsize */
563 FALSE, /* pc_relative */
564 0, /* bitpos */
565 complain_overflow_bitfield, /* complain_on_overflow */
566 bfd_elf_generic_reloc, /* special_function */
567 "R_PPC64_UADDR32", /* name */
568 FALSE, /* partial_inplace */
569 0, /* src_mask */
570 0xffffffff, /* dst_mask */
571 FALSE), /* pcrel_offset */
573 /* Like R_PPC64_ADDR16, but may be unaligned. */
574 HOWTO (R_PPC64_UADDR16, /* type */
575 0, /* rightshift */
576 1, /* size (0 = byte, 1 = short, 2 = long) */
577 16, /* bitsize */
578 FALSE, /* pc_relative */
579 0, /* bitpos */
580 complain_overflow_bitfield, /* complain_on_overflow */
581 bfd_elf_generic_reloc, /* special_function */
582 "R_PPC64_UADDR16", /* name */
583 FALSE, /* partial_inplace */
584 0, /* src_mask */
585 0xffff, /* dst_mask */
586 FALSE), /* pcrel_offset */
588 /* 32-bit PC relative. */
589 HOWTO (R_PPC64_REL32, /* type */
590 0, /* rightshift */
591 2, /* size (0 = byte, 1 = short, 2 = long) */
592 32, /* bitsize */
593 TRUE, /* pc_relative */
594 0, /* bitpos */
595 /* FIXME: Verify. Was complain_overflow_bitfield. */
596 complain_overflow_signed, /* complain_on_overflow */
597 bfd_elf_generic_reloc, /* special_function */
598 "R_PPC64_REL32", /* name */
599 FALSE, /* partial_inplace */
600 0, /* src_mask */
601 0xffffffff, /* dst_mask */
602 TRUE), /* pcrel_offset */
604 /* 32-bit relocation to the symbol's procedure linkage table. */
605 HOWTO (R_PPC64_PLT32, /* type */
606 0, /* rightshift */
607 2, /* size (0 = byte, 1 = short, 2 = long) */
608 32, /* bitsize */
609 FALSE, /* pc_relative */
610 0, /* bitpos */
611 complain_overflow_bitfield, /* complain_on_overflow */
612 ppc64_elf_unhandled_reloc, /* special_function */
613 "R_PPC64_PLT32", /* name */
614 FALSE, /* partial_inplace */
615 0, /* src_mask */
616 0xffffffff, /* dst_mask */
617 FALSE), /* pcrel_offset */
619 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
620 FIXME: R_PPC64_PLTREL32 not supported. */
621 HOWTO (R_PPC64_PLTREL32, /* type */
622 0, /* rightshift */
623 2, /* size (0 = byte, 1 = short, 2 = long) */
624 32, /* bitsize */
625 TRUE, /* pc_relative */
626 0, /* bitpos */
627 complain_overflow_signed, /* complain_on_overflow */
628 bfd_elf_generic_reloc, /* special_function */
629 "R_PPC64_PLTREL32", /* name */
630 FALSE, /* partial_inplace */
631 0, /* src_mask */
632 0xffffffff, /* dst_mask */
633 TRUE), /* pcrel_offset */
635 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
636 the symbol. */
637 HOWTO (R_PPC64_PLT16_LO, /* type */
638 0, /* rightshift */
639 1, /* size (0 = byte, 1 = short, 2 = long) */
640 16, /* bitsize */
641 FALSE, /* pc_relative */
642 0, /* bitpos */
643 complain_overflow_dont, /* complain_on_overflow */
644 ppc64_elf_unhandled_reloc, /* special_function */
645 "R_PPC64_PLT16_LO", /* name */
646 FALSE, /* partial_inplace */
647 0, /* src_mask */
648 0xffff, /* dst_mask */
649 FALSE), /* pcrel_offset */
651 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
652 the symbol. */
653 HOWTO (R_PPC64_PLT16_HI, /* type */
654 16, /* rightshift */
655 1, /* size (0 = byte, 1 = short, 2 = long) */
656 16, /* bitsize */
657 FALSE, /* pc_relative */
658 0, /* bitpos */
659 complain_overflow_dont, /* complain_on_overflow */
660 ppc64_elf_unhandled_reloc, /* special_function */
661 "R_PPC64_PLT16_HI", /* name */
662 FALSE, /* partial_inplace */
663 0, /* src_mask */
664 0xffff, /* dst_mask */
665 FALSE), /* pcrel_offset */
667 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
668 the symbol. */
669 HOWTO (R_PPC64_PLT16_HA, /* type */
670 16, /* rightshift */
671 1, /* size (0 = byte, 1 = short, 2 = long) */
672 16, /* bitsize */
673 FALSE, /* pc_relative */
674 0, /* bitpos */
675 complain_overflow_dont, /* complain_on_overflow */
676 ppc64_elf_unhandled_reloc, /* special_function */
677 "R_PPC64_PLT16_HA", /* name */
678 FALSE, /* partial_inplace */
679 0, /* src_mask */
680 0xffff, /* dst_mask */
681 FALSE), /* pcrel_offset */
683 /* 16-bit section relative relocation. */
684 HOWTO (R_PPC64_SECTOFF, /* type */
685 0, /* rightshift */
686 1, /* size (0 = byte, 1 = short, 2 = long) */
687 16, /* bitsize */
688 FALSE, /* pc_relative */
689 0, /* bitpos */
690 complain_overflow_bitfield, /* complain_on_overflow */
691 ppc64_elf_sectoff_reloc, /* special_function */
692 "R_PPC64_SECTOFF", /* name */
693 FALSE, /* partial_inplace */
694 0, /* src_mask */
695 0xffff, /* dst_mask */
696 FALSE), /* pcrel_offset */
698 /* Like R_PPC64_SECTOFF, but no overflow warning. */
699 HOWTO (R_PPC64_SECTOFF_LO, /* type */
700 0, /* rightshift */
701 1, /* size (0 = byte, 1 = short, 2 = long) */
702 16, /* bitsize */
703 FALSE, /* pc_relative */
704 0, /* bitpos */
705 complain_overflow_dont, /* complain_on_overflow */
706 ppc64_elf_sectoff_reloc, /* special_function */
707 "R_PPC64_SECTOFF_LO", /* name */
708 FALSE, /* partial_inplace */
709 0, /* src_mask */
710 0xffff, /* dst_mask */
711 FALSE), /* pcrel_offset */
713 /* 16-bit upper half section relative relocation. */
714 HOWTO (R_PPC64_SECTOFF_HI, /* type */
715 16, /* rightshift */
716 1, /* size (0 = byte, 1 = short, 2 = long) */
717 16, /* bitsize */
718 FALSE, /* pc_relative */
719 0, /* bitpos */
720 complain_overflow_dont, /* complain_on_overflow */
721 ppc64_elf_sectoff_reloc, /* special_function */
722 "R_PPC64_SECTOFF_HI", /* name */
723 FALSE, /* partial_inplace */
724 0, /* src_mask */
725 0xffff, /* dst_mask */
726 FALSE), /* pcrel_offset */
728 /* 16-bit upper half adjusted section relative relocation. */
729 HOWTO (R_PPC64_SECTOFF_HA, /* type */
730 16, /* rightshift */
731 1, /* size (0 = byte, 1 = short, 2 = long) */
732 16, /* bitsize */
733 FALSE, /* pc_relative */
734 0, /* bitpos */
735 complain_overflow_dont, /* complain_on_overflow */
736 ppc64_elf_sectoff_ha_reloc, /* special_function */
737 "R_PPC64_SECTOFF_HA", /* name */
738 FALSE, /* partial_inplace */
739 0, /* src_mask */
740 0xffff, /* dst_mask */
741 FALSE), /* pcrel_offset */
743 /* Like R_PPC64_REL24 without touching the two least significant bits. */
744 HOWTO (R_PPC64_REL30, /* type */
745 2, /* rightshift */
746 2, /* size (0 = byte, 1 = short, 2 = long) */
747 30, /* bitsize */
748 TRUE, /* pc_relative */
749 0, /* bitpos */
750 complain_overflow_dont, /* complain_on_overflow */
751 bfd_elf_generic_reloc, /* special_function */
752 "R_PPC64_REL30", /* name */
753 FALSE, /* partial_inplace */
754 0, /* src_mask */
755 0xfffffffc, /* dst_mask */
756 TRUE), /* pcrel_offset */
758 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
760 /* A standard 64-bit relocation. */
761 HOWTO (R_PPC64_ADDR64, /* type */
762 0, /* rightshift */
763 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
764 64, /* bitsize */
765 FALSE, /* pc_relative */
766 0, /* bitpos */
767 complain_overflow_dont, /* complain_on_overflow */
768 bfd_elf_generic_reloc, /* special_function */
769 "R_PPC64_ADDR64", /* name */
770 FALSE, /* partial_inplace */
771 0, /* src_mask */
772 ONES (64), /* dst_mask */
773 FALSE), /* pcrel_offset */
775 /* The bits 32-47 of an address. */
776 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
777 32, /* rightshift */
778 1, /* size (0 = byte, 1 = short, 2 = long) */
779 16, /* bitsize */
780 FALSE, /* pc_relative */
781 0, /* bitpos */
782 complain_overflow_dont, /* complain_on_overflow */
783 bfd_elf_generic_reloc, /* special_function */
784 "R_PPC64_ADDR16_HIGHER", /* name */
785 FALSE, /* partial_inplace */
786 0, /* src_mask */
787 0xffff, /* dst_mask */
788 FALSE), /* pcrel_offset */
790 /* The bits 32-47 of an address, plus 1 if the contents of the low
791 16 bits, treated as a signed number, is negative. */
792 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
793 32, /* rightshift */
794 1, /* size (0 = byte, 1 = short, 2 = long) */
795 16, /* bitsize */
796 FALSE, /* pc_relative */
797 0, /* bitpos */
798 complain_overflow_dont, /* complain_on_overflow */
799 ppc64_elf_ha_reloc, /* special_function */
800 "R_PPC64_ADDR16_HIGHERA", /* name */
801 FALSE, /* partial_inplace */
802 0, /* src_mask */
803 0xffff, /* dst_mask */
804 FALSE), /* pcrel_offset */
806 /* The bits 48-63 of an address. */
807 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
808 48, /* rightshift */
809 1, /* size (0 = byte, 1 = short, 2 = long) */
810 16, /* bitsize */
811 FALSE, /* pc_relative */
812 0, /* bitpos */
813 complain_overflow_dont, /* complain_on_overflow */
814 bfd_elf_generic_reloc, /* special_function */
815 "R_PPC64_ADDR16_HIGHEST", /* name */
816 FALSE, /* partial_inplace */
817 0, /* src_mask */
818 0xffff, /* dst_mask */
819 FALSE), /* pcrel_offset */
821 /* The bits 48-63 of an address, plus 1 if the contents of the low
822 16 bits, treated as a signed number, is negative. */
823 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
824 48, /* rightshift */
825 1, /* size (0 = byte, 1 = short, 2 = long) */
826 16, /* bitsize */
827 FALSE, /* pc_relative */
828 0, /* bitpos */
829 complain_overflow_dont, /* complain_on_overflow */
830 ppc64_elf_ha_reloc, /* special_function */
831 "R_PPC64_ADDR16_HIGHESTA", /* name */
832 FALSE, /* partial_inplace */
833 0, /* src_mask */
834 0xffff, /* dst_mask */
835 FALSE), /* pcrel_offset */
837 /* Like ADDR64, but may be unaligned. */
838 HOWTO (R_PPC64_UADDR64, /* type */
839 0, /* rightshift */
840 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
841 64, /* bitsize */
842 FALSE, /* pc_relative */
843 0, /* bitpos */
844 complain_overflow_dont, /* complain_on_overflow */
845 bfd_elf_generic_reloc, /* special_function */
846 "R_PPC64_UADDR64", /* name */
847 FALSE, /* partial_inplace */
848 0, /* src_mask */
849 ONES (64), /* dst_mask */
850 FALSE), /* pcrel_offset */
852 /* 64-bit relative relocation. */
853 HOWTO (R_PPC64_REL64, /* type */
854 0, /* rightshift */
855 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
856 64, /* bitsize */
857 TRUE, /* pc_relative */
858 0, /* bitpos */
859 complain_overflow_dont, /* complain_on_overflow */
860 bfd_elf_generic_reloc, /* special_function */
861 "R_PPC64_REL64", /* name */
862 FALSE, /* partial_inplace */
863 0, /* src_mask */
864 ONES (64), /* dst_mask */
865 TRUE), /* pcrel_offset */
867 /* 64-bit relocation to the symbol's procedure linkage table. */
868 HOWTO (R_PPC64_PLT64, /* type */
869 0, /* rightshift */
870 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
871 64, /* bitsize */
872 FALSE, /* pc_relative */
873 0, /* bitpos */
874 complain_overflow_dont, /* complain_on_overflow */
875 ppc64_elf_unhandled_reloc, /* special_function */
876 "R_PPC64_PLT64", /* name */
877 FALSE, /* partial_inplace */
878 0, /* src_mask */
879 ONES (64), /* dst_mask */
880 FALSE), /* pcrel_offset */
882 /* 64-bit PC relative relocation to the symbol's procedure linkage
883 table. */
884 /* FIXME: R_PPC64_PLTREL64 not supported. */
885 HOWTO (R_PPC64_PLTREL64, /* type */
886 0, /* rightshift */
887 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
888 64, /* bitsize */
889 TRUE, /* pc_relative */
890 0, /* bitpos */
891 complain_overflow_dont, /* complain_on_overflow */
892 ppc64_elf_unhandled_reloc, /* special_function */
893 "R_PPC64_PLTREL64", /* name */
894 FALSE, /* partial_inplace */
895 0, /* src_mask */
896 ONES (64), /* dst_mask */
897 TRUE), /* pcrel_offset */
899 /* 16 bit TOC-relative relocation. */
901 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
902 HOWTO (R_PPC64_TOC16, /* type */
903 0, /* rightshift */
904 1, /* size (0 = byte, 1 = short, 2 = long) */
905 16, /* bitsize */
906 FALSE, /* pc_relative */
907 0, /* bitpos */
908 complain_overflow_signed, /* complain_on_overflow */
909 ppc64_elf_toc_reloc, /* special_function */
910 "R_PPC64_TOC16", /* name */
911 FALSE, /* partial_inplace */
912 0, /* src_mask */
913 0xffff, /* dst_mask */
914 FALSE), /* pcrel_offset */
916 /* 16 bit TOC-relative relocation without overflow. */
918 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
919 HOWTO (R_PPC64_TOC16_LO, /* type */
920 0, /* rightshift */
921 1, /* size (0 = byte, 1 = short, 2 = long) */
922 16, /* bitsize */
923 FALSE, /* pc_relative */
924 0, /* bitpos */
925 complain_overflow_dont, /* complain_on_overflow */
926 ppc64_elf_toc_reloc, /* special_function */
927 "R_PPC64_TOC16_LO", /* name */
928 FALSE, /* partial_inplace */
929 0, /* src_mask */
930 0xffff, /* dst_mask */
931 FALSE), /* pcrel_offset */
933 /* 16 bit TOC-relative relocation, high 16 bits. */
935 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
936 HOWTO (R_PPC64_TOC16_HI, /* type */
937 16, /* rightshift */
938 1, /* size (0 = byte, 1 = short, 2 = long) */
939 16, /* bitsize */
940 FALSE, /* pc_relative */
941 0, /* bitpos */
942 complain_overflow_dont, /* complain_on_overflow */
943 ppc64_elf_toc_reloc, /* special_function */
944 "R_PPC64_TOC16_HI", /* name */
945 FALSE, /* partial_inplace */
946 0, /* src_mask */
947 0xffff, /* dst_mask */
948 FALSE), /* pcrel_offset */
950 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
951 contents of the low 16 bits, treated as a signed number, is
952 negative. */
954 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
955 HOWTO (R_PPC64_TOC16_HA, /* type */
956 16, /* rightshift */
957 1, /* size (0 = byte, 1 = short, 2 = long) */
958 16, /* bitsize */
959 FALSE, /* pc_relative */
960 0, /* bitpos */
961 complain_overflow_dont, /* complain_on_overflow */
962 ppc64_elf_toc_ha_reloc, /* special_function */
963 "R_PPC64_TOC16_HA", /* name */
964 FALSE, /* partial_inplace */
965 0, /* src_mask */
966 0xffff, /* dst_mask */
967 FALSE), /* pcrel_offset */
969 /* 64-bit relocation; insert value of TOC base (.TOC.). */
971 /* R_PPC64_TOC 51 doubleword64 .TOC. */
972 HOWTO (R_PPC64_TOC, /* type */
973 0, /* rightshift */
974 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
975 64, /* bitsize */
976 FALSE, /* pc_relative */
977 0, /* bitpos */
978 complain_overflow_bitfield, /* complain_on_overflow */
979 ppc64_elf_toc64_reloc, /* special_function */
980 "R_PPC64_TOC", /* name */
981 FALSE, /* partial_inplace */
982 0, /* src_mask */
983 ONES (64), /* dst_mask */
984 FALSE), /* pcrel_offset */
986 /* Like R_PPC64_GOT16, but also informs the link editor that the
987 value to relocate may (!) refer to a PLT entry which the link
988 editor (a) may replace with the symbol value. If the link editor
989 is unable to fully resolve the symbol, it may (b) create a PLT
990 entry and store the address to the new PLT entry in the GOT.
991 This permits lazy resolution of function symbols at run time.
992 The link editor may also skip all of this and just (c) emit a
993 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
994 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
995 HOWTO (R_PPC64_PLTGOT16, /* type */
996 0, /* rightshift */
997 1, /* size (0 = byte, 1 = short, 2 = long) */
998 16, /* bitsize */
999 FALSE, /* pc_relative */
1000 0, /* bitpos */
1001 complain_overflow_signed, /* complain_on_overflow */
1002 ppc64_elf_unhandled_reloc, /* special_function */
1003 "R_PPC64_PLTGOT16", /* name */
1004 FALSE, /* partial_inplace */
1005 0, /* src_mask */
1006 0xffff, /* dst_mask */
1007 FALSE), /* pcrel_offset */
1009 /* Like R_PPC64_PLTGOT16, but without overflow. */
1010 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1011 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1012 0, /* rightshift */
1013 1, /* size (0 = byte, 1 = short, 2 = long) */
1014 16, /* bitsize */
1015 FALSE, /* pc_relative */
1016 0, /* bitpos */
1017 complain_overflow_dont, /* complain_on_overflow */
1018 ppc64_elf_unhandled_reloc, /* special_function */
1019 "R_PPC64_PLTGOT16_LO", /* name */
1020 FALSE, /* partial_inplace */
1021 0, /* src_mask */
1022 0xffff, /* dst_mask */
1023 FALSE), /* pcrel_offset */
1025 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1026 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1027 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1028 16, /* rightshift */
1029 1, /* size (0 = byte, 1 = short, 2 = long) */
1030 16, /* bitsize */
1031 FALSE, /* pc_relative */
1032 0, /* bitpos */
1033 complain_overflow_dont, /* complain_on_overflow */
1034 ppc64_elf_unhandled_reloc, /* special_function */
1035 "R_PPC64_PLTGOT16_HI", /* name */
1036 FALSE, /* partial_inplace */
1037 0, /* src_mask */
1038 0xffff, /* dst_mask */
1039 FALSE), /* pcrel_offset */
1041 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1042 1 if the contents of the low 16 bits, treated as a signed number,
1043 is negative. */
1044 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1045 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1046 16, /* rightshift */
1047 1, /* size (0 = byte, 1 = short, 2 = long) */
1048 16, /* bitsize */
1049 FALSE, /* pc_relative */
1050 0, /* bitpos */
1051 complain_overflow_dont,/* complain_on_overflow */
1052 ppc64_elf_unhandled_reloc, /* special_function */
1053 "R_PPC64_PLTGOT16_HA", /* name */
1054 FALSE, /* partial_inplace */
1055 0, /* src_mask */
1056 0xffff, /* dst_mask */
1057 FALSE), /* pcrel_offset */
1059 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1060 HOWTO (R_PPC64_ADDR16_DS, /* type */
1061 0, /* rightshift */
1062 1, /* size (0 = byte, 1 = short, 2 = long) */
1063 16, /* bitsize */
1064 FALSE, /* pc_relative */
1065 0, /* bitpos */
1066 complain_overflow_bitfield, /* complain_on_overflow */
1067 bfd_elf_generic_reloc, /* special_function */
1068 "R_PPC64_ADDR16_DS", /* name */
1069 FALSE, /* partial_inplace */
1070 0, /* src_mask */
1071 0xfffc, /* dst_mask */
1072 FALSE), /* pcrel_offset */
1074 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1075 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1076 0, /* rightshift */
1077 1, /* size (0 = byte, 1 = short, 2 = long) */
1078 16, /* bitsize */
1079 FALSE, /* pc_relative */
1080 0, /* bitpos */
1081 complain_overflow_dont,/* complain_on_overflow */
1082 bfd_elf_generic_reloc, /* special_function */
1083 "R_PPC64_ADDR16_LO_DS",/* name */
1084 FALSE, /* partial_inplace */
1085 0, /* src_mask */
1086 0xfffc, /* dst_mask */
1087 FALSE), /* pcrel_offset */
1089 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1090 HOWTO (R_PPC64_GOT16_DS, /* type */
1091 0, /* rightshift */
1092 1, /* size (0 = byte, 1 = short, 2 = long) */
1093 16, /* bitsize */
1094 FALSE, /* pc_relative */
1095 0, /* bitpos */
1096 complain_overflow_signed, /* complain_on_overflow */
1097 ppc64_elf_unhandled_reloc, /* special_function */
1098 "R_PPC64_GOT16_DS", /* name */
1099 FALSE, /* partial_inplace */
1100 0, /* src_mask */
1101 0xfffc, /* dst_mask */
1102 FALSE), /* pcrel_offset */
1104 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1105 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1106 0, /* rightshift */
1107 1, /* size (0 = byte, 1 = short, 2 = long) */
1108 16, /* bitsize */
1109 FALSE, /* pc_relative */
1110 0, /* bitpos */
1111 complain_overflow_dont, /* complain_on_overflow */
1112 ppc64_elf_unhandled_reloc, /* special_function */
1113 "R_PPC64_GOT16_LO_DS", /* name */
1114 FALSE, /* partial_inplace */
1115 0, /* src_mask */
1116 0xfffc, /* dst_mask */
1117 FALSE), /* pcrel_offset */
1119 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1120 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1121 0, /* rightshift */
1122 1, /* size (0 = byte, 1 = short, 2 = long) */
1123 16, /* bitsize */
1124 FALSE, /* pc_relative */
1125 0, /* bitpos */
1126 complain_overflow_dont, /* complain_on_overflow */
1127 ppc64_elf_unhandled_reloc, /* special_function */
1128 "R_PPC64_PLT16_LO_DS", /* name */
1129 FALSE, /* partial_inplace */
1130 0, /* src_mask */
1131 0xfffc, /* dst_mask */
1132 FALSE), /* pcrel_offset */
1134 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1135 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1136 0, /* rightshift */
1137 1, /* size (0 = byte, 1 = short, 2 = long) */
1138 16, /* bitsize */
1139 FALSE, /* pc_relative */
1140 0, /* bitpos */
1141 complain_overflow_bitfield, /* complain_on_overflow */
1142 ppc64_elf_sectoff_reloc, /* special_function */
1143 "R_PPC64_SECTOFF_DS", /* name */
1144 FALSE, /* partial_inplace */
1145 0, /* src_mask */
1146 0xfffc, /* dst_mask */
1147 FALSE), /* pcrel_offset */
1149 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1150 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1151 0, /* rightshift */
1152 1, /* size (0 = byte, 1 = short, 2 = long) */
1153 16, /* bitsize */
1154 FALSE, /* pc_relative */
1155 0, /* bitpos */
1156 complain_overflow_dont, /* complain_on_overflow */
1157 ppc64_elf_sectoff_reloc, /* special_function */
1158 "R_PPC64_SECTOFF_LO_DS",/* name */
1159 FALSE, /* partial_inplace */
1160 0, /* src_mask */
1161 0xfffc, /* dst_mask */
1162 FALSE), /* pcrel_offset */
1164 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1165 HOWTO (R_PPC64_TOC16_DS, /* type */
1166 0, /* rightshift */
1167 1, /* size (0 = byte, 1 = short, 2 = long) */
1168 16, /* bitsize */
1169 FALSE, /* pc_relative */
1170 0, /* bitpos */
1171 complain_overflow_signed, /* complain_on_overflow */
1172 ppc64_elf_toc_reloc, /* special_function */
1173 "R_PPC64_TOC16_DS", /* name */
1174 FALSE, /* partial_inplace */
1175 0, /* src_mask */
1176 0xfffc, /* dst_mask */
1177 FALSE), /* pcrel_offset */
1179 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1180 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1181 0, /* rightshift */
1182 1, /* size (0 = byte, 1 = short, 2 = long) */
1183 16, /* bitsize */
1184 FALSE, /* pc_relative */
1185 0, /* bitpos */
1186 complain_overflow_dont, /* complain_on_overflow */
1187 ppc64_elf_toc_reloc, /* special_function */
1188 "R_PPC64_TOC16_LO_DS", /* name */
1189 FALSE, /* partial_inplace */
1190 0, /* src_mask */
1191 0xfffc, /* dst_mask */
1192 FALSE), /* pcrel_offset */
1194 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1195 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1196 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1197 0, /* rightshift */
1198 1, /* size (0 = byte, 1 = short, 2 = long) */
1199 16, /* bitsize */
1200 FALSE, /* pc_relative */
1201 0, /* bitpos */
1202 complain_overflow_signed, /* complain_on_overflow */
1203 ppc64_elf_unhandled_reloc, /* special_function */
1204 "R_PPC64_PLTGOT16_DS", /* name */
1205 FALSE, /* partial_inplace */
1206 0, /* src_mask */
1207 0xfffc, /* dst_mask */
1208 FALSE), /* pcrel_offset */
1210 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1211 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1212 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1213 0, /* rightshift */
1214 1, /* size (0 = byte, 1 = short, 2 = long) */
1215 16, /* bitsize */
1216 FALSE, /* pc_relative */
1217 0, /* bitpos */
1218 complain_overflow_dont, /* complain_on_overflow */
1219 ppc64_elf_unhandled_reloc, /* special_function */
1220 "R_PPC64_PLTGOT16_LO_DS",/* name */
1221 FALSE, /* partial_inplace */
1222 0, /* src_mask */
1223 0xfffc, /* dst_mask */
1224 FALSE), /* pcrel_offset */
1226 /* Marker reloc for TLS. */
1227 HOWTO (R_PPC64_TLS,
1228 0, /* rightshift */
1229 2, /* size (0 = byte, 1 = short, 2 = long) */
1230 32, /* bitsize */
1231 FALSE, /* pc_relative */
1232 0, /* bitpos */
1233 complain_overflow_dont, /* complain_on_overflow */
1234 bfd_elf_generic_reloc, /* special_function */
1235 "R_PPC64_TLS", /* name */
1236 FALSE, /* partial_inplace */
1237 0, /* src_mask */
1238 0, /* dst_mask */
1239 FALSE), /* pcrel_offset */
1241 /* Computes the load module index of the load module that contains the
1242 definition of its TLS sym. */
1243 HOWTO (R_PPC64_DTPMOD64,
1244 0, /* rightshift */
1245 4, /* size (0 = byte, 1 = short, 2 = long) */
1246 64, /* bitsize */
1247 FALSE, /* pc_relative */
1248 0, /* bitpos */
1249 complain_overflow_dont, /* complain_on_overflow */
1250 ppc64_elf_unhandled_reloc, /* special_function */
1251 "R_PPC64_DTPMOD64", /* name */
1252 FALSE, /* partial_inplace */
1253 0, /* src_mask */
1254 ONES (64), /* dst_mask */
1255 FALSE), /* pcrel_offset */
1257 /* Computes a dtv-relative displacement, the difference between the value
1258 of sym+add and the base address of the thread-local storage block that
1259 contains the definition of sym, minus 0x8000. */
1260 HOWTO (R_PPC64_DTPREL64,
1261 0, /* rightshift */
1262 4, /* size (0 = byte, 1 = short, 2 = long) */
1263 64, /* bitsize */
1264 FALSE, /* pc_relative */
1265 0, /* bitpos */
1266 complain_overflow_dont, /* complain_on_overflow */
1267 ppc64_elf_unhandled_reloc, /* special_function */
1268 "R_PPC64_DTPREL64", /* name */
1269 FALSE, /* partial_inplace */
1270 0, /* src_mask */
1271 ONES (64), /* dst_mask */
1272 FALSE), /* pcrel_offset */
1274 /* A 16 bit dtprel reloc. */
1275 HOWTO (R_PPC64_DTPREL16,
1276 0, /* rightshift */
1277 1, /* size (0 = byte, 1 = short, 2 = long) */
1278 16, /* bitsize */
1279 FALSE, /* pc_relative */
1280 0, /* bitpos */
1281 complain_overflow_signed, /* complain_on_overflow */
1282 ppc64_elf_unhandled_reloc, /* special_function */
1283 "R_PPC64_DTPREL16", /* name */
1284 FALSE, /* partial_inplace */
1285 0, /* src_mask */
1286 0xffff, /* dst_mask */
1287 FALSE), /* pcrel_offset */
1289 /* Like DTPREL16, but no overflow. */
1290 HOWTO (R_PPC64_DTPREL16_LO,
1291 0, /* rightshift */
1292 1, /* size (0 = byte, 1 = short, 2 = long) */
1293 16, /* bitsize */
1294 FALSE, /* pc_relative */
1295 0, /* bitpos */
1296 complain_overflow_dont, /* complain_on_overflow */
1297 ppc64_elf_unhandled_reloc, /* special_function */
1298 "R_PPC64_DTPREL16_LO", /* name */
1299 FALSE, /* partial_inplace */
1300 0, /* src_mask */
1301 0xffff, /* dst_mask */
1302 FALSE), /* pcrel_offset */
1304 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1305 HOWTO (R_PPC64_DTPREL16_HI,
1306 16, /* rightshift */
1307 1, /* size (0 = byte, 1 = short, 2 = long) */
1308 16, /* bitsize */
1309 FALSE, /* pc_relative */
1310 0, /* bitpos */
1311 complain_overflow_dont, /* complain_on_overflow */
1312 ppc64_elf_unhandled_reloc, /* special_function */
1313 "R_PPC64_DTPREL16_HI", /* name */
1314 FALSE, /* partial_inplace */
1315 0, /* src_mask */
1316 0xffff, /* dst_mask */
1317 FALSE), /* pcrel_offset */
1319 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1320 HOWTO (R_PPC64_DTPREL16_HA,
1321 16, /* rightshift */
1322 1, /* size (0 = byte, 1 = short, 2 = long) */
1323 16, /* bitsize */
1324 FALSE, /* pc_relative */
1325 0, /* bitpos */
1326 complain_overflow_dont, /* complain_on_overflow */
1327 ppc64_elf_unhandled_reloc, /* special_function */
1328 "R_PPC64_DTPREL16_HA", /* name */
1329 FALSE, /* partial_inplace */
1330 0, /* src_mask */
1331 0xffff, /* dst_mask */
1332 FALSE), /* pcrel_offset */
1334 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1335 HOWTO (R_PPC64_DTPREL16_HIGHER,
1336 32, /* rightshift */
1337 1, /* size (0 = byte, 1 = short, 2 = long) */
1338 16, /* bitsize */
1339 FALSE, /* pc_relative */
1340 0, /* bitpos */
1341 complain_overflow_dont, /* complain_on_overflow */
1342 ppc64_elf_unhandled_reloc, /* special_function */
1343 "R_PPC64_DTPREL16_HIGHER", /* name */
1344 FALSE, /* partial_inplace */
1345 0, /* src_mask */
1346 0xffff, /* dst_mask */
1347 FALSE), /* pcrel_offset */
1349 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1350 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1351 32, /* rightshift */
1352 1, /* size (0 = byte, 1 = short, 2 = long) */
1353 16, /* bitsize */
1354 FALSE, /* pc_relative */
1355 0, /* bitpos */
1356 complain_overflow_dont, /* complain_on_overflow */
1357 ppc64_elf_unhandled_reloc, /* special_function */
1358 "R_PPC64_DTPREL16_HIGHERA", /* name */
1359 FALSE, /* partial_inplace */
1360 0, /* src_mask */
1361 0xffff, /* dst_mask */
1362 FALSE), /* pcrel_offset */
1364 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1365 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1366 48, /* rightshift */
1367 1, /* size (0 = byte, 1 = short, 2 = long) */
1368 16, /* bitsize */
1369 FALSE, /* pc_relative */
1370 0, /* bitpos */
1371 complain_overflow_dont, /* complain_on_overflow */
1372 ppc64_elf_unhandled_reloc, /* special_function */
1373 "R_PPC64_DTPREL16_HIGHEST", /* name */
1374 FALSE, /* partial_inplace */
1375 0, /* src_mask */
1376 0xffff, /* dst_mask */
1377 FALSE), /* pcrel_offset */
1379 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1380 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1381 48, /* rightshift */
1382 1, /* size (0 = byte, 1 = short, 2 = long) */
1383 16, /* bitsize */
1384 FALSE, /* pc_relative */
1385 0, /* bitpos */
1386 complain_overflow_dont, /* complain_on_overflow */
1387 ppc64_elf_unhandled_reloc, /* special_function */
1388 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1389 FALSE, /* partial_inplace */
1390 0, /* src_mask */
1391 0xffff, /* dst_mask */
1392 FALSE), /* pcrel_offset */
1394 /* Like DTPREL16, but for insns with a DS field. */
1395 HOWTO (R_PPC64_DTPREL16_DS,
1396 0, /* rightshift */
1397 1, /* size (0 = byte, 1 = short, 2 = long) */
1398 16, /* bitsize */
1399 FALSE, /* pc_relative */
1400 0, /* bitpos */
1401 complain_overflow_signed, /* complain_on_overflow */
1402 ppc64_elf_unhandled_reloc, /* special_function */
1403 "R_PPC64_DTPREL16_DS", /* name */
1404 FALSE, /* partial_inplace */
1405 0, /* src_mask */
1406 0xfffc, /* dst_mask */
1407 FALSE), /* pcrel_offset */
1409 /* Like DTPREL16_DS, but no overflow. */
1410 HOWTO (R_PPC64_DTPREL16_LO_DS,
1411 0, /* rightshift */
1412 1, /* size (0 = byte, 1 = short, 2 = long) */
1413 16, /* bitsize */
1414 FALSE, /* pc_relative */
1415 0, /* bitpos */
1416 complain_overflow_dont, /* complain_on_overflow */
1417 ppc64_elf_unhandled_reloc, /* special_function */
1418 "R_PPC64_DTPREL16_LO_DS", /* name */
1419 FALSE, /* partial_inplace */
1420 0, /* src_mask */
1421 0xfffc, /* dst_mask */
1422 FALSE), /* pcrel_offset */
1424 /* Computes a tp-relative displacement, the difference between the value of
1425 sym+add and the value of the thread pointer (r13). */
1426 HOWTO (R_PPC64_TPREL64,
1427 0, /* rightshift */
1428 4, /* size (0 = byte, 1 = short, 2 = long) */
1429 64, /* bitsize */
1430 FALSE, /* pc_relative */
1431 0, /* bitpos */
1432 complain_overflow_dont, /* complain_on_overflow */
1433 ppc64_elf_unhandled_reloc, /* special_function */
1434 "R_PPC64_TPREL64", /* name */
1435 FALSE, /* partial_inplace */
1436 0, /* src_mask */
1437 ONES (64), /* dst_mask */
1438 FALSE), /* pcrel_offset */
1440 /* A 16 bit tprel reloc. */
1441 HOWTO (R_PPC64_TPREL16,
1442 0, /* rightshift */
1443 1, /* size (0 = byte, 1 = short, 2 = long) */
1444 16, /* bitsize */
1445 FALSE, /* pc_relative */
1446 0, /* bitpos */
1447 complain_overflow_signed, /* complain_on_overflow */
1448 ppc64_elf_unhandled_reloc, /* special_function */
1449 "R_PPC64_TPREL16", /* name */
1450 FALSE, /* partial_inplace */
1451 0, /* src_mask */
1452 0xffff, /* dst_mask */
1453 FALSE), /* pcrel_offset */
1455 /* Like TPREL16, but no overflow. */
1456 HOWTO (R_PPC64_TPREL16_LO,
1457 0, /* rightshift */
1458 1, /* size (0 = byte, 1 = short, 2 = long) */
1459 16, /* bitsize */
1460 FALSE, /* pc_relative */
1461 0, /* bitpos */
1462 complain_overflow_dont, /* complain_on_overflow */
1463 ppc64_elf_unhandled_reloc, /* special_function */
1464 "R_PPC64_TPREL16_LO", /* name */
1465 FALSE, /* partial_inplace */
1466 0, /* src_mask */
1467 0xffff, /* dst_mask */
1468 FALSE), /* pcrel_offset */
1470 /* Like TPREL16_LO, but next higher group of 16 bits. */
1471 HOWTO (R_PPC64_TPREL16_HI,
1472 16, /* rightshift */
1473 1, /* size (0 = byte, 1 = short, 2 = long) */
1474 16, /* bitsize */
1475 FALSE, /* pc_relative */
1476 0, /* bitpos */
1477 complain_overflow_dont, /* complain_on_overflow */
1478 ppc64_elf_unhandled_reloc, /* special_function */
1479 "R_PPC64_TPREL16_HI", /* name */
1480 FALSE, /* partial_inplace */
1481 0, /* src_mask */
1482 0xffff, /* dst_mask */
1483 FALSE), /* pcrel_offset */
1485 /* Like TPREL16_HI, but adjust for low 16 bits. */
1486 HOWTO (R_PPC64_TPREL16_HA,
1487 16, /* rightshift */
1488 1, /* size (0 = byte, 1 = short, 2 = long) */
1489 16, /* bitsize */
1490 FALSE, /* pc_relative */
1491 0, /* bitpos */
1492 complain_overflow_dont, /* complain_on_overflow */
1493 ppc64_elf_unhandled_reloc, /* special_function */
1494 "R_PPC64_TPREL16_HA", /* name */
1495 FALSE, /* partial_inplace */
1496 0, /* src_mask */
1497 0xffff, /* dst_mask */
1498 FALSE), /* pcrel_offset */
1500 /* Like TPREL16_HI, but next higher group of 16 bits. */
1501 HOWTO (R_PPC64_TPREL16_HIGHER,
1502 32, /* rightshift */
1503 1, /* size (0 = byte, 1 = short, 2 = long) */
1504 16, /* bitsize */
1505 FALSE, /* pc_relative */
1506 0, /* bitpos */
1507 complain_overflow_dont, /* complain_on_overflow */
1508 ppc64_elf_unhandled_reloc, /* special_function */
1509 "R_PPC64_TPREL16_HIGHER", /* name */
1510 FALSE, /* partial_inplace */
1511 0, /* src_mask */
1512 0xffff, /* dst_mask */
1513 FALSE), /* pcrel_offset */
1515 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1516 HOWTO (R_PPC64_TPREL16_HIGHERA,
1517 32, /* rightshift */
1518 1, /* size (0 = byte, 1 = short, 2 = long) */
1519 16, /* bitsize */
1520 FALSE, /* pc_relative */
1521 0, /* bitpos */
1522 complain_overflow_dont, /* complain_on_overflow */
1523 ppc64_elf_unhandled_reloc, /* special_function */
1524 "R_PPC64_TPREL16_HIGHERA", /* name */
1525 FALSE, /* partial_inplace */
1526 0, /* src_mask */
1527 0xffff, /* dst_mask */
1528 FALSE), /* pcrel_offset */
1530 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1531 HOWTO (R_PPC64_TPREL16_HIGHEST,
1532 48, /* rightshift */
1533 1, /* size (0 = byte, 1 = short, 2 = long) */
1534 16, /* bitsize */
1535 FALSE, /* pc_relative */
1536 0, /* bitpos */
1537 complain_overflow_dont, /* complain_on_overflow */
1538 ppc64_elf_unhandled_reloc, /* special_function */
1539 "R_PPC64_TPREL16_HIGHEST", /* name */
1540 FALSE, /* partial_inplace */
1541 0, /* src_mask */
1542 0xffff, /* dst_mask */
1543 FALSE), /* pcrel_offset */
1545 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1546 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1547 48, /* rightshift */
1548 1, /* size (0 = byte, 1 = short, 2 = long) */
1549 16, /* bitsize */
1550 FALSE, /* pc_relative */
1551 0, /* bitpos */
1552 complain_overflow_dont, /* complain_on_overflow */
1553 ppc64_elf_unhandled_reloc, /* special_function */
1554 "R_PPC64_TPREL16_HIGHESTA", /* name */
1555 FALSE, /* partial_inplace */
1556 0, /* src_mask */
1557 0xffff, /* dst_mask */
1558 FALSE), /* pcrel_offset */
1560 /* Like TPREL16, but for insns with a DS field. */
1561 HOWTO (R_PPC64_TPREL16_DS,
1562 0, /* rightshift */
1563 1, /* size (0 = byte, 1 = short, 2 = long) */
1564 16, /* bitsize */
1565 FALSE, /* pc_relative */
1566 0, /* bitpos */
1567 complain_overflow_signed, /* complain_on_overflow */
1568 ppc64_elf_unhandled_reloc, /* special_function */
1569 "R_PPC64_TPREL16_DS", /* name */
1570 FALSE, /* partial_inplace */
1571 0, /* src_mask */
1572 0xfffc, /* dst_mask */
1573 FALSE), /* pcrel_offset */
1575 /* Like TPREL16_DS, but no overflow. */
1576 HOWTO (R_PPC64_TPREL16_LO_DS,
1577 0, /* rightshift */
1578 1, /* size (0 = byte, 1 = short, 2 = long) */
1579 16, /* bitsize */
1580 FALSE, /* pc_relative */
1581 0, /* bitpos */
1582 complain_overflow_dont, /* complain_on_overflow */
1583 ppc64_elf_unhandled_reloc, /* special_function */
1584 "R_PPC64_TPREL16_LO_DS", /* name */
1585 FALSE, /* partial_inplace */
1586 0, /* src_mask */
1587 0xfffc, /* dst_mask */
1588 FALSE), /* pcrel_offset */
1590 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1591 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1592 to the first entry relative to the TOC base (r2). */
1593 HOWTO (R_PPC64_GOT_TLSGD16,
1594 0, /* rightshift */
1595 1, /* size (0 = byte, 1 = short, 2 = long) */
1596 16, /* bitsize */
1597 FALSE, /* pc_relative */
1598 0, /* bitpos */
1599 complain_overflow_signed, /* complain_on_overflow */
1600 ppc64_elf_unhandled_reloc, /* special_function */
1601 "R_PPC64_GOT_TLSGD16", /* name */
1602 FALSE, /* partial_inplace */
1603 0, /* src_mask */
1604 0xffff, /* dst_mask */
1605 FALSE), /* pcrel_offset */
1607 /* Like GOT_TLSGD16, but no overflow. */
1608 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1609 0, /* rightshift */
1610 1, /* size (0 = byte, 1 = short, 2 = long) */
1611 16, /* bitsize */
1612 FALSE, /* pc_relative */
1613 0, /* bitpos */
1614 complain_overflow_dont, /* complain_on_overflow */
1615 ppc64_elf_unhandled_reloc, /* special_function */
1616 "R_PPC64_GOT_TLSGD16_LO", /* name */
1617 FALSE, /* partial_inplace */
1618 0, /* src_mask */
1619 0xffff, /* dst_mask */
1620 FALSE), /* pcrel_offset */
1622 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1623 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1624 16, /* rightshift */
1625 1, /* size (0 = byte, 1 = short, 2 = long) */
1626 16, /* bitsize */
1627 FALSE, /* pc_relative */
1628 0, /* bitpos */
1629 complain_overflow_dont, /* complain_on_overflow */
1630 ppc64_elf_unhandled_reloc, /* special_function */
1631 "R_PPC64_GOT_TLSGD16_HI", /* name */
1632 FALSE, /* partial_inplace */
1633 0, /* src_mask */
1634 0xffff, /* dst_mask */
1635 FALSE), /* pcrel_offset */
1637 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1638 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1639 16, /* rightshift */
1640 1, /* size (0 = byte, 1 = short, 2 = long) */
1641 16, /* bitsize */
1642 FALSE, /* pc_relative */
1643 0, /* bitpos */
1644 complain_overflow_dont, /* complain_on_overflow */
1645 ppc64_elf_unhandled_reloc, /* special_function */
1646 "R_PPC64_GOT_TLSGD16_HA", /* name */
1647 FALSE, /* partial_inplace */
1648 0, /* src_mask */
1649 0xffff, /* dst_mask */
1650 FALSE), /* pcrel_offset */
1652 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1653 with values (sym+add)@dtpmod and zero, and computes the offset to the
1654 first entry relative to the TOC base (r2). */
1655 HOWTO (R_PPC64_GOT_TLSLD16,
1656 0, /* rightshift */
1657 1, /* size (0 = byte, 1 = short, 2 = long) */
1658 16, /* bitsize */
1659 FALSE, /* pc_relative */
1660 0, /* bitpos */
1661 complain_overflow_signed, /* complain_on_overflow */
1662 ppc64_elf_unhandled_reloc, /* special_function */
1663 "R_PPC64_GOT_TLSLD16", /* name */
1664 FALSE, /* partial_inplace */
1665 0, /* src_mask */
1666 0xffff, /* dst_mask */
1667 FALSE), /* pcrel_offset */
1669 /* Like GOT_TLSLD16, but no overflow. */
1670 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1671 0, /* rightshift */
1672 1, /* size (0 = byte, 1 = short, 2 = long) */
1673 16, /* bitsize */
1674 FALSE, /* pc_relative */
1675 0, /* bitpos */
1676 complain_overflow_dont, /* complain_on_overflow */
1677 ppc64_elf_unhandled_reloc, /* special_function */
1678 "R_PPC64_GOT_TLSLD16_LO", /* name */
1679 FALSE, /* partial_inplace */
1680 0, /* src_mask */
1681 0xffff, /* dst_mask */
1682 FALSE), /* pcrel_offset */
1684 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1685 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1686 16, /* rightshift */
1687 1, /* size (0 = byte, 1 = short, 2 = long) */
1688 16, /* bitsize */
1689 FALSE, /* pc_relative */
1690 0, /* bitpos */
1691 complain_overflow_dont, /* complain_on_overflow */
1692 ppc64_elf_unhandled_reloc, /* special_function */
1693 "R_PPC64_GOT_TLSLD16_HI", /* name */
1694 FALSE, /* partial_inplace */
1695 0, /* src_mask */
1696 0xffff, /* dst_mask */
1697 FALSE), /* pcrel_offset */
1699 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1700 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1701 16, /* rightshift */
1702 1, /* size (0 = byte, 1 = short, 2 = long) */
1703 16, /* bitsize */
1704 FALSE, /* pc_relative */
1705 0, /* bitpos */
1706 complain_overflow_dont, /* complain_on_overflow */
1707 ppc64_elf_unhandled_reloc, /* special_function */
1708 "R_PPC64_GOT_TLSLD16_HA", /* name */
1709 FALSE, /* partial_inplace */
1710 0, /* src_mask */
1711 0xffff, /* dst_mask */
1712 FALSE), /* pcrel_offset */
1714 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1715 the offset to the entry relative to the TOC base (r2). */
1716 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1717 0, /* rightshift */
1718 1, /* size (0 = byte, 1 = short, 2 = long) */
1719 16, /* bitsize */
1720 FALSE, /* pc_relative */
1721 0, /* bitpos */
1722 complain_overflow_signed, /* complain_on_overflow */
1723 ppc64_elf_unhandled_reloc, /* special_function */
1724 "R_PPC64_GOT_DTPREL16_DS", /* name */
1725 FALSE, /* partial_inplace */
1726 0, /* src_mask */
1727 0xfffc, /* dst_mask */
1728 FALSE), /* pcrel_offset */
1730 /* Like GOT_DTPREL16_DS, but no overflow. */
1731 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1732 0, /* rightshift */
1733 1, /* size (0 = byte, 1 = short, 2 = long) */
1734 16, /* bitsize */
1735 FALSE, /* pc_relative */
1736 0, /* bitpos */
1737 complain_overflow_dont, /* complain_on_overflow */
1738 ppc64_elf_unhandled_reloc, /* special_function */
1739 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1740 FALSE, /* partial_inplace */
1741 0, /* src_mask */
1742 0xfffc, /* dst_mask */
1743 FALSE), /* pcrel_offset */
1745 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1746 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1747 16, /* rightshift */
1748 1, /* size (0 = byte, 1 = short, 2 = long) */
1749 16, /* bitsize */
1750 FALSE, /* pc_relative */
1751 0, /* bitpos */
1752 complain_overflow_dont, /* complain_on_overflow */
1753 ppc64_elf_unhandled_reloc, /* special_function */
1754 "R_PPC64_GOT_DTPREL16_HI", /* name */
1755 FALSE, /* partial_inplace */
1756 0, /* src_mask */
1757 0xffff, /* dst_mask */
1758 FALSE), /* pcrel_offset */
1760 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1761 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1762 16, /* rightshift */
1763 1, /* size (0 = byte, 1 = short, 2 = long) */
1764 16, /* bitsize */
1765 FALSE, /* pc_relative */
1766 0, /* bitpos */
1767 complain_overflow_dont, /* complain_on_overflow */
1768 ppc64_elf_unhandled_reloc, /* special_function */
1769 "R_PPC64_GOT_DTPREL16_HA", /* name */
1770 FALSE, /* partial_inplace */
1771 0, /* src_mask */
1772 0xffff, /* dst_mask */
1773 FALSE), /* pcrel_offset */
1775 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1776 offset to the entry relative to the TOC base (r2). */
1777 HOWTO (R_PPC64_GOT_TPREL16_DS,
1778 0, /* rightshift */
1779 1, /* size (0 = byte, 1 = short, 2 = long) */
1780 16, /* bitsize */
1781 FALSE, /* pc_relative */
1782 0, /* bitpos */
1783 complain_overflow_signed, /* complain_on_overflow */
1784 ppc64_elf_unhandled_reloc, /* special_function */
1785 "R_PPC64_GOT_TPREL16_DS", /* name */
1786 FALSE, /* partial_inplace */
1787 0, /* src_mask */
1788 0xfffc, /* dst_mask */
1789 FALSE), /* pcrel_offset */
1791 /* Like GOT_TPREL16_DS, but no overflow. */
1792 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1793 0, /* rightshift */
1794 1, /* size (0 = byte, 1 = short, 2 = long) */
1795 16, /* bitsize */
1796 FALSE, /* pc_relative */
1797 0, /* bitpos */
1798 complain_overflow_dont, /* complain_on_overflow */
1799 ppc64_elf_unhandled_reloc, /* special_function */
1800 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1801 FALSE, /* partial_inplace */
1802 0, /* src_mask */
1803 0xfffc, /* dst_mask */
1804 FALSE), /* pcrel_offset */
1806 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1807 HOWTO (R_PPC64_GOT_TPREL16_HI,
1808 16, /* rightshift */
1809 1, /* size (0 = byte, 1 = short, 2 = long) */
1810 16, /* bitsize */
1811 FALSE, /* pc_relative */
1812 0, /* bitpos */
1813 complain_overflow_dont, /* complain_on_overflow */
1814 ppc64_elf_unhandled_reloc, /* special_function */
1815 "R_PPC64_GOT_TPREL16_HI", /* name */
1816 FALSE, /* partial_inplace */
1817 0, /* src_mask */
1818 0xffff, /* dst_mask */
1819 FALSE), /* pcrel_offset */
1821 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1822 HOWTO (R_PPC64_GOT_TPREL16_HA,
1823 16, /* rightshift */
1824 1, /* size (0 = byte, 1 = short, 2 = long) */
1825 16, /* bitsize */
1826 FALSE, /* pc_relative */
1827 0, /* bitpos */
1828 complain_overflow_dont, /* complain_on_overflow */
1829 ppc64_elf_unhandled_reloc, /* special_function */
1830 "R_PPC64_GOT_TPREL16_HA", /* name */
1831 FALSE, /* partial_inplace */
1832 0, /* src_mask */
1833 0xffff, /* dst_mask */
1834 FALSE), /* pcrel_offset */
1836 /* GNU extension to record C++ vtable hierarchy. */
1837 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1838 0, /* rightshift */
1839 0, /* size (0 = byte, 1 = short, 2 = long) */
1840 0, /* bitsize */
1841 FALSE, /* pc_relative */
1842 0, /* bitpos */
1843 complain_overflow_dont, /* complain_on_overflow */
1844 NULL, /* special_function */
1845 "R_PPC64_GNU_VTINHERIT", /* name */
1846 FALSE, /* partial_inplace */
1847 0, /* src_mask */
1848 0, /* dst_mask */
1849 FALSE), /* pcrel_offset */
1851 /* GNU extension to record C++ vtable member usage. */
1852 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1853 0, /* rightshift */
1854 0, /* size (0 = byte, 1 = short, 2 = long) */
1855 0, /* bitsize */
1856 FALSE, /* pc_relative */
1857 0, /* bitpos */
1858 complain_overflow_dont, /* complain_on_overflow */
1859 NULL, /* special_function */
1860 "R_PPC64_GNU_VTENTRY", /* name */
1861 FALSE, /* partial_inplace */
1862 0, /* src_mask */
1863 0, /* dst_mask */
1864 FALSE), /* pcrel_offset */
1868 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1869 be done. */
1871 static void
1872 ppc_howto_init (void)
1874 unsigned int i, type;
1876 for (i = 0;
1877 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1878 i++)
1880 type = ppc64_elf_howto_raw[i].type;
1881 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1882 / sizeof (ppc64_elf_howto_table[0])));
1883 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1887 static reloc_howto_type *
1888 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1889 bfd_reloc_code_real_type code)
1891 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1893 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1894 /* Initialize howto table if needed. */
1895 ppc_howto_init ();
1897 switch (code)
1899 default:
1900 return NULL;
1902 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1903 break;
1904 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1905 break;
1906 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1907 break;
1908 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1909 break;
1910 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1911 break;
1912 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1913 break;
1914 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1915 break;
1916 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1917 break;
1918 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1919 break;
1920 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1921 break;
1922 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1923 break;
1924 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1925 break;
1926 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1927 break;
1928 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1929 break;
1930 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1931 break;
1932 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1933 break;
1934 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1935 break;
1936 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1937 break;
1938 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1939 break;
1940 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1941 break;
1942 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1943 break;
1944 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1945 break;
1946 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1947 break;
1948 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1949 break;
1950 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1951 break;
1952 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1953 break;
1954 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1955 break;
1956 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1957 break;
1958 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1959 break;
1960 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1961 break;
1962 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1963 break;
1964 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1965 break;
1966 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1967 break;
1968 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1969 break;
1970 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1971 break;
1972 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1973 break;
1974 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1975 break;
1976 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1977 break;
1978 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1979 break;
1980 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1981 break;
1982 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1983 break;
1984 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1985 break;
1986 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1987 break;
1988 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1989 break;
1990 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1991 break;
1992 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1993 break;
1994 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1995 break;
1996 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1997 break;
1998 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1999 break;
2000 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2001 break;
2002 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2003 break;
2004 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2005 break;
2006 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2007 break;
2008 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2009 break;
2010 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2011 break;
2012 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2013 break;
2014 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2015 break;
2016 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2017 break;
2018 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2019 break;
2020 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2021 break;
2022 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2023 break;
2024 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2025 break;
2026 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2027 break;
2028 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2029 break;
2030 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2031 break;
2032 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2033 break;
2034 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2035 break;
2036 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2037 break;
2038 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2039 break;
2040 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2041 break;
2042 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2043 break;
2044 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2045 break;
2046 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2047 break;
2048 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2049 break;
2050 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2051 break;
2052 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2053 break;
2054 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2055 break;
2056 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2057 break;
2058 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2059 break;
2060 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2061 break;
2062 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2063 break;
2064 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2065 break;
2066 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2067 break;
2068 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2069 break;
2070 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2071 break;
2072 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2073 break;
2074 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2075 break;
2076 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2077 break;
2078 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2079 break;
2080 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2081 break;
2082 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2083 break;
2084 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2085 break;
2086 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2087 break;
2088 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2089 break;
2090 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2091 break;
2092 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2093 break;
2094 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2095 break;
2096 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2097 break;
2098 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2099 break;
2100 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2101 break;
2102 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2103 break;
2106 return ppc64_elf_howto_table[r];
2109 /* Set the howto pointer for a PowerPC ELF reloc. */
2111 static void
2112 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2113 Elf_Internal_Rela *dst)
2115 unsigned int type;
2117 /* Initialize howto table if needed. */
2118 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2119 ppc_howto_init ();
2121 type = ELF64_R_TYPE (dst->r_info);
2122 if (type >= (sizeof (ppc64_elf_howto_table)
2123 / sizeof (ppc64_elf_howto_table[0])))
2125 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2126 abfd, (int) type);
2127 type = R_PPC64_NONE;
2129 cache_ptr->howto = ppc64_elf_howto_table[type];
2132 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2134 static bfd_reloc_status_type
2135 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2136 void *data, asection *input_section,
2137 bfd *output_bfd, char **error_message)
2139 /* If this is a relocatable link (output_bfd test tells us), just
2140 call the generic function. Any adjustment will be done at final
2141 link time. */
2142 if (output_bfd != NULL)
2143 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2144 input_section, output_bfd, error_message);
2146 /* Adjust the addend for sign extension of the low 16 bits.
2147 We won't actually be using the low 16 bits, so trashing them
2148 doesn't matter. */
2149 reloc_entry->addend += 0x8000;
2150 return bfd_reloc_continue;
2153 static bfd_reloc_status_type
2154 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2155 void *data, asection *input_section,
2156 bfd *output_bfd, char **error_message)
2158 if (output_bfd != NULL)
2159 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2160 input_section, output_bfd, error_message);
2162 if (strcmp (symbol->section->name, ".opd") == 0
2163 && (symbol->section->owner->flags & DYNAMIC) == 0)
2165 bfd_vma dest = opd_entry_value (symbol->section,
2166 symbol->value + reloc_entry->addend,
2167 NULL, NULL);
2168 if (dest != (bfd_vma) -1)
2169 reloc_entry->addend = dest - (symbol->value
2170 + symbol->section->output_section->vma
2171 + symbol->section->output_offset);
2173 return bfd_reloc_continue;
2176 static bfd_reloc_status_type
2177 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2178 void *data, asection *input_section,
2179 bfd *output_bfd, char **error_message)
2181 long insn;
2182 enum elf_ppc64_reloc_type r_type;
2183 bfd_size_type octets;
2184 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
2185 bfd_boolean is_power4 = FALSE;
2187 /* If this is a relocatable link (output_bfd test tells us), just
2188 call the generic function. Any adjustment will be done at final
2189 link time. */
2190 if (output_bfd != NULL)
2191 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2192 input_section, output_bfd, error_message);
2194 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2195 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2196 insn &= ~(0x01 << 21);
2197 r_type = reloc_entry->howto->type;
2198 if (r_type == R_PPC64_ADDR14_BRTAKEN
2199 || r_type == R_PPC64_REL14_BRTAKEN)
2200 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2202 if (is_power4)
2204 /* Set 'a' bit. This is 0b00010 in BO field for branch
2205 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2206 for branch on CTR insns (BO == 1a00t or 1a01t). */
2207 if ((insn & (0x14 << 21)) == (0x04 << 21))
2208 insn |= 0x02 << 21;
2209 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2210 insn |= 0x08 << 21;
2211 else
2212 goto out;
2214 else
2216 bfd_vma target = 0;
2217 bfd_vma from;
2219 if (!bfd_is_com_section (symbol->section))
2220 target = symbol->value;
2221 target += symbol->section->output_section->vma;
2222 target += symbol->section->output_offset;
2223 target += reloc_entry->addend;
2225 from = (reloc_entry->address
2226 + input_section->output_offset
2227 + input_section->output_section->vma);
2229 /* Invert 'y' bit if not the default. */
2230 if ((bfd_signed_vma) (target - from) < 0)
2231 insn ^= 0x01 << 21;
2233 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2234 out:
2235 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2236 input_section, output_bfd, error_message);
2239 static bfd_reloc_status_type
2240 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2241 void *data, asection *input_section,
2242 bfd *output_bfd, char **error_message)
2244 /* If this is a relocatable link (output_bfd test tells us), just
2245 call the generic function. Any adjustment will be done at final
2246 link time. */
2247 if (output_bfd != NULL)
2248 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2249 input_section, output_bfd, error_message);
2251 /* Subtract the symbol section base address. */
2252 reloc_entry->addend -= symbol->section->output_section->vma;
2253 return bfd_reloc_continue;
2256 static bfd_reloc_status_type
2257 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2258 void *data, asection *input_section,
2259 bfd *output_bfd, char **error_message)
2261 /* If this is a relocatable link (output_bfd test tells us), just
2262 call the generic function. Any adjustment will be done at final
2263 link time. */
2264 if (output_bfd != NULL)
2265 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2266 input_section, output_bfd, error_message);
2268 /* Subtract the symbol section base address. */
2269 reloc_entry->addend -= symbol->section->output_section->vma;
2271 /* Adjust the addend for sign extension of the low 16 bits. */
2272 reloc_entry->addend += 0x8000;
2273 return bfd_reloc_continue;
2276 static bfd_reloc_status_type
2277 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2278 void *data, asection *input_section,
2279 bfd *output_bfd, char **error_message)
2281 bfd_vma TOCstart;
2283 /* If this is a relocatable link (output_bfd test tells us), just
2284 call the generic function. Any adjustment will be done at final
2285 link time. */
2286 if (output_bfd != NULL)
2287 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2288 input_section, output_bfd, error_message);
2290 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2291 if (TOCstart == 0)
2292 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2294 /* Subtract the TOC base address. */
2295 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2296 return bfd_reloc_continue;
2299 static bfd_reloc_status_type
2300 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2301 void *data, asection *input_section,
2302 bfd *output_bfd, char **error_message)
2304 bfd_vma TOCstart;
2306 /* If this is a relocatable link (output_bfd test tells us), just
2307 call the generic function. Any adjustment will be done at final
2308 link time. */
2309 if (output_bfd != NULL)
2310 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2311 input_section, output_bfd, error_message);
2313 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2314 if (TOCstart == 0)
2315 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2317 /* Subtract the TOC base address. */
2318 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2320 /* Adjust the addend for sign extension of the low 16 bits. */
2321 reloc_entry->addend += 0x8000;
2322 return bfd_reloc_continue;
2325 static bfd_reloc_status_type
2326 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2327 void *data, asection *input_section,
2328 bfd *output_bfd, char **error_message)
2330 bfd_vma TOCstart;
2331 bfd_size_type octets;
2333 /* If this is a relocatable link (output_bfd test tells us), just
2334 call the generic function. Any adjustment will be done at final
2335 link time. */
2336 if (output_bfd != NULL)
2337 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2338 input_section, output_bfd, error_message);
2340 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2341 if (TOCstart == 0)
2342 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2344 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2345 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2346 return bfd_reloc_ok;
2349 static bfd_reloc_status_type
2350 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2351 void *data, asection *input_section,
2352 bfd *output_bfd, char **error_message)
2354 /* If this is a relocatable link (output_bfd test tells us), just
2355 call the generic function. Any adjustment will be done at final
2356 link time. */
2357 if (output_bfd != NULL)
2358 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2359 input_section, output_bfd, error_message);
2361 if (error_message != NULL)
2363 static char buf[60];
2364 sprintf (buf, "generic linker can't handle %s",
2365 reloc_entry->howto->name);
2366 *error_message = buf;
2368 return bfd_reloc_dangerous;
2371 struct ppc64_elf_obj_tdata
2373 struct elf_obj_tdata elf;
2375 /* Shortcuts to dynamic linker sections. */
2376 asection *got;
2377 asection *relgot;
2379 union {
2380 /* Used during garbage collection. We attach global symbols defined
2381 on removed .opd entries to this section so that the sym is removed. */
2382 asection *deleted_section;
2384 /* Used when adding symbols. */
2385 bfd_boolean has_dotsym;
2386 } u;
2388 /* TLS local dynamic got entry handling. Suppose for multiple GOT
2389 sections means we potentially need one of these for each input bfd. */
2390 union {
2391 bfd_signed_vma refcount;
2392 bfd_vma offset;
2393 } tlsld_got;
2395 /* A copy of relocs before they are modified for --emit-relocs. */
2396 Elf_Internal_Rela *opd_relocs;
2399 #define ppc64_elf_tdata(bfd) \
2400 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2402 #define ppc64_tlsld_got(bfd) \
2403 (&ppc64_elf_tdata (bfd)->tlsld_got)
2405 /* Override the generic function because we store some extras. */
2407 static bfd_boolean
2408 ppc64_elf_mkobject (bfd *abfd)
2410 bfd_size_type amt = sizeof (struct ppc64_elf_obj_tdata);
2411 abfd->tdata.any = bfd_zalloc (abfd, amt);
2412 if (abfd->tdata.any == NULL)
2413 return FALSE;
2414 return TRUE;
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 { ".plt", 4, 0, SHT_NOBITS, 0 },
2521 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2522 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2523 { ".toc", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2524 { ".toc1", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2525 { ".tocbss", 7, 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2526 { NULL, 0, 0, 0, 0 }
2529 struct _ppc64_elf_section_data
2531 struct bfd_elf_section_data elf;
2533 /* An array with one entry for each opd function descriptor. */
2534 union
2536 /* Points to the function code section for local opd entries. */
2537 asection **func_sec;
2538 /* After editing .opd, adjust references to opd local syms. */
2539 long *adjust;
2540 } opd;
2542 /* An array for toc sections, indexed by offset/8.
2543 Specifies the relocation symbol index used at a given toc offset. */
2544 unsigned *t_symndx;
2547 #define ppc64_elf_section_data(sec) \
2548 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2550 static bfd_boolean
2551 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2553 struct _ppc64_elf_section_data *sdata;
2554 bfd_size_type amt = sizeof (*sdata);
2556 sdata = bfd_zalloc (abfd, amt);
2557 if (sdata == NULL)
2558 return FALSE;
2559 sec->used_by_bfd = sdata;
2561 return _bfd_elf_new_section_hook (abfd, sec);
2564 static void *
2565 get_opd_info (asection * sec)
2567 if (sec != NULL
2568 && ppc64_elf_section_data (sec) != NULL
2569 && ppc64_elf_section_data (sec)->opd.adjust != NULL)
2570 return ppc64_elf_section_data (sec)->opd.adjust;
2571 return NULL;
2574 /* Parameters for the qsort hook. */
2575 static asection *synthetic_opd;
2576 static bfd_boolean synthetic_relocatable;
2578 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2580 static int
2581 compare_symbols (const void *ap, const void *bp)
2583 const asymbol *a = * (const asymbol **) ap;
2584 const asymbol *b = * (const asymbol **) bp;
2586 /* Section symbols first. */
2587 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2588 return -1;
2589 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2590 return 1;
2592 /* then .opd symbols. */
2593 if (a->section == synthetic_opd && b->section != synthetic_opd)
2594 return -1;
2595 if (a->section != synthetic_opd && b->section == synthetic_opd)
2596 return 1;
2598 /* then other code symbols. */
2599 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2600 == (SEC_CODE | SEC_ALLOC)
2601 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2602 != (SEC_CODE | SEC_ALLOC))
2603 return -1;
2605 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2606 != (SEC_CODE | SEC_ALLOC)
2607 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2608 == (SEC_CODE | SEC_ALLOC))
2609 return 1;
2611 if (synthetic_relocatable)
2613 if (a->section->id < b->section->id)
2614 return -1;
2616 if (a->section->id > b->section->id)
2617 return 1;
2620 if (a->value + a->section->vma < b->value + b->section->vma)
2621 return -1;
2623 if (a->value + a->section->vma > b->value + b->section->vma)
2624 return 1;
2626 return 0;
2629 /* Search SYMS for a symbol of the given VALUE. */
2631 static asymbol *
2632 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
2634 long mid;
2636 if (id == -1)
2638 while (lo < hi)
2640 mid = (lo + hi) >> 1;
2641 if (syms[mid]->value + syms[mid]->section->vma < value)
2642 lo = mid + 1;
2643 else if (syms[mid]->value + syms[mid]->section->vma > value)
2644 hi = mid;
2645 else
2646 return syms[mid];
2649 else
2651 while (lo < hi)
2653 mid = (lo + hi) >> 1;
2654 if (syms[mid]->section->id < id)
2655 lo = mid + 1;
2656 else if (syms[mid]->section->id > id)
2657 hi = mid;
2658 else if (syms[mid]->value < value)
2659 lo = mid + 1;
2660 else if (syms[mid]->value > value)
2661 hi = mid;
2662 else
2663 return syms[mid];
2666 return NULL;
2669 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2670 entry syms. */
2672 static long
2673 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2674 long static_count, asymbol **static_syms,
2675 long dyn_count, asymbol **dyn_syms,
2676 asymbol **ret)
2678 asymbol *s;
2679 long i;
2680 long count;
2681 char *names;
2682 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2683 asection *opd;
2684 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2685 asymbol **syms;
2687 *ret = NULL;
2689 opd = bfd_get_section_by_name (abfd, ".opd");
2690 if (opd == NULL)
2691 return 0;
2693 symcount = static_count;
2694 if (!relocatable)
2695 symcount += dyn_count;
2696 if (symcount == 0)
2697 return 0;
2699 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2700 if (syms == NULL)
2701 return -1;
2703 if (!relocatable && static_count != 0 && dyn_count != 0)
2705 /* Use both symbol tables. */
2706 memcpy (syms, static_syms, static_count * sizeof (*syms));
2707 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
2709 else if (!relocatable && static_count == 0)
2710 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2711 else
2712 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2714 synthetic_opd = opd;
2715 synthetic_relocatable = relocatable;
2716 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2718 if (!relocatable && symcount > 1)
2720 long j;
2721 /* Trim duplicate syms, since we may have merged the normal and
2722 dynamic symbols. Actually, we only care about syms that have
2723 different values, so trim any with the same value. */
2724 for (i = 1, j = 1; i < symcount; ++i)
2725 if (syms[i - 1]->value + syms[i - 1]->section->vma
2726 != syms[i]->value + syms[i]->section->vma)
2727 syms[j++] = syms[i];
2728 symcount = j;
2731 i = 0;
2732 if (syms[i]->section == opd)
2733 ++i;
2734 codesecsym = i;
2736 for (; i < symcount; ++i)
2737 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2738 != (SEC_CODE | SEC_ALLOC))
2739 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2740 break;
2741 codesecsymend = i;
2743 for (; i < symcount; ++i)
2744 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2745 break;
2746 secsymend = i;
2748 for (; i < symcount; ++i)
2749 if (syms[i]->section != opd)
2750 break;
2751 opdsymend = i;
2753 for (; i < symcount; ++i)
2754 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2755 != (SEC_CODE | SEC_ALLOC))
2756 break;
2757 symcount = i;
2759 count = 0;
2760 if (opdsymend == secsymend)
2761 goto done;
2763 if (relocatable)
2765 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2766 arelent *r;
2767 size_t size;
2768 long relcount;
2770 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2771 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2772 if (relcount == 0)
2773 goto done;
2775 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2777 count = -1;
2778 goto done;
2781 size = 0;
2782 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2784 asymbol *sym;
2786 while (r < opd->relocation + relcount
2787 && r->address < syms[i]->value + opd->vma)
2788 ++r;
2790 if (r == opd->relocation + relcount)
2791 break;
2793 if (r->address != syms[i]->value + opd->vma)
2794 continue;
2796 if (r->howto->type != R_PPC64_ADDR64)
2797 continue;
2799 sym = *r->sym_ptr_ptr;
2800 if (!sym_exists_at (syms, opdsymend, symcount,
2801 sym->section->id, sym->value + r->addend))
2803 ++count;
2804 size += sizeof (asymbol);
2805 size += strlen (syms[i]->name) + 2;
2809 s = *ret = bfd_malloc (size);
2810 if (s == NULL)
2812 count = -1;
2813 goto done;
2816 names = (char *) (s + count);
2818 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2820 asymbol *sym;
2822 while (r < opd->relocation + relcount
2823 && r->address < syms[i]->value + opd->vma)
2824 ++r;
2826 if (r == opd->relocation + relcount)
2827 break;
2829 if (r->address != syms[i]->value + opd->vma)
2830 continue;
2832 if (r->howto->type != R_PPC64_ADDR64)
2833 continue;
2835 sym = *r->sym_ptr_ptr;
2836 if (!sym_exists_at (syms, opdsymend, symcount,
2837 sym->section->id, sym->value + r->addend))
2839 size_t len;
2841 *s = *syms[i];
2842 s->section = sym->section;
2843 s->value = sym->value + r->addend;
2844 s->name = names;
2845 *names++ = '.';
2846 len = strlen (syms[i]->name);
2847 memcpy (names, syms[i]->name, len + 1);
2848 names += len + 1;
2849 s++;
2853 else
2855 bfd_byte *contents;
2856 size_t size;
2858 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
2860 if (contents)
2862 free_contents_and_exit:
2863 free (contents);
2865 count = -1;
2866 goto done;
2869 size = 0;
2870 for (i = secsymend; i < opdsymend; ++i)
2872 bfd_vma ent;
2874 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2875 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2877 ++count;
2878 size += sizeof (asymbol);
2879 size += strlen (syms[i]->name) + 2;
2883 s = *ret = bfd_malloc (size);
2884 if (s == NULL)
2885 goto free_contents_and_exit;
2887 names = (char *) (s + count);
2889 for (i = secsymend; i < opdsymend; ++i)
2891 bfd_vma ent;
2893 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2894 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2896 long lo, hi;
2897 size_t len;
2898 asection *sec = abfd->sections;
2900 *s = *syms[i];
2901 lo = codesecsym;
2902 hi = codesecsymend;
2903 while (lo < hi)
2905 long mid = (lo + hi) >> 1;
2906 if (syms[mid]->section->vma < ent)
2907 lo = mid + 1;
2908 else if (syms[mid]->section->vma > ent)
2909 hi = mid;
2910 else
2912 sec = syms[mid]->section;
2913 break;
2917 if (lo >= hi && lo > codesecsym)
2918 sec = syms[lo - 1]->section;
2920 for (; sec != NULL; sec = sec->next)
2922 if (sec->vma > ent)
2923 break;
2924 if ((sec->flags & SEC_ALLOC) == 0
2925 || (sec->flags & SEC_LOAD) == 0)
2926 break;
2927 if ((sec->flags & SEC_CODE) != 0)
2928 s->section = sec;
2930 s->value = ent - s->section->vma;
2931 s->name = names;
2932 *names++ = '.';
2933 len = strlen (syms[i]->name);
2934 memcpy (names, syms[i]->name, len + 1);
2935 names += len + 1;
2936 s++;
2939 free (contents);
2942 done:
2943 free (syms);
2944 return count;
2947 /* The following functions are specific to the ELF linker, while
2948 functions above are used generally. Those named ppc64_elf_* are
2949 called by the main ELF linker code. They appear in this file more
2950 or less in the order in which they are called. eg.
2951 ppc64_elf_check_relocs is called early in the link process,
2952 ppc64_elf_finish_dynamic_sections is one of the last functions
2953 called.
2955 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2956 functions have both a function code symbol and a function descriptor
2957 symbol. A call to foo in a relocatable object file looks like:
2959 . .text
2960 . x:
2961 . bl .foo
2962 . nop
2964 The function definition in another object file might be:
2966 . .section .opd
2967 . foo: .quad .foo
2968 . .quad .TOC.@tocbase
2969 . .quad 0
2971 . .text
2972 . .foo: blr
2974 When the linker resolves the call during a static link, the branch
2975 unsurprisingly just goes to .foo and the .opd information is unused.
2976 If the function definition is in a shared library, things are a little
2977 different: The call goes via a plt call stub, the opd information gets
2978 copied to the plt, and the linker patches the nop.
2980 . x:
2981 . bl .foo_stub
2982 . ld 2,40(1)
2985 . .foo_stub:
2986 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
2987 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
2988 . std 2,40(1) # this is the general idea
2989 . ld 11,0(12)
2990 . ld 2,8(12)
2991 . mtctr 11
2992 . ld 11,16(12)
2993 . bctr
2995 . .section .plt
2996 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2998 The "reloc ()" notation is supposed to indicate that the linker emits
2999 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3000 copying.
3002 What are the difficulties here? Well, firstly, the relocations
3003 examined by the linker in check_relocs are against the function code
3004 sym .foo, while the dynamic relocation in the plt is emitted against
3005 the function descriptor symbol, foo. Somewhere along the line, we need
3006 to carefully copy dynamic link information from one symbol to the other.
3007 Secondly, the generic part of the elf linker will make .foo a dynamic
3008 symbol as is normal for most other backends. We need foo dynamic
3009 instead, at least for an application final link. However, when
3010 creating a shared library containing foo, we need to have both symbols
3011 dynamic so that references to .foo are satisfied during the early
3012 stages of linking. Otherwise the linker might decide to pull in a
3013 definition from some other object, eg. a static library.
3015 Update: As of August 2004, we support a new convention. Function
3016 calls may use the function descriptor symbol, ie. "bl foo". This
3017 behaves exactly as "bl .foo". */
3019 /* The linker needs to keep track of the number of relocs that it
3020 decides to copy as dynamic relocs in check_relocs for each symbol.
3021 This is so that it can later discard them if they are found to be
3022 unnecessary. We store the information in a field extending the
3023 regular ELF linker hash table. */
3025 struct ppc_dyn_relocs
3027 struct ppc_dyn_relocs *next;
3029 /* The input section of the reloc. */
3030 asection *sec;
3032 /* Total number of relocs copied for the input section. */
3033 bfd_size_type count;
3035 /* Number of pc-relative relocs copied for the input section. */
3036 bfd_size_type pc_count;
3039 /* Track GOT entries needed for a given symbol. We might need more
3040 than one got entry per symbol. */
3041 struct got_entry
3043 struct got_entry *next;
3045 /* The symbol addend that we'll be placing in the GOT. */
3046 bfd_vma addend;
3048 /* Unlike other ELF targets, we use separate GOT entries for the same
3049 symbol referenced from different input files. This is to support
3050 automatic multiple TOC/GOT sections, where the TOC base can vary
3051 from one input file to another.
3053 Point to the BFD owning this GOT entry. */
3054 bfd *owner;
3056 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
3057 TLS_TPREL or TLS_DTPREL for tls entries. */
3058 char tls_type;
3060 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
3061 union
3063 bfd_signed_vma refcount;
3064 bfd_vma offset;
3065 } got;
3068 /* The same for PLT. */
3069 struct plt_entry
3071 struct plt_entry *next;
3073 bfd_vma addend;
3075 union
3077 bfd_signed_vma refcount;
3078 bfd_vma offset;
3079 } plt;
3082 /* Of those relocs that might be copied as dynamic relocs, this macro
3083 selects those that must be copied when linking a shared library,
3084 even when the symbol is local. */
3086 #define MUST_BE_DYN_RELOC(RTYPE) \
3087 ((RTYPE) != R_PPC64_REL32 \
3088 && (RTYPE) != R_PPC64_REL64 \
3089 && (RTYPE) != R_PPC64_REL30)
3091 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3092 copying dynamic variables from a shared lib into an app's dynbss
3093 section, and instead use a dynamic relocation to point into the
3094 shared lib. With code that gcc generates, it's vital that this be
3095 enabled; In the PowerPC64 ABI, the address of a function is actually
3096 the address of a function descriptor, which resides in the .opd
3097 section. gcc uses the descriptor directly rather than going via the
3098 GOT as some other ABI's do, which means that initialized function
3099 pointers must reference the descriptor. Thus, a function pointer
3100 initialized to the address of a function in a shared library will
3101 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3102 redefines the function descriptor symbol to point to the copy. This
3103 presents a problem as a plt entry for that function is also
3104 initialized from the function descriptor symbol and the copy reloc
3105 may not be initialized first. */
3106 #define ELIMINATE_COPY_RELOCS 1
3108 /* Section name for stubs is the associated section name plus this
3109 string. */
3110 #define STUB_SUFFIX ".stub"
3112 /* Linker stubs.
3113 ppc_stub_long_branch:
3114 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3115 destination, but a 24 bit branch in a stub section will reach.
3116 . b dest
3118 ppc_stub_plt_branch:
3119 Similar to the above, but a 24 bit branch in the stub section won't
3120 reach its destination.
3121 . addis %r12,%r2,xxx@toc@ha
3122 . ld %r11,xxx@toc@l(%r12)
3123 . mtctr %r11
3124 . bctr
3126 ppc_stub_plt_call:
3127 Used to call a function in a shared library. If it so happens that
3128 the plt entry referenced crosses a 64k boundary, then an extra
3129 "addis %r12,%r12,1" will be inserted before the load at xxx+8 or
3130 xxx+16 as appropriate.
3131 . addis %r12,%r2,xxx@toc@ha
3132 . std %r2,40(%r1)
3133 . ld %r11,xxx+0@toc@l(%r12)
3134 . ld %r2,xxx+8@toc@l(%r12)
3135 . mtctr %r11
3136 . ld %r11,xxx+16@toc@l(%r12)
3137 . bctr
3139 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3140 code to adjust the value and save r2 to support multiple toc sections.
3141 A ppc_stub_long_branch with an r2 offset looks like:
3142 . std %r2,40(%r1)
3143 . addis %r2,%r2,off@ha
3144 . addi %r2,%r2,off@l
3145 . b dest
3147 A ppc_stub_plt_branch with an r2 offset looks like:
3148 . std %r2,40(%r1)
3149 . addis %r12,%r2,xxx@toc@ha
3150 . ld %r11,xxx@toc@l(%r12)
3151 . addis %r2,%r2,off@ha
3152 . addi %r2,%r2,off@l
3153 . mtctr %r11
3154 . bctr
3157 enum ppc_stub_type {
3158 ppc_stub_none,
3159 ppc_stub_long_branch,
3160 ppc_stub_long_branch_r2off,
3161 ppc_stub_plt_branch,
3162 ppc_stub_plt_branch_r2off,
3163 ppc_stub_plt_call
3166 struct ppc_stub_hash_entry {
3168 /* Base hash table entry structure. */
3169 struct bfd_hash_entry root;
3171 enum ppc_stub_type stub_type;
3173 /* The stub section. */
3174 asection *stub_sec;
3176 /* Offset within stub_sec of the beginning of this stub. */
3177 bfd_vma stub_offset;
3179 /* Given the symbol's value and its section we can determine its final
3180 value when building the stubs (so the stub knows where to jump. */
3181 bfd_vma target_value;
3182 asection *target_section;
3184 /* The symbol table entry, if any, that this was derived from. */
3185 struct ppc_link_hash_entry *h;
3187 /* And the reloc addend that this was derived from. */
3188 bfd_vma addend;
3190 /* Where this stub is being called from, or, in the case of combined
3191 stub sections, the first input section in the group. */
3192 asection *id_sec;
3195 struct ppc_branch_hash_entry {
3197 /* Base hash table entry structure. */
3198 struct bfd_hash_entry root;
3200 /* Offset within branch lookup table. */
3201 unsigned int offset;
3203 /* Generation marker. */
3204 unsigned int iter;
3207 struct ppc_link_hash_entry
3209 struct elf_link_hash_entry elf;
3211 /* A pointer to the most recently used stub hash entry against this
3212 symbol. */
3213 struct ppc_stub_hash_entry *stub_cache;
3215 /* Track dynamic relocs copied for this symbol. */
3216 struct ppc_dyn_relocs *dyn_relocs;
3218 /* Link between function code and descriptor symbols. */
3219 struct ppc_link_hash_entry *oh;
3221 /* Flag function code and descriptor symbols. */
3222 unsigned int is_func:1;
3223 unsigned int is_func_descriptor:1;
3224 unsigned int fake:1;
3226 /* Whether global opd/toc sym has been adjusted or not.
3227 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3228 should be set for all globals defined in any opd/toc section. */
3229 unsigned int adjust_done:1;
3231 /* Set if we twiddled this symbol to weak at some stage. */
3232 unsigned int was_undefined:1;
3234 /* Contexts in which symbol is used in the GOT (or TOC).
3235 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3236 corresponding relocs are encountered during check_relocs.
3237 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3238 indicate the corresponding GOT entry type is not needed.
3239 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3240 a TPREL one. We use a separate flag rather than setting TPREL
3241 just for convenience in distinguishing the two cases. */
3242 #define TLS_GD 1 /* GD reloc. */
3243 #define TLS_LD 2 /* LD reloc. */
3244 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
3245 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3246 #define TLS_TLS 16 /* Any TLS reloc. */
3247 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3248 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3249 char tls_mask;
3252 /* ppc64 ELF linker hash table. */
3254 struct ppc_link_hash_table
3256 struct elf_link_hash_table elf;
3258 /* The stub hash table. */
3259 struct bfd_hash_table stub_hash_table;
3261 /* Another hash table for plt_branch stubs. */
3262 struct bfd_hash_table branch_hash_table;
3264 /* Linker stub bfd. */
3265 bfd *stub_bfd;
3267 /* Linker call-backs. */
3268 asection * (*add_stub_section) (const char *, asection *);
3269 void (*layout_sections_again) (void);
3271 /* Array to keep track of which stub sections have been created, and
3272 information on stub grouping. */
3273 struct map_stub {
3274 /* This is the section to which stubs in the group will be attached. */
3275 asection *link_sec;
3276 /* The stub section. */
3277 asection *stub_sec;
3278 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3279 bfd_vma toc_off;
3280 } *stub_group;
3282 /* Temp used when calculating TOC pointers. */
3283 bfd_vma toc_curr;
3285 /* Highest input section id. */
3286 int top_id;
3288 /* Highest output section index. */
3289 int top_index;
3291 /* List of input sections for each output section. */
3292 asection **input_list;
3294 /* Short-cuts to get to dynamic linker sections. */
3295 asection *got;
3296 asection *plt;
3297 asection *relplt;
3298 asection *dynbss;
3299 asection *relbss;
3300 asection *glink;
3301 asection *sfpr;
3302 asection *brlt;
3303 asection *relbrlt;
3305 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3306 struct ppc_link_hash_entry *tls_get_addr;
3307 struct ppc_link_hash_entry *tls_get_addr_fd;
3309 /* Statistics. */
3310 unsigned long stub_count[ppc_stub_plt_call];
3312 /* Number of stubs against global syms. */
3313 unsigned long stub_globals;
3315 /* Set if we should emit symbols for stubs. */
3316 unsigned int emit_stub_syms:1;
3318 /* Support for multiple toc sections. */
3319 unsigned int no_multi_toc:1;
3320 unsigned int multi_toc_needed:1;
3322 /* Set on error. */
3323 unsigned int stub_error:1;
3325 /* Flag set when small branches are detected. Used to
3326 select suitable defaults for the stub group size. */
3327 unsigned int has_14bit_branch:1;
3329 /* Temp used by ppc64_elf_check_directives. */
3330 unsigned int twiddled_syms:1;
3332 /* Incremented every time we size stubs. */
3333 unsigned int stub_iteration;
3335 /* Small local sym to section mapping cache. */
3336 struct sym_sec_cache sym_sec;
3339 /* Rename some of the generic section flags to better document how they
3340 are used here. */
3341 #define has_toc_reloc has_gp_reloc
3342 #define makes_toc_func_call need_finalize_relax
3343 #define call_check_in_progress reloc_done
3345 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3347 #define ppc_hash_table(p) \
3348 ((struct ppc_link_hash_table *) ((p)->hash))
3350 #define ppc_stub_hash_lookup(table, string, create, copy) \
3351 ((struct ppc_stub_hash_entry *) \
3352 bfd_hash_lookup ((table), (string), (create), (copy)))
3354 #define ppc_branch_hash_lookup(table, string, create, copy) \
3355 ((struct ppc_branch_hash_entry *) \
3356 bfd_hash_lookup ((table), (string), (create), (copy)))
3358 /* Create an entry in the stub hash table. */
3360 static struct bfd_hash_entry *
3361 stub_hash_newfunc (struct bfd_hash_entry *entry,
3362 struct bfd_hash_table *table,
3363 const char *string)
3365 /* Allocate the structure if it has not already been allocated by a
3366 subclass. */
3367 if (entry == NULL)
3369 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3370 if (entry == NULL)
3371 return entry;
3374 /* Call the allocation method of the superclass. */
3375 entry = bfd_hash_newfunc (entry, table, string);
3376 if (entry != NULL)
3378 struct ppc_stub_hash_entry *eh;
3380 /* Initialize the local fields. */
3381 eh = (struct ppc_stub_hash_entry *) entry;
3382 eh->stub_type = ppc_stub_none;
3383 eh->stub_sec = NULL;
3384 eh->stub_offset = 0;
3385 eh->target_value = 0;
3386 eh->target_section = NULL;
3387 eh->h = NULL;
3388 eh->id_sec = NULL;
3391 return entry;
3394 /* Create an entry in the branch hash table. */
3396 static struct bfd_hash_entry *
3397 branch_hash_newfunc (struct bfd_hash_entry *entry,
3398 struct bfd_hash_table *table,
3399 const char *string)
3401 /* Allocate the structure if it has not already been allocated by a
3402 subclass. */
3403 if (entry == NULL)
3405 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3406 if (entry == NULL)
3407 return entry;
3410 /* Call the allocation method of the superclass. */
3411 entry = bfd_hash_newfunc (entry, table, string);
3412 if (entry != NULL)
3414 struct ppc_branch_hash_entry *eh;
3416 /* Initialize the local fields. */
3417 eh = (struct ppc_branch_hash_entry *) entry;
3418 eh->offset = 0;
3419 eh->iter = 0;
3422 return entry;
3425 /* Create an entry in a ppc64 ELF linker hash table. */
3427 static struct bfd_hash_entry *
3428 link_hash_newfunc (struct bfd_hash_entry *entry,
3429 struct bfd_hash_table *table,
3430 const char *string)
3432 /* Allocate the structure if it has not already been allocated by a
3433 subclass. */
3434 if (entry == NULL)
3436 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3437 if (entry == NULL)
3438 return entry;
3441 /* Call the allocation method of the superclass. */
3442 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3443 if (entry != NULL)
3445 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3447 memset (&eh->stub_cache, 0,
3448 (sizeof (struct ppc_link_hash_entry)
3449 - offsetof (struct ppc_link_hash_entry, stub_cache)));
3452 return entry;
3455 /* Create a ppc64 ELF linker hash table. */
3457 static struct bfd_link_hash_table *
3458 ppc64_elf_link_hash_table_create (bfd *abfd)
3460 struct ppc_link_hash_table *htab;
3461 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3463 htab = bfd_zmalloc (amt);
3464 if (htab == NULL)
3465 return NULL;
3467 if (! _bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc))
3469 free (htab);
3470 return NULL;
3473 /* Init the stub hash table too. */
3474 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc))
3475 return NULL;
3477 /* And the branch hash table. */
3478 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc))
3479 return NULL;
3481 /* Initializing two fields of the union is just cosmetic. We really
3482 only care about glist, but when compiled on a 32-bit host the
3483 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3484 debugger inspection of these fields look nicer. */
3485 htab->elf.init_got_refcount.refcount = 0;
3486 htab->elf.init_got_refcount.glist = NULL;
3487 htab->elf.init_plt_refcount.refcount = 0;
3488 htab->elf.init_plt_refcount.glist = NULL;
3489 htab->elf.init_got_offset.offset = 0;
3490 htab->elf.init_got_offset.glist = NULL;
3491 htab->elf.init_plt_offset.offset = 0;
3492 htab->elf.init_plt_offset.glist = NULL;
3494 return &htab->elf.root;
3497 /* Free the derived linker hash table. */
3499 static void
3500 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
3502 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
3504 bfd_hash_table_free (&ret->stub_hash_table);
3505 bfd_hash_table_free (&ret->branch_hash_table);
3506 _bfd_generic_link_hash_table_free (hash);
3509 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3511 void
3512 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
3514 struct ppc_link_hash_table *htab;
3516 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
3518 /* Always hook our dynamic sections into the first bfd, which is the
3519 linker created stub bfd. This ensures that the GOT header is at
3520 the start of the output TOC section. */
3521 htab = ppc_hash_table (info);
3522 htab->stub_bfd = abfd;
3523 htab->elf.dynobj = abfd;
3526 /* Build a name for an entry in the stub hash table. */
3528 static char *
3529 ppc_stub_name (const asection *input_section,
3530 const asection *sym_sec,
3531 const struct ppc_link_hash_entry *h,
3532 const Elf_Internal_Rela *rel)
3534 char *stub_name;
3535 bfd_size_type len;
3537 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3538 offsets from a sym as a branch target? In fact, we could
3539 probably assume the addend is always zero. */
3540 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3542 if (h)
3544 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3545 stub_name = bfd_malloc (len);
3546 if (stub_name == NULL)
3547 return stub_name;
3549 sprintf (stub_name, "%08x.%s+%x",
3550 input_section->id & 0xffffffff,
3551 h->elf.root.root.string,
3552 (int) rel->r_addend & 0xffffffff);
3554 else
3556 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3557 stub_name = bfd_malloc (len);
3558 if (stub_name == NULL)
3559 return stub_name;
3561 sprintf (stub_name, "%08x.%x:%x+%x",
3562 input_section->id & 0xffffffff,
3563 sym_sec->id & 0xffffffff,
3564 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3565 (int) rel->r_addend & 0xffffffff);
3567 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3568 stub_name[len - 2] = 0;
3569 return stub_name;
3572 /* Look up an entry in the stub hash. Stub entries are cached because
3573 creating the stub name takes a bit of time. */
3575 static struct ppc_stub_hash_entry *
3576 ppc_get_stub_entry (const asection *input_section,
3577 const asection *sym_sec,
3578 struct ppc_link_hash_entry *h,
3579 const Elf_Internal_Rela *rel,
3580 struct ppc_link_hash_table *htab)
3582 struct ppc_stub_hash_entry *stub_entry;
3583 const asection *id_sec;
3585 /* If this input section is part of a group of sections sharing one
3586 stub section, then use the id of the first section in the group.
3587 Stub names need to include a section id, as there may well be
3588 more than one stub used to reach say, printf, and we need to
3589 distinguish between them. */
3590 id_sec = htab->stub_group[input_section->id].link_sec;
3592 if (h != NULL && h->stub_cache != NULL
3593 && h->stub_cache->h == h
3594 && h->stub_cache->id_sec == id_sec)
3596 stub_entry = h->stub_cache;
3598 else
3600 char *stub_name;
3602 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
3603 if (stub_name == NULL)
3604 return NULL;
3606 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3607 stub_name, FALSE, FALSE);
3608 if (h != NULL)
3609 h->stub_cache = stub_entry;
3611 free (stub_name);
3614 return stub_entry;
3617 /* Add a new stub entry to the stub hash. Not all fields of the new
3618 stub entry are initialised. */
3620 static struct ppc_stub_hash_entry *
3621 ppc_add_stub (const char *stub_name,
3622 asection *section,
3623 struct ppc_link_hash_table *htab)
3625 asection *link_sec;
3626 asection *stub_sec;
3627 struct ppc_stub_hash_entry *stub_entry;
3629 link_sec = htab->stub_group[section->id].link_sec;
3630 stub_sec = htab->stub_group[section->id].stub_sec;
3631 if (stub_sec == NULL)
3633 stub_sec = htab->stub_group[link_sec->id].stub_sec;
3634 if (stub_sec == NULL)
3636 size_t namelen;
3637 bfd_size_type len;
3638 char *s_name;
3640 namelen = strlen (link_sec->name);
3641 len = namelen + sizeof (STUB_SUFFIX);
3642 s_name = bfd_alloc (htab->stub_bfd, len);
3643 if (s_name == NULL)
3644 return NULL;
3646 memcpy (s_name, link_sec->name, namelen);
3647 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3648 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3649 if (stub_sec == NULL)
3650 return NULL;
3651 htab->stub_group[link_sec->id].stub_sec = stub_sec;
3653 htab->stub_group[section->id].stub_sec = stub_sec;
3656 /* Enter this entry into the linker stub hash table. */
3657 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3658 TRUE, FALSE);
3659 if (stub_entry == NULL)
3661 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
3662 section->owner, stub_name);
3663 return NULL;
3666 stub_entry->stub_sec = stub_sec;
3667 stub_entry->stub_offset = 0;
3668 stub_entry->id_sec = link_sec;
3669 return stub_entry;
3672 /* Create sections for linker generated code. */
3674 static bfd_boolean
3675 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3677 struct ppc_link_hash_table *htab;
3678 flagword flags;
3680 htab = ppc_hash_table (info);
3682 /* Create .sfpr for code to save and restore fp regs. */
3683 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3684 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3685 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3686 flags);
3687 if (htab->sfpr == NULL
3688 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3689 return FALSE;
3691 /* Create .glink for lazy dynamic linking support. */
3692 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3693 flags);
3694 if (htab->glink == NULL
3695 || ! bfd_set_section_alignment (dynobj, htab->glink, 2))
3696 return FALSE;
3698 /* Create branch lookup table for plt_branch stubs. */
3699 if (info->shared)
3701 flags = (SEC_ALLOC | SEC_LOAD
3702 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3703 htab->brlt
3704 = bfd_make_section_anyway_with_flags (dynobj, ".data.rel.ro.brlt",
3705 flags);
3707 else
3709 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3710 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3711 htab->brlt
3712 = bfd_make_section_anyway_with_flags (dynobj, ".rodata.brlt", flags);
3715 if (htab->brlt == NULL
3716 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
3717 return FALSE;
3719 if (info->shared)
3721 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3722 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3723 htab->relbrlt
3724 = bfd_make_section_anyway_with_flags (dynobj, ".rela.data.rel.ro.brlt",
3725 flags);
3727 else if (info->emitrelocations)
3729 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3730 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3731 htab->relbrlt
3732 = bfd_make_section_anyway_with_flags (dynobj, ".rela.rodata.brlt",
3733 flags);
3735 else
3736 return TRUE;
3738 if (!htab->relbrlt
3739 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3740 return FALSE;
3742 return TRUE;
3745 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3746 not already done. */
3748 static bfd_boolean
3749 create_got_section (bfd *abfd, struct bfd_link_info *info)
3751 asection *got, *relgot;
3752 flagword flags;
3753 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3755 if (!htab->got)
3757 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
3758 return FALSE;
3760 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
3761 if (!htab->got)
3762 abort ();
3765 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3766 | SEC_LINKER_CREATED);
3768 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3769 if (!got
3770 || !bfd_set_section_alignment (abfd, got, 3))
3771 return FALSE;
3773 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3774 flags | SEC_READONLY);
3775 if (!relgot
3776 || ! bfd_set_section_alignment (abfd, relgot, 3))
3777 return FALSE;
3779 ppc64_elf_tdata (abfd)->got = got;
3780 ppc64_elf_tdata (abfd)->relgot = relgot;
3781 return TRUE;
3784 /* Create the dynamic sections, and set up shortcuts. */
3786 static bfd_boolean
3787 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3789 struct ppc_link_hash_table *htab;
3791 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3792 return FALSE;
3794 htab = ppc_hash_table (info);
3795 if (!htab->got)
3796 htab->got = bfd_get_section_by_name (dynobj, ".got");
3797 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
3798 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
3799 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
3800 if (!info->shared)
3801 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
3803 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
3804 || (!info->shared && !htab->relbss))
3805 abort ();
3807 return TRUE;
3810 /* Merge PLT info on FROM with that on TO. */
3812 static void
3813 move_plt_plist (struct ppc_link_hash_entry *from,
3814 struct ppc_link_hash_entry *to)
3816 if (from->elf.plt.plist != NULL)
3818 if (to->elf.plt.plist != NULL)
3820 struct plt_entry **entp;
3821 struct plt_entry *ent;
3823 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3825 struct plt_entry *dent;
3827 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3828 if (dent->addend == ent->addend)
3830 dent->plt.refcount += ent->plt.refcount;
3831 *entp = ent->next;
3832 break;
3834 if (dent == NULL)
3835 entp = &ent->next;
3837 *entp = to->elf.plt.plist;
3840 to->elf.plt.plist = from->elf.plt.plist;
3841 from->elf.plt.plist = NULL;
3845 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3847 static void
3848 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3849 struct elf_link_hash_entry *dir,
3850 struct elf_link_hash_entry *ind)
3852 struct ppc_link_hash_entry *edir, *eind;
3854 edir = (struct ppc_link_hash_entry *) dir;
3855 eind = (struct ppc_link_hash_entry *) ind;
3857 /* Copy over any dynamic relocs we may have on the indirect sym. */
3858 if (eind->dyn_relocs != NULL)
3860 if (edir->dyn_relocs != NULL)
3862 struct ppc_dyn_relocs **pp;
3863 struct ppc_dyn_relocs *p;
3865 /* Add reloc counts against the indirect sym to the direct sym
3866 list. Merge any entries against the same section. */
3867 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3869 struct ppc_dyn_relocs *q;
3871 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3872 if (q->sec == p->sec)
3874 q->pc_count += p->pc_count;
3875 q->count += p->count;
3876 *pp = p->next;
3877 break;
3879 if (q == NULL)
3880 pp = &p->next;
3882 *pp = edir->dyn_relocs;
3885 edir->dyn_relocs = eind->dyn_relocs;
3886 eind->dyn_relocs = NULL;
3889 edir->is_func |= eind->is_func;
3890 edir->is_func_descriptor |= eind->is_func_descriptor;
3891 edir->tls_mask |= eind->tls_mask;
3893 /* If called to transfer flags for a weakdef during processing
3894 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
3895 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
3896 if (!(ELIMINATE_COPY_RELOCS
3897 && eind->elf.root.type != bfd_link_hash_indirect
3898 && edir->elf.dynamic_adjusted))
3899 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3901 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3902 edir->elf.ref_regular |= eind->elf.ref_regular;
3903 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3904 edir->elf.needs_plt |= eind->elf.needs_plt;
3906 /* If we were called to copy over info for a weak sym, that's all. */
3907 if (eind->elf.root.type != bfd_link_hash_indirect)
3908 return;
3910 /* Copy over got entries that we may have already seen to the
3911 symbol which just became indirect. */
3912 if (eind->elf.got.glist != NULL)
3914 if (edir->elf.got.glist != NULL)
3916 struct got_entry **entp;
3917 struct got_entry *ent;
3919 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3921 struct got_entry *dent;
3923 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3924 if (dent->addend == ent->addend
3925 && dent->owner == ent->owner
3926 && dent->tls_type == ent->tls_type)
3928 dent->got.refcount += ent->got.refcount;
3929 *entp = ent->next;
3930 break;
3932 if (dent == NULL)
3933 entp = &ent->next;
3935 *entp = edir->elf.got.glist;
3938 edir->elf.got.glist = eind->elf.got.glist;
3939 eind->elf.got.glist = NULL;
3942 /* And plt entries. */
3943 move_plt_plist (eind, edir);
3945 if (eind->elf.dynindx != -1)
3947 if (edir->elf.dynindx != -1)
3948 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3949 edir->elf.dynstr_index);
3950 edir->elf.dynindx = eind->elf.dynindx;
3951 edir->elf.dynstr_index = eind->elf.dynstr_index;
3952 eind->elf.dynindx = -1;
3953 eind->elf.dynstr_index = 0;
3957 /* Find the function descriptor hash entry from the given function code
3958 hash entry FH. Link the entries via their OH fields. */
3960 static struct ppc_link_hash_entry *
3961 get_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3963 struct ppc_link_hash_entry *fdh = fh->oh;
3965 if (fdh == NULL)
3967 const char *fd_name = fh->elf.root.root.string + 1;
3969 fdh = (struct ppc_link_hash_entry *)
3970 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3971 if (fdh != NULL)
3973 fdh->is_func_descriptor = 1;
3974 fdh->oh = fh;
3975 fh->is_func = 1;
3976 fh->oh = fdh;
3980 return fdh;
3983 /* Make a fake function descriptor sym for the code sym FH. */
3985 static struct ppc_link_hash_entry *
3986 make_fdh (struct bfd_link_info *info,
3987 struct ppc_link_hash_entry *fh)
3989 bfd *abfd;
3990 asymbol *newsym;
3991 struct bfd_link_hash_entry *bh;
3992 struct ppc_link_hash_entry *fdh;
3994 abfd = fh->elf.root.u.undef.abfd;
3995 newsym = bfd_make_empty_symbol (abfd);
3996 newsym->name = fh->elf.root.root.string + 1;
3997 newsym->section = bfd_und_section_ptr;
3998 newsym->value = 0;
3999 newsym->flags = BSF_WEAK;
4001 bh = NULL;
4002 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4003 newsym->flags, newsym->section,
4004 newsym->value, NULL, FALSE, FALSE,
4005 &bh))
4006 return NULL;
4008 fdh = (struct ppc_link_hash_entry *) bh;
4009 fdh->elf.non_elf = 0;
4010 fdh->fake = 1;
4011 fdh->is_func_descriptor = 1;
4012 fdh->oh = fh;
4013 fh->is_func = 1;
4014 fh->oh = fdh;
4015 return fdh;
4018 /* Hacks to support old ABI code.
4019 When making function calls, old ABI code references function entry
4020 points (dot symbols), while new ABI code references the function
4021 descriptor symbol. We need to make any combination of reference and
4022 definition work together, without breaking archive linking.
4024 For a defined function "foo" and an undefined call to "bar":
4025 An old object defines "foo" and ".foo", references ".bar" (possibly
4026 "bar" too).
4027 A new object defines "foo" and references "bar".
4029 A new object thus has no problem with its undefined symbols being
4030 satisfied by definitions in an old object. On the other hand, the
4031 old object won't have ".bar" satisfied by a new object. */
4033 /* Fix function descriptor symbols defined in .opd sections to be
4034 function type. */
4036 static bfd_boolean
4037 ppc64_elf_add_symbol_hook (bfd *ibfd,
4038 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4039 Elf_Internal_Sym *isym,
4040 const char **name,
4041 flagword *flags ATTRIBUTE_UNUSED,
4042 asection **sec,
4043 bfd_vma *value ATTRIBUTE_UNUSED)
4045 if (*sec != NULL
4046 && strcmp (bfd_get_section_name (ibfd, *sec), ".opd") == 0)
4047 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4049 if ((*name)[0] == '.'
4050 && ELF_ST_BIND (isym->st_info) == STB_GLOBAL
4051 && ELF_ST_TYPE (isym->st_info) < STT_SECTION
4052 && is_ppc64_elf_target (ibfd->xvec))
4053 ppc64_elf_tdata (ibfd)->u.has_dotsym = 1;
4055 return TRUE;
4058 /* This function makes an old ABI object reference to ".bar" cause the
4059 inclusion of a new ABI object archive that defines "bar".
4060 NAME is a symbol defined in an archive. Return a symbol in the hash
4061 table that might be satisfied by the archive symbols. */
4063 static struct elf_link_hash_entry *
4064 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4065 struct bfd_link_info *info,
4066 const char *name)
4068 struct elf_link_hash_entry *h;
4069 char *dot_name;
4070 size_t len;
4072 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4073 if (h != NULL
4074 /* Don't return this sym if it is a fake function descriptor
4075 created by add_symbol_adjust. */
4076 && !(h->root.type == bfd_link_hash_undefweak
4077 && ((struct ppc_link_hash_entry *) h)->fake))
4078 return h;
4080 if (name[0] == '.')
4081 return h;
4083 len = strlen (name);
4084 dot_name = bfd_alloc (abfd, len + 2);
4085 if (dot_name == NULL)
4086 return (struct elf_link_hash_entry *) 0 - 1;
4087 dot_name[0] = '.';
4088 memcpy (dot_name + 1, name, len + 1);
4089 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4090 bfd_release (abfd, dot_name);
4091 return h;
4094 /* This function satisfies all old ABI object references to ".bar" if a
4095 new ABI object defines "bar". Well, at least, undefined dot symbols
4096 are made weak. This stops later archive searches from including an
4097 object if we already have a function descriptor definition. It also
4098 prevents the linker complaining about undefined symbols.
4099 We also check and correct mismatched symbol visibility here. The
4100 most restrictive visibility of the function descriptor and the
4101 function entry symbol is used. */
4103 struct add_symbol_adjust_data
4105 struct bfd_link_info *info;
4106 bfd_boolean ok;
4109 static bfd_boolean
4110 add_symbol_adjust (struct elf_link_hash_entry *h, void *inf)
4112 struct add_symbol_adjust_data *data;
4113 struct ppc_link_hash_table *htab;
4114 struct ppc_link_hash_entry *eh;
4115 struct ppc_link_hash_entry *fdh;
4117 if (h->root.type == bfd_link_hash_indirect)
4118 return TRUE;
4120 if (h->root.type == bfd_link_hash_warning)
4121 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4123 if (h->root.root.string[0] != '.')
4124 return TRUE;
4126 data = inf;
4127 htab = ppc_hash_table (data->info);
4128 eh = (struct ppc_link_hash_entry *) h;
4129 fdh = get_fdh (eh, htab);
4130 if (fdh == NULL
4131 && !data->info->relocatable
4132 && (eh->elf.root.type == bfd_link_hash_undefined
4133 || eh->elf.root.type == bfd_link_hash_undefweak)
4134 && eh->elf.ref_regular)
4136 /* Make an undefweak function descriptor sym, which is enough to
4137 pull in an --as-needed shared lib, but won't cause link
4138 errors. Archives are handled elsewhere. */
4139 fdh = make_fdh (data->info, eh);
4140 if (fdh == NULL)
4141 data->ok = FALSE;
4142 else
4143 fdh->elf.ref_regular = 1;
4145 else if (fdh != NULL)
4147 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4148 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4149 if (entry_vis < descr_vis)
4150 fdh->elf.other += entry_vis - descr_vis;
4151 else if (entry_vis > descr_vis)
4152 eh->elf.other += descr_vis - entry_vis;
4154 if ((fdh->elf.root.type == bfd_link_hash_defined
4155 || fdh->elf.root.type == bfd_link_hash_defweak)
4156 && eh->elf.root.type == bfd_link_hash_undefined)
4158 eh->elf.root.type = bfd_link_hash_undefweak;
4159 eh->was_undefined = 1;
4160 htab->twiddled_syms = 1;
4164 return TRUE;
4167 static bfd_boolean
4168 ppc64_elf_check_directives (bfd *abfd, struct bfd_link_info *info)
4170 struct ppc_link_hash_table *htab;
4171 struct add_symbol_adjust_data data;
4173 if (!is_ppc64_elf_target (abfd->xvec))
4174 return TRUE;
4176 if (!ppc64_elf_tdata (abfd)->u.has_dotsym)
4177 return TRUE;
4178 ppc64_elf_tdata (abfd)->u.deleted_section = NULL;
4180 htab = ppc_hash_table (info);
4181 if (!is_ppc64_elf_target (htab->elf.root.creator))
4182 return TRUE;
4184 data.info = info;
4185 data.ok = TRUE;
4186 elf_link_hash_traverse (&htab->elf, add_symbol_adjust, &data);
4188 /* We need to fix the undefs list for any syms we have twiddled to
4189 undef_weak. */
4190 if (htab->twiddled_syms)
4192 bfd_link_repair_undef_list (&htab->elf.root);
4193 htab->twiddled_syms = 0;
4195 return data.ok;
4198 static bfd_boolean
4199 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4200 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4202 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4203 char *local_got_tls_masks;
4205 if (local_got_ents == NULL)
4207 bfd_size_type size = symtab_hdr->sh_info;
4209 size *= sizeof (*local_got_ents) + sizeof (*local_got_tls_masks);
4210 local_got_ents = bfd_zalloc (abfd, size);
4211 if (local_got_ents == NULL)
4212 return FALSE;
4213 elf_local_got_ents (abfd) = local_got_ents;
4216 if ((tls_type & TLS_EXPLICIT) == 0)
4218 struct got_entry *ent;
4220 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4221 if (ent->addend == r_addend
4222 && ent->owner == abfd
4223 && ent->tls_type == tls_type)
4224 break;
4225 if (ent == NULL)
4227 bfd_size_type amt = sizeof (*ent);
4228 ent = bfd_alloc (abfd, amt);
4229 if (ent == NULL)
4230 return FALSE;
4231 ent->next = local_got_ents[r_symndx];
4232 ent->addend = r_addend;
4233 ent->owner = abfd;
4234 ent->tls_type = tls_type;
4235 ent->got.refcount = 0;
4236 local_got_ents[r_symndx] = ent;
4238 ent->got.refcount += 1;
4241 local_got_tls_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
4242 local_got_tls_masks[r_symndx] |= tls_type;
4243 return TRUE;
4246 static bfd_boolean
4247 update_plt_info (bfd *abfd, struct ppc_link_hash_entry *eh, bfd_vma addend)
4249 struct plt_entry *ent;
4251 for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
4252 if (ent->addend == addend)
4253 break;
4254 if (ent == NULL)
4256 bfd_size_type amt = sizeof (*ent);
4257 ent = bfd_alloc (abfd, amt);
4258 if (ent == NULL)
4259 return FALSE;
4260 ent->next = eh->elf.plt.plist;
4261 ent->addend = addend;
4262 ent->plt.refcount = 0;
4263 eh->elf.plt.plist = ent;
4265 ent->plt.refcount += 1;
4266 eh->elf.needs_plt = 1;
4267 if (eh->elf.root.root.string[0] == '.'
4268 && eh->elf.root.root.string[1] != '\0')
4269 eh->is_func = 1;
4270 return TRUE;
4273 /* Look through the relocs for a section during the first phase, and
4274 calculate needed space in the global offset table, procedure
4275 linkage table, and dynamic reloc sections. */
4277 static bfd_boolean
4278 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4279 asection *sec, const Elf_Internal_Rela *relocs)
4281 struct ppc_link_hash_table *htab;
4282 Elf_Internal_Shdr *symtab_hdr;
4283 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4284 const Elf_Internal_Rela *rel;
4285 const Elf_Internal_Rela *rel_end;
4286 asection *sreloc;
4287 asection **opd_sym_map;
4289 if (info->relocatable)
4290 return TRUE;
4292 /* Don't do anything special with non-loaded, non-alloced sections.
4293 In particular, any relocs in such sections should not affect GOT
4294 and PLT reference counting (ie. we don't allow them to create GOT
4295 or PLT entries), there's no possibility or desire to optimize TLS
4296 relocs, and there's not much point in propagating relocs to shared
4297 libs that the dynamic linker won't relocate. */
4298 if ((sec->flags & SEC_ALLOC) == 0)
4299 return TRUE;
4301 htab = ppc_hash_table (info);
4302 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4304 sym_hashes = elf_sym_hashes (abfd);
4305 sym_hashes_end = (sym_hashes
4306 + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
4307 - symtab_hdr->sh_info);
4309 sreloc = NULL;
4310 opd_sym_map = NULL;
4311 if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
4313 /* Garbage collection needs some extra help with .opd sections.
4314 We don't want to necessarily keep everything referenced by
4315 relocs in .opd, as that would keep all functions. Instead,
4316 if we reference an .opd symbol (a function descriptor), we
4317 want to keep the function code symbol's section. This is
4318 easy for global symbols, but for local syms we need to keep
4319 information about the associated function section. Later, if
4320 edit_opd deletes entries, we'll use this array to adjust
4321 local syms in .opd. */
4322 union opd_info {
4323 asection *func_section;
4324 long entry_adjust;
4326 bfd_size_type amt;
4328 amt = sec->size * sizeof (union opd_info) / 8;
4329 opd_sym_map = bfd_zalloc (abfd, amt);
4330 if (opd_sym_map == NULL)
4331 return FALSE;
4332 ppc64_elf_section_data (sec)->opd.func_sec = opd_sym_map;
4335 if (htab->sfpr == NULL
4336 && !create_linkage_sections (htab->elf.dynobj, info))
4337 return FALSE;
4339 rel_end = relocs + sec->reloc_count;
4340 for (rel = relocs; rel < rel_end; rel++)
4342 unsigned long r_symndx;
4343 struct elf_link_hash_entry *h;
4344 enum elf_ppc64_reloc_type r_type;
4345 int tls_type = 0;
4347 r_symndx = ELF64_R_SYM (rel->r_info);
4348 if (r_symndx < symtab_hdr->sh_info)
4349 h = NULL;
4350 else
4352 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4353 while (h->root.type == bfd_link_hash_indirect
4354 || h->root.type == bfd_link_hash_warning)
4355 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4358 r_type = ELF64_R_TYPE (rel->r_info);
4359 switch (r_type)
4361 case R_PPC64_GOT_TLSLD16:
4362 case R_PPC64_GOT_TLSLD16_LO:
4363 case R_PPC64_GOT_TLSLD16_HI:
4364 case R_PPC64_GOT_TLSLD16_HA:
4365 ppc64_tlsld_got (abfd)->refcount += 1;
4366 tls_type = TLS_TLS | TLS_LD;
4367 goto dogottls;
4369 case R_PPC64_GOT_TLSGD16:
4370 case R_PPC64_GOT_TLSGD16_LO:
4371 case R_PPC64_GOT_TLSGD16_HI:
4372 case R_PPC64_GOT_TLSGD16_HA:
4373 tls_type = TLS_TLS | TLS_GD;
4374 goto dogottls;
4376 case R_PPC64_GOT_TPREL16_DS:
4377 case R_PPC64_GOT_TPREL16_LO_DS:
4378 case R_PPC64_GOT_TPREL16_HI:
4379 case R_PPC64_GOT_TPREL16_HA:
4380 if (info->shared)
4381 info->flags |= DF_STATIC_TLS;
4382 tls_type = TLS_TLS | TLS_TPREL;
4383 goto dogottls;
4385 case R_PPC64_GOT_DTPREL16_DS:
4386 case R_PPC64_GOT_DTPREL16_LO_DS:
4387 case R_PPC64_GOT_DTPREL16_HI:
4388 case R_PPC64_GOT_DTPREL16_HA:
4389 tls_type = TLS_TLS | TLS_DTPREL;
4390 dogottls:
4391 sec->has_tls_reloc = 1;
4392 /* Fall thru */
4394 case R_PPC64_GOT16:
4395 case R_PPC64_GOT16_DS:
4396 case R_PPC64_GOT16_HA:
4397 case R_PPC64_GOT16_HI:
4398 case R_PPC64_GOT16_LO:
4399 case R_PPC64_GOT16_LO_DS:
4400 /* This symbol requires a global offset table entry. */
4401 sec->has_toc_reloc = 1;
4402 if (ppc64_elf_tdata (abfd)->got == NULL
4403 && !create_got_section (abfd, info))
4404 return FALSE;
4406 if (h != NULL)
4408 struct ppc_link_hash_entry *eh;
4409 struct got_entry *ent;
4411 eh = (struct ppc_link_hash_entry *) h;
4412 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4413 if (ent->addend == rel->r_addend
4414 && ent->owner == abfd
4415 && ent->tls_type == tls_type)
4416 break;
4417 if (ent == NULL)
4419 bfd_size_type amt = sizeof (*ent);
4420 ent = bfd_alloc (abfd, amt);
4421 if (ent == NULL)
4422 return FALSE;
4423 ent->next = eh->elf.got.glist;
4424 ent->addend = rel->r_addend;
4425 ent->owner = abfd;
4426 ent->tls_type = tls_type;
4427 ent->got.refcount = 0;
4428 eh->elf.got.glist = ent;
4430 ent->got.refcount += 1;
4431 eh->tls_mask |= tls_type;
4433 else
4434 /* This is a global offset table entry for a local symbol. */
4435 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4436 rel->r_addend, tls_type))
4437 return FALSE;
4438 break;
4440 case R_PPC64_PLT16_HA:
4441 case R_PPC64_PLT16_HI:
4442 case R_PPC64_PLT16_LO:
4443 case R_PPC64_PLT32:
4444 case R_PPC64_PLT64:
4445 /* This symbol requires a procedure linkage table entry. We
4446 actually build the entry in adjust_dynamic_symbol,
4447 because this might be a case of linking PIC code without
4448 linking in any dynamic objects, in which case we don't
4449 need to generate a procedure linkage table after all. */
4450 if (h == NULL)
4452 /* It does not make sense to have a procedure linkage
4453 table entry for a local symbol. */
4454 bfd_set_error (bfd_error_bad_value);
4455 return FALSE;
4457 else
4458 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4459 rel->r_addend))
4460 return FALSE;
4461 break;
4463 /* The following relocations don't need to propagate the
4464 relocation if linking a shared object since they are
4465 section relative. */
4466 case R_PPC64_SECTOFF:
4467 case R_PPC64_SECTOFF_LO:
4468 case R_PPC64_SECTOFF_HI:
4469 case R_PPC64_SECTOFF_HA:
4470 case R_PPC64_SECTOFF_DS:
4471 case R_PPC64_SECTOFF_LO_DS:
4472 case R_PPC64_DTPREL16:
4473 case R_PPC64_DTPREL16_LO:
4474 case R_PPC64_DTPREL16_HI:
4475 case R_PPC64_DTPREL16_HA:
4476 case R_PPC64_DTPREL16_DS:
4477 case R_PPC64_DTPREL16_LO_DS:
4478 case R_PPC64_DTPREL16_HIGHER:
4479 case R_PPC64_DTPREL16_HIGHERA:
4480 case R_PPC64_DTPREL16_HIGHEST:
4481 case R_PPC64_DTPREL16_HIGHESTA:
4482 break;
4484 /* Nor do these. */
4485 case R_PPC64_TOC16:
4486 case R_PPC64_TOC16_LO:
4487 case R_PPC64_TOC16_HI:
4488 case R_PPC64_TOC16_HA:
4489 case R_PPC64_TOC16_DS:
4490 case R_PPC64_TOC16_LO_DS:
4491 sec->has_toc_reloc = 1;
4492 break;
4494 /* This relocation describes the C++ object vtable hierarchy.
4495 Reconstruct it for later use during GC. */
4496 case R_PPC64_GNU_VTINHERIT:
4497 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4498 return FALSE;
4499 break;
4501 /* This relocation describes which C++ vtable entries are actually
4502 used. Record for later use during GC. */
4503 case R_PPC64_GNU_VTENTRY:
4504 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4505 return FALSE;
4506 break;
4508 case R_PPC64_REL14:
4509 case R_PPC64_REL14_BRTAKEN:
4510 case R_PPC64_REL14_BRNTAKEN:
4512 asection *dest = NULL;
4514 /* Heuristic: If jumping outside our section, chances are
4515 we are going to need a stub. */
4516 if (h != NULL)
4518 /* If the sym is weak it may be overridden later, so
4519 don't assume we know where a weak sym lives. */
4520 if (h->root.type == bfd_link_hash_defined)
4521 dest = h->root.u.def.section;
4523 else
4524 dest = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4525 sec, r_symndx);
4526 if (dest != sec)
4527 htab->has_14bit_branch = 1;
4529 /* Fall through. */
4531 case R_PPC64_REL24:
4532 if (h != NULL)
4534 /* We may need a .plt entry if the function this reloc
4535 refers to is in a shared lib. */
4536 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4537 rel->r_addend))
4538 return FALSE;
4539 if (h == &htab->tls_get_addr->elf
4540 || h == &htab->tls_get_addr_fd->elf)
4541 sec->has_tls_reloc = 1;
4542 else if (htab->tls_get_addr == NULL
4543 && !strncmp (h->root.root.string, ".__tls_get_addr", 15)
4544 && (h->root.root.string[15] == 0
4545 || h->root.root.string[15] == '@'))
4547 htab->tls_get_addr = (struct ppc_link_hash_entry *) h;
4548 sec->has_tls_reloc = 1;
4550 else if (htab->tls_get_addr_fd == NULL
4551 && !strncmp (h->root.root.string, "__tls_get_addr", 14)
4552 && (h->root.root.string[14] == 0
4553 || h->root.root.string[14] == '@'))
4555 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) h;
4556 sec->has_tls_reloc = 1;
4559 break;
4561 case R_PPC64_TPREL64:
4562 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4563 if (info->shared)
4564 info->flags |= DF_STATIC_TLS;
4565 goto dotlstoc;
4567 case R_PPC64_DTPMOD64:
4568 if (rel + 1 < rel_end
4569 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4570 && rel[1].r_offset == rel->r_offset + 8)
4571 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4572 else
4573 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4574 goto dotlstoc;
4576 case R_PPC64_DTPREL64:
4577 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4578 if (rel != relocs
4579 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4580 && rel[-1].r_offset == rel->r_offset - 8)
4581 /* This is the second reloc of a dtpmod, dtprel pair.
4582 Don't mark with TLS_DTPREL. */
4583 goto dodyn;
4585 dotlstoc:
4586 sec->has_tls_reloc = 1;
4587 if (h != NULL)
4589 struct ppc_link_hash_entry *eh;
4590 eh = (struct ppc_link_hash_entry *) h;
4591 eh->tls_mask |= tls_type;
4593 else
4594 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4595 rel->r_addend, tls_type))
4596 return FALSE;
4598 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
4600 /* One extra to simplify get_tls_mask. */
4601 bfd_size_type amt = sec->size * sizeof (unsigned) / 8 + 1;
4602 ppc64_elf_section_data (sec)->t_symndx = bfd_zalloc (abfd, amt);
4603 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
4604 return FALSE;
4606 BFD_ASSERT (rel->r_offset % 8 == 0);
4607 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8] = r_symndx;
4609 /* Mark the second slot of a GD or LD entry.
4610 -1 to indicate GD and -2 to indicate LD. */
4611 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4612 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -1;
4613 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4614 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -2;
4615 goto dodyn;
4617 case R_PPC64_TPREL16:
4618 case R_PPC64_TPREL16_LO:
4619 case R_PPC64_TPREL16_HI:
4620 case R_PPC64_TPREL16_HA:
4621 case R_PPC64_TPREL16_DS:
4622 case R_PPC64_TPREL16_LO_DS:
4623 case R_PPC64_TPREL16_HIGHER:
4624 case R_PPC64_TPREL16_HIGHERA:
4625 case R_PPC64_TPREL16_HIGHEST:
4626 case R_PPC64_TPREL16_HIGHESTA:
4627 if (info->shared)
4629 info->flags |= DF_STATIC_TLS;
4630 goto dodyn;
4632 break;
4634 case R_PPC64_ADDR64:
4635 if (opd_sym_map != NULL
4636 && rel + 1 < rel_end
4637 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
4639 if (h != NULL)
4641 if (h->root.root.string[0] == '.'
4642 && h->root.root.string[1] != 0
4643 && get_fdh ((struct ppc_link_hash_entry *) h, htab))
4645 else
4646 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4648 else
4650 asection *s;
4652 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
4653 r_symndx);
4654 if (s == NULL)
4655 return FALSE;
4656 else if (s != sec)
4657 opd_sym_map[rel->r_offset / 8] = s;
4660 /* Fall through. */
4662 case R_PPC64_REL30:
4663 case R_PPC64_REL32:
4664 case R_PPC64_REL64:
4665 case R_PPC64_ADDR14:
4666 case R_PPC64_ADDR14_BRNTAKEN:
4667 case R_PPC64_ADDR14_BRTAKEN:
4668 case R_PPC64_ADDR16:
4669 case R_PPC64_ADDR16_DS:
4670 case R_PPC64_ADDR16_HA:
4671 case R_PPC64_ADDR16_HI:
4672 case R_PPC64_ADDR16_HIGHER:
4673 case R_PPC64_ADDR16_HIGHERA:
4674 case R_PPC64_ADDR16_HIGHEST:
4675 case R_PPC64_ADDR16_HIGHESTA:
4676 case R_PPC64_ADDR16_LO:
4677 case R_PPC64_ADDR16_LO_DS:
4678 case R_PPC64_ADDR24:
4679 case R_PPC64_ADDR32:
4680 case R_PPC64_UADDR16:
4681 case R_PPC64_UADDR32:
4682 case R_PPC64_UADDR64:
4683 case R_PPC64_TOC:
4684 if (h != NULL && !info->shared)
4685 /* We may need a copy reloc. */
4686 h->non_got_ref = 1;
4688 /* Don't propagate .opd relocs. */
4689 if (NO_OPD_RELOCS && opd_sym_map != NULL)
4690 break;
4692 /* If we are creating a shared library, and this is a reloc
4693 against a global symbol, or a non PC relative reloc
4694 against a local symbol, then we need to copy the reloc
4695 into the shared library. However, if we are linking with
4696 -Bsymbolic, we do not need to copy a reloc against a
4697 global symbol which is defined in an object we are
4698 including in the link (i.e., DEF_REGULAR is set). At
4699 this point we have not seen all the input files, so it is
4700 possible that DEF_REGULAR is not set now but will be set
4701 later (it is never cleared). In case of a weak definition,
4702 DEF_REGULAR may be cleared later by a strong definition in
4703 a shared library. We account for that possibility below by
4704 storing information in the dyn_relocs field of the hash
4705 table entry. A similar situation occurs when creating
4706 shared libraries and symbol visibility changes render the
4707 symbol local.
4709 If on the other hand, we are creating an executable, we
4710 may need to keep relocations for symbols satisfied by a
4711 dynamic library if we manage to avoid copy relocs for the
4712 symbol. */
4713 dodyn:
4714 if ((info->shared
4715 && (MUST_BE_DYN_RELOC (r_type)
4716 || (h != NULL
4717 && (! info->symbolic
4718 || h->root.type == bfd_link_hash_defweak
4719 || !h->def_regular))))
4720 || (ELIMINATE_COPY_RELOCS
4721 && !info->shared
4722 && h != NULL
4723 && (h->root.type == bfd_link_hash_defweak
4724 || !h->def_regular)))
4726 struct ppc_dyn_relocs *p;
4727 struct ppc_dyn_relocs **head;
4729 /* We must copy these reloc types into the output file.
4730 Create a reloc section in dynobj and make room for
4731 this reloc. */
4732 if (sreloc == NULL)
4734 const char *name;
4735 bfd *dynobj;
4737 name = (bfd_elf_string_from_elf_section
4738 (abfd,
4739 elf_elfheader (abfd)->e_shstrndx,
4740 elf_section_data (sec)->rel_hdr.sh_name));
4741 if (name == NULL)
4742 return FALSE;
4744 if (strncmp (name, ".rela", 5) != 0
4745 || strcmp (bfd_get_section_name (abfd, sec),
4746 name + 5) != 0)
4748 (*_bfd_error_handler)
4749 (_("%B: bad relocation section name `%s\'"),
4750 abfd, name);
4751 bfd_set_error (bfd_error_bad_value);
4754 dynobj = htab->elf.dynobj;
4755 sreloc = bfd_get_section_by_name (dynobj, name);
4756 if (sreloc == NULL)
4758 flagword flags;
4760 flags = (SEC_HAS_CONTENTS | SEC_READONLY
4761 | SEC_IN_MEMORY | SEC_LINKER_CREATED
4762 | SEC_ALLOC | SEC_LOAD);
4763 sreloc = bfd_make_section_with_flags (dynobj,
4764 name,
4765 flags);
4766 if (sreloc == NULL
4767 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
4768 return FALSE;
4770 elf_section_data (sec)->sreloc = sreloc;
4773 /* If this is a global symbol, we count the number of
4774 relocations we need for this symbol. */
4775 if (h != NULL)
4777 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
4779 else
4781 /* Track dynamic relocs needed for local syms too.
4782 We really need local syms available to do this
4783 easily. Oh well. */
4785 asection *s;
4786 void *vpp;
4788 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4789 sec, r_symndx);
4790 if (s == NULL)
4791 return FALSE;
4793 vpp = &elf_section_data (s)->local_dynrel;
4794 head = (struct ppc_dyn_relocs **) vpp;
4797 p = *head;
4798 if (p == NULL || p->sec != sec)
4800 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4801 if (p == NULL)
4802 return FALSE;
4803 p->next = *head;
4804 *head = p;
4805 p->sec = sec;
4806 p->count = 0;
4807 p->pc_count = 0;
4810 p->count += 1;
4811 if (!MUST_BE_DYN_RELOC (r_type))
4812 p->pc_count += 1;
4814 break;
4816 default:
4817 break;
4821 return TRUE;
4824 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
4825 of the code entry point, and its section. */
4827 static bfd_vma
4828 opd_entry_value (asection *opd_sec,
4829 bfd_vma offset,
4830 asection **code_sec,
4831 bfd_vma *code_off)
4833 bfd *opd_bfd = opd_sec->owner;
4834 Elf_Internal_Rela *relocs;
4835 Elf_Internal_Rela *lo, *hi, *look;
4836 bfd_vma val;
4838 /* No relocs implies we are linking a --just-symbols object. */
4839 if (opd_sec->reloc_count == 0)
4841 bfd_vma val;
4843 if (!bfd_get_section_contents (opd_bfd, opd_sec, &val, offset, 8))
4844 return (bfd_vma) -1;
4846 if (code_sec != NULL)
4848 asection *sec, *likely = NULL;
4849 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
4850 if (sec->vma <= val
4851 && (sec->flags & SEC_LOAD) != 0
4852 && (sec->flags & SEC_ALLOC) != 0)
4853 likely = sec;
4854 if (likely != NULL)
4856 *code_sec = likely;
4857 if (code_off != NULL)
4858 *code_off = val - likely->vma;
4861 return val;
4864 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
4865 if (relocs == NULL)
4866 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
4868 /* Go find the opd reloc at the sym address. */
4869 lo = relocs;
4870 BFD_ASSERT (lo != NULL);
4871 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
4872 val = (bfd_vma) -1;
4873 while (lo < hi)
4875 look = lo + (hi - lo) / 2;
4876 if (look->r_offset < offset)
4877 lo = look + 1;
4878 else if (look->r_offset > offset)
4879 hi = look;
4880 else
4882 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (opd_bfd)->symtab_hdr;
4883 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
4884 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
4886 unsigned long symndx = ELF64_R_SYM (look->r_info);
4887 asection *sec;
4889 if (symndx < symtab_hdr->sh_info)
4891 Elf_Internal_Sym *sym;
4893 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
4894 if (sym == NULL)
4896 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
4897 symtab_hdr->sh_info,
4898 0, NULL, NULL, NULL);
4899 if (sym == NULL)
4900 break;
4901 symtab_hdr->contents = (bfd_byte *) sym;
4904 sym += symndx;
4905 val = sym->st_value;
4906 sec = NULL;
4907 if ((sym->st_shndx != SHN_UNDEF
4908 && sym->st_shndx < SHN_LORESERVE)
4909 || sym->st_shndx > SHN_HIRESERVE)
4910 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
4911 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
4913 else
4915 struct elf_link_hash_entry **sym_hashes;
4916 struct elf_link_hash_entry *rh;
4918 sym_hashes = elf_sym_hashes (opd_bfd);
4919 rh = sym_hashes[symndx - symtab_hdr->sh_info];
4920 while (rh->root.type == bfd_link_hash_indirect
4921 || rh->root.type == bfd_link_hash_warning)
4922 rh = ((struct elf_link_hash_entry *) rh->root.u.i.link);
4923 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
4924 || rh->root.type == bfd_link_hash_defweak);
4925 val = rh->root.u.def.value;
4926 sec = rh->root.u.def.section;
4928 val += look->r_addend;
4929 if (code_off != NULL)
4930 *code_off = val;
4931 if (code_sec != NULL)
4932 *code_sec = sec;
4933 if (sec != NULL && sec->output_section != NULL)
4934 val += sec->output_section->vma + sec->output_offset;
4936 break;
4940 return val;
4943 /* Mark sections containing dynamically referenced symbols. When
4944 building shared libraries, we must assume that any visible symbol is
4945 referenced. */
4947 static bfd_boolean
4948 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
4950 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4951 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
4953 if (eh->elf.root.type == bfd_link_hash_warning)
4954 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4956 /* Dynamic linking info is on the func descriptor sym. */
4957 if (eh->oh != NULL
4958 && eh->oh->is_func_descriptor
4959 && (eh->oh->elf.root.type == bfd_link_hash_defined
4960 || eh->oh->elf.root.type == bfd_link_hash_defweak))
4961 eh = eh->oh;
4963 if ((eh->elf.root.type == bfd_link_hash_defined
4964 || eh->elf.root.type == bfd_link_hash_defweak)
4965 && (eh->elf.ref_dynamic
4966 || (!info->executable
4967 && eh->elf.def_regular
4968 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4969 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
4971 asection *code_sec;
4973 eh->elf.root.u.def.section->flags |= SEC_KEEP;
4975 /* Function descriptor syms cause the associated
4976 function code sym section to be marked. */
4977 if (eh->is_func_descriptor
4978 && (eh->oh->elf.root.type == bfd_link_hash_defined
4979 || eh->oh->elf.root.type == bfd_link_hash_defweak))
4980 eh->oh->elf.root.u.def.section->flags |= SEC_KEEP;
4981 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
4982 && opd_entry_value (eh->elf.root.u.def.section,
4983 eh->elf.root.u.def.value,
4984 &code_sec, NULL) != (bfd_vma) -1)
4985 code_sec->flags |= SEC_KEEP;
4988 return TRUE;
4991 /* Return the section that should be marked against GC for a given
4992 relocation. */
4994 static asection *
4995 ppc64_elf_gc_mark_hook (asection *sec,
4996 struct bfd_link_info *info,
4997 Elf_Internal_Rela *rel,
4998 struct elf_link_hash_entry *h,
4999 Elf_Internal_Sym *sym)
5001 asection *rsec;
5003 /* First mark all our entry sym sections. */
5004 if (info->gc_sym_list != NULL)
5006 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5007 struct bfd_sym_chain *sym = info->gc_sym_list;
5009 info->gc_sym_list = NULL;
5010 for (; sym != NULL; sym = sym->next)
5012 struct ppc_link_hash_entry *eh;
5014 eh = (struct ppc_link_hash_entry *)
5015 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
5016 if (eh == NULL)
5017 continue;
5018 if (eh->elf.root.type != bfd_link_hash_defined
5019 && eh->elf.root.type != bfd_link_hash_defweak)
5020 continue;
5022 if (eh->is_func_descriptor
5023 && (eh->oh->elf.root.type == bfd_link_hash_defined
5024 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5025 rsec = eh->oh->elf.root.u.def.section;
5026 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5027 && opd_entry_value (eh->elf.root.u.def.section,
5028 eh->elf.root.u.def.value,
5029 &rsec, NULL) != (bfd_vma) -1)
5031 else
5032 continue;
5034 if (!rsec->gc_mark)
5035 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5037 rsec = eh->elf.root.u.def.section;
5038 if (!rsec->gc_mark)
5039 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5043 /* Syms return NULL if we're marking .opd, so we avoid marking all
5044 function sections, as all functions are referenced in .opd. */
5045 rsec = NULL;
5046 if (get_opd_info (sec) != NULL)
5047 return rsec;
5049 if (h != NULL)
5051 enum elf_ppc64_reloc_type r_type;
5052 struct ppc_link_hash_entry *eh;
5054 r_type = ELF64_R_TYPE (rel->r_info);
5055 switch (r_type)
5057 case R_PPC64_GNU_VTINHERIT:
5058 case R_PPC64_GNU_VTENTRY:
5059 break;
5061 default:
5062 switch (h->root.type)
5064 case bfd_link_hash_defined:
5065 case bfd_link_hash_defweak:
5066 eh = (struct ppc_link_hash_entry *) h;
5067 if (eh->oh != NULL
5068 && eh->oh->is_func_descriptor
5069 && (eh->oh->elf.root.type == bfd_link_hash_defined
5070 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5071 eh = eh->oh;
5073 /* Function descriptor syms cause the associated
5074 function code sym section to be marked. */
5075 if (eh->is_func_descriptor
5076 && (eh->oh->elf.root.type == bfd_link_hash_defined
5077 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5079 /* They also mark their opd section. */
5080 if (!eh->elf.root.u.def.section->gc_mark)
5081 _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5082 ppc64_elf_gc_mark_hook);
5084 rsec = eh->oh->elf.root.u.def.section;
5086 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5087 && opd_entry_value (eh->elf.root.u.def.section,
5088 eh->elf.root.u.def.value,
5089 &rsec, NULL) != (bfd_vma) -1)
5091 if (!eh->elf.root.u.def.section->gc_mark)
5092 _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5093 ppc64_elf_gc_mark_hook);
5095 else
5096 rsec = h->root.u.def.section;
5097 break;
5099 case bfd_link_hash_common:
5100 rsec = h->root.u.c.p->section;
5101 break;
5103 default:
5104 break;
5108 else
5110 asection **opd_sym_section;
5112 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5113 opd_sym_section = get_opd_info (rsec);
5114 if (opd_sym_section != NULL)
5116 if (!rsec->gc_mark)
5117 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5119 rsec = opd_sym_section[(sym->st_value + rel->r_addend) / 8];
5123 return rsec;
5126 /* Update the .got, .plt. and dynamic reloc reference counts for the
5127 section being removed. */
5129 static bfd_boolean
5130 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5131 asection *sec, const Elf_Internal_Rela *relocs)
5133 struct ppc_link_hash_table *htab;
5134 Elf_Internal_Shdr *symtab_hdr;
5135 struct elf_link_hash_entry **sym_hashes;
5136 struct got_entry **local_got_ents;
5137 const Elf_Internal_Rela *rel, *relend;
5139 if ((sec->flags & SEC_ALLOC) == 0)
5140 return TRUE;
5142 elf_section_data (sec)->local_dynrel = NULL;
5144 htab = ppc_hash_table (info);
5145 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5146 sym_hashes = elf_sym_hashes (abfd);
5147 local_got_ents = elf_local_got_ents (abfd);
5149 relend = relocs + sec->reloc_count;
5150 for (rel = relocs; rel < relend; rel++)
5152 unsigned long r_symndx;
5153 enum elf_ppc64_reloc_type r_type;
5154 struct elf_link_hash_entry *h = NULL;
5155 char tls_type = 0;
5157 r_symndx = ELF64_R_SYM (rel->r_info);
5158 r_type = ELF64_R_TYPE (rel->r_info);
5159 if (r_symndx >= symtab_hdr->sh_info)
5161 struct ppc_link_hash_entry *eh;
5162 struct ppc_dyn_relocs **pp;
5163 struct ppc_dyn_relocs *p;
5165 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5166 while (h->root.type == bfd_link_hash_indirect
5167 || h->root.type == bfd_link_hash_warning)
5168 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5169 eh = (struct ppc_link_hash_entry *) h;
5171 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5172 if (p->sec == sec)
5174 /* Everything must go for SEC. */
5175 *pp = p->next;
5176 break;
5180 switch (r_type)
5182 case R_PPC64_GOT_TLSLD16:
5183 case R_PPC64_GOT_TLSLD16_LO:
5184 case R_PPC64_GOT_TLSLD16_HI:
5185 case R_PPC64_GOT_TLSLD16_HA:
5186 ppc64_tlsld_got (abfd)->refcount -= 1;
5187 tls_type = TLS_TLS | TLS_LD;
5188 goto dogot;
5190 case R_PPC64_GOT_TLSGD16:
5191 case R_PPC64_GOT_TLSGD16_LO:
5192 case R_PPC64_GOT_TLSGD16_HI:
5193 case R_PPC64_GOT_TLSGD16_HA:
5194 tls_type = TLS_TLS | TLS_GD;
5195 goto dogot;
5197 case R_PPC64_GOT_TPREL16_DS:
5198 case R_PPC64_GOT_TPREL16_LO_DS:
5199 case R_PPC64_GOT_TPREL16_HI:
5200 case R_PPC64_GOT_TPREL16_HA:
5201 tls_type = TLS_TLS | TLS_TPREL;
5202 goto dogot;
5204 case R_PPC64_GOT_DTPREL16_DS:
5205 case R_PPC64_GOT_DTPREL16_LO_DS:
5206 case R_PPC64_GOT_DTPREL16_HI:
5207 case R_PPC64_GOT_DTPREL16_HA:
5208 tls_type = TLS_TLS | TLS_DTPREL;
5209 goto dogot;
5211 case R_PPC64_GOT16:
5212 case R_PPC64_GOT16_DS:
5213 case R_PPC64_GOT16_HA:
5214 case R_PPC64_GOT16_HI:
5215 case R_PPC64_GOT16_LO:
5216 case R_PPC64_GOT16_LO_DS:
5217 dogot:
5219 struct got_entry *ent;
5221 if (h != NULL)
5222 ent = h->got.glist;
5223 else
5224 ent = local_got_ents[r_symndx];
5226 for (; ent != NULL; ent = ent->next)
5227 if (ent->addend == rel->r_addend
5228 && ent->owner == abfd
5229 && ent->tls_type == tls_type)
5230 break;
5231 if (ent == NULL)
5232 abort ();
5233 if (ent->got.refcount > 0)
5234 ent->got.refcount -= 1;
5236 break;
5238 case R_PPC64_PLT16_HA:
5239 case R_PPC64_PLT16_HI:
5240 case R_PPC64_PLT16_LO:
5241 case R_PPC64_PLT32:
5242 case R_PPC64_PLT64:
5243 case R_PPC64_REL14:
5244 case R_PPC64_REL14_BRNTAKEN:
5245 case R_PPC64_REL14_BRTAKEN:
5246 case R_PPC64_REL24:
5247 if (h != NULL)
5249 struct plt_entry *ent;
5251 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5252 if (ent->addend == rel->r_addend)
5253 break;
5254 if (ent == NULL)
5255 abort ();
5256 if (ent->plt.refcount > 0)
5257 ent->plt.refcount -= 1;
5259 break;
5261 default:
5262 break;
5265 return TRUE;
5268 /* The maximum size of .sfpr. */
5269 #define SFPR_MAX (218*4)
5271 struct sfpr_def_parms
5273 const char name[12];
5274 unsigned char lo, hi;
5275 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5276 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5279 /* Auto-generate _save*, _rest* functions in .sfpr. */
5281 static unsigned int
5282 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5284 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5285 unsigned int i;
5286 size_t len = strlen (parm->name);
5287 bfd_boolean writing = FALSE;
5288 char sym[16];
5290 memcpy (sym, parm->name, len);
5291 sym[len + 2] = 0;
5293 for (i = parm->lo; i <= parm->hi; i++)
5295 struct elf_link_hash_entry *h;
5297 sym[len + 0] = i / 10 + '0';
5298 sym[len + 1] = i % 10 + '0';
5299 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
5300 if (h != NULL
5301 && !h->def_regular)
5303 h->root.type = bfd_link_hash_defined;
5304 h->root.u.def.section = htab->sfpr;
5305 h->root.u.def.value = htab->sfpr->size;
5306 h->type = STT_FUNC;
5307 h->def_regular = 1;
5308 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
5309 writing = TRUE;
5310 if (htab->sfpr->contents == NULL)
5312 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5313 if (htab->sfpr->contents == NULL)
5314 return FALSE;
5317 if (writing)
5319 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5320 if (i != parm->hi)
5321 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5322 else
5323 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5324 htab->sfpr->size = p - htab->sfpr->contents;
5328 return TRUE;
5331 static bfd_byte *
5332 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5334 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5335 return p + 4;
5338 static bfd_byte *
5339 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5341 p = savegpr0 (abfd, p, r);
5342 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5343 p = p + 4;
5344 bfd_put_32 (abfd, BLR, p);
5345 return p + 4;
5348 static bfd_byte *
5349 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5351 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5352 return p + 4;
5355 static bfd_byte *
5356 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5358 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5359 p = p + 4;
5360 p = restgpr0 (abfd, p, r);
5361 bfd_put_32 (abfd, MTLR_R0, p);
5362 p = p + 4;
5363 if (r == 29)
5365 p = restgpr0 (abfd, p, 30);
5366 p = restgpr0 (abfd, p, 31);
5368 bfd_put_32 (abfd, BLR, p);
5369 return p + 4;
5372 static bfd_byte *
5373 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5375 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5376 return p + 4;
5379 static bfd_byte *
5380 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5382 p = savegpr1 (abfd, p, r);
5383 bfd_put_32 (abfd, BLR, p);
5384 return p + 4;
5387 static bfd_byte *
5388 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5390 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5391 return p + 4;
5394 static bfd_byte *
5395 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5397 p = restgpr1 (abfd, p, r);
5398 bfd_put_32 (abfd, BLR, p);
5399 return p + 4;
5402 static bfd_byte *
5403 savefpr (bfd *abfd, bfd_byte *p, int r)
5405 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5406 return p + 4;
5409 static bfd_byte *
5410 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5412 p = savefpr (abfd, p, r);
5413 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5414 p = p + 4;
5415 bfd_put_32 (abfd, BLR, p);
5416 return p + 4;
5419 static bfd_byte *
5420 restfpr (bfd *abfd, bfd_byte *p, int r)
5422 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5423 return p + 4;
5426 static bfd_byte *
5427 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5429 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5430 p = p + 4;
5431 p = restfpr (abfd, p, r);
5432 bfd_put_32 (abfd, MTLR_R0, p);
5433 p = p + 4;
5434 if (r == 29)
5436 p = restfpr (abfd, p, 30);
5437 p = restfpr (abfd, p, 31);
5439 bfd_put_32 (abfd, BLR, p);
5440 return p + 4;
5443 static bfd_byte *
5444 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5446 p = savefpr (abfd, p, r);
5447 bfd_put_32 (abfd, BLR, p);
5448 return p + 4;
5451 static bfd_byte *
5452 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5454 p = restfpr (abfd, p, r);
5455 bfd_put_32 (abfd, BLR, p);
5456 return p + 4;
5459 static bfd_byte *
5460 savevr (bfd *abfd, bfd_byte *p, int r)
5462 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5463 p = p + 4;
5464 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5465 return p + 4;
5468 static bfd_byte *
5469 savevr_tail (bfd *abfd, bfd_byte *p, int r)
5471 p = savevr (abfd, p, r);
5472 bfd_put_32 (abfd, BLR, p);
5473 return p + 4;
5476 static bfd_byte *
5477 restvr (bfd *abfd, bfd_byte *p, int r)
5479 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5480 p = p + 4;
5481 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5482 return p + 4;
5485 static bfd_byte *
5486 restvr_tail (bfd *abfd, bfd_byte *p, int r)
5488 p = restvr (abfd, p, r);
5489 bfd_put_32 (abfd, BLR, p);
5490 return p + 4;
5493 /* Called via elf_link_hash_traverse to transfer dynamic linking
5494 information on function code symbol entries to their corresponding
5495 function descriptor symbol entries. */
5497 static bfd_boolean
5498 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5500 struct bfd_link_info *info;
5501 struct ppc_link_hash_table *htab;
5502 struct plt_entry *ent;
5503 struct ppc_link_hash_entry *fh;
5504 struct ppc_link_hash_entry *fdh;
5505 bfd_boolean force_local;
5507 fh = (struct ppc_link_hash_entry *) h;
5508 if (fh->elf.root.type == bfd_link_hash_indirect)
5509 return TRUE;
5511 if (fh->elf.root.type == bfd_link_hash_warning)
5512 fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
5514 info = inf;
5515 htab = ppc_hash_table (info);
5517 /* Resolve undefined references to dot-symbols as the value
5518 in the function descriptor, if we have one in a regular object.
5519 This is to satisfy cases like ".quad .foo". Calls to functions
5520 in dynamic objects are handled elsewhere. */
5521 if (fh->elf.root.type == bfd_link_hash_undefweak
5522 && fh->was_undefined
5523 && (fh->oh->elf.root.type == bfd_link_hash_defined
5524 || fh->oh->elf.root.type == bfd_link_hash_defweak)
5525 && get_opd_info (fh->oh->elf.root.u.def.section) != NULL
5526 && opd_entry_value (fh->oh->elf.root.u.def.section,
5527 fh->oh->elf.root.u.def.value,
5528 &fh->elf.root.u.def.section,
5529 &fh->elf.root.u.def.value) != (bfd_vma) -1)
5531 fh->elf.root.type = fh->oh->elf.root.type;
5532 fh->elf.forced_local = 1;
5535 /* If this is a function code symbol, transfer dynamic linking
5536 information to the function descriptor symbol. */
5537 if (!fh->is_func)
5538 return TRUE;
5540 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5541 if (ent->plt.refcount > 0)
5542 break;
5543 if (ent == NULL
5544 || fh->elf.root.root.string[0] != '.'
5545 || fh->elf.root.root.string[1] == '\0')
5546 return TRUE;
5548 /* Find the corresponding function descriptor symbol. Create it
5549 as undefined if necessary. */
5551 fdh = get_fdh (fh, htab);
5552 if (fdh != NULL)
5553 while (fdh->elf.root.type == bfd_link_hash_indirect
5554 || fdh->elf.root.type == bfd_link_hash_warning)
5555 fdh = (struct ppc_link_hash_entry *) fdh->elf.root.u.i.link;
5557 if (fdh == NULL
5558 && info->shared
5559 && (fh->elf.root.type == bfd_link_hash_undefined
5560 || fh->elf.root.type == bfd_link_hash_undefweak))
5562 fdh = make_fdh (info, fh);
5563 if (fdh == NULL)
5564 return FALSE;
5567 /* Fake function descriptors are made undefweak. If the function
5568 code symbol is strong undefined, make the fake sym the same.
5569 If the function code symbol is defined, then force the fake
5570 descriptor local; We can't support overriding of symbols in a
5571 shared library on a fake descriptor. */
5573 if (fdh != NULL
5574 && fdh->fake
5575 && fdh->elf.root.type == bfd_link_hash_undefweak)
5577 if (fh->elf.root.type == bfd_link_hash_undefined)
5579 fdh->elf.root.type = bfd_link_hash_undefined;
5580 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
5582 else if (fh->elf.root.type == bfd_link_hash_defined
5583 || fh->elf.root.type == bfd_link_hash_defweak)
5585 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
5589 if (fdh != NULL
5590 && !fdh->elf.forced_local
5591 && (info->shared
5592 || fdh->elf.def_dynamic
5593 || fdh->elf.ref_dynamic
5594 || (fdh->elf.root.type == bfd_link_hash_undefweak
5595 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
5597 if (fdh->elf.dynindx == -1)
5598 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5599 return FALSE;
5600 fdh->elf.ref_regular |= fh->elf.ref_regular;
5601 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5602 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5603 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
5604 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
5606 move_plt_plist (fh, fdh);
5607 fdh->elf.needs_plt = 1;
5609 fdh->is_func_descriptor = 1;
5610 fdh->oh = fh;
5611 fh->oh = fdh;
5614 /* Now that the info is on the function descriptor, clear the
5615 function code sym info. Any function code syms for which we
5616 don't have a definition in a regular file, we force local.
5617 This prevents a shared library from exporting syms that have
5618 been imported from another library. Function code syms that
5619 are really in the library we must leave global to prevent the
5620 linker dragging in a definition from a static library. */
5621 force_local = (!fh->elf.def_regular
5622 || fdh == NULL
5623 || !fdh->elf.def_regular
5624 || fdh->elf.forced_local);
5625 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5627 return TRUE;
5630 /* Called near the start of bfd_elf_size_dynamic_sections. We use
5631 this hook to a) provide some gcc support functions, and b) transfer
5632 dynamic linking information gathered so far on function code symbol
5633 entries, to their corresponding function descriptor symbol entries. */
5635 static bfd_boolean
5636 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5637 struct bfd_link_info *info)
5639 struct ppc_link_hash_table *htab;
5640 unsigned int i;
5641 const struct sfpr_def_parms funcs[] =
5643 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5644 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5645 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5646 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5647 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5648 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5649 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5650 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5651 { "._savef", 14, 31, savefpr, savefpr1_tail },
5652 { "._restf", 14, 31, restfpr, restfpr1_tail },
5653 { "_savevr_", 20, 31, savevr, savevr_tail },
5654 { "_restvr_", 20, 31, restvr, restvr_tail }
5657 htab = ppc_hash_table (info);
5658 if (htab->sfpr == NULL)
5659 /* We don't have any relocs. */
5660 return TRUE;
5662 /* Provide any missing _save* and _rest* functions. */
5663 htab->sfpr->size = 0;
5664 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
5665 if (!sfpr_define (info, &funcs[i]))
5666 return FALSE;
5668 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5670 if (htab->sfpr->size == 0)
5671 htab->sfpr->flags |= SEC_EXCLUDE;
5673 return TRUE;
5676 /* Adjust a symbol defined by a dynamic object and referenced by a
5677 regular object. The current definition is in some section of the
5678 dynamic object, but we're not including those sections. We have to
5679 change the definition to something the rest of the link can
5680 understand. */
5682 static bfd_boolean
5683 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
5684 struct elf_link_hash_entry *h)
5686 struct ppc_link_hash_table *htab;
5687 asection *s;
5688 unsigned int power_of_two;
5690 htab = ppc_hash_table (info);
5692 /* Deal with function syms. */
5693 if (h->type == STT_FUNC
5694 || h->needs_plt)
5696 /* Clear procedure linkage table information for any symbol that
5697 won't need a .plt entry. */
5698 struct plt_entry *ent;
5699 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5700 if (ent->plt.refcount > 0)
5701 break;
5702 if (ent == NULL
5703 || SYMBOL_CALLS_LOCAL (info, h)
5704 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5705 && h->root.type == bfd_link_hash_undefweak))
5707 h->plt.plist = NULL;
5708 h->needs_plt = 0;
5711 else
5712 h->plt.plist = NULL;
5714 /* If this is a weak symbol, and there is a real definition, the
5715 processor independent code will have arranged for us to see the
5716 real definition first, and we can just use the same value. */
5717 if (h->u.weakdef != NULL)
5719 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
5720 || h->u.weakdef->root.type == bfd_link_hash_defweak);
5721 h->root.u.def.section = h->u.weakdef->root.u.def.section;
5722 h->root.u.def.value = h->u.weakdef->root.u.def.value;
5723 if (ELIMINATE_COPY_RELOCS)
5724 h->non_got_ref = h->u.weakdef->non_got_ref;
5725 return TRUE;
5728 /* If we are creating a shared library, we must presume that the
5729 only references to the symbol are via the global offset table.
5730 For such cases we need not do anything here; the relocations will
5731 be handled correctly by relocate_section. */
5732 if (info->shared)
5733 return TRUE;
5735 /* If there are no references to this symbol that do not use the
5736 GOT, we don't need to generate a copy reloc. */
5737 if (!h->non_got_ref)
5738 return TRUE;
5740 if (ELIMINATE_COPY_RELOCS)
5742 struct ppc_link_hash_entry * eh;
5743 struct ppc_dyn_relocs *p;
5745 eh = (struct ppc_link_hash_entry *) h;
5746 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5748 s = p->sec->output_section;
5749 if (s != NULL && (s->flags & SEC_READONLY) != 0)
5750 break;
5753 /* If we didn't find any dynamic relocs in read-only sections, then
5754 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
5755 if (p == NULL)
5757 h->non_got_ref = 0;
5758 return TRUE;
5762 if (h->plt.plist != NULL)
5764 /* We should never get here, but unfortunately there are versions
5765 of gcc out there that improperly (for this ABI) put initialized
5766 function pointers, vtable refs and suchlike in read-only
5767 sections. Allow them to proceed, but warn that this might
5768 break at runtime. */
5769 (*_bfd_error_handler)
5770 (_("copy reloc against `%s' requires lazy plt linking; "
5771 "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
5772 h->root.root.string);
5775 /* This is a reference to a symbol defined by a dynamic object which
5776 is not a function. */
5778 if (h->size == 0)
5780 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
5781 h->root.root.string);
5782 return TRUE;
5785 /* We must allocate the symbol in our .dynbss section, which will
5786 become part of the .bss section of the executable. There will be
5787 an entry for this symbol in the .dynsym section. The dynamic
5788 object will contain position independent code, so all references
5789 from the dynamic object to this symbol will go through the global
5790 offset table. The dynamic linker will use the .dynsym entry to
5791 determine the address it must put in the global offset table, so
5792 both the dynamic object and the regular object will refer to the
5793 same memory location for the variable. */
5795 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
5796 to copy the initial value out of the dynamic object and into the
5797 runtime process image. We need to remember the offset into the
5798 .rela.bss section we are going to use. */
5799 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5801 htab->relbss->size += sizeof (Elf64_External_Rela);
5802 h->needs_copy = 1;
5805 /* We need to figure out the alignment required for this symbol. I
5806 have no idea how ELF linkers handle this. */
5807 power_of_two = bfd_log2 (h->size);
5808 if (power_of_two > 4)
5809 power_of_two = 4;
5811 /* Apply the required alignment. */
5812 s = htab->dynbss;
5813 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
5814 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
5816 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
5817 return FALSE;
5820 /* Define the symbol as being at this point in the section. */
5821 h->root.u.def.section = s;
5822 h->root.u.def.value = s->size;
5824 /* Increment the section size to make room for the symbol. */
5825 s->size += h->size;
5827 return TRUE;
5830 /* If given a function descriptor symbol, hide both the function code
5831 sym and the descriptor. */
5832 static void
5833 ppc64_elf_hide_symbol (struct bfd_link_info *info,
5834 struct elf_link_hash_entry *h,
5835 bfd_boolean force_local)
5837 struct ppc_link_hash_entry *eh;
5838 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
5840 eh = (struct ppc_link_hash_entry *) h;
5841 if (eh->is_func_descriptor)
5843 struct ppc_link_hash_entry *fh = eh->oh;
5845 if (fh == NULL)
5847 const char *p, *q;
5848 struct ppc_link_hash_table *htab;
5849 char save;
5851 /* We aren't supposed to use alloca in BFD because on
5852 systems which do not have alloca the version in libiberty
5853 calls xmalloc, which might cause the program to crash
5854 when it runs out of memory. This function doesn't have a
5855 return status, so there's no way to gracefully return an
5856 error. So cheat. We know that string[-1] can be safely
5857 accessed; It's either a string in an ELF string table,
5858 or allocated in an objalloc structure. */
5860 p = eh->elf.root.root.string - 1;
5861 save = *p;
5862 *(char *) p = '.';
5863 htab = ppc_hash_table (info);
5864 fh = (struct ppc_link_hash_entry *)
5865 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
5866 *(char *) p = save;
5868 /* Unfortunately, if it so happens that the string we were
5869 looking for was allocated immediately before this string,
5870 then we overwrote the string terminator. That's the only
5871 reason the lookup should fail. */
5872 if (fh == NULL)
5874 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
5875 while (q >= eh->elf.root.root.string && *q == *p)
5876 --q, --p;
5877 if (q < eh->elf.root.root.string && *p == '.')
5878 fh = (struct ppc_link_hash_entry *)
5879 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
5881 if (fh != NULL)
5883 eh->oh = fh;
5884 fh->oh = eh;
5887 if (fh != NULL)
5888 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5892 static bfd_boolean
5893 get_sym_h (struct elf_link_hash_entry **hp,
5894 Elf_Internal_Sym **symp,
5895 asection **symsecp,
5896 char **tls_maskp,
5897 Elf_Internal_Sym **locsymsp,
5898 unsigned long r_symndx,
5899 bfd *ibfd)
5901 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5903 if (r_symndx >= symtab_hdr->sh_info)
5905 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
5906 struct elf_link_hash_entry *h;
5908 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5909 while (h->root.type == bfd_link_hash_indirect
5910 || h->root.type == bfd_link_hash_warning)
5911 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5913 if (hp != NULL)
5914 *hp = h;
5916 if (symp != NULL)
5917 *symp = NULL;
5919 if (symsecp != NULL)
5921 asection *symsec = NULL;
5922 if (h->root.type == bfd_link_hash_defined
5923 || h->root.type == bfd_link_hash_defweak)
5924 symsec = h->root.u.def.section;
5925 *symsecp = symsec;
5928 if (tls_maskp != NULL)
5930 struct ppc_link_hash_entry *eh;
5932 eh = (struct ppc_link_hash_entry *) h;
5933 *tls_maskp = &eh->tls_mask;
5936 else
5938 Elf_Internal_Sym *sym;
5939 Elf_Internal_Sym *locsyms = *locsymsp;
5941 if (locsyms == NULL)
5943 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
5944 if (locsyms == NULL)
5945 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
5946 symtab_hdr->sh_info,
5947 0, NULL, NULL, NULL);
5948 if (locsyms == NULL)
5949 return FALSE;
5950 *locsymsp = locsyms;
5952 sym = locsyms + r_symndx;
5954 if (hp != NULL)
5955 *hp = NULL;
5957 if (symp != NULL)
5958 *symp = sym;
5960 if (symsecp != NULL)
5962 asection *symsec = NULL;
5963 if ((sym->st_shndx != SHN_UNDEF
5964 && sym->st_shndx < SHN_LORESERVE)
5965 || sym->st_shndx > SHN_HIRESERVE)
5966 symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
5967 *symsecp = symsec;
5970 if (tls_maskp != NULL)
5972 struct got_entry **lgot_ents;
5973 char *tls_mask;
5975 tls_mask = NULL;
5976 lgot_ents = elf_local_got_ents (ibfd);
5977 if (lgot_ents != NULL)
5979 char *lgot_masks = (char *) (lgot_ents + symtab_hdr->sh_info);
5980 tls_mask = &lgot_masks[r_symndx];
5982 *tls_maskp = tls_mask;
5985 return TRUE;
5988 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
5989 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
5990 type suitable for optimization, and 1 otherwise. */
5992 static int
5993 get_tls_mask (char **tls_maskp, unsigned long *toc_symndx,
5994 Elf_Internal_Sym **locsymsp,
5995 const Elf_Internal_Rela *rel, bfd *ibfd)
5997 unsigned long r_symndx;
5998 int next_r;
5999 struct elf_link_hash_entry *h;
6000 Elf_Internal_Sym *sym;
6001 asection *sec;
6002 bfd_vma off;
6004 r_symndx = ELF64_R_SYM (rel->r_info);
6005 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6006 return 0;
6008 if ((*tls_maskp != NULL && **tls_maskp != 0)
6009 || sec == NULL
6010 || ppc64_elf_section_data (sec)->t_symndx == NULL)
6011 return 1;
6013 /* Look inside a TOC section too. */
6014 if (h != NULL)
6016 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6017 off = h->root.u.def.value;
6019 else
6020 off = sym->st_value;
6021 off += rel->r_addend;
6022 BFD_ASSERT (off % 8 == 0);
6023 r_symndx = ppc64_elf_section_data (sec)->t_symndx[off / 8];
6024 next_r = ppc64_elf_section_data (sec)->t_symndx[off / 8 + 1];
6025 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6026 return 0;
6027 if (toc_symndx != NULL)
6028 *toc_symndx = r_symndx;
6029 if ((h == NULL
6030 || ((h->root.type == bfd_link_hash_defined
6031 || h->root.type == bfd_link_hash_defweak)
6032 && !h->def_dynamic))
6033 && (next_r == -1 || next_r == -2))
6034 return 1 - next_r;
6035 return 1;
6038 /* Adjust all global syms defined in opd sections. In gcc generated
6039 code for the old ABI, these will already have been done. */
6041 static bfd_boolean
6042 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6044 struct ppc_link_hash_entry *eh;
6045 asection *sym_sec;
6046 long *opd_adjust;
6048 if (h->root.type == bfd_link_hash_indirect)
6049 return TRUE;
6051 if (h->root.type == bfd_link_hash_warning)
6052 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6054 if (h->root.type != bfd_link_hash_defined
6055 && h->root.type != bfd_link_hash_defweak)
6056 return TRUE;
6058 eh = (struct ppc_link_hash_entry *) h;
6059 if (eh->adjust_done)
6060 return TRUE;
6062 sym_sec = eh->elf.root.u.def.section;
6063 opd_adjust = get_opd_info (sym_sec);
6064 if (opd_adjust != NULL)
6066 long adjust = opd_adjust[eh->elf.root.u.def.value / 8];
6067 if (adjust == -1)
6069 /* This entry has been deleted. */
6070 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->u.deleted_section;
6071 if (dsec == NULL)
6073 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6074 if (elf_discarded_section (dsec))
6076 ppc64_elf_tdata (sym_sec->owner)->u.deleted_section = dsec;
6077 break;
6080 eh->elf.root.u.def.value = 0;
6081 eh->elf.root.u.def.section = dsec;
6083 else
6084 eh->elf.root.u.def.value += adjust;
6085 eh->adjust_done = 1;
6087 return TRUE;
6090 /* Handles decrementing dynamic reloc counts for the reloc specified by
6091 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6092 have already been determined. */
6094 static bfd_boolean
6095 dec_dynrel_count (bfd_vma r_info,
6096 asection *sec,
6097 struct bfd_link_info *info,
6098 Elf_Internal_Sym **local_syms,
6099 struct elf_link_hash_entry *h,
6100 asection *sym_sec)
6102 enum elf_ppc64_reloc_type r_type;
6103 struct ppc_dyn_relocs *p;
6104 struct ppc_dyn_relocs **pp;
6106 /* Can this reloc be dynamic? This switch, and later tests here
6107 should be kept in sync with the code in check_relocs. */
6108 r_type = ELF64_R_TYPE (r_info);
6109 switch (r_type)
6111 default:
6112 return TRUE;
6114 case R_PPC64_TPREL16:
6115 case R_PPC64_TPREL16_LO:
6116 case R_PPC64_TPREL16_HI:
6117 case R_PPC64_TPREL16_HA:
6118 case R_PPC64_TPREL16_DS:
6119 case R_PPC64_TPREL16_LO_DS:
6120 case R_PPC64_TPREL16_HIGHER:
6121 case R_PPC64_TPREL16_HIGHERA:
6122 case R_PPC64_TPREL16_HIGHEST:
6123 case R_PPC64_TPREL16_HIGHESTA:
6124 if (!info->shared)
6125 return TRUE;
6127 case R_PPC64_TPREL64:
6128 case R_PPC64_DTPMOD64:
6129 case R_PPC64_DTPREL64:
6130 case R_PPC64_ADDR64:
6131 case R_PPC64_REL30:
6132 case R_PPC64_REL32:
6133 case R_PPC64_REL64:
6134 case R_PPC64_ADDR14:
6135 case R_PPC64_ADDR14_BRNTAKEN:
6136 case R_PPC64_ADDR14_BRTAKEN:
6137 case R_PPC64_ADDR16:
6138 case R_PPC64_ADDR16_DS:
6139 case R_PPC64_ADDR16_HA:
6140 case R_PPC64_ADDR16_HI:
6141 case R_PPC64_ADDR16_HIGHER:
6142 case R_PPC64_ADDR16_HIGHERA:
6143 case R_PPC64_ADDR16_HIGHEST:
6144 case R_PPC64_ADDR16_HIGHESTA:
6145 case R_PPC64_ADDR16_LO:
6146 case R_PPC64_ADDR16_LO_DS:
6147 case R_PPC64_ADDR24:
6148 case R_PPC64_ADDR32:
6149 case R_PPC64_UADDR16:
6150 case R_PPC64_UADDR32:
6151 case R_PPC64_UADDR64:
6152 case R_PPC64_TOC:
6153 break;
6156 if (local_syms != NULL)
6158 unsigned long r_symndx;
6159 Elf_Internal_Sym *sym;
6160 bfd *ibfd = sec->owner;
6162 r_symndx = ELF64_R_SYM (r_info);
6163 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6164 return FALSE;
6167 if ((info->shared
6168 && (MUST_BE_DYN_RELOC (r_type)
6169 || (h != NULL
6170 && (!info->symbolic
6171 || h->root.type == bfd_link_hash_defweak
6172 || !h->def_regular))))
6173 || (ELIMINATE_COPY_RELOCS
6174 && !info->shared
6175 && h != NULL
6176 && (h->root.type == bfd_link_hash_defweak
6177 || !h->def_regular)))
6179 else
6180 return TRUE;
6182 if (h != NULL)
6183 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6184 else
6186 if (sym_sec != NULL)
6188 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6189 pp = (struct ppc_dyn_relocs **) vpp;
6191 else
6193 void *vpp = &elf_section_data (sec)->local_dynrel;
6194 pp = (struct ppc_dyn_relocs **) vpp;
6197 /* elf_gc_sweep may have already removed all dyn relocs associated
6198 with local syms for a given section. Don't report a dynreloc
6199 miscount. */
6200 if (*pp == NULL)
6201 return TRUE;
6204 while ((p = *pp) != NULL)
6206 if (p->sec == sec)
6208 if (!MUST_BE_DYN_RELOC (r_type))
6209 p->pc_count -= 1;
6210 p->count -= 1;
6211 if (p->count == 0)
6212 *pp = p->next;
6213 return TRUE;
6215 pp = &p->next;
6218 (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
6219 sec->owner, sec);
6220 bfd_set_error (bfd_error_bad_value);
6221 return FALSE;
6224 /* Remove unused Official Procedure Descriptor entries. Currently we
6225 only remove those associated with functions in discarded link-once
6226 sections, or weakly defined functions that have been overridden. It
6227 would be possible to remove many more entries for statically linked
6228 applications. */
6230 bfd_boolean
6231 ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info,
6232 bfd_boolean no_opd_opt,
6233 bfd_boolean non_overlapping)
6235 bfd *ibfd;
6236 bfd_boolean some_edited = FALSE;
6237 asection *need_pad = NULL;
6239 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6241 asection *sec;
6242 Elf_Internal_Rela *relstart, *rel, *relend;
6243 Elf_Internal_Shdr *symtab_hdr;
6244 Elf_Internal_Sym *local_syms;
6245 struct elf_link_hash_entry **sym_hashes;
6246 bfd_vma offset;
6247 bfd_size_type amt;
6248 long *opd_adjust;
6249 bfd_boolean need_edit, add_aux_fields;
6250 bfd_size_type cnt_16b = 0;
6252 sec = bfd_get_section_by_name (ibfd, ".opd");
6253 if (sec == NULL || sec->size == 0)
6254 continue;
6256 amt = sec->size * sizeof (long) / 8;
6257 opd_adjust = get_opd_info (sec);
6258 if (opd_adjust == NULL)
6260 /* check_relocs hasn't been called. Must be a ld -r link
6261 or --just-symbols object. */
6262 opd_adjust = bfd_alloc (obfd, amt);
6263 if (opd_adjust == NULL)
6264 return FALSE;
6265 ppc64_elf_section_data (sec)->opd.adjust = opd_adjust;
6267 memset (opd_adjust, 0, amt);
6269 if (no_opd_opt)
6270 continue;
6272 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6273 continue;
6275 if (sec->output_section == bfd_abs_section_ptr)
6276 continue;
6278 /* Look through the section relocs. */
6279 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6280 continue;
6282 local_syms = NULL;
6283 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
6284 sym_hashes = elf_sym_hashes (ibfd);
6286 /* Read the relocations. */
6287 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6288 info->keep_memory);
6289 if (relstart == NULL)
6290 return FALSE;
6292 /* First run through the relocs to check they are sane, and to
6293 determine whether we need to edit this opd section. */
6294 need_edit = FALSE;
6295 need_pad = sec;
6296 offset = 0;
6297 relend = relstart + sec->reloc_count;
6298 for (rel = relstart; rel < relend; )
6300 enum elf_ppc64_reloc_type r_type;
6301 unsigned long r_symndx;
6302 asection *sym_sec;
6303 struct elf_link_hash_entry *h;
6304 Elf_Internal_Sym *sym;
6306 /* .opd contains a regular array of 16 or 24 byte entries. We're
6307 only interested in the reloc pointing to a function entry
6308 point. */
6309 if (rel->r_offset != offset
6310 || rel + 1 >= relend
6311 || (rel + 1)->r_offset != offset + 8)
6313 /* If someone messes with .opd alignment then after a
6314 "ld -r" we might have padding in the middle of .opd.
6315 Also, there's nothing to prevent someone putting
6316 something silly in .opd with the assembler. No .opd
6317 optimization for them! */
6318 broken_opd:
6319 (*_bfd_error_handler)
6320 (_("%B: .opd is not a regular array of opd entries"), ibfd);
6321 need_edit = FALSE;
6322 break;
6325 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6326 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6328 (*_bfd_error_handler)
6329 (_("%B: unexpected reloc type %u in .opd section"),
6330 ibfd, r_type);
6331 need_edit = FALSE;
6332 break;
6335 r_symndx = ELF64_R_SYM (rel->r_info);
6336 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6337 r_symndx, ibfd))
6338 goto error_ret;
6340 if (sym_sec == NULL || sym_sec->owner == NULL)
6342 const char *sym_name;
6343 if (h != NULL)
6344 sym_name = h->root.root.string;
6345 else
6346 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6347 sym_sec);
6349 (*_bfd_error_handler)
6350 (_("%B: undefined sym `%s' in .opd section"),
6351 ibfd, sym_name);
6352 need_edit = FALSE;
6353 break;
6356 /* opd entries are always for functions defined in the
6357 current input bfd. If the symbol isn't defined in the
6358 input bfd, then we won't be using the function in this
6359 bfd; It must be defined in a linkonce section in another
6360 bfd, or is weak. It's also possible that we are
6361 discarding the function due to a linker script /DISCARD/,
6362 which we test for via the output_section. */
6363 if (sym_sec->owner != ibfd
6364 || sym_sec->output_section == bfd_abs_section_ptr)
6365 need_edit = TRUE;
6367 rel += 2;
6368 if (rel == relend
6369 || (rel + 1 == relend && rel->r_offset == offset + 16))
6371 if (sec->size == offset + 24)
6373 need_pad = NULL;
6374 break;
6376 if (rel == relend && sec->size == offset + 16)
6378 cnt_16b++;
6379 break;
6381 goto broken_opd;
6384 if (rel->r_offset == offset + 24)
6385 offset += 24;
6386 else if (rel->r_offset != offset + 16)
6387 goto broken_opd;
6388 else if (rel + 1 < relend
6389 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6390 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6392 offset += 16;
6393 cnt_16b++;
6395 else if (rel + 2 < relend
6396 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
6397 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
6399 offset += 24;
6400 rel += 1;
6402 else
6403 goto broken_opd;
6406 add_aux_fields = non_overlapping && cnt_16b > 0;
6408 if (need_edit || add_aux_fields)
6410 Elf_Internal_Rela *write_rel;
6411 bfd_byte *rptr, *wptr;
6412 bfd_byte *new_contents = NULL;
6413 bfd_boolean skip;
6414 long opd_ent_size;
6416 /* This seems a waste of time as input .opd sections are all
6417 zeros as generated by gcc, but I suppose there's no reason
6418 this will always be so. We might start putting something in
6419 the third word of .opd entries. */
6420 if ((sec->flags & SEC_IN_MEMORY) == 0)
6422 bfd_byte *loc;
6423 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6425 if (loc != NULL)
6426 free (loc);
6427 error_ret:
6428 if (local_syms != NULL
6429 && symtab_hdr->contents != (unsigned char *) local_syms)
6430 free (local_syms);
6431 if (elf_section_data (sec)->relocs != relstart)
6432 free (relstart);
6433 return FALSE;
6435 sec->contents = loc;
6436 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6439 elf_section_data (sec)->relocs = relstart;
6441 new_contents = sec->contents;
6442 if (add_aux_fields)
6444 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6445 if (new_contents == NULL)
6446 return FALSE;
6447 need_pad = FALSE;
6449 wptr = new_contents;
6450 rptr = sec->contents;
6452 write_rel = relstart;
6453 skip = FALSE;
6454 offset = 0;
6455 opd_ent_size = 0;
6456 for (rel = relstart; rel < relend; rel++)
6458 unsigned long r_symndx;
6459 asection *sym_sec;
6460 struct elf_link_hash_entry *h;
6461 Elf_Internal_Sym *sym;
6463 r_symndx = ELF64_R_SYM (rel->r_info);
6464 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6465 r_symndx, ibfd))
6466 goto error_ret;
6468 if (rel->r_offset == offset)
6470 struct ppc_link_hash_entry *fdh = NULL;
6472 /* See if the .opd entry is full 24 byte or
6473 16 byte (with fd_aux entry overlapped with next
6474 fd_func). */
6475 opd_ent_size = 24;
6476 if ((rel + 2 == relend && sec->size == offset + 16)
6477 || (rel + 3 < relend
6478 && rel[2].r_offset == offset + 16
6479 && rel[3].r_offset == offset + 24
6480 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
6481 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
6482 opd_ent_size = 16;
6484 if (h != NULL
6485 && h->root.root.string[0] == '.')
6487 fdh = get_fdh ((struct ppc_link_hash_entry *) h,
6488 ppc_hash_table (info));
6489 if (fdh != NULL
6490 && fdh->elf.root.type != bfd_link_hash_defined
6491 && fdh->elf.root.type != bfd_link_hash_defweak)
6492 fdh = NULL;
6495 skip = (sym_sec->owner != ibfd
6496 || sym_sec->output_section == bfd_abs_section_ptr);
6497 if (skip)
6499 if (fdh != NULL && sym_sec->owner == ibfd)
6501 /* Arrange for the function descriptor sym
6502 to be dropped. */
6503 fdh->elf.root.u.def.value = 0;
6504 fdh->elf.root.u.def.section = sym_sec;
6506 opd_adjust[rel->r_offset / 8] = -1;
6508 else
6510 /* We'll be keeping this opd entry. */
6512 if (fdh != NULL)
6514 /* Redefine the function descriptor symbol to
6515 this location in the opd section. It is
6516 necessary to update the value here rather
6517 than using an array of adjustments as we do
6518 for local symbols, because various places
6519 in the generic ELF code use the value
6520 stored in u.def.value. */
6521 fdh->elf.root.u.def.value = wptr - new_contents;
6522 fdh->adjust_done = 1;
6525 /* Local syms are a bit tricky. We could
6526 tweak them as they can be cached, but
6527 we'd need to look through the local syms
6528 for the function descriptor sym which we
6529 don't have at the moment. So keep an
6530 array of adjustments. */
6531 opd_adjust[rel->r_offset / 8]
6532 = (wptr - new_contents) - (rptr - sec->contents);
6534 if (wptr != rptr)
6535 memcpy (wptr, rptr, opd_ent_size);
6536 wptr += opd_ent_size;
6537 if (add_aux_fields && opd_ent_size == 16)
6539 memset (wptr, '\0', 8);
6540 wptr += 8;
6543 rptr += opd_ent_size;
6544 offset += opd_ent_size;
6547 if (skip)
6549 if (!NO_OPD_RELOCS
6550 && !info->relocatable
6551 && !dec_dynrel_count (rel->r_info, sec, info,
6552 NULL, h, sym_sec))
6553 goto error_ret;
6555 else
6557 /* We need to adjust any reloc offsets to point to the
6558 new opd entries. While we're at it, we may as well
6559 remove redundant relocs. */
6560 rel->r_offset += opd_adjust[(offset - opd_ent_size) / 8];
6561 if (write_rel != rel)
6562 memcpy (write_rel, rel, sizeof (*rel));
6563 ++write_rel;
6567 sec->size = wptr - new_contents;
6568 sec->reloc_count = write_rel - relstart;
6569 if (add_aux_fields)
6571 free (sec->contents);
6572 sec->contents = new_contents;
6575 /* Fudge the size too, as this is used later in
6576 elf_bfd_final_link if we are emitting relocs. */
6577 elf_section_data (sec)->rel_hdr.sh_size
6578 = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
6579 BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
6580 some_edited = TRUE;
6582 else if (elf_section_data (sec)->relocs != relstart)
6583 free (relstart);
6585 if (local_syms != NULL
6586 && symtab_hdr->contents != (unsigned char *) local_syms)
6588 if (!info->keep_memory)
6589 free (local_syms);
6590 else
6591 symtab_hdr->contents = (unsigned char *) local_syms;
6595 if (some_edited)
6596 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
6598 /* If we are doing a final link and the last .opd entry is just 16 byte
6599 long, add a 8 byte padding after it. */
6600 if (need_pad != NULL && !info->relocatable)
6602 bfd_byte *p;
6604 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
6606 BFD_ASSERT (need_pad->size > 0);
6608 p = bfd_malloc (need_pad->size + 8);
6609 if (p == NULL)
6610 return FALSE;
6612 if (! bfd_get_section_contents (need_pad->owner, need_pad,
6613 p, 0, need_pad->size))
6614 return FALSE;
6616 need_pad->contents = p;
6617 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6619 else
6621 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
6622 if (p == NULL)
6623 return FALSE;
6625 need_pad->contents = p;
6628 memset (need_pad->contents + need_pad->size, 0, 8);
6629 need_pad->size += 8;
6632 return TRUE;
6635 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
6637 asection *
6638 ppc64_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
6640 struct ppc_link_hash_table *htab;
6642 htab = ppc_hash_table (info);
6643 if (htab->tls_get_addr != NULL)
6645 struct ppc_link_hash_entry *h = htab->tls_get_addr;
6647 while (h->elf.root.type == bfd_link_hash_indirect
6648 || h->elf.root.type == bfd_link_hash_warning)
6649 h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
6651 htab->tls_get_addr = h;
6653 if (htab->tls_get_addr_fd == NULL
6654 && h->oh != NULL
6655 && h->oh->is_func_descriptor
6656 && (h->oh->elf.root.type == bfd_link_hash_defined
6657 || h->oh->elf.root.type == bfd_link_hash_defweak))
6658 htab->tls_get_addr_fd = h->oh;
6661 if (htab->tls_get_addr_fd != NULL)
6663 struct ppc_link_hash_entry *h = htab->tls_get_addr_fd;
6665 while (h->elf.root.type == bfd_link_hash_indirect
6666 || h->elf.root.type == bfd_link_hash_warning)
6667 h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
6669 htab->tls_get_addr_fd = h;
6672 return _bfd_elf_tls_setup (obfd, info);
6675 /* Run through all the TLS relocs looking for optimization
6676 opportunities. The linker has been hacked (see ppc64elf.em) to do
6677 a preliminary section layout so that we know the TLS segment
6678 offsets. We can't optimize earlier because some optimizations need
6679 to know the tp offset, and we need to optimize before allocating
6680 dynamic relocations. */
6682 bfd_boolean
6683 ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6685 bfd *ibfd;
6686 asection *sec;
6687 struct ppc_link_hash_table *htab;
6689 if (info->relocatable || info->shared)
6690 return TRUE;
6692 htab = ppc_hash_table (info);
6693 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6695 Elf_Internal_Sym *locsyms = NULL;
6697 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6698 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
6700 Elf_Internal_Rela *relstart, *rel, *relend;
6701 int expecting_tls_get_addr;
6703 /* Read the relocations. */
6704 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6705 info->keep_memory);
6706 if (relstart == NULL)
6707 return FALSE;
6709 expecting_tls_get_addr = 0;
6710 relend = relstart + sec->reloc_count;
6711 for (rel = relstart; rel < relend; rel++)
6713 enum elf_ppc64_reloc_type r_type;
6714 unsigned long r_symndx;
6715 struct elf_link_hash_entry *h;
6716 Elf_Internal_Sym *sym;
6717 asection *sym_sec;
6718 char *tls_mask;
6719 char tls_set, tls_clear, tls_type = 0;
6720 bfd_vma value;
6721 bfd_boolean ok_tprel, is_local;
6723 r_symndx = ELF64_R_SYM (rel->r_info);
6724 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
6725 r_symndx, ibfd))
6727 err_free_rel:
6728 if (elf_section_data (sec)->relocs != relstart)
6729 free (relstart);
6730 if (locsyms != NULL
6731 && (elf_tdata (ibfd)->symtab_hdr.contents
6732 != (unsigned char *) locsyms))
6733 free (locsyms);
6734 return FALSE;
6737 if (h != NULL)
6739 if (h->root.type != bfd_link_hash_defined
6740 && h->root.type != bfd_link_hash_defweak)
6741 continue;
6742 value = h->root.u.def.value;
6744 else
6745 /* Symbols referenced by TLS relocs must be of type
6746 STT_TLS. So no need for .opd local sym adjust. */
6747 value = sym->st_value;
6749 ok_tprel = FALSE;
6750 is_local = FALSE;
6751 if (h == NULL
6752 || !h->def_dynamic)
6754 is_local = TRUE;
6755 value += sym_sec->output_offset;
6756 value += sym_sec->output_section->vma;
6757 value -= htab->elf.tls_sec->vma;
6758 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
6759 < (bfd_vma) 1 << 32);
6762 r_type = ELF64_R_TYPE (rel->r_info);
6763 switch (r_type)
6765 case R_PPC64_GOT_TLSLD16:
6766 case R_PPC64_GOT_TLSLD16_LO:
6767 case R_PPC64_GOT_TLSLD16_HI:
6768 case R_PPC64_GOT_TLSLD16_HA:
6769 /* These relocs should never be against a symbol
6770 defined in a shared lib. Leave them alone if
6771 that turns out to be the case. */
6772 ppc64_tlsld_got (ibfd)->refcount -= 1;
6773 if (!is_local)
6774 continue;
6776 /* LD -> LE */
6777 tls_set = 0;
6778 tls_clear = TLS_LD;
6779 tls_type = TLS_TLS | TLS_LD;
6780 expecting_tls_get_addr = 1;
6781 break;
6783 case R_PPC64_GOT_TLSGD16:
6784 case R_PPC64_GOT_TLSGD16_LO:
6785 case R_PPC64_GOT_TLSGD16_HI:
6786 case R_PPC64_GOT_TLSGD16_HA:
6787 if (ok_tprel)
6788 /* GD -> LE */
6789 tls_set = 0;
6790 else
6791 /* GD -> IE */
6792 tls_set = TLS_TLS | TLS_TPRELGD;
6793 tls_clear = TLS_GD;
6794 tls_type = TLS_TLS | TLS_GD;
6795 expecting_tls_get_addr = 1;
6796 break;
6798 case R_PPC64_GOT_TPREL16_DS:
6799 case R_PPC64_GOT_TPREL16_LO_DS:
6800 case R_PPC64_GOT_TPREL16_HI:
6801 case R_PPC64_GOT_TPREL16_HA:
6802 expecting_tls_get_addr = 0;
6803 if (ok_tprel)
6805 /* IE -> LE */
6806 tls_set = 0;
6807 tls_clear = TLS_TPREL;
6808 tls_type = TLS_TLS | TLS_TPREL;
6809 break;
6811 else
6812 continue;
6814 case R_PPC64_REL14:
6815 case R_PPC64_REL14_BRTAKEN:
6816 case R_PPC64_REL14_BRNTAKEN:
6817 case R_PPC64_REL24:
6818 if (h != NULL
6819 && (h == &htab->tls_get_addr->elf
6820 || h == &htab->tls_get_addr_fd->elf))
6822 if (!expecting_tls_get_addr
6823 && rel != relstart
6824 && ((ELF64_R_TYPE (rel[-1].r_info)
6825 == R_PPC64_TOC16)
6826 || (ELF64_R_TYPE (rel[-1].r_info)
6827 == R_PPC64_TOC16_LO)))
6829 /* Check for toc tls entries. */
6830 char *toc_tls;
6831 int retval;
6833 retval = get_tls_mask (&toc_tls, NULL, &locsyms,
6834 rel - 1, ibfd);
6835 if (retval == 0)
6836 goto err_free_rel;
6837 if (toc_tls != NULL)
6838 expecting_tls_get_addr = retval > 1;
6841 if (expecting_tls_get_addr)
6843 struct plt_entry *ent;
6844 for (ent = h->plt.plist; ent; ent = ent->next)
6845 if (ent->addend == 0)
6847 if (ent->plt.refcount > 0)
6848 ent->plt.refcount -= 1;
6849 break;
6853 expecting_tls_get_addr = 0;
6854 continue;
6856 case R_PPC64_TPREL64:
6857 expecting_tls_get_addr = 0;
6858 if (ok_tprel)
6860 /* IE -> LE */
6861 tls_set = TLS_EXPLICIT;
6862 tls_clear = TLS_TPREL;
6863 break;
6865 else
6866 continue;
6868 case R_PPC64_DTPMOD64:
6869 expecting_tls_get_addr = 0;
6870 if (rel + 1 < relend
6871 && (rel[1].r_info
6872 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
6873 && rel[1].r_offset == rel->r_offset + 8)
6875 if (ok_tprel)
6876 /* GD -> LE */
6877 tls_set = TLS_EXPLICIT | TLS_GD;
6878 else
6879 /* GD -> IE */
6880 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
6881 tls_clear = TLS_GD;
6883 else
6885 if (!is_local)
6886 continue;
6888 /* LD -> LE */
6889 tls_set = TLS_EXPLICIT;
6890 tls_clear = TLS_LD;
6892 break;
6894 default:
6895 expecting_tls_get_addr = 0;
6896 continue;
6899 if ((tls_set & TLS_EXPLICIT) == 0)
6901 struct got_entry *ent;
6903 /* Adjust got entry for this reloc. */
6904 if (h != NULL)
6905 ent = h->got.glist;
6906 else
6907 ent = elf_local_got_ents (ibfd)[r_symndx];
6909 for (; ent != NULL; ent = ent->next)
6910 if (ent->addend == rel->r_addend
6911 && ent->owner == ibfd
6912 && ent->tls_type == tls_type)
6913 break;
6914 if (ent == NULL)
6915 abort ();
6917 if (tls_set == 0)
6919 /* We managed to get rid of a got entry. */
6920 if (ent->got.refcount > 0)
6921 ent->got.refcount -= 1;
6924 else
6926 /* If we got rid of a DTPMOD/DTPREL reloc pair then
6927 we'll lose one or two dyn relocs. */
6928 if (!dec_dynrel_count (rel->r_info, sec, info,
6929 NULL, h, sym_sec))
6930 return FALSE;
6932 if (tls_set == (TLS_EXPLICIT | TLS_GD))
6934 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
6935 NULL, h, sym_sec))
6936 return FALSE;
6940 *tls_mask |= tls_set;
6941 *tls_mask &= ~tls_clear;
6944 if (elf_section_data (sec)->relocs != relstart)
6945 free (relstart);
6948 if (locsyms != NULL
6949 && (elf_tdata (ibfd)->symtab_hdr.contents
6950 != (unsigned char *) locsyms))
6952 if (!info->keep_memory)
6953 free (locsyms);
6954 else
6955 elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
6958 return TRUE;
6961 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
6962 the values of any global symbols in a toc section that has been
6963 edited. Globals in toc sections should be a rarity, so this function
6964 sets a flag if any are found in toc sections other than the one just
6965 edited, so that futher hash table traversals can be avoided. */
6967 struct adjust_toc_info
6969 asection *toc;
6970 unsigned long *skip;
6971 bfd_boolean global_toc_syms;
6974 static bfd_boolean
6975 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
6977 struct ppc_link_hash_entry *eh;
6978 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
6980 if (h->root.type == bfd_link_hash_indirect)
6981 return TRUE;
6983 if (h->root.type == bfd_link_hash_warning)
6984 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6986 if (h->root.type != bfd_link_hash_defined
6987 && h->root.type != bfd_link_hash_defweak)
6988 return TRUE;
6990 eh = (struct ppc_link_hash_entry *) h;
6991 if (eh->adjust_done)
6992 return TRUE;
6994 if (eh->elf.root.u.def.section == toc_inf->toc)
6996 unsigned long skip = toc_inf->skip[eh->elf.root.u.def.value >> 3];
6997 if (skip != (unsigned long) -1)
6998 eh->elf.root.u.def.value -= skip;
6999 else
7001 (*_bfd_error_handler)
7002 (_("%s defined in removed toc entry"), eh->elf.root.root.string);
7003 eh->elf.root.u.def.section = &bfd_abs_section;
7004 eh->elf.root.u.def.value = 0;
7006 eh->adjust_done = 1;
7008 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7009 toc_inf->global_toc_syms = TRUE;
7011 return TRUE;
7014 /* Examine all relocs referencing .toc sections in order to remove
7015 unused .toc entries. */
7017 bfd_boolean
7018 ppc64_elf_edit_toc (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
7020 bfd *ibfd;
7021 struct adjust_toc_info toc_inf;
7023 toc_inf.global_toc_syms = TRUE;
7024 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7026 asection *toc, *sec;
7027 Elf_Internal_Shdr *symtab_hdr;
7028 Elf_Internal_Sym *local_syms;
7029 struct elf_link_hash_entry **sym_hashes;
7030 Elf_Internal_Rela *relstart, *rel;
7031 unsigned long *skip, *drop;
7032 unsigned char *used;
7033 unsigned char *keep, last, some_unused;
7035 toc = bfd_get_section_by_name (ibfd, ".toc");
7036 if (toc == NULL
7037 || toc->size == 0
7038 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7039 || elf_discarded_section (toc))
7040 continue;
7042 local_syms = NULL;
7043 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7044 sym_hashes = elf_sym_hashes (ibfd);
7046 /* Look at sections dropped from the final link. */
7047 skip = NULL;
7048 relstart = NULL;
7049 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7051 if (sec->reloc_count == 0
7052 || !elf_discarded_section (sec)
7053 || get_opd_info (sec)
7054 || (sec->flags & SEC_ALLOC) == 0
7055 || (sec->flags & SEC_DEBUGGING) != 0)
7056 continue;
7058 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7059 if (relstart == NULL)
7060 goto error_ret;
7062 /* Run through the relocs to see which toc entries might be
7063 unused. */
7064 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7066 enum elf_ppc64_reloc_type r_type;
7067 unsigned long r_symndx;
7068 asection *sym_sec;
7069 struct elf_link_hash_entry *h;
7070 Elf_Internal_Sym *sym;
7071 bfd_vma val;
7073 r_type = ELF64_R_TYPE (rel->r_info);
7074 switch (r_type)
7076 default:
7077 continue;
7079 case R_PPC64_TOC16:
7080 case R_PPC64_TOC16_LO:
7081 case R_PPC64_TOC16_HI:
7082 case R_PPC64_TOC16_HA:
7083 case R_PPC64_TOC16_DS:
7084 case R_PPC64_TOC16_LO_DS:
7085 break;
7088 r_symndx = ELF64_R_SYM (rel->r_info);
7089 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7090 r_symndx, ibfd))
7091 goto error_ret;
7093 if (sym_sec != toc)
7094 continue;
7096 if (h != NULL)
7097 val = h->root.u.def.value;
7098 else
7099 val = sym->st_value;
7100 val += rel->r_addend;
7102 if (val >= toc->size)
7103 continue;
7105 /* Anything in the toc ought to be aligned to 8 bytes.
7106 If not, don't mark as unused. */
7107 if (val & 7)
7108 continue;
7110 if (skip == NULL)
7112 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 7) / 8);
7113 if (skip == NULL)
7114 goto error_ret;
7117 skip[val >> 3] = 1;
7120 if (elf_section_data (sec)->relocs != relstart)
7121 free (relstart);
7124 if (skip == NULL)
7125 continue;
7127 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
7128 if (used == NULL)
7130 error_ret:
7131 if (local_syms != NULL
7132 && symtab_hdr->contents != (unsigned char *) local_syms)
7133 free (local_syms);
7134 if (sec != NULL
7135 && relstart != NULL
7136 && elf_section_data (sec)->relocs != relstart)
7137 free (relstart);
7138 if (skip != NULL)
7139 free (skip);
7140 return FALSE;
7143 /* Now check all kept sections that might reference the toc. */
7144 for (sec = ibfd->sections;
7145 sec != NULL;
7146 /* Check the toc itself last. */
7147 sec = (sec == toc ? NULL
7148 : sec->next == toc && sec->next->next ? sec->next->next
7149 : sec->next == NULL ? toc
7150 : sec->next))
7152 int repeat;
7154 if (sec->reloc_count == 0
7155 || elf_discarded_section (sec)
7156 || get_opd_info (sec)
7157 || (sec->flags & SEC_ALLOC) == 0
7158 || (sec->flags & SEC_DEBUGGING) != 0)
7159 continue;
7161 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, TRUE);
7162 if (relstart == NULL)
7163 goto error_ret;
7165 /* Mark toc entries referenced as used. */
7166 repeat = 0;
7168 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7170 enum elf_ppc64_reloc_type r_type;
7171 unsigned long r_symndx;
7172 asection *sym_sec;
7173 struct elf_link_hash_entry *h;
7174 Elf_Internal_Sym *sym;
7175 bfd_vma val;
7177 r_type = ELF64_R_TYPE (rel->r_info);
7178 switch (r_type)
7180 case R_PPC64_TOC16:
7181 case R_PPC64_TOC16_LO:
7182 case R_PPC64_TOC16_HI:
7183 case R_PPC64_TOC16_HA:
7184 case R_PPC64_TOC16_DS:
7185 case R_PPC64_TOC16_LO_DS:
7186 /* In case we're taking addresses of toc entries. */
7187 case R_PPC64_ADDR64:
7188 break;
7190 default:
7191 continue;
7194 r_symndx = ELF64_R_SYM (rel->r_info);
7195 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7196 r_symndx, ibfd))
7198 free (used);
7199 goto error_ret;
7202 if (sym_sec != toc)
7203 continue;
7205 if (h != NULL)
7206 val = h->root.u.def.value;
7207 else
7208 val = sym->st_value;
7209 val += rel->r_addend;
7211 if (val >= toc->size)
7212 continue;
7214 /* For the toc section, we only mark as used if
7215 this entry itself isn't unused. */
7216 if (sec == toc
7217 && !used[val >> 3]
7218 && (used[rel->r_offset >> 3]
7219 || !skip[rel->r_offset >> 3]))
7220 /* Do all the relocs again, to catch reference
7221 chains. */
7222 repeat = 1;
7224 used[val >> 3] = 1;
7226 while (repeat);
7229 /* Merge the used and skip arrays. Assume that TOC
7230 doublewords not appearing as either used or unused belong
7231 to to an entry more than one doubleword in size. */
7232 for (drop = skip, keep = used, last = 0, some_unused = 0;
7233 drop < skip + (toc->size + 7) / 8;
7234 ++drop, ++keep)
7236 if (*keep)
7238 *drop = 0;
7239 last = 0;
7241 else if (*drop)
7243 some_unused = 1;
7244 last = 1;
7246 else
7247 *drop = last;
7250 free (used);
7252 if (some_unused)
7254 bfd_byte *contents, *src;
7255 unsigned long off;
7257 /* Shuffle the toc contents, and at the same time convert the
7258 skip array from booleans into offsets. */
7259 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
7260 goto error_ret;
7262 elf_section_data (toc)->this_hdr.contents = contents;
7264 for (src = contents, off = 0, drop = skip;
7265 src < contents + toc->size;
7266 src += 8, ++drop)
7268 if (*drop)
7270 *drop = (unsigned long) -1;
7271 off += 8;
7273 else if (off != 0)
7275 *drop = off;
7276 memcpy (src - off, src, 8);
7279 toc->rawsize = toc->size;
7280 toc->size = src - contents - off;
7282 if (toc->reloc_count != 0)
7284 Elf_Internal_Rela *wrel;
7285 bfd_size_type sz;
7287 /* Read toc relocs. */
7288 relstart = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
7289 TRUE);
7290 if (relstart == NULL)
7291 goto error_ret;
7293 /* Remove unused toc relocs, and adjust those we keep. */
7294 wrel = relstart;
7295 for (rel = relstart; rel < relstart + toc->reloc_count; ++rel)
7296 if (skip[rel->r_offset >> 3] != (unsigned long) -1)
7298 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
7299 wrel->r_info = rel->r_info;
7300 wrel->r_addend = rel->r_addend;
7301 ++wrel;
7303 else if (!dec_dynrel_count (rel->r_info, toc, info,
7304 &local_syms, NULL, NULL))
7305 goto error_ret;
7307 toc->reloc_count = wrel - relstart;
7308 sz = elf_section_data (toc)->rel_hdr.sh_entsize;
7309 elf_section_data (toc)->rel_hdr.sh_size = toc->reloc_count * sz;
7310 BFD_ASSERT (elf_section_data (toc)->rel_hdr2 == NULL);
7313 /* Adjust addends for relocs against the toc section sym. */
7314 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7316 if (sec->reloc_count == 0
7317 || elf_discarded_section (sec))
7318 continue;
7320 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7321 TRUE);
7322 if (relstart == NULL)
7323 goto error_ret;
7325 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7327 enum elf_ppc64_reloc_type r_type;
7328 unsigned long r_symndx;
7329 asection *sym_sec;
7330 struct elf_link_hash_entry *h;
7331 Elf_Internal_Sym *sym;
7333 r_type = ELF64_R_TYPE (rel->r_info);
7334 switch (r_type)
7336 default:
7337 continue;
7339 case R_PPC64_TOC16:
7340 case R_PPC64_TOC16_LO:
7341 case R_PPC64_TOC16_HI:
7342 case R_PPC64_TOC16_HA:
7343 case R_PPC64_TOC16_DS:
7344 case R_PPC64_TOC16_LO_DS:
7345 case R_PPC64_ADDR64:
7346 break;
7349 r_symndx = ELF64_R_SYM (rel->r_info);
7350 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7351 r_symndx, ibfd))
7352 goto error_ret;
7354 if (sym_sec != toc || h != NULL || sym->st_value != 0)
7355 continue;
7357 rel->r_addend -= skip[rel->r_addend >> 3];
7361 /* We shouldn't have local or global symbols defined in the TOC,
7362 but handle them anyway. */
7363 if (local_syms != NULL)
7365 Elf_Internal_Sym *sym;
7367 for (sym = local_syms;
7368 sym < local_syms + symtab_hdr->sh_info;
7369 ++sym)
7370 if (sym->st_shndx != SHN_UNDEF
7371 && (sym->st_shndx < SHN_LORESERVE
7372 || sym->st_shndx > SHN_HIRESERVE)
7373 && sym->st_value != 0
7374 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
7376 if (skip[sym->st_value >> 3] != (unsigned long) -1)
7377 sym->st_value -= skip[sym->st_value >> 3];
7378 else
7380 (*_bfd_error_handler)
7381 (_("%s defined in removed toc entry"),
7382 bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7383 NULL));
7384 sym->st_value = 0;
7385 sym->st_shndx = SHN_ABS;
7387 symtab_hdr->contents = (unsigned char *) local_syms;
7391 /* Finally, adjust any global syms defined in the toc. */
7392 if (toc_inf.global_toc_syms)
7394 toc_inf.toc = toc;
7395 toc_inf.skip = skip;
7396 toc_inf.global_toc_syms = FALSE;
7397 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
7398 &toc_inf);
7402 if (local_syms != NULL
7403 && symtab_hdr->contents != (unsigned char *) local_syms)
7405 if (!info->keep_memory)
7406 free (local_syms);
7407 else
7408 symtab_hdr->contents = (unsigned char *) local_syms;
7410 free (skip);
7413 return TRUE;
7416 /* Allocate space in .plt, .got and associated reloc sections for
7417 dynamic relocs. */
7419 static bfd_boolean
7420 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7422 struct bfd_link_info *info;
7423 struct ppc_link_hash_table *htab;
7424 asection *s;
7425 struct ppc_link_hash_entry *eh;
7426 struct ppc_dyn_relocs *p;
7427 struct got_entry *gent;
7429 if (h->root.type == bfd_link_hash_indirect)
7430 return TRUE;
7432 if (h->root.type == bfd_link_hash_warning)
7433 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7435 info = (struct bfd_link_info *) inf;
7436 htab = ppc_hash_table (info);
7438 if (htab->elf.dynamic_sections_created
7439 && h->dynindx != -1
7440 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
7442 struct plt_entry *pent;
7443 bfd_boolean doneone = FALSE;
7444 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7445 if (pent->plt.refcount > 0)
7447 /* If this is the first .plt entry, make room for the special
7448 first entry. */
7449 s = htab->plt;
7450 if (s->size == 0)
7451 s->size += PLT_INITIAL_ENTRY_SIZE;
7453 pent->plt.offset = s->size;
7455 /* Make room for this entry. */
7456 s->size += PLT_ENTRY_SIZE;
7458 /* Make room for the .glink code. */
7459 s = htab->glink;
7460 if (s->size == 0)
7461 s->size += GLINK_CALL_STUB_SIZE;
7462 /* We need bigger stubs past index 32767. */
7463 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
7464 s->size += 4;
7465 s->size += 2*4;
7467 /* We also need to make an entry in the .rela.plt section. */
7468 s = htab->relplt;
7469 s->size += sizeof (Elf64_External_Rela);
7470 doneone = TRUE;
7472 else
7473 pent->plt.offset = (bfd_vma) -1;
7474 if (!doneone)
7476 h->plt.plist = NULL;
7477 h->needs_plt = 0;
7480 else
7482 h->plt.plist = NULL;
7483 h->needs_plt = 0;
7486 eh = (struct ppc_link_hash_entry *) h;
7487 /* Run through the TLS GD got entries first if we're changing them
7488 to TPREL. */
7489 if ((eh->tls_mask & TLS_TPRELGD) != 0)
7490 for (gent = h->got.glist; gent != NULL; gent = gent->next)
7491 if (gent->got.refcount > 0
7492 && (gent->tls_type & TLS_GD) != 0)
7494 /* This was a GD entry that has been converted to TPREL. If
7495 there happens to be a TPREL entry we can use that one. */
7496 struct got_entry *ent;
7497 for (ent = h->got.glist; ent != NULL; ent = ent->next)
7498 if (ent->got.refcount > 0
7499 && (ent->tls_type & TLS_TPREL) != 0
7500 && ent->addend == gent->addend
7501 && ent->owner == gent->owner)
7503 gent->got.refcount = 0;
7504 break;
7507 /* If not, then we'll be using our own TPREL entry. */
7508 if (gent->got.refcount != 0)
7509 gent->tls_type = TLS_TLS | TLS_TPREL;
7512 for (gent = h->got.glist; gent != NULL; gent = gent->next)
7513 if (gent->got.refcount > 0)
7515 bfd_boolean dyn;
7517 /* Make sure this symbol is output as a dynamic symbol.
7518 Undefined weak syms won't yet be marked as dynamic,
7519 nor will all TLS symbols. */
7520 if (h->dynindx == -1
7521 && !h->forced_local)
7523 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7524 return FALSE;
7527 if ((gent->tls_type & TLS_LD) != 0
7528 && !h->def_dynamic)
7530 gent->got.offset = ppc64_tlsld_got (gent->owner)->offset;
7531 continue;
7534 s = ppc64_elf_tdata (gent->owner)->got;
7535 gent->got.offset = s->size;
7536 s->size
7537 += (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)) ? 16 : 8;
7538 dyn = htab->elf.dynamic_sections_created;
7539 if ((info->shared
7540 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
7541 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7542 || h->root.type != bfd_link_hash_undefweak))
7543 ppc64_elf_tdata (gent->owner)->relgot->size
7544 += (gent->tls_type & eh->tls_mask & TLS_GD
7545 ? 2 * sizeof (Elf64_External_Rela)
7546 : sizeof (Elf64_External_Rela));
7548 else
7549 gent->got.offset = (bfd_vma) -1;
7551 if (eh->dyn_relocs == NULL)
7552 return TRUE;
7554 /* In the shared -Bsymbolic case, discard space allocated for
7555 dynamic pc-relative relocs against symbols which turn out to be
7556 defined in regular objects. For the normal shared case, discard
7557 space for relocs that have become local due to symbol visibility
7558 changes. */
7560 if (info->shared)
7562 /* Relocs that use pc_count are those that appear on a call insn,
7563 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
7564 generated via assembly. We want calls to protected symbols to
7565 resolve directly to the function rather than going via the plt.
7566 If people want function pointer comparisons to work as expected
7567 then they should avoid writing weird assembly. */
7568 if (SYMBOL_CALLS_LOCAL (info, h))
7570 struct ppc_dyn_relocs **pp;
7572 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
7574 p->count -= p->pc_count;
7575 p->pc_count = 0;
7576 if (p->count == 0)
7577 *pp = p->next;
7578 else
7579 pp = &p->next;
7583 /* Also discard relocs on undefined weak syms with non-default
7584 visibility. */
7585 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7586 && h->root.type == bfd_link_hash_undefweak)
7587 eh->dyn_relocs = NULL;
7589 else if (ELIMINATE_COPY_RELOCS)
7591 /* For the non-shared case, discard space for relocs against
7592 symbols which turn out to need copy relocs or are not
7593 dynamic. */
7595 if (!h->non_got_ref
7596 && h->def_dynamic
7597 && !h->def_regular)
7599 /* Make sure this symbol is output as a dynamic symbol.
7600 Undefined weak syms won't yet be marked as dynamic. */
7601 if (h->dynindx == -1
7602 && !h->forced_local)
7604 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7605 return FALSE;
7608 /* If that succeeded, we know we'll be keeping all the
7609 relocs. */
7610 if (h->dynindx != -1)
7611 goto keep;
7614 eh->dyn_relocs = NULL;
7616 keep: ;
7619 /* Finally, allocate space. */
7620 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7622 asection *sreloc = elf_section_data (p->sec)->sreloc;
7623 sreloc->size += p->count * sizeof (Elf64_External_Rela);
7626 return TRUE;
7629 /* Find any dynamic relocs that apply to read-only sections. */
7631 static bfd_boolean
7632 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7634 struct ppc_link_hash_entry *eh;
7635 struct ppc_dyn_relocs *p;
7637 if (h->root.type == bfd_link_hash_warning)
7638 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7640 eh = (struct ppc_link_hash_entry *) h;
7641 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7643 asection *s = p->sec->output_section;
7645 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7647 struct bfd_link_info *info = inf;
7649 info->flags |= DF_TEXTREL;
7651 /* Not an error, just cut short the traversal. */
7652 return FALSE;
7655 return TRUE;
7658 /* Set the sizes of the dynamic sections. */
7660 static bfd_boolean
7661 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
7662 struct bfd_link_info *info)
7664 struct ppc_link_hash_table *htab;
7665 bfd *dynobj;
7666 asection *s;
7667 bfd_boolean relocs;
7668 bfd *ibfd;
7670 htab = ppc_hash_table (info);
7671 dynobj = htab->elf.dynobj;
7672 if (dynobj == NULL)
7673 abort ();
7675 if (htab->elf.dynamic_sections_created)
7677 /* Set the contents of the .interp section to the interpreter. */
7678 if (info->executable)
7680 s = bfd_get_section_by_name (dynobj, ".interp");
7681 if (s == NULL)
7682 abort ();
7683 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
7684 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
7688 /* Set up .got offsets for local syms, and space for local dynamic
7689 relocs. */
7690 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7692 struct got_entry **lgot_ents;
7693 struct got_entry **end_lgot_ents;
7694 char *lgot_masks;
7695 bfd_size_type locsymcount;
7696 Elf_Internal_Shdr *symtab_hdr;
7697 asection *srel;
7699 if (!is_ppc64_elf_target (ibfd->xvec))
7700 continue;
7702 if (ppc64_tlsld_got (ibfd)->refcount > 0)
7704 s = ppc64_elf_tdata (ibfd)->got;
7705 ppc64_tlsld_got (ibfd)->offset = s->size;
7706 s->size += 16;
7707 if (info->shared)
7709 srel = ppc64_elf_tdata (ibfd)->relgot;
7710 srel->size += sizeof (Elf64_External_Rela);
7713 else
7714 ppc64_tlsld_got (ibfd)->offset = (bfd_vma) -1;
7716 for (s = ibfd->sections; s != NULL; s = s->next)
7718 struct ppc_dyn_relocs *p;
7720 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
7722 if (!bfd_is_abs_section (p->sec)
7723 && bfd_is_abs_section (p->sec->output_section))
7725 /* Input section has been discarded, either because
7726 it is a copy of a linkonce section or due to
7727 linker script /DISCARD/, so we'll be discarding
7728 the relocs too. */
7730 else if (p->count != 0)
7732 srel = elf_section_data (p->sec)->sreloc;
7733 srel->size += p->count * sizeof (Elf64_External_Rela);
7734 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
7735 info->flags |= DF_TEXTREL;
7740 lgot_ents = elf_local_got_ents (ibfd);
7741 if (!lgot_ents)
7742 continue;
7744 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7745 locsymcount = symtab_hdr->sh_info;
7746 end_lgot_ents = lgot_ents + locsymcount;
7747 lgot_masks = (char *) end_lgot_ents;
7748 s = ppc64_elf_tdata (ibfd)->got;
7749 srel = ppc64_elf_tdata (ibfd)->relgot;
7750 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
7752 struct got_entry *ent;
7754 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
7755 if (ent->got.refcount > 0)
7757 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
7759 if (ppc64_tlsld_got (ibfd)->offset == (bfd_vma) -1)
7761 ppc64_tlsld_got (ibfd)->offset = s->size;
7762 s->size += 16;
7763 if (info->shared)
7764 srel->size += sizeof (Elf64_External_Rela);
7766 ent->got.offset = ppc64_tlsld_got (ibfd)->offset;
7768 else
7770 ent->got.offset = s->size;
7771 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
7773 s->size += 16;
7774 if (info->shared)
7775 srel->size += 2 * sizeof (Elf64_External_Rela);
7777 else
7779 s->size += 8;
7780 if (info->shared)
7781 srel->size += sizeof (Elf64_External_Rela);
7785 else
7786 ent->got.offset = (bfd_vma) -1;
7790 /* Allocate global sym .plt and .got entries, and space for global
7791 sym dynamic relocs. */
7792 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
7794 /* We now have determined the sizes of the various dynamic sections.
7795 Allocate memory for them. */
7796 relocs = FALSE;
7797 for (s = dynobj->sections; s != NULL; s = s->next)
7799 if ((s->flags & SEC_LINKER_CREATED) == 0)
7800 continue;
7802 if (s == htab->brlt || s == htab->relbrlt)
7803 /* These haven't been allocated yet; don't strip. */
7804 continue;
7805 else if (s == htab->got
7806 || s == htab->plt
7807 || s == htab->glink
7808 || s == htab->dynbss)
7810 /* Strip this section if we don't need it; see the
7811 comment below. */
7813 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
7815 if (s->size != 0)
7817 if (s != htab->relplt)
7818 relocs = TRUE;
7820 /* We use the reloc_count field as a counter if we need
7821 to copy relocs into the output file. */
7822 s->reloc_count = 0;
7825 else
7827 /* It's not one of our sections, so don't allocate space. */
7828 continue;
7831 if (s->size == 0)
7833 /* If we don't need this section, strip it from the
7834 output file. This is mostly to handle .rela.bss and
7835 .rela.plt. We must create both sections in
7836 create_dynamic_sections, because they must be created
7837 before the linker maps input sections to output
7838 sections. The linker does that before
7839 adjust_dynamic_symbol is called, and it is that
7840 function which decides whether anything needs to go
7841 into these sections. */
7842 s->flags |= SEC_EXCLUDE;
7843 continue;
7846 if ((s->flags & SEC_HAS_CONTENTS) == 0)
7847 continue;
7849 /* Allocate memory for the section contents. We use bfd_zalloc
7850 here in case unused entries are not reclaimed before the
7851 section's contents are written out. This should not happen,
7852 but this way if it does we get a R_PPC64_NONE reloc in .rela
7853 sections instead of garbage.
7854 We also rely on the section contents being zero when writing
7855 the GOT. */
7856 s->contents = bfd_zalloc (dynobj, s->size);
7857 if (s->contents == NULL)
7858 return FALSE;
7861 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7863 if (!is_ppc64_elf_target (ibfd->xvec))
7864 continue;
7866 s = ppc64_elf_tdata (ibfd)->got;
7867 if (s != NULL && s != htab->got)
7869 if (s->size == 0)
7870 s->flags |= SEC_EXCLUDE;
7871 else
7873 s->contents = bfd_zalloc (ibfd, s->size);
7874 if (s->contents == NULL)
7875 return FALSE;
7878 s = ppc64_elf_tdata (ibfd)->relgot;
7879 if (s != NULL)
7881 if (s->size == 0)
7882 s->flags |= SEC_EXCLUDE;
7883 else
7885 s->contents = bfd_zalloc (ibfd, s->size);
7886 if (s->contents == NULL)
7887 return FALSE;
7888 relocs = TRUE;
7889 s->reloc_count = 0;
7894 if (htab->elf.dynamic_sections_created)
7896 /* Add some entries to the .dynamic section. We fill in the
7897 values later, in ppc64_elf_finish_dynamic_sections, but we
7898 must add the entries now so that we get the correct size for
7899 the .dynamic section. The DT_DEBUG entry is filled in by the
7900 dynamic linker and used by the debugger. */
7901 #define add_dynamic_entry(TAG, VAL) \
7902 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
7904 if (info->executable)
7906 if (!add_dynamic_entry (DT_DEBUG, 0))
7907 return FALSE;
7910 if (htab->plt != NULL && htab->plt->size != 0)
7912 if (!add_dynamic_entry (DT_PLTGOT, 0)
7913 || !add_dynamic_entry (DT_PLTRELSZ, 0)
7914 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
7915 || !add_dynamic_entry (DT_JMPREL, 0)
7916 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
7917 return FALSE;
7920 if (NO_OPD_RELOCS)
7922 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
7923 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
7924 return FALSE;
7927 if (relocs)
7929 if (!add_dynamic_entry (DT_RELA, 0)
7930 || !add_dynamic_entry (DT_RELASZ, 0)
7931 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
7932 return FALSE;
7934 /* If any dynamic relocs apply to a read-only section,
7935 then we need a DT_TEXTREL entry. */
7936 if ((info->flags & DF_TEXTREL) == 0)
7937 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
7939 if ((info->flags & DF_TEXTREL) != 0)
7941 if (!add_dynamic_entry (DT_TEXTREL, 0))
7942 return FALSE;
7946 #undef add_dynamic_entry
7948 return TRUE;
7951 /* Determine the type of stub needed, if any, for a call. */
7953 static inline enum ppc_stub_type
7954 ppc_type_of_stub (asection *input_sec,
7955 const Elf_Internal_Rela *rel,
7956 struct ppc_link_hash_entry **hash,
7957 bfd_vma destination)
7959 struct ppc_link_hash_entry *h = *hash;
7960 bfd_vma location;
7961 bfd_vma branch_offset;
7962 bfd_vma max_branch_offset;
7963 enum elf_ppc64_reloc_type r_type;
7965 if (h != NULL)
7967 struct ppc_link_hash_entry *fdh = h;
7968 if (fdh->oh != NULL
7969 && fdh->oh->is_func_descriptor)
7970 fdh = fdh->oh;
7972 if (fdh->elf.dynindx != -1)
7974 struct plt_entry *ent;
7976 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
7977 if (ent->addend == rel->r_addend
7978 && ent->plt.offset != (bfd_vma) -1)
7980 *hash = fdh;
7981 return ppc_stub_plt_call;
7985 /* Here, we know we don't have a plt entry. If we don't have a
7986 either a defined function descriptor or a defined entry symbol
7987 in a regular object file, then it is pointless trying to make
7988 any other type of stub. */
7989 if (!((fdh->elf.root.type == bfd_link_hash_defined
7990 || fdh->elf.root.type == bfd_link_hash_defweak)
7991 && fdh->elf.root.u.def.section->output_section != NULL)
7992 && !((h->elf.root.type == bfd_link_hash_defined
7993 || h->elf.root.type == bfd_link_hash_defweak)
7994 && h->elf.root.u.def.section->output_section != NULL))
7995 return ppc_stub_none;
7998 /* Determine where the call point is. */
7999 location = (input_sec->output_offset
8000 + input_sec->output_section->vma
8001 + rel->r_offset);
8003 branch_offset = destination - location;
8004 r_type = ELF64_R_TYPE (rel->r_info);
8006 /* Determine if a long branch stub is needed. */
8007 max_branch_offset = 1 << 25;
8008 if (r_type != R_PPC64_REL24)
8009 max_branch_offset = 1 << 15;
8011 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
8012 /* We need a stub. Figure out whether a long_branch or plt_branch
8013 is needed later. */
8014 return ppc_stub_long_branch;
8016 return ppc_stub_none;
8019 /* Build a .plt call stub. */
8021 static inline bfd_byte *
8022 build_plt_stub (bfd *obfd, bfd_byte *p, int offset)
8024 #define PPC_LO(v) ((v) & 0xffff)
8025 #define PPC_HI(v) (((v) >> 16) & 0xffff)
8026 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
8028 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
8029 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
8030 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
8031 if (PPC_HA (offset + 8) != PPC_HA (offset))
8032 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
8033 offset += 8;
8034 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p), p += 4;
8035 if (PPC_HA (offset + 8) != PPC_HA (offset))
8036 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
8037 offset += 8;
8038 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
8039 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
8040 bfd_put_32 (obfd, BCTR, p), p += 4;
8041 return p;
8044 static bfd_boolean
8045 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
8047 struct ppc_stub_hash_entry *stub_entry;
8048 struct ppc_branch_hash_entry *br_entry;
8049 struct bfd_link_info *info;
8050 struct ppc_link_hash_table *htab;
8051 bfd_byte *loc;
8052 bfd_byte *p;
8053 unsigned int indx;
8054 struct plt_entry *ent;
8055 bfd_vma dest, off;
8056 int size;
8058 /* Massage our args to the form they really have. */
8059 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
8060 info = in_arg;
8062 htab = ppc_hash_table (info);
8064 /* Make a note of the offset within the stubs for this entry. */
8065 stub_entry->stub_offset = stub_entry->stub_sec->size;
8066 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
8068 htab->stub_count[stub_entry->stub_type - 1] += 1;
8069 switch (stub_entry->stub_type)
8071 case ppc_stub_long_branch:
8072 case ppc_stub_long_branch_r2off:
8073 /* Branches are relative. This is where we are going to. */
8074 off = dest = (stub_entry->target_value
8075 + stub_entry->target_section->output_offset
8076 + stub_entry->target_section->output_section->vma);
8078 /* And this is where we are coming from. */
8079 off -= (stub_entry->stub_offset
8080 + stub_entry->stub_sec->output_offset
8081 + stub_entry->stub_sec->output_section->vma);
8083 if (stub_entry->stub_type != ppc_stub_long_branch_r2off)
8084 size = 4;
8085 else
8087 bfd_vma r2off;
8089 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8090 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8091 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
8092 loc += 4;
8093 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
8094 loc += 4;
8095 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
8096 loc += 4;
8097 off -= 12;
8098 size = 16;
8100 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
8102 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8104 (*_bfd_error_handler) (_("long branch stub `%s' offset overflow"),
8105 stub_entry->root.string);
8106 htab->stub_error = TRUE;
8107 return FALSE;
8110 if (info->emitrelocations)
8112 Elf_Internal_Rela *relocs, *r;
8113 struct bfd_elf_section_data *elfsec_data;
8115 elfsec_data = elf_section_data (stub_entry->stub_sec);
8116 relocs = elfsec_data->relocs;
8117 if (relocs == NULL)
8119 bfd_size_type relsize;
8120 relsize = stub_entry->stub_sec->reloc_count * sizeof (*relocs);
8121 relocs = bfd_alloc (htab->stub_bfd, relsize);
8122 if (relocs == NULL)
8123 return FALSE;
8124 elfsec_data->relocs = relocs;
8125 elfsec_data->rel_hdr.sh_size = relsize;
8126 elfsec_data->rel_hdr.sh_entsize = 24;
8127 stub_entry->stub_sec->reloc_count = 0;
8129 r = relocs + stub_entry->stub_sec->reloc_count;
8130 stub_entry->stub_sec->reloc_count += 1;
8131 r->r_offset = loc - stub_entry->stub_sec->contents;
8132 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
8133 r->r_addend = dest;
8134 if (stub_entry->h != NULL)
8136 struct elf_link_hash_entry **hashes;
8137 unsigned long symndx;
8138 struct ppc_link_hash_entry *h;
8140 hashes = elf_sym_hashes (htab->stub_bfd);
8141 if (hashes == NULL)
8143 bfd_size_type hsize;
8145 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
8146 hashes = bfd_zalloc (htab->stub_bfd, hsize);
8147 if (hashes == NULL)
8148 return FALSE;
8149 elf_sym_hashes (htab->stub_bfd) = hashes;
8150 htab->stub_globals = 1;
8152 symndx = htab->stub_globals++;
8153 h = stub_entry->h;
8154 hashes[symndx] = &h->elf;
8155 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
8156 if (h->oh != NULL && h->oh->is_func)
8157 h = h->oh;
8158 if (h->elf.root.u.def.section != stub_entry->target_section)
8159 /* H is an opd symbol. The addend must be zero. */
8160 r->r_addend = 0;
8161 else
8163 off = (h->elf.root.u.def.value
8164 + h->elf.root.u.def.section->output_offset
8165 + h->elf.root.u.def.section->output_section->vma);
8166 r->r_addend -= off;
8170 break;
8172 case ppc_stub_plt_branch:
8173 case ppc_stub_plt_branch_r2off:
8174 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8175 stub_entry->root.string + 9,
8176 FALSE, FALSE);
8177 if (br_entry == NULL)
8179 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
8180 stub_entry->root.string);
8181 htab->stub_error = TRUE;
8182 return FALSE;
8185 off = (stub_entry->target_value
8186 + stub_entry->target_section->output_offset
8187 + stub_entry->target_section->output_section->vma);
8189 bfd_put_64 (htab->brlt->owner, off,
8190 htab->brlt->contents + br_entry->offset);
8192 if (htab->relbrlt != NULL)
8194 /* Create a reloc for the branch lookup table entry. */
8195 Elf_Internal_Rela rela;
8196 bfd_byte *rl;
8198 rela.r_offset = (br_entry->offset
8199 + htab->brlt->output_offset
8200 + htab->brlt->output_section->vma);
8201 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
8202 rela.r_addend = off;
8204 rl = htab->relbrlt->contents;
8205 rl += htab->relbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
8206 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
8209 off = (br_entry->offset
8210 + htab->brlt->output_offset
8211 + htab->brlt->output_section->vma
8212 - elf_gp (htab->brlt->output_section->owner)
8213 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8215 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
8217 (*_bfd_error_handler)
8218 (_("linkage table error against `%s'"),
8219 stub_entry->root.string);
8220 bfd_set_error (bfd_error_bad_value);
8221 htab->stub_error = TRUE;
8222 return FALSE;
8225 indx = off;
8226 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
8228 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
8229 loc += 4;
8230 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
8231 size = 16;
8233 else
8235 bfd_vma r2off;
8237 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8238 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8239 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
8240 loc += 4;
8241 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
8242 loc += 4;
8243 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
8244 loc += 4;
8245 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
8246 loc += 4;
8247 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
8248 size = 28;
8250 loc += 4;
8251 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
8252 loc += 4;
8253 bfd_put_32 (htab->stub_bfd, BCTR, loc);
8254 break;
8256 case ppc_stub_plt_call:
8257 /* Do the best we can for shared libraries built without
8258 exporting ".foo" for each "foo". This can happen when symbol
8259 versioning scripts strip all bar a subset of symbols. */
8260 if (stub_entry->h->oh != NULL
8261 && stub_entry->h->oh->elf.root.type != bfd_link_hash_defined
8262 && stub_entry->h->oh->elf.root.type != bfd_link_hash_defweak)
8264 /* Point the symbol at the stub. There may be multiple stubs,
8265 we don't really care; The main thing is to make this sym
8266 defined somewhere. Maybe defining the symbol in the stub
8267 section is a silly idea. If we didn't do this, htab->top_id
8268 could disappear. */
8269 stub_entry->h->oh->elf.root.type = bfd_link_hash_defined;
8270 stub_entry->h->oh->elf.root.u.def.section = stub_entry->stub_sec;
8271 stub_entry->h->oh->elf.root.u.def.value = stub_entry->stub_offset;
8274 /* Now build the stub. */
8275 off = (bfd_vma) -1;
8276 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8277 if (ent->addend == stub_entry->addend)
8279 off = ent->plt.offset;
8280 break;
8282 if (off >= (bfd_vma) -2)
8283 abort ();
8285 off &= ~ (bfd_vma) 1;
8286 off += (htab->plt->output_offset
8287 + htab->plt->output_section->vma
8288 - elf_gp (htab->plt->output_section->owner)
8289 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8291 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
8293 (*_bfd_error_handler)
8294 (_("linkage table error against `%s'"),
8295 stub_entry->h->elf.root.root.string);
8296 bfd_set_error (bfd_error_bad_value);
8297 htab->stub_error = TRUE;
8298 return FALSE;
8301 p = build_plt_stub (htab->stub_bfd, loc, off);
8302 size = p - loc;
8303 break;
8305 default:
8306 BFD_FAIL ();
8307 return FALSE;
8310 stub_entry->stub_sec->size += size;
8312 if (htab->emit_stub_syms)
8314 struct elf_link_hash_entry *h;
8315 size_t len1, len2;
8316 char *name;
8317 const char *const stub_str[] = { "long_branch",
8318 "long_branch_r2off",
8319 "plt_branch",
8320 "plt_branch_r2off",
8321 "plt_call" };
8323 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
8324 len2 = strlen (stub_entry->root.string);
8325 name = bfd_malloc (len1 + len2 + 2);
8326 if (name == NULL)
8327 return FALSE;
8328 memcpy (name, stub_entry->root.string, 9);
8329 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
8330 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
8331 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
8332 if (h == NULL)
8333 return FALSE;
8334 if (h->root.type == bfd_link_hash_new)
8336 h->root.type = bfd_link_hash_defined;
8337 h->root.u.def.section = stub_entry->stub_sec;
8338 h->root.u.def.value = stub_entry->stub_offset;
8339 h->ref_regular = 1;
8340 h->def_regular = 1;
8341 h->ref_regular_nonweak = 1;
8342 h->forced_local = 1;
8343 h->non_elf = 0;
8347 return TRUE;
8350 /* As above, but don't actually build the stub. Just bump offset so
8351 we know stub section sizes, and select plt_branch stubs where
8352 long_branch stubs won't do. */
8354 static bfd_boolean
8355 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
8357 struct ppc_stub_hash_entry *stub_entry;
8358 struct bfd_link_info *info;
8359 struct ppc_link_hash_table *htab;
8360 bfd_vma off;
8361 int size;
8363 /* Massage our args to the form they really have. */
8364 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
8365 info = in_arg;
8367 htab = ppc_hash_table (info);
8369 if (stub_entry->stub_type == ppc_stub_plt_call)
8371 struct plt_entry *ent;
8372 off = (bfd_vma) -1;
8373 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8374 if (ent->addend == stub_entry->addend)
8376 off = ent->plt.offset & ~(bfd_vma) 1;
8377 break;
8379 if (off >= (bfd_vma) -2)
8380 abort ();
8381 off += (htab->plt->output_offset
8382 + htab->plt->output_section->vma
8383 - elf_gp (htab->plt->output_section->owner)
8384 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8386 size = PLT_CALL_STUB_SIZE;
8387 if (PPC_HA (off + 16) != PPC_HA (off))
8388 size += 4;
8390 else
8392 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
8393 variants. */
8394 off = (stub_entry->target_value
8395 + stub_entry->target_section->output_offset
8396 + stub_entry->target_section->output_section->vma);
8397 off -= (stub_entry->stub_sec->size
8398 + stub_entry->stub_sec->output_offset
8399 + stub_entry->stub_sec->output_section->vma);
8401 /* Reset the stub type from the plt variant in case we now
8402 can reach with a shorter stub. */
8403 if (stub_entry->stub_type >= ppc_stub_plt_branch)
8404 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
8406 size = 4;
8407 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
8409 off -= 12;
8410 size = 16;
8413 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
8414 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8416 struct ppc_branch_hash_entry *br_entry;
8418 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8419 stub_entry->root.string + 9,
8420 TRUE, FALSE);
8421 if (br_entry == NULL)
8423 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
8424 stub_entry->root.string);
8425 htab->stub_error = TRUE;
8426 return FALSE;
8429 if (br_entry->iter != htab->stub_iteration)
8431 br_entry->iter = htab->stub_iteration;
8432 br_entry->offset = htab->brlt->size;
8433 htab->brlt->size += 8;
8435 if (htab->relbrlt != NULL)
8436 htab->relbrlt->size += sizeof (Elf64_External_Rela);
8439 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
8440 size = 16;
8441 if (stub_entry->stub_type != ppc_stub_plt_branch)
8442 size = 28;
8445 if (info->emitrelocations
8446 && (stub_entry->stub_type == ppc_stub_long_branch
8447 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
8448 stub_entry->stub_sec->reloc_count += 1;
8451 stub_entry->stub_sec->size += size;
8452 return TRUE;
8455 /* Set up various things so that we can make a list of input sections
8456 for each output section included in the link. Returns -1 on error,
8457 0 when no stubs will be needed, and 1 on success. */
8460 ppc64_elf_setup_section_lists (bfd *output_bfd,
8461 struct bfd_link_info *info,
8462 int no_multi_toc)
8464 bfd *input_bfd;
8465 int top_id, top_index, id;
8466 asection *section;
8467 asection **input_list;
8468 bfd_size_type amt;
8469 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8471 htab->no_multi_toc = no_multi_toc;
8473 if (htab->brlt == NULL)
8474 return 0;
8476 /* Find the top input section id. */
8477 for (input_bfd = info->input_bfds, top_id = 3;
8478 input_bfd != NULL;
8479 input_bfd = input_bfd->link_next)
8481 for (section = input_bfd->sections;
8482 section != NULL;
8483 section = section->next)
8485 if (top_id < section->id)
8486 top_id = section->id;
8490 htab->top_id = top_id;
8491 amt = sizeof (struct map_stub) * (top_id + 1);
8492 htab->stub_group = bfd_zmalloc (amt);
8493 if (htab->stub_group == NULL)
8494 return -1;
8496 /* Set toc_off for com, und, abs and ind sections. */
8497 for (id = 0; id < 3; id++)
8498 htab->stub_group[id].toc_off = TOC_BASE_OFF;
8500 elf_gp (output_bfd) = htab->toc_curr = ppc64_elf_toc (output_bfd);
8502 /* We can't use output_bfd->section_count here to find the top output
8503 section index as some sections may have been removed, and
8504 strip_excluded_output_sections doesn't renumber the indices. */
8505 for (section = output_bfd->sections, top_index = 0;
8506 section != NULL;
8507 section = section->next)
8509 if (top_index < section->index)
8510 top_index = section->index;
8513 htab->top_index = top_index;
8514 amt = sizeof (asection *) * (top_index + 1);
8515 input_list = bfd_zmalloc (amt);
8516 htab->input_list = input_list;
8517 if (input_list == NULL)
8518 return -1;
8520 return 1;
8523 /* The linker repeatedly calls this function for each TOC input section
8524 and linker generated GOT section. Group input bfds such that the toc
8525 within a group is less than 64k in size. Will break with cute linker
8526 scripts that play games with dot in the output toc section. */
8528 void
8529 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
8531 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8533 if (!htab->no_multi_toc)
8535 bfd_vma addr = isec->output_offset + isec->output_section->vma;
8536 bfd_vma off = addr - htab->toc_curr;
8538 if (off + isec->size > 0x10000)
8539 htab->toc_curr = addr;
8541 elf_gp (isec->owner) = (htab->toc_curr
8542 - elf_gp (isec->output_section->owner)
8543 + TOC_BASE_OFF);
8547 /* Called after the last call to the above function. */
8549 void
8550 ppc64_elf_reinit_toc (bfd *output_bfd, struct bfd_link_info *info)
8552 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8554 htab->multi_toc_needed = htab->toc_curr != elf_gp (output_bfd);
8556 /* toc_curr tracks the TOC offset used for code sections below in
8557 ppc64_elf_next_input_section. Start off at 0x8000. */
8558 htab->toc_curr = TOC_BASE_OFF;
8561 /* No toc references were found in ISEC. If the code in ISEC makes no
8562 calls, then there's no need to use toc adjusting stubs when branching
8563 into ISEC. Actually, indirect calls from ISEC are OK as they will
8564 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
8565 needed, and 2 if a cyclical call-graph was found but no other reason
8566 for a stub was detected. If called from the top level, a return of
8567 2 means the same as a return of 0. */
8569 static int
8570 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
8572 Elf_Internal_Rela *relstart, *rel;
8573 Elf_Internal_Sym *local_syms;
8574 int ret;
8575 struct ppc_link_hash_table *htab;
8577 /* We know none of our code bearing sections will need toc stubs. */
8578 if ((isec->flags & SEC_LINKER_CREATED) != 0)
8579 return 0;
8581 if (isec->size == 0)
8582 return 0;
8584 if (isec->output_section == NULL)
8585 return 0;
8587 /* Hack for linux kernel. .fixup contains branches, but only back to
8588 the function that hit an exception. */
8589 if (strcmp (isec->name, ".fixup") == 0)
8590 return 0;
8592 if (isec->reloc_count == 0)
8593 return 0;
8595 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
8596 info->keep_memory);
8597 if (relstart == NULL)
8598 return -1;
8600 /* Look for branches to outside of this section. */
8601 local_syms = NULL;
8602 ret = 0;
8603 htab = ppc_hash_table (info);
8604 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
8606 enum elf_ppc64_reloc_type r_type;
8607 unsigned long r_symndx;
8608 struct elf_link_hash_entry *h;
8609 Elf_Internal_Sym *sym;
8610 asection *sym_sec;
8611 long *opd_adjust;
8612 bfd_vma sym_value;
8613 bfd_vma dest;
8615 r_type = ELF64_R_TYPE (rel->r_info);
8616 if (r_type != R_PPC64_REL24
8617 && r_type != R_PPC64_REL14
8618 && r_type != R_PPC64_REL14_BRTAKEN
8619 && r_type != R_PPC64_REL14_BRNTAKEN)
8620 continue;
8622 r_symndx = ELF64_R_SYM (rel->r_info);
8623 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
8624 isec->owner))
8626 ret = -1;
8627 break;
8630 /* Calls to dynamic lib functions go through a plt call stub
8631 that uses r2. Branches to undefined symbols might be a call
8632 using old-style dot symbols that can be satisfied by a plt
8633 call into a new-style dynamic library. */
8634 if (sym_sec == NULL)
8636 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8637 if (eh != NULL
8638 && eh->oh != NULL
8639 && eh->oh->elf.plt.plist != NULL)
8641 ret = 1;
8642 break;
8645 /* Ignore other undefined symbols. */
8646 continue;
8649 /* Assume branches to other sections not included in the link need
8650 stubs too, to cover -R and absolute syms. */
8651 if (sym_sec->output_section == NULL)
8653 ret = 1;
8654 break;
8657 if (h == NULL)
8658 sym_value = sym->st_value;
8659 else
8661 if (h->root.type != bfd_link_hash_defined
8662 && h->root.type != bfd_link_hash_defweak)
8663 abort ();
8664 sym_value = h->root.u.def.value;
8666 sym_value += rel->r_addend;
8668 /* If this branch reloc uses an opd sym, find the code section. */
8669 opd_adjust = get_opd_info (sym_sec);
8670 if (opd_adjust != NULL)
8672 if (h == NULL)
8674 long adjust;
8676 adjust = opd_adjust[sym->st_value / 8];
8677 if (adjust == -1)
8678 /* Assume deleted functions won't ever be called. */
8679 continue;
8680 sym_value += adjust;
8683 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
8684 if (dest == (bfd_vma) -1)
8685 continue;
8687 else
8688 dest = (sym_value
8689 + sym_sec->output_offset
8690 + sym_sec->output_section->vma);
8692 /* Ignore branch to self. */
8693 if (sym_sec == isec)
8694 continue;
8696 /* If the called function uses the toc, we need a stub. */
8697 if (sym_sec->has_toc_reloc
8698 || sym_sec->makes_toc_func_call)
8700 ret = 1;
8701 break;
8704 /* Assume any branch that needs a long branch stub might in fact
8705 need a plt_branch stub. A plt_branch stub uses r2. */
8706 else if (dest - (isec->output_offset
8707 + isec->output_section->vma
8708 + rel->r_offset) + (1 << 25) >= (2 << 25))
8710 ret = 1;
8711 break;
8714 /* If calling back to a section in the process of being tested, we
8715 can't say for sure that no toc adjusting stubs are needed, so
8716 don't return zero. */
8717 else if (sym_sec->call_check_in_progress)
8718 ret = 2;
8720 /* Branches to another section that itself doesn't have any TOC
8721 references are OK. Recursively call ourselves to check. */
8722 else if (sym_sec->id <= htab->top_id
8723 && htab->stub_group[sym_sec->id].toc_off == 0)
8725 int recur;
8727 /* Mark current section as indeterminate, so that other
8728 sections that call back to current won't be marked as
8729 known. */
8730 isec->call_check_in_progress = 1;
8731 recur = toc_adjusting_stub_needed (info, sym_sec);
8732 isec->call_check_in_progress = 0;
8734 if (recur < 0)
8736 /* An error. Exit. */
8737 ret = -1;
8738 break;
8740 else if (recur <= 1)
8742 /* Known result. Mark as checked and set section flag. */
8743 htab->stub_group[sym_sec->id].toc_off = 1;
8744 if (recur != 0)
8746 sym_sec->makes_toc_func_call = 1;
8747 ret = 1;
8748 break;
8751 else
8753 /* Unknown result. Continue checking. */
8754 ret = 2;
8759 if (local_syms != NULL
8760 && (elf_tdata (isec->owner)->symtab_hdr.contents
8761 != (unsigned char *) local_syms))
8762 free (local_syms);
8763 if (elf_section_data (isec)->relocs != relstart)
8764 free (relstart);
8766 return ret;
8769 /* The linker repeatedly calls this function for each input section,
8770 in the order that input sections are linked into output sections.
8771 Build lists of input sections to determine groupings between which
8772 we may insert linker stubs. */
8774 bfd_boolean
8775 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
8777 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8779 if ((isec->output_section->flags & SEC_CODE) != 0
8780 && isec->output_section->index <= htab->top_index)
8782 asection **list = htab->input_list + isec->output_section->index;
8783 /* Steal the link_sec pointer for our list. */
8784 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
8785 /* This happens to make the list in reverse order,
8786 which is what we want. */
8787 PREV_SEC (isec) = *list;
8788 *list = isec;
8791 if (htab->multi_toc_needed)
8793 /* If a code section has a function that uses the TOC then we need
8794 to use the right TOC (obviously). Also, make sure that .opd gets
8795 the correct TOC value for R_PPC64_TOC relocs that don't have or
8796 can't find their function symbol (shouldn't ever happen now). */
8797 if (isec->has_toc_reloc || (isec->flags & SEC_CODE) == 0)
8799 if (elf_gp (isec->owner) != 0)
8800 htab->toc_curr = elf_gp (isec->owner);
8802 else if (htab->stub_group[isec->id].toc_off == 0)
8804 int ret = toc_adjusting_stub_needed (info, isec);
8805 if (ret < 0)
8806 return FALSE;
8807 else
8808 isec->makes_toc_func_call = ret & 1;
8812 /* Functions that don't use the TOC can belong in any TOC group.
8813 Use the last TOC base. This happens to make _init and _fini
8814 pasting work. */
8815 htab->stub_group[isec->id].toc_off = htab->toc_curr;
8816 return TRUE;
8819 /* See whether we can group stub sections together. Grouping stub
8820 sections may result in fewer stubs. More importantly, we need to
8821 put all .init* and .fini* stubs at the beginning of the .init or
8822 .fini output sections respectively, because glibc splits the
8823 _init and _fini functions into multiple parts. Putting a stub in
8824 the middle of a function is not a good idea. */
8826 static void
8827 group_sections (struct ppc_link_hash_table *htab,
8828 bfd_size_type stub_group_size,
8829 bfd_boolean stubs_always_before_branch)
8831 asection **list = htab->input_list + htab->top_index;
8834 asection *tail = *list;
8835 while (tail != NULL)
8837 asection *curr;
8838 asection *prev;
8839 bfd_size_type total;
8840 bfd_boolean big_sec;
8841 bfd_vma curr_toc;
8843 curr = tail;
8844 total = tail->size;
8845 big_sec = total > stub_group_size;
8846 if (big_sec)
8847 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
8848 tail->owner, tail);
8849 curr_toc = htab->stub_group[tail->id].toc_off;
8851 while ((prev = PREV_SEC (curr)) != NULL
8852 && ((total += curr->output_offset - prev->output_offset)
8853 < stub_group_size)
8854 && htab->stub_group[prev->id].toc_off == curr_toc)
8855 curr = prev;
8857 /* OK, the size from the start of CURR to the end is less
8858 than stub_group_size and thus can be handled by one stub
8859 section. (or the tail section is itself larger than
8860 stub_group_size, in which case we may be toast.) We
8861 should really be keeping track of the total size of stubs
8862 added here, as stubs contribute to the final output
8863 section size. That's a little tricky, and this way will
8864 only break if stubs added make the total size more than
8865 2^25, ie. for the default stub_group_size, if stubs total
8866 more than 2097152 bytes, or nearly 75000 plt call stubs. */
8869 prev = PREV_SEC (tail);
8870 /* Set up this stub group. */
8871 htab->stub_group[tail->id].link_sec = curr;
8873 while (tail != curr && (tail = prev) != NULL);
8875 /* But wait, there's more! Input sections up to stub_group_size
8876 bytes before the stub section can be handled by it too.
8877 Don't do this if we have a really large section after the
8878 stubs, as adding more stubs increases the chance that
8879 branches may not reach into the stub section. */
8880 if (!stubs_always_before_branch && !big_sec)
8882 total = 0;
8883 while (prev != NULL
8884 && ((total += tail->output_offset - prev->output_offset)
8885 < stub_group_size)
8886 && htab->stub_group[prev->id].toc_off == curr_toc)
8888 tail = prev;
8889 prev = PREV_SEC (tail);
8890 htab->stub_group[tail->id].link_sec = curr;
8893 tail = prev;
8896 while (list-- != htab->input_list);
8897 free (htab->input_list);
8898 #undef PREV_SEC
8901 /* Determine and set the size of the stub section for a final link.
8903 The basic idea here is to examine all the relocations looking for
8904 PC-relative calls to a target that is unreachable with a "bl"
8905 instruction. */
8907 bfd_boolean
8908 ppc64_elf_size_stubs (bfd *output_bfd,
8909 struct bfd_link_info *info,
8910 bfd_signed_vma group_size,
8911 asection *(*add_stub_section) (const char *, asection *),
8912 void (*layout_sections_again) (void))
8914 bfd_size_type stub_group_size;
8915 bfd_boolean stubs_always_before_branch;
8916 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8918 /* Stash our params away. */
8919 htab->add_stub_section = add_stub_section;
8920 htab->layout_sections_again = layout_sections_again;
8921 stubs_always_before_branch = group_size < 0;
8922 if (group_size < 0)
8923 stub_group_size = -group_size;
8924 else
8925 stub_group_size = group_size;
8926 if (stub_group_size == 1)
8928 /* Default values. */
8929 if (stubs_always_before_branch)
8931 stub_group_size = 0x1e00000;
8932 if (htab->has_14bit_branch)
8933 stub_group_size = 0x7800;
8935 else
8937 stub_group_size = 0x1c00000;
8938 if (htab->has_14bit_branch)
8939 stub_group_size = 0x7000;
8943 group_sections (htab, stub_group_size, stubs_always_before_branch);
8945 while (1)
8947 bfd *input_bfd;
8948 unsigned int bfd_indx;
8949 asection *stub_sec;
8951 htab->stub_iteration += 1;
8953 for (input_bfd = info->input_bfds, bfd_indx = 0;
8954 input_bfd != NULL;
8955 input_bfd = input_bfd->link_next, bfd_indx++)
8957 Elf_Internal_Shdr *symtab_hdr;
8958 asection *section;
8959 Elf_Internal_Sym *local_syms = NULL;
8961 if (!is_ppc64_elf_target (input_bfd->xvec))
8962 continue;
8964 /* We'll need the symbol table in a second. */
8965 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8966 if (symtab_hdr->sh_info == 0)
8967 continue;
8969 /* Walk over each section attached to the input bfd. */
8970 for (section = input_bfd->sections;
8971 section != NULL;
8972 section = section->next)
8974 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
8976 /* If there aren't any relocs, then there's nothing more
8977 to do. */
8978 if ((section->flags & SEC_RELOC) == 0
8979 || section->reloc_count == 0)
8980 continue;
8982 /* If this section is a link-once section that will be
8983 discarded, then don't create any stubs. */
8984 if (section->output_section == NULL
8985 || section->output_section->owner != output_bfd)
8986 continue;
8988 /* Get the relocs. */
8989 internal_relocs
8990 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
8991 info->keep_memory);
8992 if (internal_relocs == NULL)
8993 goto error_ret_free_local;
8995 /* Now examine each relocation. */
8996 irela = internal_relocs;
8997 irelaend = irela + section->reloc_count;
8998 for (; irela < irelaend; irela++)
9000 enum elf_ppc64_reloc_type r_type;
9001 unsigned int r_indx;
9002 enum ppc_stub_type stub_type;
9003 struct ppc_stub_hash_entry *stub_entry;
9004 asection *sym_sec, *code_sec;
9005 bfd_vma sym_value;
9006 bfd_vma destination;
9007 bfd_boolean ok_dest;
9008 struct ppc_link_hash_entry *hash;
9009 struct ppc_link_hash_entry *fdh;
9010 struct elf_link_hash_entry *h;
9011 Elf_Internal_Sym *sym;
9012 char *stub_name;
9013 const asection *id_sec;
9014 long *opd_adjust;
9016 r_type = ELF64_R_TYPE (irela->r_info);
9017 r_indx = ELF64_R_SYM (irela->r_info);
9019 if (r_type >= R_PPC64_max)
9021 bfd_set_error (bfd_error_bad_value);
9022 goto error_ret_free_internal;
9025 /* Only look for stubs on branch instructions. */
9026 if (r_type != R_PPC64_REL24
9027 && r_type != R_PPC64_REL14
9028 && r_type != R_PPC64_REL14_BRTAKEN
9029 && r_type != R_PPC64_REL14_BRNTAKEN)
9030 continue;
9032 /* Now determine the call target, its name, value,
9033 section. */
9034 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9035 r_indx, input_bfd))
9036 goto error_ret_free_internal;
9037 hash = (struct ppc_link_hash_entry *) h;
9039 ok_dest = FALSE;
9040 fdh = NULL;
9041 sym_value = 0;
9042 if (hash == NULL)
9044 sym_value = sym->st_value;
9045 ok_dest = TRUE;
9047 else if (hash->elf.root.type == bfd_link_hash_defined
9048 || hash->elf.root.type == bfd_link_hash_defweak)
9050 sym_value = hash->elf.root.u.def.value;
9051 if (sym_sec->output_section != NULL)
9052 ok_dest = TRUE;
9054 else if (hash->elf.root.type == bfd_link_hash_undefweak
9055 || hash->elf.root.type == bfd_link_hash_undefined)
9057 /* Recognise an old ABI func code entry sym, and
9058 use the func descriptor sym instead if it is
9059 defined. */
9060 if (hash->elf.root.root.string[0] == '.'
9061 && (fdh = get_fdh (hash, htab)) != NULL)
9063 if (fdh->elf.root.type == bfd_link_hash_defined
9064 || fdh->elf.root.type == bfd_link_hash_defweak)
9066 sym_sec = fdh->elf.root.u.def.section;
9067 sym_value = fdh->elf.root.u.def.value;
9068 if (sym_sec->output_section != NULL)
9069 ok_dest = TRUE;
9071 else
9072 fdh = NULL;
9075 else
9077 bfd_set_error (bfd_error_bad_value);
9078 goto error_ret_free_internal;
9081 destination = 0;
9082 if (ok_dest)
9084 sym_value += irela->r_addend;
9085 destination = (sym_value
9086 + sym_sec->output_offset
9087 + sym_sec->output_section->vma);
9090 code_sec = sym_sec;
9091 opd_adjust = get_opd_info (sym_sec);
9092 if (opd_adjust != NULL)
9094 bfd_vma dest;
9096 if (hash == NULL)
9098 long adjust = opd_adjust[sym_value / 8];
9099 if (adjust == -1)
9100 continue;
9101 sym_value += adjust;
9103 dest = opd_entry_value (sym_sec, sym_value,
9104 &code_sec, &sym_value);
9105 if (dest != (bfd_vma) -1)
9107 destination = dest;
9108 if (fdh != NULL)
9110 /* Fixup old ABI sym to point at code
9111 entry. */
9112 hash->elf.root.type = bfd_link_hash_defweak;
9113 hash->elf.root.u.def.section = code_sec;
9114 hash->elf.root.u.def.value = sym_value;
9119 /* Determine what (if any) linker stub is needed. */
9120 stub_type = ppc_type_of_stub (section, irela, &hash,
9121 destination);
9123 if (stub_type != ppc_stub_plt_call)
9125 /* Check whether we need a TOC adjusting stub.
9126 Since the linker pastes together pieces from
9127 different object files when creating the
9128 _init and _fini functions, it may be that a
9129 call to what looks like a local sym is in
9130 fact a call needing a TOC adjustment. */
9131 if (code_sec != NULL
9132 && code_sec->output_section != NULL
9133 && (htab->stub_group[code_sec->id].toc_off
9134 != htab->stub_group[section->id].toc_off)
9135 && (code_sec->has_toc_reloc
9136 || code_sec->makes_toc_func_call))
9137 stub_type = ppc_stub_long_branch_r2off;
9140 if (stub_type == ppc_stub_none)
9141 continue;
9143 /* __tls_get_addr calls might be eliminated. */
9144 if (stub_type != ppc_stub_plt_call
9145 && hash != NULL
9146 && (hash == htab->tls_get_addr
9147 || hash == htab->tls_get_addr_fd)
9148 && section->has_tls_reloc
9149 && irela != internal_relocs)
9151 /* Get tls info. */
9152 char *tls_mask;
9154 if (!get_tls_mask (&tls_mask, NULL, &local_syms,
9155 irela - 1, input_bfd))
9156 goto error_ret_free_internal;
9157 if (*tls_mask != 0)
9158 continue;
9161 /* Support for grouping stub sections. */
9162 id_sec = htab->stub_group[section->id].link_sec;
9164 /* Get the name of this stub. */
9165 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
9166 if (!stub_name)
9167 goto error_ret_free_internal;
9169 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
9170 stub_name, FALSE, FALSE);
9171 if (stub_entry != NULL)
9173 /* The proper stub has already been created. */
9174 free (stub_name);
9175 continue;
9178 stub_entry = ppc_add_stub (stub_name, section, htab);
9179 if (stub_entry == NULL)
9181 free (stub_name);
9182 error_ret_free_internal:
9183 if (elf_section_data (section)->relocs == NULL)
9184 free (internal_relocs);
9185 error_ret_free_local:
9186 if (local_syms != NULL
9187 && (symtab_hdr->contents
9188 != (unsigned char *) local_syms))
9189 free (local_syms);
9190 return FALSE;
9193 stub_entry->stub_type = stub_type;
9194 stub_entry->target_value = sym_value;
9195 stub_entry->target_section = code_sec;
9196 stub_entry->h = hash;
9197 stub_entry->addend = irela->r_addend;
9199 if (stub_entry->h != NULL)
9200 htab->stub_globals += 1;
9203 /* We're done with the internal relocs, free them. */
9204 if (elf_section_data (section)->relocs != internal_relocs)
9205 free (internal_relocs);
9208 if (local_syms != NULL
9209 && symtab_hdr->contents != (unsigned char *) local_syms)
9211 if (!info->keep_memory)
9212 free (local_syms);
9213 else
9214 symtab_hdr->contents = (unsigned char *) local_syms;
9218 /* We may have added some stubs. Find out the new size of the
9219 stub sections. */
9220 for (stub_sec = htab->stub_bfd->sections;
9221 stub_sec != NULL;
9222 stub_sec = stub_sec->next)
9223 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
9225 stub_sec->rawsize = stub_sec->size;
9226 stub_sec->size = 0;
9227 stub_sec->reloc_count = 0;
9230 htab->brlt->size = 0;
9231 if (htab->relbrlt != NULL)
9232 htab->relbrlt->size = 0;
9234 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
9236 for (stub_sec = htab->stub_bfd->sections;
9237 stub_sec != NULL;
9238 stub_sec = stub_sec->next)
9239 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9240 && stub_sec->rawsize != stub_sec->size)
9241 break;
9243 /* Exit from this loop when no stubs have been added, and no stubs
9244 have changed size. */
9245 if (stub_sec == NULL)
9246 break;
9248 /* Ask the linker to do its stuff. */
9249 (*htab->layout_sections_again) ();
9252 /* It would be nice to strip htab->brlt from the output if the
9253 section is empty, but it's too late. If we strip sections here,
9254 the dynamic symbol table is corrupted since the section symbol
9255 for the stripped section isn't written. */
9257 return TRUE;
9260 /* Called after we have determined section placement. If sections
9261 move, we'll be called again. Provide a value for TOCstart. */
9263 bfd_vma
9264 ppc64_elf_toc (bfd *obfd)
9266 asection *s;
9267 bfd_vma TOCstart;
9269 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
9270 order. The TOC starts where the first of these sections starts. */
9271 s = bfd_get_section_by_name (obfd, ".got");
9272 if (s == NULL)
9273 s = bfd_get_section_by_name (obfd, ".toc");
9274 if (s == NULL)
9275 s = bfd_get_section_by_name (obfd, ".tocbss");
9276 if (s == NULL)
9277 s = bfd_get_section_by_name (obfd, ".plt");
9278 if (s == NULL)
9280 /* This may happen for
9281 o references to TOC base (SYM@toc / TOC[tc0]) without a
9282 .toc directive
9283 o bad linker script
9284 o --gc-sections and empty TOC sections
9286 FIXME: Warn user? */
9288 /* Look for a likely section. We probably won't even be
9289 using TOCstart. */
9290 for (s = obfd->sections; s != NULL; s = s->next)
9291 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
9292 == (SEC_ALLOC | SEC_SMALL_DATA))
9293 break;
9294 if (s == NULL)
9295 for (s = obfd->sections; s != NULL; s = s->next)
9296 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
9297 == (SEC_ALLOC | SEC_SMALL_DATA))
9298 break;
9299 if (s == NULL)
9300 for (s = obfd->sections; s != NULL; s = s->next)
9301 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
9302 break;
9303 if (s == NULL)
9304 for (s = obfd->sections; s != NULL; s = s->next)
9305 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
9306 break;
9309 TOCstart = 0;
9310 if (s != NULL)
9311 TOCstart = s->output_section->vma + s->output_offset;
9313 return TOCstart;
9316 /* Build all the stubs associated with the current output file.
9317 The stubs are kept in a hash table attached to the main linker
9318 hash table. This function is called via gldelf64ppc_finish. */
9320 bfd_boolean
9321 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
9322 struct bfd_link_info *info,
9323 char **stats)
9325 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9326 asection *stub_sec;
9327 bfd_byte *p;
9328 int stub_sec_count = 0;
9330 htab->emit_stub_syms = emit_stub_syms;
9332 /* Allocate memory to hold the linker stubs. */
9333 for (stub_sec = htab->stub_bfd->sections;
9334 stub_sec != NULL;
9335 stub_sec = stub_sec->next)
9336 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9337 && stub_sec->size != 0)
9339 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
9340 if (stub_sec->contents == NULL)
9341 return FALSE;
9342 /* We want to check that built size is the same as calculated
9343 size. rawsize is a convenient location to use. */
9344 stub_sec->rawsize = stub_sec->size;
9345 stub_sec->size = 0;
9348 if (htab->plt != NULL)
9350 unsigned int indx;
9351 bfd_vma plt0;
9353 /* Build the .glink plt call stub. */
9354 plt0 = (htab->plt->output_section->vma
9355 + htab->plt->output_offset
9356 - (htab->glink->output_section->vma
9357 + htab->glink->output_offset
9358 + GLINK_CALL_STUB_SIZE));
9359 if (plt0 + 0x80008000 > 0xffffffff)
9361 (*_bfd_error_handler) (_(".glink and .plt too far apart"));
9362 bfd_set_error (bfd_error_bad_value);
9363 return FALSE;
9366 if (htab->emit_stub_syms)
9368 struct elf_link_hash_entry *h;
9369 h = elf_link_hash_lookup (&htab->elf, "__glink", TRUE, FALSE, FALSE);
9370 if (h == NULL)
9371 return FALSE;
9372 if (h->root.type == bfd_link_hash_new)
9374 h->root.type = bfd_link_hash_defined;
9375 h->root.u.def.section = htab->glink;
9376 h->root.u.def.value = 0;
9377 h->ref_regular = 1;
9378 h->def_regular = 1;
9379 h->ref_regular_nonweak = 1;
9380 h->forced_local = 1;
9381 h->non_elf = 0;
9384 p = htab->glink->contents;
9385 bfd_put_32 (htab->glink->owner, MFCTR_R12, p);
9386 p += 4;
9387 bfd_put_32 (htab->glink->owner, SLDI_R11_R0_3, p);
9388 p += 4;
9389 bfd_put_32 (htab->glink->owner, ADDIC_R2_R0_32K, p);
9390 p += 4;
9391 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
9392 p += 4;
9393 bfd_put_32 (htab->glink->owner, SRADI_R2_R2_63, p);
9394 p += 4;
9395 bfd_put_32 (htab->glink->owner, SLDI_R11_R0_2, p);
9396 p += 4;
9397 bfd_put_32 (htab->glink->owner, AND_R2_R2_R11, p);
9398 p += 4;
9399 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
9400 p += 4;
9401 bfd_put_32 (htab->glink->owner, ADD_R12_R12_R2, p);
9402 p += 4;
9403 bfd_put_32 (htab->glink->owner, ADDIS_R12_R12 | PPC_HA (plt0), p);
9404 p += 4;
9405 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | PPC_LO (plt0), p);
9406 p += 4;
9407 bfd_put_32 (htab->glink->owner, ADDI_R12_R12 | PPC_LO (plt0), p);
9408 p += 4;
9409 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
9410 p += 4;
9411 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
9412 p += 4;
9413 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
9414 p += 4;
9415 bfd_put_32 (htab->glink->owner, BCTR, p);
9416 p += 4;
9418 /* Build the .glink lazy link call stubs. */
9419 indx = 0;
9420 while (p < htab->glink->contents + htab->glink->size)
9422 if (indx < 0x8000)
9424 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9425 p += 4;
9427 else
9429 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9430 p += 4;
9431 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9432 p += 4;
9434 bfd_put_32 (htab->glink->owner,
9435 B_DOT | ((htab->glink->contents - p) & 0x3fffffc), p);
9436 indx++;
9437 p += 4;
9439 htab->glink->rawsize = p - htab->glink->contents;
9442 if (htab->brlt->size != 0)
9444 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
9445 htab->brlt->size);
9446 if (htab->brlt->contents == NULL)
9447 return FALSE;
9449 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
9451 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
9452 htab->relbrlt->size);
9453 if (htab->relbrlt->contents == NULL)
9454 return FALSE;
9457 /* Build the stubs as directed by the stub hash table. */
9458 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
9460 for (stub_sec = htab->stub_bfd->sections;
9461 stub_sec != NULL;
9462 stub_sec = stub_sec->next)
9463 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
9465 stub_sec_count += 1;
9466 if (stub_sec->rawsize != stub_sec->size)
9467 break;
9470 if (stub_sec != NULL
9471 || htab->glink->rawsize != htab->glink->size)
9473 htab->stub_error = TRUE;
9474 (*_bfd_error_handler) (_("stubs don't match calculated size"));
9477 if (htab->stub_error)
9478 return FALSE;
9480 if (stats != NULL)
9482 *stats = bfd_malloc (500);
9483 if (*stats == NULL)
9484 return FALSE;
9486 sprintf (*stats, _("linker stubs in %u group%s\n"
9487 " branch %lu\n"
9488 " toc adjust %lu\n"
9489 " long branch %lu\n"
9490 " long toc adj %lu\n"
9491 " plt call %lu"),
9492 stub_sec_count,
9493 stub_sec_count == 1 ? "" : "s",
9494 htab->stub_count[ppc_stub_long_branch - 1],
9495 htab->stub_count[ppc_stub_long_branch_r2off - 1],
9496 htab->stub_count[ppc_stub_plt_branch - 1],
9497 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
9498 htab->stub_count[ppc_stub_plt_call - 1]);
9500 return TRUE;
9503 /* This function undoes the changes made by add_symbol_adjust. */
9505 static bfd_boolean
9506 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
9508 struct ppc_link_hash_entry *eh;
9510 if (h->root.type == bfd_link_hash_indirect)
9511 return TRUE;
9513 if (h->root.type == bfd_link_hash_warning)
9514 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9516 eh = (struct ppc_link_hash_entry *) h;
9517 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
9518 return TRUE;
9520 eh->elf.root.type = bfd_link_hash_undefined;
9521 return TRUE;
9524 void
9525 ppc64_elf_restore_symbols (struct bfd_link_info *info)
9527 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9528 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
9531 /* What to do when ld finds relocations against symbols defined in
9532 discarded sections. */
9534 static unsigned int
9535 ppc64_elf_action_discarded (asection *sec)
9537 if (strcmp (".opd", sec->name) == 0)
9538 return 0;
9540 if (strcmp (".toc", sec->name) == 0)
9541 return 0;
9543 if (strcmp (".toc1", sec->name) == 0)
9544 return 0;
9546 return _bfd_elf_default_action_discarded (sec);
9549 /* The RELOCATE_SECTION function is called by the ELF backend linker
9550 to handle the relocations for a section.
9552 The relocs are always passed as Rela structures; if the section
9553 actually uses Rel structures, the r_addend field will always be
9554 zero.
9556 This function is responsible for adjust the section contents as
9557 necessary, and (if using Rela relocs and generating a
9558 relocatable output file) adjusting the reloc addend as
9559 necessary.
9561 This function does not have to worry about setting the reloc
9562 address or the reloc symbol index.
9564 LOCAL_SYMS is a pointer to the swapped in local symbols.
9566 LOCAL_SECTIONS is an array giving the section in the input file
9567 corresponding to the st_shndx field of each local symbol.
9569 The global hash table entry for the global symbols can be found
9570 via elf_sym_hashes (input_bfd).
9572 When generating relocatable output, this function must handle
9573 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
9574 going to be the section symbol corresponding to the output
9575 section, which means that the addend must be adjusted
9576 accordingly. */
9578 static bfd_boolean
9579 ppc64_elf_relocate_section (bfd *output_bfd,
9580 struct bfd_link_info *info,
9581 bfd *input_bfd,
9582 asection *input_section,
9583 bfd_byte *contents,
9584 Elf_Internal_Rela *relocs,
9585 Elf_Internal_Sym *local_syms,
9586 asection **local_sections)
9588 struct ppc_link_hash_table *htab;
9589 Elf_Internal_Shdr *symtab_hdr;
9590 struct elf_link_hash_entry **sym_hashes;
9591 Elf_Internal_Rela *rel;
9592 Elf_Internal_Rela *relend;
9593 Elf_Internal_Rela outrel;
9594 bfd_byte *loc;
9595 struct got_entry **local_got_ents;
9596 bfd_vma TOCstart;
9597 bfd_boolean ret = TRUE;
9598 bfd_boolean is_opd;
9599 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
9600 bfd_boolean is_power4 = FALSE;
9602 /* Initialize howto table if needed. */
9603 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9604 ppc_howto_init ();
9606 htab = ppc_hash_table (info);
9608 /* Don't relocate stub sections. */
9609 if (input_section->owner == htab->stub_bfd)
9610 return TRUE;
9612 local_got_ents = elf_local_got_ents (input_bfd);
9613 TOCstart = elf_gp (output_bfd);
9614 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9615 sym_hashes = elf_sym_hashes (input_bfd);
9616 is_opd = ppc64_elf_section_data (input_section)->opd.adjust != NULL;
9618 rel = relocs;
9619 relend = relocs + input_section->reloc_count;
9620 for (; rel < relend; rel++)
9622 enum elf_ppc64_reloc_type r_type;
9623 bfd_vma addend, orig_addend;
9624 bfd_reloc_status_type r;
9625 Elf_Internal_Sym *sym;
9626 asection *sec;
9627 struct elf_link_hash_entry *h_elf;
9628 struct ppc_link_hash_entry *h;
9629 struct ppc_link_hash_entry *fdh;
9630 const char *sym_name;
9631 unsigned long r_symndx, toc_symndx;
9632 char tls_mask, tls_gd, tls_type;
9633 char sym_type;
9634 bfd_vma relocation;
9635 bfd_boolean unresolved_reloc;
9636 bfd_boolean warned;
9637 unsigned long insn, mask;
9638 struct ppc_stub_hash_entry *stub_entry;
9639 bfd_vma max_br_offset;
9640 bfd_vma from;
9642 r_type = ELF64_R_TYPE (rel->r_info);
9643 r_symndx = ELF64_R_SYM (rel->r_info);
9645 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
9646 symbol of the previous ADDR64 reloc. The symbol gives us the
9647 proper TOC base to use. */
9648 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
9649 && rel != relocs
9650 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
9651 && is_opd)
9652 r_symndx = ELF64_R_SYM (rel[-1].r_info);
9654 sym = NULL;
9655 sec = NULL;
9656 h_elf = NULL;
9657 sym_name = NULL;
9658 unresolved_reloc = FALSE;
9659 warned = FALSE;
9660 orig_addend = rel->r_addend;
9662 if (r_symndx < symtab_hdr->sh_info)
9664 /* It's a local symbol. */
9665 long *opd_adjust;
9667 sym = local_syms + r_symndx;
9668 sec = local_sections[r_symndx];
9669 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
9670 sym_type = ELF64_ST_TYPE (sym->st_info);
9671 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9672 opd_adjust = get_opd_info (sec);
9673 if (opd_adjust != NULL)
9675 long adjust = opd_adjust[(sym->st_value + rel->r_addend) / 8];
9676 if (adjust == -1)
9677 relocation = 0;
9678 else
9680 /* If this is a relocation against the opd section sym
9681 and we have edited .opd, adjust the reloc addend so
9682 that ld -r and ld --emit-relocs output is correct.
9683 If it is a reloc against some other .opd symbol,
9684 then the symbol value will be adjusted later. */
9685 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9686 rel->r_addend += adjust;
9687 else
9688 relocation += adjust;
9691 if (info->relocatable)
9692 continue;
9694 else
9696 if (info->relocatable)
9697 continue;
9698 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
9699 r_symndx, symtab_hdr, sym_hashes,
9700 h_elf, sec, relocation,
9701 unresolved_reloc, warned);
9702 sym_name = h_elf->root.root.string;
9703 sym_type = h_elf->type;
9705 h = (struct ppc_link_hash_entry *) h_elf;
9707 /* TLS optimizations. Replace instruction sequences and relocs
9708 based on information we collected in tls_optimize. We edit
9709 RELOCS so that --emit-relocs will output something sensible
9710 for the final instruction stream. */
9711 tls_mask = 0;
9712 tls_gd = 0;
9713 toc_symndx = 0;
9714 if (IS_PPC64_TLS_RELOC (r_type))
9716 if (h != NULL)
9717 tls_mask = h->tls_mask;
9718 else if (local_got_ents != NULL)
9720 char *lgot_masks;
9721 lgot_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
9722 tls_mask = lgot_masks[r_symndx];
9724 if (tls_mask == 0 && r_type == R_PPC64_TLS)
9726 /* Check for toc tls entries. */
9727 char *toc_tls;
9729 if (!get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
9730 rel, input_bfd))
9731 return FALSE;
9733 if (toc_tls)
9734 tls_mask = *toc_tls;
9738 /* Check that tls relocs are used with tls syms, and non-tls
9739 relocs are used with non-tls syms. */
9740 if (r_symndx != 0
9741 && r_type != R_PPC64_NONE
9742 && (h == NULL
9743 || h->elf.root.type == bfd_link_hash_defined
9744 || h->elf.root.type == bfd_link_hash_defweak)
9745 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
9747 if (r_type == R_PPC64_TLS && tls_mask != 0)
9748 /* R_PPC64_TLS is OK against a symbol in the TOC. */
9750 else
9751 (*_bfd_error_handler)
9752 (sym_type == STT_TLS
9753 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
9754 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s"),
9755 input_bfd,
9756 input_section,
9757 (long) rel->r_offset,
9758 ppc64_elf_howto_table[r_type]->name,
9759 sym_name);
9762 /* Ensure reloc mapping code below stays sane. */
9763 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
9764 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
9765 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
9766 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
9767 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
9768 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
9769 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
9770 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
9771 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
9772 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
9773 abort ();
9775 switch (r_type)
9777 default:
9778 break;
9780 case R_PPC64_TOC16:
9781 case R_PPC64_TOC16_LO:
9782 case R_PPC64_TOC16_DS:
9783 case R_PPC64_TOC16_LO_DS:
9785 /* Check for toc tls entries. */
9786 char *toc_tls;
9787 int retval;
9789 retval = get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
9790 rel, input_bfd);
9791 if (retval == 0)
9792 return FALSE;
9794 if (toc_tls)
9796 tls_mask = *toc_tls;
9797 if (r_type == R_PPC64_TOC16_DS
9798 || r_type == R_PPC64_TOC16_LO_DS)
9800 if (tls_mask != 0
9801 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
9802 goto toctprel;
9804 else
9806 /* If we found a GD reloc pair, then we might be
9807 doing a GD->IE transition. */
9808 if (retval == 2)
9810 tls_gd = TLS_TPRELGD;
9811 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
9812 goto tls_get_addr_check;
9814 else if (retval == 3)
9816 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
9817 goto tls_get_addr_check;
9822 break;
9824 case R_PPC64_GOT_TPREL16_DS:
9825 case R_PPC64_GOT_TPREL16_LO_DS:
9826 if (tls_mask != 0
9827 && (tls_mask & TLS_TPREL) == 0)
9829 toctprel:
9830 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
9831 insn &= 31 << 21;
9832 insn |= 0x3c0d0000; /* addis 0,13,0 */
9833 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
9834 r_type = R_PPC64_TPREL16_HA;
9835 if (toc_symndx != 0)
9837 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
9838 /* We changed the symbol. Start over in order to
9839 get h, sym, sec etc. right. */
9840 rel--;
9841 continue;
9843 else
9844 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
9846 break;
9848 case R_PPC64_TLS:
9849 if (tls_mask != 0
9850 && (tls_mask & TLS_TPREL) == 0)
9852 bfd_vma rtra;
9853 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
9854 if ((insn & ((0x3f << 26) | (31 << 11)))
9855 == ((31 << 26) | (13 << 11)))
9856 rtra = insn & ((1 << 26) - (1 << 16));
9857 else if ((insn & ((0x3f << 26) | (31 << 16)))
9858 == ((31 << 26) | (13 << 16)))
9859 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
9860 else
9861 abort ();
9862 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
9863 /* add -> addi. */
9864 insn = 14 << 26;
9865 else if ((insn & (31 << 1)) == 23 << 1
9866 && ((insn & (31 << 6)) < 14 << 6
9867 || ((insn & (31 << 6)) >= 16 << 6
9868 && (insn & (31 << 6)) < 24 << 6)))
9869 /* load and store indexed -> dform. */
9870 insn = (32 | ((insn >> 6) & 31)) << 26;
9871 else if ((insn & (31 << 1)) == 21 << 1
9872 && (insn & (0x1a << 6)) == 0)
9873 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
9874 insn = (((58 | ((insn >> 6) & 4)) << 26)
9875 | ((insn >> 6) & 1));
9876 else if ((insn & (31 << 1)) == 21 << 1
9877 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
9878 /* lwax -> lwa. */
9879 insn = (58 << 26) | 2;
9880 else
9881 abort ();
9882 insn |= rtra;
9883 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
9884 /* Was PPC64_TLS which sits on insn boundary, now
9885 PPC64_TPREL16_LO which is at insn+2. */
9886 rel->r_offset += 2;
9887 r_type = R_PPC64_TPREL16_LO;
9888 if (toc_symndx != 0)
9890 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
9891 /* We changed the symbol. Start over in order to
9892 get h, sym, sec etc. right. */
9893 rel--;
9894 continue;
9896 else
9897 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
9899 break;
9901 case R_PPC64_GOT_TLSGD16_HI:
9902 case R_PPC64_GOT_TLSGD16_HA:
9903 tls_gd = TLS_TPRELGD;
9904 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
9905 goto tls_gdld_hi;
9906 break;
9908 case R_PPC64_GOT_TLSLD16_HI:
9909 case R_PPC64_GOT_TLSLD16_HA:
9910 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
9912 tls_gdld_hi:
9913 if ((tls_mask & tls_gd) != 0)
9914 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
9915 + R_PPC64_GOT_TPREL16_DS);
9916 else
9918 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
9919 rel->r_offset -= 2;
9920 r_type = R_PPC64_NONE;
9922 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
9924 break;
9926 case R_PPC64_GOT_TLSGD16:
9927 case R_PPC64_GOT_TLSGD16_LO:
9928 tls_gd = TLS_TPRELGD;
9929 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
9930 goto tls_get_addr_check;
9931 break;
9933 case R_PPC64_GOT_TLSLD16:
9934 case R_PPC64_GOT_TLSLD16_LO:
9935 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
9937 tls_get_addr_check:
9938 if (rel + 1 < relend)
9940 enum elf_ppc64_reloc_type r_type2;
9941 unsigned long r_symndx2;
9942 struct elf_link_hash_entry *h2;
9943 bfd_vma insn1, insn2, insn3;
9944 bfd_vma offset;
9946 /* The next instruction should be a call to
9947 __tls_get_addr. Peek at the reloc to be sure. */
9948 r_type2 = ELF64_R_TYPE (rel[1].r_info);
9949 r_symndx2 = ELF64_R_SYM (rel[1].r_info);
9950 if (r_symndx2 < symtab_hdr->sh_info
9951 || (r_type2 != R_PPC64_REL14
9952 && r_type2 != R_PPC64_REL14_BRTAKEN
9953 && r_type2 != R_PPC64_REL14_BRNTAKEN
9954 && r_type2 != R_PPC64_REL24))
9955 break;
9957 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
9958 while (h2->root.type == bfd_link_hash_indirect
9959 || h2->root.type == bfd_link_hash_warning)
9960 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
9961 if (h2 == NULL || (h2 != &htab->tls_get_addr->elf
9962 && h2 != &htab->tls_get_addr_fd->elf))
9963 break;
9965 /* OK, it checks out. Replace the call. */
9966 offset = rel[1].r_offset;
9967 insn1 = bfd_get_32 (output_bfd,
9968 contents + rel->r_offset - 2);
9969 insn3 = bfd_get_32 (output_bfd,
9970 contents + offset + 4);
9971 if ((tls_mask & tls_gd) != 0)
9973 /* IE */
9974 insn1 &= (1 << 26) - (1 << 2);
9975 insn1 |= 58 << 26; /* ld */
9976 insn2 = 0x7c636a14; /* add 3,3,13 */
9977 rel[1].r_info = ELF64_R_INFO (r_symndx2, R_PPC64_NONE);
9978 if ((tls_mask & TLS_EXPLICIT) == 0)
9979 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
9980 + R_PPC64_GOT_TPREL16_DS);
9981 else
9982 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
9983 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
9985 else
9987 /* LE */
9988 insn1 = 0x3c6d0000; /* addis 3,13,0 */
9989 insn2 = 0x38630000; /* addi 3,3,0 */
9990 if (tls_gd == 0)
9992 /* Was an LD reloc. */
9993 r_symndx = 0;
9994 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
9995 rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
9997 else if (toc_symndx != 0)
9998 r_symndx = toc_symndx;
9999 r_type = R_PPC64_TPREL16_HA;
10000 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10001 rel[1].r_info = ELF64_R_INFO (r_symndx,
10002 R_PPC64_TPREL16_LO);
10003 rel[1].r_offset += 2;
10005 if (insn3 == NOP
10006 || insn3 == CROR_151515 || insn3 == CROR_313131)
10008 insn3 = insn2;
10009 insn2 = NOP;
10010 rel[1].r_offset += 4;
10012 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
10013 bfd_put_32 (output_bfd, insn2, contents + offset);
10014 bfd_put_32 (output_bfd, insn3, contents + offset + 4);
10015 if (tls_gd == 0 || toc_symndx != 0)
10017 /* We changed the symbol. Start over in order
10018 to get h, sym, sec etc. right. */
10019 rel--;
10020 continue;
10024 break;
10026 case R_PPC64_DTPMOD64:
10027 if (rel + 1 < relend
10028 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
10029 && rel[1].r_offset == rel->r_offset + 8)
10031 if ((tls_mask & TLS_GD) == 0)
10033 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
10034 if ((tls_mask & TLS_TPRELGD) != 0)
10035 r_type = R_PPC64_TPREL64;
10036 else
10038 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
10039 r_type = R_PPC64_NONE;
10041 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10044 else
10046 if ((tls_mask & TLS_LD) == 0)
10048 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
10049 r_type = R_PPC64_NONE;
10050 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10053 break;
10055 case R_PPC64_TPREL64:
10056 if ((tls_mask & TLS_TPREL) == 0)
10058 r_type = R_PPC64_NONE;
10059 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10061 break;
10064 /* Handle other relocations that tweak non-addend part of insn. */
10065 insn = 0;
10066 max_br_offset = 1 << 25;
10067 addend = rel->r_addend;
10068 switch (r_type)
10070 default:
10071 break;
10073 /* Branch taken prediction relocations. */
10074 case R_PPC64_ADDR14_BRTAKEN:
10075 case R_PPC64_REL14_BRTAKEN:
10076 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
10077 /* Fall thru. */
10079 /* Branch not taken prediction relocations. */
10080 case R_PPC64_ADDR14_BRNTAKEN:
10081 case R_PPC64_REL14_BRNTAKEN:
10082 insn |= bfd_get_32 (output_bfd,
10083 contents + rel->r_offset) & ~(0x01 << 21);
10084 /* Fall thru. */
10086 case R_PPC64_REL14:
10087 max_br_offset = 1 << 15;
10088 /* Fall thru. */
10090 case R_PPC64_REL24:
10091 /* Calls to functions with a different TOC, such as calls to
10092 shared objects, need to alter the TOC pointer. This is
10093 done using a linkage stub. A REL24 branching to these
10094 linkage stubs needs to be followed by a nop, as the nop
10095 will be replaced with an instruction to restore the TOC
10096 base pointer. */
10097 stub_entry = NULL;
10098 fdh = h;
10099 if (((h != NULL
10100 && (((fdh = h->oh) != NULL
10101 && fdh->elf.plt.plist != NULL)
10102 || (fdh = h)->elf.plt.plist != NULL))
10103 || (sec != NULL
10104 && sec->output_section != NULL
10105 && sec->id <= htab->top_id
10106 && (htab->stub_group[sec->id].toc_off
10107 != htab->stub_group[input_section->id].toc_off)))
10108 && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
10109 rel, htab)) != NULL
10110 && (stub_entry->stub_type == ppc_stub_plt_call
10111 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
10112 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
10114 bfd_boolean can_plt_call = FALSE;
10116 if (rel->r_offset + 8 <= input_section->size)
10118 unsigned long nop;
10119 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
10120 if (nop == NOP
10121 || nop == CROR_151515 || nop == CROR_313131)
10123 bfd_put_32 (input_bfd, LD_R2_40R1,
10124 contents + rel->r_offset + 4);
10125 can_plt_call = TRUE;
10129 if (!can_plt_call)
10131 if (stub_entry->stub_type == ppc_stub_plt_call)
10133 /* If this is a plain branch rather than a branch
10134 and link, don't require a nop. However, don't
10135 allow tail calls in a shared library as they
10136 will result in r2 being corrupted. */
10137 unsigned long br;
10138 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
10139 if (info->executable && (br & 1) == 0)
10140 can_plt_call = TRUE;
10141 else
10142 stub_entry = NULL;
10144 else if (h != NULL
10145 && strcmp (h->elf.root.root.string,
10146 ".__libc_start_main") == 0)
10148 /* Allow crt1 branch to go via a toc adjusting stub. */
10149 can_plt_call = TRUE;
10151 else
10153 if (strcmp (input_section->output_section->name,
10154 ".init") == 0
10155 || strcmp (input_section->output_section->name,
10156 ".fini") == 0)
10157 (*_bfd_error_handler)
10158 (_("%B(%A+0x%lx): automatic multiple TOCs "
10159 "not supported using your crt files; "
10160 "recompile with -mminimal-toc or upgrade gcc"),
10161 input_bfd,
10162 input_section,
10163 (long) rel->r_offset);
10164 else
10165 (*_bfd_error_handler)
10166 (_("%B(%A+0x%lx): sibling call optimization to `%s' "
10167 "does not allow automatic multiple TOCs; "
10168 "recompile with -mminimal-toc or "
10169 "-fno-optimize-sibling-calls, "
10170 "or make `%s' extern"),
10171 input_bfd,
10172 input_section,
10173 (long) rel->r_offset,
10174 sym_name,
10175 sym_name);
10176 bfd_set_error (bfd_error_bad_value);
10177 ret = FALSE;
10181 if (can_plt_call
10182 && stub_entry->stub_type == ppc_stub_plt_call)
10183 unresolved_reloc = FALSE;
10186 if (stub_entry == NULL
10187 && get_opd_info (sec) != NULL)
10189 /* The branch destination is the value of the opd entry. */
10190 bfd_vma off = (relocation + addend
10191 - sec->output_section->vma
10192 - sec->output_offset);
10193 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
10194 if (dest != (bfd_vma) -1)
10196 relocation = dest;
10197 addend = 0;
10201 /* If the branch is out of reach we ought to have a long
10202 branch stub. */
10203 from = (rel->r_offset
10204 + input_section->output_offset
10205 + input_section->output_section->vma);
10207 if (stub_entry == NULL
10208 && (relocation + addend - from + max_br_offset
10209 >= 2 * max_br_offset)
10210 && r_type != R_PPC64_ADDR14_BRTAKEN
10211 && r_type != R_PPC64_ADDR14_BRNTAKEN)
10212 stub_entry = ppc_get_stub_entry (input_section, sec, h, rel,
10213 htab);
10215 if (stub_entry != NULL)
10217 /* Munge up the value and addend so that we call the stub
10218 rather than the procedure directly. */
10219 relocation = (stub_entry->stub_offset
10220 + stub_entry->stub_sec->output_offset
10221 + stub_entry->stub_sec->output_section->vma);
10222 addend = 0;
10225 if (insn != 0)
10227 if (is_power4)
10229 /* Set 'a' bit. This is 0b00010 in BO field for branch
10230 on CR(BI) insns (BO == 001at or 011at), and 0b01000
10231 for branch on CTR insns (BO == 1a00t or 1a01t). */
10232 if ((insn & (0x14 << 21)) == (0x04 << 21))
10233 insn |= 0x02 << 21;
10234 else if ((insn & (0x14 << 21)) == (0x10 << 21))
10235 insn |= 0x08 << 21;
10236 else
10237 break;
10239 else
10241 /* Invert 'y' bit if not the default. */
10242 if ((bfd_signed_vma) (relocation + addend - from) < 0)
10243 insn ^= 0x01 << 21;
10246 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
10249 /* NOP out calls to undefined weak functions.
10250 We can thus call a weak function without first
10251 checking whether the function is defined. */
10252 else if (h != NULL
10253 && h->elf.root.type == bfd_link_hash_undefweak
10254 && r_type == R_PPC64_REL24
10255 && relocation == 0
10256 && addend == 0)
10258 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
10259 continue;
10261 break;
10264 /* Set `addend'. */
10265 tls_type = 0;
10266 switch (r_type)
10268 default:
10269 (*_bfd_error_handler)
10270 (_("%B: unknown relocation type %d for symbol %s"),
10271 input_bfd, (int) r_type, sym_name);
10273 bfd_set_error (bfd_error_bad_value);
10274 ret = FALSE;
10275 continue;
10277 case R_PPC64_NONE:
10278 case R_PPC64_TLS:
10279 case R_PPC64_GNU_VTINHERIT:
10280 case R_PPC64_GNU_VTENTRY:
10281 continue;
10283 /* GOT16 relocations. Like an ADDR16 using the symbol's
10284 address in the GOT as relocation value instead of the
10285 symbol's value itself. Also, create a GOT entry for the
10286 symbol and put the symbol value there. */
10287 case R_PPC64_GOT_TLSGD16:
10288 case R_PPC64_GOT_TLSGD16_LO:
10289 case R_PPC64_GOT_TLSGD16_HI:
10290 case R_PPC64_GOT_TLSGD16_HA:
10291 tls_type = TLS_TLS | TLS_GD;
10292 goto dogot;
10294 case R_PPC64_GOT_TLSLD16:
10295 case R_PPC64_GOT_TLSLD16_LO:
10296 case R_PPC64_GOT_TLSLD16_HI:
10297 case R_PPC64_GOT_TLSLD16_HA:
10298 tls_type = TLS_TLS | TLS_LD;
10299 goto dogot;
10301 case R_PPC64_GOT_TPREL16_DS:
10302 case R_PPC64_GOT_TPREL16_LO_DS:
10303 case R_PPC64_GOT_TPREL16_HI:
10304 case R_PPC64_GOT_TPREL16_HA:
10305 tls_type = TLS_TLS | TLS_TPREL;
10306 goto dogot;
10308 case R_PPC64_GOT_DTPREL16_DS:
10309 case R_PPC64_GOT_DTPREL16_LO_DS:
10310 case R_PPC64_GOT_DTPREL16_HI:
10311 case R_PPC64_GOT_DTPREL16_HA:
10312 tls_type = TLS_TLS | TLS_DTPREL;
10313 goto dogot;
10315 case R_PPC64_GOT16:
10316 case R_PPC64_GOT16_LO:
10317 case R_PPC64_GOT16_HI:
10318 case R_PPC64_GOT16_HA:
10319 case R_PPC64_GOT16_DS:
10320 case R_PPC64_GOT16_LO_DS:
10321 dogot:
10323 /* Relocation is to the entry for this symbol in the global
10324 offset table. */
10325 asection *got;
10326 bfd_vma *offp;
10327 bfd_vma off;
10328 unsigned long indx = 0;
10330 if (tls_type == (TLS_TLS | TLS_LD)
10331 && (h == NULL
10332 || !h->elf.def_dynamic))
10333 offp = &ppc64_tlsld_got (input_bfd)->offset;
10334 else
10336 struct got_entry *ent;
10338 if (h != NULL)
10340 bfd_boolean dyn = htab->elf.dynamic_sections_created;
10341 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
10342 &h->elf)
10343 || (info->shared
10344 && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
10345 /* This is actually a static link, or it is a
10346 -Bsymbolic link and the symbol is defined
10347 locally, or the symbol was forced to be local
10348 because of a version file. */
10350 else
10352 indx = h->elf.dynindx;
10353 unresolved_reloc = FALSE;
10355 ent = h->elf.got.glist;
10357 else
10359 if (local_got_ents == NULL)
10360 abort ();
10361 ent = local_got_ents[r_symndx];
10364 for (; ent != NULL; ent = ent->next)
10365 if (ent->addend == orig_addend
10366 && ent->owner == input_bfd
10367 && ent->tls_type == tls_type)
10368 break;
10369 if (ent == NULL)
10370 abort ();
10371 offp = &ent->got.offset;
10374 got = ppc64_elf_tdata (input_bfd)->got;
10375 if (got == NULL)
10376 abort ();
10378 /* The offset must always be a multiple of 8. We use the
10379 least significant bit to record whether we have already
10380 processed this entry. */
10381 off = *offp;
10382 if ((off & 1) != 0)
10383 off &= ~1;
10384 else
10386 /* Generate relocs for the dynamic linker, except in
10387 the case of TLSLD where we'll use one entry per
10388 module. */
10389 asection *relgot = ppc64_elf_tdata (input_bfd)->relgot;
10391 *offp = off | 1;
10392 if ((info->shared || indx != 0)
10393 && (h == NULL
10394 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10395 || h->elf.root.type != bfd_link_hash_undefweak))
10397 outrel.r_offset = (got->output_section->vma
10398 + got->output_offset
10399 + off);
10400 outrel.r_addend = addend;
10401 if (tls_type & (TLS_LD | TLS_GD))
10403 outrel.r_addend = 0;
10404 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
10405 if (tls_type == (TLS_TLS | TLS_GD))
10407 loc = relgot->contents;
10408 loc += (relgot->reloc_count++
10409 * sizeof (Elf64_External_Rela));
10410 bfd_elf64_swap_reloca_out (output_bfd,
10411 &outrel, loc);
10412 outrel.r_offset += 8;
10413 outrel.r_addend = addend;
10414 outrel.r_info
10415 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
10418 else if (tls_type == (TLS_TLS | TLS_DTPREL))
10419 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
10420 else if (tls_type == (TLS_TLS | TLS_TPREL))
10421 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
10422 else if (indx == 0)
10424 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_RELATIVE);
10426 /* Write the .got section contents for the sake
10427 of prelink. */
10428 loc = got->contents + off;
10429 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
10430 loc);
10432 else
10433 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
10435 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
10437 outrel.r_addend += relocation;
10438 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
10439 outrel.r_addend -= htab->elf.tls_sec->vma;
10441 loc = relgot->contents;
10442 loc += (relgot->reloc_count++
10443 * sizeof (Elf64_External_Rela));
10444 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10447 /* Init the .got section contents here if we're not
10448 emitting a reloc. */
10449 else
10451 relocation += addend;
10452 if (tls_type == (TLS_TLS | TLS_LD))
10453 relocation = 1;
10454 else if (tls_type != 0)
10456 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
10457 if (tls_type == (TLS_TLS | TLS_TPREL))
10458 relocation += DTP_OFFSET - TP_OFFSET;
10460 if (tls_type == (TLS_TLS | TLS_GD))
10462 bfd_put_64 (output_bfd, relocation,
10463 got->contents + off + 8);
10464 relocation = 1;
10468 bfd_put_64 (output_bfd, relocation,
10469 got->contents + off);
10473 if (off >= (bfd_vma) -2)
10474 abort ();
10476 relocation = got->output_offset + off;
10478 /* TOC base (r2) is TOC start plus 0x8000. */
10479 addend = -TOC_BASE_OFF;
10481 break;
10483 case R_PPC64_PLT16_HA:
10484 case R_PPC64_PLT16_HI:
10485 case R_PPC64_PLT16_LO:
10486 case R_PPC64_PLT32:
10487 case R_PPC64_PLT64:
10488 /* Relocation is to the entry for this symbol in the
10489 procedure linkage table. */
10491 /* Resolve a PLT reloc against a local symbol directly,
10492 without using the procedure linkage table. */
10493 if (h == NULL)
10494 break;
10496 /* It's possible that we didn't make a PLT entry for this
10497 symbol. This happens when statically linking PIC code,
10498 or when using -Bsymbolic. Go find a match if there is a
10499 PLT entry. */
10500 if (htab->plt != NULL)
10502 struct plt_entry *ent;
10503 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
10504 if (ent->addend == orig_addend
10505 && ent->plt.offset != (bfd_vma) -1)
10507 relocation = (htab->plt->output_section->vma
10508 + htab->plt->output_offset
10509 + ent->plt.offset);
10510 unresolved_reloc = FALSE;
10513 break;
10515 case R_PPC64_TOC:
10516 /* Relocation value is TOC base. */
10517 relocation = TOCstart;
10518 if (r_symndx == 0)
10519 relocation += htab->stub_group[input_section->id].toc_off;
10520 else if (unresolved_reloc)
10522 else if (sec != NULL && sec->id <= htab->top_id)
10523 relocation += htab->stub_group[sec->id].toc_off;
10524 else
10525 unresolved_reloc = TRUE;
10526 goto dodyn2;
10528 /* TOC16 relocs. We want the offset relative to the TOC base,
10529 which is the address of the start of the TOC plus 0x8000.
10530 The TOC consists of sections .got, .toc, .tocbss, and .plt,
10531 in this order. */
10532 case R_PPC64_TOC16:
10533 case R_PPC64_TOC16_LO:
10534 case R_PPC64_TOC16_HI:
10535 case R_PPC64_TOC16_DS:
10536 case R_PPC64_TOC16_LO_DS:
10537 case R_PPC64_TOC16_HA:
10538 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
10539 break;
10541 /* Relocate against the beginning of the section. */
10542 case R_PPC64_SECTOFF:
10543 case R_PPC64_SECTOFF_LO:
10544 case R_PPC64_SECTOFF_HI:
10545 case R_PPC64_SECTOFF_DS:
10546 case R_PPC64_SECTOFF_LO_DS:
10547 case R_PPC64_SECTOFF_HA:
10548 if (sec != NULL)
10549 addend -= sec->output_section->vma;
10550 break;
10552 case R_PPC64_REL14:
10553 case R_PPC64_REL14_BRNTAKEN:
10554 case R_PPC64_REL14_BRTAKEN:
10555 case R_PPC64_REL24:
10556 break;
10558 case R_PPC64_TPREL16:
10559 case R_PPC64_TPREL16_LO:
10560 case R_PPC64_TPREL16_HI:
10561 case R_PPC64_TPREL16_HA:
10562 case R_PPC64_TPREL16_DS:
10563 case R_PPC64_TPREL16_LO_DS:
10564 case R_PPC64_TPREL16_HIGHER:
10565 case R_PPC64_TPREL16_HIGHERA:
10566 case R_PPC64_TPREL16_HIGHEST:
10567 case R_PPC64_TPREL16_HIGHESTA:
10568 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
10569 if (info->shared)
10570 /* The TPREL16 relocs shouldn't really be used in shared
10571 libs as they will result in DT_TEXTREL being set, but
10572 support them anyway. */
10573 goto dodyn;
10574 break;
10576 case R_PPC64_DTPREL16:
10577 case R_PPC64_DTPREL16_LO:
10578 case R_PPC64_DTPREL16_HI:
10579 case R_PPC64_DTPREL16_HA:
10580 case R_PPC64_DTPREL16_DS:
10581 case R_PPC64_DTPREL16_LO_DS:
10582 case R_PPC64_DTPREL16_HIGHER:
10583 case R_PPC64_DTPREL16_HIGHERA:
10584 case R_PPC64_DTPREL16_HIGHEST:
10585 case R_PPC64_DTPREL16_HIGHESTA:
10586 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
10587 break;
10589 case R_PPC64_DTPMOD64:
10590 relocation = 1;
10591 addend = 0;
10592 goto dodyn;
10594 case R_PPC64_TPREL64:
10595 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
10596 goto dodyn;
10598 case R_PPC64_DTPREL64:
10599 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
10600 /* Fall thru */
10602 /* Relocations that may need to be propagated if this is a
10603 dynamic object. */
10604 case R_PPC64_REL30:
10605 case R_PPC64_REL32:
10606 case R_PPC64_REL64:
10607 case R_PPC64_ADDR14:
10608 case R_PPC64_ADDR14_BRNTAKEN:
10609 case R_PPC64_ADDR14_BRTAKEN:
10610 case R_PPC64_ADDR16:
10611 case R_PPC64_ADDR16_DS:
10612 case R_PPC64_ADDR16_HA:
10613 case R_PPC64_ADDR16_HI:
10614 case R_PPC64_ADDR16_HIGHER:
10615 case R_PPC64_ADDR16_HIGHERA:
10616 case R_PPC64_ADDR16_HIGHEST:
10617 case R_PPC64_ADDR16_HIGHESTA:
10618 case R_PPC64_ADDR16_LO:
10619 case R_PPC64_ADDR16_LO_DS:
10620 case R_PPC64_ADDR24:
10621 case R_PPC64_ADDR32:
10622 case R_PPC64_ADDR64:
10623 case R_PPC64_UADDR16:
10624 case R_PPC64_UADDR32:
10625 case R_PPC64_UADDR64:
10626 /* r_symndx will be zero only for relocs against symbols
10627 from removed linkonce sections, or sections discarded by
10628 a linker script. */
10629 dodyn:
10630 if (r_symndx == 0)
10631 break;
10632 /* Fall thru. */
10634 dodyn2:
10635 if ((input_section->flags & SEC_ALLOC) == 0)
10636 break;
10638 if (NO_OPD_RELOCS && is_opd)
10639 break;
10641 if ((info->shared
10642 && (h == NULL
10643 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10644 || h->elf.root.type != bfd_link_hash_undefweak)
10645 && (MUST_BE_DYN_RELOC (r_type)
10646 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
10647 || (ELIMINATE_COPY_RELOCS
10648 && !info->shared
10649 && h != NULL
10650 && h->elf.dynindx != -1
10651 && !h->elf.non_got_ref
10652 && h->elf.def_dynamic
10653 && !h->elf.def_regular))
10655 Elf_Internal_Rela outrel;
10656 bfd_boolean skip, relocate;
10657 asection *sreloc;
10658 bfd_byte *loc;
10659 bfd_vma out_off;
10661 /* When generating a dynamic object, these relocations
10662 are copied into the output file to be resolved at run
10663 time. */
10665 skip = FALSE;
10666 relocate = FALSE;
10668 out_off = _bfd_elf_section_offset (output_bfd, info,
10669 input_section, rel->r_offset);
10670 if (out_off == (bfd_vma) -1)
10671 skip = TRUE;
10672 else if (out_off == (bfd_vma) -2)
10673 skip = TRUE, relocate = TRUE;
10674 out_off += (input_section->output_section->vma
10675 + input_section->output_offset);
10676 outrel.r_offset = out_off;
10677 outrel.r_addend = rel->r_addend;
10679 /* Optimize unaligned reloc use. */
10680 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
10681 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
10682 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
10683 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
10684 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
10685 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
10686 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
10687 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
10688 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
10690 if (skip)
10691 memset (&outrel, 0, sizeof outrel);
10692 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
10693 && !is_opd
10694 && r_type != R_PPC64_TOC)
10695 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
10696 else
10698 /* This symbol is local, or marked to become local,
10699 or this is an opd section reloc which must point
10700 at a local function. */
10701 outrel.r_addend += relocation;
10702 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
10704 if (is_opd && h != NULL)
10706 /* Lie about opd entries. This case occurs
10707 when building shared libraries and we
10708 reference a function in another shared
10709 lib. The same thing happens for a weak
10710 definition in an application that's
10711 overridden by a strong definition in a
10712 shared lib. (I believe this is a generic
10713 bug in binutils handling of weak syms.)
10714 In these cases we won't use the opd
10715 entry in this lib. */
10716 unresolved_reloc = FALSE;
10718 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10720 /* We need to relocate .opd contents for ld.so.
10721 Prelink also wants simple and consistent rules
10722 for relocs. This make all RELATIVE relocs have
10723 *r_offset equal to r_addend. */
10724 relocate = TRUE;
10726 else
10728 long indx = 0;
10730 if (bfd_is_abs_section (sec))
10732 else if (sec == NULL || sec->owner == NULL)
10734 bfd_set_error (bfd_error_bad_value);
10735 return FALSE;
10737 else
10739 asection *osec;
10741 osec = sec->output_section;
10742 indx = elf_section_data (osec)->dynindx;
10744 /* We are turning this relocation into one
10745 against a section symbol, so subtract out
10746 the output section's address but not the
10747 offset of the input section in the output
10748 section. */
10749 outrel.r_addend -= osec->vma;
10752 outrel.r_info = ELF64_R_INFO (indx, r_type);
10756 sreloc = elf_section_data (input_section)->sreloc;
10757 if (sreloc == NULL)
10758 abort ();
10760 loc = sreloc->contents;
10761 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
10762 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10764 /* If this reloc is against an external symbol, it will
10765 be computed at runtime, so there's no need to do
10766 anything now. However, for the sake of prelink ensure
10767 that the section contents are a known value. */
10768 if (! relocate)
10770 unresolved_reloc = FALSE;
10771 /* The value chosen here is quite arbitrary as ld.so
10772 ignores section contents except for the special
10773 case of .opd where the contents might be accessed
10774 before relocation. Choose zero, as that won't
10775 cause reloc overflow. */
10776 relocation = 0;
10777 addend = 0;
10778 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
10779 to improve backward compatibility with older
10780 versions of ld. */
10781 if (r_type == R_PPC64_ADDR64)
10782 addend = outrel.r_addend;
10783 /* Adjust pc_relative relocs to have zero in *r_offset. */
10784 else if (ppc64_elf_howto_table[r_type]->pc_relative)
10785 addend = (input_section->output_section->vma
10786 + input_section->output_offset
10787 + rel->r_offset);
10790 break;
10792 case R_PPC64_COPY:
10793 case R_PPC64_GLOB_DAT:
10794 case R_PPC64_JMP_SLOT:
10795 case R_PPC64_RELATIVE:
10796 /* We shouldn't ever see these dynamic relocs in relocatable
10797 files. */
10798 /* Fall through. */
10800 case R_PPC64_PLTGOT16:
10801 case R_PPC64_PLTGOT16_DS:
10802 case R_PPC64_PLTGOT16_HA:
10803 case R_PPC64_PLTGOT16_HI:
10804 case R_PPC64_PLTGOT16_LO:
10805 case R_PPC64_PLTGOT16_LO_DS:
10806 case R_PPC64_PLTREL32:
10807 case R_PPC64_PLTREL64:
10808 /* These ones haven't been implemented yet. */
10810 (*_bfd_error_handler)
10811 (_("%B: relocation %s is not supported for symbol %s."),
10812 input_bfd,
10813 ppc64_elf_howto_table[r_type]->name, sym_name);
10815 bfd_set_error (bfd_error_invalid_operation);
10816 ret = FALSE;
10817 continue;
10820 /* Do any further special processing. */
10821 switch (r_type)
10823 default:
10824 break;
10826 case R_PPC64_ADDR16_HA:
10827 case R_PPC64_ADDR16_HIGHERA:
10828 case R_PPC64_ADDR16_HIGHESTA:
10829 case R_PPC64_GOT16_HA:
10830 case R_PPC64_PLTGOT16_HA:
10831 case R_PPC64_PLT16_HA:
10832 case R_PPC64_TOC16_HA:
10833 case R_PPC64_SECTOFF_HA:
10834 case R_PPC64_TPREL16_HA:
10835 case R_PPC64_DTPREL16_HA:
10836 case R_PPC64_GOT_TLSGD16_HA:
10837 case R_PPC64_GOT_TLSLD16_HA:
10838 case R_PPC64_GOT_TPREL16_HA:
10839 case R_PPC64_GOT_DTPREL16_HA:
10840 case R_PPC64_TPREL16_HIGHER:
10841 case R_PPC64_TPREL16_HIGHERA:
10842 case R_PPC64_TPREL16_HIGHEST:
10843 case R_PPC64_TPREL16_HIGHESTA:
10844 case R_PPC64_DTPREL16_HIGHER:
10845 case R_PPC64_DTPREL16_HIGHERA:
10846 case R_PPC64_DTPREL16_HIGHEST:
10847 case R_PPC64_DTPREL16_HIGHESTA:
10848 /* It's just possible that this symbol is a weak symbol
10849 that's not actually defined anywhere. In that case,
10850 'sec' would be NULL, and we should leave the symbol
10851 alone (it will be set to zero elsewhere in the link). */
10852 if (sec != NULL)
10853 /* Add 0x10000 if sign bit in 0:15 is set.
10854 Bits 0:15 are not used. */
10855 addend += 0x8000;
10856 break;
10858 case R_PPC64_ADDR16_DS:
10859 case R_PPC64_ADDR16_LO_DS:
10860 case R_PPC64_GOT16_DS:
10861 case R_PPC64_GOT16_LO_DS:
10862 case R_PPC64_PLT16_LO_DS:
10863 case R_PPC64_SECTOFF_DS:
10864 case R_PPC64_SECTOFF_LO_DS:
10865 case R_PPC64_TOC16_DS:
10866 case R_PPC64_TOC16_LO_DS:
10867 case R_PPC64_PLTGOT16_DS:
10868 case R_PPC64_PLTGOT16_LO_DS:
10869 case R_PPC64_GOT_TPREL16_DS:
10870 case R_PPC64_GOT_TPREL16_LO_DS:
10871 case R_PPC64_GOT_DTPREL16_DS:
10872 case R_PPC64_GOT_DTPREL16_LO_DS:
10873 case R_PPC64_TPREL16_DS:
10874 case R_PPC64_TPREL16_LO_DS:
10875 case R_PPC64_DTPREL16_DS:
10876 case R_PPC64_DTPREL16_LO_DS:
10877 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
10878 mask = 3;
10879 /* If this reloc is against an lq insn, then the value must be
10880 a multiple of 16. This is somewhat of a hack, but the
10881 "correct" way to do this by defining _DQ forms of all the
10882 _DS relocs bloats all reloc switches in this file. It
10883 doesn't seem to make much sense to use any of these relocs
10884 in data, so testing the insn should be safe. */
10885 if ((insn & (0x3f << 26)) == (56u << 26))
10886 mask = 15;
10887 if (((relocation + addend) & mask) != 0)
10889 (*_bfd_error_handler)
10890 (_("%B: error: relocation %s not a multiple of %d"),
10891 input_bfd,
10892 ppc64_elf_howto_table[r_type]->name,
10893 mask + 1);
10894 bfd_set_error (bfd_error_bad_value);
10895 ret = FALSE;
10896 continue;
10898 break;
10901 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
10902 because such sections are not SEC_ALLOC and thus ld.so will
10903 not process them. */
10904 if (unresolved_reloc
10905 && !((input_section->flags & SEC_DEBUGGING) != 0
10906 && h->elf.def_dynamic))
10908 (*_bfd_error_handler)
10909 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
10910 input_bfd,
10911 input_section,
10912 (long) rel->r_offset,
10913 ppc64_elf_howto_table[(int) r_type]->name,
10914 h->elf.root.root.string);
10915 ret = FALSE;
10918 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
10919 input_bfd,
10920 input_section,
10921 contents,
10922 rel->r_offset,
10923 relocation,
10924 addend);
10926 if (r != bfd_reloc_ok)
10928 if (sym_name == NULL)
10929 sym_name = "(null)";
10930 if (r == bfd_reloc_overflow)
10932 if (warned)
10933 continue;
10934 if (h != NULL
10935 && h->elf.root.type == bfd_link_hash_undefweak
10936 && ppc64_elf_howto_table[r_type]->pc_relative)
10938 /* Assume this is a call protected by other code that
10939 detects the symbol is undefined. If this is the case,
10940 we can safely ignore the overflow. If not, the
10941 program is hosed anyway, and a little warning isn't
10942 going to help. */
10944 continue;
10947 if (!((*info->callbacks->reloc_overflow)
10948 (info, (h ? &h->elf.root : NULL), sym_name,
10949 ppc64_elf_howto_table[r_type]->name,
10950 orig_addend, input_bfd, input_section, rel->r_offset)))
10951 return FALSE;
10953 else
10955 (*_bfd_error_handler)
10956 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
10957 input_bfd,
10958 input_section,
10959 (long) rel->r_offset,
10960 ppc64_elf_howto_table[r_type]->name,
10961 sym_name,
10962 (int) r);
10963 ret = FALSE;
10968 /* If we're emitting relocations, then shortly after this function
10969 returns, reloc offsets and addends for this section will be
10970 adjusted. Worse, reloc symbol indices will be for the output
10971 file rather than the input. Save a copy of the relocs for
10972 opd_entry_value. */
10973 if (is_opd && (info->emitrelocations || info->relocatable))
10975 bfd_size_type amt;
10976 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
10977 rel = bfd_alloc (input_bfd, amt);
10978 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
10979 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
10980 if (rel == NULL)
10981 return FALSE;
10982 memcpy (rel, relocs, amt);
10984 return ret;
10987 /* Adjust the value of any local symbols in opd sections. */
10989 static bfd_boolean
10990 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
10991 const char *name ATTRIBUTE_UNUSED,
10992 Elf_Internal_Sym *elfsym,
10993 asection *input_sec,
10994 struct elf_link_hash_entry *h)
10996 long *opd_adjust, adjust;
10997 bfd_vma value;
10999 if (h != NULL)
11000 return TRUE;
11002 opd_adjust = get_opd_info (input_sec);
11003 if (opd_adjust == NULL)
11004 return TRUE;
11006 value = elfsym->st_value - input_sec->output_offset;
11007 if (!info->relocatable)
11008 value -= input_sec->output_section->vma;
11010 adjust = opd_adjust[value / 8];
11011 if (adjust == -1)
11012 elfsym->st_value = 0;
11013 else
11014 elfsym->st_value += adjust;
11015 return TRUE;
11018 /* Finish up dynamic symbol handling. We set the contents of various
11019 dynamic sections here. */
11021 static bfd_boolean
11022 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
11023 struct bfd_link_info *info,
11024 struct elf_link_hash_entry *h,
11025 Elf_Internal_Sym *sym)
11027 struct ppc_link_hash_table *htab;
11028 bfd *dynobj;
11029 struct plt_entry *ent;
11030 Elf_Internal_Rela rela;
11031 bfd_byte *loc;
11033 htab = ppc_hash_table (info);
11034 dynobj = htab->elf.dynobj;
11036 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
11037 if (ent->plt.offset != (bfd_vma) -1)
11039 /* This symbol has an entry in the procedure linkage
11040 table. Set it up. */
11042 if (htab->plt == NULL
11043 || htab->relplt == NULL
11044 || htab->glink == NULL)
11045 abort ();
11047 /* Create a JMP_SLOT reloc to inform the dynamic linker to
11048 fill in the PLT entry. */
11049 rela.r_offset = (htab->plt->output_section->vma
11050 + htab->plt->output_offset
11051 + ent->plt.offset);
11052 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
11053 rela.r_addend = ent->addend;
11055 loc = htab->relplt->contents;
11056 loc += ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
11057 * sizeof (Elf64_External_Rela));
11058 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
11061 if (h->needs_copy)
11063 Elf_Internal_Rela rela;
11064 bfd_byte *loc;
11066 /* This symbol needs a copy reloc. Set it up. */
11068 if (h->dynindx == -1
11069 || (h->root.type != bfd_link_hash_defined
11070 && h->root.type != bfd_link_hash_defweak)
11071 || htab->relbss == NULL)
11072 abort ();
11074 rela.r_offset = (h->root.u.def.value
11075 + h->root.u.def.section->output_section->vma
11076 + h->root.u.def.section->output_offset);
11077 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
11078 rela.r_addend = 0;
11079 loc = htab->relbss->contents;
11080 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
11081 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
11084 /* Mark some specially defined symbols as absolute. */
11085 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
11086 sym->st_shndx = SHN_ABS;
11088 return TRUE;
11091 /* Used to decide how to sort relocs in an optimal manner for the
11092 dynamic linker, before writing them out. */
11094 static enum elf_reloc_type_class
11095 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
11097 enum elf_ppc64_reloc_type r_type;
11099 r_type = ELF64_R_TYPE (rela->r_info);
11100 switch (r_type)
11102 case R_PPC64_RELATIVE:
11103 return reloc_class_relative;
11104 case R_PPC64_JMP_SLOT:
11105 return reloc_class_plt;
11106 case R_PPC64_COPY:
11107 return reloc_class_copy;
11108 default:
11109 return reloc_class_normal;
11113 /* Finish up the dynamic sections. */
11115 static bfd_boolean
11116 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
11117 struct bfd_link_info *info)
11119 struct ppc_link_hash_table *htab;
11120 bfd *dynobj;
11121 asection *sdyn;
11123 htab = ppc_hash_table (info);
11124 dynobj = htab->elf.dynobj;
11125 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
11127 if (htab->elf.dynamic_sections_created)
11129 Elf64_External_Dyn *dyncon, *dynconend;
11131 if (sdyn == NULL || htab->got == NULL)
11132 abort ();
11134 dyncon = (Elf64_External_Dyn *) sdyn->contents;
11135 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
11136 for (; dyncon < dynconend; dyncon++)
11138 Elf_Internal_Dyn dyn;
11139 asection *s;
11141 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
11143 switch (dyn.d_tag)
11145 default:
11146 continue;
11148 case DT_PPC64_GLINK:
11149 s = htab->glink;
11150 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11151 /* We stupidly defined DT_PPC64_GLINK to be the start
11152 of glink rather than the first entry point, which is
11153 what ld.so needs, and now have a bigger stub to
11154 support automatic multiple TOCs. */
11155 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
11156 break;
11158 case DT_PPC64_OPD:
11159 s = bfd_get_section_by_name (output_bfd, ".opd");
11160 if (s == NULL)
11161 continue;
11162 dyn.d_un.d_ptr = s->vma;
11163 break;
11165 case DT_PPC64_OPDSZ:
11166 s = bfd_get_section_by_name (output_bfd, ".opd");
11167 if (s == NULL)
11168 continue;
11169 dyn.d_un.d_val = s->size;
11170 break;
11172 case DT_PLTGOT:
11173 s = htab->plt;
11174 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11175 break;
11177 case DT_JMPREL:
11178 s = htab->relplt;
11179 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11180 break;
11182 case DT_PLTRELSZ:
11183 dyn.d_un.d_val = htab->relplt->size;
11184 break;
11186 case DT_RELASZ:
11187 /* Don't count procedure linkage table relocs in the
11188 overall reloc count. */
11189 s = htab->relplt;
11190 if (s == NULL)
11191 continue;
11192 dyn.d_un.d_val -= s->size;
11193 break;
11195 case DT_RELA:
11196 /* We may not be using the standard ELF linker script.
11197 If .rela.plt is the first .rela section, we adjust
11198 DT_RELA to not include it. */
11199 s = htab->relplt;
11200 if (s == NULL)
11201 continue;
11202 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
11203 continue;
11204 dyn.d_un.d_ptr += s->size;
11205 break;
11208 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
11212 if (htab->got != NULL && htab->got->size != 0)
11214 /* Fill in the first entry in the global offset table.
11215 We use it to hold the link-time TOCbase. */
11216 bfd_put_64 (output_bfd,
11217 elf_gp (output_bfd) + TOC_BASE_OFF,
11218 htab->got->contents);
11220 /* Set .got entry size. */
11221 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
11224 if (htab->plt != NULL && htab->plt->size != 0)
11226 /* Set .plt entry size. */
11227 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
11228 = PLT_ENTRY_SIZE;
11231 /* We need to handle writing out multiple GOT sections ourselves,
11232 since we didn't add them to DYNOBJ. We know dynobj is the first
11233 bfd. */
11234 while ((dynobj = dynobj->link_next) != NULL)
11236 asection *s;
11238 if (!is_ppc64_elf_target (dynobj->xvec))
11239 continue;
11241 s = ppc64_elf_tdata (dynobj)->got;
11242 if (s != NULL
11243 && s->size != 0
11244 && s->output_section != bfd_abs_section_ptr
11245 && !bfd_set_section_contents (output_bfd, s->output_section,
11246 s->contents, s->output_offset,
11247 s->size))
11248 return FALSE;
11249 s = ppc64_elf_tdata (dynobj)->relgot;
11250 if (s != NULL
11251 && s->size != 0
11252 && s->output_section != bfd_abs_section_ptr
11253 && !bfd_set_section_contents (output_bfd, s->output_section,
11254 s->contents, s->output_offset,
11255 s->size))
11256 return FALSE;
11259 return TRUE;
11262 #include "elf64-target.h"