Run --gc-sections tests only if supported.
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010, 2011 Free Software Foundation, Inc.
4 Written by Linus Nordberg, Swox AB <info@swox.com>,
5 based on elf32-ppc.c by Ian Lance Taylor.
6 Largely rewritten by Alan Modra.
7
8 This file is part of BFD, the Binary File Descriptor library.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
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.
19
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. */
23
24
25 /* The 64-bit PowerPC ELF ABI may be found at
26 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
28
29 #include "sysdep.h"
30 #include <stdarg.h>
31 #include "bfd.h"
32 #include "bfdlink.h"
33 #include "libbfd.h"
34 #include "elf-bfd.h"
35 #include "elf/ppc64.h"
36 #include "elf64-ppc.h"
37 #include "dwarf2.h"
38
39 static bfd_reloc_status_type ppc64_elf_ha_reloc
40 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41 static bfd_reloc_status_type ppc64_elf_branch_reloc
42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
44 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
45 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
46 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
47 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
48 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
49 static bfd_reloc_status_type ppc64_elf_toc_reloc
50 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
51 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
52 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
53 static bfd_reloc_status_type ppc64_elf_toc64_reloc
54 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
55 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
56 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
57 static bfd_vma opd_entry_value
58 (asection *, bfd_vma, asection **, bfd_vma *);
59
60 #define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
61 #define TARGET_LITTLE_NAME "elf64-powerpcle"
62 #define TARGET_BIG_SYM bfd_elf64_powerpc_vec
63 #define TARGET_BIG_NAME "elf64-powerpc"
64 #define ELF_ARCH bfd_arch_powerpc
65 #define ELF_TARGET_ID PPC64_ELF_DATA
66 #define ELF_MACHINE_CODE EM_PPC64
67 #define ELF_MAXPAGESIZE 0x10000
68 #define ELF_COMMONPAGESIZE 0x1000
69 #define elf_info_to_howto ppc64_elf_info_to_howto
70
71 #define elf_backend_want_got_sym 0
72 #define elf_backend_want_plt_sym 0
73 #define elf_backend_plt_alignment 3
74 #define elf_backend_plt_not_loaded 1
75 #define elf_backend_got_header_size 8
76 #define elf_backend_can_gc_sections 1
77 #define elf_backend_can_refcount 1
78 #define elf_backend_rela_normal 1
79 #define elf_backend_default_execstack 0
80
81 #define bfd_elf64_mkobject ppc64_elf_mkobject
82 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
83 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
84 #define bfd_elf64_bfd_merge_private_bfd_data _bfd_generic_verify_endian_match
85 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
86 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
87 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
88 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
89 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
90
91 #define elf_backend_object_p ppc64_elf_object_p
92 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
93 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
94 #define elf_backend_write_core_note ppc64_elf_write_core_note
95 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
96 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
97 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
98 #define elf_backend_check_directives ppc64_elf_process_dot_syms
99 #define elf_backend_as_needed_cleanup ppc64_elf_as_needed_cleanup
100 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
101 #define elf_backend_check_relocs ppc64_elf_check_relocs
102 #define elf_backend_gc_keep ppc64_elf_gc_keep
103 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
104 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
105 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
106 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
107 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
108 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
109 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
110 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
111 #define elf_backend_action_discarded ppc64_elf_action_discarded
112 #define elf_backend_relocate_section ppc64_elf_relocate_section
113 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
114 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
115 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
116 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
117 #define elf_backend_special_sections ppc64_elf_special_sections
118 #define elf_backend_post_process_headers _bfd_elf_set_osabi
119
120 /* The name of the dynamic interpreter. This is put in the .interp
121 section. */
122 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
123
124 /* The size in bytes of an entry in the procedure linkage table. */
125 #define PLT_ENTRY_SIZE 24
126
127 /* The initial size of the plt reserved for the dynamic linker. */
128 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
129
130 /* TOC base pointers offset from start of TOC. */
131 #define TOC_BASE_OFF 0x8000
132
133 /* Offset of tp and dtp pointers from start of TLS block. */
134 #define TP_OFFSET 0x7000
135 #define DTP_OFFSET 0x8000
136
137 /* .plt call stub instructions. The normal stub is like this, but
138 sometimes the .plt entry crosses a 64k boundary and we need to
139 insert an addi to adjust r12. */
140 #define PLT_CALL_STUB_SIZE (7*4)
141 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
142 #define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
143 #define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
144 #define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
145 #define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
146 /* ld %r11,xxx+16@l(%r12) */
147 #define BCTR 0x4e800420 /* bctr */
148
149
150 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,off@ha */
151 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
152 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
153 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
154
155 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
156 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
157
158 #define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
159
160 /* glink call stub instructions. We enter with the index in R0. */
161 #define GLINK_CALL_STUB_SIZE (16*4)
162 /* 0: */
163 /* .quad plt0-1f */
164 /* __glink: */
165 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
166 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
167 /* 1: */
168 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
169 #define LD_R2_M16R11 0xe84bfff0 /* ld %2,(0b-1b)(%11) */
170 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
171 #define ADD_R12_R2_R11 0x7d825a14 /* add %12,%2,%11 */
172 /* ld %11,0(%12) */
173 /* ld %2,8(%12) */
174 /* mtctr %11 */
175 /* ld %11,16(%12) */
176 /* bctr */
177
178 /* Pad with this. */
179 #define NOP 0x60000000
180
181 /* Some other nops. */
182 #define CROR_151515 0x4def7b82
183 #define CROR_313131 0x4ffffb82
184
185 /* .glink entries for the first 32k functions are two instructions. */
186 #define LI_R0_0 0x38000000 /* li %r0,0 */
187 #define B_DOT 0x48000000 /* b . */
188
189 /* After that, we need two instructions to load the index, followed by
190 a branch. */
191 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
192 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
193
194 /* Instructions used by the save and restore reg functions. */
195 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
196 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
197 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
198 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
199 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
200 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
201 #define LI_R12_0 0x39800000 /* li %r12,0 */
202 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
203 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
204 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
205 #define BLR 0x4e800020 /* blr */
206
207 /* Since .opd is an array of descriptors and each entry will end up
208 with identical R_PPC64_RELATIVE relocs, there is really no need to
209 propagate .opd relocs; The dynamic linker should be taught to
210 relocate .opd without reloc entries. */
211 #ifndef NO_OPD_RELOCS
212 #define NO_OPD_RELOCS 0
213 #endif
214 \f
215 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
216
217 /* Relocation HOWTO's. */
218 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
219
220 static reloc_howto_type ppc64_elf_howto_raw[] = {
221 /* This reloc does nothing. */
222 HOWTO (R_PPC64_NONE, /* type */
223 0, /* rightshift */
224 2, /* size (0 = byte, 1 = short, 2 = long) */
225 32, /* bitsize */
226 FALSE, /* pc_relative */
227 0, /* bitpos */
228 complain_overflow_dont, /* complain_on_overflow */
229 bfd_elf_generic_reloc, /* special_function */
230 "R_PPC64_NONE", /* name */
231 FALSE, /* partial_inplace */
232 0, /* src_mask */
233 0, /* dst_mask */
234 FALSE), /* pcrel_offset */
235
236 /* A standard 32 bit relocation. */
237 HOWTO (R_PPC64_ADDR32, /* type */
238 0, /* rightshift */
239 2, /* size (0 = byte, 1 = short, 2 = long) */
240 32, /* 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_ADDR32", /* name */
246 FALSE, /* partial_inplace */
247 0, /* src_mask */
248 0xffffffff, /* dst_mask */
249 FALSE), /* pcrel_offset */
250
251 /* An absolute 26 bit branch; the lower two bits must be zero.
252 FIXME: we don't check that, we just clear them. */
253 HOWTO (R_PPC64_ADDR24, /* type */
254 0, /* rightshift */
255 2, /* size (0 = byte, 1 = short, 2 = long) */
256 26, /* bitsize */
257 FALSE, /* pc_relative */
258 0, /* bitpos */
259 complain_overflow_bitfield, /* complain_on_overflow */
260 bfd_elf_generic_reloc, /* special_function */
261 "R_PPC64_ADDR24", /* name */
262 FALSE, /* partial_inplace */
263 0, /* src_mask */
264 0x03fffffc, /* dst_mask */
265 FALSE), /* pcrel_offset */
266
267 /* A standard 16 bit relocation. */
268 HOWTO (R_PPC64_ADDR16, /* type */
269 0, /* rightshift */
270 1, /* size (0 = byte, 1 = short, 2 = long) */
271 16, /* bitsize */
272 FALSE, /* pc_relative */
273 0, /* bitpos */
274 complain_overflow_bitfield, /* complain_on_overflow */
275 bfd_elf_generic_reloc, /* special_function */
276 "R_PPC64_ADDR16", /* name */
277 FALSE, /* partial_inplace */
278 0, /* src_mask */
279 0xffff, /* dst_mask */
280 FALSE), /* pcrel_offset */
281
282 /* A 16 bit relocation without overflow. */
283 HOWTO (R_PPC64_ADDR16_LO, /* type */
284 0, /* rightshift */
285 1, /* size (0 = byte, 1 = short, 2 = long) */
286 16, /* bitsize */
287 FALSE, /* pc_relative */
288 0, /* bitpos */
289 complain_overflow_dont,/* complain_on_overflow */
290 bfd_elf_generic_reloc, /* special_function */
291 "R_PPC64_ADDR16_LO", /* name */
292 FALSE, /* partial_inplace */
293 0, /* src_mask */
294 0xffff, /* dst_mask */
295 FALSE), /* pcrel_offset */
296
297 /* Bits 16-31 of an address. */
298 HOWTO (R_PPC64_ADDR16_HI, /* 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 bfd_elf_generic_reloc, /* special_function */
306 "R_PPC64_ADDR16_HI", /* name */
307 FALSE, /* partial_inplace */
308 0, /* src_mask */
309 0xffff, /* dst_mask */
310 FALSE), /* pcrel_offset */
311
312 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
313 bits, treated as a signed number, is negative. */
314 HOWTO (R_PPC64_ADDR16_HA, /* type */
315 16, /* rightshift */
316 1, /* size (0 = byte, 1 = short, 2 = long) */
317 16, /* bitsize */
318 FALSE, /* pc_relative */
319 0, /* bitpos */
320 complain_overflow_dont, /* complain_on_overflow */
321 ppc64_elf_ha_reloc, /* special_function */
322 "R_PPC64_ADDR16_HA", /* name */
323 FALSE, /* partial_inplace */
324 0, /* src_mask */
325 0xffff, /* dst_mask */
326 FALSE), /* pcrel_offset */
327
328 /* An absolute 16 bit branch; the lower two bits must be zero.
329 FIXME: we don't check that, we just clear them. */
330 HOWTO (R_PPC64_ADDR14, /* type */
331 0, /* rightshift */
332 2, /* size (0 = byte, 1 = short, 2 = long) */
333 16, /* bitsize */
334 FALSE, /* pc_relative */
335 0, /* bitpos */
336 complain_overflow_bitfield, /* complain_on_overflow */
337 ppc64_elf_branch_reloc, /* special_function */
338 "R_PPC64_ADDR14", /* name */
339 FALSE, /* partial_inplace */
340 0, /* src_mask */
341 0x0000fffc, /* dst_mask */
342 FALSE), /* pcrel_offset */
343
344 /* An absolute 16 bit branch, for which bit 10 should be set to
345 indicate that the branch is expected to be taken. The lower two
346 bits must be zero. */
347 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
348 0, /* rightshift */
349 2, /* size (0 = byte, 1 = short, 2 = long) */
350 16, /* bitsize */
351 FALSE, /* pc_relative */
352 0, /* bitpos */
353 complain_overflow_bitfield, /* complain_on_overflow */
354 ppc64_elf_brtaken_reloc, /* special_function */
355 "R_PPC64_ADDR14_BRTAKEN",/* name */
356 FALSE, /* partial_inplace */
357 0, /* src_mask */
358 0x0000fffc, /* dst_mask */
359 FALSE), /* pcrel_offset */
360
361 /* An absolute 16 bit branch, for which bit 10 should be set to
362 indicate that the branch is not expected to be taken. The lower
363 two bits must be zero. */
364 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
365 0, /* rightshift */
366 2, /* size (0 = byte, 1 = short, 2 = long) */
367 16, /* bitsize */
368 FALSE, /* pc_relative */
369 0, /* bitpos */
370 complain_overflow_bitfield, /* complain_on_overflow */
371 ppc64_elf_brtaken_reloc, /* special_function */
372 "R_PPC64_ADDR14_BRNTAKEN",/* name */
373 FALSE, /* partial_inplace */
374 0, /* src_mask */
375 0x0000fffc, /* dst_mask */
376 FALSE), /* pcrel_offset */
377
378 /* A relative 26 bit branch; the lower two bits must be zero. */
379 HOWTO (R_PPC64_REL24, /* type */
380 0, /* rightshift */
381 2, /* size (0 = byte, 1 = short, 2 = long) */
382 26, /* bitsize */
383 TRUE, /* pc_relative */
384 0, /* bitpos */
385 complain_overflow_signed, /* complain_on_overflow */
386 ppc64_elf_branch_reloc, /* special_function */
387 "R_PPC64_REL24", /* name */
388 FALSE, /* partial_inplace */
389 0, /* src_mask */
390 0x03fffffc, /* dst_mask */
391 TRUE), /* pcrel_offset */
392
393 /* A relative 16 bit branch; the lower two bits must be zero. */
394 HOWTO (R_PPC64_REL14, /* type */
395 0, /* rightshift */
396 2, /* size (0 = byte, 1 = short, 2 = long) */
397 16, /* bitsize */
398 TRUE, /* pc_relative */
399 0, /* bitpos */
400 complain_overflow_signed, /* complain_on_overflow */
401 ppc64_elf_branch_reloc, /* special_function */
402 "R_PPC64_REL14", /* name */
403 FALSE, /* partial_inplace */
404 0, /* src_mask */
405 0x0000fffc, /* dst_mask */
406 TRUE), /* pcrel_offset */
407
408 /* A relative 16 bit branch. Bit 10 should be set to indicate that
409 the branch is expected to be taken. The lower two bits must be
410 zero. */
411 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
412 0, /* rightshift */
413 2, /* size (0 = byte, 1 = short, 2 = long) */
414 16, /* bitsize */
415 TRUE, /* pc_relative */
416 0, /* bitpos */
417 complain_overflow_signed, /* complain_on_overflow */
418 ppc64_elf_brtaken_reloc, /* special_function */
419 "R_PPC64_REL14_BRTAKEN", /* name */
420 FALSE, /* partial_inplace */
421 0, /* src_mask */
422 0x0000fffc, /* dst_mask */
423 TRUE), /* pcrel_offset */
424
425 /* A relative 16 bit branch. Bit 10 should be set to indicate that
426 the branch is not expected to be taken. The lower two bits must
427 be zero. */
428 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
429 0, /* rightshift */
430 2, /* size (0 = byte, 1 = short, 2 = long) */
431 16, /* bitsize */
432 TRUE, /* pc_relative */
433 0, /* bitpos */
434 complain_overflow_signed, /* complain_on_overflow */
435 ppc64_elf_brtaken_reloc, /* special_function */
436 "R_PPC64_REL14_BRNTAKEN",/* name */
437 FALSE, /* partial_inplace */
438 0, /* src_mask */
439 0x0000fffc, /* dst_mask */
440 TRUE), /* pcrel_offset */
441
442 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
443 symbol. */
444 HOWTO (R_PPC64_GOT16, /* 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_signed, /* complain_on_overflow */
451 ppc64_elf_unhandled_reloc, /* special_function */
452 "R_PPC64_GOT16", /* name */
453 FALSE, /* partial_inplace */
454 0, /* src_mask */
455 0xffff, /* dst_mask */
456 FALSE), /* pcrel_offset */
457
458 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
459 the symbol. */
460 HOWTO (R_PPC64_GOT16_LO, /* type */
461 0, /* 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_LO", /* name */
469 FALSE, /* partial_inplace */
470 0, /* src_mask */
471 0xffff, /* dst_mask */
472 FALSE), /* pcrel_offset */
473
474 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
475 the symbol. */
476 HOWTO (R_PPC64_GOT16_HI, /* 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_HI", /* name */
485 FALSE, /* partial_inplace */
486 0, /* src_mask */
487 0xffff, /* dst_mask */
488 FALSE), /* pcrel_offset */
489
490 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
491 the symbol. */
492 HOWTO (R_PPC64_GOT16_HA, /* type */
493 16, /* rightshift */
494 1, /* size (0 = byte, 1 = short, 2 = long) */
495 16, /* bitsize */
496 FALSE, /* pc_relative */
497 0, /* bitpos */
498 complain_overflow_dont,/* complain_on_overflow */
499 ppc64_elf_unhandled_reloc, /* special_function */
500 "R_PPC64_GOT16_HA", /* name */
501 FALSE, /* partial_inplace */
502 0, /* src_mask */
503 0xffff, /* dst_mask */
504 FALSE), /* pcrel_offset */
505
506 /* This is used only by the dynamic linker. The symbol should exist
507 both in the object being run and in some shared library. The
508 dynamic linker copies the data addressed by the symbol from the
509 shared library into the object, because the object being
510 run has to have the data at some particular address. */
511 HOWTO (R_PPC64_COPY, /* type */
512 0, /* rightshift */
513 0, /* this one is variable size */
514 0, /* 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_COPY", /* name */
520 FALSE, /* partial_inplace */
521 0, /* src_mask */
522 0, /* dst_mask */
523 FALSE), /* pcrel_offset */
524
525 /* Like R_PPC64_ADDR64, but used when setting global offset table
526 entries. */
527 HOWTO (R_PPC64_GLOB_DAT, /* type */
528 0, /* rightshift */
529 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
530 64, /* 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_GLOB_DAT", /* name */
536 FALSE, /* partial_inplace */
537 0, /* src_mask */
538 ONES (64), /* dst_mask */
539 FALSE), /* pcrel_offset */
540
541 /* Created by the link editor. Marks a procedure linkage table
542 entry for a symbol. */
543 HOWTO (R_PPC64_JMP_SLOT, /* type */
544 0, /* rightshift */
545 0, /* size (0 = byte, 1 = short, 2 = long) */
546 0, /* bitsize */
547 FALSE, /* pc_relative */
548 0, /* bitpos */
549 complain_overflow_dont, /* complain_on_overflow */
550 ppc64_elf_unhandled_reloc, /* special_function */
551 "R_PPC64_JMP_SLOT", /* name */
552 FALSE, /* partial_inplace */
553 0, /* src_mask */
554 0, /* dst_mask */
555 FALSE), /* pcrel_offset */
556
557 /* Used only by the dynamic linker. When the object is run, this
558 doubleword64 is set to the load address of the object, plus the
559 addend. */
560 HOWTO (R_PPC64_RELATIVE, /* type */
561 0, /* rightshift */
562 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
563 64, /* bitsize */
564 FALSE, /* pc_relative */
565 0, /* bitpos */
566 complain_overflow_dont, /* complain_on_overflow */
567 bfd_elf_generic_reloc, /* special_function */
568 "R_PPC64_RELATIVE", /* name */
569 FALSE, /* partial_inplace */
570 0, /* src_mask */
571 ONES (64), /* dst_mask */
572 FALSE), /* pcrel_offset */
573
574 /* Like R_PPC64_ADDR32, but may be unaligned. */
575 HOWTO (R_PPC64_UADDR32, /* type */
576 0, /* rightshift */
577 2, /* size (0 = byte, 1 = short, 2 = long) */
578 32, /* bitsize */
579 FALSE, /* pc_relative */
580 0, /* bitpos */
581 complain_overflow_bitfield, /* complain_on_overflow */
582 bfd_elf_generic_reloc, /* special_function */
583 "R_PPC64_UADDR32", /* name */
584 FALSE, /* partial_inplace */
585 0, /* src_mask */
586 0xffffffff, /* dst_mask */
587 FALSE), /* pcrel_offset */
588
589 /* Like R_PPC64_ADDR16, but may be unaligned. */
590 HOWTO (R_PPC64_UADDR16, /* type */
591 0, /* rightshift */
592 1, /* size (0 = byte, 1 = short, 2 = long) */
593 16, /* bitsize */
594 FALSE, /* pc_relative */
595 0, /* bitpos */
596 complain_overflow_bitfield, /* complain_on_overflow */
597 bfd_elf_generic_reloc, /* special_function */
598 "R_PPC64_UADDR16", /* name */
599 FALSE, /* partial_inplace */
600 0, /* src_mask */
601 0xffff, /* dst_mask */
602 FALSE), /* pcrel_offset */
603
604 /* 32-bit PC relative. */
605 HOWTO (R_PPC64_REL32, /* type */
606 0, /* rightshift */
607 2, /* size (0 = byte, 1 = short, 2 = long) */
608 32, /* bitsize */
609 TRUE, /* pc_relative */
610 0, /* bitpos */
611 /* FIXME: Verify. Was complain_overflow_bitfield. */
612 complain_overflow_signed, /* complain_on_overflow */
613 bfd_elf_generic_reloc, /* special_function */
614 "R_PPC64_REL32", /* name */
615 FALSE, /* partial_inplace */
616 0, /* src_mask */
617 0xffffffff, /* dst_mask */
618 TRUE), /* pcrel_offset */
619
620 /* 32-bit relocation to the symbol's procedure linkage table. */
621 HOWTO (R_PPC64_PLT32, /* type */
622 0, /* rightshift */
623 2, /* size (0 = byte, 1 = short, 2 = long) */
624 32, /* bitsize */
625 FALSE, /* pc_relative */
626 0, /* bitpos */
627 complain_overflow_bitfield, /* complain_on_overflow */
628 ppc64_elf_unhandled_reloc, /* special_function */
629 "R_PPC64_PLT32", /* name */
630 FALSE, /* partial_inplace */
631 0, /* src_mask */
632 0xffffffff, /* dst_mask */
633 FALSE), /* pcrel_offset */
634
635 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
636 FIXME: R_PPC64_PLTREL32 not supported. */
637 HOWTO (R_PPC64_PLTREL32, /* type */
638 0, /* rightshift */
639 2, /* size (0 = byte, 1 = short, 2 = long) */
640 32, /* bitsize */
641 TRUE, /* pc_relative */
642 0, /* bitpos */
643 complain_overflow_signed, /* complain_on_overflow */
644 bfd_elf_generic_reloc, /* special_function */
645 "R_PPC64_PLTREL32", /* name */
646 FALSE, /* partial_inplace */
647 0, /* src_mask */
648 0xffffffff, /* dst_mask */
649 TRUE), /* pcrel_offset */
650
651 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
652 the symbol. */
653 HOWTO (R_PPC64_PLT16_LO, /* type */
654 0, /* 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_LO", /* name */
662 FALSE, /* partial_inplace */
663 0, /* src_mask */
664 0xffff, /* dst_mask */
665 FALSE), /* pcrel_offset */
666
667 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
668 the symbol. */
669 HOWTO (R_PPC64_PLT16_HI, /* 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_HI", /* name */
678 FALSE, /* partial_inplace */
679 0, /* src_mask */
680 0xffff, /* dst_mask */
681 FALSE), /* pcrel_offset */
682
683 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
684 the symbol. */
685 HOWTO (R_PPC64_PLT16_HA, /* type */
686 16, /* rightshift */
687 1, /* size (0 = byte, 1 = short, 2 = long) */
688 16, /* bitsize */
689 FALSE, /* pc_relative */
690 0, /* bitpos */
691 complain_overflow_dont, /* complain_on_overflow */
692 ppc64_elf_unhandled_reloc, /* special_function */
693 "R_PPC64_PLT16_HA", /* name */
694 FALSE, /* partial_inplace */
695 0, /* src_mask */
696 0xffff, /* dst_mask */
697 FALSE), /* pcrel_offset */
698
699 /* 16-bit section relative relocation. */
700 HOWTO (R_PPC64_SECTOFF, /* type */
701 0, /* rightshift */
702 1, /* size (0 = byte, 1 = short, 2 = long) */
703 16, /* bitsize */
704 FALSE, /* pc_relative */
705 0, /* bitpos */
706 complain_overflow_bitfield, /* complain_on_overflow */
707 ppc64_elf_sectoff_reloc, /* special_function */
708 "R_PPC64_SECTOFF", /* name */
709 FALSE, /* partial_inplace */
710 0, /* src_mask */
711 0xffff, /* dst_mask */
712 FALSE), /* pcrel_offset */
713
714 /* Like R_PPC64_SECTOFF, but no overflow warning. */
715 HOWTO (R_PPC64_SECTOFF_LO, /* type */
716 0, /* rightshift */
717 1, /* size (0 = byte, 1 = short, 2 = long) */
718 16, /* bitsize */
719 FALSE, /* pc_relative */
720 0, /* bitpos */
721 complain_overflow_dont, /* complain_on_overflow */
722 ppc64_elf_sectoff_reloc, /* special_function */
723 "R_PPC64_SECTOFF_LO", /* name */
724 FALSE, /* partial_inplace */
725 0, /* src_mask */
726 0xffff, /* dst_mask */
727 FALSE), /* pcrel_offset */
728
729 /* 16-bit upper half section relative relocation. */
730 HOWTO (R_PPC64_SECTOFF_HI, /* type */
731 16, /* rightshift */
732 1, /* size (0 = byte, 1 = short, 2 = long) */
733 16, /* bitsize */
734 FALSE, /* pc_relative */
735 0, /* bitpos */
736 complain_overflow_dont, /* complain_on_overflow */
737 ppc64_elf_sectoff_reloc, /* special_function */
738 "R_PPC64_SECTOFF_HI", /* name */
739 FALSE, /* partial_inplace */
740 0, /* src_mask */
741 0xffff, /* dst_mask */
742 FALSE), /* pcrel_offset */
743
744 /* 16-bit upper half adjusted section relative relocation. */
745 HOWTO (R_PPC64_SECTOFF_HA, /* type */
746 16, /* rightshift */
747 1, /* size (0 = byte, 1 = short, 2 = long) */
748 16, /* bitsize */
749 FALSE, /* pc_relative */
750 0, /* bitpos */
751 complain_overflow_dont, /* complain_on_overflow */
752 ppc64_elf_sectoff_ha_reloc, /* special_function */
753 "R_PPC64_SECTOFF_HA", /* name */
754 FALSE, /* partial_inplace */
755 0, /* src_mask */
756 0xffff, /* dst_mask */
757 FALSE), /* pcrel_offset */
758
759 /* Like R_PPC64_REL24 without touching the two least significant bits. */
760 HOWTO (R_PPC64_REL30, /* type */
761 2, /* rightshift */
762 2, /* size (0 = byte, 1 = short, 2 = long) */
763 30, /* bitsize */
764 TRUE, /* pc_relative */
765 0, /* bitpos */
766 complain_overflow_dont, /* complain_on_overflow */
767 bfd_elf_generic_reloc, /* special_function */
768 "R_PPC64_REL30", /* name */
769 FALSE, /* partial_inplace */
770 0, /* src_mask */
771 0xfffffffc, /* dst_mask */
772 TRUE), /* pcrel_offset */
773
774 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
775
776 /* A standard 64-bit relocation. */
777 HOWTO (R_PPC64_ADDR64, /* type */
778 0, /* rightshift */
779 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
780 64, /* bitsize */
781 FALSE, /* pc_relative */
782 0, /* bitpos */
783 complain_overflow_dont, /* complain_on_overflow */
784 bfd_elf_generic_reloc, /* special_function */
785 "R_PPC64_ADDR64", /* name */
786 FALSE, /* partial_inplace */
787 0, /* src_mask */
788 ONES (64), /* dst_mask */
789 FALSE), /* pcrel_offset */
790
791 /* The bits 32-47 of an address. */
792 HOWTO (R_PPC64_ADDR16_HIGHER, /* 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 bfd_elf_generic_reloc, /* special_function */
800 "R_PPC64_ADDR16_HIGHER", /* name */
801 FALSE, /* partial_inplace */
802 0, /* src_mask */
803 0xffff, /* dst_mask */
804 FALSE), /* pcrel_offset */
805
806 /* The bits 32-47 of an address, plus 1 if the contents of the low
807 16 bits, treated as a signed number, is negative. */
808 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
809 32, /* rightshift */
810 1, /* size (0 = byte, 1 = short, 2 = long) */
811 16, /* bitsize */
812 FALSE, /* pc_relative */
813 0, /* bitpos */
814 complain_overflow_dont, /* complain_on_overflow */
815 ppc64_elf_ha_reloc, /* special_function */
816 "R_PPC64_ADDR16_HIGHERA", /* name */
817 FALSE, /* partial_inplace */
818 0, /* src_mask */
819 0xffff, /* dst_mask */
820 FALSE), /* pcrel_offset */
821
822 /* The bits 48-63 of an address. */
823 HOWTO (R_PPC64_ADDR16_HIGHEST,/* 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 bfd_elf_generic_reloc, /* special_function */
831 "R_PPC64_ADDR16_HIGHEST", /* name */
832 FALSE, /* partial_inplace */
833 0, /* src_mask */
834 0xffff, /* dst_mask */
835 FALSE), /* pcrel_offset */
836
837 /* The bits 48-63 of an address, plus 1 if the contents of the low
838 16 bits, treated as a signed number, is negative. */
839 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
840 48, /* rightshift */
841 1, /* size (0 = byte, 1 = short, 2 = long) */
842 16, /* bitsize */
843 FALSE, /* pc_relative */
844 0, /* bitpos */
845 complain_overflow_dont, /* complain_on_overflow */
846 ppc64_elf_ha_reloc, /* special_function */
847 "R_PPC64_ADDR16_HIGHESTA", /* name */
848 FALSE, /* partial_inplace */
849 0, /* src_mask */
850 0xffff, /* dst_mask */
851 FALSE), /* pcrel_offset */
852
853 /* Like ADDR64, but may be unaligned. */
854 HOWTO (R_PPC64_UADDR64, /* type */
855 0, /* rightshift */
856 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
857 64, /* bitsize */
858 FALSE, /* pc_relative */
859 0, /* bitpos */
860 complain_overflow_dont, /* complain_on_overflow */
861 bfd_elf_generic_reloc, /* special_function */
862 "R_PPC64_UADDR64", /* name */
863 FALSE, /* partial_inplace */
864 0, /* src_mask */
865 ONES (64), /* dst_mask */
866 FALSE), /* pcrel_offset */
867
868 /* 64-bit relative relocation. */
869 HOWTO (R_PPC64_REL64, /* type */
870 0, /* rightshift */
871 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
872 64, /* bitsize */
873 TRUE, /* pc_relative */
874 0, /* bitpos */
875 complain_overflow_dont, /* complain_on_overflow */
876 bfd_elf_generic_reloc, /* special_function */
877 "R_PPC64_REL64", /* name */
878 FALSE, /* partial_inplace */
879 0, /* src_mask */
880 ONES (64), /* dst_mask */
881 TRUE), /* pcrel_offset */
882
883 /* 64-bit relocation to the symbol's procedure linkage table. */
884 HOWTO (R_PPC64_PLT64, /* type */
885 0, /* rightshift */
886 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
887 64, /* bitsize */
888 FALSE, /* pc_relative */
889 0, /* bitpos */
890 complain_overflow_dont, /* complain_on_overflow */
891 ppc64_elf_unhandled_reloc, /* special_function */
892 "R_PPC64_PLT64", /* name */
893 FALSE, /* partial_inplace */
894 0, /* src_mask */
895 ONES (64), /* dst_mask */
896 FALSE), /* pcrel_offset */
897
898 /* 64-bit PC relative relocation to the symbol's procedure linkage
899 table. */
900 /* FIXME: R_PPC64_PLTREL64 not supported. */
901 HOWTO (R_PPC64_PLTREL64, /* type */
902 0, /* rightshift */
903 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
904 64, /* bitsize */
905 TRUE, /* pc_relative */
906 0, /* bitpos */
907 complain_overflow_dont, /* complain_on_overflow */
908 ppc64_elf_unhandled_reloc, /* special_function */
909 "R_PPC64_PLTREL64", /* name */
910 FALSE, /* partial_inplace */
911 0, /* src_mask */
912 ONES (64), /* dst_mask */
913 TRUE), /* pcrel_offset */
914
915 /* 16 bit TOC-relative relocation. */
916
917 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
918 HOWTO (R_PPC64_TOC16, /* type */
919 0, /* rightshift */
920 1, /* size (0 = byte, 1 = short, 2 = long) */
921 16, /* bitsize */
922 FALSE, /* pc_relative */
923 0, /* bitpos */
924 complain_overflow_signed, /* complain_on_overflow */
925 ppc64_elf_toc_reloc, /* special_function */
926 "R_PPC64_TOC16", /* name */
927 FALSE, /* partial_inplace */
928 0, /* src_mask */
929 0xffff, /* dst_mask */
930 FALSE), /* pcrel_offset */
931
932 /* 16 bit TOC-relative relocation without overflow. */
933
934 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
935 HOWTO (R_PPC64_TOC16_LO, /* type */
936 0, /* rightshift */
937 1, /* size (0 = byte, 1 = short, 2 = long) */
938 16, /* bitsize */
939 FALSE, /* pc_relative */
940 0, /* bitpos */
941 complain_overflow_dont, /* complain_on_overflow */
942 ppc64_elf_toc_reloc, /* special_function */
943 "R_PPC64_TOC16_LO", /* name */
944 FALSE, /* partial_inplace */
945 0, /* src_mask */
946 0xffff, /* dst_mask */
947 FALSE), /* pcrel_offset */
948
949 /* 16 bit TOC-relative relocation, high 16 bits. */
950
951 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
952 HOWTO (R_PPC64_TOC16_HI, /* type */
953 16, /* rightshift */
954 1, /* size (0 = byte, 1 = short, 2 = long) */
955 16, /* bitsize */
956 FALSE, /* pc_relative */
957 0, /* bitpos */
958 complain_overflow_dont, /* complain_on_overflow */
959 ppc64_elf_toc_reloc, /* special_function */
960 "R_PPC64_TOC16_HI", /* name */
961 FALSE, /* partial_inplace */
962 0, /* src_mask */
963 0xffff, /* dst_mask */
964 FALSE), /* pcrel_offset */
965
966 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
967 contents of the low 16 bits, treated as a signed number, is
968 negative. */
969
970 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
971 HOWTO (R_PPC64_TOC16_HA, /* type */
972 16, /* rightshift */
973 1, /* size (0 = byte, 1 = short, 2 = long) */
974 16, /* bitsize */
975 FALSE, /* pc_relative */
976 0, /* bitpos */
977 complain_overflow_dont, /* complain_on_overflow */
978 ppc64_elf_toc_ha_reloc, /* special_function */
979 "R_PPC64_TOC16_HA", /* name */
980 FALSE, /* partial_inplace */
981 0, /* src_mask */
982 0xffff, /* dst_mask */
983 FALSE), /* pcrel_offset */
984
985 /* 64-bit relocation; insert value of TOC base (.TOC.). */
986
987 /* R_PPC64_TOC 51 doubleword64 .TOC. */
988 HOWTO (R_PPC64_TOC, /* type */
989 0, /* rightshift */
990 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
991 64, /* bitsize */
992 FALSE, /* pc_relative */
993 0, /* bitpos */
994 complain_overflow_bitfield, /* complain_on_overflow */
995 ppc64_elf_toc64_reloc, /* special_function */
996 "R_PPC64_TOC", /* name */
997 FALSE, /* partial_inplace */
998 0, /* src_mask */
999 ONES (64), /* dst_mask */
1000 FALSE), /* pcrel_offset */
1001
1002 /* Like R_PPC64_GOT16, but also informs the link editor that the
1003 value to relocate may (!) refer to a PLT entry which the link
1004 editor (a) may replace with the symbol value. If the link editor
1005 is unable to fully resolve the symbol, it may (b) create a PLT
1006 entry and store the address to the new PLT entry in the GOT.
1007 This permits lazy resolution of function symbols at run time.
1008 The link editor may also skip all of this and just (c) emit a
1009 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1010 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1011 HOWTO (R_PPC64_PLTGOT16, /* 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_signed, /* complain_on_overflow */
1018 ppc64_elf_unhandled_reloc, /* special_function */
1019 "R_PPC64_PLTGOT16", /* name */
1020 FALSE, /* partial_inplace */
1021 0, /* src_mask */
1022 0xffff, /* dst_mask */
1023 FALSE), /* pcrel_offset */
1024
1025 /* Like R_PPC64_PLTGOT16, but without overflow. */
1026 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1027 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1028 0, /* 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_LO", /* name */
1036 FALSE, /* partial_inplace */
1037 0, /* src_mask */
1038 0xffff, /* dst_mask */
1039 FALSE), /* pcrel_offset */
1040
1041 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1042 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1043 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1044 16, /* rightshift */
1045 1, /* size (0 = byte, 1 = short, 2 = long) */
1046 16, /* bitsize */
1047 FALSE, /* pc_relative */
1048 0, /* bitpos */
1049 complain_overflow_dont, /* complain_on_overflow */
1050 ppc64_elf_unhandled_reloc, /* special_function */
1051 "R_PPC64_PLTGOT16_HI", /* name */
1052 FALSE, /* partial_inplace */
1053 0, /* src_mask */
1054 0xffff, /* dst_mask */
1055 FALSE), /* pcrel_offset */
1056
1057 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1058 1 if the contents of the low 16 bits, treated as a signed number,
1059 is negative. */
1060 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1061 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1062 16, /* rightshift */
1063 1, /* size (0 = byte, 1 = short, 2 = long) */
1064 16, /* bitsize */
1065 FALSE, /* pc_relative */
1066 0, /* bitpos */
1067 complain_overflow_dont,/* complain_on_overflow */
1068 ppc64_elf_unhandled_reloc, /* special_function */
1069 "R_PPC64_PLTGOT16_HA", /* name */
1070 FALSE, /* partial_inplace */
1071 0, /* src_mask */
1072 0xffff, /* dst_mask */
1073 FALSE), /* pcrel_offset */
1074
1075 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1076 HOWTO (R_PPC64_ADDR16_DS, /* type */
1077 0, /* rightshift */
1078 1, /* size (0 = byte, 1 = short, 2 = long) */
1079 16, /* bitsize */
1080 FALSE, /* pc_relative */
1081 0, /* bitpos */
1082 complain_overflow_bitfield, /* complain_on_overflow */
1083 bfd_elf_generic_reloc, /* special_function */
1084 "R_PPC64_ADDR16_DS", /* name */
1085 FALSE, /* partial_inplace */
1086 0, /* src_mask */
1087 0xfffc, /* dst_mask */
1088 FALSE), /* pcrel_offset */
1089
1090 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1091 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1092 0, /* rightshift */
1093 1, /* size (0 = byte, 1 = short, 2 = long) */
1094 16, /* bitsize */
1095 FALSE, /* pc_relative */
1096 0, /* bitpos */
1097 complain_overflow_dont,/* complain_on_overflow */
1098 bfd_elf_generic_reloc, /* special_function */
1099 "R_PPC64_ADDR16_LO_DS",/* name */
1100 FALSE, /* partial_inplace */
1101 0, /* src_mask */
1102 0xfffc, /* dst_mask */
1103 FALSE), /* pcrel_offset */
1104
1105 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1106 HOWTO (R_PPC64_GOT16_DS, /* type */
1107 0, /* rightshift */
1108 1, /* size (0 = byte, 1 = short, 2 = long) */
1109 16, /* bitsize */
1110 FALSE, /* pc_relative */
1111 0, /* bitpos */
1112 complain_overflow_signed, /* complain_on_overflow */
1113 ppc64_elf_unhandled_reloc, /* special_function */
1114 "R_PPC64_GOT16_DS", /* name */
1115 FALSE, /* partial_inplace */
1116 0, /* src_mask */
1117 0xfffc, /* dst_mask */
1118 FALSE), /* pcrel_offset */
1119
1120 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1121 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1122 0, /* rightshift */
1123 1, /* size (0 = byte, 1 = short, 2 = long) */
1124 16, /* bitsize */
1125 FALSE, /* pc_relative */
1126 0, /* bitpos */
1127 complain_overflow_dont, /* complain_on_overflow */
1128 ppc64_elf_unhandled_reloc, /* special_function */
1129 "R_PPC64_GOT16_LO_DS", /* name */
1130 FALSE, /* partial_inplace */
1131 0, /* src_mask */
1132 0xfffc, /* dst_mask */
1133 FALSE), /* pcrel_offset */
1134
1135 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1136 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1137 0, /* rightshift */
1138 1, /* size (0 = byte, 1 = short, 2 = long) */
1139 16, /* bitsize */
1140 FALSE, /* pc_relative */
1141 0, /* bitpos */
1142 complain_overflow_dont, /* complain_on_overflow */
1143 ppc64_elf_unhandled_reloc, /* special_function */
1144 "R_PPC64_PLT16_LO_DS", /* name */
1145 FALSE, /* partial_inplace */
1146 0, /* src_mask */
1147 0xfffc, /* dst_mask */
1148 FALSE), /* pcrel_offset */
1149
1150 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1151 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1152 0, /* rightshift */
1153 1, /* size (0 = byte, 1 = short, 2 = long) */
1154 16, /* bitsize */
1155 FALSE, /* pc_relative */
1156 0, /* bitpos */
1157 complain_overflow_bitfield, /* complain_on_overflow */
1158 ppc64_elf_sectoff_reloc, /* special_function */
1159 "R_PPC64_SECTOFF_DS", /* name */
1160 FALSE, /* partial_inplace */
1161 0, /* src_mask */
1162 0xfffc, /* dst_mask */
1163 FALSE), /* pcrel_offset */
1164
1165 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1166 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1167 0, /* rightshift */
1168 1, /* size (0 = byte, 1 = short, 2 = long) */
1169 16, /* bitsize */
1170 FALSE, /* pc_relative */
1171 0, /* bitpos */
1172 complain_overflow_dont, /* complain_on_overflow */
1173 ppc64_elf_sectoff_reloc, /* special_function */
1174 "R_PPC64_SECTOFF_LO_DS",/* name */
1175 FALSE, /* partial_inplace */
1176 0, /* src_mask */
1177 0xfffc, /* dst_mask */
1178 FALSE), /* pcrel_offset */
1179
1180 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1181 HOWTO (R_PPC64_TOC16_DS, /* type */
1182 0, /* rightshift */
1183 1, /* size (0 = byte, 1 = short, 2 = long) */
1184 16, /* bitsize */
1185 FALSE, /* pc_relative */
1186 0, /* bitpos */
1187 complain_overflow_signed, /* complain_on_overflow */
1188 ppc64_elf_toc_reloc, /* special_function */
1189 "R_PPC64_TOC16_DS", /* name */
1190 FALSE, /* partial_inplace */
1191 0, /* src_mask */
1192 0xfffc, /* dst_mask */
1193 FALSE), /* pcrel_offset */
1194
1195 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1196 HOWTO (R_PPC64_TOC16_LO_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_dont, /* complain_on_overflow */
1203 ppc64_elf_toc_reloc, /* special_function */
1204 "R_PPC64_TOC16_LO_DS", /* name */
1205 FALSE, /* partial_inplace */
1206 0, /* src_mask */
1207 0xfffc, /* dst_mask */
1208 FALSE), /* pcrel_offset */
1209
1210 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1211 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1212 HOWTO (R_PPC64_PLTGOT16_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_signed, /* complain_on_overflow */
1219 ppc64_elf_unhandled_reloc, /* special_function */
1220 "R_PPC64_PLTGOT16_DS", /* name */
1221 FALSE, /* partial_inplace */
1222 0, /* src_mask */
1223 0xfffc, /* dst_mask */
1224 FALSE), /* pcrel_offset */
1225
1226 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1227 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1228 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1229 0, /* rightshift */
1230 1, /* size (0 = byte, 1 = short, 2 = long) */
1231 16, /* bitsize */
1232 FALSE, /* pc_relative */
1233 0, /* bitpos */
1234 complain_overflow_dont, /* complain_on_overflow */
1235 ppc64_elf_unhandled_reloc, /* special_function */
1236 "R_PPC64_PLTGOT16_LO_DS",/* name */
1237 FALSE, /* partial_inplace */
1238 0, /* src_mask */
1239 0xfffc, /* dst_mask */
1240 FALSE), /* pcrel_offset */
1241
1242 /* Marker relocs for TLS. */
1243 HOWTO (R_PPC64_TLS,
1244 0, /* rightshift */
1245 2, /* size (0 = byte, 1 = short, 2 = long) */
1246 32, /* bitsize */
1247 FALSE, /* pc_relative */
1248 0, /* bitpos */
1249 complain_overflow_dont, /* complain_on_overflow */
1250 bfd_elf_generic_reloc, /* special_function */
1251 "R_PPC64_TLS", /* name */
1252 FALSE, /* partial_inplace */
1253 0, /* src_mask */
1254 0, /* dst_mask */
1255 FALSE), /* pcrel_offset */
1256
1257 HOWTO (R_PPC64_TLSGD,
1258 0, /* rightshift */
1259 2, /* size (0 = byte, 1 = short, 2 = long) */
1260 32, /* bitsize */
1261 FALSE, /* pc_relative */
1262 0, /* bitpos */
1263 complain_overflow_dont, /* complain_on_overflow */
1264 bfd_elf_generic_reloc, /* special_function */
1265 "R_PPC64_TLSGD", /* name */
1266 FALSE, /* partial_inplace */
1267 0, /* src_mask */
1268 0, /* dst_mask */
1269 FALSE), /* pcrel_offset */
1270
1271 HOWTO (R_PPC64_TLSLD,
1272 0, /* rightshift */
1273 2, /* size (0 = byte, 1 = short, 2 = long) */
1274 32, /* bitsize */
1275 FALSE, /* pc_relative */
1276 0, /* bitpos */
1277 complain_overflow_dont, /* complain_on_overflow */
1278 bfd_elf_generic_reloc, /* special_function */
1279 "R_PPC64_TLSLD", /* name */
1280 FALSE, /* partial_inplace */
1281 0, /* src_mask */
1282 0, /* dst_mask */
1283 FALSE), /* pcrel_offset */
1284
1285 /* Computes the load module index of the load module that contains the
1286 definition of its TLS sym. */
1287 HOWTO (R_PPC64_DTPMOD64,
1288 0, /* rightshift */
1289 4, /* size (0 = byte, 1 = short, 2 = long) */
1290 64, /* bitsize */
1291 FALSE, /* pc_relative */
1292 0, /* bitpos */
1293 complain_overflow_dont, /* complain_on_overflow */
1294 ppc64_elf_unhandled_reloc, /* special_function */
1295 "R_PPC64_DTPMOD64", /* name */
1296 FALSE, /* partial_inplace */
1297 0, /* src_mask */
1298 ONES (64), /* dst_mask */
1299 FALSE), /* pcrel_offset */
1300
1301 /* Computes a dtv-relative displacement, the difference between the value
1302 of sym+add and the base address of the thread-local storage block that
1303 contains the definition of sym, minus 0x8000. */
1304 HOWTO (R_PPC64_DTPREL64,
1305 0, /* rightshift */
1306 4, /* size (0 = byte, 1 = short, 2 = long) */
1307 64, /* bitsize */
1308 FALSE, /* pc_relative */
1309 0, /* bitpos */
1310 complain_overflow_dont, /* complain_on_overflow */
1311 ppc64_elf_unhandled_reloc, /* special_function */
1312 "R_PPC64_DTPREL64", /* name */
1313 FALSE, /* partial_inplace */
1314 0, /* src_mask */
1315 ONES (64), /* dst_mask */
1316 FALSE), /* pcrel_offset */
1317
1318 /* A 16 bit dtprel reloc. */
1319 HOWTO (R_PPC64_DTPREL16,
1320 0, /* rightshift */
1321 1, /* size (0 = byte, 1 = short, 2 = long) */
1322 16, /* bitsize */
1323 FALSE, /* pc_relative */
1324 0, /* bitpos */
1325 complain_overflow_signed, /* complain_on_overflow */
1326 ppc64_elf_unhandled_reloc, /* special_function */
1327 "R_PPC64_DTPREL16", /* name */
1328 FALSE, /* partial_inplace */
1329 0, /* src_mask */
1330 0xffff, /* dst_mask */
1331 FALSE), /* pcrel_offset */
1332
1333 /* Like DTPREL16, but no overflow. */
1334 HOWTO (R_PPC64_DTPREL16_LO,
1335 0, /* rightshift */
1336 1, /* size (0 = byte, 1 = short, 2 = long) */
1337 16, /* bitsize */
1338 FALSE, /* pc_relative */
1339 0, /* bitpos */
1340 complain_overflow_dont, /* complain_on_overflow */
1341 ppc64_elf_unhandled_reloc, /* special_function */
1342 "R_PPC64_DTPREL16_LO", /* name */
1343 FALSE, /* partial_inplace */
1344 0, /* src_mask */
1345 0xffff, /* dst_mask */
1346 FALSE), /* pcrel_offset */
1347
1348 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1349 HOWTO (R_PPC64_DTPREL16_HI,
1350 16, /* rightshift */
1351 1, /* size (0 = byte, 1 = short, 2 = long) */
1352 16, /* bitsize */
1353 FALSE, /* pc_relative */
1354 0, /* bitpos */
1355 complain_overflow_dont, /* complain_on_overflow */
1356 ppc64_elf_unhandled_reloc, /* special_function */
1357 "R_PPC64_DTPREL16_HI", /* name */
1358 FALSE, /* partial_inplace */
1359 0, /* src_mask */
1360 0xffff, /* dst_mask */
1361 FALSE), /* pcrel_offset */
1362
1363 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1364 HOWTO (R_PPC64_DTPREL16_HA,
1365 16, /* rightshift */
1366 1, /* size (0 = byte, 1 = short, 2 = long) */
1367 16, /* bitsize */
1368 FALSE, /* pc_relative */
1369 0, /* bitpos */
1370 complain_overflow_dont, /* complain_on_overflow */
1371 ppc64_elf_unhandled_reloc, /* special_function */
1372 "R_PPC64_DTPREL16_HA", /* name */
1373 FALSE, /* partial_inplace */
1374 0, /* src_mask */
1375 0xffff, /* dst_mask */
1376 FALSE), /* pcrel_offset */
1377
1378 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1379 HOWTO (R_PPC64_DTPREL16_HIGHER,
1380 32, /* rightshift */
1381 1, /* size (0 = byte, 1 = short, 2 = long) */
1382 16, /* bitsize */
1383 FALSE, /* pc_relative */
1384 0, /* bitpos */
1385 complain_overflow_dont, /* complain_on_overflow */
1386 ppc64_elf_unhandled_reloc, /* special_function */
1387 "R_PPC64_DTPREL16_HIGHER", /* name */
1388 FALSE, /* partial_inplace */
1389 0, /* src_mask */
1390 0xffff, /* dst_mask */
1391 FALSE), /* pcrel_offset */
1392
1393 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1394 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1395 32, /* rightshift */
1396 1, /* size (0 = byte, 1 = short, 2 = long) */
1397 16, /* bitsize */
1398 FALSE, /* pc_relative */
1399 0, /* bitpos */
1400 complain_overflow_dont, /* complain_on_overflow */
1401 ppc64_elf_unhandled_reloc, /* special_function */
1402 "R_PPC64_DTPREL16_HIGHERA", /* name */
1403 FALSE, /* partial_inplace */
1404 0, /* src_mask */
1405 0xffff, /* dst_mask */
1406 FALSE), /* pcrel_offset */
1407
1408 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1409 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1410 48, /* rightshift */
1411 1, /* size (0 = byte, 1 = short, 2 = long) */
1412 16, /* bitsize */
1413 FALSE, /* pc_relative */
1414 0, /* bitpos */
1415 complain_overflow_dont, /* complain_on_overflow */
1416 ppc64_elf_unhandled_reloc, /* special_function */
1417 "R_PPC64_DTPREL16_HIGHEST", /* name */
1418 FALSE, /* partial_inplace */
1419 0, /* src_mask */
1420 0xffff, /* dst_mask */
1421 FALSE), /* pcrel_offset */
1422
1423 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1424 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1425 48, /* rightshift */
1426 1, /* size (0 = byte, 1 = short, 2 = long) */
1427 16, /* bitsize */
1428 FALSE, /* pc_relative */
1429 0, /* bitpos */
1430 complain_overflow_dont, /* complain_on_overflow */
1431 ppc64_elf_unhandled_reloc, /* special_function */
1432 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1433 FALSE, /* partial_inplace */
1434 0, /* src_mask */
1435 0xffff, /* dst_mask */
1436 FALSE), /* pcrel_offset */
1437
1438 /* Like DTPREL16, but for insns with a DS field. */
1439 HOWTO (R_PPC64_DTPREL16_DS,
1440 0, /* rightshift */
1441 1, /* size (0 = byte, 1 = short, 2 = long) */
1442 16, /* bitsize */
1443 FALSE, /* pc_relative */
1444 0, /* bitpos */
1445 complain_overflow_signed, /* complain_on_overflow */
1446 ppc64_elf_unhandled_reloc, /* special_function */
1447 "R_PPC64_DTPREL16_DS", /* name */
1448 FALSE, /* partial_inplace */
1449 0, /* src_mask */
1450 0xfffc, /* dst_mask */
1451 FALSE), /* pcrel_offset */
1452
1453 /* Like DTPREL16_DS, but no overflow. */
1454 HOWTO (R_PPC64_DTPREL16_LO_DS,
1455 0, /* rightshift */
1456 1, /* size (0 = byte, 1 = short, 2 = long) */
1457 16, /* bitsize */
1458 FALSE, /* pc_relative */
1459 0, /* bitpos */
1460 complain_overflow_dont, /* complain_on_overflow */
1461 ppc64_elf_unhandled_reloc, /* special_function */
1462 "R_PPC64_DTPREL16_LO_DS", /* name */
1463 FALSE, /* partial_inplace */
1464 0, /* src_mask */
1465 0xfffc, /* dst_mask */
1466 FALSE), /* pcrel_offset */
1467
1468 /* Computes a tp-relative displacement, the difference between the value of
1469 sym+add and the value of the thread pointer (r13). */
1470 HOWTO (R_PPC64_TPREL64,
1471 0, /* rightshift */
1472 4, /* size (0 = byte, 1 = short, 2 = long) */
1473 64, /* bitsize */
1474 FALSE, /* pc_relative */
1475 0, /* bitpos */
1476 complain_overflow_dont, /* complain_on_overflow */
1477 ppc64_elf_unhandled_reloc, /* special_function */
1478 "R_PPC64_TPREL64", /* name */
1479 FALSE, /* partial_inplace */
1480 0, /* src_mask */
1481 ONES (64), /* dst_mask */
1482 FALSE), /* pcrel_offset */
1483
1484 /* A 16 bit tprel reloc. */
1485 HOWTO (R_PPC64_TPREL16,
1486 0, /* rightshift */
1487 1, /* size (0 = byte, 1 = short, 2 = long) */
1488 16, /* bitsize */
1489 FALSE, /* pc_relative */
1490 0, /* bitpos */
1491 complain_overflow_signed, /* complain_on_overflow */
1492 ppc64_elf_unhandled_reloc, /* special_function */
1493 "R_PPC64_TPREL16", /* name */
1494 FALSE, /* partial_inplace */
1495 0, /* src_mask */
1496 0xffff, /* dst_mask */
1497 FALSE), /* pcrel_offset */
1498
1499 /* Like TPREL16, but no overflow. */
1500 HOWTO (R_PPC64_TPREL16_LO,
1501 0, /* rightshift */
1502 1, /* size (0 = byte, 1 = short, 2 = long) */
1503 16, /* bitsize */
1504 FALSE, /* pc_relative */
1505 0, /* bitpos */
1506 complain_overflow_dont, /* complain_on_overflow */
1507 ppc64_elf_unhandled_reloc, /* special_function */
1508 "R_PPC64_TPREL16_LO", /* name */
1509 FALSE, /* partial_inplace */
1510 0, /* src_mask */
1511 0xffff, /* dst_mask */
1512 FALSE), /* pcrel_offset */
1513
1514 /* Like TPREL16_LO, but next higher group of 16 bits. */
1515 HOWTO (R_PPC64_TPREL16_HI,
1516 16, /* rightshift */
1517 1, /* size (0 = byte, 1 = short, 2 = long) */
1518 16, /* bitsize */
1519 FALSE, /* pc_relative */
1520 0, /* bitpos */
1521 complain_overflow_dont, /* complain_on_overflow */
1522 ppc64_elf_unhandled_reloc, /* special_function */
1523 "R_PPC64_TPREL16_HI", /* name */
1524 FALSE, /* partial_inplace */
1525 0, /* src_mask */
1526 0xffff, /* dst_mask */
1527 FALSE), /* pcrel_offset */
1528
1529 /* Like TPREL16_HI, but adjust for low 16 bits. */
1530 HOWTO (R_PPC64_TPREL16_HA,
1531 16, /* rightshift */
1532 1, /* size (0 = byte, 1 = short, 2 = long) */
1533 16, /* bitsize */
1534 FALSE, /* pc_relative */
1535 0, /* bitpos */
1536 complain_overflow_dont, /* complain_on_overflow */
1537 ppc64_elf_unhandled_reloc, /* special_function */
1538 "R_PPC64_TPREL16_HA", /* name */
1539 FALSE, /* partial_inplace */
1540 0, /* src_mask */
1541 0xffff, /* dst_mask */
1542 FALSE), /* pcrel_offset */
1543
1544 /* Like TPREL16_HI, but next higher group of 16 bits. */
1545 HOWTO (R_PPC64_TPREL16_HIGHER,
1546 32, /* rightshift */
1547 1, /* size (0 = byte, 1 = short, 2 = long) */
1548 16, /* bitsize */
1549 FALSE, /* pc_relative */
1550 0, /* bitpos */
1551 complain_overflow_dont, /* complain_on_overflow */
1552 ppc64_elf_unhandled_reloc, /* special_function */
1553 "R_PPC64_TPREL16_HIGHER", /* name */
1554 FALSE, /* partial_inplace */
1555 0, /* src_mask */
1556 0xffff, /* dst_mask */
1557 FALSE), /* pcrel_offset */
1558
1559 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1560 HOWTO (R_PPC64_TPREL16_HIGHERA,
1561 32, /* rightshift */
1562 1, /* size (0 = byte, 1 = short, 2 = long) */
1563 16, /* bitsize */
1564 FALSE, /* pc_relative */
1565 0, /* bitpos */
1566 complain_overflow_dont, /* complain_on_overflow */
1567 ppc64_elf_unhandled_reloc, /* special_function */
1568 "R_PPC64_TPREL16_HIGHERA", /* name */
1569 FALSE, /* partial_inplace */
1570 0, /* src_mask */
1571 0xffff, /* dst_mask */
1572 FALSE), /* pcrel_offset */
1573
1574 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1575 HOWTO (R_PPC64_TPREL16_HIGHEST,
1576 48, /* rightshift */
1577 1, /* size (0 = byte, 1 = short, 2 = long) */
1578 16, /* bitsize */
1579 FALSE, /* pc_relative */
1580 0, /* bitpos */
1581 complain_overflow_dont, /* complain_on_overflow */
1582 ppc64_elf_unhandled_reloc, /* special_function */
1583 "R_PPC64_TPREL16_HIGHEST", /* name */
1584 FALSE, /* partial_inplace */
1585 0, /* src_mask */
1586 0xffff, /* dst_mask */
1587 FALSE), /* pcrel_offset */
1588
1589 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1590 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1591 48, /* rightshift */
1592 1, /* size (0 = byte, 1 = short, 2 = long) */
1593 16, /* bitsize */
1594 FALSE, /* pc_relative */
1595 0, /* bitpos */
1596 complain_overflow_dont, /* complain_on_overflow */
1597 ppc64_elf_unhandled_reloc, /* special_function */
1598 "R_PPC64_TPREL16_HIGHESTA", /* name */
1599 FALSE, /* partial_inplace */
1600 0, /* src_mask */
1601 0xffff, /* dst_mask */
1602 FALSE), /* pcrel_offset */
1603
1604 /* Like TPREL16, but for insns with a DS field. */
1605 HOWTO (R_PPC64_TPREL16_DS,
1606 0, /* rightshift */
1607 1, /* size (0 = byte, 1 = short, 2 = long) */
1608 16, /* bitsize */
1609 FALSE, /* pc_relative */
1610 0, /* bitpos */
1611 complain_overflow_signed, /* complain_on_overflow */
1612 ppc64_elf_unhandled_reloc, /* special_function */
1613 "R_PPC64_TPREL16_DS", /* name */
1614 FALSE, /* partial_inplace */
1615 0, /* src_mask */
1616 0xfffc, /* dst_mask */
1617 FALSE), /* pcrel_offset */
1618
1619 /* Like TPREL16_DS, but no overflow. */
1620 HOWTO (R_PPC64_TPREL16_LO_DS,
1621 0, /* rightshift */
1622 1, /* size (0 = byte, 1 = short, 2 = long) */
1623 16, /* bitsize */
1624 FALSE, /* pc_relative */
1625 0, /* bitpos */
1626 complain_overflow_dont, /* complain_on_overflow */
1627 ppc64_elf_unhandled_reloc, /* special_function */
1628 "R_PPC64_TPREL16_LO_DS", /* name */
1629 FALSE, /* partial_inplace */
1630 0, /* src_mask */
1631 0xfffc, /* dst_mask */
1632 FALSE), /* pcrel_offset */
1633
1634 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1635 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1636 to the first entry relative to the TOC base (r2). */
1637 HOWTO (R_PPC64_GOT_TLSGD16,
1638 0, /* rightshift */
1639 1, /* size (0 = byte, 1 = short, 2 = long) */
1640 16, /* bitsize */
1641 FALSE, /* pc_relative */
1642 0, /* bitpos */
1643 complain_overflow_signed, /* complain_on_overflow */
1644 ppc64_elf_unhandled_reloc, /* special_function */
1645 "R_PPC64_GOT_TLSGD16", /* name */
1646 FALSE, /* partial_inplace */
1647 0, /* src_mask */
1648 0xffff, /* dst_mask */
1649 FALSE), /* pcrel_offset */
1650
1651 /* Like GOT_TLSGD16, but no overflow. */
1652 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1653 0, /* rightshift */
1654 1, /* size (0 = byte, 1 = short, 2 = long) */
1655 16, /* bitsize */
1656 FALSE, /* pc_relative */
1657 0, /* bitpos */
1658 complain_overflow_dont, /* complain_on_overflow */
1659 ppc64_elf_unhandled_reloc, /* special_function */
1660 "R_PPC64_GOT_TLSGD16_LO", /* name */
1661 FALSE, /* partial_inplace */
1662 0, /* src_mask */
1663 0xffff, /* dst_mask */
1664 FALSE), /* pcrel_offset */
1665
1666 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1667 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1668 16, /* rightshift */
1669 1, /* size (0 = byte, 1 = short, 2 = long) */
1670 16, /* bitsize */
1671 FALSE, /* pc_relative */
1672 0, /* bitpos */
1673 complain_overflow_dont, /* complain_on_overflow */
1674 ppc64_elf_unhandled_reloc, /* special_function */
1675 "R_PPC64_GOT_TLSGD16_HI", /* name */
1676 FALSE, /* partial_inplace */
1677 0, /* src_mask */
1678 0xffff, /* dst_mask */
1679 FALSE), /* pcrel_offset */
1680
1681 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1682 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1683 16, /* rightshift */
1684 1, /* size (0 = byte, 1 = short, 2 = long) */
1685 16, /* bitsize */
1686 FALSE, /* pc_relative */
1687 0, /* bitpos */
1688 complain_overflow_dont, /* complain_on_overflow */
1689 ppc64_elf_unhandled_reloc, /* special_function */
1690 "R_PPC64_GOT_TLSGD16_HA", /* name */
1691 FALSE, /* partial_inplace */
1692 0, /* src_mask */
1693 0xffff, /* dst_mask */
1694 FALSE), /* pcrel_offset */
1695
1696 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1697 with values (sym+add)@dtpmod and zero, and computes the offset to the
1698 first entry relative to the TOC base (r2). */
1699 HOWTO (R_PPC64_GOT_TLSLD16,
1700 0, /* rightshift */
1701 1, /* size (0 = byte, 1 = short, 2 = long) */
1702 16, /* bitsize */
1703 FALSE, /* pc_relative */
1704 0, /* bitpos */
1705 complain_overflow_signed, /* complain_on_overflow */
1706 ppc64_elf_unhandled_reloc, /* special_function */
1707 "R_PPC64_GOT_TLSLD16", /* name */
1708 FALSE, /* partial_inplace */
1709 0, /* src_mask */
1710 0xffff, /* dst_mask */
1711 FALSE), /* pcrel_offset */
1712
1713 /* Like GOT_TLSLD16, but no overflow. */
1714 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1715 0, /* rightshift */
1716 1, /* size (0 = byte, 1 = short, 2 = long) */
1717 16, /* bitsize */
1718 FALSE, /* pc_relative */
1719 0, /* bitpos */
1720 complain_overflow_dont, /* complain_on_overflow */
1721 ppc64_elf_unhandled_reloc, /* special_function */
1722 "R_PPC64_GOT_TLSLD16_LO", /* name */
1723 FALSE, /* partial_inplace */
1724 0, /* src_mask */
1725 0xffff, /* dst_mask */
1726 FALSE), /* pcrel_offset */
1727
1728 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1729 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1730 16, /* rightshift */
1731 1, /* size (0 = byte, 1 = short, 2 = long) */
1732 16, /* bitsize */
1733 FALSE, /* pc_relative */
1734 0, /* bitpos */
1735 complain_overflow_dont, /* complain_on_overflow */
1736 ppc64_elf_unhandled_reloc, /* special_function */
1737 "R_PPC64_GOT_TLSLD16_HI", /* name */
1738 FALSE, /* partial_inplace */
1739 0, /* src_mask */
1740 0xffff, /* dst_mask */
1741 FALSE), /* pcrel_offset */
1742
1743 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1744 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1745 16, /* rightshift */
1746 1, /* size (0 = byte, 1 = short, 2 = long) */
1747 16, /* bitsize */
1748 FALSE, /* pc_relative */
1749 0, /* bitpos */
1750 complain_overflow_dont, /* complain_on_overflow */
1751 ppc64_elf_unhandled_reloc, /* special_function */
1752 "R_PPC64_GOT_TLSLD16_HA", /* name */
1753 FALSE, /* partial_inplace */
1754 0, /* src_mask */
1755 0xffff, /* dst_mask */
1756 FALSE), /* pcrel_offset */
1757
1758 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1759 the offset to the entry relative to the TOC base (r2). */
1760 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1761 0, /* rightshift */
1762 1, /* size (0 = byte, 1 = short, 2 = long) */
1763 16, /* bitsize */
1764 FALSE, /* pc_relative */
1765 0, /* bitpos */
1766 complain_overflow_signed, /* complain_on_overflow */
1767 ppc64_elf_unhandled_reloc, /* special_function */
1768 "R_PPC64_GOT_DTPREL16_DS", /* name */
1769 FALSE, /* partial_inplace */
1770 0, /* src_mask */
1771 0xfffc, /* dst_mask */
1772 FALSE), /* pcrel_offset */
1773
1774 /* Like GOT_DTPREL16_DS, but no overflow. */
1775 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1776 0, /* rightshift */
1777 1, /* size (0 = byte, 1 = short, 2 = long) */
1778 16, /* bitsize */
1779 FALSE, /* pc_relative */
1780 0, /* bitpos */
1781 complain_overflow_dont, /* complain_on_overflow */
1782 ppc64_elf_unhandled_reloc, /* special_function */
1783 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1784 FALSE, /* partial_inplace */
1785 0, /* src_mask */
1786 0xfffc, /* dst_mask */
1787 FALSE), /* pcrel_offset */
1788
1789 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1790 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1791 16, /* rightshift */
1792 1, /* size (0 = byte, 1 = short, 2 = long) */
1793 16, /* bitsize */
1794 FALSE, /* pc_relative */
1795 0, /* bitpos */
1796 complain_overflow_dont, /* complain_on_overflow */
1797 ppc64_elf_unhandled_reloc, /* special_function */
1798 "R_PPC64_GOT_DTPREL16_HI", /* name */
1799 FALSE, /* partial_inplace */
1800 0, /* src_mask */
1801 0xffff, /* dst_mask */
1802 FALSE), /* pcrel_offset */
1803
1804 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1805 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1806 16, /* rightshift */
1807 1, /* size (0 = byte, 1 = short, 2 = long) */
1808 16, /* bitsize */
1809 FALSE, /* pc_relative */
1810 0, /* bitpos */
1811 complain_overflow_dont, /* complain_on_overflow */
1812 ppc64_elf_unhandled_reloc, /* special_function */
1813 "R_PPC64_GOT_DTPREL16_HA", /* name */
1814 FALSE, /* partial_inplace */
1815 0, /* src_mask */
1816 0xffff, /* dst_mask */
1817 FALSE), /* pcrel_offset */
1818
1819 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1820 offset to the entry relative to the TOC base (r2). */
1821 HOWTO (R_PPC64_GOT_TPREL16_DS,
1822 0, /* rightshift */
1823 1, /* size (0 = byte, 1 = short, 2 = long) */
1824 16, /* bitsize */
1825 FALSE, /* pc_relative */
1826 0, /* bitpos */
1827 complain_overflow_signed, /* complain_on_overflow */
1828 ppc64_elf_unhandled_reloc, /* special_function */
1829 "R_PPC64_GOT_TPREL16_DS", /* name */
1830 FALSE, /* partial_inplace */
1831 0, /* src_mask */
1832 0xfffc, /* dst_mask */
1833 FALSE), /* pcrel_offset */
1834
1835 /* Like GOT_TPREL16_DS, but no overflow. */
1836 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1837 0, /* rightshift */
1838 1, /* size (0 = byte, 1 = short, 2 = long) */
1839 16, /* bitsize */
1840 FALSE, /* pc_relative */
1841 0, /* bitpos */
1842 complain_overflow_dont, /* complain_on_overflow */
1843 ppc64_elf_unhandled_reloc, /* special_function */
1844 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1845 FALSE, /* partial_inplace */
1846 0, /* src_mask */
1847 0xfffc, /* dst_mask */
1848 FALSE), /* pcrel_offset */
1849
1850 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1851 HOWTO (R_PPC64_GOT_TPREL16_HI,
1852 16, /* rightshift */
1853 1, /* size (0 = byte, 1 = short, 2 = long) */
1854 16, /* bitsize */
1855 FALSE, /* pc_relative */
1856 0, /* bitpos */
1857 complain_overflow_dont, /* complain_on_overflow */
1858 ppc64_elf_unhandled_reloc, /* special_function */
1859 "R_PPC64_GOT_TPREL16_HI", /* name */
1860 FALSE, /* partial_inplace */
1861 0, /* src_mask */
1862 0xffff, /* dst_mask */
1863 FALSE), /* pcrel_offset */
1864
1865 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1866 HOWTO (R_PPC64_GOT_TPREL16_HA,
1867 16, /* rightshift */
1868 1, /* size (0 = byte, 1 = short, 2 = long) */
1869 16, /* bitsize */
1870 FALSE, /* pc_relative */
1871 0, /* bitpos */
1872 complain_overflow_dont, /* complain_on_overflow */
1873 ppc64_elf_unhandled_reloc, /* special_function */
1874 "R_PPC64_GOT_TPREL16_HA", /* name */
1875 FALSE, /* partial_inplace */
1876 0, /* src_mask */
1877 0xffff, /* dst_mask */
1878 FALSE), /* pcrel_offset */
1879
1880 HOWTO (R_PPC64_JMP_IREL, /* type */
1881 0, /* rightshift */
1882 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1883 0, /* bitsize */
1884 FALSE, /* pc_relative */
1885 0, /* bitpos */
1886 complain_overflow_dont, /* complain_on_overflow */
1887 ppc64_elf_unhandled_reloc, /* special_function */
1888 "R_PPC64_JMP_IREL", /* name */
1889 FALSE, /* partial_inplace */
1890 0, /* src_mask */
1891 0, /* dst_mask */
1892 FALSE), /* pcrel_offset */
1893
1894 HOWTO (R_PPC64_IRELATIVE, /* type */
1895 0, /* rightshift */
1896 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1897 64, /* bitsize */
1898 FALSE, /* pc_relative */
1899 0, /* bitpos */
1900 complain_overflow_dont, /* complain_on_overflow */
1901 bfd_elf_generic_reloc, /* special_function */
1902 "R_PPC64_IRELATIVE", /* name */
1903 FALSE, /* partial_inplace */
1904 0, /* src_mask */
1905 ONES (64), /* dst_mask */
1906 FALSE), /* pcrel_offset */
1907
1908 /* A 16 bit relative relocation. */
1909 HOWTO (R_PPC64_REL16, /* type */
1910 0, /* rightshift */
1911 1, /* size (0 = byte, 1 = short, 2 = long) */
1912 16, /* bitsize */
1913 TRUE, /* pc_relative */
1914 0, /* bitpos */
1915 complain_overflow_bitfield, /* complain_on_overflow */
1916 bfd_elf_generic_reloc, /* special_function */
1917 "R_PPC64_REL16", /* name */
1918 FALSE, /* partial_inplace */
1919 0, /* src_mask */
1920 0xffff, /* dst_mask */
1921 TRUE), /* pcrel_offset */
1922
1923 /* A 16 bit relative relocation without overflow. */
1924 HOWTO (R_PPC64_REL16_LO, /* type */
1925 0, /* rightshift */
1926 1, /* size (0 = byte, 1 = short, 2 = long) */
1927 16, /* bitsize */
1928 TRUE, /* pc_relative */
1929 0, /* bitpos */
1930 complain_overflow_dont,/* complain_on_overflow */
1931 bfd_elf_generic_reloc, /* special_function */
1932 "R_PPC64_REL16_LO", /* name */
1933 FALSE, /* partial_inplace */
1934 0, /* src_mask */
1935 0xffff, /* dst_mask */
1936 TRUE), /* pcrel_offset */
1937
1938 /* The high order 16 bits of a relative address. */
1939 HOWTO (R_PPC64_REL16_HI, /* type */
1940 16, /* rightshift */
1941 1, /* size (0 = byte, 1 = short, 2 = long) */
1942 16, /* bitsize */
1943 TRUE, /* pc_relative */
1944 0, /* bitpos */
1945 complain_overflow_dont, /* complain_on_overflow */
1946 bfd_elf_generic_reloc, /* special_function */
1947 "R_PPC64_REL16_HI", /* name */
1948 FALSE, /* partial_inplace */
1949 0, /* src_mask */
1950 0xffff, /* dst_mask */
1951 TRUE), /* pcrel_offset */
1952
1953 /* The high order 16 bits of a relative address, plus 1 if the contents of
1954 the low 16 bits, treated as a signed number, is negative. */
1955 HOWTO (R_PPC64_REL16_HA, /* type */
1956 16, /* rightshift */
1957 1, /* size (0 = byte, 1 = short, 2 = long) */
1958 16, /* bitsize */
1959 TRUE, /* pc_relative */
1960 0, /* bitpos */
1961 complain_overflow_dont, /* complain_on_overflow */
1962 ppc64_elf_ha_reloc, /* special_function */
1963 "R_PPC64_REL16_HA", /* name */
1964 FALSE, /* partial_inplace */
1965 0, /* src_mask */
1966 0xffff, /* dst_mask */
1967 TRUE), /* pcrel_offset */
1968
1969 /* GNU extension to record C++ vtable hierarchy. */
1970 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1971 0, /* rightshift */
1972 0, /* size (0 = byte, 1 = short, 2 = long) */
1973 0, /* bitsize */
1974 FALSE, /* pc_relative */
1975 0, /* bitpos */
1976 complain_overflow_dont, /* complain_on_overflow */
1977 NULL, /* special_function */
1978 "R_PPC64_GNU_VTINHERIT", /* name */
1979 FALSE, /* partial_inplace */
1980 0, /* src_mask */
1981 0, /* dst_mask */
1982 FALSE), /* pcrel_offset */
1983
1984 /* GNU extension to record C++ vtable member usage. */
1985 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1986 0, /* rightshift */
1987 0, /* size (0 = byte, 1 = short, 2 = long) */
1988 0, /* bitsize */
1989 FALSE, /* pc_relative */
1990 0, /* bitpos */
1991 complain_overflow_dont, /* complain_on_overflow */
1992 NULL, /* special_function */
1993 "R_PPC64_GNU_VTENTRY", /* name */
1994 FALSE, /* partial_inplace */
1995 0, /* src_mask */
1996 0, /* dst_mask */
1997 FALSE), /* pcrel_offset */
1998 };
1999
2000 \f
2001 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2002 be done. */
2003
2004 static void
2005 ppc_howto_init (void)
2006 {
2007 unsigned int i, type;
2008
2009 for (i = 0;
2010 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2011 i++)
2012 {
2013 type = ppc64_elf_howto_raw[i].type;
2014 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2015 / sizeof (ppc64_elf_howto_table[0])));
2016 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2017 }
2018 }
2019
2020 static reloc_howto_type *
2021 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2022 bfd_reloc_code_real_type code)
2023 {
2024 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2025
2026 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2027 /* Initialize howto table if needed. */
2028 ppc_howto_init ();
2029
2030 switch (code)
2031 {
2032 default:
2033 return NULL;
2034
2035 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2036 break;
2037 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2038 break;
2039 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2040 break;
2041 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2042 break;
2043 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2044 break;
2045 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2046 break;
2047 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2048 break;
2049 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2050 break;
2051 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2052 break;
2053 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2054 break;
2055 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2056 break;
2057 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2058 break;
2059 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2060 break;
2061 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2062 break;
2063 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2064 break;
2065 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2066 break;
2067 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2068 break;
2069 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2070 break;
2071 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2072 break;
2073 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2074 break;
2075 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2076 break;
2077 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2078 break;
2079 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2080 break;
2081 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2082 break;
2083 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2084 break;
2085 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2086 break;
2087 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2088 break;
2089 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2090 break;
2091 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2092 break;
2093 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2094 break;
2095 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2096 break;
2097 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2098 break;
2099 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2100 break;
2101 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2102 break;
2103 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2104 break;
2105 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2106 break;
2107 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2108 break;
2109 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2110 break;
2111 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2112 break;
2113 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2114 break;
2115 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2116 break;
2117 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2118 break;
2119 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2120 break;
2121 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2122 break;
2123 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2124 break;
2125 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2126 break;
2127 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2128 break;
2129 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2130 break;
2131 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2132 break;
2133 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2134 break;
2135 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2136 break;
2137 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2138 break;
2139 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2140 break;
2141 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2142 break;
2143 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2144 break;
2145 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2146 break;
2147 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2148 break;
2149 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2150 break;
2151 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2152 break;
2153 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2154 break;
2155 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2156 break;
2157 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2158 break;
2159 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2160 break;
2161 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2162 break;
2163 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2164 break;
2165 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2166 break;
2167 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2168 break;
2169 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2170 break;
2171 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2172 break;
2173 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2174 break;
2175 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2176 break;
2177 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2178 break;
2179 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2180 break;
2181 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2182 break;
2183 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2184 break;
2185 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2186 break;
2187 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2188 break;
2189 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2190 break;
2191 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2192 break;
2193 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2194 break;
2195 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2196 break;
2197 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2198 break;
2199 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2200 break;
2201 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2202 break;
2203 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2204 break;
2205 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2206 break;
2207 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2208 break;
2209 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2210 break;
2211 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2212 break;
2213 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2214 break;
2215 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2216 break;
2217 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2218 break;
2219 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2220 break;
2221 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2222 break;
2223 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2224 break;
2225 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2226 break;
2227 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2228 break;
2229 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2230 break;
2231 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2232 break;
2233 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2234 break;
2235 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2236 break;
2237 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2238 break;
2239 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2240 break;
2241 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2242 break;
2243 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2244 break;
2245 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2246 break;
2247 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2248 break;
2249 }
2250
2251 return ppc64_elf_howto_table[r];
2252 };
2253
2254 static reloc_howto_type *
2255 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2256 const char *r_name)
2257 {
2258 unsigned int i;
2259
2260 for (i = 0;
2261 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2262 i++)
2263 if (ppc64_elf_howto_raw[i].name != NULL
2264 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2265 return &ppc64_elf_howto_raw[i];
2266
2267 return NULL;
2268 }
2269
2270 /* Set the howto pointer for a PowerPC ELF reloc. */
2271
2272 static void
2273 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2274 Elf_Internal_Rela *dst)
2275 {
2276 unsigned int type;
2277
2278 /* Initialize howto table if needed. */
2279 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2280 ppc_howto_init ();
2281
2282 type = ELF64_R_TYPE (dst->r_info);
2283 if (type >= (sizeof (ppc64_elf_howto_table)
2284 / sizeof (ppc64_elf_howto_table[0])))
2285 {
2286 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2287 abfd, (int) type);
2288 type = R_PPC64_NONE;
2289 }
2290 cache_ptr->howto = ppc64_elf_howto_table[type];
2291 }
2292
2293 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2294
2295 static bfd_reloc_status_type
2296 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2297 void *data, asection *input_section,
2298 bfd *output_bfd, char **error_message)
2299 {
2300 /* If this is a relocatable link (output_bfd test tells us), just
2301 call the generic function. Any adjustment will be done at final
2302 link time. */
2303 if (output_bfd != NULL)
2304 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2305 input_section, output_bfd, error_message);
2306
2307 /* Adjust the addend for sign extension of the low 16 bits.
2308 We won't actually be using the low 16 bits, so trashing them
2309 doesn't matter. */
2310 reloc_entry->addend += 0x8000;
2311 return bfd_reloc_continue;
2312 }
2313
2314 static bfd_reloc_status_type
2315 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2316 void *data, asection *input_section,
2317 bfd *output_bfd, char **error_message)
2318 {
2319 if (output_bfd != NULL)
2320 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2321 input_section, output_bfd, error_message);
2322
2323 if (strcmp (symbol->section->name, ".opd") == 0
2324 && (symbol->section->owner->flags & DYNAMIC) == 0)
2325 {
2326 bfd_vma dest = opd_entry_value (symbol->section,
2327 symbol->value + reloc_entry->addend,
2328 NULL, NULL);
2329 if (dest != (bfd_vma) -1)
2330 reloc_entry->addend = dest - (symbol->value
2331 + symbol->section->output_section->vma
2332 + symbol->section->output_offset);
2333 }
2334 return bfd_reloc_continue;
2335 }
2336
2337 static bfd_reloc_status_type
2338 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2339 void *data, asection *input_section,
2340 bfd *output_bfd, char **error_message)
2341 {
2342 long insn;
2343 enum elf_ppc64_reloc_type r_type;
2344 bfd_size_type octets;
2345 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
2346 bfd_boolean is_power4 = FALSE;
2347
2348 /* If this is a relocatable link (output_bfd test tells us), just
2349 call the generic function. Any adjustment will be done at final
2350 link time. */
2351 if (output_bfd != NULL)
2352 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2353 input_section, output_bfd, error_message);
2354
2355 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2356 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2357 insn &= ~(0x01 << 21);
2358 r_type = reloc_entry->howto->type;
2359 if (r_type == R_PPC64_ADDR14_BRTAKEN
2360 || r_type == R_PPC64_REL14_BRTAKEN)
2361 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2362
2363 if (is_power4)
2364 {
2365 /* Set 'a' bit. This is 0b00010 in BO field for branch
2366 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2367 for branch on CTR insns (BO == 1a00t or 1a01t). */
2368 if ((insn & (0x14 << 21)) == (0x04 << 21))
2369 insn |= 0x02 << 21;
2370 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2371 insn |= 0x08 << 21;
2372 else
2373 goto out;
2374 }
2375 else
2376 {
2377 bfd_vma target = 0;
2378 bfd_vma from;
2379
2380 if (!bfd_is_com_section (symbol->section))
2381 target = symbol->value;
2382 target += symbol->section->output_section->vma;
2383 target += symbol->section->output_offset;
2384 target += reloc_entry->addend;
2385
2386 from = (reloc_entry->address
2387 + input_section->output_offset
2388 + input_section->output_section->vma);
2389
2390 /* Invert 'y' bit if not the default. */
2391 if ((bfd_signed_vma) (target - from) < 0)
2392 insn ^= 0x01 << 21;
2393 }
2394 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2395 out:
2396 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2397 input_section, output_bfd, error_message);
2398 }
2399
2400 static bfd_reloc_status_type
2401 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2402 void *data, asection *input_section,
2403 bfd *output_bfd, char **error_message)
2404 {
2405 /* If this is a relocatable link (output_bfd test tells us), just
2406 call the generic function. Any adjustment will be done at final
2407 link time. */
2408 if (output_bfd != NULL)
2409 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2410 input_section, output_bfd, error_message);
2411
2412 /* Subtract the symbol section base address. */
2413 reloc_entry->addend -= symbol->section->output_section->vma;
2414 return bfd_reloc_continue;
2415 }
2416
2417 static bfd_reloc_status_type
2418 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2419 void *data, asection *input_section,
2420 bfd *output_bfd, char **error_message)
2421 {
2422 /* If this is a relocatable link (output_bfd test tells us), just
2423 call the generic function. Any adjustment will be done at final
2424 link time. */
2425 if (output_bfd != NULL)
2426 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2427 input_section, output_bfd, error_message);
2428
2429 /* Subtract the symbol section base address. */
2430 reloc_entry->addend -= symbol->section->output_section->vma;
2431
2432 /* Adjust the addend for sign extension of the low 16 bits. */
2433 reloc_entry->addend += 0x8000;
2434 return bfd_reloc_continue;
2435 }
2436
2437 static bfd_reloc_status_type
2438 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2439 void *data, asection *input_section,
2440 bfd *output_bfd, char **error_message)
2441 {
2442 bfd_vma TOCstart;
2443
2444 /* If this is a relocatable link (output_bfd test tells us), just
2445 call the generic function. Any adjustment will be done at final
2446 link time. */
2447 if (output_bfd != NULL)
2448 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2449 input_section, output_bfd, error_message);
2450
2451 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2452 if (TOCstart == 0)
2453 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2454
2455 /* Subtract the TOC base address. */
2456 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2457 return bfd_reloc_continue;
2458 }
2459
2460 static bfd_reloc_status_type
2461 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2462 void *data, asection *input_section,
2463 bfd *output_bfd, char **error_message)
2464 {
2465 bfd_vma TOCstart;
2466
2467 /* If this is a relocatable link (output_bfd test tells us), just
2468 call the generic function. Any adjustment will be done at final
2469 link time. */
2470 if (output_bfd != NULL)
2471 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2472 input_section, output_bfd, error_message);
2473
2474 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2475 if (TOCstart == 0)
2476 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2477
2478 /* Subtract the TOC base address. */
2479 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2480
2481 /* Adjust the addend for sign extension of the low 16 bits. */
2482 reloc_entry->addend += 0x8000;
2483 return bfd_reloc_continue;
2484 }
2485
2486 static bfd_reloc_status_type
2487 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2488 void *data, asection *input_section,
2489 bfd *output_bfd, char **error_message)
2490 {
2491 bfd_vma TOCstart;
2492 bfd_size_type octets;
2493
2494 /* If this is a relocatable link (output_bfd test tells us), just
2495 call the generic function. Any adjustment will be done at final
2496 link time. */
2497 if (output_bfd != NULL)
2498 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2499 input_section, output_bfd, error_message);
2500
2501 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2502 if (TOCstart == 0)
2503 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2504
2505 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2506 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2507 return bfd_reloc_ok;
2508 }
2509
2510 static bfd_reloc_status_type
2511 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2512 void *data, asection *input_section,
2513 bfd *output_bfd, char **error_message)
2514 {
2515 /* If this is a relocatable link (output_bfd test tells us), just
2516 call the generic function. Any adjustment will be done at final
2517 link time. */
2518 if (output_bfd != NULL)
2519 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2520 input_section, output_bfd, error_message);
2521
2522 if (error_message != NULL)
2523 {
2524 static char buf[60];
2525 sprintf (buf, "generic linker can't handle %s",
2526 reloc_entry->howto->name);
2527 *error_message = buf;
2528 }
2529 return bfd_reloc_dangerous;
2530 }
2531
2532 /* Track GOT entries needed for a given symbol. We might need more
2533 than one got entry per symbol. */
2534 struct got_entry
2535 {
2536 struct got_entry *next;
2537
2538 /* The symbol addend that we'll be placing in the GOT. */
2539 bfd_vma addend;
2540
2541 /* Unlike other ELF targets, we use separate GOT entries for the same
2542 symbol referenced from different input files. This is to support
2543 automatic multiple TOC/GOT sections, where the TOC base can vary
2544 from one input file to another. After partitioning into TOC groups
2545 we merge entries within the group.
2546
2547 Point to the BFD owning this GOT entry. */
2548 bfd *owner;
2549
2550 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2551 TLS_TPREL or TLS_DTPREL for tls entries. */
2552 unsigned char tls_type;
2553
2554 /* Non-zero if got.ent points to real entry. */
2555 unsigned char is_indirect;
2556
2557 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2558 union
2559 {
2560 bfd_signed_vma refcount;
2561 bfd_vma offset;
2562 struct got_entry *ent;
2563 } got;
2564 };
2565
2566 /* The same for PLT. */
2567 struct plt_entry
2568 {
2569 struct plt_entry *next;
2570
2571 bfd_vma addend;
2572
2573 union
2574 {
2575 bfd_signed_vma refcount;
2576 bfd_vma offset;
2577 } plt;
2578 };
2579
2580 struct ppc64_elf_obj_tdata
2581 {
2582 struct elf_obj_tdata elf;
2583
2584 /* Shortcuts to dynamic linker sections. */
2585 asection *got;
2586 asection *relgot;
2587
2588 /* Used during garbage collection. We attach global symbols defined
2589 on removed .opd entries to this section so that the sym is removed. */
2590 asection *deleted_section;
2591
2592 /* TLS local dynamic got entry handling. Support for multiple GOT
2593 sections means we potentially need one of these for each input bfd. */
2594 struct got_entry tlsld_got;
2595
2596 /* A copy of relocs before they are modified for --emit-relocs. */
2597 Elf_Internal_Rela *opd_relocs;
2598
2599 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2600 the reloc to be in the range -32768 to 32767. */
2601 unsigned int has_small_toc_reloc : 1;
2602
2603 /* Set if toc/got ha relocs detected not using r2. */
2604 unsigned int ha_relocs_not_using_r2 : 1;
2605 };
2606
2607 #define ppc64_elf_tdata(bfd) \
2608 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2609
2610 #define ppc64_tlsld_got(bfd) \
2611 (&ppc64_elf_tdata (bfd)->tlsld_got)
2612
2613 #define is_ppc64_elf(bfd) \
2614 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2615 && elf_object_id (bfd) == PPC64_ELF_DATA)
2616
2617 /* Override the generic function because we store some extras. */
2618
2619 static bfd_boolean
2620 ppc64_elf_mkobject (bfd *abfd)
2621 {
2622 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2623 PPC64_ELF_DATA);
2624 }
2625
2626 /* Fix bad default arch selected for a 64 bit input bfd when the
2627 default is 32 bit. */
2628
2629 static bfd_boolean
2630 ppc64_elf_object_p (bfd *abfd)
2631 {
2632 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2633 {
2634 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2635
2636 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2637 {
2638 /* Relies on arch after 32 bit default being 64 bit default. */
2639 abfd->arch_info = abfd->arch_info->next;
2640 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2641 }
2642 }
2643 return TRUE;
2644 }
2645
2646 /* Support for core dump NOTE sections. */
2647
2648 static bfd_boolean
2649 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2650 {
2651 size_t offset, size;
2652
2653 if (note->descsz != 504)
2654 return FALSE;
2655
2656 /* pr_cursig */
2657 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2658
2659 /* pr_pid */
2660 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
2661
2662 /* pr_reg */
2663 offset = 112;
2664 size = 384;
2665
2666 /* Make a ".reg/999" section. */
2667 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2668 size, note->descpos + offset);
2669 }
2670
2671 static bfd_boolean
2672 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2673 {
2674 if (note->descsz != 136)
2675 return FALSE;
2676
2677 elf_tdata (abfd)->core_pid
2678 = bfd_get_32 (abfd, note->descdata + 24);
2679 elf_tdata (abfd)->core_program
2680 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2681 elf_tdata (abfd)->core_command
2682 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2683
2684 return TRUE;
2685 }
2686
2687 static char *
2688 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2689 ...)
2690 {
2691 switch (note_type)
2692 {
2693 default:
2694 return NULL;
2695
2696 case NT_PRPSINFO:
2697 {
2698 char data[136];
2699 va_list ap;
2700
2701 va_start (ap, note_type);
2702 memset (data, 0, 40);
2703 strncpy (data + 40, va_arg (ap, const char *), 16);
2704 strncpy (data + 56, va_arg (ap, const char *), 80);
2705 va_end (ap);
2706 return elfcore_write_note (abfd, buf, bufsiz,
2707 "CORE", note_type, data, sizeof (data));
2708 }
2709
2710 case NT_PRSTATUS:
2711 {
2712 char data[504];
2713 va_list ap;
2714 long pid;
2715 int cursig;
2716 const void *greg;
2717
2718 va_start (ap, note_type);
2719 memset (data, 0, 112);
2720 pid = va_arg (ap, long);
2721 bfd_put_32 (abfd, pid, data + 32);
2722 cursig = va_arg (ap, int);
2723 bfd_put_16 (abfd, cursig, data + 12);
2724 greg = va_arg (ap, const void *);
2725 memcpy (data + 112, greg, 384);
2726 memset (data + 496, 0, 8);
2727 va_end (ap);
2728 return elfcore_write_note (abfd, buf, bufsiz,
2729 "CORE", note_type, data, sizeof (data));
2730 }
2731 }
2732 }
2733
2734 /* Add extra PPC sections. */
2735
2736 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2737 {
2738 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2739 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2740 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2741 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2742 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2743 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2744 { NULL, 0, 0, 0, 0 }
2745 };
2746
2747 enum _ppc64_sec_type {
2748 sec_normal = 0,
2749 sec_opd = 1,
2750 sec_toc = 2
2751 };
2752
2753 struct _ppc64_elf_section_data
2754 {
2755 struct bfd_elf_section_data elf;
2756
2757 union
2758 {
2759 /* An array with one entry for each opd function descriptor. */
2760 struct _opd_sec_data
2761 {
2762 /* Points to the function code section for local opd entries. */
2763 asection **func_sec;
2764
2765 /* After editing .opd, adjust references to opd local syms. */
2766 long *adjust;
2767 } opd;
2768
2769 /* An array for toc sections, indexed by offset/8. */
2770 struct _toc_sec_data
2771 {
2772 /* Specifies the relocation symbol index used at a given toc offset. */
2773 unsigned *symndx;
2774
2775 /* And the relocation addend. */
2776 bfd_vma *add;
2777 } toc;
2778 } u;
2779
2780 enum _ppc64_sec_type sec_type:2;
2781
2782 /* Flag set when small branches are detected. Used to
2783 select suitable defaults for the stub group size. */
2784 unsigned int has_14bit_branch:1;
2785 };
2786
2787 #define ppc64_elf_section_data(sec) \
2788 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2789
2790 static bfd_boolean
2791 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2792 {
2793 if (!sec->used_by_bfd)
2794 {
2795 struct _ppc64_elf_section_data *sdata;
2796 bfd_size_type amt = sizeof (*sdata);
2797
2798 sdata = bfd_zalloc (abfd, amt);
2799 if (sdata == NULL)
2800 return FALSE;
2801 sec->used_by_bfd = sdata;
2802 }
2803
2804 return _bfd_elf_new_section_hook (abfd, sec);
2805 }
2806
2807 static struct _opd_sec_data *
2808 get_opd_info (asection * sec)
2809 {
2810 if (sec != NULL
2811 && ppc64_elf_section_data (sec) != NULL
2812 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2813 return &ppc64_elf_section_data (sec)->u.opd;
2814 return NULL;
2815 }
2816 \f
2817 /* Parameters for the qsort hook. */
2818 static bfd_boolean synthetic_relocatable;
2819
2820 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2821
2822 static int
2823 compare_symbols (const void *ap, const void *bp)
2824 {
2825 const asymbol *a = * (const asymbol **) ap;
2826 const asymbol *b = * (const asymbol **) bp;
2827
2828 /* Section symbols first. */
2829 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2830 return -1;
2831 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2832 return 1;
2833
2834 /* then .opd symbols. */
2835 if (strcmp (a->section->name, ".opd") == 0
2836 && strcmp (b->section->name, ".opd") != 0)
2837 return -1;
2838 if (strcmp (a->section->name, ".opd") != 0
2839 && strcmp (b->section->name, ".opd") == 0)
2840 return 1;
2841
2842 /* then other code symbols. */
2843 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2844 == (SEC_CODE | SEC_ALLOC)
2845 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2846 != (SEC_CODE | SEC_ALLOC))
2847 return -1;
2848
2849 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2850 != (SEC_CODE | SEC_ALLOC)
2851 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2852 == (SEC_CODE | SEC_ALLOC))
2853 return 1;
2854
2855 if (synthetic_relocatable)
2856 {
2857 if (a->section->id < b->section->id)
2858 return -1;
2859
2860 if (a->section->id > b->section->id)
2861 return 1;
2862 }
2863
2864 if (a->value + a->section->vma < b->value + b->section->vma)
2865 return -1;
2866
2867 if (a->value + a->section->vma > b->value + b->section->vma)
2868 return 1;
2869
2870 /* For syms with the same value, prefer strong dynamic global function
2871 syms over other syms. */
2872 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2873 return -1;
2874
2875 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2876 return 1;
2877
2878 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2879 return -1;
2880
2881 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2882 return 1;
2883
2884 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2885 return -1;
2886
2887 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2888 return 1;
2889
2890 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2891 return -1;
2892
2893 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2894 return 1;
2895
2896 return 0;
2897 }
2898
2899 /* Search SYMS for a symbol of the given VALUE. */
2900
2901 static asymbol *
2902 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
2903 {
2904 long mid;
2905
2906 if (id == -1)
2907 {
2908 while (lo < hi)
2909 {
2910 mid = (lo + hi) >> 1;
2911 if (syms[mid]->value + syms[mid]->section->vma < value)
2912 lo = mid + 1;
2913 else if (syms[mid]->value + syms[mid]->section->vma > value)
2914 hi = mid;
2915 else
2916 return syms[mid];
2917 }
2918 }
2919 else
2920 {
2921 while (lo < hi)
2922 {
2923 mid = (lo + hi) >> 1;
2924 if (syms[mid]->section->id < id)
2925 lo = mid + 1;
2926 else if (syms[mid]->section->id > id)
2927 hi = mid;
2928 else if (syms[mid]->value < value)
2929 lo = mid + 1;
2930 else if (syms[mid]->value > value)
2931 hi = mid;
2932 else
2933 return syms[mid];
2934 }
2935 }
2936 return NULL;
2937 }
2938
2939 static bfd_boolean
2940 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2941 {
2942 bfd_vma vma = *(bfd_vma *) ptr;
2943 return ((section->flags & SEC_ALLOC) != 0
2944 && section->vma <= vma
2945 && vma < section->vma + section->size);
2946 }
2947
2948 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2949 entry syms. Also generate @plt symbols for the glink branch table. */
2950
2951 static long
2952 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2953 long static_count, asymbol **static_syms,
2954 long dyn_count, asymbol **dyn_syms,
2955 asymbol **ret)
2956 {
2957 asymbol *s;
2958 long i;
2959 long count;
2960 char *names;
2961 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2962 asection *opd;
2963 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2964 asymbol **syms;
2965
2966 *ret = NULL;
2967
2968 opd = bfd_get_section_by_name (abfd, ".opd");
2969 if (opd == NULL)
2970 return 0;
2971
2972 symcount = static_count;
2973 if (!relocatable)
2974 symcount += dyn_count;
2975 if (symcount == 0)
2976 return 0;
2977
2978 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2979 if (syms == NULL)
2980 return -1;
2981
2982 if (!relocatable && static_count != 0 && dyn_count != 0)
2983 {
2984 /* Use both symbol tables. */
2985 memcpy (syms, static_syms, static_count * sizeof (*syms));
2986 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
2987 }
2988 else if (!relocatable && static_count == 0)
2989 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2990 else
2991 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2992
2993 synthetic_relocatable = relocatable;
2994 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2995
2996 if (!relocatable && symcount > 1)
2997 {
2998 long j;
2999 /* Trim duplicate syms, since we may have merged the normal and
3000 dynamic symbols. Actually, we only care about syms that have
3001 different values, so trim any with the same value. */
3002 for (i = 1, j = 1; i < symcount; ++i)
3003 if (syms[i - 1]->value + syms[i - 1]->section->vma
3004 != syms[i]->value + syms[i]->section->vma)
3005 syms[j++] = syms[i];
3006 symcount = j;
3007 }
3008
3009 i = 0;
3010 if (strcmp (syms[i]->section->name, ".opd") == 0)
3011 ++i;
3012 codesecsym = i;
3013
3014 for (; i < symcount; ++i)
3015 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3016 != (SEC_CODE | SEC_ALLOC))
3017 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3018 break;
3019 codesecsymend = i;
3020
3021 for (; i < symcount; ++i)
3022 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3023 break;
3024 secsymend = i;
3025
3026 for (; i < symcount; ++i)
3027 if (strcmp (syms[i]->section->name, ".opd") != 0)
3028 break;
3029 opdsymend = i;
3030
3031 for (; i < symcount; ++i)
3032 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3033 != (SEC_CODE | SEC_ALLOC))
3034 break;
3035 symcount = i;
3036
3037 count = 0;
3038
3039 if (relocatable)
3040 {
3041 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3042 arelent *r;
3043 size_t size;
3044 long relcount;
3045
3046 if (opdsymend == secsymend)
3047 goto done;
3048
3049 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3050 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3051 if (relcount == 0)
3052 goto done;
3053
3054 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3055 {
3056 count = -1;
3057 goto done;
3058 }
3059
3060 size = 0;
3061 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3062 {
3063 asymbol *sym;
3064
3065 while (r < opd->relocation + relcount
3066 && r->address < syms[i]->value + opd->vma)
3067 ++r;
3068
3069 if (r == opd->relocation + relcount)
3070 break;
3071
3072 if (r->address != syms[i]->value + opd->vma)
3073 continue;
3074
3075 if (r->howto->type != R_PPC64_ADDR64)
3076 continue;
3077
3078 sym = *r->sym_ptr_ptr;
3079 if (!sym_exists_at (syms, opdsymend, symcount,
3080 sym->section->id, sym->value + r->addend))
3081 {
3082 ++count;
3083 size += sizeof (asymbol);
3084 size += strlen (syms[i]->name) + 2;
3085 }
3086 }
3087
3088 s = *ret = bfd_malloc (size);
3089 if (s == NULL)
3090 {
3091 count = -1;
3092 goto done;
3093 }
3094
3095 names = (char *) (s + count);
3096
3097 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3098 {
3099 asymbol *sym;
3100
3101 while (r < opd->relocation + relcount
3102 && r->address < syms[i]->value + opd->vma)
3103 ++r;
3104
3105 if (r == opd->relocation + relcount)
3106 break;
3107
3108 if (r->address != syms[i]->value + opd->vma)
3109 continue;
3110
3111 if (r->howto->type != R_PPC64_ADDR64)
3112 continue;
3113
3114 sym = *r->sym_ptr_ptr;
3115 if (!sym_exists_at (syms, opdsymend, symcount,
3116 sym->section->id, sym->value + r->addend))
3117 {
3118 size_t len;
3119
3120 *s = *syms[i];
3121 s->flags |= BSF_SYNTHETIC;
3122 s->section = sym->section;
3123 s->value = sym->value + r->addend;
3124 s->name = names;
3125 *names++ = '.';
3126 len = strlen (syms[i]->name);
3127 memcpy (names, syms[i]->name, len + 1);
3128 names += len + 1;
3129 /* Have udata.p point back to the original symbol this
3130 synthetic symbol was derived from. */
3131 s->udata.p = syms[i];
3132 s++;
3133 }
3134 }
3135 }
3136 else
3137 {
3138 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3139 bfd_byte *contents;
3140 size_t size;
3141 long plt_count = 0;
3142 bfd_vma glink_vma = 0, resolv_vma = 0;
3143 asection *dynamic, *glink = NULL, *relplt = NULL;
3144 arelent *p;
3145
3146 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
3147 {
3148 if (contents)
3149 {
3150 free_contents_and_exit:
3151 free (contents);
3152 }
3153 count = -1;
3154 goto done;
3155 }
3156
3157 size = 0;
3158 for (i = secsymend; i < opdsymend; ++i)
3159 {
3160 bfd_vma ent;
3161
3162 /* Ignore bogus symbols. */
3163 if (syms[i]->value > opd->size - 8)
3164 continue;
3165
3166 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3167 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3168 {
3169 ++count;
3170 size += sizeof (asymbol);
3171 size += strlen (syms[i]->name) + 2;
3172 }
3173 }
3174
3175 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3176 if (dyn_count != 0
3177 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3178 {
3179 bfd_byte *dynbuf, *extdyn, *extdynend;
3180 size_t extdynsize;
3181 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3182
3183 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3184 goto free_contents_and_exit;
3185
3186 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3187 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3188
3189 extdyn = dynbuf;
3190 extdynend = extdyn + dynamic->size;
3191 for (; extdyn < extdynend; extdyn += extdynsize)
3192 {
3193 Elf_Internal_Dyn dyn;
3194 (*swap_dyn_in) (abfd, extdyn, &dyn);
3195
3196 if (dyn.d_tag == DT_NULL)
3197 break;
3198
3199 if (dyn.d_tag == DT_PPC64_GLINK)
3200 {
3201 /* The first glink stub starts at offset 32; see comment in
3202 ppc64_elf_finish_dynamic_sections. */
3203 glink_vma = dyn.d_un.d_val + 32;
3204 /* The .glink section usually does not survive the final
3205 link; search for the section (usually .text) where the
3206 glink stubs now reside. */
3207 glink = bfd_sections_find_if (abfd, section_covers_vma,
3208 &glink_vma);
3209 break;
3210 }
3211 }
3212
3213 free (dynbuf);
3214 }
3215
3216 if (glink != NULL)
3217 {
3218 /* Determine __glink trampoline by reading the relative branch
3219 from the first glink stub. */
3220 bfd_byte buf[4];
3221 if (bfd_get_section_contents (abfd, glink, buf,
3222 glink_vma + 4 - glink->vma, 4))
3223 {
3224 unsigned int insn = bfd_get_32 (abfd, buf);
3225 insn ^= B_DOT;
3226 if ((insn & ~0x3fffffc) == 0)
3227 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3228 }
3229
3230 if (resolv_vma)
3231 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3232
3233 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3234 if (relplt != NULL)
3235 {
3236 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3237 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3238 goto free_contents_and_exit;
3239
3240 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3241 size += plt_count * sizeof (asymbol);
3242
3243 p = relplt->relocation;
3244 for (i = 0; i < plt_count; i++, p++)
3245 {
3246 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3247 if (p->addend != 0)
3248 size += sizeof ("+0x") - 1 + 16;
3249 }
3250 }
3251 }
3252
3253 s = *ret = bfd_malloc (size);
3254 if (s == NULL)
3255 goto free_contents_and_exit;
3256
3257 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3258
3259 for (i = secsymend; i < opdsymend; ++i)
3260 {
3261 bfd_vma ent;
3262
3263 if (syms[i]->value > opd->size - 8)
3264 continue;
3265
3266 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3267 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3268 {
3269 long lo, hi;
3270 size_t len;
3271 asection *sec = abfd->sections;
3272
3273 *s = *syms[i];
3274 lo = codesecsym;
3275 hi = codesecsymend;
3276 while (lo < hi)
3277 {
3278 long mid = (lo + hi) >> 1;
3279 if (syms[mid]->section->vma < ent)
3280 lo = mid + 1;
3281 else if (syms[mid]->section->vma > ent)
3282 hi = mid;
3283 else
3284 {
3285 sec = syms[mid]->section;
3286 break;
3287 }
3288 }
3289
3290 if (lo >= hi && lo > codesecsym)
3291 sec = syms[lo - 1]->section;
3292
3293 for (; sec != NULL; sec = sec->next)
3294 {
3295 if (sec->vma > ent)
3296 break;
3297 /* SEC_LOAD may not be set if SEC is from a separate debug
3298 info file. */
3299 if ((sec->flags & SEC_ALLOC) == 0)
3300 break;
3301 if ((sec->flags & SEC_CODE) != 0)
3302 s->section = sec;
3303 }
3304 s->flags |= BSF_SYNTHETIC;
3305 s->value = ent - s->section->vma;
3306 s->name = names;
3307 *names++ = '.';
3308 len = strlen (syms[i]->name);
3309 memcpy (names, syms[i]->name, len + 1);
3310 names += len + 1;
3311 /* Have udata.p point back to the original symbol this
3312 synthetic symbol was derived from. */
3313 s->udata.p = syms[i];
3314 s++;
3315 }
3316 }
3317 free (contents);
3318
3319 if (glink != NULL && relplt != NULL)
3320 {
3321 if (resolv_vma)
3322 {
3323 /* Add a symbol for the main glink trampoline. */
3324 memset (s, 0, sizeof *s);
3325 s->the_bfd = abfd;
3326 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3327 s->section = glink;
3328 s->value = resolv_vma - glink->vma;
3329 s->name = names;
3330 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3331 names += sizeof ("__glink_PLTresolve");
3332 s++;
3333 count++;
3334 }
3335
3336 /* FIXME: It would be very much nicer to put sym@plt on the
3337 stub rather than on the glink branch table entry. The
3338 objdump disassembler would then use a sensible symbol
3339 name on plt calls. The difficulty in doing so is
3340 a) finding the stubs, and,
3341 b) matching stubs against plt entries, and,
3342 c) there can be multiple stubs for a given plt entry.
3343
3344 Solving (a) could be done by code scanning, but older
3345 ppc64 binaries used different stubs to current code.
3346 (b) is the tricky one since you need to known the toc
3347 pointer for at least one function that uses a pic stub to
3348 be able to calculate the plt address referenced.
3349 (c) means gdb would need to set multiple breakpoints (or
3350 find the glink branch itself) when setting breakpoints
3351 for pending shared library loads. */
3352 p = relplt->relocation;
3353 for (i = 0; i < plt_count; i++, p++)
3354 {
3355 size_t len;
3356
3357 *s = **p->sym_ptr_ptr;
3358 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3359 we are defining a symbol, ensure one of them is set. */
3360 if ((s->flags & BSF_LOCAL) == 0)
3361 s->flags |= BSF_GLOBAL;
3362 s->flags |= BSF_SYNTHETIC;
3363 s->section = glink;
3364 s->value = glink_vma - glink->vma;
3365 s->name = names;
3366 s->udata.p = NULL;
3367 len = strlen ((*p->sym_ptr_ptr)->name);
3368 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3369 names += len;
3370 if (p->addend != 0)
3371 {
3372 memcpy (names, "+0x", sizeof ("+0x") - 1);
3373 names += sizeof ("+0x") - 1;
3374 bfd_sprintf_vma (abfd, names, p->addend);
3375 names += strlen (names);
3376 }
3377 memcpy (names, "@plt", sizeof ("@plt"));
3378 names += sizeof ("@plt");
3379 s++;
3380 glink_vma += 8;
3381 if (i >= 0x8000)
3382 glink_vma += 4;
3383 }
3384 count += plt_count;
3385 }
3386 }
3387
3388 done:
3389 free (syms);
3390 return count;
3391 }
3392 \f
3393 /* The following functions are specific to the ELF linker, while
3394 functions above are used generally. Those named ppc64_elf_* are
3395 called by the main ELF linker code. They appear in this file more
3396 or less in the order in which they are called. eg.
3397 ppc64_elf_check_relocs is called early in the link process,
3398 ppc64_elf_finish_dynamic_sections is one of the last functions
3399 called.
3400
3401 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3402 functions have both a function code symbol and a function descriptor
3403 symbol. A call to foo in a relocatable object file looks like:
3404
3405 . .text
3406 . x:
3407 . bl .foo
3408 . nop
3409
3410 The function definition in another object file might be:
3411
3412 . .section .opd
3413 . foo: .quad .foo
3414 . .quad .TOC.@tocbase
3415 . .quad 0
3416 .
3417 . .text
3418 . .foo: blr
3419
3420 When the linker resolves the call during a static link, the branch
3421 unsurprisingly just goes to .foo and the .opd information is unused.
3422 If the function definition is in a shared library, things are a little
3423 different: The call goes via a plt call stub, the opd information gets
3424 copied to the plt, and the linker patches the nop.
3425
3426 . x:
3427 . bl .foo_stub
3428 . ld 2,40(1)
3429 .
3430 .
3431 . .foo_stub:
3432 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
3433 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
3434 . std 2,40(1) # this is the general idea
3435 . ld 11,0(12)
3436 . ld 2,8(12)
3437 . mtctr 11
3438 . ld 11,16(12)
3439 . bctr
3440 .
3441 . .section .plt
3442 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3443
3444 The "reloc ()" notation is supposed to indicate that the linker emits
3445 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3446 copying.
3447
3448 What are the difficulties here? Well, firstly, the relocations
3449 examined by the linker in check_relocs are against the function code
3450 sym .foo, while the dynamic relocation in the plt is emitted against
3451 the function descriptor symbol, foo. Somewhere along the line, we need
3452 to carefully copy dynamic link information from one symbol to the other.
3453 Secondly, the generic part of the elf linker will make .foo a dynamic
3454 symbol as is normal for most other backends. We need foo dynamic
3455 instead, at least for an application final link. However, when
3456 creating a shared library containing foo, we need to have both symbols
3457 dynamic so that references to .foo are satisfied during the early
3458 stages of linking. Otherwise the linker might decide to pull in a
3459 definition from some other object, eg. a static library.
3460
3461 Update: As of August 2004, we support a new convention. Function
3462 calls may use the function descriptor symbol, ie. "bl foo". This
3463 behaves exactly as "bl .foo". */
3464
3465 /* Of those relocs that might be copied as dynamic relocs, this function
3466 selects those that must be copied when linking a shared library,
3467 even when the symbol is local. */
3468
3469 static int
3470 must_be_dyn_reloc (struct bfd_link_info *info,
3471 enum elf_ppc64_reloc_type r_type)
3472 {
3473 switch (r_type)
3474 {
3475 default:
3476 return 1;
3477
3478 case R_PPC64_REL32:
3479 case R_PPC64_REL64:
3480 case R_PPC64_REL30:
3481 return 0;
3482
3483 case R_PPC64_TPREL16:
3484 case R_PPC64_TPREL16_LO:
3485 case R_PPC64_TPREL16_HI:
3486 case R_PPC64_TPREL16_HA:
3487 case R_PPC64_TPREL16_DS:
3488 case R_PPC64_TPREL16_LO_DS:
3489 case R_PPC64_TPREL16_HIGHER:
3490 case R_PPC64_TPREL16_HIGHERA:
3491 case R_PPC64_TPREL16_HIGHEST:
3492 case R_PPC64_TPREL16_HIGHESTA:
3493 case R_PPC64_TPREL64:
3494 return !info->executable;
3495 }
3496 }
3497
3498 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3499 copying dynamic variables from a shared lib into an app's dynbss
3500 section, and instead use a dynamic relocation to point into the
3501 shared lib. With code that gcc generates, it's vital that this be
3502 enabled; In the PowerPC64 ABI, the address of a function is actually
3503 the address of a function descriptor, which resides in the .opd
3504 section. gcc uses the descriptor directly rather than going via the
3505 GOT as some other ABI's do, which means that initialized function
3506 pointers must reference the descriptor. Thus, a function pointer
3507 initialized to the address of a function in a shared library will
3508 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3509 redefines the function descriptor symbol to point to the copy. This
3510 presents a problem as a plt entry for that function is also
3511 initialized from the function descriptor symbol and the copy reloc
3512 may not be initialized first. */
3513 #define ELIMINATE_COPY_RELOCS 1
3514
3515 /* Section name for stubs is the associated section name plus this
3516 string. */
3517 #define STUB_SUFFIX ".stub"
3518
3519 /* Linker stubs.
3520 ppc_stub_long_branch:
3521 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3522 destination, but a 24 bit branch in a stub section will reach.
3523 . b dest
3524
3525 ppc_stub_plt_branch:
3526 Similar to the above, but a 24 bit branch in the stub section won't
3527 reach its destination.
3528 . addis %r12,%r2,xxx@toc@ha
3529 . ld %r11,xxx@toc@l(%r12)
3530 . mtctr %r11
3531 . bctr
3532
3533 ppc_stub_plt_call:
3534 Used to call a function in a shared library. If it so happens that
3535 the plt entry referenced crosses a 64k boundary, then an extra
3536 "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
3537 . addis %r12,%r2,xxx@toc@ha
3538 . std %r2,40(%r1)
3539 . ld %r11,xxx+0@toc@l(%r12)
3540 . mtctr %r11
3541 . ld %r2,xxx+8@toc@l(%r12)
3542 . ld %r11,xxx+16@toc@l(%r12)
3543 . bctr
3544
3545 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3546 code to adjust the value and save r2 to support multiple toc sections.
3547 A ppc_stub_long_branch with an r2 offset looks like:
3548 . std %r2,40(%r1)
3549 . addis %r2,%r2,off@ha
3550 . addi %r2,%r2,off@l
3551 . b dest
3552
3553 A ppc_stub_plt_branch with an r2 offset looks like:
3554 . std %r2,40(%r1)
3555 . addis %r12,%r2,xxx@toc@ha
3556 . ld %r11,xxx@toc@l(%r12)
3557 . addis %r2,%r2,off@ha
3558 . addi %r2,%r2,off@l
3559 . mtctr %r11
3560 . bctr
3561
3562 In cases where the "addis" instruction would add zero, the "addis" is
3563 omitted and following instructions modified slightly in some cases.
3564 */
3565
3566 enum ppc_stub_type {
3567 ppc_stub_none,
3568 ppc_stub_long_branch,
3569 ppc_stub_long_branch_r2off,
3570 ppc_stub_plt_branch,
3571 ppc_stub_plt_branch_r2off,
3572 ppc_stub_plt_call
3573 };
3574
3575 struct ppc_stub_hash_entry {
3576
3577 /* Base hash table entry structure. */
3578 struct bfd_hash_entry root;
3579
3580 enum ppc_stub_type stub_type;
3581
3582 /* The stub section. */
3583 asection *stub_sec;
3584
3585 /* Offset within stub_sec of the beginning of this stub. */
3586 bfd_vma stub_offset;
3587
3588 /* Given the symbol's value and its section we can determine its final
3589 value when building the stubs (so the stub knows where to jump. */
3590 bfd_vma target_value;
3591 asection *target_section;
3592
3593 /* The symbol table entry, if any, that this was derived from. */
3594 struct ppc_link_hash_entry *h;
3595 struct plt_entry *plt_ent;
3596
3597 /* And the reloc addend that this was derived from. */
3598 bfd_vma addend;
3599
3600 /* Where this stub is being called from, or, in the case of combined
3601 stub sections, the first input section in the group. */
3602 asection *id_sec;
3603 };
3604
3605 struct ppc_branch_hash_entry {
3606
3607 /* Base hash table entry structure. */
3608 struct bfd_hash_entry root;
3609
3610 /* Offset within branch lookup table. */
3611 unsigned int offset;
3612
3613 /* Generation marker. */
3614 unsigned int iter;
3615 };
3616
3617 struct ppc_link_hash_entry
3618 {
3619 struct elf_link_hash_entry elf;
3620
3621 union {
3622 /* A pointer to the most recently used stub hash entry against this
3623 symbol. */
3624 struct ppc_stub_hash_entry *stub_cache;
3625
3626 /* A pointer to the next symbol starting with a '.' */
3627 struct ppc_link_hash_entry *next_dot_sym;
3628 } u;
3629
3630 /* Track dynamic relocs copied for this symbol. */
3631 struct elf_dyn_relocs *dyn_relocs;
3632
3633 /* Link between function code and descriptor symbols. */
3634 struct ppc_link_hash_entry *oh;
3635
3636 /* Flag function code and descriptor symbols. */
3637 unsigned int is_func:1;
3638 unsigned int is_func_descriptor:1;
3639 unsigned int fake:1;
3640
3641 /* Whether global opd/toc sym has been adjusted or not.
3642 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3643 should be set for all globals defined in any opd/toc section. */
3644 unsigned int adjust_done:1;
3645
3646 /* Set if we twiddled this symbol to weak at some stage. */
3647 unsigned int was_undefined:1;
3648
3649 /* Contexts in which symbol is used in the GOT (or TOC).
3650 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3651 corresponding relocs are encountered during check_relocs.
3652 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3653 indicate the corresponding GOT entry type is not needed.
3654 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3655 a TPREL one. We use a separate flag rather than setting TPREL
3656 just for convenience in distinguishing the two cases. */
3657 #define TLS_GD 1 /* GD reloc. */
3658 #define TLS_LD 2 /* LD reloc. */
3659 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
3660 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3661 #define TLS_TLS 16 /* Any TLS reloc. */
3662 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3663 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3664 #define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
3665 unsigned char tls_mask;
3666 };
3667
3668 /* ppc64 ELF linker hash table. */
3669
3670 struct ppc_link_hash_table
3671 {
3672 struct elf_link_hash_table elf;
3673
3674 /* The stub hash table. */
3675 struct bfd_hash_table stub_hash_table;
3676
3677 /* Another hash table for plt_branch stubs. */
3678 struct bfd_hash_table branch_hash_table;
3679
3680 /* Linker stub bfd. */
3681 bfd *stub_bfd;
3682
3683 /* Linker call-backs. */
3684 asection * (*add_stub_section) (const char *, asection *);
3685 void (*layout_sections_again) (void);
3686
3687 /* Array to keep track of which stub sections have been created, and
3688 information on stub grouping. */
3689 struct map_stub {
3690 /* This is the section to which stubs in the group will be attached. */
3691 asection *link_sec;
3692 /* The stub section. */
3693 asection *stub_sec;
3694 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3695 bfd_vma toc_off;
3696 } *stub_group;
3697
3698 /* Temp used when calculating TOC pointers. */
3699 bfd_vma toc_curr;
3700 bfd *toc_bfd;
3701 asection *toc_first_sec;
3702
3703 /* Highest input section id. */
3704 int top_id;
3705
3706 /* Highest output section index. */
3707 int top_index;
3708
3709 /* Used when adding symbols. */
3710 struct ppc_link_hash_entry *dot_syms;
3711
3712 /* List of input sections for each output section. */
3713 asection **input_list;
3714
3715 /* Short-cuts to get to dynamic linker sections. */
3716 asection *got;
3717 asection *plt;
3718 asection *relplt;
3719 asection *iplt;
3720 asection *reliplt;
3721 asection *dynbss;
3722 asection *relbss;
3723 asection *glink;
3724 asection *sfpr;
3725 asection *brlt;
3726 asection *relbrlt;
3727 asection *glink_eh_frame;
3728
3729 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3730 struct ppc_link_hash_entry *tls_get_addr;
3731 struct ppc_link_hash_entry *tls_get_addr_fd;
3732
3733 /* The size of reliplt used by got entry relocs. */
3734 bfd_size_type got_reli_size;
3735
3736 /* Statistics. */
3737 unsigned long stub_count[ppc_stub_plt_call];
3738
3739 /* Number of stubs against global syms. */
3740 unsigned long stub_globals;
3741
3742 /* Set if PLT call stubs should load r11. */
3743 unsigned int plt_static_chain:1;
3744
3745 /* Set if we should emit symbols for stubs. */
3746 unsigned int emit_stub_syms:1;
3747
3748 /* Set if __tls_get_addr optimization should not be done. */
3749 unsigned int no_tls_get_addr_opt:1;
3750
3751 /* Support for multiple toc sections. */
3752 unsigned int do_multi_toc:1;
3753 unsigned int multi_toc_needed:1;
3754 unsigned int second_toc_pass:1;
3755 unsigned int do_toc_opt:1;
3756
3757 /* Set on error. */
3758 unsigned int stub_error:1;
3759
3760 /* Temp used by ppc64_elf_process_dot_syms. */
3761 unsigned int twiddled_syms:1;
3762
3763 /* Incremented every time we size stubs. */
3764 unsigned int stub_iteration;
3765
3766 /* Small local sym cache. */
3767 struct sym_cache sym_cache;
3768 };
3769
3770 /* Rename some of the generic section flags to better document how they
3771 are used here. */
3772
3773 /* Nonzero if this section has TLS related relocations. */
3774 #define has_tls_reloc sec_flg0
3775
3776 /* Nonzero if this section has a call to __tls_get_addr. */
3777 #define has_tls_get_addr_call sec_flg1
3778
3779 /* Nonzero if this section has any toc or got relocs. */
3780 #define has_toc_reloc sec_flg2
3781
3782 /* Nonzero if this section has a call to another section that uses
3783 the toc or got. */
3784 #define makes_toc_func_call sec_flg3
3785
3786 /* Recursion protection when determining above flag. */
3787 #define call_check_in_progress sec_flg4
3788 #define call_check_done sec_flg5
3789
3790 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3791
3792 #define ppc_hash_table(p) \
3793 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3794 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3795
3796 #define ppc_stub_hash_lookup(table, string, create, copy) \
3797 ((struct ppc_stub_hash_entry *) \
3798 bfd_hash_lookup ((table), (string), (create), (copy)))
3799
3800 #define ppc_branch_hash_lookup(table, string, create, copy) \
3801 ((struct ppc_branch_hash_entry *) \
3802 bfd_hash_lookup ((table), (string), (create), (copy)))
3803
3804 /* Create an entry in the stub hash table. */
3805
3806 static struct bfd_hash_entry *
3807 stub_hash_newfunc (struct bfd_hash_entry *entry,
3808 struct bfd_hash_table *table,
3809 const char *string)
3810 {
3811 /* Allocate the structure if it has not already been allocated by a
3812 subclass. */
3813 if (entry == NULL)
3814 {
3815 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3816 if (entry == NULL)
3817 return entry;
3818 }
3819
3820 /* Call the allocation method of the superclass. */
3821 entry = bfd_hash_newfunc (entry, table, string);
3822 if (entry != NULL)
3823 {
3824 struct ppc_stub_hash_entry *eh;
3825
3826 /* Initialize the local fields. */
3827 eh = (struct ppc_stub_hash_entry *) entry;
3828 eh->stub_type = ppc_stub_none;
3829 eh->stub_sec = NULL;
3830 eh->stub_offset = 0;
3831 eh->target_value = 0;
3832 eh->target_section = NULL;
3833 eh->h = NULL;
3834 eh->id_sec = NULL;
3835 }
3836
3837 return entry;
3838 }
3839
3840 /* Create an entry in the branch hash table. */
3841
3842 static struct bfd_hash_entry *
3843 branch_hash_newfunc (struct bfd_hash_entry *entry,
3844 struct bfd_hash_table *table,
3845 const char *string)
3846 {
3847 /* Allocate the structure if it has not already been allocated by a
3848 subclass. */
3849 if (entry == NULL)
3850 {
3851 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3852 if (entry == NULL)
3853 return entry;
3854 }
3855
3856 /* Call the allocation method of the superclass. */
3857 entry = bfd_hash_newfunc (entry, table, string);
3858 if (entry != NULL)
3859 {
3860 struct ppc_branch_hash_entry *eh;
3861
3862 /* Initialize the local fields. */
3863 eh = (struct ppc_branch_hash_entry *) entry;
3864 eh->offset = 0;
3865 eh->iter = 0;
3866 }
3867
3868 return entry;
3869 }
3870
3871 /* Create an entry in a ppc64 ELF linker hash table. */
3872
3873 static struct bfd_hash_entry *
3874 link_hash_newfunc (struct bfd_hash_entry *entry,
3875 struct bfd_hash_table *table,
3876 const char *string)
3877 {
3878 /* Allocate the structure if it has not already been allocated by a
3879 subclass. */
3880 if (entry == NULL)
3881 {
3882 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3883 if (entry == NULL)
3884 return entry;
3885 }
3886
3887 /* Call the allocation method of the superclass. */
3888 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3889 if (entry != NULL)
3890 {
3891 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3892
3893 memset (&eh->u.stub_cache, 0,
3894 (sizeof (struct ppc_link_hash_entry)
3895 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3896
3897 /* When making function calls, old ABI code references function entry
3898 points (dot symbols), while new ABI code references the function
3899 descriptor symbol. We need to make any combination of reference and
3900 definition work together, without breaking archive linking.
3901
3902 For a defined function "foo" and an undefined call to "bar":
3903 An old object defines "foo" and ".foo", references ".bar" (possibly
3904 "bar" too).
3905 A new object defines "foo" and references "bar".
3906
3907 A new object thus has no problem with its undefined symbols being
3908 satisfied by definitions in an old object. On the other hand, the
3909 old object won't have ".bar" satisfied by a new object.
3910
3911 Keep a list of newly added dot-symbols. */
3912
3913 if (string[0] == '.')
3914 {
3915 struct ppc_link_hash_table *htab;
3916
3917 htab = (struct ppc_link_hash_table *) table;
3918 eh->u.next_dot_sym = htab->dot_syms;
3919 htab->dot_syms = eh;
3920 }
3921 }
3922
3923 return entry;
3924 }
3925
3926 /* Create a ppc64 ELF linker hash table. */
3927
3928 static struct bfd_link_hash_table *
3929 ppc64_elf_link_hash_table_create (bfd *abfd)
3930 {
3931 struct ppc_link_hash_table *htab;
3932 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3933
3934 htab = bfd_zmalloc (amt);
3935 if (htab == NULL)
3936 return NULL;
3937
3938 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3939 sizeof (struct ppc_link_hash_entry),
3940 PPC64_ELF_DATA))
3941 {
3942 free (htab);
3943 return NULL;
3944 }
3945
3946 /* Init the stub hash table too. */
3947 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3948 sizeof (struct ppc_stub_hash_entry)))
3949 return NULL;
3950
3951 /* And the branch hash table. */
3952 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3953 sizeof (struct ppc_branch_hash_entry)))
3954 return NULL;
3955
3956 /* Initializing two fields of the union is just cosmetic. We really
3957 only care about glist, but when compiled on a 32-bit host the
3958 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3959 debugger inspection of these fields look nicer. */
3960 htab->elf.init_got_refcount.refcount = 0;
3961 htab->elf.init_got_refcount.glist = NULL;
3962 htab->elf.init_plt_refcount.refcount = 0;
3963 htab->elf.init_plt_refcount.glist = NULL;
3964 htab->elf.init_got_offset.offset = 0;
3965 htab->elf.init_got_offset.glist = NULL;
3966 htab->elf.init_plt_offset.offset = 0;
3967 htab->elf.init_plt_offset.glist = NULL;
3968
3969 return &htab->elf.root;
3970 }
3971
3972 /* Free the derived linker hash table. */
3973
3974 static void
3975 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
3976 {
3977 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
3978
3979 bfd_hash_table_free (&ret->stub_hash_table);
3980 bfd_hash_table_free (&ret->branch_hash_table);
3981 _bfd_generic_link_hash_table_free (hash);
3982 }
3983
3984 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3985
3986 void
3987 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
3988 {
3989 struct ppc_link_hash_table *htab;
3990
3991 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
3992
3993 /* Always hook our dynamic sections into the first bfd, which is the
3994 linker created stub bfd. This ensures that the GOT header is at
3995 the start of the output TOC section. */
3996 htab = ppc_hash_table (info);
3997 if (htab == NULL)
3998 return;
3999 htab->stub_bfd = abfd;
4000 htab->elf.dynobj = abfd;
4001 }
4002
4003 /* Build a name for an entry in the stub hash table. */
4004
4005 static char *
4006 ppc_stub_name (const asection *input_section,
4007 const asection *sym_sec,
4008 const struct ppc_link_hash_entry *h,
4009 const Elf_Internal_Rela *rel)
4010 {
4011 char *stub_name;
4012 bfd_size_type len;
4013
4014 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4015 offsets from a sym as a branch target? In fact, we could
4016 probably assume the addend is always zero. */
4017 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4018
4019 if (h)
4020 {
4021 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4022 stub_name = bfd_malloc (len);
4023 if (stub_name == NULL)
4024 return stub_name;
4025
4026 sprintf (stub_name, "%08x.%s+%x",
4027 input_section->id & 0xffffffff,
4028 h->elf.root.root.string,
4029 (int) rel->r_addend & 0xffffffff);
4030 }
4031 else
4032 {
4033 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4034 stub_name = bfd_malloc (len);
4035 if (stub_name == NULL)
4036 return stub_name;
4037
4038 sprintf (stub_name, "%08x.%x:%x+%x",
4039 input_section->id & 0xffffffff,
4040 sym_sec->id & 0xffffffff,
4041 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4042 (int) rel->r_addend & 0xffffffff);
4043 }
4044 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4045 stub_name[len - 2] = 0;
4046 return stub_name;
4047 }
4048
4049 /* Look up an entry in the stub hash. Stub entries are cached because
4050 creating the stub name takes a bit of time. */
4051
4052 static struct ppc_stub_hash_entry *
4053 ppc_get_stub_entry (const asection *input_section,
4054 const asection *sym_sec,
4055 struct ppc_link_hash_entry *h,
4056 const Elf_Internal_Rela *rel,
4057 struct ppc_link_hash_table *htab)
4058 {
4059 struct ppc_stub_hash_entry *stub_entry;
4060 const asection *id_sec;
4061
4062 /* If this input section is part of a group of sections sharing one
4063 stub section, then use the id of the first section in the group.
4064 Stub names need to include a section id, as there may well be
4065 more than one stub used to reach say, printf, and we need to
4066 distinguish between them. */
4067 id_sec = htab->stub_group[input_section->id].link_sec;
4068
4069 if (h != NULL && h->u.stub_cache != NULL
4070 && h->u.stub_cache->h == h
4071 && h->u.stub_cache->id_sec == id_sec)
4072 {
4073 stub_entry = h->u.stub_cache;
4074 }
4075 else
4076 {
4077 char *stub_name;
4078
4079 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4080 if (stub_name == NULL)
4081 return NULL;
4082
4083 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4084 stub_name, FALSE, FALSE);
4085 if (h != NULL)
4086 h->u.stub_cache = stub_entry;
4087
4088 free (stub_name);
4089 }
4090
4091 return stub_entry;
4092 }
4093
4094 /* Add a new stub entry to the stub hash. Not all fields of the new
4095 stub entry are initialised. */
4096
4097 static struct ppc_stub_hash_entry *
4098 ppc_add_stub (const char *stub_name,
4099 asection *section,
4100 struct bfd_link_info *info)
4101 {
4102 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4103 asection *link_sec;
4104 asection *stub_sec;
4105 struct ppc_stub_hash_entry *stub_entry;
4106
4107 link_sec = htab->stub_group[section->id].link_sec;
4108 stub_sec = htab->stub_group[section->id].stub_sec;
4109 if (stub_sec == NULL)
4110 {
4111 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4112 if (stub_sec == NULL)
4113 {
4114 size_t namelen;
4115 bfd_size_type len;
4116 char *s_name;
4117
4118 namelen = strlen (link_sec->name);
4119 len = namelen + sizeof (STUB_SUFFIX);
4120 s_name = bfd_alloc (htab->stub_bfd, len);
4121 if (s_name == NULL)
4122 return NULL;
4123
4124 memcpy (s_name, link_sec->name, namelen);
4125 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4126 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4127 if (stub_sec == NULL)
4128 return NULL;
4129 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4130 }
4131 htab->stub_group[section->id].stub_sec = stub_sec;
4132 }
4133
4134 /* Enter this entry into the linker stub hash table. */
4135 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4136 TRUE, FALSE);
4137 if (stub_entry == NULL)
4138 {
4139 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4140 section->owner, stub_name);
4141 return NULL;
4142 }
4143
4144 stub_entry->stub_sec = stub_sec;
4145 stub_entry->stub_offset = 0;
4146 stub_entry->id_sec = link_sec;
4147 return stub_entry;
4148 }
4149
4150 /* Create sections for linker generated code. */
4151
4152 static bfd_boolean
4153 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4154 {
4155 struct ppc_link_hash_table *htab;
4156 flagword flags;
4157
4158 htab = ppc_hash_table (info);
4159 if (htab == NULL)
4160 return FALSE;
4161
4162 /* Create .sfpr for code to save and restore fp regs. */
4163 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4164 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4165 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4166 flags);
4167 if (htab->sfpr == NULL
4168 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4169 return FALSE;
4170
4171 /* Create .glink for lazy dynamic linking support. */
4172 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4173 flags);
4174 if (htab->glink == NULL
4175 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4176 return FALSE;
4177
4178 if (!info->no_ld_generated_unwind_info)
4179 {
4180 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4181 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4182 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4183 ".eh_frame",
4184 flags);
4185 if (htab->glink_eh_frame == NULL
4186 || !bfd_set_section_alignment (abfd, htab->glink_eh_frame, 2))
4187 return FALSE;
4188 }
4189
4190 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4191 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4192 if (htab->iplt == NULL
4193 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4194 return FALSE;
4195
4196 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4197 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4198 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4199 ".rela.iplt",
4200 flags);
4201 if (htab->reliplt == NULL
4202 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4203 return FALSE;
4204
4205 /* Create branch lookup table for plt_branch stubs. */
4206 flags = (SEC_ALLOC | SEC_LOAD
4207 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4208 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4209 flags);
4210 if (htab->brlt == NULL
4211 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4212 return FALSE;
4213
4214 if (!info->shared)
4215 return TRUE;
4216
4217 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4218 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4219 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4220 ".rela.branch_lt",
4221 flags);
4222 if (htab->relbrlt == NULL
4223 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4224 return FALSE;
4225
4226 return TRUE;
4227 }
4228
4229 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4230 not already done. */
4231
4232 static bfd_boolean
4233 create_got_section (bfd *abfd, struct bfd_link_info *info)
4234 {
4235 asection *got, *relgot;
4236 flagword flags;
4237 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4238
4239 if (!is_ppc64_elf (abfd))
4240 return FALSE;
4241 if (htab == NULL)
4242 return FALSE;
4243
4244 if (!htab->got)
4245 {
4246 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4247 return FALSE;
4248
4249 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4250 if (!htab->got)
4251 abort ();
4252 }
4253
4254 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4255 | SEC_LINKER_CREATED);
4256
4257 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4258 if (!got
4259 || !bfd_set_section_alignment (abfd, got, 3))
4260 return FALSE;
4261
4262 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4263 flags | SEC_READONLY);
4264 if (!relgot
4265 || ! bfd_set_section_alignment (abfd, relgot, 3))
4266 return FALSE;
4267
4268 ppc64_elf_tdata (abfd)->got = got;
4269 ppc64_elf_tdata (abfd)->relgot = relgot;
4270 return TRUE;
4271 }
4272
4273 /* Create the dynamic sections, and set up shortcuts. */
4274
4275 static bfd_boolean
4276 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4277 {
4278 struct ppc_link_hash_table *htab;
4279
4280 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4281 return FALSE;
4282
4283 htab = ppc_hash_table (info);
4284 if (htab == NULL)
4285 return FALSE;
4286
4287 if (!htab->got)
4288 htab->got = bfd_get_section_by_name (dynobj, ".got");
4289 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4290 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4291 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
4292 if (!info->shared)
4293 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
4294
4295 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4296 || (!info->shared && !htab->relbss))
4297 abort ();
4298
4299 return TRUE;
4300 }
4301
4302 /* Follow indirect and warning symbol links. */
4303
4304 static inline struct bfd_link_hash_entry *
4305 follow_link (struct bfd_link_hash_entry *h)
4306 {
4307 while (h->type == bfd_link_hash_indirect
4308 || h->type == bfd_link_hash_warning)
4309 h = h->u.i.link;
4310 return h;
4311 }
4312
4313 static inline struct elf_link_hash_entry *
4314 elf_follow_link (struct elf_link_hash_entry *h)
4315 {
4316 return (struct elf_link_hash_entry *) follow_link (&h->root);
4317 }
4318
4319 static inline struct ppc_link_hash_entry *
4320 ppc_follow_link (struct ppc_link_hash_entry *h)
4321 {
4322 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4323 }
4324
4325 /* Merge PLT info on FROM with that on TO. */
4326
4327 static void
4328 move_plt_plist (struct ppc_link_hash_entry *from,
4329 struct ppc_link_hash_entry *to)
4330 {
4331 if (from->elf.plt.plist != NULL)
4332 {
4333 if (to->elf.plt.plist != NULL)
4334 {
4335 struct plt_entry **entp;
4336 struct plt_entry *ent;
4337
4338 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4339 {
4340 struct plt_entry *dent;
4341
4342 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4343 if (dent->addend == ent->addend)
4344 {
4345 dent->plt.refcount += ent->plt.refcount;
4346 *entp = ent->next;
4347 break;
4348 }
4349 if (dent == NULL)
4350 entp = &ent->next;
4351 }
4352 *entp = to->elf.plt.plist;
4353 }
4354
4355 to->elf.plt.plist = from->elf.plt.plist;
4356 from->elf.plt.plist = NULL;
4357 }
4358 }
4359
4360 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4361
4362 static void
4363 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4364 struct elf_link_hash_entry *dir,
4365 struct elf_link_hash_entry *ind)
4366 {
4367 struct ppc_link_hash_entry *edir, *eind;
4368
4369 edir = (struct ppc_link_hash_entry *) dir;
4370 eind = (struct ppc_link_hash_entry *) ind;
4371
4372 edir->is_func |= eind->is_func;
4373 edir->is_func_descriptor |= eind->is_func_descriptor;
4374 edir->tls_mask |= eind->tls_mask;
4375 if (eind->oh != NULL)
4376 edir->oh = ppc_follow_link (eind->oh);
4377
4378 /* If called to transfer flags for a weakdef during processing
4379 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4380 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4381 if (!(ELIMINATE_COPY_RELOCS
4382 && eind->elf.root.type != bfd_link_hash_indirect
4383 && edir->elf.dynamic_adjusted))
4384 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4385
4386 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4387 edir->elf.ref_regular |= eind->elf.ref_regular;
4388 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4389 edir->elf.needs_plt |= eind->elf.needs_plt;
4390
4391 /* If we were called to copy over info for a weak sym, that's all. */
4392 if (eind->elf.root.type != bfd_link_hash_indirect)
4393 return;
4394
4395 /* Copy over any dynamic relocs we may have on the indirect sym. */
4396 if (eind->dyn_relocs != NULL)
4397 {
4398 if (edir->dyn_relocs != NULL)
4399 {
4400 struct elf_dyn_relocs **pp;
4401 struct elf_dyn_relocs *p;
4402
4403 /* Add reloc counts against the indirect sym to the direct sym
4404 list. Merge any entries against the same section. */
4405 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4406 {
4407 struct elf_dyn_relocs *q;
4408
4409 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4410 if (q->sec == p->sec)
4411 {
4412 q->pc_count += p->pc_count;
4413 q->count += p->count;
4414 *pp = p->next;
4415 break;
4416 }
4417 if (q == NULL)
4418 pp = &p->next;
4419 }
4420 *pp = edir->dyn_relocs;
4421 }
4422
4423 edir->dyn_relocs = eind->dyn_relocs;
4424 eind->dyn_relocs = NULL;
4425 }
4426
4427 /* Copy over got entries that we may have already seen to the
4428 symbol which just became indirect. */
4429 if (eind->elf.got.glist != NULL)
4430 {
4431 if (edir->elf.got.glist != NULL)
4432 {
4433 struct got_entry **entp;
4434 struct got_entry *ent;
4435
4436 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4437 {
4438 struct got_entry *dent;
4439
4440 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4441 if (dent->addend == ent->addend
4442 && dent->owner == ent->owner
4443 && dent->tls_type == ent->tls_type)
4444 {
4445 dent->got.refcount += ent->got.refcount;
4446 *entp = ent->next;
4447 break;
4448 }
4449 if (dent == NULL)
4450 entp = &ent->next;
4451 }
4452 *entp = edir->elf.got.glist;
4453 }
4454
4455 edir->elf.got.glist = eind->elf.got.glist;
4456 eind->elf.got.glist = NULL;
4457 }
4458
4459 /* And plt entries. */
4460 move_plt_plist (eind, edir);
4461
4462 if (eind->elf.dynindx != -1)
4463 {
4464 if (edir->elf.dynindx != -1)
4465 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4466 edir->elf.dynstr_index);
4467 edir->elf.dynindx = eind->elf.dynindx;
4468 edir->elf.dynstr_index = eind->elf.dynstr_index;
4469 eind->elf.dynindx = -1;
4470 eind->elf.dynstr_index = 0;
4471 }
4472 }
4473
4474 /* Find the function descriptor hash entry from the given function code
4475 hash entry FH. Link the entries via their OH fields. */
4476
4477 static struct ppc_link_hash_entry *
4478 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4479 {
4480 struct ppc_link_hash_entry *fdh = fh->oh;
4481
4482 if (fdh == NULL)
4483 {
4484 const char *fd_name = fh->elf.root.root.string + 1;
4485
4486 fdh = (struct ppc_link_hash_entry *)
4487 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4488 if (fdh == NULL)
4489 return fdh;
4490
4491 fdh->is_func_descriptor = 1;
4492 fdh->oh = fh;
4493 fh->is_func = 1;
4494 fh->oh = fdh;
4495 }
4496
4497 return ppc_follow_link (fdh);
4498 }
4499
4500 /* Make a fake function descriptor sym for the code sym FH. */
4501
4502 static struct ppc_link_hash_entry *
4503 make_fdh (struct bfd_link_info *info,
4504 struct ppc_link_hash_entry *fh)
4505 {
4506 bfd *abfd;
4507 asymbol *newsym;
4508 struct bfd_link_hash_entry *bh;
4509 struct ppc_link_hash_entry *fdh;
4510
4511 abfd = fh->elf.root.u.undef.abfd;
4512 newsym = bfd_make_empty_symbol (abfd);
4513 newsym->name = fh->elf.root.root.string + 1;
4514 newsym->section = bfd_und_section_ptr;
4515 newsym->value = 0;
4516 newsym->flags = BSF_WEAK;
4517
4518 bh = NULL;
4519 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4520 newsym->flags, newsym->section,
4521 newsym->value, NULL, FALSE, FALSE,
4522 &bh))
4523 return NULL;
4524
4525 fdh = (struct ppc_link_hash_entry *) bh;
4526 fdh->elf.non_elf = 0;
4527 fdh->fake = 1;
4528 fdh->is_func_descriptor = 1;
4529 fdh->oh = fh;
4530 fh->is_func = 1;
4531 fh->oh = fdh;
4532 return fdh;
4533 }
4534
4535 /* Fix function descriptor symbols defined in .opd sections to be
4536 function type. */
4537
4538 static bfd_boolean
4539 ppc64_elf_add_symbol_hook (bfd *ibfd,
4540 struct bfd_link_info *info,
4541 Elf_Internal_Sym *isym,
4542 const char **name ATTRIBUTE_UNUSED,
4543 flagword *flags ATTRIBUTE_UNUSED,
4544 asection **sec,
4545 bfd_vma *value ATTRIBUTE_UNUSED)
4546 {
4547 if ((ibfd->flags & DYNAMIC) == 0
4548 && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4549 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4550
4551 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4552 {
4553 if ((ibfd->flags & DYNAMIC) == 0)
4554 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4555 }
4556 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4557 ;
4558 else if (*sec != NULL
4559 && strcmp ((*sec)->name, ".opd") == 0)
4560 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4561
4562 return TRUE;
4563 }
4564
4565 /* This function makes an old ABI object reference to ".bar" cause the
4566 inclusion of a new ABI object archive that defines "bar".
4567 NAME is a symbol defined in an archive. Return a symbol in the hash
4568 table that might be satisfied by the archive symbols. */
4569
4570 static struct elf_link_hash_entry *
4571 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4572 struct bfd_link_info *info,
4573 const char *name)
4574 {
4575 struct elf_link_hash_entry *h;
4576 char *dot_name;
4577 size_t len;
4578
4579 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4580 if (h != NULL
4581 /* Don't return this sym if it is a fake function descriptor
4582 created by add_symbol_adjust. */
4583 && !(h->root.type == bfd_link_hash_undefweak
4584 && ((struct ppc_link_hash_entry *) h)->fake))
4585 return h;
4586
4587 if (name[0] == '.')
4588 return h;
4589
4590 len = strlen (name);
4591 dot_name = bfd_alloc (abfd, len + 2);
4592 if (dot_name == NULL)
4593 return (struct elf_link_hash_entry *) 0 - 1;
4594 dot_name[0] = '.';
4595 memcpy (dot_name + 1, name, len + 1);
4596 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4597 bfd_release (abfd, dot_name);
4598 return h;
4599 }
4600
4601 /* This function satisfies all old ABI object references to ".bar" if a
4602 new ABI object defines "bar". Well, at least, undefined dot symbols
4603 are made weak. This stops later archive searches from including an
4604 object if we already have a function descriptor definition. It also
4605 prevents the linker complaining about undefined symbols.
4606 We also check and correct mismatched symbol visibility here. The
4607 most restrictive visibility of the function descriptor and the
4608 function entry symbol is used. */
4609
4610 static bfd_boolean
4611 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4612 {
4613 struct ppc_link_hash_table *htab;
4614 struct ppc_link_hash_entry *fdh;
4615
4616 if (eh->elf.root.type == bfd_link_hash_indirect)
4617 return TRUE;
4618
4619 if (eh->elf.root.type == bfd_link_hash_warning)
4620 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4621
4622 if (eh->elf.root.root.string[0] != '.')
4623 abort ();
4624
4625 htab = ppc_hash_table (info);
4626 if (htab == NULL)
4627 return FALSE;
4628
4629 fdh = lookup_fdh (eh, htab);
4630 if (fdh == NULL)
4631 {
4632 if (!info->relocatable
4633 && (eh->elf.root.type == bfd_link_hash_undefined
4634 || eh->elf.root.type == bfd_link_hash_undefweak)
4635 && eh->elf.ref_regular)
4636 {
4637 /* Make an undefweak function descriptor sym, which is enough to
4638 pull in an --as-needed shared lib, but won't cause link
4639 errors. Archives are handled elsewhere. */
4640 fdh = make_fdh (info, eh);
4641 if (fdh == NULL)
4642 return FALSE;
4643 fdh->elf.ref_regular = 1;
4644 }
4645 }
4646 else
4647 {
4648 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4649 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4650 if (entry_vis < descr_vis)
4651 fdh->elf.other += entry_vis - descr_vis;
4652 else if (entry_vis > descr_vis)
4653 eh->elf.other += descr_vis - entry_vis;
4654
4655 if ((fdh->elf.root.type == bfd_link_hash_defined
4656 || fdh->elf.root.type == bfd_link_hash_defweak)
4657 && eh->elf.root.type == bfd_link_hash_undefined)
4658 {
4659 eh->elf.root.type = bfd_link_hash_undefweak;
4660 eh->was_undefined = 1;
4661 htab->twiddled_syms = 1;
4662 }
4663 }
4664
4665 return TRUE;
4666 }
4667
4668 /* Process list of dot-symbols we made in link_hash_newfunc. */
4669
4670 static bfd_boolean
4671 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
4672 {
4673 struct ppc_link_hash_table *htab;
4674 struct ppc_link_hash_entry **p, *eh;
4675
4676 if (!is_ppc64_elf (info->output_bfd))
4677 return TRUE;
4678 htab = ppc_hash_table (info);
4679 if (htab == NULL)
4680 return FALSE;
4681
4682 if (is_ppc64_elf (ibfd))
4683 {
4684 p = &htab->dot_syms;
4685 while ((eh = *p) != NULL)
4686 {
4687 *p = NULL;
4688 if (!add_symbol_adjust (eh, info))
4689 return FALSE;
4690 p = &eh->u.next_dot_sym;
4691 }
4692 }
4693
4694 /* Clear the list for non-ppc64 input files. */
4695 p = &htab->dot_syms;
4696 while ((eh = *p) != NULL)
4697 {
4698 *p = NULL;
4699 p = &eh->u.next_dot_sym;
4700 }
4701
4702 /* We need to fix the undefs list for any syms we have twiddled to
4703 undef_weak. */
4704 if (htab->twiddled_syms)
4705 {
4706 bfd_link_repair_undef_list (&htab->elf.root);
4707 htab->twiddled_syms = 0;
4708 }
4709 return TRUE;
4710 }
4711
4712 /* Undo hash table changes when an --as-needed input file is determined
4713 not to be needed. */
4714
4715 static bfd_boolean
4716 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4717 struct bfd_link_info *info)
4718 {
4719 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4720
4721 if (htab == NULL)
4722 return FALSE;
4723
4724 htab->dot_syms = NULL;
4725 return TRUE;
4726 }
4727
4728 /* If --just-symbols against a final linked binary, then assume we need
4729 toc adjusting stubs when calling functions defined there. */
4730
4731 static void
4732 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4733 {
4734 if ((sec->flags & SEC_CODE) != 0
4735 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4736 && is_ppc64_elf (sec->owner))
4737 {
4738 asection *got = bfd_get_section_by_name (sec->owner, ".got");
4739 if (got != NULL
4740 && got->size >= elf_backend_got_header_size
4741 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4742 sec->has_toc_reloc = 1;
4743 }
4744 _bfd_elf_link_just_syms (sec, info);
4745 }
4746
4747 static struct plt_entry **
4748 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4749 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4750 {
4751 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4752 struct plt_entry **local_plt;
4753 unsigned char *local_got_tls_masks;
4754
4755 if (local_got_ents == NULL)
4756 {
4757 bfd_size_type size = symtab_hdr->sh_info;
4758
4759 size *= (sizeof (*local_got_ents)
4760 + sizeof (*local_plt)
4761 + sizeof (*local_got_tls_masks));
4762 local_got_ents = bfd_zalloc (abfd, size);
4763 if (local_got_ents == NULL)
4764 return NULL;
4765 elf_local_got_ents (abfd) = local_got_ents;
4766 }
4767
4768 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
4769 {
4770 struct got_entry *ent;
4771
4772 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4773 if (ent->addend == r_addend
4774 && ent->owner == abfd
4775 && ent->tls_type == tls_type)
4776 break;
4777 if (ent == NULL)
4778 {
4779 bfd_size_type amt = sizeof (*ent);
4780 ent = bfd_alloc (abfd, amt);
4781 if (ent == NULL)
4782 return FALSE;
4783 ent->next = local_got_ents[r_symndx];
4784 ent->addend = r_addend;
4785 ent->owner = abfd;
4786 ent->tls_type = tls_type;
4787 ent->is_indirect = FALSE;
4788 ent->got.refcount = 0;
4789 local_got_ents[r_symndx] = ent;
4790 }
4791 ent->got.refcount += 1;
4792 }
4793
4794 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4795 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4796 local_got_tls_masks[r_symndx] |= tls_type;
4797
4798 return local_plt + r_symndx;
4799 }
4800
4801 static bfd_boolean
4802 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4803 {
4804 struct plt_entry *ent;
4805
4806 for (ent = *plist; ent != NULL; ent = ent->next)
4807 if (ent->addend == addend)
4808 break;
4809 if (ent == NULL)
4810 {
4811 bfd_size_type amt = sizeof (*ent);
4812 ent = bfd_alloc (abfd, amt);
4813 if (ent == NULL)
4814 return FALSE;
4815 ent->next = *plist;
4816 ent->addend = addend;
4817 ent->plt.refcount = 0;
4818 *plist = ent;
4819 }
4820 ent->plt.refcount += 1;
4821 return TRUE;
4822 }
4823
4824 static bfd_boolean
4825 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4826 {
4827 return (r_type == R_PPC64_REL24
4828 || r_type == R_PPC64_REL14
4829 || r_type == R_PPC64_REL14_BRTAKEN
4830 || r_type == R_PPC64_REL14_BRNTAKEN
4831 || r_type == R_PPC64_ADDR24
4832 || r_type == R_PPC64_ADDR14
4833 || r_type == R_PPC64_ADDR14_BRTAKEN
4834 || r_type == R_PPC64_ADDR14_BRNTAKEN);
4835 }
4836
4837 /* Look through the relocs for a section during the first phase, and
4838 calculate needed space in the global offset table, procedure
4839 linkage table, and dynamic reloc sections. */
4840
4841 static bfd_boolean
4842 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4843 asection *sec, const Elf_Internal_Rela *relocs)
4844 {
4845 struct ppc_link_hash_table *htab;
4846 Elf_Internal_Shdr *symtab_hdr;
4847 struct elf_link_hash_entry **sym_hashes;
4848 const Elf_Internal_Rela *rel;
4849 const Elf_Internal_Rela *rel_end;
4850 asection *sreloc;
4851 asection **opd_sym_map;
4852 struct elf_link_hash_entry *tga, *dottga;
4853
4854 if (info->relocatable)
4855 return TRUE;
4856
4857 /* Don't do anything special with non-loaded, non-alloced sections.
4858 In particular, any relocs in such sections should not affect GOT
4859 and PLT reference counting (ie. we don't allow them to create GOT
4860 or PLT entries), there's no possibility or desire to optimize TLS
4861 relocs, and there's not much point in propagating relocs to shared
4862 libs that the dynamic linker won't relocate. */
4863 if ((sec->flags & SEC_ALLOC) == 0)
4864 return TRUE;
4865
4866 BFD_ASSERT (is_ppc64_elf (abfd));
4867
4868 htab = ppc_hash_table (info);
4869 if (htab == NULL)
4870 return FALSE;
4871
4872 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4873 FALSE, FALSE, TRUE);
4874 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4875 FALSE, FALSE, TRUE);
4876 symtab_hdr = &elf_symtab_hdr (abfd);
4877 sym_hashes = elf_sym_hashes (abfd);
4878 sreloc = NULL;
4879 opd_sym_map = NULL;
4880 if (strcmp (sec->name, ".opd") == 0)
4881 {
4882 /* Garbage collection needs some extra help with .opd sections.
4883 We don't want to necessarily keep everything referenced by
4884 relocs in .opd, as that would keep all functions. Instead,
4885 if we reference an .opd symbol (a function descriptor), we
4886 want to keep the function code symbol's section. This is
4887 easy for global symbols, but for local syms we need to keep
4888 information about the associated function section. */
4889 bfd_size_type amt;
4890
4891 amt = sec->size * sizeof (*opd_sym_map) / 8;
4892 opd_sym_map = bfd_zalloc (abfd, amt);
4893 if (opd_sym_map == NULL)
4894 return FALSE;
4895 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
4896 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4897 ppc64_elf_section_data (sec)->sec_type = sec_opd;
4898 }
4899
4900 if (htab->sfpr == NULL
4901 && !create_linkage_sections (htab->elf.dynobj, info))
4902 return FALSE;
4903
4904 rel_end = relocs + sec->reloc_count;
4905 for (rel = relocs; rel < rel_end; rel++)
4906 {
4907 unsigned long r_symndx;
4908 struct elf_link_hash_entry *h;
4909 enum elf_ppc64_reloc_type r_type;
4910 int tls_type;
4911 struct _ppc64_elf_section_data *ppc64_sec;
4912 struct plt_entry **ifunc;
4913
4914 r_symndx = ELF64_R_SYM (rel->r_info);
4915 if (r_symndx < symtab_hdr->sh_info)
4916 h = NULL;
4917 else
4918 {
4919 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4920 h = elf_follow_link (h);
4921 }
4922
4923 tls_type = 0;
4924 ifunc = NULL;
4925 if (h != NULL)
4926 {
4927 if (h->type == STT_GNU_IFUNC)
4928 {
4929 h->needs_plt = 1;
4930 ifunc = &h->plt.plist;
4931 }
4932 }
4933 else
4934 {
4935 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4936 abfd, r_symndx);
4937 if (isym == NULL)
4938 return FALSE;
4939
4940 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4941 {
4942 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4943 rel->r_addend, PLT_IFUNC);
4944 if (ifunc == NULL)
4945 return FALSE;
4946 }
4947 }
4948 r_type = ELF64_R_TYPE (rel->r_info);
4949 if (is_branch_reloc (r_type))
4950 {
4951 if (h != NULL && (h == tga || h == dottga))
4952 {
4953 if (rel != relocs
4954 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4955 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4956 /* We have a new-style __tls_get_addr call with a marker
4957 reloc. */
4958 ;
4959 else
4960 /* Mark this section as having an old-style call. */
4961 sec->has_tls_get_addr_call = 1;
4962 }
4963
4964 /* STT_GNU_IFUNC symbols must have a PLT entry. */
4965 if (ifunc != NULL
4966 && !update_plt_info (abfd, ifunc, rel->r_addend))
4967 return FALSE;
4968 }
4969
4970 switch (r_type)
4971 {
4972 case R_PPC64_TLSGD:
4973 case R_PPC64_TLSLD:
4974 /* These special tls relocs tie a call to __tls_get_addr with
4975 its parameter symbol. */
4976 break;
4977
4978 case R_PPC64_GOT_TLSLD16:
4979 case R_PPC64_GOT_TLSLD16_LO:
4980 case R_PPC64_GOT_TLSLD16_HI:
4981 case R_PPC64_GOT_TLSLD16_HA:
4982 tls_type = TLS_TLS | TLS_LD;
4983 goto dogottls;
4984
4985 case R_PPC64_GOT_TLSGD16:
4986 case R_PPC64_GOT_TLSGD16_LO:
4987 case R_PPC64_GOT_TLSGD16_HI:
4988 case R_PPC64_GOT_TLSGD16_HA:
4989 tls_type = TLS_TLS | TLS_GD;
4990 goto dogottls;
4991
4992 case R_PPC64_GOT_TPREL16_DS:
4993 case R_PPC64_GOT_TPREL16_LO_DS:
4994 case R_PPC64_GOT_TPREL16_HI:
4995 case R_PPC64_GOT_TPREL16_HA:
4996 if (!info->executable)
4997 info->flags |= DF_STATIC_TLS;
4998 tls_type = TLS_TLS | TLS_TPREL;
4999 goto dogottls;
5000
5001 case R_PPC64_GOT_DTPREL16_DS:
5002 case R_PPC64_GOT_DTPREL16_LO_DS:
5003 case R_PPC64_GOT_DTPREL16_HI:
5004 case R_PPC64_GOT_DTPREL16_HA:
5005 tls_type = TLS_TLS | TLS_DTPREL;
5006 dogottls:
5007 sec->has_tls_reloc = 1;
5008 /* Fall thru */
5009
5010 case R_PPC64_GOT16:
5011 case R_PPC64_GOT16_DS:
5012 case R_PPC64_GOT16_HA:
5013 case R_PPC64_GOT16_HI:
5014 case R_PPC64_GOT16_LO:
5015 case R_PPC64_GOT16_LO_DS:
5016 /* This symbol requires a global offset table entry. */
5017 sec->has_toc_reloc = 1;
5018 if (r_type == R_PPC64_GOT_TLSLD16
5019 || r_type == R_PPC64_GOT_TLSGD16
5020 || r_type == R_PPC64_GOT_TPREL16_DS
5021 || r_type == R_PPC64_GOT_DTPREL16_DS
5022 || r_type == R_PPC64_GOT16
5023 || r_type == R_PPC64_GOT16_DS)
5024 {
5025 htab->do_multi_toc = 1;
5026 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5027 }
5028
5029 if (ppc64_elf_tdata (abfd)->got == NULL
5030 && !create_got_section (abfd, info))
5031 return FALSE;
5032
5033 if (h != NULL)
5034 {
5035 struct ppc_link_hash_entry *eh;
5036 struct got_entry *ent;
5037
5038 eh = (struct ppc_link_hash_entry *) h;
5039 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5040 if (ent->addend == rel->r_addend
5041 && ent->owner == abfd
5042 && ent->tls_type == tls_type)
5043 break;
5044 if (ent == NULL)
5045 {
5046 bfd_size_type amt = sizeof (*ent);
5047 ent = bfd_alloc (abfd, amt);
5048 if (ent == NULL)
5049 return FALSE;
5050 ent->next = eh->elf.got.glist;
5051 ent->addend = rel->r_addend;
5052 ent->owner = abfd;
5053 ent->tls_type = tls_type;
5054 ent->is_indirect = FALSE;
5055 ent->got.refcount = 0;
5056 eh->elf.got.glist = ent;
5057 }
5058 ent->got.refcount += 1;
5059 eh->tls_mask |= tls_type;
5060 }
5061 else
5062 /* This is a global offset table entry for a local symbol. */
5063 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5064 rel->r_addend, tls_type))
5065 return FALSE;
5066 break;
5067
5068 case R_PPC64_PLT16_HA:
5069 case R_PPC64_PLT16_HI:
5070 case R_PPC64_PLT16_LO:
5071 case R_PPC64_PLT32:
5072 case R_PPC64_PLT64:
5073 /* This symbol requires a procedure linkage table entry. We
5074 actually build the entry in adjust_dynamic_symbol,
5075 because this might be a case of linking PIC code without
5076 linking in any dynamic objects, in which case we don't
5077 need to generate a procedure linkage table after all. */
5078 if (h == NULL)
5079 {
5080 /* It does not make sense to have a procedure linkage
5081 table entry for a local symbol. */
5082 bfd_set_error (bfd_error_bad_value);
5083 return FALSE;
5084 }
5085 else
5086 {
5087 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5088 return FALSE;
5089 h->needs_plt = 1;
5090 if (h->root.root.string[0] == '.'
5091 && h->root.root.string[1] != '\0')
5092 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5093 }
5094 break;
5095
5096 /* The following relocations don't need to propagate the
5097 relocation if linking a shared object since they are
5098 section relative. */
5099 case R_PPC64_SECTOFF:
5100 case R_PPC64_SECTOFF_LO:
5101 case R_PPC64_SECTOFF_HI:
5102 case R_PPC64_SECTOFF_HA:
5103 case R_PPC64_SECTOFF_DS:
5104 case R_PPC64_SECTOFF_LO_DS:
5105 case R_PPC64_DTPREL16:
5106 case R_PPC64_DTPREL16_LO:
5107 case R_PPC64_DTPREL16_HI:
5108 case R_PPC64_DTPREL16_HA:
5109 case R_PPC64_DTPREL16_DS:
5110 case R_PPC64_DTPREL16_LO_DS:
5111 case R_PPC64_DTPREL16_HIGHER:
5112 case R_PPC64_DTPREL16_HIGHERA:
5113 case R_PPC64_DTPREL16_HIGHEST:
5114 case R_PPC64_DTPREL16_HIGHESTA:
5115 break;
5116
5117 /* Nor do these. */
5118 case R_PPC64_REL16:
5119 case R_PPC64_REL16_LO:
5120 case R_PPC64_REL16_HI:
5121 case R_PPC64_REL16_HA:
5122 break;
5123
5124 case R_PPC64_TOC16:
5125 case R_PPC64_TOC16_DS:
5126 htab->do_multi_toc = 1;
5127 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5128 case R_PPC64_TOC16_LO:
5129 case R_PPC64_TOC16_HI:
5130 case R_PPC64_TOC16_HA:
5131 case R_PPC64_TOC16_LO_DS:
5132 sec->has_toc_reloc = 1;
5133 break;
5134
5135 /* This relocation describes the C++ object vtable hierarchy.
5136 Reconstruct it for later use during GC. */
5137 case R_PPC64_GNU_VTINHERIT:
5138 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5139 return FALSE;
5140 break;
5141
5142 /* This relocation describes which C++ vtable entries are actually
5143 used. Record for later use during GC. */
5144 case R_PPC64_GNU_VTENTRY:
5145 BFD_ASSERT (h != NULL);
5146 if (h != NULL
5147 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5148 return FALSE;
5149 break;
5150
5151 case R_PPC64_REL14:
5152 case R_PPC64_REL14_BRTAKEN:
5153 case R_PPC64_REL14_BRNTAKEN:
5154 {
5155 asection *dest = NULL;
5156
5157 /* Heuristic: If jumping outside our section, chances are
5158 we are going to need a stub. */
5159 if (h != NULL)
5160 {
5161 /* If the sym is weak it may be overridden later, so
5162 don't assume we know where a weak sym lives. */
5163 if (h->root.type == bfd_link_hash_defined)
5164 dest = h->root.u.def.section;
5165 }
5166 else
5167 {
5168 Elf_Internal_Sym *isym;
5169
5170 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5171 abfd, r_symndx);
5172 if (isym == NULL)
5173 return FALSE;
5174
5175 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5176 }
5177
5178 if (dest != sec)
5179 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5180 }
5181 /* Fall through. */
5182
5183 case R_PPC64_REL24:
5184 if (h != NULL && ifunc == NULL)
5185 {
5186 /* We may need a .plt entry if the function this reloc
5187 refers to is in a shared lib. */
5188 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5189 return FALSE;
5190 h->needs_plt = 1;
5191 if (h->root.root.string[0] == '.'
5192 && h->root.root.string[1] != '\0')
5193 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5194 if (h == tga || h == dottga)
5195 sec->has_tls_reloc = 1;
5196 }
5197 break;
5198
5199 case R_PPC64_TPREL64:
5200 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5201 if (!info->executable)
5202 info->flags |= DF_STATIC_TLS;
5203 goto dotlstoc;
5204
5205 case R_PPC64_DTPMOD64:
5206 if (rel + 1 < rel_end
5207 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5208 && rel[1].r_offset == rel->r_offset + 8)
5209 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5210 else
5211 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5212 goto dotlstoc;
5213
5214 case R_PPC64_DTPREL64:
5215 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5216 if (rel != relocs
5217 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5218 && rel[-1].r_offset == rel->r_offset - 8)
5219 /* This is the second reloc of a dtpmod, dtprel pair.
5220 Don't mark with TLS_DTPREL. */
5221 goto dodyn;
5222
5223 dotlstoc:
5224 sec->has_tls_reloc = 1;
5225 if (h != NULL)
5226 {
5227 struct ppc_link_hash_entry *eh;
5228 eh = (struct ppc_link_hash_entry *) h;
5229 eh->tls_mask |= tls_type;
5230 }
5231 else
5232 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5233 rel->r_addend, tls_type))
5234 return FALSE;
5235
5236 ppc64_sec = ppc64_elf_section_data (sec);
5237 if (ppc64_sec->sec_type != sec_toc)
5238 {
5239 bfd_size_type amt;
5240
5241 /* One extra to simplify get_tls_mask. */
5242 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5243 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5244 if (ppc64_sec->u.toc.symndx == NULL)
5245 return FALSE;
5246 amt = sec->size * sizeof (bfd_vma) / 8;
5247 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5248 if (ppc64_sec->u.toc.add == NULL)
5249 return FALSE;
5250 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5251 ppc64_sec->sec_type = sec_toc;
5252 }
5253 BFD_ASSERT (rel->r_offset % 8 == 0);
5254 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5255 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5256
5257 /* Mark the second slot of a GD or LD entry.
5258 -1 to indicate GD and -2 to indicate LD. */
5259 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5260 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5261 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5262 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5263 goto dodyn;
5264
5265 case R_PPC64_TPREL16:
5266 case R_PPC64_TPREL16_LO:
5267 case R_PPC64_TPREL16_HI:
5268 case R_PPC64_TPREL16_HA:
5269 case R_PPC64_TPREL16_DS:
5270 case R_PPC64_TPREL16_LO_DS:
5271 case R_PPC64_TPREL16_HIGHER:
5272 case R_PPC64_TPREL16_HIGHERA:
5273 case R_PPC64_TPREL16_HIGHEST:
5274 case R_PPC64_TPREL16_HIGHESTA:
5275 if (info->shared)
5276 {
5277 if (!info->executable)
5278 info->flags |= DF_STATIC_TLS;
5279 goto dodyn;
5280 }
5281 break;
5282
5283 case R_PPC64_ADDR64:
5284 if (opd_sym_map != NULL
5285 && rel + 1 < rel_end
5286 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5287 {
5288 if (h != NULL)
5289 {
5290 if (h->root.root.string[0] == '.'
5291 && h->root.root.string[1] != 0
5292 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5293 ;
5294 else
5295 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5296 }
5297 else
5298 {
5299 asection *s;
5300 Elf_Internal_Sym *isym;
5301
5302 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5303 abfd, r_symndx);
5304 if (isym == NULL)
5305 return FALSE;
5306
5307 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5308 if (s != NULL && s != sec)
5309 opd_sym_map[rel->r_offset / 8] = s;
5310 }
5311 }
5312 /* Fall through. */
5313
5314 case R_PPC64_REL30:
5315 case R_PPC64_REL32:
5316 case R_PPC64_REL64:
5317 case R_PPC64_ADDR14:
5318 case R_PPC64_ADDR14_BRNTAKEN:
5319 case R_PPC64_ADDR14_BRTAKEN:
5320 case R_PPC64_ADDR16:
5321 case R_PPC64_ADDR16_DS:
5322 case R_PPC64_ADDR16_HA:
5323 case R_PPC64_ADDR16_HI:
5324 case R_PPC64_ADDR16_HIGHER:
5325 case R_PPC64_ADDR16_HIGHERA:
5326 case R_PPC64_ADDR16_HIGHEST:
5327 case R_PPC64_ADDR16_HIGHESTA:
5328 case R_PPC64_ADDR16_LO:
5329 case R_PPC64_ADDR16_LO_DS:
5330 case R_PPC64_ADDR24:
5331 case R_PPC64_ADDR32:
5332 case R_PPC64_UADDR16:
5333 case R_PPC64_UADDR32:
5334 case R_PPC64_UADDR64:
5335 case R_PPC64_TOC:
5336 if (h != NULL && !info->shared)
5337 /* We may need a copy reloc. */
5338 h->non_got_ref = 1;
5339
5340 /* Don't propagate .opd relocs. */
5341 if (NO_OPD_RELOCS && opd_sym_map != NULL)
5342 break;
5343
5344 /* If we are creating a shared library, and this is a reloc
5345 against a global symbol, or a non PC relative reloc
5346 against a local symbol, then we need to copy the reloc
5347 into the shared library. However, if we are linking with
5348 -Bsymbolic, we do not need to copy a reloc against a
5349 global symbol which is defined in an object we are
5350 including in the link (i.e., DEF_REGULAR is set). At
5351 this point we have not seen all the input files, so it is
5352 possible that DEF_REGULAR is not set now but will be set
5353 later (it is never cleared). In case of a weak definition,
5354 DEF_REGULAR may be cleared later by a strong definition in
5355 a shared library. We account for that possibility below by
5356 storing information in the dyn_relocs field of the hash
5357 table entry. A similar situation occurs when creating
5358 shared libraries and symbol visibility changes render the
5359 symbol local.
5360
5361 If on the other hand, we are creating an executable, we
5362 may need to keep relocations for symbols satisfied by a
5363 dynamic library if we manage to avoid copy relocs for the
5364 symbol. */
5365 dodyn:
5366 if ((info->shared
5367 && (must_be_dyn_reloc (info, r_type)
5368 || (h != NULL
5369 && (! info->symbolic
5370 || h->root.type == bfd_link_hash_defweak
5371 || !h->def_regular))))
5372 || (ELIMINATE_COPY_RELOCS
5373 && !info->shared
5374 && h != NULL
5375 && (h->root.type == bfd_link_hash_defweak
5376 || !h->def_regular))
5377 || (!info->shared
5378 && ifunc != NULL))
5379 {
5380 struct elf_dyn_relocs *p;
5381 struct elf_dyn_relocs **head;
5382
5383 /* We must copy these reloc types into the output file.
5384 Create a reloc section in dynobj and make room for
5385 this reloc. */
5386 if (sreloc == NULL)
5387 {
5388 sreloc = _bfd_elf_make_dynamic_reloc_section
5389 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5390
5391 if (sreloc == NULL)
5392 return FALSE;
5393 }
5394
5395 /* If this is a global symbol, we count the number of
5396 relocations we need for this symbol. */
5397 if (h != NULL)
5398 {
5399 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5400 }
5401 else
5402 {
5403 /* Track dynamic relocs needed for local syms too.
5404 We really need local syms available to do this
5405 easily. Oh well. */
5406 asection *s;
5407 void *vpp;
5408 Elf_Internal_Sym *isym;
5409
5410 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5411 abfd, r_symndx);
5412 if (isym == NULL)
5413 return FALSE;
5414
5415 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5416 if (s == NULL)
5417 s = sec;
5418
5419 vpp = &elf_section_data (s)->local_dynrel;
5420 head = (struct elf_dyn_relocs **) vpp;
5421 }
5422
5423 p = *head;
5424 if (p == NULL || p->sec != sec)
5425 {
5426 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5427 if (p == NULL)
5428 return FALSE;
5429 p->next = *head;
5430 *head = p;
5431 p->sec = sec;
5432 p->count = 0;
5433 p->pc_count = 0;
5434 }
5435
5436 p->count += 1;
5437 if (!must_be_dyn_reloc (info, r_type))
5438 p->pc_count += 1;
5439 }
5440 break;
5441
5442 default:
5443 break;
5444 }
5445 }
5446
5447 return TRUE;
5448 }
5449
5450 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5451 of the code entry point, and its section. */
5452
5453 static bfd_vma
5454 opd_entry_value (asection *opd_sec,
5455 bfd_vma offset,
5456 asection **code_sec,
5457 bfd_vma *code_off)
5458 {
5459 bfd *opd_bfd = opd_sec->owner;
5460 Elf_Internal_Rela *relocs;
5461 Elf_Internal_Rela *lo, *hi, *look;
5462 bfd_vma val;
5463
5464 /* No relocs implies we are linking a --just-symbols object. */
5465 if (opd_sec->reloc_count == 0)
5466 {
5467 char buf[8];
5468
5469 if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
5470 return (bfd_vma) -1;
5471
5472 val = bfd_get_64 (opd_bfd, buf);
5473 if (code_sec != NULL)
5474 {
5475 asection *sec, *likely = NULL;
5476 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5477 if (sec->vma <= val
5478 && (sec->flags & SEC_LOAD) != 0
5479 && (sec->flags & SEC_ALLOC) != 0)
5480 likely = sec;
5481 if (likely != NULL)
5482 {
5483 *code_sec = likely;
5484 if (code_off != NULL)
5485 *code_off = val - likely->vma;
5486 }
5487 }
5488 return val;
5489 }
5490
5491 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5492
5493 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5494 if (relocs == NULL)
5495 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5496
5497 /* Go find the opd reloc at the sym address. */
5498 lo = relocs;
5499 BFD_ASSERT (lo != NULL);
5500 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5501 val = (bfd_vma) -1;
5502 while (lo < hi)
5503 {
5504 look = lo + (hi - lo) / 2;
5505 if (look->r_offset < offset)
5506 lo = look + 1;
5507 else if (look->r_offset > offset)
5508 hi = look;
5509 else
5510 {
5511 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5512
5513 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5514 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5515 {
5516 unsigned long symndx = ELF64_R_SYM (look->r_info);
5517 asection *sec;
5518
5519 if (symndx < symtab_hdr->sh_info)
5520 {
5521 Elf_Internal_Sym *sym;
5522
5523 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5524 if (sym == NULL)
5525 {
5526 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5527 symtab_hdr->sh_info,
5528 0, NULL, NULL, NULL);
5529 if (sym == NULL)
5530 break;
5531 symtab_hdr->contents = (bfd_byte *) sym;
5532 }
5533
5534 sym += symndx;
5535 val = sym->st_value;
5536 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5537 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5538 }
5539 else
5540 {
5541 struct elf_link_hash_entry **sym_hashes;
5542 struct elf_link_hash_entry *rh;
5543
5544 sym_hashes = elf_sym_hashes (opd_bfd);
5545 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5546 rh = elf_follow_link (rh);
5547 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5548 || rh->root.type == bfd_link_hash_defweak);
5549 val = rh->root.u.def.value;
5550 sec = rh->root.u.def.section;
5551 }
5552 val += look->r_addend;
5553 if (code_off != NULL)
5554 *code_off = val;
5555 if (code_sec != NULL)
5556 *code_sec = sec;
5557 if (sec != NULL && sec->output_section != NULL)
5558 val += sec->output_section->vma + sec->output_offset;
5559 }
5560 break;
5561 }
5562 }
5563
5564 return val;
5565 }
5566
5567 /* Return true if symbol is defined in a regular object file. */
5568
5569 static bfd_boolean
5570 is_static_defined (struct elf_link_hash_entry *h)
5571 {
5572 return ((h->root.type == bfd_link_hash_defined
5573 || h->root.type == bfd_link_hash_defweak)
5574 && h->root.u.def.section != NULL
5575 && h->root.u.def.section->output_section != NULL);
5576 }
5577
5578 /* If FDH is a function descriptor symbol, return the associated code
5579 entry symbol if it is defined. Return NULL otherwise. */
5580
5581 static struct ppc_link_hash_entry *
5582 defined_code_entry (struct ppc_link_hash_entry *fdh)
5583 {
5584 if (fdh->is_func_descriptor)
5585 {
5586 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5587 if (fh->elf.root.type == bfd_link_hash_defined
5588 || fh->elf.root.type == bfd_link_hash_defweak)
5589 return fh;
5590 }
5591 return NULL;
5592 }
5593
5594 /* If FH is a function code entry symbol, return the associated
5595 function descriptor symbol if it is defined. Return NULL otherwise. */
5596
5597 static struct ppc_link_hash_entry *
5598 defined_func_desc (struct ppc_link_hash_entry *fh)
5599 {
5600 if (fh->oh != NULL
5601 && fh->oh->is_func_descriptor)
5602 {
5603 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5604 if (fdh->elf.root.type == bfd_link_hash_defined
5605 || fdh->elf.root.type == bfd_link_hash_defweak)
5606 return fdh;
5607 }
5608 return NULL;
5609 }
5610
5611 /* Mark all our entry sym sections, both opd and code section. */
5612
5613 static void
5614 ppc64_elf_gc_keep (struct bfd_link_info *info)
5615 {
5616 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5617 struct bfd_sym_chain *sym;
5618
5619 if (htab == NULL)
5620 return;
5621
5622 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5623 {
5624 struct ppc_link_hash_entry *eh, *fh;
5625 asection *sec;
5626
5627 eh = (struct ppc_link_hash_entry *)
5628 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5629 if (eh == NULL)
5630 continue;
5631 if (eh->elf.root.type != bfd_link_hash_defined
5632 && eh->elf.root.type != bfd_link_hash_defweak)
5633 continue;
5634
5635 fh = defined_code_entry (eh);
5636 if (fh != NULL)
5637 {
5638 sec = fh->elf.root.u.def.section;
5639 sec->flags |= SEC_KEEP;
5640 }
5641 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5642 && opd_entry_value (eh->elf.root.u.def.section,
5643 eh->elf.root.u.def.value,
5644 &sec, NULL) != (bfd_vma) -1)
5645 sec->flags |= SEC_KEEP;
5646
5647 sec = eh->elf.root.u.def.section;
5648 sec->flags |= SEC_KEEP;
5649 }
5650 }
5651
5652 /* Mark sections containing dynamically referenced symbols. When
5653 building shared libraries, we must assume that any visible symbol is
5654 referenced. */
5655
5656 static bfd_boolean
5657 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5658 {
5659 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5660 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5661 struct ppc_link_hash_entry *fdh;
5662
5663 /* Dynamic linking info is on the func descriptor sym. */
5664 fdh = defined_func_desc (eh);
5665 if (fdh != NULL)
5666 eh = fdh;
5667
5668 if ((eh->elf.root.type == bfd_link_hash_defined
5669 || eh->elf.root.type == bfd_link_hash_defweak)
5670 && (eh->elf.ref_dynamic
5671 || (!info->executable
5672 && eh->elf.def_regular
5673 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5674 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
5675 {
5676 asection *code_sec;
5677 struct ppc_link_hash_entry *fh;
5678
5679 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5680
5681 /* Function descriptor syms cause the associated
5682 function code sym section to be marked. */
5683 fh = defined_code_entry (eh);
5684 if (fh != NULL)
5685 {
5686 code_sec = fh->elf.root.u.def.section;
5687 code_sec->flags |= SEC_KEEP;
5688 }
5689 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5690 && opd_entry_value (eh->elf.root.u.def.section,
5691 eh->elf.root.u.def.value,
5692 &code_sec, NULL) != (bfd_vma) -1)
5693 code_sec->flags |= SEC_KEEP;
5694 }
5695
5696 return TRUE;
5697 }
5698
5699 /* Return the section that should be marked against GC for a given
5700 relocation. */
5701
5702 static asection *
5703 ppc64_elf_gc_mark_hook (asection *sec,
5704 struct bfd_link_info *info,
5705 Elf_Internal_Rela *rel,
5706 struct elf_link_hash_entry *h,
5707 Elf_Internal_Sym *sym)
5708 {
5709 asection *rsec;
5710
5711 /* Syms return NULL if we're marking .opd, so we avoid marking all
5712 function sections, as all functions are referenced in .opd. */
5713 rsec = NULL;
5714 if (get_opd_info (sec) != NULL)
5715 return rsec;
5716
5717 if (h != NULL)
5718 {
5719 enum elf_ppc64_reloc_type r_type;
5720 struct ppc_link_hash_entry *eh, *fh, *fdh;
5721
5722 r_type = ELF64_R_TYPE (rel->r_info);
5723 switch (r_type)
5724 {
5725 case R_PPC64_GNU_VTINHERIT:
5726 case R_PPC64_GNU_VTENTRY:
5727 break;
5728
5729 default:
5730 switch (h->root.type)
5731 {
5732 case bfd_link_hash_defined:
5733 case bfd_link_hash_defweak:
5734 eh = (struct ppc_link_hash_entry *) h;
5735 fdh = defined_func_desc (eh);
5736 if (fdh != NULL)
5737 eh = fdh;
5738
5739 /* Function descriptor syms cause the associated
5740 function code sym section to be marked. */
5741 fh = defined_code_entry (eh);
5742 if (fh != NULL)
5743 {
5744 /* They also mark their opd section. */
5745 eh->elf.root.u.def.section->gc_mark = 1;
5746
5747 rsec = fh->elf.root.u.def.section;
5748 }
5749 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5750 && opd_entry_value (eh->elf.root.u.def.section,
5751 eh->elf.root.u.def.value,
5752 &rsec, NULL) != (bfd_vma) -1)
5753 eh->elf.root.u.def.section->gc_mark = 1;
5754 else
5755 rsec = h->root.u.def.section;
5756 break;
5757
5758 case bfd_link_hash_common:
5759 rsec = h->root.u.c.p->section;
5760 break;
5761
5762 default:
5763 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5764 }
5765 }
5766 }
5767 else
5768 {
5769 struct _opd_sec_data *opd;
5770
5771 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5772 opd = get_opd_info (rsec);
5773 if (opd != NULL && opd->func_sec != NULL)
5774 {
5775 rsec->gc_mark = 1;
5776
5777 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
5778 }
5779 }
5780
5781 return rsec;
5782 }
5783
5784 /* Update the .got, .plt. and dynamic reloc reference counts for the
5785 section being removed. */
5786
5787 static bfd_boolean
5788 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5789 asection *sec, const Elf_Internal_Rela *relocs)
5790 {
5791 struct ppc_link_hash_table *htab;
5792 Elf_Internal_Shdr *symtab_hdr;
5793 struct elf_link_hash_entry **sym_hashes;
5794 struct got_entry **local_got_ents;
5795 const Elf_Internal_Rela *rel, *relend;
5796
5797 if (info->relocatable)
5798 return TRUE;
5799
5800 if ((sec->flags & SEC_ALLOC) == 0)
5801 return TRUE;
5802
5803 elf_section_data (sec)->local_dynrel = NULL;
5804
5805 htab = ppc_hash_table (info);
5806 if (htab == NULL)
5807 return FALSE;
5808
5809 symtab_hdr = &elf_symtab_hdr (abfd);
5810 sym_hashes = elf_sym_hashes (abfd);
5811 local_got_ents = elf_local_got_ents (abfd);
5812
5813 relend = relocs + sec->reloc_count;
5814 for (rel = relocs; rel < relend; rel++)
5815 {
5816 unsigned long r_symndx;
5817 enum elf_ppc64_reloc_type r_type;
5818 struct elf_link_hash_entry *h = NULL;
5819 unsigned char tls_type = 0;
5820
5821 r_symndx = ELF64_R_SYM (rel->r_info);
5822 r_type = ELF64_R_TYPE (rel->r_info);
5823 if (r_symndx >= symtab_hdr->sh_info)
5824 {
5825 struct ppc_link_hash_entry *eh;
5826 struct elf_dyn_relocs **pp;
5827 struct elf_dyn_relocs *p;
5828
5829 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5830 h = elf_follow_link (h);
5831 eh = (struct ppc_link_hash_entry *) h;
5832
5833 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5834 if (p->sec == sec)
5835 {
5836 /* Everything must go for SEC. */
5837 *pp = p->next;
5838 break;
5839 }
5840 }
5841
5842 if (is_branch_reloc (r_type))
5843 {
5844 struct plt_entry **ifunc = NULL;
5845 if (h != NULL)
5846 {
5847 if (h->type == STT_GNU_IFUNC)
5848 ifunc = &h->plt.plist;
5849 }
5850 else if (local_got_ents != NULL)
5851 {
5852 struct plt_entry **local_plt = (struct plt_entry **)
5853 (local_got_ents + symtab_hdr->sh_info);
5854 unsigned char *local_got_tls_masks = (unsigned char *)
5855 (local_plt + symtab_hdr->sh_info);
5856 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5857 ifunc = local_plt + r_symndx;
5858 }
5859 if (ifunc != NULL)
5860 {
5861 struct plt_entry *ent;
5862
5863 for (ent = *ifunc; ent != NULL; ent = ent->next)
5864 if (ent->addend == rel->r_addend)
5865 break;
5866 if (ent == NULL)
5867 abort ();
5868 if (ent->plt.refcount > 0)
5869 ent->plt.refcount -= 1;
5870 continue;
5871 }
5872 }
5873
5874 switch (r_type)
5875 {
5876 case R_PPC64_GOT_TLSLD16:
5877 case R_PPC64_GOT_TLSLD16_LO:
5878 case R_PPC64_GOT_TLSLD16_HI:
5879 case R_PPC64_GOT_TLSLD16_HA:
5880 tls_type = TLS_TLS | TLS_LD;
5881 goto dogot;
5882
5883 case R_PPC64_GOT_TLSGD16:
5884 case R_PPC64_GOT_TLSGD16_LO:
5885 case R_PPC64_GOT_TLSGD16_HI:
5886 case R_PPC64_GOT_TLSGD16_HA:
5887 tls_type = TLS_TLS | TLS_GD;
5888 goto dogot;
5889
5890 case R_PPC64_GOT_TPREL16_DS:
5891 case R_PPC64_GOT_TPREL16_LO_DS:
5892 case R_PPC64_GOT_TPREL16_HI:
5893 case R_PPC64_GOT_TPREL16_HA:
5894 tls_type = TLS_TLS | TLS_TPREL;
5895 goto dogot;
5896
5897 case R_PPC64_GOT_DTPREL16_DS:
5898 case R_PPC64_GOT_DTPREL16_LO_DS:
5899 case R_PPC64_GOT_DTPREL16_HI:
5900 case R_PPC64_GOT_DTPREL16_HA:
5901 tls_type = TLS_TLS | TLS_DTPREL;
5902 goto dogot;
5903
5904 case R_PPC64_GOT16:
5905 case R_PPC64_GOT16_DS:
5906 case R_PPC64_GOT16_HA:
5907 case R_PPC64_GOT16_HI:
5908 case R_PPC64_GOT16_LO:
5909 case R_PPC64_GOT16_LO_DS:
5910 dogot:
5911 {
5912 struct got_entry *ent;
5913
5914 if (h != NULL)
5915 ent = h->got.glist;
5916 else
5917 ent = local_got_ents[r_symndx];
5918
5919 for (; ent != NULL; ent = ent->next)
5920 if (ent->addend == rel->r_addend
5921 && ent->owner == abfd
5922 && ent->tls_type == tls_type)
5923 break;
5924 if (ent == NULL)
5925 abort ();
5926 if (ent->got.refcount > 0)
5927 ent->got.refcount -= 1;
5928 }
5929 break;
5930
5931 case R_PPC64_PLT16_HA:
5932 case R_PPC64_PLT16_HI:
5933 case R_PPC64_PLT16_LO:
5934 case R_PPC64_PLT32:
5935 case R_PPC64_PLT64:
5936 case R_PPC64_REL14:
5937 case R_PPC64_REL14_BRNTAKEN:
5938 case R_PPC64_REL14_BRTAKEN:
5939 case R_PPC64_REL24:
5940 if (h != NULL)
5941 {
5942 struct plt_entry *ent;
5943
5944 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5945 if (ent->addend == rel->r_addend)
5946 break;
5947 if (ent != NULL && ent->plt.refcount > 0)
5948 ent->plt.refcount -= 1;
5949 }
5950 break;
5951
5952 default:
5953 break;
5954 }
5955 }
5956 return TRUE;
5957 }
5958
5959 /* The maximum size of .sfpr. */
5960 #define SFPR_MAX (218*4)
5961
5962 struct sfpr_def_parms
5963 {
5964 const char name[12];
5965 unsigned char lo, hi;
5966 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5967 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5968 };
5969
5970 /* Auto-generate _save*, _rest* functions in .sfpr. */
5971
5972 static bfd_boolean
5973 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5974 {
5975 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5976 unsigned int i;
5977 size_t len = strlen (parm->name);
5978 bfd_boolean writing = FALSE;
5979 char sym[16];
5980
5981 if (htab == NULL)
5982 return FALSE;
5983
5984 memcpy (sym, parm->name, len);
5985 sym[len + 2] = 0;
5986
5987 for (i = parm->lo; i <= parm->hi; i++)
5988 {
5989 struct elf_link_hash_entry *h;
5990
5991 sym[len + 0] = i / 10 + '0';
5992 sym[len + 1] = i % 10 + '0';
5993 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
5994 if (h != NULL
5995 && !h->def_regular)
5996 {
5997 h->root.type = bfd_link_hash_defined;
5998 h->root.u.def.section = htab->sfpr;
5999 h->root.u.def.value = htab->sfpr->size;
6000 h->type = STT_FUNC;
6001 h->def_regular = 1;
6002 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6003 writing = TRUE;
6004 if (htab->sfpr->contents == NULL)
6005 {
6006 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6007 if (htab->sfpr->contents == NULL)
6008 return FALSE;
6009 }
6010 }
6011 if (writing)
6012 {
6013 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6014 if (i != parm->hi)
6015 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6016 else
6017 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6018 htab->sfpr->size = p - htab->sfpr->contents;
6019 }
6020 }
6021
6022 return TRUE;
6023 }
6024
6025 static bfd_byte *
6026 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6027 {
6028 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6029 return p + 4;
6030 }
6031
6032 static bfd_byte *
6033 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6034 {
6035 p = savegpr0 (abfd, p, r);
6036 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6037 p = p + 4;
6038 bfd_put_32 (abfd, BLR, p);
6039 return p + 4;
6040 }
6041
6042 static bfd_byte *
6043 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6044 {
6045 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6046 return p + 4;
6047 }
6048
6049 static bfd_byte *
6050 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6051 {
6052 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6053 p = p + 4;
6054 p = restgpr0 (abfd, p, r);
6055 bfd_put_32 (abfd, MTLR_R0, p);
6056 p = p + 4;
6057 if (r == 29)
6058 {
6059 p = restgpr0 (abfd, p, 30);
6060 p = restgpr0 (abfd, p, 31);
6061 }
6062 bfd_put_32 (abfd, BLR, p);
6063 return p + 4;
6064 }
6065
6066 static bfd_byte *
6067 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6068 {
6069 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6070 return p + 4;
6071 }
6072
6073 static bfd_byte *
6074 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6075 {
6076 p = savegpr1 (abfd, p, r);
6077 bfd_put_32 (abfd, BLR, p);
6078 return p + 4;
6079 }
6080
6081 static bfd_byte *
6082 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6083 {
6084 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6085 return p + 4;
6086 }
6087
6088 static bfd_byte *
6089 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6090 {
6091 p = restgpr1 (abfd, p, r);
6092 bfd_put_32 (abfd, BLR, p);
6093 return p + 4;
6094 }
6095
6096 static bfd_byte *
6097 savefpr (bfd *abfd, bfd_byte *p, int r)
6098 {
6099 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6100 return p + 4;
6101 }
6102
6103 static bfd_byte *
6104 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6105 {
6106 p = savefpr (abfd, p, r);
6107 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6108 p = p + 4;
6109 bfd_put_32 (abfd, BLR, p);
6110 return p + 4;
6111 }
6112
6113 static bfd_byte *
6114 restfpr (bfd *abfd, bfd_byte *p, int r)
6115 {
6116 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6117 return p + 4;
6118 }
6119
6120 static bfd_byte *
6121 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6122 {
6123 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6124 p = p + 4;
6125 p = restfpr (abfd, p, r);
6126 bfd_put_32 (abfd, MTLR_R0, p);
6127 p = p + 4;
6128 if (r == 29)
6129 {
6130 p = restfpr (abfd, p, 30);
6131 p = restfpr (abfd, p, 31);
6132 }
6133 bfd_put_32 (abfd, BLR, p);
6134 return p + 4;
6135 }
6136
6137 static bfd_byte *
6138 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6139 {
6140 p = savefpr (abfd, p, r);
6141 bfd_put_32 (abfd, BLR, p);
6142 return p + 4;
6143 }
6144
6145 static bfd_byte *
6146 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6147 {
6148 p = restfpr (abfd, p, r);
6149 bfd_put_32 (abfd, BLR, p);
6150 return p + 4;
6151 }
6152
6153 static bfd_byte *
6154 savevr (bfd *abfd, bfd_byte *p, int r)
6155 {
6156 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6157 p = p + 4;
6158 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6159 return p + 4;
6160 }
6161
6162 static bfd_byte *
6163 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6164 {
6165 p = savevr (abfd, p, r);
6166 bfd_put_32 (abfd, BLR, p);
6167 return p + 4;
6168 }
6169
6170 static bfd_byte *
6171 restvr (bfd *abfd, bfd_byte *p, int r)
6172 {
6173 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6174 p = p + 4;
6175 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6176 return p + 4;
6177 }
6178
6179 static bfd_byte *
6180 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6181 {
6182 p = restvr (abfd, p, r);
6183 bfd_put_32 (abfd, BLR, p);
6184 return p + 4;
6185 }
6186
6187 /* Called via elf_link_hash_traverse to transfer dynamic linking
6188 information on function code symbol entries to their corresponding
6189 function descriptor symbol entries. */
6190
6191 static bfd_boolean
6192 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6193 {
6194 struct bfd_link_info *info;
6195 struct ppc_link_hash_table *htab;
6196 struct plt_entry *ent;
6197 struct ppc_link_hash_entry *fh;
6198 struct ppc_link_hash_entry *fdh;
6199 bfd_boolean force_local;
6200
6201 fh = (struct ppc_link_hash_entry *) h;
6202 if (fh->elf.root.type == bfd_link_hash_indirect)
6203 return TRUE;
6204
6205 info = inf;
6206 htab = ppc_hash_table (info);
6207 if (htab == NULL)
6208 return FALSE;
6209
6210 /* Resolve undefined references to dot-symbols as the value
6211 in the function descriptor, if we have one in a regular object.
6212 This is to satisfy cases like ".quad .foo". Calls to functions
6213 in dynamic objects are handled elsewhere. */
6214 if (fh->elf.root.type == bfd_link_hash_undefweak
6215 && fh->was_undefined
6216 && (fdh = defined_func_desc (fh)) != NULL
6217 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6218 && opd_entry_value (fdh->elf.root.u.def.section,
6219 fdh->elf.root.u.def.value,
6220 &fh->elf.root.u.def.section,
6221 &fh->elf.root.u.def.value) != (bfd_vma) -1)
6222 {
6223 fh->elf.root.type = fdh->elf.root.type;
6224 fh->elf.forced_local = 1;
6225 fh->elf.def_regular = fdh->elf.def_regular;
6226 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6227 }
6228
6229 /* If this is a function code symbol, transfer dynamic linking
6230 information to the function descriptor symbol. */
6231 if (!fh->is_func)
6232 return TRUE;
6233
6234 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6235 if (ent->plt.refcount > 0)
6236 break;
6237 if (ent == NULL
6238 || fh->elf.root.root.string[0] != '.'
6239 || fh->elf.root.root.string[1] == '\0')
6240 return TRUE;
6241
6242 /* Find the corresponding function descriptor symbol. Create it
6243 as undefined if necessary. */
6244
6245 fdh = lookup_fdh (fh, htab);
6246 if (fdh == NULL
6247 && !info->executable
6248 && (fh->elf.root.type == bfd_link_hash_undefined
6249 || fh->elf.root.type == bfd_link_hash_undefweak))
6250 {
6251 fdh = make_fdh (info, fh);
6252 if (fdh == NULL)
6253 return FALSE;
6254 }
6255
6256 /* Fake function descriptors are made undefweak. If the function
6257 code symbol is strong undefined, make the fake sym the same.
6258 If the function code symbol is defined, then force the fake
6259 descriptor local; We can't support overriding of symbols in a
6260 shared library on a fake descriptor. */
6261
6262 if (fdh != NULL
6263 && fdh->fake
6264 && fdh->elf.root.type == bfd_link_hash_undefweak)
6265 {
6266 if (fh->elf.root.type == bfd_link_hash_undefined)
6267 {
6268 fdh->elf.root.type = bfd_link_hash_undefined;
6269 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6270 }
6271 else if (fh->elf.root.type == bfd_link_hash_defined
6272 || fh->elf.root.type == bfd_link_hash_defweak)
6273 {
6274 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6275 }
6276 }
6277
6278 if (fdh != NULL
6279 && !fdh->elf.forced_local
6280 && (!info->executable
6281 || fdh->elf.def_dynamic
6282 || fdh->elf.ref_dynamic
6283 || (fdh->elf.root.type == bfd_link_hash_undefweak
6284 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6285 {
6286 if (fdh->elf.dynindx == -1)
6287 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6288 return FALSE;
6289 fdh->elf.ref_regular |= fh->elf.ref_regular;
6290 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6291 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6292 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6293 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6294 {
6295 move_plt_plist (fh, fdh);
6296 fdh->elf.needs_plt = 1;
6297 }
6298 fdh->is_func_descriptor = 1;
6299 fdh->oh = fh;
6300 fh->oh = fdh;
6301 }
6302
6303 /* Now that the info is on the function descriptor, clear the
6304 function code sym info. Any function code syms for which we
6305 don't have a definition in a regular file, we force local.
6306 This prevents a shared library from exporting syms that have
6307 been imported from another library. Function code syms that
6308 are really in the library we must leave global to prevent the
6309 linker dragging in a definition from a static library. */
6310 force_local = (!fh->elf.def_regular
6311 || fdh == NULL
6312 || !fdh->elf.def_regular
6313 || fdh->elf.forced_local);
6314 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6315
6316 return TRUE;
6317 }
6318
6319 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6320 this hook to a) provide some gcc support functions, and b) transfer
6321 dynamic linking information gathered so far on function code symbol
6322 entries, to their corresponding function descriptor symbol entries. */
6323
6324 static bfd_boolean
6325 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6326 struct bfd_link_info *info)
6327 {
6328 struct ppc_link_hash_table *htab;
6329 unsigned int i;
6330 const struct sfpr_def_parms funcs[] =
6331 {
6332 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6333 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6334 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6335 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6336 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6337 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6338 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6339 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6340 { "._savef", 14, 31, savefpr, savefpr1_tail },
6341 { "._restf", 14, 31, restfpr, restfpr1_tail },
6342 { "_savevr_", 20, 31, savevr, savevr_tail },
6343 { "_restvr_", 20, 31, restvr, restvr_tail }
6344 };
6345
6346 htab = ppc_hash_table (info);
6347 if (htab == NULL)
6348 return FALSE;
6349
6350 if (htab->sfpr == NULL)
6351 /* We don't have any relocs. */
6352 return TRUE;
6353
6354 /* Provide any missing _save* and _rest* functions. */
6355 htab->sfpr->size = 0;
6356 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6357 if (!sfpr_define (info, &funcs[i]))
6358 return FALSE;
6359
6360 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6361
6362 if (htab->sfpr->size == 0)
6363 htab->sfpr->flags |= SEC_EXCLUDE;
6364
6365 return TRUE;
6366 }
6367
6368 /* Adjust a symbol defined by a dynamic object and referenced by a
6369 regular object. The current definition is in some section of the
6370 dynamic object, but we're not including those sections. We have to
6371 change the definition to something the rest of the link can
6372 understand. */
6373
6374 static bfd_boolean
6375 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6376 struct elf_link_hash_entry *h)
6377 {
6378 struct ppc_link_hash_table *htab;
6379 asection *s;
6380
6381 htab = ppc_hash_table (info);
6382 if (htab == NULL)
6383 return FALSE;
6384
6385 /* Deal with function syms. */
6386 if (h->type == STT_FUNC
6387 || h->type == STT_GNU_IFUNC
6388 || h->needs_plt)
6389 {
6390 /* Clear procedure linkage table information for any symbol that
6391 won't need a .plt entry. */
6392 struct plt_entry *ent;
6393 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6394 if (ent->plt.refcount > 0)
6395 break;
6396 if (ent == NULL
6397 || (h->type != STT_GNU_IFUNC
6398 && (SYMBOL_CALLS_LOCAL (info, h)
6399 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6400 && h->root.type == bfd_link_hash_undefweak))))
6401 {
6402 h->plt.plist = NULL;
6403 h->needs_plt = 0;
6404 }
6405 }
6406 else
6407 h->plt.plist = NULL;
6408
6409 /* If this is a weak symbol, and there is a real definition, the
6410 processor independent code will have arranged for us to see the
6411 real definition first, and we can just use the same value. */
6412 if (h->u.weakdef != NULL)
6413 {
6414 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6415 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6416 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6417 h->root.u.def.value = h->u.weakdef->root.u.def.value;
6418 if (ELIMINATE_COPY_RELOCS)
6419 h->non_got_ref = h->u.weakdef->non_got_ref;
6420 return TRUE;
6421 }
6422
6423 /* If we are creating a shared library, we must presume that the
6424 only references to the symbol are via the global offset table.
6425 For such cases we need not do anything here; the relocations will
6426 be handled correctly by relocate_section. */
6427 if (info->shared)
6428 return TRUE;
6429
6430 /* If there are no references to this symbol that do not use the
6431 GOT, we don't need to generate a copy reloc. */
6432 if (!h->non_got_ref)
6433 return TRUE;
6434
6435 /* Don't generate a copy reloc for symbols defined in the executable. */
6436 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6437 return TRUE;
6438
6439 if (ELIMINATE_COPY_RELOCS)
6440 {
6441 struct ppc_link_hash_entry * eh;
6442 struct elf_dyn_relocs *p;
6443
6444 eh = (struct ppc_link_hash_entry *) h;
6445 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6446 {
6447 s = p->sec->output_section;
6448 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6449 break;
6450 }
6451
6452 /* If we didn't find any dynamic relocs in read-only sections, then
6453 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6454 if (p == NULL)
6455 {
6456 h->non_got_ref = 0;
6457 return TRUE;
6458 }
6459 }
6460
6461 if (h->plt.plist != NULL)
6462 {
6463 /* We should never get here, but unfortunately there are versions
6464 of gcc out there that improperly (for this ABI) put initialized
6465 function pointers, vtable refs and suchlike in read-only
6466 sections. Allow them to proceed, but warn that this might
6467 break at runtime. */
6468 info->callbacks->einfo
6469 (_("%P: copy reloc against `%s' requires lazy plt linking; "
6470 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6471 h->root.root.string);
6472 }
6473
6474 /* This is a reference to a symbol defined by a dynamic object which
6475 is not a function. */
6476
6477 if (h->size == 0)
6478 {
6479 info->callbacks->einfo (_("%P: dynamic variable `%s' is zero size\n"),
6480 h->root.root.string);
6481 return TRUE;
6482 }
6483
6484 /* We must allocate the symbol in our .dynbss section, which will
6485 become part of the .bss section of the executable. There will be
6486 an entry for this symbol in the .dynsym section. The dynamic
6487 object will contain position independent code, so all references
6488 from the dynamic object to this symbol will go through the global
6489 offset table. The dynamic linker will use the .dynsym entry to
6490 determine the address it must put in the global offset table, so
6491 both the dynamic object and the regular object will refer to the
6492 same memory location for the variable. */
6493
6494 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6495 to copy the initial value out of the dynamic object and into the
6496 runtime process image. We need to remember the offset into the
6497 .rela.bss section we are going to use. */
6498 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6499 {
6500 htab->relbss->size += sizeof (Elf64_External_Rela);
6501 h->needs_copy = 1;
6502 }
6503
6504 s = htab->dynbss;
6505
6506 return _bfd_elf_adjust_dynamic_copy (h, s);
6507 }
6508
6509 /* If given a function descriptor symbol, hide both the function code
6510 sym and the descriptor. */
6511 static void
6512 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6513 struct elf_link_hash_entry *h,
6514 bfd_boolean force_local)
6515 {
6516 struct ppc_link_hash_entry *eh;
6517 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6518
6519 eh = (struct ppc_link_hash_entry *) h;
6520 if (eh->is_func_descriptor)
6521 {
6522 struct ppc_link_hash_entry *fh = eh->oh;
6523
6524 if (fh == NULL)
6525 {
6526 const char *p, *q;
6527 struct ppc_link_hash_table *htab;
6528 char save;
6529
6530 /* We aren't supposed to use alloca in BFD because on
6531 systems which do not have alloca the version in libiberty
6532 calls xmalloc, which might cause the program to crash
6533 when it runs out of memory. This function doesn't have a
6534 return status, so there's no way to gracefully return an
6535 error. So cheat. We know that string[-1] can be safely
6536 accessed; It's either a string in an ELF string table,
6537 or allocated in an objalloc structure. */
6538
6539 p = eh->elf.root.root.string - 1;
6540 save = *p;
6541 *(char *) p = '.';
6542 htab = ppc_hash_table (info);
6543 if (htab == NULL)
6544 return;
6545
6546 fh = (struct ppc_link_hash_entry *)
6547 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6548 *(char *) p = save;
6549
6550 /* Unfortunately, if it so happens that the string we were
6551 looking for was allocated immediately before this string,
6552 then we overwrote the string terminator. That's the only
6553 reason the lookup should fail. */
6554 if (fh == NULL)
6555 {
6556 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6557 while (q >= eh->elf.root.root.string && *q == *p)
6558 --q, --p;
6559 if (q < eh->elf.root.root.string && *p == '.')
6560 fh = (struct ppc_link_hash_entry *)
6561 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6562 }
6563 if (fh != NULL)
6564 {
6565 eh->oh = fh;
6566 fh->oh = eh;
6567 }
6568 }
6569 if (fh != NULL)
6570 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6571 }
6572 }
6573
6574 static bfd_boolean
6575 get_sym_h (struct elf_link_hash_entry **hp,
6576 Elf_Internal_Sym **symp,
6577 asection **symsecp,
6578 unsigned char **tls_maskp,
6579 Elf_Internal_Sym **locsymsp,
6580 unsigned long r_symndx,
6581 bfd *ibfd)
6582 {
6583 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6584
6585 if (r_symndx >= symtab_hdr->sh_info)
6586 {
6587 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6588 struct elf_link_hash_entry *h;
6589
6590 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6591 h = elf_follow_link (h);
6592
6593 if (hp != NULL)
6594 *hp = h;
6595
6596 if (symp != NULL)
6597 *symp = NULL;
6598
6599 if (symsecp != NULL)
6600 {
6601 asection *symsec = NULL;
6602 if (h->root.type == bfd_link_hash_defined
6603 || h->root.type == bfd_link_hash_defweak)
6604 symsec = h->root.u.def.section;
6605 *symsecp = symsec;
6606 }
6607
6608 if (tls_maskp != NULL)
6609 {
6610 struct ppc_link_hash_entry *eh;
6611
6612 eh = (struct ppc_link_hash_entry *) h;
6613 *tls_maskp = &eh->tls_mask;
6614 }
6615 }
6616 else
6617 {
6618 Elf_Internal_Sym *sym;
6619 Elf_Internal_Sym *locsyms = *locsymsp;
6620
6621 if (locsyms == NULL)
6622 {
6623 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6624 if (locsyms == NULL)
6625 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6626 symtab_hdr->sh_info,
6627 0, NULL, NULL, NULL);
6628 if (locsyms == NULL)
6629 return FALSE;
6630 *locsymsp = locsyms;
6631 }
6632 sym = locsyms + r_symndx;
6633
6634 if (hp != NULL)
6635 *hp = NULL;
6636
6637 if (symp != NULL)
6638 *symp = sym;
6639
6640 if (symsecp != NULL)
6641 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6642
6643 if (tls_maskp != NULL)
6644 {
6645 struct got_entry **lgot_ents;
6646 unsigned char *tls_mask;
6647
6648 tls_mask = NULL;
6649 lgot_ents = elf_local_got_ents (ibfd);
6650 if (lgot_ents != NULL)
6651 {
6652 struct plt_entry **local_plt = (struct plt_entry **)
6653 (lgot_ents + symtab_hdr->sh_info);
6654 unsigned char *lgot_masks = (unsigned char *)
6655 (local_plt + symtab_hdr->sh_info);
6656 tls_mask = &lgot_masks[r_symndx];
6657 }
6658 *tls_maskp = tls_mask;
6659 }
6660 }
6661 return TRUE;
6662 }
6663
6664 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6665 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6666 type suitable for optimization, and 1 otherwise. */
6667
6668 static int
6669 get_tls_mask (unsigned char **tls_maskp,
6670 unsigned long *toc_symndx,
6671 bfd_vma *toc_addend,
6672 Elf_Internal_Sym **locsymsp,
6673 const Elf_Internal_Rela *rel,
6674 bfd *ibfd)
6675 {
6676 unsigned long r_symndx;
6677 int next_r;
6678 struct elf_link_hash_entry *h;
6679 Elf_Internal_Sym *sym;
6680 asection *sec;
6681 bfd_vma off;
6682
6683 r_symndx = ELF64_R_SYM (rel->r_info);
6684 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6685 return 0;
6686
6687 if ((*tls_maskp != NULL && **tls_maskp != 0)
6688 || sec == NULL
6689 || ppc64_elf_section_data (sec) == NULL
6690 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6691 return 1;
6692
6693 /* Look inside a TOC section too. */
6694 if (h != NULL)
6695 {
6696 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6697 off = h->root.u.def.value;
6698 }
6699 else
6700 off = sym->st_value;
6701 off += rel->r_addend;
6702 BFD_ASSERT (off % 8 == 0);
6703 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6704 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6705 if (toc_symndx != NULL)
6706 *toc_symndx = r_symndx;
6707 if (toc_addend != NULL)
6708 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6709 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6710 return 0;
6711 if ((h == NULL || is_static_defined (h))
6712 && (next_r == -1 || next_r == -2))
6713 return 1 - next_r;
6714 return 1;
6715 }
6716
6717 /* Adjust all global syms defined in opd sections. In gcc generated
6718 code for the old ABI, these will already have been done. */
6719
6720 static bfd_boolean
6721 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6722 {
6723 struct ppc_link_hash_entry *eh;
6724 asection *sym_sec;
6725 struct _opd_sec_data *opd;
6726
6727 if (h->root.type == bfd_link_hash_indirect)
6728 return TRUE;
6729
6730 if (h->root.type != bfd_link_hash_defined
6731 && h->root.type != bfd_link_hash_defweak)
6732 return TRUE;
6733
6734 eh = (struct ppc_link_hash_entry *) h;
6735 if (eh->adjust_done)
6736 return TRUE;
6737
6738 sym_sec = eh->elf.root.u.def.section;
6739 opd = get_opd_info (sym_sec);
6740 if (opd != NULL && opd->adjust != NULL)
6741 {
6742 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
6743 if (adjust == -1)
6744 {
6745 /* This entry has been deleted. */
6746 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6747 if (dsec == NULL)
6748 {
6749 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6750 if (elf_discarded_section (dsec))
6751 {
6752 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6753 break;
6754 }
6755 }
6756 eh->elf.root.u.def.value = 0;
6757 eh->elf.root.u.def.section = dsec;
6758 }
6759 else
6760 eh->elf.root.u.def.value += adjust;
6761 eh->adjust_done = 1;
6762 }
6763 return TRUE;
6764 }
6765
6766 /* Handles decrementing dynamic reloc counts for the reloc specified by
6767 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6768 have already been determined. */
6769
6770 static bfd_boolean
6771 dec_dynrel_count (bfd_vma r_info,
6772 asection *sec,
6773 struct bfd_link_info *info,
6774 Elf_Internal_Sym **local_syms,
6775 struct elf_link_hash_entry *h,
6776 asection *sym_sec)
6777 {
6778 enum elf_ppc64_reloc_type r_type;
6779 struct elf_dyn_relocs *p;
6780 struct elf_dyn_relocs **pp;
6781
6782 /* Can this reloc be dynamic? This switch, and later tests here
6783 should be kept in sync with the code in check_relocs. */
6784 r_type = ELF64_R_TYPE (r_info);
6785 switch (r_type)
6786 {
6787 default:
6788 return TRUE;
6789
6790 case R_PPC64_TPREL16:
6791 case R_PPC64_TPREL16_LO:
6792 case R_PPC64_TPREL16_HI:
6793 case R_PPC64_TPREL16_HA:
6794 case R_PPC64_TPREL16_DS:
6795 case R_PPC64_TPREL16_LO_DS:
6796 case R_PPC64_TPREL16_HIGHER:
6797 case R_PPC64_TPREL16_HIGHERA:
6798 case R_PPC64_TPREL16_HIGHEST:
6799 case R_PPC64_TPREL16_HIGHESTA:
6800 if (!info->shared)
6801 return TRUE;
6802
6803 case R_PPC64_TPREL64:
6804 case R_PPC64_DTPMOD64:
6805 case R_PPC64_DTPREL64:
6806 case R_PPC64_ADDR64:
6807 case R_PPC64_REL30:
6808 case R_PPC64_REL32:
6809 case R_PPC64_REL64:
6810 case R_PPC64_ADDR14:
6811 case R_PPC64_ADDR14_BRNTAKEN:
6812 case R_PPC64_ADDR14_BRTAKEN:
6813 case R_PPC64_ADDR16:
6814 case R_PPC64_ADDR16_DS:
6815 case R_PPC64_ADDR16_HA:
6816 case R_PPC64_ADDR16_HI:
6817 case R_PPC64_ADDR16_HIGHER:
6818 case R_PPC64_ADDR16_HIGHERA:
6819 case R_PPC64_ADDR16_HIGHEST:
6820 case R_PPC64_ADDR16_HIGHESTA:
6821 case R_PPC64_ADDR16_LO:
6822 case R_PPC64_ADDR16_LO_DS:
6823 case R_PPC64_ADDR24:
6824 case R_PPC64_ADDR32:
6825 case R_PPC64_UADDR16:
6826 case R_PPC64_UADDR32:
6827 case R_PPC64_UADDR64:
6828 case R_PPC64_TOC:
6829 break;
6830 }
6831
6832 if (local_syms != NULL)
6833 {
6834 unsigned long r_symndx;
6835 Elf_Internal_Sym *sym;
6836 bfd *ibfd = sec->owner;
6837
6838 r_symndx = ELF64_R_SYM (r_info);
6839 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6840 return FALSE;
6841 }
6842
6843 if ((info->shared
6844 && (must_be_dyn_reloc (info, r_type)
6845 || (h != NULL
6846 && (!info->symbolic
6847 || h->root.type == bfd_link_hash_defweak
6848 || !h->def_regular))))
6849 || (ELIMINATE_COPY_RELOCS
6850 && !info->shared
6851 && h != NULL
6852 && (h->root.type == bfd_link_hash_defweak
6853 || !h->def_regular)))
6854 ;
6855 else
6856 return TRUE;
6857
6858 if (h != NULL)
6859 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6860 else
6861 {
6862 if (sym_sec != NULL)
6863 {
6864 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6865 pp = (struct elf_dyn_relocs **) vpp;
6866 }
6867 else
6868 {
6869 void *vpp = &elf_section_data (sec)->local_dynrel;
6870 pp = (struct elf_dyn_relocs **) vpp;
6871 }
6872
6873 /* elf_gc_sweep may have already removed all dyn relocs associated
6874 with local syms for a given section. Don't report a dynreloc
6875 miscount. */
6876 if (*pp == NULL)
6877 return TRUE;
6878 }
6879
6880 while ((p = *pp) != NULL)
6881 {
6882 if (p->sec == sec)
6883 {
6884 if (!must_be_dyn_reloc (info, r_type))
6885 p->pc_count -= 1;
6886 p->count -= 1;
6887 if (p->count == 0)
6888 *pp = p->next;
6889 return TRUE;
6890 }
6891 pp = &p->next;
6892 }
6893
6894 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
6895 sec->owner, sec);
6896 bfd_set_error (bfd_error_bad_value);
6897 return FALSE;
6898 }
6899
6900 /* Remove unused Official Procedure Descriptor entries. Currently we
6901 only remove those associated with functions in discarded link-once
6902 sections, or weakly defined functions that have been overridden. It
6903 would be possible to remove many more entries for statically linked
6904 applications. */
6905
6906 bfd_boolean
6907 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
6908 {
6909 bfd *ibfd;
6910 bfd_boolean some_edited = FALSE;
6911 asection *need_pad = NULL;
6912
6913 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6914 {
6915 asection *sec;
6916 Elf_Internal_Rela *relstart, *rel, *relend;
6917 Elf_Internal_Shdr *symtab_hdr;
6918 Elf_Internal_Sym *local_syms;
6919 bfd_vma offset;
6920 struct _opd_sec_data *opd;
6921 bfd_boolean need_edit, add_aux_fields;
6922 bfd_size_type cnt_16b = 0;
6923
6924 if (!is_ppc64_elf (ibfd))
6925 continue;
6926
6927 sec = bfd_get_section_by_name (ibfd, ".opd");
6928 if (sec == NULL || sec->size == 0)
6929 continue;
6930
6931 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6932 continue;
6933
6934 if (sec->output_section == bfd_abs_section_ptr)
6935 continue;
6936
6937 /* Look through the section relocs. */
6938 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6939 continue;
6940
6941 local_syms = NULL;
6942 symtab_hdr = &elf_symtab_hdr (ibfd);
6943
6944 /* Read the relocations. */
6945 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6946 info->keep_memory);
6947 if (relstart == NULL)
6948 return FALSE;
6949
6950 /* First run through the relocs to check they are sane, and to
6951 determine whether we need to edit this opd section. */
6952 need_edit = FALSE;
6953 need_pad = sec;
6954 offset = 0;
6955 relend = relstart + sec->reloc_count;
6956 for (rel = relstart; rel < relend; )
6957 {
6958 enum elf_ppc64_reloc_type r_type;
6959 unsigned long r_symndx;
6960 asection *sym_sec;
6961 struct elf_link_hash_entry *h;
6962 Elf_Internal_Sym *sym;
6963
6964 /* .opd contains a regular array of 16 or 24 byte entries. We're
6965 only interested in the reloc pointing to a function entry
6966 point. */
6967 if (rel->r_offset != offset
6968 || rel + 1 >= relend
6969 || (rel + 1)->r_offset != offset + 8)
6970 {
6971 /* If someone messes with .opd alignment then after a
6972 "ld -r" we might have padding in the middle of .opd.
6973 Also, there's nothing to prevent someone putting
6974 something silly in .opd with the assembler. No .opd
6975 optimization for them! */
6976 broken_opd:
6977 (*_bfd_error_handler)
6978 (_("%B: .opd is not a regular array of opd entries"), ibfd);
6979 need_edit = FALSE;
6980 break;
6981 }
6982
6983 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6984 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6985 {
6986 (*_bfd_error_handler)
6987 (_("%B: unexpected reloc type %u in .opd section"),
6988 ibfd, r_type);
6989 need_edit = FALSE;
6990 break;
6991 }
6992
6993 r_symndx = ELF64_R_SYM (rel->r_info);
6994 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6995 r_symndx, ibfd))
6996 goto error_ret;
6997
6998 if (sym_sec == NULL || sym_sec->owner == NULL)
6999 {
7000 const char *sym_name;
7001 if (h != NULL)
7002 sym_name = h->root.root.string;
7003 else
7004 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7005 sym_sec);
7006
7007 (*_bfd_error_handler)
7008 (_("%B: undefined sym `%s' in .opd section"),
7009 ibfd, sym_name);
7010 need_edit = FALSE;
7011 break;
7012 }
7013
7014 /* opd entries are always for functions defined in the
7015 current input bfd. If the symbol isn't defined in the
7016 input bfd, then we won't be using the function in this
7017 bfd; It must be defined in a linkonce section in another
7018 bfd, or is weak. It's also possible that we are
7019 discarding the function due to a linker script /DISCARD/,
7020 which we test for via the output_section. */
7021 if (sym_sec->owner != ibfd
7022 || sym_sec->output_section == bfd_abs_section_ptr)
7023 need_edit = TRUE;
7024
7025 rel += 2;
7026 if (rel == relend
7027 || (rel + 1 == relend && rel->r_offset == offset + 16))
7028 {
7029 if (sec->size == offset + 24)
7030 {
7031 need_pad = NULL;
7032 break;
7033 }
7034 if (rel == relend && sec->size == offset + 16)
7035 {
7036 cnt_16b++;
7037 break;
7038 }
7039 goto broken_opd;
7040 }
7041
7042 if (rel->r_offset == offset + 24)
7043 offset += 24;
7044 else if (rel->r_offset != offset + 16)
7045 goto broken_opd;
7046 else if (rel + 1 < relend
7047 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7048 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7049 {
7050 offset += 16;
7051 cnt_16b++;
7052 }
7053 else if (rel + 2 < relend
7054 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7055 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7056 {
7057 offset += 24;
7058 rel += 1;
7059 }
7060 else
7061 goto broken_opd;
7062 }
7063
7064 add_aux_fields = non_overlapping && cnt_16b > 0;
7065
7066 if (need_edit || add_aux_fields)
7067 {
7068 Elf_Internal_Rela *write_rel;
7069 Elf_Internal_Shdr *rel_hdr;
7070 bfd_byte *rptr, *wptr;
7071 bfd_byte *new_contents;
7072 bfd_boolean skip;
7073 long opd_ent_size;
7074 bfd_size_type amt;
7075
7076 new_contents = NULL;
7077 amt = sec->size * sizeof (long) / 8;
7078 opd = &ppc64_elf_section_data (sec)->u.opd;
7079 opd->adjust = bfd_zalloc (sec->owner, amt);
7080 if (opd->adjust == NULL)
7081 return FALSE;
7082 ppc64_elf_section_data (sec)->sec_type = sec_opd;
7083
7084 /* This seems a waste of time as input .opd sections are all
7085 zeros as generated by gcc, but I suppose there's no reason
7086 this will always be so. We might start putting something in
7087 the third word of .opd entries. */
7088 if ((sec->flags & SEC_IN_MEMORY) == 0)
7089 {
7090 bfd_byte *loc;
7091 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7092 {
7093 if (loc != NULL)
7094 free (loc);
7095 error_ret:
7096 if (local_syms != NULL
7097 && symtab_hdr->contents != (unsigned char *) local_syms)
7098 free (local_syms);
7099 if (elf_section_data (sec)->relocs != relstart)
7100 free (relstart);
7101 return FALSE;
7102 }
7103 sec->contents = loc;
7104 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7105 }
7106
7107 elf_section_data (sec)->relocs = relstart;
7108
7109 new_contents = sec->contents;
7110 if (add_aux_fields)
7111 {
7112 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7113 if (new_contents == NULL)
7114 return FALSE;
7115 need_pad = FALSE;
7116 }
7117 wptr = new_contents;
7118 rptr = sec->contents;
7119
7120 write_rel = relstart;
7121 skip = FALSE;
7122 offset = 0;
7123 opd_ent_size = 0;
7124 for (rel = relstart; rel < relend; rel++)
7125 {
7126 unsigned long r_symndx;
7127 asection *sym_sec;
7128 struct elf_link_hash_entry *h;
7129 Elf_Internal_Sym *sym;
7130
7131 r_symndx = ELF64_R_SYM (rel->r_info);
7132 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7133 r_symndx, ibfd))
7134 goto error_ret;
7135
7136 if (rel->r_offset == offset)
7137 {
7138 struct ppc_link_hash_entry *fdh = NULL;
7139
7140 /* See if the .opd entry is full 24 byte or
7141 16 byte (with fd_aux entry overlapped with next
7142 fd_func). */
7143 opd_ent_size = 24;
7144 if ((rel + 2 == relend && sec->size == offset + 16)
7145 || (rel + 3 < relend
7146 && rel[2].r_offset == offset + 16
7147 && rel[3].r_offset == offset + 24
7148 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7149 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7150 opd_ent_size = 16;
7151
7152 if (h != NULL
7153 && h->root.root.string[0] == '.')
7154 {
7155 struct ppc_link_hash_table *htab;
7156
7157 htab = ppc_hash_table (info);
7158 if (htab != NULL)
7159 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7160 htab);
7161 if (fdh != NULL
7162 && fdh->elf.root.type != bfd_link_hash_defined
7163 && fdh->elf.root.type != bfd_link_hash_defweak)
7164 fdh = NULL;
7165 }
7166
7167 skip = (sym_sec->owner != ibfd
7168 || sym_sec->output_section == bfd_abs_section_ptr);
7169 if (skip)
7170 {
7171 if (fdh != NULL && sym_sec->owner == ibfd)
7172 {
7173 /* Arrange for the function descriptor sym
7174 to be dropped. */
7175 fdh->elf.root.u.def.value = 0;
7176 fdh->elf.root.u.def.section = sym_sec;
7177 }
7178 opd->adjust[rel->r_offset / 8] = -1;
7179 }
7180 else
7181 {
7182 /* We'll be keeping this opd entry. */
7183
7184 if (fdh != NULL)
7185 {
7186 /* Redefine the function descriptor symbol to
7187 this location in the opd section. It is
7188 necessary to update the value here rather
7189 than using an array of adjustments as we do
7190 for local symbols, because various places
7191 in the generic ELF code use the value
7192 stored in u.def.value. */
7193 fdh->elf.root.u.def.value = wptr - new_contents;
7194 fdh->adjust_done = 1;
7195 }
7196
7197 /* Local syms are a bit tricky. We could
7198 tweak them as they can be cached, but
7199 we'd need to look through the local syms
7200 for the function descriptor sym which we
7201 don't have at the moment. So keep an
7202 array of adjustments. */
7203 opd->adjust[rel->r_offset / 8]
7204 = (wptr - new_contents) - (rptr - sec->contents);
7205
7206 if (wptr != rptr)
7207 memcpy (wptr, rptr, opd_ent_size);
7208 wptr += opd_ent_size;
7209 if (add_aux_fields && opd_ent_size == 16)
7210 {
7211 memset (wptr, '\0', 8);
7212 wptr += 8;
7213 }
7214 }
7215 rptr += opd_ent_size;
7216 offset += opd_ent_size;
7217 }
7218
7219 if (skip)
7220 {
7221 if (!NO_OPD_RELOCS
7222 && !info->relocatable
7223 && !dec_dynrel_count (rel->r_info, sec, info,
7224 NULL, h, sym_sec))
7225 goto error_ret;
7226 }
7227 else
7228 {
7229 /* We need to adjust any reloc offsets to point to the
7230 new opd entries. While we're at it, we may as well
7231 remove redundant relocs. */
7232 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
7233 if (write_rel != rel)
7234 memcpy (write_rel, rel, sizeof (*rel));
7235 ++write_rel;
7236 }
7237 }
7238
7239 sec->size = wptr - new_contents;
7240 sec->reloc_count = write_rel - relstart;
7241 if (add_aux_fields)
7242 {
7243 free (sec->contents);
7244 sec->contents = new_contents;
7245 }
7246
7247 /* Fudge the header size too, as this is used later in
7248 elf_bfd_final_link if we are emitting relocs. */
7249 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7250 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7251 some_edited = TRUE;
7252 }
7253 else if (elf_section_data (sec)->relocs != relstart)
7254 free (relstart);
7255
7256 if (local_syms != NULL
7257 && symtab_hdr->contents != (unsigned char *) local_syms)
7258 {
7259 if (!info->keep_memory)
7260 free (local_syms);
7261 else
7262 symtab_hdr->contents = (unsigned char *) local_syms;
7263 }
7264 }
7265
7266 if (some_edited)
7267 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7268
7269 /* If we are doing a final link and the last .opd entry is just 16 byte
7270 long, add a 8 byte padding after it. */
7271 if (need_pad != NULL && !info->relocatable)
7272 {
7273 bfd_byte *p;
7274
7275 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7276 {
7277 BFD_ASSERT (need_pad->size > 0);
7278
7279 p = bfd_malloc (need_pad->size + 8);
7280 if (p == NULL)
7281 return FALSE;
7282
7283 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7284 p, 0, need_pad->size))
7285 return FALSE;
7286
7287 need_pad->contents = p;
7288 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7289 }
7290 else
7291 {
7292 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7293 if (p == NULL)
7294 return FALSE;
7295
7296 need_pad->contents = p;
7297 }
7298
7299 memset (need_pad->contents + need_pad->size, 0, 8);
7300 need_pad->size += 8;
7301 }
7302
7303 return TRUE;
7304 }
7305
7306 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7307
7308 asection *
7309 ppc64_elf_tls_setup (struct bfd_link_info *info,
7310 int no_tls_get_addr_opt,
7311 int *no_multi_toc)
7312 {
7313 struct ppc_link_hash_table *htab;
7314
7315 htab = ppc_hash_table (info);
7316 if (htab == NULL)
7317 return NULL;
7318
7319 if (*no_multi_toc)
7320 htab->do_multi_toc = 0;
7321 else if (!htab->do_multi_toc)
7322 *no_multi_toc = 1;
7323
7324 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7325 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7326 FALSE, FALSE, TRUE));
7327 /* Move dynamic linking info to the function descriptor sym. */
7328 if (htab->tls_get_addr != NULL)
7329 func_desc_adjust (&htab->tls_get_addr->elf, info);
7330 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7331 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7332 FALSE, FALSE, TRUE));
7333 if (!no_tls_get_addr_opt)
7334 {
7335 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7336
7337 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7338 FALSE, FALSE, TRUE);
7339 if (opt != NULL)
7340 func_desc_adjust (opt, info);
7341 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7342 FALSE, FALSE, TRUE);
7343 if (opt_fd != NULL
7344 && (opt_fd->root.type == bfd_link_hash_defined
7345 || opt_fd->root.type == bfd_link_hash_defweak))
7346 {
7347 /* If glibc supports an optimized __tls_get_addr call stub,
7348 signalled by the presence of __tls_get_addr_opt, and we'll
7349 be calling __tls_get_addr via a plt call stub, then
7350 make __tls_get_addr point to __tls_get_addr_opt. */
7351 tga_fd = &htab->tls_get_addr_fd->elf;
7352 if (htab->elf.dynamic_sections_created
7353 && tga_fd != NULL
7354 && (tga_fd->type == STT_FUNC
7355 || tga_fd->needs_plt)
7356 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7357 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7358 && tga_fd->root.type == bfd_link_hash_undefweak)))
7359 {
7360 struct plt_entry *ent;
7361
7362 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7363 if (ent->plt.refcount > 0)
7364 break;
7365 if (ent != NULL)
7366 {
7367 tga_fd->root.type = bfd_link_hash_indirect;
7368 tga_fd->root.u.i.link = &opt_fd->root;
7369 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7370 if (opt_fd->dynindx != -1)
7371 {
7372 /* Use __tls_get_addr_opt in dynamic relocations. */
7373 opt_fd->dynindx = -1;
7374 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7375 opt_fd->dynstr_index);
7376 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7377 return NULL;
7378 }
7379 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7380 tga = &htab->tls_get_addr->elf;
7381 if (opt != NULL && tga != NULL)
7382 {
7383 tga->root.type = bfd_link_hash_indirect;
7384 tga->root.u.i.link = &opt->root;
7385 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7386 _bfd_elf_link_hash_hide_symbol (info, opt,
7387 tga->forced_local);
7388 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7389 }
7390 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7391 htab->tls_get_addr_fd->is_func_descriptor = 1;
7392 if (htab->tls_get_addr != NULL)
7393 {
7394 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7395 htab->tls_get_addr->is_func = 1;
7396 }
7397 }
7398 }
7399 }
7400 else
7401 no_tls_get_addr_opt = TRUE;
7402 }
7403 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
7404 return _bfd_elf_tls_setup (info->output_bfd, info);
7405 }
7406
7407 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7408 HASH1 or HASH2. */
7409
7410 static bfd_boolean
7411 branch_reloc_hash_match (const bfd *ibfd,
7412 const Elf_Internal_Rela *rel,
7413 const struct ppc_link_hash_entry *hash1,
7414 const struct ppc_link_hash_entry *hash2)
7415 {
7416 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7417 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7418 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7419
7420 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7421 {
7422 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7423 struct elf_link_hash_entry *h;
7424
7425 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7426 h = elf_follow_link (h);
7427 if (h == &hash1->elf || h == &hash2->elf)
7428 return TRUE;
7429 }
7430 return FALSE;
7431 }
7432
7433 /* Run through all the TLS relocs looking for optimization
7434 opportunities. The linker has been hacked (see ppc64elf.em) to do
7435 a preliminary section layout so that we know the TLS segment
7436 offsets. We can't optimize earlier because some optimizations need
7437 to know the tp offset, and we need to optimize before allocating
7438 dynamic relocations. */
7439
7440 bfd_boolean
7441 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7442 {
7443 bfd *ibfd;
7444 asection *sec;
7445 struct ppc_link_hash_table *htab;
7446 unsigned char *toc_ref;
7447 int pass;
7448
7449 if (info->relocatable || !info->executable)
7450 return TRUE;
7451
7452 htab = ppc_hash_table (info);
7453 if (htab == NULL)
7454 return FALSE;
7455
7456 /* Make two passes over the relocs. On the first pass, mark toc
7457 entries involved with tls relocs, and check that tls relocs
7458 involved in setting up a tls_get_addr call are indeed followed by
7459 such a call. If they are not, we can't do any tls optimization.
7460 On the second pass twiddle tls_mask flags to notify
7461 relocate_section that optimization can be done, and adjust got
7462 and plt refcounts. */
7463 toc_ref = NULL;
7464 for (pass = 0; pass < 2; ++pass)
7465 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7466 {
7467 Elf_Internal_Sym *locsyms = NULL;
7468 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7469
7470 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7471 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7472 {
7473 Elf_Internal_Rela *relstart, *rel, *relend;
7474 bfd_boolean found_tls_get_addr_arg = 0;
7475
7476 /* Read the relocations. */
7477 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7478 info->keep_memory);
7479 if (relstart == NULL)
7480 return FALSE;
7481
7482 relend = relstart + sec->reloc_count;
7483 for (rel = relstart; rel < relend; rel++)
7484 {
7485 enum elf_ppc64_reloc_type r_type;
7486 unsigned long r_symndx;
7487 struct elf_link_hash_entry *h;
7488 Elf_Internal_Sym *sym;
7489 asection *sym_sec;
7490 unsigned char *tls_mask;
7491 unsigned char tls_set, tls_clear, tls_type = 0;
7492 bfd_vma value;
7493 bfd_boolean ok_tprel, is_local;
7494 long toc_ref_index = 0;
7495 int expecting_tls_get_addr = 0;
7496 bfd_boolean ret = FALSE;
7497
7498 r_symndx = ELF64_R_SYM (rel->r_info);
7499 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7500 r_symndx, ibfd))
7501 {
7502 err_free_rel:
7503 if (elf_section_data (sec)->relocs != relstart)
7504 free (relstart);
7505 if (toc_ref != NULL)
7506 free (toc_ref);
7507 if (locsyms != NULL
7508 && (elf_symtab_hdr (ibfd).contents
7509 != (unsigned char *) locsyms))
7510 free (locsyms);
7511 return ret;
7512 }
7513
7514 if (h != NULL)
7515 {
7516 if (h->root.type == bfd_link_hash_defined
7517 || h->root.type == bfd_link_hash_defweak)
7518 value = h->root.u.def.value;
7519 else if (h->root.type == bfd_link_hash_undefweak)
7520 value = 0;
7521 else
7522 {
7523 found_tls_get_addr_arg = 0;
7524 continue;
7525 }
7526 }
7527 else
7528 /* Symbols referenced by TLS relocs must be of type
7529 STT_TLS. So no need for .opd local sym adjust. */
7530 value = sym->st_value;
7531
7532 ok_tprel = FALSE;
7533 is_local = FALSE;
7534 if (h == NULL
7535 || !h->def_dynamic)
7536 {
7537 is_local = TRUE;
7538 if (h != NULL
7539 && h->root.type == bfd_link_hash_undefweak)
7540 ok_tprel = TRUE;
7541 else
7542 {
7543 value += sym_sec->output_offset;
7544 value += sym_sec->output_section->vma;
7545 value -= htab->elf.tls_sec->vma;
7546 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7547 < (bfd_vma) 1 << 32);
7548 }
7549 }
7550
7551 r_type = ELF64_R_TYPE (rel->r_info);
7552 /* If this section has old-style __tls_get_addr calls
7553 without marker relocs, then check that each
7554 __tls_get_addr call reloc is preceded by a reloc
7555 that conceivably belongs to the __tls_get_addr arg
7556 setup insn. If we don't find matching arg setup
7557 relocs, don't do any tls optimization. */
7558 if (pass == 0
7559 && sec->has_tls_get_addr_call
7560 && h != NULL
7561 && (h == &htab->tls_get_addr->elf
7562 || h == &htab->tls_get_addr_fd->elf)
7563 && !found_tls_get_addr_arg
7564 && is_branch_reloc (r_type))
7565 {
7566 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7567 "TLS optimization disabled\n"),
7568 ibfd, sec, rel->r_offset);
7569 ret = TRUE;
7570 goto err_free_rel;
7571 }
7572
7573 found_tls_get_addr_arg = 0;
7574 switch (r_type)
7575 {
7576 case R_PPC64_GOT_TLSLD16:
7577 case R_PPC64_GOT_TLSLD16_LO:
7578 expecting_tls_get_addr = 1;
7579 found_tls_get_addr_arg = 1;
7580 /* Fall thru */
7581
7582 case R_PPC64_GOT_TLSLD16_HI:
7583 case R_PPC64_GOT_TLSLD16_HA:
7584 /* These relocs should never be against a symbol
7585 defined in a shared lib. Leave them alone if
7586 that turns out to be the case. */
7587 if (!is_local)
7588 continue;
7589
7590 /* LD -> LE */
7591 tls_set = 0;
7592 tls_clear = TLS_LD;
7593 tls_type = TLS_TLS | TLS_LD;
7594 break;
7595
7596 case R_PPC64_GOT_TLSGD16:
7597 case R_PPC64_GOT_TLSGD16_LO:
7598 expecting_tls_get_addr = 1;
7599 found_tls_get_addr_arg = 1;
7600 /* Fall thru */
7601
7602 case R_PPC64_GOT_TLSGD16_HI:
7603 case R_PPC64_GOT_TLSGD16_HA:
7604 if (ok_tprel)
7605 /* GD -> LE */
7606 tls_set = 0;
7607 else
7608 /* GD -> IE */
7609 tls_set = TLS_TLS | TLS_TPRELGD;
7610 tls_clear = TLS_GD;
7611 tls_type = TLS_TLS | TLS_GD;
7612 break;
7613
7614 case R_PPC64_GOT_TPREL16_DS:
7615 case R_PPC64_GOT_TPREL16_LO_DS:
7616 case R_PPC64_GOT_TPREL16_HI:
7617 case R_PPC64_GOT_TPREL16_HA:
7618 if (ok_tprel)
7619 {
7620 /* IE -> LE */
7621 tls_set = 0;
7622 tls_clear = TLS_TPREL;
7623 tls_type = TLS_TLS | TLS_TPREL;
7624 break;
7625 }
7626 continue;
7627
7628 case R_PPC64_TLSGD:
7629 case R_PPC64_TLSLD:
7630 found_tls_get_addr_arg = 1;
7631 /* Fall thru */
7632
7633 case R_PPC64_TLS:
7634 case R_PPC64_TOC16:
7635 case R_PPC64_TOC16_LO:
7636 if (sym_sec == NULL || sym_sec != toc)
7637 continue;
7638
7639 /* Mark this toc entry as referenced by a TLS
7640 code sequence. We can do that now in the
7641 case of R_PPC64_TLS, and after checking for
7642 tls_get_addr for the TOC16 relocs. */
7643 if (toc_ref == NULL)
7644 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
7645 if (toc_ref == NULL)
7646 goto err_free_rel;
7647
7648 if (h != NULL)
7649 value = h->root.u.def.value;
7650 else
7651 value = sym->st_value;
7652 value += rel->r_addend;
7653 BFD_ASSERT (value < toc->size && value % 8 == 0);
7654 toc_ref_index = (value + toc->output_offset) / 8;
7655 if (r_type == R_PPC64_TLS
7656 || r_type == R_PPC64_TLSGD
7657 || r_type == R_PPC64_TLSLD)
7658 {
7659 toc_ref[toc_ref_index] = 1;
7660 continue;
7661 }
7662
7663 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7664 continue;
7665
7666 tls_set = 0;
7667 tls_clear = 0;
7668 expecting_tls_get_addr = 2;
7669 break;
7670
7671 case R_PPC64_TPREL64:
7672 if (pass == 0
7673 || sec != toc
7674 || toc_ref == NULL
7675 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7676 continue;
7677 if (ok_tprel)
7678 {
7679 /* IE -> LE */
7680 tls_set = TLS_EXPLICIT;
7681 tls_clear = TLS_TPREL;
7682 break;
7683 }
7684 continue;
7685
7686 case R_PPC64_DTPMOD64:
7687 if (pass == 0
7688 || sec != toc
7689 || toc_ref == NULL
7690 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7691 continue;
7692 if (rel + 1 < relend
7693 && (rel[1].r_info
7694 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7695 && rel[1].r_offset == rel->r_offset + 8)
7696 {
7697 if (ok_tprel)
7698 /* GD -> LE */
7699 tls_set = TLS_EXPLICIT | TLS_GD;
7700 else
7701 /* GD -> IE */
7702 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7703 tls_clear = TLS_GD;
7704 }
7705 else
7706 {
7707 if (!is_local)
7708 continue;
7709
7710 /* LD -> LE */
7711 tls_set = TLS_EXPLICIT;
7712 tls_clear = TLS_LD;
7713 }
7714 break;
7715
7716 default:
7717 continue;
7718 }
7719
7720 if (pass == 0)
7721 {
7722 if (!expecting_tls_get_addr
7723 || !sec->has_tls_get_addr_call)
7724 continue;
7725
7726 if (rel + 1 < relend
7727 && branch_reloc_hash_match (ibfd, rel + 1,
7728 htab->tls_get_addr,
7729 htab->tls_get_addr_fd))
7730 {
7731 if (expecting_tls_get_addr == 2)
7732 {
7733 /* Check for toc tls entries. */
7734 unsigned char *toc_tls;
7735 int retval;
7736
7737 retval = get_tls_mask (&toc_tls, NULL, NULL,
7738 &locsyms,
7739 rel, ibfd);
7740 if (retval == 0)
7741 goto err_free_rel;
7742 if (toc_tls != NULL)
7743 {
7744 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
7745 found_tls_get_addr_arg = 1;
7746 if (retval > 1)
7747 toc_ref[toc_ref_index] = 1;
7748 }
7749 }
7750 continue;
7751 }
7752
7753 if (expecting_tls_get_addr != 1)
7754 continue;
7755
7756 /* Uh oh, we didn't find the expected call. We
7757 could just mark this symbol to exclude it
7758 from tls optimization but it's safer to skip
7759 the entire optimization. */
7760 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
7761 "TLS optimization disabled\n"),
7762 ibfd, sec, rel->r_offset);
7763 ret = TRUE;
7764 goto err_free_rel;
7765 }
7766
7767 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
7768 {
7769 struct plt_entry *ent;
7770 for (ent = htab->tls_get_addr->elf.plt.plist;
7771 ent != NULL;
7772 ent = ent->next)
7773 if (ent->addend == 0)
7774 {
7775 if (ent->plt.refcount > 0)
7776 {
7777 ent->plt.refcount -= 1;
7778 expecting_tls_get_addr = 0;
7779 }
7780 break;
7781 }
7782 }
7783
7784 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
7785 {
7786 struct plt_entry *ent;
7787 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7788 ent != NULL;
7789 ent = ent->next)
7790 if (ent->addend == 0)
7791 {
7792 if (ent->plt.refcount > 0)
7793 ent->plt.refcount -= 1;
7794 break;
7795 }
7796 }
7797
7798 if (tls_clear == 0)
7799 continue;
7800
7801 if ((tls_set & TLS_EXPLICIT) == 0)
7802 {
7803 struct got_entry *ent;
7804
7805 /* Adjust got entry for this reloc. */
7806 if (h != NULL)
7807 ent = h->got.glist;
7808 else
7809 ent = elf_local_got_ents (ibfd)[r_symndx];
7810
7811 for (; ent != NULL; ent = ent->next)
7812 if (ent->addend == rel->r_addend
7813 && ent->owner == ibfd
7814 && ent->tls_type == tls_type)
7815 break;
7816 if (ent == NULL)
7817 abort ();
7818
7819 if (tls_set == 0)
7820 {
7821 /* We managed to get rid of a got entry. */
7822 if (ent->got.refcount > 0)
7823 ent->got.refcount -= 1;
7824 }
7825 }
7826 else
7827 {
7828 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7829 we'll lose one or two dyn relocs. */
7830 if (!dec_dynrel_count (rel->r_info, sec, info,
7831 NULL, h, sym_sec))
7832 return FALSE;
7833
7834 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7835 {
7836 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7837 NULL, h, sym_sec))
7838 return FALSE;
7839 }
7840 }
7841
7842 *tls_mask |= tls_set;
7843 *tls_mask &= ~tls_clear;
7844 }
7845
7846 if (elf_section_data (sec)->relocs != relstart)
7847 free (relstart);
7848 }
7849
7850 if (locsyms != NULL
7851 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7852 {
7853 if (!info->keep_memory)
7854 free (locsyms);
7855 else
7856 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7857 }
7858 }
7859
7860 if (toc_ref != NULL)
7861 free (toc_ref);
7862 return TRUE;
7863 }
7864
7865 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7866 the values of any global symbols in a toc section that has been
7867 edited. Globals in toc sections should be a rarity, so this function
7868 sets a flag if any are found in toc sections other than the one just
7869 edited, so that futher hash table traversals can be avoided. */
7870
7871 struct adjust_toc_info
7872 {
7873 asection *toc;
7874 unsigned long *skip;
7875 bfd_boolean global_toc_syms;
7876 };
7877
7878 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7879
7880 static bfd_boolean
7881 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7882 {
7883 struct ppc_link_hash_entry *eh;
7884 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7885 unsigned long i;
7886
7887 if (h->root.type != bfd_link_hash_defined
7888 && h->root.type != bfd_link_hash_defweak)
7889 return TRUE;
7890
7891 eh = (struct ppc_link_hash_entry *) h;
7892 if (eh->adjust_done)
7893 return TRUE;
7894
7895 if (eh->elf.root.u.def.section == toc_inf->toc)
7896 {
7897 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7898 i = toc_inf->toc->rawsize >> 3;
7899 else
7900 i = eh->elf.root.u.def.value >> 3;
7901
7902 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
7903 {
7904 (*_bfd_error_handler)
7905 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7906 do
7907 ++i;
7908 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
7909 eh->elf.root.u.def.value = (bfd_vma) i << 3;
7910 }
7911
7912 eh->elf.root.u.def.value -= toc_inf->skip[i];
7913 eh->adjust_done = 1;
7914 }
7915 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7916 toc_inf->global_toc_syms = TRUE;
7917
7918 return TRUE;
7919 }
7920
7921 /* Examine all relocs referencing .toc sections in order to remove
7922 unused .toc entries. */
7923
7924 bfd_boolean
7925 ppc64_elf_edit_toc (struct bfd_link_info *info)
7926 {
7927 bfd *ibfd;
7928 struct adjust_toc_info toc_inf;
7929 struct ppc_link_hash_table *htab = ppc_hash_table (info);
7930
7931 htab->do_toc_opt = 1;
7932 toc_inf.global_toc_syms = TRUE;
7933 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7934 {
7935 asection *toc, *sec;
7936 Elf_Internal_Shdr *symtab_hdr;
7937 Elf_Internal_Sym *local_syms;
7938 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
7939 unsigned long *skip, *drop;
7940 unsigned char *used;
7941 unsigned char *keep, last, some_unused;
7942
7943 if (!is_ppc64_elf (ibfd))
7944 continue;
7945
7946 toc = bfd_get_section_by_name (ibfd, ".toc");
7947 if (toc == NULL
7948 || toc->size == 0
7949 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7950 || elf_discarded_section (toc))
7951 continue;
7952
7953 toc_relocs = NULL;
7954 local_syms = NULL;
7955 symtab_hdr = &elf_symtab_hdr (ibfd);
7956
7957 /* Look at sections dropped from the final link. */
7958 skip = NULL;
7959 relstart = NULL;
7960 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7961 {
7962 if (sec->reloc_count == 0
7963 || !elf_discarded_section (sec)
7964 || get_opd_info (sec)
7965 || (sec->flags & SEC_ALLOC) == 0
7966 || (sec->flags & SEC_DEBUGGING) != 0)
7967 continue;
7968
7969 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7970 if (relstart == NULL)
7971 goto error_ret;
7972
7973 /* Run through the relocs to see which toc entries might be
7974 unused. */
7975 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7976 {
7977 enum elf_ppc64_reloc_type r_type;
7978 unsigned long r_symndx;
7979 asection *sym_sec;
7980 struct elf_link_hash_entry *h;
7981 Elf_Internal_Sym *sym;
7982 bfd_vma val;
7983
7984 r_type = ELF64_R_TYPE (rel->r_info);
7985 switch (r_type)
7986 {
7987 default:
7988 continue;
7989
7990 case R_PPC64_TOC16:
7991 case R_PPC64_TOC16_LO:
7992 case R_PPC64_TOC16_HI:
7993 case R_PPC64_TOC16_HA:
7994 case R_PPC64_TOC16_DS:
7995 case R_PPC64_TOC16_LO_DS:
7996 break;
7997 }
7998
7999 r_symndx = ELF64_R_SYM (rel->r_info);
8000 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8001 r_symndx, ibfd))
8002 goto error_ret;
8003
8004 if (sym_sec != toc)
8005 continue;
8006
8007 if (h != NULL)
8008 val = h->root.u.def.value;
8009 else
8010 val = sym->st_value;
8011 val += rel->r_addend;
8012
8013 if (val >= toc->size)
8014 continue;
8015
8016 /* Anything in the toc ought to be aligned to 8 bytes.
8017 If not, don't mark as unused. */
8018 if (val & 7)
8019 continue;
8020
8021 if (skip == NULL)
8022 {
8023 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8024 if (skip == NULL)
8025 goto error_ret;
8026 }
8027
8028 skip[val >> 3] = ref_from_discarded;
8029 }
8030
8031 if (elf_section_data (sec)->relocs != relstart)
8032 free (relstart);
8033 }
8034
8035 /* For largetoc loads of address constants, we can convert
8036 . addis rx,2,addr@got@ha
8037 . ld ry,addr@got@l(rx)
8038 to
8039 . addis rx,2,addr@toc@ha
8040 . addi ry,rx,addr@toc@l
8041 when addr is within 2G of the toc pointer. This then means
8042 that the word storing "addr" in the toc is no longer needed. */
8043
8044 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8045 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8046 && toc->reloc_count != 0)
8047 {
8048 /* Read toc relocs. */
8049 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8050 info->keep_memory);
8051 if (toc_relocs == NULL)
8052 goto error_ret;
8053
8054 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8055 {
8056 enum elf_ppc64_reloc_type r_type;
8057 unsigned long r_symndx;
8058 asection *sym_sec;
8059 struct elf_link_hash_entry *h;
8060 Elf_Internal_Sym *sym;
8061 bfd_vma val, addr;
8062
8063 r_type = ELF64_R_TYPE (rel->r_info);
8064 if (r_type != R_PPC64_ADDR64)
8065 continue;
8066
8067 r_symndx = ELF64_R_SYM (rel->r_info);
8068 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8069 r_symndx, ibfd))
8070 goto error_ret;
8071
8072 if (sym_sec == NULL
8073 || elf_discarded_section (sym_sec))
8074 continue;
8075
8076 if (!SYMBOL_CALLS_LOCAL (info, h))
8077 continue;
8078
8079 if (h != NULL)
8080 {
8081 if (h->type == STT_GNU_IFUNC)
8082 continue;
8083 val = h->root.u.def.value;
8084 }
8085 else
8086 {
8087 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8088 continue;
8089 val = sym->st_value;
8090 }
8091 val += rel->r_addend;
8092 val += sym_sec->output_section->vma + sym_sec->output_offset;
8093
8094 /* We don't yet know the exact toc pointer value, but we
8095 know it will be somewhere in the toc section. Don't
8096 optimize if the difference from any possible toc
8097 pointer is outside [ff..f80008000, 7fff7fff]. */
8098 addr = toc->output_section->vma + TOC_BASE_OFF;
8099 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8100 continue;
8101
8102 addr = toc->output_section->vma + toc->output_section->rawsize;
8103 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8104 continue;
8105
8106 if (skip == NULL)
8107 {
8108 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8109 if (skip == NULL)
8110 goto error_ret;
8111 }
8112
8113 skip[rel->r_offset >> 3]
8114 |= can_optimize | ((rel - toc_relocs) << 2);
8115 }
8116 }
8117
8118 if (skip == NULL)
8119 continue;
8120
8121 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8122 if (used == NULL)
8123 {
8124 error_ret:
8125 if (local_syms != NULL
8126 && symtab_hdr->contents != (unsigned char *) local_syms)
8127 free (local_syms);
8128 if (sec != NULL
8129 && relstart != NULL
8130 && elf_section_data (sec)->relocs != relstart)
8131 free (relstart);
8132 if (toc_relocs != NULL
8133 && elf_section_data (toc)->relocs != toc_relocs)
8134 free (toc_relocs);
8135 if (skip != NULL)
8136 free (skip);
8137 return FALSE;
8138 }
8139
8140 /* Now check all kept sections that might reference the toc.
8141 Check the toc itself last. */
8142 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8143 : ibfd->sections);
8144 sec != NULL;
8145 sec = (sec == toc ? NULL
8146 : sec->next == NULL ? toc
8147 : sec->next == toc && toc->next ? toc->next
8148 : sec->next))
8149 {
8150 int repeat;
8151
8152 if (sec->reloc_count == 0
8153 || elf_discarded_section (sec)
8154 || get_opd_info (sec)
8155 || (sec->flags & SEC_ALLOC) == 0
8156 || (sec->flags & SEC_DEBUGGING) != 0)
8157 continue;
8158
8159 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8160 info->keep_memory);
8161 if (relstart == NULL)
8162 goto error_ret;
8163
8164 /* Mark toc entries referenced as used. */
8165 repeat = 0;
8166 do
8167 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8168 {
8169 enum elf_ppc64_reloc_type r_type;
8170 unsigned long r_symndx;
8171 asection *sym_sec;
8172 struct elf_link_hash_entry *h;
8173 Elf_Internal_Sym *sym;
8174 bfd_vma val;
8175
8176 r_type = ELF64_R_TYPE (rel->r_info);
8177 switch (r_type)
8178 {
8179 default:
8180 break;
8181
8182 case R_PPC64_GOT_TLSLD16_HA:
8183 case R_PPC64_GOT_TLSGD16_HA:
8184 case R_PPC64_GOT_TPREL16_HA:
8185 case R_PPC64_GOT_DTPREL16_HA:
8186 case R_PPC64_GOT16_HA:
8187 case R_PPC64_TOC16_HA:
8188 {
8189 bfd_vma off = rel->r_offset & ~3;
8190 unsigned char buf[4];
8191 unsigned int insn;
8192
8193 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8194 {
8195 free (used);
8196 goto error_ret;
8197 }
8198 insn = bfd_get_32 (ibfd, buf);
8199 if ((insn & ((0x3f << 26) | 0x1f << 16))
8200 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
8201 ppc64_elf_tdata (ibfd)->ha_relocs_not_using_r2 = 1;
8202 }
8203 break;
8204 }
8205
8206 switch (r_type)
8207 {
8208 case R_PPC64_TOC16:
8209 case R_PPC64_TOC16_LO:
8210 case R_PPC64_TOC16_HI:
8211 case R_PPC64_TOC16_HA:
8212 case R_PPC64_TOC16_DS:
8213 case R_PPC64_TOC16_LO_DS:
8214 /* In case we're taking addresses of toc entries. */
8215 case R_PPC64_ADDR64:
8216 break;
8217
8218 default:
8219 continue;
8220 }
8221
8222 r_symndx = ELF64_R_SYM (rel->r_info);
8223 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8224 r_symndx, ibfd))
8225 {
8226 free (used);
8227 goto error_ret;
8228 }
8229
8230 if (sym_sec != toc)
8231 continue;
8232
8233 if (h != NULL)
8234 val = h->root.u.def.value;
8235 else
8236 val = sym->st_value;
8237 val += rel->r_addend;
8238
8239 if (val >= toc->size)
8240 continue;
8241
8242 if ((skip[val >> 3] & can_optimize) != 0)
8243 {
8244 bfd_vma off;
8245 unsigned char opc;
8246
8247 switch (r_type)
8248 {
8249 case R_PPC64_TOC16_HA:
8250 break;
8251
8252 case R_PPC64_TOC16_LO_DS:
8253 off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8254 if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
8255 {
8256 free (used);
8257 goto error_ret;
8258 }
8259 if ((opc & (0x3f << 2)) == (58u << 2))
8260 break;
8261 /* Fall thru */
8262
8263 default:
8264 /* Wrong sort of reloc, or not a ld. We may
8265 as well clear ref_from_discarded too. */
8266 skip[val >> 3] = 0;
8267 }
8268 }
8269
8270 /* For the toc section, we only mark as used if
8271 this entry itself isn't unused. */
8272 if (sec == toc
8273 && !used[val >> 3]
8274 && (used[rel->r_offset >> 3]
8275 || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
8276 /* Do all the relocs again, to catch reference
8277 chains. */
8278 repeat = 1;
8279
8280 used[val >> 3] = 1;
8281 }
8282 while (repeat);
8283
8284 if (elf_section_data (sec)->relocs != relstart)
8285 free (relstart);
8286 }
8287
8288 /* Merge the used and skip arrays. Assume that TOC
8289 doublewords not appearing as either used or unused belong
8290 to to an entry more than one doubleword in size. */
8291 for (drop = skip, keep = used, last = 0, some_unused = 0;
8292 drop < skip + (toc->size + 7) / 8;
8293 ++drop, ++keep)
8294 {
8295 if (*keep)
8296 {
8297 *drop &= ~ref_from_discarded;
8298 if ((*drop & can_optimize) != 0)
8299 some_unused = 1;
8300 last = 0;
8301 }
8302 else if ((*drop & ref_from_discarded) != 0)
8303 {
8304 some_unused = 1;
8305 last = ref_from_discarded;
8306 }
8307 else
8308 *drop = last;
8309 }
8310
8311 free (used);
8312
8313 if (some_unused)
8314 {
8315 bfd_byte *contents, *src;
8316 unsigned long off;
8317 Elf_Internal_Sym *sym;
8318 bfd_boolean local_toc_syms = FALSE;
8319
8320 /* Shuffle the toc contents, and at the same time convert the
8321 skip array from booleans into offsets. */
8322 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8323 goto error_ret;
8324
8325 elf_section_data (toc)->this_hdr.contents = contents;
8326
8327 for (src = contents, off = 0, drop = skip;
8328 src < contents + toc->size;
8329 src += 8, ++drop)
8330 {
8331 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8332 off += 8;
8333 else if (off != 0)
8334 {
8335 *drop = off;
8336 memcpy (src - off, src, 8);
8337 }
8338 }
8339 *drop = off;
8340 toc->rawsize = toc->size;
8341 toc->size = src - contents - off;
8342
8343 /* Adjust addends for relocs against the toc section sym,
8344 and optimize any accesses we can. */
8345 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8346 {
8347 if (sec->reloc_count == 0
8348 || elf_discarded_section (sec))
8349 continue;
8350
8351 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8352 info->keep_memory);
8353 if (relstart == NULL)
8354 goto error_ret;
8355
8356 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8357 {
8358 enum elf_ppc64_reloc_type r_type;
8359 unsigned long r_symndx;
8360 asection *sym_sec;
8361 struct elf_link_hash_entry *h;
8362 bfd_vma val;
8363
8364 r_type = ELF64_R_TYPE (rel->r_info);
8365 switch (r_type)
8366 {
8367 default:
8368 continue;
8369
8370 case R_PPC64_TOC16:
8371 case R_PPC64_TOC16_LO:
8372 case R_PPC64_TOC16_HI:
8373 case R_PPC64_TOC16_HA:
8374 case R_PPC64_TOC16_DS:
8375 case R_PPC64_TOC16_LO_DS:
8376 case R_PPC64_ADDR64:
8377 break;
8378 }
8379
8380 r_symndx = ELF64_R_SYM (rel->r_info);
8381 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8382 r_symndx, ibfd))
8383 goto error_ret;
8384
8385 if (sym_sec != toc)
8386 continue;
8387
8388 if (h != NULL)
8389 val = h->root.u.def.value;
8390 else
8391 {
8392 val = sym->st_value;
8393 if (val != 0)
8394 local_toc_syms = TRUE;
8395 }
8396
8397 val += rel->r_addend;
8398
8399 if (val > toc->rawsize)
8400 val = toc->rawsize;
8401 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8402 continue;
8403 else if ((skip[val >> 3] & can_optimize) != 0)
8404 {
8405 Elf_Internal_Rela *tocrel
8406 = toc_relocs + (skip[val >> 3] >> 2);
8407 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8408
8409 switch (r_type)
8410 {
8411 case R_PPC64_TOC16_HA:
8412 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8413 break;
8414
8415 case R_PPC64_TOC16_LO_DS:
8416 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8417 break;
8418
8419 default:
8420 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8421 ppc_howto_init ();
8422 info->callbacks->einfo
8423 (_("%P: %H: %s relocation references "
8424 "optimized away TOC entry\n"),
8425 ibfd, sec, rel->r_offset,
8426 ppc64_elf_howto_table[r_type]->name);
8427 bfd_set_error (bfd_error_bad_value);
8428 goto error_ret;
8429 }
8430 rel->r_addend = tocrel->r_addend;
8431 elf_section_data (sec)->relocs = relstart;
8432 continue;
8433 }
8434
8435 if (h != NULL || sym->st_value != 0)
8436 continue;
8437
8438 rel->r_addend -= skip[val >> 3];
8439 elf_section_data (sec)->relocs = relstart;
8440 }
8441
8442 if (elf_section_data (sec)->relocs != relstart)
8443 free (relstart);
8444 }
8445
8446 /* We shouldn't have local or global symbols defined in the TOC,
8447 but handle them anyway. */
8448 if (local_syms != NULL)
8449 for (sym = local_syms;
8450 sym < local_syms + symtab_hdr->sh_info;
8451 ++sym)
8452 if (sym->st_value != 0
8453 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8454 {
8455 unsigned long i;
8456
8457 if (sym->st_value > toc->rawsize)
8458 i = toc->rawsize >> 3;
8459 else
8460 i = sym->st_value >> 3;
8461
8462 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8463 {
8464 if (local_toc_syms)
8465 (*_bfd_error_handler)
8466 (_("%s defined on removed toc entry"),
8467 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8468 do
8469 ++i;
8470 while ((skip[i] & (ref_from_discarded | can_optimize)));
8471 sym->st_value = (bfd_vma) i << 3;
8472 }
8473
8474 sym->st_value -= skip[i];
8475 symtab_hdr->contents = (unsigned char *) local_syms;
8476 }
8477
8478 /* Adjust any global syms defined in this toc input section. */
8479 if (toc_inf.global_toc_syms)
8480 {
8481 toc_inf.toc = toc;
8482 toc_inf.skip = skip;
8483 toc_inf.global_toc_syms = FALSE;
8484 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8485 &toc_inf);
8486 }
8487
8488 if (toc->reloc_count != 0)
8489 {
8490 Elf_Internal_Shdr *rel_hdr;
8491 Elf_Internal_Rela *wrel;
8492 bfd_size_type sz;
8493
8494 /* Remove unused toc relocs, and adjust those we keep. */
8495 if (toc_relocs == NULL)
8496 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8497 info->keep_memory);
8498 if (toc_relocs == NULL)
8499 goto error_ret;
8500
8501 wrel = toc_relocs;
8502 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8503 if ((skip[rel->r_offset >> 3]
8504 & (ref_from_discarded | can_optimize)) == 0)
8505 {
8506 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8507 wrel->r_info = rel->r_info;
8508 wrel->r_addend = rel->r_addend;
8509 ++wrel;
8510 }
8511 else if (!dec_dynrel_count (rel->r_info, toc, info,
8512 &local_syms, NULL, NULL))
8513 goto error_ret;
8514
8515 elf_section_data (toc)->relocs = toc_relocs;
8516 toc->reloc_count = wrel - toc_relocs;
8517 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8518 sz = rel_hdr->sh_entsize;
8519 rel_hdr->sh_size = toc->reloc_count * sz;
8520 }
8521 }
8522 else if (toc_relocs != NULL
8523 && elf_section_data (toc)->relocs != toc_relocs)
8524 free (toc_relocs);
8525
8526 if (local_syms != NULL
8527 && symtab_hdr->contents != (unsigned char *) local_syms)
8528 {
8529 if (!info->keep_memory)
8530 free (local_syms);
8531 else
8532 symtab_hdr->contents = (unsigned char *) local_syms;
8533 }
8534 free (skip);
8535 }
8536
8537 return TRUE;
8538 }
8539
8540 /* Return true iff input section I references the TOC using
8541 instructions limited to +/-32k offsets. */
8542
8543 bfd_boolean
8544 ppc64_elf_has_small_toc_reloc (asection *i)
8545 {
8546 return (is_ppc64_elf (i->owner)
8547 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8548 }
8549
8550 /* Allocate space for one GOT entry. */
8551
8552 static void
8553 allocate_got (struct elf_link_hash_entry *h,
8554 struct bfd_link_info *info,
8555 struct got_entry *gent)
8556 {
8557 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8558 bfd_boolean dyn;
8559 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8560 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8561 ? 16 : 8);
8562 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8563 ? 2 : 1) * sizeof (Elf64_External_Rela);
8564 asection *got = ppc64_elf_tdata (gent->owner)->got;
8565
8566 gent->got.offset = got->size;
8567 got->size += entsize;
8568
8569 dyn = htab->elf.dynamic_sections_created;
8570 if ((info->shared
8571 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8572 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8573 || h->root.type != bfd_link_hash_undefweak))
8574 {
8575 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8576 relgot->size += rentsize;
8577 }
8578 else if (h->type == STT_GNU_IFUNC)
8579 {
8580 asection *relgot = htab->reliplt;
8581 relgot->size += rentsize;
8582 htab->got_reli_size += rentsize;
8583 }
8584 }
8585
8586 /* This function merges got entries in the same toc group. */
8587
8588 static void
8589 merge_got_entries (struct got_entry **pent)
8590 {
8591 struct got_entry *ent, *ent2;
8592
8593 for (ent = *pent; ent != NULL; ent = ent->next)
8594 if (!ent->is_indirect)
8595 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8596 if (!ent2->is_indirect
8597 && ent2->addend == ent->addend
8598 && ent2->tls_type == ent->tls_type
8599 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8600 {
8601 ent2->is_indirect = TRUE;
8602 ent2->got.ent = ent;
8603 }
8604 }
8605
8606 /* Allocate space in .plt, .got and associated reloc sections for
8607 dynamic relocs. */
8608
8609 static bfd_boolean
8610 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8611 {
8612 struct bfd_link_info *info;
8613 struct ppc_link_hash_table *htab;
8614 asection *s;
8615 struct ppc_link_hash_entry *eh;
8616 struct elf_dyn_relocs *p;
8617 struct got_entry **pgent, *gent;
8618
8619 if (h->root.type == bfd_link_hash_indirect)
8620 return TRUE;
8621
8622 info = (struct bfd_link_info *) inf;
8623 htab = ppc_hash_table (info);
8624 if (htab == NULL)
8625 return FALSE;
8626
8627 if ((htab->elf.dynamic_sections_created
8628 && h->dynindx != -1
8629 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8630 || h->type == STT_GNU_IFUNC)
8631 {
8632 struct plt_entry *pent;
8633 bfd_boolean doneone = FALSE;
8634 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8635 if (pent->plt.refcount > 0)
8636 {
8637 if (!htab->elf.dynamic_sections_created
8638 || h->dynindx == -1)
8639 {
8640 s = htab->iplt;
8641 pent->plt.offset = s->size;
8642 s->size += PLT_ENTRY_SIZE;
8643 s = htab->reliplt;
8644 }
8645 else
8646 {
8647 /* If this is the first .plt entry, make room for the special
8648 first entry. */
8649 s = htab->plt;
8650 if (s->size == 0)
8651 s->size += PLT_INITIAL_ENTRY_SIZE;
8652
8653 pent->plt.offset = s->size;
8654
8655 /* Make room for this entry. */
8656 s->size += PLT_ENTRY_SIZE;
8657
8658 /* Make room for the .glink code. */
8659 s = htab->glink;
8660 if (s->size == 0)
8661 s->size += GLINK_CALL_STUB_SIZE;
8662 /* We need bigger stubs past index 32767. */
8663 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8664 s->size += 4;
8665 s->size += 2*4;
8666
8667 /* We also need to make an entry in the .rela.plt section. */
8668 s = htab->relplt;
8669 }
8670 s->size += sizeof (Elf64_External_Rela);
8671 doneone = TRUE;
8672 }
8673 else
8674 pent->plt.offset = (bfd_vma) -1;
8675 if (!doneone)
8676 {
8677 h->plt.plist = NULL;
8678 h->needs_plt = 0;
8679 }
8680 }
8681 else
8682 {
8683 h->plt.plist = NULL;
8684 h->needs_plt = 0;
8685 }
8686
8687 eh = (struct ppc_link_hash_entry *) h;
8688 /* Run through the TLS GD got entries first if we're changing them
8689 to TPREL. */
8690 if ((eh->tls_mask & TLS_TPRELGD) != 0)
8691 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8692 if (gent->got.refcount > 0
8693 && (gent->tls_type & TLS_GD) != 0)
8694 {
8695 /* This was a GD entry that has been converted to TPREL. If
8696 there happens to be a TPREL entry we can use that one. */
8697 struct got_entry *ent;
8698 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8699 if (ent->got.refcount > 0
8700 && (ent->tls_type & TLS_TPREL) != 0
8701 && ent->addend == gent->addend
8702 && ent->owner == gent->owner)
8703 {
8704 gent->got.refcount = 0;
8705 break;
8706 }
8707
8708 /* If not, then we'll be using our own TPREL entry. */
8709 if (gent->got.refcount != 0)
8710 gent->tls_type = TLS_TLS | TLS_TPREL;
8711 }
8712
8713 /* Remove any list entry that won't generate a word in the GOT before
8714 we call merge_got_entries. Otherwise we risk merging to empty
8715 entries. */
8716 pgent = &h->got.glist;
8717 while ((gent = *pgent) != NULL)
8718 if (gent->got.refcount > 0)
8719 {
8720 if ((gent->tls_type & TLS_LD) != 0
8721 && !h->def_dynamic)
8722 {
8723 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8724 *pgent = gent->next;
8725 }
8726 else
8727 pgent = &gent->next;
8728 }
8729 else
8730 *pgent = gent->next;
8731
8732 if (!htab->do_multi_toc)
8733 merge_got_entries (&h->got.glist);
8734
8735 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8736 if (!gent->is_indirect)
8737 {
8738 /* Make sure this symbol is output as a dynamic symbol.
8739 Undefined weak syms won't yet be marked as dynamic,
8740 nor will all TLS symbols. */
8741 if (h->dynindx == -1
8742 && !h->forced_local
8743 && h->type != STT_GNU_IFUNC
8744 && htab->elf.dynamic_sections_created)
8745 {
8746 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8747 return FALSE;
8748 }
8749
8750 if (!is_ppc64_elf (gent->owner))
8751 abort ();
8752
8753 allocate_got (h, info, gent);
8754 }
8755
8756 if (eh->dyn_relocs == NULL
8757 || (!htab->elf.dynamic_sections_created
8758 && h->type != STT_GNU_IFUNC))
8759 return TRUE;
8760
8761 /* In the shared -Bsymbolic case, discard space allocated for
8762 dynamic pc-relative relocs against symbols which turn out to be
8763 defined in regular objects. For the normal shared case, discard
8764 space for relocs that have become local due to symbol visibility
8765 changes. */
8766
8767 if (info->shared)
8768 {
8769 /* Relocs that use pc_count are those that appear on a call insn,
8770 or certain REL relocs (see must_be_dyn_reloc) that can be
8771 generated via assembly. We want calls to protected symbols to
8772 resolve directly to the function rather than going via the plt.
8773 If people want function pointer comparisons to work as expected
8774 then they should avoid writing weird assembly. */
8775 if (SYMBOL_CALLS_LOCAL (info, h))
8776 {
8777 struct elf_dyn_relocs **pp;
8778
8779 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8780 {
8781 p->count -= p->pc_count;
8782 p->pc_count = 0;
8783 if (p->count == 0)
8784 *pp = p->next;
8785 else
8786 pp = &p->next;
8787 }
8788 }
8789
8790 /* Also discard relocs on undefined weak syms with non-default
8791 visibility. */
8792 if (eh->dyn_relocs != NULL
8793 && h->root.type == bfd_link_hash_undefweak)
8794 {
8795 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8796 eh->dyn_relocs = NULL;
8797
8798 /* Make sure this symbol is output as a dynamic symbol.
8799 Undefined weak syms won't yet be marked as dynamic. */
8800 else if (h->dynindx == -1
8801 && !h->forced_local)
8802 {
8803 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8804 return FALSE;
8805 }
8806 }
8807 }
8808 else if (h->type == STT_GNU_IFUNC)
8809 {
8810 if (!h->non_got_ref)
8811 eh->dyn_relocs = NULL;
8812 }
8813 else if (ELIMINATE_COPY_RELOCS)
8814 {
8815 /* For the non-shared case, discard space for relocs against
8816 symbols which turn out to need copy relocs or are not
8817 dynamic. */
8818
8819 if (!h->non_got_ref
8820 && !h->def_regular)
8821 {
8822 /* Make sure this symbol is output as a dynamic symbol.
8823 Undefined weak syms won't yet be marked as dynamic. */
8824 if (h->dynindx == -1
8825 && !h->forced_local)
8826 {
8827 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8828 return FALSE;
8829 }
8830
8831 /* If that succeeded, we know we'll be keeping all the
8832 relocs. */
8833 if (h->dynindx != -1)
8834 goto keep;
8835 }
8836
8837 eh->dyn_relocs = NULL;
8838
8839 keep: ;
8840 }
8841
8842 /* Finally, allocate space. */
8843 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8844 {
8845 asection *sreloc = elf_section_data (p->sec)->sreloc;
8846 if (!htab->elf.dynamic_sections_created)
8847 sreloc = htab->reliplt;
8848 sreloc->size += p->count * sizeof (Elf64_External_Rela);
8849 }
8850
8851 return TRUE;
8852 }
8853
8854 /* Find any dynamic relocs that apply to read-only sections. */
8855
8856 static bfd_boolean
8857 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8858 {
8859 struct ppc_link_hash_entry *eh;
8860 struct elf_dyn_relocs *p;
8861
8862 eh = (struct ppc_link_hash_entry *) h;
8863 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8864 {
8865 asection *s = p->sec->output_section;
8866
8867 if (s != NULL && (s->flags & SEC_READONLY) != 0)
8868 {
8869 struct bfd_link_info *info = inf;
8870
8871 info->flags |= DF_TEXTREL;
8872
8873 /* Not an error, just cut short the traversal. */
8874 return FALSE;
8875 }
8876 }
8877 return TRUE;
8878 }
8879
8880 /* Set the sizes of the dynamic sections. */
8881
8882 static bfd_boolean
8883 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
8884 struct bfd_link_info *info)
8885 {
8886 struct ppc_link_hash_table *htab;
8887 bfd *dynobj;
8888 asection *s;
8889 bfd_boolean relocs;
8890 bfd *ibfd;
8891 struct got_entry *first_tlsld;
8892
8893 htab = ppc_hash_table (info);
8894 if (htab == NULL)
8895 return FALSE;
8896
8897 dynobj = htab->elf.dynobj;
8898 if (dynobj == NULL)
8899 abort ();
8900
8901 if (htab->elf.dynamic_sections_created)
8902 {
8903 /* Set the contents of the .interp section to the interpreter. */
8904 if (info->executable)
8905 {
8906 s = bfd_get_section_by_name (dynobj, ".interp");
8907 if (s == NULL)
8908 abort ();
8909 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
8910 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
8911 }
8912 }
8913
8914 /* Set up .got offsets for local syms, and space for local dynamic
8915 relocs. */
8916 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8917 {
8918 struct got_entry **lgot_ents;
8919 struct got_entry **end_lgot_ents;
8920 struct plt_entry **local_plt;
8921 struct plt_entry **end_local_plt;
8922 unsigned char *lgot_masks;
8923 bfd_size_type locsymcount;
8924 Elf_Internal_Shdr *symtab_hdr;
8925 asection *srel;
8926
8927 if (!is_ppc64_elf (ibfd))
8928 continue;
8929
8930 for (s = ibfd->sections; s != NULL; s = s->next)
8931 {
8932 struct elf_dyn_relocs *p;
8933
8934 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
8935 {
8936 if (!bfd_is_abs_section (p->sec)
8937 && bfd_is_abs_section (p->sec->output_section))
8938 {
8939 /* Input section has been discarded, either because
8940 it is a copy of a linkonce section or due to
8941 linker script /DISCARD/, so we'll be discarding
8942 the relocs too. */
8943 }
8944 else if (p->count != 0)
8945 {
8946 srel = elf_section_data (p->sec)->sreloc;
8947 if (!htab->elf.dynamic_sections_created)
8948 srel = htab->reliplt;
8949 srel->size += p->count * sizeof (Elf64_External_Rela);
8950 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
8951 info->flags |= DF_TEXTREL;
8952 }
8953 }
8954 }
8955
8956 lgot_ents = elf_local_got_ents (ibfd);
8957 if (!lgot_ents)
8958 continue;
8959
8960 symtab_hdr = &elf_symtab_hdr (ibfd);
8961 locsymcount = symtab_hdr->sh_info;
8962 end_lgot_ents = lgot_ents + locsymcount;
8963 local_plt = (struct plt_entry **) end_lgot_ents;
8964 end_local_plt = local_plt + locsymcount;
8965 lgot_masks = (unsigned char *) end_local_plt;
8966 s = ppc64_elf_tdata (ibfd)->got;
8967 srel = ppc64_elf_tdata (ibfd)->relgot;
8968 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
8969 {
8970 struct got_entry **pent, *ent;
8971
8972 pent = lgot_ents;
8973 while ((ent = *pent) != NULL)
8974 if (ent->got.refcount > 0)
8975 {
8976 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
8977 {
8978 ppc64_tlsld_got (ibfd)->got.refcount += 1;
8979 *pent = ent->next;
8980 }
8981 else
8982 {
8983 unsigned int num = 1;
8984 ent->got.offset = s->size;
8985 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
8986 num = 2;
8987 s->size += num * 8;
8988 if (info->shared)
8989 srel->size += num * sizeof (Elf64_External_Rela);
8990 else if ((*lgot_masks & PLT_IFUNC) != 0)
8991 {
8992 htab->reliplt->size
8993 += num * sizeof (Elf64_External_Rela);
8994 htab->got_reli_size
8995 += num * sizeof (Elf64_External_Rela);
8996 }
8997 pent = &ent->next;
8998 }
8999 }
9000 else
9001 *pent = ent->next;
9002 }
9003
9004 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
9005 for (; local_plt < end_local_plt; ++local_plt)
9006 {
9007 struct plt_entry *ent;
9008
9009 for (ent = *local_plt; ent != NULL; ent = ent->next)
9010 if (ent->plt.refcount > 0)
9011 {
9012 s = htab->iplt;
9013 ent->plt.offset = s->size;
9014 s->size += PLT_ENTRY_SIZE;
9015
9016 htab->reliplt->size += sizeof (Elf64_External_Rela);
9017 }
9018 else
9019 ent->plt.offset = (bfd_vma) -1;
9020 }
9021 }
9022
9023 /* Allocate global sym .plt and .got entries, and space for global
9024 sym dynamic relocs. */
9025 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9026
9027 first_tlsld = NULL;
9028 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9029 {
9030 struct got_entry *ent;
9031
9032 if (!is_ppc64_elf (ibfd))
9033 continue;
9034
9035 ent = ppc64_tlsld_got (ibfd);
9036 if (ent->got.refcount > 0)
9037 {
9038 if (!htab->do_multi_toc && first_tlsld != NULL)
9039 {
9040 ent->is_indirect = TRUE;
9041 ent->got.ent = first_tlsld;
9042 }
9043 else
9044 {
9045 if (first_tlsld == NULL)
9046 first_tlsld = ent;
9047 s = ppc64_elf_tdata (ibfd)->got;
9048 ent->got.offset = s->size;
9049 ent->owner = ibfd;
9050 s->size += 16;
9051 if (info->shared)
9052 {
9053 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9054 srel->size += sizeof (Elf64_External_Rela);
9055 }
9056 }
9057 }
9058 else
9059 ent->got.offset = (bfd_vma) -1;
9060 }
9061
9062 /* We now have determined the sizes of the various dynamic sections.
9063 Allocate memory for them. */
9064 relocs = FALSE;
9065 for (s = dynobj->sections; s != NULL; s = s->next)
9066 {
9067 if ((s->flags & SEC_LINKER_CREATED) == 0)
9068 continue;
9069
9070 if (s == htab->brlt || s == htab->relbrlt)
9071 /* These haven't been allocated yet; don't strip. */
9072 continue;
9073 else if (s == htab->got
9074 || s == htab->plt
9075 || s == htab->iplt
9076 || s == htab->glink
9077 || s == htab->dynbss)
9078 {
9079 /* Strip this section if we don't need it; see the
9080 comment below. */
9081 }
9082 else if (s == htab->glink_eh_frame)
9083 {
9084 if (!bfd_is_abs_section (s->output_section))
9085 /* Not sized yet. */
9086 continue;
9087 }
9088 else if (CONST_STRNEQ (s->name, ".rela"))
9089 {
9090 if (s->size != 0)
9091 {
9092 if (s != htab->relplt)
9093 relocs = TRUE;
9094
9095 /* We use the reloc_count field as a counter if we need
9096 to copy relocs into the output file. */
9097 s->reloc_count = 0;
9098 }
9099 }
9100 else
9101 {
9102 /* It's not one of our sections, so don't allocate space. */
9103 continue;
9104 }
9105
9106 if (s->size == 0)
9107 {
9108 /* If we don't need this section, strip it from the
9109 output file. This is mostly to handle .rela.bss and
9110 .rela.plt. We must create both sections in
9111 create_dynamic_sections, because they must be created
9112 before the linker maps input sections to output
9113 sections. The linker does that before
9114 adjust_dynamic_symbol is called, and it is that
9115 function which decides whether anything needs to go
9116 into these sections. */
9117 s->flags |= SEC_EXCLUDE;
9118 continue;
9119 }
9120
9121 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9122 continue;
9123
9124 /* Allocate memory for the section contents. We use bfd_zalloc
9125 here in case unused entries are not reclaimed before the
9126 section's contents are written out. This should not happen,
9127 but this way if it does we get a R_PPC64_NONE reloc in .rela
9128 sections instead of garbage.
9129 We also rely on the section contents being zero when writing
9130 the GOT. */
9131 s->contents = bfd_zalloc (dynobj, s->size);
9132 if (s->contents == NULL)
9133 return FALSE;
9134 }
9135
9136 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9137 {
9138 if (!is_ppc64_elf (ibfd))
9139 continue;
9140
9141 s = ppc64_elf_tdata (ibfd)->got;
9142 if (s != NULL && s != htab->got)
9143 {
9144 if (s->size == 0)
9145 s->flags |= SEC_EXCLUDE;
9146 else
9147 {
9148 s->contents = bfd_zalloc (ibfd, s->size);
9149 if (s->contents == NULL)
9150 return FALSE;
9151 }
9152 }
9153 s = ppc64_elf_tdata (ibfd)->relgot;
9154 if (s != NULL)
9155 {
9156 if (s->size == 0)
9157 s->flags |= SEC_EXCLUDE;
9158 else
9159 {
9160 s->contents = bfd_zalloc (ibfd, s->size);
9161 if (s->contents == NULL)
9162 return FALSE;
9163 relocs = TRUE;
9164 s->reloc_count = 0;
9165 }
9166 }
9167 }
9168
9169 if (htab->elf.dynamic_sections_created)
9170 {
9171 /* Add some entries to the .dynamic section. We fill in the
9172 values later, in ppc64_elf_finish_dynamic_sections, but we
9173 must add the entries now so that we get the correct size for
9174 the .dynamic section. The DT_DEBUG entry is filled in by the
9175 dynamic linker and used by the debugger. */
9176 #define add_dynamic_entry(TAG, VAL) \
9177 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9178
9179 if (info->executable)
9180 {
9181 if (!add_dynamic_entry (DT_DEBUG, 0))
9182 return FALSE;
9183 }
9184
9185 if (htab->plt != NULL && htab->plt->size != 0)
9186 {
9187 if (!add_dynamic_entry (DT_PLTGOT, 0)
9188 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9189 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9190 || !add_dynamic_entry (DT_JMPREL, 0)
9191 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9192 return FALSE;
9193 }
9194
9195 if (NO_OPD_RELOCS)
9196 {
9197 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9198 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9199 return FALSE;
9200 }
9201
9202 if (!htab->no_tls_get_addr_opt
9203 && htab->tls_get_addr_fd != NULL
9204 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9205 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9206 return FALSE;
9207
9208 if (relocs)
9209 {
9210 if (!add_dynamic_entry (DT_RELA, 0)
9211 || !add_dynamic_entry (DT_RELASZ, 0)
9212 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9213 return FALSE;
9214
9215 /* If any dynamic relocs apply to a read-only section,
9216 then we need a DT_TEXTREL entry. */
9217 if ((info->flags & DF_TEXTREL) == 0)
9218 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
9219
9220 if ((info->flags & DF_TEXTREL) != 0)
9221 {
9222 if (!add_dynamic_entry (DT_TEXTREL, 0))
9223 return FALSE;
9224 }
9225 }
9226 }
9227 #undef add_dynamic_entry
9228
9229 return TRUE;
9230 }
9231
9232 /* Determine the type of stub needed, if any, for a call. */
9233
9234 static inline enum ppc_stub_type
9235 ppc_type_of_stub (asection *input_sec,
9236 const Elf_Internal_Rela *rel,
9237 struct ppc_link_hash_entry **hash,
9238 struct plt_entry **plt_ent,
9239 bfd_vma destination)
9240 {
9241 struct ppc_link_hash_entry *h = *hash;
9242 bfd_vma location;
9243 bfd_vma branch_offset;
9244 bfd_vma max_branch_offset;
9245 enum elf_ppc64_reloc_type r_type;
9246
9247 if (h != NULL)
9248 {
9249 struct plt_entry *ent;
9250 struct ppc_link_hash_entry *fdh = h;
9251 if (h->oh != NULL
9252 && h->oh->is_func_descriptor)
9253 {
9254 fdh = ppc_follow_link (h->oh);
9255 *hash = fdh;
9256 }
9257
9258 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9259 if (ent->addend == rel->r_addend
9260 && ent->plt.offset != (bfd_vma) -1)
9261 {
9262 *plt_ent = ent;
9263 return ppc_stub_plt_call;
9264 }
9265
9266 /* Here, we know we don't have a plt entry. If we don't have a
9267 either a defined function descriptor or a defined entry symbol
9268 in a regular object file, then it is pointless trying to make
9269 any other type of stub. */
9270 if (!is_static_defined (&fdh->elf)
9271 && !is_static_defined (&h->elf))
9272 return ppc_stub_none;
9273 }
9274 else if (elf_local_got_ents (input_sec->owner) != NULL)
9275 {
9276 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9277 struct plt_entry **local_plt = (struct plt_entry **)
9278 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9279 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9280
9281 if (local_plt[r_symndx] != NULL)
9282 {
9283 struct plt_entry *ent;
9284
9285 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9286 if (ent->addend == rel->r_addend
9287 && ent->plt.offset != (bfd_vma) -1)
9288 {
9289 *plt_ent = ent;
9290 return ppc_stub_plt_call;
9291 }
9292 }
9293 }
9294
9295 /* Determine where the call point is. */
9296 location = (input_sec->output_offset
9297 + input_sec->output_section->vma
9298 + rel->r_offset);
9299
9300 branch_offset = destination - location;
9301 r_type = ELF64_R_TYPE (rel->r_info);
9302
9303 /* Determine if a long branch stub is needed. */
9304 max_branch_offset = 1 << 25;
9305 if (r_type != R_PPC64_REL24)
9306 max_branch_offset = 1 << 15;
9307
9308 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9309 /* We need a stub. Figure out whether a long_branch or plt_branch
9310 is needed later. */
9311 return ppc_stub_long_branch;
9312
9313 return ppc_stub_none;
9314 }
9315
9316 /* Build a .plt call stub. */
9317
9318 static inline bfd_byte *
9319 build_plt_stub (bfd *obfd, bfd_byte *p, int offset, Elf_Internal_Rela *r,
9320 bfd_boolean plt_static_chain)
9321 {
9322 #define PPC_LO(v) ((v) & 0xffff)
9323 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9324 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9325
9326 if (PPC_HA (offset) != 0)
9327 {
9328 if (r != NULL)
9329 {
9330 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9331 r[1].r_offset = r[0].r_offset + 8;
9332 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9333 r[1].r_addend = r[0].r_addend;
9334 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9335 {
9336 r[2].r_offset = r[1].r_offset + 4;
9337 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9338 r[2].r_addend = r[0].r_addend;
9339 }
9340 else
9341 {
9342 r[2].r_offset = r[1].r_offset + 8;
9343 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9344 r[2].r_addend = r[0].r_addend + 8;
9345 if (plt_static_chain)
9346 {
9347 r[3].r_offset = r[2].r_offset + 4;
9348 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9349 r[3].r_addend = r[0].r_addend + 16;
9350 }
9351 }
9352 }
9353 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9354 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9355 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
9356 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9357 {
9358 bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9359 offset = 0;
9360 }
9361 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9362 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p), p += 4;
9363 if (plt_static_chain)
9364 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
9365 bfd_put_32 (obfd, BCTR, p), p += 4;
9366 }
9367 else
9368 {
9369 if (r != NULL)
9370 {
9371 r[0].r_offset += 4;
9372 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9373 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9374 {
9375 r[1].r_offset = r[0].r_offset + 4;
9376 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9377 r[1].r_addend = r[0].r_addend;
9378 }
9379 else
9380 {
9381 r[1].r_offset = r[0].r_offset + 8;
9382 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9383 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
9384 if (plt_static_chain)
9385 {
9386 r[2].r_offset = r[1].r_offset + 4;
9387 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9388 r[2].r_addend = r[0].r_addend + 8;
9389 }
9390 }
9391 }
9392 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9393 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p), p += 4;
9394 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9395 {
9396 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9397 offset = 0;
9398 }
9399 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9400 if (plt_static_chain)
9401 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9402 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9403 bfd_put_32 (obfd, BCTR, p), p += 4;
9404 }
9405 return p;
9406 }
9407
9408 /* Build a special .plt call stub for __tls_get_addr. */
9409
9410 #define LD_R11_0R3 0xe9630000
9411 #define LD_R12_0R3 0xe9830000
9412 #define MR_R0_R3 0x7c601b78
9413 #define CMPDI_R11_0 0x2c2b0000
9414 #define ADD_R3_R12_R13 0x7c6c6a14
9415 #define BEQLR 0x4d820020
9416 #define MR_R3_R0 0x7c030378
9417 #define MFLR_R11 0x7d6802a6
9418 #define STD_R11_0R1 0xf9610000
9419 #define BCTRL 0x4e800421
9420 #define LD_R11_0R1 0xe9610000
9421 #define LD_R2_0R1 0xe8410000
9422 #define MTLR_R11 0x7d6803a6
9423
9424 static inline bfd_byte *
9425 build_tls_get_addr_stub (bfd *obfd, bfd_byte *p, int offset,
9426 Elf_Internal_Rela *r, bfd_boolean plt_static_chain)
9427 {
9428 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9429 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9430 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9431 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9432 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9433 bfd_put_32 (obfd, BEQLR, p), p += 4;
9434 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9435 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9436 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
9437
9438 if (r != NULL)
9439 r[0].r_offset += 9 * 4;
9440 p = build_plt_stub (obfd, p, offset, r, plt_static_chain);
9441 bfd_put_32 (obfd, BCTRL, p - 4);
9442
9443 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
9444 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
9445 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9446 bfd_put_32 (obfd, BLR, p), p += 4;
9447
9448 return p;
9449 }
9450
9451 static Elf_Internal_Rela *
9452 get_relocs (asection *sec, int count)
9453 {
9454 Elf_Internal_Rela *relocs;
9455 struct bfd_elf_section_data *elfsec_data;
9456
9457 elfsec_data = elf_section_data (sec);
9458 relocs = elfsec_data->relocs;
9459 if (relocs == NULL)
9460 {
9461 bfd_size_type relsize;
9462 relsize = sec->reloc_count * sizeof (*relocs);
9463 relocs = bfd_alloc (sec->owner, relsize);
9464 if (relocs == NULL)
9465 return NULL;
9466 elfsec_data->relocs = relocs;
9467 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9468 sizeof (Elf_Internal_Shdr));
9469 if (elfsec_data->rela.hdr == NULL)
9470 return NULL;
9471 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9472 * sizeof (Elf64_External_Rela));
9473 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
9474 sec->reloc_count = 0;
9475 }
9476 relocs += sec->reloc_count;
9477 sec->reloc_count += count;
9478 return relocs;
9479 }
9480
9481 static bfd_vma
9482 get_r2off (struct bfd_link_info *info,
9483 struct ppc_stub_hash_entry *stub_entry)
9484 {
9485 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9486 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
9487
9488 if (r2off == 0)
9489 {
9490 /* Support linking -R objects. Get the toc pointer from the
9491 opd entry. */
9492 char buf[8];
9493 asection *opd = stub_entry->h->elf.root.u.def.section;
9494 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
9495
9496 if (strcmp (opd->name, ".opd") != 0
9497 || opd->reloc_count != 0)
9498 {
9499 info->callbacks->einfo (_("%P: cannot find opd entry toc for %s\n"),
9500 stub_entry->h->elf.root.root.string);
9501 bfd_set_error (bfd_error_bad_value);
9502 return 0;
9503 }
9504 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
9505 return 0;
9506 r2off = bfd_get_64 (opd->owner, buf);
9507 r2off -= elf_gp (info->output_bfd);
9508 }
9509 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
9510 return r2off;
9511 }
9512
9513 static bfd_boolean
9514 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9515 {
9516 struct ppc_stub_hash_entry *stub_entry;
9517 struct ppc_branch_hash_entry *br_entry;
9518 struct bfd_link_info *info;
9519 struct ppc_link_hash_table *htab;
9520 bfd_byte *loc;
9521 bfd_byte *p;
9522 bfd_vma dest, off;
9523 int size;
9524 Elf_Internal_Rela *r;
9525 asection *plt;
9526
9527 /* Massage our args to the form they really have. */
9528 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9529 info = in_arg;
9530
9531 htab = ppc_hash_table (info);
9532 if (htab == NULL)
9533 return FALSE;
9534
9535 /* Make a note of the offset within the stubs for this entry. */
9536 stub_entry->stub_offset = stub_entry->stub_sec->size;
9537 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
9538
9539 htab->stub_count[stub_entry->stub_type - 1] += 1;
9540 switch (stub_entry->stub_type)
9541 {
9542 case ppc_stub_long_branch:
9543 case ppc_stub_long_branch_r2off:
9544 /* Branches are relative. This is where we are going to. */
9545 off = dest = (stub_entry->target_value
9546 + stub_entry->target_section->output_offset
9547 + stub_entry->target_section->output_section->vma);
9548
9549 /* And this is where we are coming from. */
9550 off -= (stub_entry->stub_offset
9551 + stub_entry->stub_sec->output_offset
9552 + stub_entry->stub_sec->output_section->vma);
9553
9554 size = 4;
9555 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9556 {
9557 bfd_vma r2off = get_r2off (info, stub_entry);
9558
9559 if (r2off == 0)
9560 {
9561 htab->stub_error = TRUE;
9562 return FALSE;
9563 }
9564 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9565 loc += 4;
9566 size = 12;
9567 if (PPC_HA (r2off) != 0)
9568 {
9569 size = 16;
9570 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9571 loc += 4;
9572 }
9573 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9574 loc += 4;
9575 off -= size - 4;
9576 }
9577 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
9578
9579 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9580 {
9581 info->callbacks->einfo (_("%P: long branch stub `%s' offset overflow\n"),
9582 stub_entry->root.string);
9583 htab->stub_error = TRUE;
9584 return FALSE;
9585 }
9586
9587 if (info->emitrelocations)
9588 {
9589 r = get_relocs (stub_entry->stub_sec, 1);
9590 if (r == NULL)
9591 return FALSE;
9592 r->r_offset = loc - stub_entry->stub_sec->contents;
9593 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9594 r->r_addend = dest;
9595 if (stub_entry->h != NULL)
9596 {
9597 struct elf_link_hash_entry **hashes;
9598 unsigned long symndx;
9599 struct ppc_link_hash_entry *h;
9600
9601 hashes = elf_sym_hashes (htab->stub_bfd);
9602 if (hashes == NULL)
9603 {
9604 bfd_size_type hsize;
9605
9606 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9607 hashes = bfd_zalloc (htab->stub_bfd, hsize);
9608 if (hashes == NULL)
9609 return FALSE;
9610 elf_sym_hashes (htab->stub_bfd) = hashes;
9611 htab->stub_globals = 1;
9612 }
9613 symndx = htab->stub_globals++;
9614 h = stub_entry->h;
9615 hashes[symndx] = &h->elf;
9616 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9617 if (h->oh != NULL && h->oh->is_func)
9618 h = ppc_follow_link (h->oh);
9619 if (h->elf.root.u.def.section != stub_entry->target_section)
9620 /* H is an opd symbol. The addend must be zero. */
9621 r->r_addend = 0;
9622 else
9623 {
9624 off = (h->elf.root.u.def.value
9625 + h->elf.root.u.def.section->output_offset
9626 + h->elf.root.u.def.section->output_section->vma);
9627 r->r_addend -= off;
9628 }
9629 }
9630 }
9631 break;
9632
9633 case ppc_stub_plt_branch:
9634 case ppc_stub_plt_branch_r2off:
9635 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9636 stub_entry->root.string + 9,
9637 FALSE, FALSE);
9638 if (br_entry == NULL)
9639 {
9640 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
9641 stub_entry->root.string);
9642 htab->stub_error = TRUE;
9643 return FALSE;
9644 }
9645
9646 dest = (stub_entry->target_value
9647 + stub_entry->target_section->output_offset
9648 + stub_entry->target_section->output_section->vma);
9649
9650 bfd_put_64 (htab->brlt->owner, dest,
9651 htab->brlt->contents + br_entry->offset);
9652
9653 if (br_entry->iter == htab->stub_iteration)
9654 {
9655 br_entry->iter = 0;
9656
9657 if (htab->relbrlt != NULL)
9658 {
9659 /* Create a reloc for the branch lookup table entry. */
9660 Elf_Internal_Rela rela;
9661 bfd_byte *rl;
9662
9663 rela.r_offset = (br_entry->offset
9664 + htab->brlt->output_offset
9665 + htab->brlt->output_section->vma);
9666 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9667 rela.r_addend = dest;
9668
9669 rl = htab->relbrlt->contents;
9670 rl += (htab->relbrlt->reloc_count++
9671 * sizeof (Elf64_External_Rela));
9672 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9673 }
9674 else if (info->emitrelocations)
9675 {
9676 r = get_relocs (htab->brlt, 1);
9677 if (r == NULL)
9678 return FALSE;
9679 /* brlt, being SEC_LINKER_CREATED does not go through the
9680 normal reloc processing. Symbols and offsets are not
9681 translated from input file to output file form, so
9682 set up the offset per the output file. */
9683 r->r_offset = (br_entry->offset
9684 + htab->brlt->output_offset
9685 + htab->brlt->output_section->vma);
9686 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9687 r->r_addend = dest;
9688 }
9689 }
9690
9691 dest = (br_entry->offset
9692 + htab->brlt->output_offset
9693 + htab->brlt->output_section->vma);
9694
9695 off = (dest
9696 - elf_gp (htab->brlt->output_section->owner)
9697 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9698
9699 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
9700 {
9701 info->callbacks->einfo
9702 (_("%P: linkage table error against `%s'\n"),
9703 stub_entry->root.string);
9704 bfd_set_error (bfd_error_bad_value);
9705 htab->stub_error = TRUE;
9706 return FALSE;
9707 }
9708
9709 if (info->emitrelocations)
9710 {
9711 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
9712 if (r == NULL)
9713 return FALSE;
9714 r[0].r_offset = loc - stub_entry->stub_sec->contents;
9715 if (bfd_big_endian (info->output_bfd))
9716 r[0].r_offset += 2;
9717 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
9718 r[0].r_offset += 4;
9719 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9720 r[0].r_addend = dest;
9721 if (PPC_HA (off) != 0)
9722 {
9723 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9724 r[1].r_offset = r[0].r_offset + 4;
9725 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9726 r[1].r_addend = r[0].r_addend;
9727 }
9728 }
9729
9730 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9731 {
9732 if (PPC_HA (off) != 0)
9733 {
9734 size = 16;
9735 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
9736 loc += 4;
9737 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
9738 }
9739 else
9740 {
9741 size = 12;
9742 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
9743 }
9744 }
9745 else
9746 {
9747 bfd_vma r2off = get_r2off (info, stub_entry);
9748
9749 if (r2off == 0)
9750 {
9751 htab->stub_error = TRUE;
9752 return FALSE;
9753 }
9754
9755 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9756 loc += 4;
9757 size = 20;
9758 if (PPC_HA (off) != 0)
9759 {
9760 size += 4;
9761 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
9762 loc += 4;
9763 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
9764 loc += 4;
9765 }
9766 else
9767 {
9768 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
9769 loc += 4;
9770 }
9771
9772 if (PPC_HA (r2off) != 0)
9773 {
9774 size += 4;
9775 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9776 loc += 4;
9777 }
9778 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9779 }
9780 loc += 4;
9781 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
9782 loc += 4;
9783 bfd_put_32 (htab->stub_bfd, BCTR, loc);
9784 break;
9785
9786 case ppc_stub_plt_call:
9787 if (stub_entry->h != NULL
9788 && stub_entry->h->is_func_descriptor
9789 && stub_entry->h->oh != NULL)
9790 {
9791 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
9792
9793 /* If the old-ABI "dot-symbol" is undefined make it weak so
9794 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
9795 FIXME: We used to define the symbol on one of the call
9796 stubs instead, which is why we test symbol section id
9797 against htab->top_id in various places. Likely all
9798 these checks could now disappear. */
9799 if (fh->elf.root.type == bfd_link_hash_undefined)
9800 fh->elf.root.type = bfd_link_hash_undefweak;
9801 /* Stop undo_symbol_twiddle changing it back to undefined. */
9802 fh->was_undefined = 0;
9803 }
9804
9805 /* Now build the stub. */
9806 dest = stub_entry->plt_ent->plt.offset & ~1;
9807 if (dest >= (bfd_vma) -2)
9808 abort ();
9809
9810 plt = htab->plt;
9811 if (!htab->elf.dynamic_sections_created
9812 || stub_entry->h == NULL
9813 || stub_entry->h->elf.dynindx == -1)
9814 plt = htab->iplt;
9815
9816 dest += plt->output_offset + plt->output_section->vma;
9817
9818 if (stub_entry->h == NULL
9819 && (stub_entry->plt_ent->plt.offset & 1) == 0)
9820 {
9821 Elf_Internal_Rela rela;
9822 bfd_byte *rl;
9823
9824 rela.r_offset = dest;
9825 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
9826 rela.r_addend = (stub_entry->target_value
9827 + stub_entry->target_section->output_offset
9828 + stub_entry->target_section->output_section->vma);
9829
9830 rl = (htab->reliplt->contents
9831 + (htab->reliplt->reloc_count++
9832 * sizeof (Elf64_External_Rela)));
9833 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
9834 stub_entry->plt_ent->plt.offset |= 1;
9835 }
9836
9837 off = (dest
9838 - elf_gp (plt->output_section->owner)
9839 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9840
9841 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
9842 {
9843 info->callbacks->einfo
9844 (_("%P: linkage table error against `%s'\n"),
9845 stub_entry->h != NULL
9846 ? stub_entry->h->elf.root.root.string
9847 : "<local sym>");
9848 bfd_set_error (bfd_error_bad_value);
9849 htab->stub_error = TRUE;
9850 return FALSE;
9851 }
9852
9853 r = NULL;
9854 if (info->emitrelocations)
9855 {
9856 r = get_relocs (stub_entry->stub_sec,
9857 (2
9858 + (PPC_HA (off) != 0)
9859 + (htab->plt_static_chain
9860 && PPC_HA (off + 16) == PPC_HA (off))));
9861 if (r == NULL)
9862 return FALSE;
9863 r[0].r_offset = loc - stub_entry->stub_sec->contents;
9864 if (bfd_big_endian (info->output_bfd))
9865 r[0].r_offset += 2;
9866 r[0].r_addend = dest;
9867 }
9868 if (stub_entry->h != NULL
9869 && (stub_entry->h == htab->tls_get_addr_fd
9870 || stub_entry->h == htab->tls_get_addr)
9871 && !htab->no_tls_get_addr_opt)
9872 p = build_tls_get_addr_stub (htab->stub_bfd, loc, off, r,
9873 htab->plt_static_chain);
9874 else
9875 p = build_plt_stub (htab->stub_bfd, loc, off, r,
9876 htab->plt_static_chain);
9877 size = p - loc;
9878 break;
9879
9880 default:
9881 BFD_FAIL ();
9882 return FALSE;
9883 }
9884
9885 stub_entry->stub_sec->size += size;
9886
9887 if (htab->emit_stub_syms)
9888 {
9889 struct elf_link_hash_entry *h;
9890 size_t len1, len2;
9891 char *name;
9892 const char *const stub_str[] = { "long_branch",
9893 "long_branch_r2off",
9894 "plt_branch",
9895 "plt_branch_r2off",
9896 "plt_call" };
9897
9898 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
9899 len2 = strlen (stub_entry->root.string);
9900 name = bfd_malloc (len1 + len2 + 2);
9901 if (name == NULL)
9902 return FALSE;
9903 memcpy (name, stub_entry->root.string, 9);
9904 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
9905 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
9906 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
9907 if (h == NULL)
9908 return FALSE;
9909 if (h->root.type == bfd_link_hash_new)
9910 {
9911 h->root.type = bfd_link_hash_defined;
9912 h->root.u.def.section = stub_entry->stub_sec;
9913 h->root.u.def.value = stub_entry->stub_offset;
9914 h->ref_regular = 1;
9915 h->def_regular = 1;
9916 h->ref_regular_nonweak = 1;
9917 h->forced_local = 1;
9918 h->non_elf = 0;
9919 }
9920 }
9921
9922 return TRUE;
9923 }
9924
9925 /* As above, but don't actually build the stub. Just bump offset so
9926 we know stub section sizes, and select plt_branch stubs where
9927 long_branch stubs won't do. */
9928
9929 static bfd_boolean
9930 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9931 {
9932 struct ppc_stub_hash_entry *stub_entry;
9933 struct bfd_link_info *info;
9934 struct ppc_link_hash_table *htab;
9935 bfd_vma off;
9936 int size;
9937
9938 /* Massage our args to the form they really have. */
9939 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9940 info = in_arg;
9941
9942 htab = ppc_hash_table (info);
9943 if (htab == NULL)
9944 return FALSE;
9945
9946 if (stub_entry->stub_type == ppc_stub_plt_call)
9947 {
9948 asection *plt;
9949 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
9950 if (off >= (bfd_vma) -2)
9951 abort ();
9952 plt = htab->plt;
9953 if (!htab->elf.dynamic_sections_created
9954 || stub_entry->h == NULL
9955 || stub_entry->h->elf.dynindx == -1)
9956 plt = htab->iplt;
9957 off += (plt->output_offset
9958 + plt->output_section->vma
9959 - elf_gp (plt->output_section->owner)
9960 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9961
9962 size = PLT_CALL_STUB_SIZE;
9963 if (!htab->plt_static_chain)
9964 size -= 4;
9965 if (PPC_HA (off) == 0)
9966 size -= 4;
9967 if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
9968 size += 4;
9969 if (stub_entry->h != NULL
9970 && (stub_entry->h == htab->tls_get_addr_fd
9971 || stub_entry->h == htab->tls_get_addr)
9972 && !htab->no_tls_get_addr_opt)
9973 size += 13 * 4;
9974 if (info->emitrelocations)
9975 {
9976 stub_entry->stub_sec->reloc_count
9977 += (2
9978 + (PPC_HA (off) != 0)
9979 + (htab->plt_static_chain
9980 && PPC_HA (off + 16) == PPC_HA (off)));
9981 stub_entry->stub_sec->flags |= SEC_RELOC;
9982 }
9983 }
9984 else
9985 {
9986 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
9987 variants. */
9988 bfd_vma r2off = 0;
9989
9990 off = (stub_entry->target_value
9991 + stub_entry->target_section->output_offset
9992 + stub_entry->target_section->output_section->vma);
9993 off -= (stub_entry->stub_sec->size
9994 + stub_entry->stub_sec->output_offset
9995 + stub_entry->stub_sec->output_section->vma);
9996
9997 /* Reset the stub type from the plt variant in case we now
9998 can reach with a shorter stub. */
9999 if (stub_entry->stub_type >= ppc_stub_plt_branch)
10000 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10001
10002 size = 4;
10003 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10004 {
10005 r2off = get_r2off (info, stub_entry);
10006 if (r2off == 0)
10007 {
10008 htab->stub_error = TRUE;
10009 return FALSE;
10010 }
10011 size = 12;
10012 if (PPC_HA (r2off) != 0)
10013 size = 16;
10014 off -= size - 4;
10015 }
10016
10017 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
10018 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10019 {
10020 struct ppc_branch_hash_entry *br_entry;
10021
10022 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10023 stub_entry->root.string + 9,
10024 TRUE, FALSE);
10025 if (br_entry == NULL)
10026 {
10027 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
10028 stub_entry->root.string);
10029 htab->stub_error = TRUE;
10030 return FALSE;
10031 }
10032
10033 if (br_entry->iter != htab->stub_iteration)
10034 {
10035 br_entry->iter = htab->stub_iteration;
10036 br_entry->offset = htab->brlt->size;
10037 htab->brlt->size += 8;
10038
10039 if (htab->relbrlt != NULL)
10040 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10041 else if (info->emitrelocations)
10042 {
10043 htab->brlt->reloc_count += 1;
10044 htab->brlt->flags |= SEC_RELOC;
10045 }
10046 }
10047
10048 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
10049 off = (br_entry->offset
10050 + htab->brlt->output_offset
10051 + htab->brlt->output_section->vma
10052 - elf_gp (htab->brlt->output_section->owner)
10053 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10054
10055 if (info->emitrelocations)
10056 {
10057 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10058 stub_entry->stub_sec->flags |= SEC_RELOC;
10059 }
10060
10061 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10062 {
10063 size = 12;
10064 if (PPC_HA (off) != 0)
10065 size = 16;
10066 }
10067 else
10068 {
10069 size = 20;
10070 if (PPC_HA (off) != 0)
10071 size += 4;
10072
10073 if (PPC_HA (r2off) != 0)
10074 size += 4;
10075 }
10076 }
10077 else if (info->emitrelocations)
10078 {
10079 stub_entry->stub_sec->reloc_count += 1;
10080 stub_entry->stub_sec->flags |= SEC_RELOC;
10081 }
10082 }
10083
10084 stub_entry->stub_sec->size += size;
10085 return TRUE;
10086 }
10087
10088 /* Set up various things so that we can make a list of input sections
10089 for each output section included in the link. Returns -1 on error,
10090 0 when no stubs will be needed, and 1 on success. */
10091
10092 int
10093 ppc64_elf_setup_section_lists
10094 (struct bfd_link_info *info,
10095 asection *(*add_stub_section) (const char *, asection *),
10096 void (*layout_sections_again) (void))
10097 {
10098 bfd *input_bfd;
10099 int top_id, top_index, id;
10100 asection *section;
10101 asection **input_list;
10102 bfd_size_type amt;
10103 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10104
10105 if (htab == NULL)
10106 return -1;
10107 /* Stash our params away. */
10108 htab->add_stub_section = add_stub_section;
10109 htab->layout_sections_again = layout_sections_again;
10110
10111 if (htab->brlt == NULL)
10112 return 0;
10113
10114 /* Find the top input section id. */
10115 for (input_bfd = info->input_bfds, top_id = 3;
10116 input_bfd != NULL;
10117 input_bfd = input_bfd->link_next)
10118 {
10119 for (section = input_bfd->sections;
10120 section != NULL;
10121 section = section->next)
10122 {
10123 if (top_id < section->id)
10124 top_id = section->id;
10125 }
10126 }
10127
10128 htab->top_id = top_id;
10129 amt = sizeof (struct map_stub) * (top_id + 1);
10130 htab->stub_group = bfd_zmalloc (amt);
10131 if (htab->stub_group == NULL)
10132 return -1;
10133
10134 /* Set toc_off for com, und, abs and ind sections. */
10135 for (id = 0; id < 3; id++)
10136 htab->stub_group[id].toc_off = TOC_BASE_OFF;
10137
10138 /* We can't use output_bfd->section_count here to find the top output
10139 section index as some sections may have been removed, and
10140 strip_excluded_output_sections doesn't renumber the indices. */
10141 for (section = info->output_bfd->sections, top_index = 0;
10142 section != NULL;
10143 section = section->next)
10144 {
10145 if (top_index < section->index)
10146 top_index = section->index;
10147 }
10148
10149 htab->top_index = top_index;
10150 amt = sizeof (asection *) * (top_index + 1);
10151 input_list = bfd_zmalloc (amt);
10152 htab->input_list = input_list;
10153 if (input_list == NULL)
10154 return -1;
10155
10156 return 1;
10157 }
10158
10159 /* Set up for first pass at multitoc partitioning. */
10160
10161 void
10162 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10163 {
10164 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10165
10166 elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10167 htab->toc_curr = elf_gp (info->output_bfd);
10168 htab->toc_bfd = NULL;
10169 htab->toc_first_sec = NULL;
10170 }
10171
10172 /* The linker repeatedly calls this function for each TOC input section
10173 and linker generated GOT section. Group input bfds such that the toc
10174 within a group is less than 64k in size. */
10175
10176 bfd_boolean
10177 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10178 {
10179 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10180 bfd_vma addr, off, limit;
10181
10182 if (htab == NULL)
10183 return FALSE;
10184
10185 if (!htab->second_toc_pass)
10186 {
10187 /* Keep track of the first .toc or .got section for this input bfd. */
10188 if (htab->toc_bfd != isec->owner)
10189 {
10190 htab->toc_bfd = isec->owner;
10191 htab->toc_first_sec = isec;
10192 }
10193
10194 addr = isec->output_offset + isec->output_section->vma;
10195 off = addr - htab->toc_curr;
10196 limit = 0x80008000;
10197 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10198 limit = 0x10000;
10199 if (off + isec->size > limit)
10200 {
10201 addr = (htab->toc_first_sec->output_offset
10202 + htab->toc_first_sec->output_section->vma);
10203 htab->toc_curr = addr;
10204 }
10205
10206 /* toc_curr is the base address of this toc group. Set elf_gp
10207 for the input section to be the offset relative to the
10208 output toc base plus 0x8000. Making the input elf_gp an
10209 offset allows us to move the toc as a whole without
10210 recalculating input elf_gp. */
10211 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10212 off += TOC_BASE_OFF;
10213
10214 /* Die if someone uses a linker script that doesn't keep input
10215 file .toc and .got together. */
10216 if (elf_gp (isec->owner) != 0
10217 && elf_gp (isec->owner) != off)
10218 return FALSE;
10219
10220 elf_gp (isec->owner) = off;
10221 return TRUE;
10222 }
10223
10224 /* During the second pass toc_first_sec points to the start of
10225 a toc group, and toc_curr is used to track the old elf_gp.
10226 We use toc_bfd to ensure we only look at each bfd once. */
10227 if (htab->toc_bfd == isec->owner)
10228 return TRUE;
10229 htab->toc_bfd = isec->owner;
10230
10231 if (htab->toc_first_sec == NULL
10232 || htab->toc_curr != elf_gp (isec->owner))
10233 {
10234 htab->toc_curr = elf_gp (isec->owner);
10235 htab->toc_first_sec = isec;
10236 }
10237 addr = (htab->toc_first_sec->output_offset
10238 + htab->toc_first_sec->output_section->vma);
10239 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10240 elf_gp (isec->owner) = off;
10241
10242 return TRUE;
10243 }
10244
10245 /* Called via elf_link_hash_traverse to merge GOT entries for global
10246 symbol H. */
10247
10248 static bfd_boolean
10249 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10250 {
10251 if (h->root.type == bfd_link_hash_indirect)
10252 return TRUE;
10253
10254 merge_got_entries (&h->got.glist);
10255
10256 return TRUE;
10257 }
10258
10259 /* Called via elf_link_hash_traverse to allocate GOT entries for global
10260 symbol H. */
10261
10262 static bfd_boolean
10263 reallocate_got (struct elf_link_hash_entry *h, void *inf)
10264 {
10265 struct got_entry *gent;
10266
10267 if (h->root.type == bfd_link_hash_indirect)
10268 return TRUE;
10269
10270 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10271 if (!gent->is_indirect)
10272 allocate_got (h, (struct bfd_link_info *) inf, gent);
10273 return TRUE;
10274 }
10275
10276 /* Called on the first multitoc pass after the last call to
10277 ppc64_elf_next_toc_section. This function removes duplicate GOT
10278 entries. */
10279
10280 bfd_boolean
10281 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
10282 {
10283 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10284 struct bfd *ibfd, *ibfd2;
10285 bfd_boolean done_something;
10286
10287 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
10288
10289 if (!htab->do_multi_toc)
10290 return FALSE;
10291
10292 /* Merge global sym got entries within a toc group. */
10293 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10294
10295 /* And tlsld_got. */
10296 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10297 {
10298 struct got_entry *ent, *ent2;
10299
10300 if (!is_ppc64_elf (ibfd))
10301 continue;
10302
10303 ent = ppc64_tlsld_got (ibfd);
10304 if (!ent->is_indirect
10305 && ent->got.offset != (bfd_vma) -1)
10306 {
10307 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10308 {
10309 if (!is_ppc64_elf (ibfd2))
10310 continue;
10311
10312 ent2 = ppc64_tlsld_got (ibfd2);
10313 if (!ent2->is_indirect
10314 && ent2->got.offset != (bfd_vma) -1
10315 && elf_gp (ibfd2) == elf_gp (ibfd))
10316 {
10317 ent2->is_indirect = TRUE;
10318 ent2->got.ent = ent;
10319 }
10320 }
10321 }
10322 }
10323
10324 /* Zap sizes of got sections. */
10325 htab->reliplt->rawsize = htab->reliplt->size;
10326 htab->reliplt->size -= htab->got_reli_size;
10327 htab->got_reli_size = 0;
10328
10329 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10330 {
10331 asection *got, *relgot;
10332
10333 if (!is_ppc64_elf (ibfd))
10334 continue;
10335
10336 got = ppc64_elf_tdata (ibfd)->got;
10337 if (got != NULL)
10338 {
10339 got->rawsize = got->size;
10340 got->size = 0;
10341 relgot = ppc64_elf_tdata (ibfd)->relgot;
10342 relgot->rawsize = relgot->size;
10343 relgot->size = 0;
10344 }
10345 }
10346
10347 /* Now reallocate the got, local syms first. We don't need to
10348 allocate section contents again since we never increase size. */
10349 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10350 {
10351 struct got_entry **lgot_ents;
10352 struct got_entry **end_lgot_ents;
10353 struct plt_entry **local_plt;
10354 struct plt_entry **end_local_plt;
10355 unsigned char *lgot_masks;
10356 bfd_size_type locsymcount;
10357 Elf_Internal_Shdr *symtab_hdr;
10358 asection *s, *srel;
10359
10360 if (!is_ppc64_elf (ibfd))
10361 continue;
10362
10363 lgot_ents = elf_local_got_ents (ibfd);
10364 if (!lgot_ents)
10365 continue;
10366
10367 symtab_hdr = &elf_symtab_hdr (ibfd);
10368 locsymcount = symtab_hdr->sh_info;
10369 end_lgot_ents = lgot_ents + locsymcount;
10370 local_plt = (struct plt_entry **) end_lgot_ents;
10371 end_local_plt = local_plt + locsymcount;
10372 lgot_masks = (unsigned char *) end_local_plt;
10373 s = ppc64_elf_tdata (ibfd)->got;
10374 srel = ppc64_elf_tdata (ibfd)->relgot;
10375 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10376 {
10377 struct got_entry *ent;
10378
10379 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
10380 {
10381 unsigned int num = 1;
10382 ent->got.offset = s->size;
10383 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10384 num = 2;
10385 s->size += num * 8;
10386 if (info->shared)
10387 srel->size += num * sizeof (Elf64_External_Rela);
10388 else if ((*lgot_masks & PLT_IFUNC) != 0)
10389 {
10390 htab->reliplt->size
10391 += num * sizeof (Elf64_External_Rela);
10392 htab->got_reli_size
10393 += num * sizeof (Elf64_External_Rela);
10394 }
10395 }
10396 }
10397 }
10398
10399 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10400
10401 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10402 {
10403 struct got_entry *ent;
10404
10405 if (!is_ppc64_elf (ibfd))
10406 continue;
10407
10408 ent = ppc64_tlsld_got (ibfd);
10409 if (!ent->is_indirect
10410 && ent->got.offset != (bfd_vma) -1)
10411 {
10412 asection *s = ppc64_elf_tdata (ibfd)->got;
10413 ent->got.offset = s->size;
10414 s->size += 16;
10415 if (info->shared)
10416 {
10417 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10418 srel->size += sizeof (Elf64_External_Rela);
10419 }
10420 }
10421 }
10422
10423 done_something = htab->reliplt->rawsize != htab->reliplt->size;
10424 if (!done_something)
10425 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10426 {
10427 asection *got;
10428
10429 if (!is_ppc64_elf (ibfd))
10430 continue;
10431
10432 got = ppc64_elf_tdata (ibfd)->got;
10433 if (got != NULL)
10434 {
10435 done_something = got->rawsize != got->size;
10436 if (done_something)
10437 break;
10438 }
10439 }
10440
10441 if (done_something)
10442 (*htab->layout_sections_again) ();
10443
10444 /* Set up for second pass over toc sections to recalculate elf_gp
10445 on input sections. */
10446 htab->toc_bfd = NULL;
10447 htab->toc_first_sec = NULL;
10448 htab->second_toc_pass = TRUE;
10449 return done_something;
10450 }
10451
10452 /* Called after second pass of multitoc partitioning. */
10453
10454 void
10455 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10456 {
10457 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10458
10459 /* After the second pass, toc_curr tracks the TOC offset used
10460 for code sections below in ppc64_elf_next_input_section. */
10461 htab->toc_curr = TOC_BASE_OFF;
10462 }
10463
10464 /* No toc references were found in ISEC. If the code in ISEC makes no
10465 calls, then there's no need to use toc adjusting stubs when branching
10466 into ISEC. Actually, indirect calls from ISEC are OK as they will
10467 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
10468 needed, and 2 if a cyclical call-graph was found but no other reason
10469 for a stub was detected. If called from the top level, a return of
10470 2 means the same as a return of 0. */
10471
10472 static int
10473 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
10474 {
10475 int ret;
10476
10477 /* Mark this section as checked. */
10478 isec->call_check_done = 1;
10479
10480 /* We know none of our code bearing sections will need toc stubs. */
10481 if ((isec->flags & SEC_LINKER_CREATED) != 0)
10482 return 0;
10483
10484 if (isec->size == 0)
10485 return 0;
10486
10487 if (isec->output_section == NULL)
10488 return 0;
10489
10490 ret = 0;
10491 if (isec->reloc_count != 0)
10492 {
10493 Elf_Internal_Rela *relstart, *rel;
10494 Elf_Internal_Sym *local_syms;
10495 struct ppc_link_hash_table *htab;
10496
10497 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10498 info->keep_memory);
10499 if (relstart == NULL)
10500 return -1;
10501
10502 /* Look for branches to outside of this section. */
10503 local_syms = NULL;
10504 htab = ppc_hash_table (info);
10505 if (htab == NULL)
10506 return -1;
10507
10508 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
10509 {
10510 enum elf_ppc64_reloc_type r_type;
10511 unsigned long r_symndx;
10512 struct elf_link_hash_entry *h;
10513 struct ppc_link_hash_entry *eh;
10514 Elf_Internal_Sym *sym;
10515 asection *sym_sec;
10516 struct _opd_sec_data *opd;
10517 bfd_vma sym_value;
10518 bfd_vma dest;
10519
10520 r_type = ELF64_R_TYPE (rel->r_info);
10521 if (r_type != R_PPC64_REL24
10522 && r_type != R_PPC64_REL14
10523 && r_type != R_PPC64_REL14_BRTAKEN
10524 && r_type != R_PPC64_REL14_BRNTAKEN)
10525 continue;
10526
10527 r_symndx = ELF64_R_SYM (rel->r_info);
10528 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10529 isec->owner))
10530 {
10531 ret = -1;
10532 break;
10533 }
10534
10535 /* Calls to dynamic lib functions go through a plt call stub
10536 that uses r2. */
10537 eh = (struct ppc_link_hash_entry *) h;
10538 if (eh != NULL
10539 && (eh->elf.plt.plist != NULL
10540 || (eh->oh != NULL
10541 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10542 {
10543 ret = 1;
10544 break;
10545 }
10546
10547 if (sym_sec == NULL)
10548 /* Ignore other undefined symbols. */
10549 continue;
10550
10551 /* Assume branches to other sections not included in the
10552 link need stubs too, to cover -R and absolute syms. */
10553 if (sym_sec->output_section == NULL)
10554 {
10555 ret = 1;
10556 break;
10557 }
10558
10559 if (h == NULL)
10560 sym_value = sym->st_value;
10561 else
10562 {
10563 if (h->root.type != bfd_link_hash_defined
10564 && h->root.type != bfd_link_hash_defweak)
10565 abort ();
10566 sym_value = h->root.u.def.value;
10567 }
10568 sym_value += rel->r_addend;
10569
10570 /* If this branch reloc uses an opd sym, find the code section. */
10571 opd = get_opd_info (sym_sec);
10572 if (opd != NULL)
10573 {
10574 if (h == NULL && opd->adjust != NULL)
10575 {
10576 long adjust;
10577
10578 adjust = opd->adjust[sym->st_value / 8];
10579 if (adjust == -1)
10580 /* Assume deleted functions won't ever be called. */
10581 continue;
10582 sym_value += adjust;
10583 }
10584
10585 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10586 if (dest == (bfd_vma) -1)
10587 continue;
10588 }
10589 else
10590 dest = (sym_value
10591 + sym_sec->output_offset
10592 + sym_sec->output_section->vma);
10593
10594 /* Ignore branch to self. */
10595 if (sym_sec == isec)
10596 continue;
10597
10598 /* If the called function uses the toc, we need a stub. */
10599 if (sym_sec->has_toc_reloc
10600 || sym_sec->makes_toc_func_call)
10601 {
10602 ret = 1;
10603 break;
10604 }
10605
10606 /* Assume any branch that needs a long branch stub might in fact
10607 need a plt_branch stub. A plt_branch stub uses r2. */
10608 else if (dest - (isec->output_offset
10609 + isec->output_section->vma
10610 + rel->r_offset) + (1 << 25) >= (2 << 25))
10611 {
10612 ret = 1;
10613 break;
10614 }
10615
10616 /* If calling back to a section in the process of being
10617 tested, we can't say for sure that no toc adjusting stubs
10618 are needed, so don't return zero. */
10619 else if (sym_sec->call_check_in_progress)
10620 ret = 2;
10621
10622 /* Branches to another section that itself doesn't have any TOC
10623 references are OK. Recursively call ourselves to check. */
10624 else if (!sym_sec->call_check_done)
10625 {
10626 int recur;
10627
10628 /* Mark current section as indeterminate, so that other
10629 sections that call back to current won't be marked as
10630 known. */
10631 isec->call_check_in_progress = 1;
10632 recur = toc_adjusting_stub_needed (info, sym_sec);
10633 isec->call_check_in_progress = 0;
10634
10635 if (recur != 0)
10636 {
10637 ret = recur;
10638 if (recur != 2)
10639 break;
10640 }
10641 }
10642 }
10643
10644 if (local_syms != NULL
10645 && (elf_symtab_hdr (isec->owner).contents
10646 != (unsigned char *) local_syms))
10647 free (local_syms);
10648 if (elf_section_data (isec)->relocs != relstart)
10649 free (relstart);
10650 }
10651
10652 if ((ret & 1) == 0
10653 && isec->map_head.s != NULL
10654 && (strcmp (isec->output_section->name, ".init") == 0
10655 || strcmp (isec->output_section->name, ".fini") == 0))
10656 {
10657 if (isec->map_head.s->has_toc_reloc
10658 || isec->map_head.s->makes_toc_func_call)
10659 ret = 1;
10660 else if (!isec->map_head.s->call_check_done)
10661 {
10662 int recur;
10663 isec->call_check_in_progress = 1;
10664 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10665 isec->call_check_in_progress = 0;
10666 if (recur != 0)
10667 ret = recur;
10668 }
10669 }
10670
10671 if (ret == 1)
10672 isec->makes_toc_func_call = 1;
10673
10674 return ret;
10675 }
10676
10677 /* The linker repeatedly calls this function for each input section,
10678 in the order that input sections are linked into output sections.
10679 Build lists of input sections to determine groupings between which
10680 we may insert linker stubs. */
10681
10682 bfd_boolean
10683 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
10684 {
10685 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10686
10687 if (htab == NULL)
10688 return FALSE;
10689
10690 if ((isec->output_section->flags & SEC_CODE) != 0
10691 && isec->output_section->index <= htab->top_index)
10692 {
10693 asection **list = htab->input_list + isec->output_section->index;
10694 /* Steal the link_sec pointer for our list. */
10695 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
10696 /* This happens to make the list in reverse order,
10697 which is what we want. */
10698 PREV_SEC (isec) = *list;
10699 *list = isec;
10700 }
10701
10702 if (htab->multi_toc_needed)
10703 {
10704 /* If a code section has a function that uses the TOC then we need
10705 to use the right TOC (obviously). Also, make sure that .opd gets
10706 the correct TOC value for R_PPC64_TOC relocs that don't have or
10707 can't find their function symbol (shouldn't ever happen now).
10708 Also specially treat .fixup for the linux kernel. .fixup
10709 contains branches, but only back to the function that hit an
10710 exception. */
10711 if (isec->has_toc_reloc
10712 || (isec->flags & SEC_CODE) == 0
10713 || strcmp (isec->name, ".fixup") == 0)
10714 {
10715 if (elf_gp (isec->owner) != 0)
10716 htab->toc_curr = elf_gp (isec->owner);
10717 }
10718 else
10719 {
10720 if (!isec->call_check_done
10721 && toc_adjusting_stub_needed (info, isec) < 0)
10722 return FALSE;
10723 /* If we make a local call from this section, ie. a branch
10724 without a following nop, then we have no place to put a
10725 toc restoring insn. We must use the same toc group as
10726 the callee.
10727 Testing makes_toc_func_call actually tests for *any*
10728 calls to functions that need a good toc pointer. A more
10729 precise test would be better, as this one will set
10730 incorrect values for pasted .init/.fini fragments.
10731 (Fixed later in check_pasted_section.) */
10732 if (isec->makes_toc_func_call
10733 && elf_gp (isec->owner) != 0)
10734 htab->toc_curr = elf_gp (isec->owner);
10735 }
10736 }
10737
10738 /* Functions that don't use the TOC can belong in any TOC group.
10739 Use the last TOC base. */
10740 htab->stub_group[isec->id].toc_off = htab->toc_curr;
10741 return TRUE;
10742 }
10743
10744 /* Check that all .init and .fini sections use the same toc, if they
10745 have toc relocs. */
10746
10747 static bfd_boolean
10748 check_pasted_section (struct bfd_link_info *info, const char *name)
10749 {
10750 asection *o = bfd_get_section_by_name (info->output_bfd, name);
10751
10752 if (o != NULL)
10753 {
10754 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10755 bfd_vma toc_off = 0;
10756 asection *i;
10757
10758 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10759 if (i->has_toc_reloc)
10760 {
10761 if (toc_off == 0)
10762 toc_off = htab->stub_group[i->id].toc_off;
10763 else if (toc_off != htab->stub_group[i->id].toc_off)
10764 return FALSE;
10765 }
10766
10767 if (toc_off == 0)
10768 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10769 if (i->makes_toc_func_call)
10770 {
10771 toc_off = htab->stub_group[i->id].toc_off;
10772 break;
10773 }
10774
10775 /* Make sure the whole pasted function uses the same toc offset. */
10776 if (toc_off != 0)
10777 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10778 htab->stub_group[i->id].toc_off = toc_off;
10779 }
10780 return TRUE;
10781 }
10782
10783 bfd_boolean
10784 ppc64_elf_check_init_fini (struct bfd_link_info *info)
10785 {
10786 return (check_pasted_section (info, ".init")
10787 & check_pasted_section (info, ".fini"));
10788 }
10789
10790 /* See whether we can group stub sections together. Grouping stub
10791 sections may result in fewer stubs. More importantly, we need to
10792 put all .init* and .fini* stubs at the beginning of the .init or
10793 .fini output sections respectively, because glibc splits the
10794 _init and _fini functions into multiple parts. Putting a stub in
10795 the middle of a function is not a good idea. */
10796
10797 static void
10798 group_sections (struct ppc_link_hash_table *htab,
10799 bfd_size_type stub_group_size,
10800 bfd_boolean stubs_always_before_branch)
10801 {
10802 asection **list;
10803 bfd_size_type stub14_group_size;
10804 bfd_boolean suppress_size_errors;
10805
10806 suppress_size_errors = FALSE;
10807 stub14_group_size = stub_group_size;
10808 if (stub_group_size == 1)
10809 {
10810 /* Default values. */
10811 if (stubs_always_before_branch)
10812 {
10813 stub_group_size = 0x1e00000;
10814 stub14_group_size = 0x7800;
10815 }
10816 else
10817 {
10818 stub_group_size = 0x1c00000;
10819 stub14_group_size = 0x7000;
10820 }
10821 suppress_size_errors = TRUE;
10822 }
10823
10824 list = htab->input_list + htab->top_index;
10825 do
10826 {
10827 asection *tail = *list;
10828 while (tail != NULL)
10829 {
10830 asection *curr;
10831 asection *prev;
10832 bfd_size_type total;
10833 bfd_boolean big_sec;
10834 bfd_vma curr_toc;
10835
10836 curr = tail;
10837 total = tail->size;
10838 big_sec = total > (ppc64_elf_section_data (tail) != NULL
10839 && ppc64_elf_section_data (tail)->has_14bit_branch
10840 ? stub14_group_size : stub_group_size);
10841 if (big_sec && !suppress_size_errors)
10842 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
10843 tail->owner, tail);
10844 curr_toc = htab->stub_group[tail->id].toc_off;
10845
10846 while ((prev = PREV_SEC (curr)) != NULL
10847 && ((total += curr->output_offset - prev->output_offset)
10848 < (ppc64_elf_section_data (prev) != NULL
10849 && ppc64_elf_section_data (prev)->has_14bit_branch
10850 ? stub14_group_size : stub_group_size))
10851 && htab->stub_group[prev->id].toc_off == curr_toc)
10852 curr = prev;
10853
10854 /* OK, the size from the start of CURR to the end is less
10855 than stub_group_size and thus can be handled by one stub
10856 section. (or the tail section is itself larger than
10857 stub_group_size, in which case we may be toast.) We
10858 should really be keeping track of the total size of stubs
10859 added here, as stubs contribute to the final output
10860 section size. That's a little tricky, and this way will
10861 only break if stubs added make the total size more than
10862 2^25, ie. for the default stub_group_size, if stubs total
10863 more than 2097152 bytes, or nearly 75000 plt call stubs. */
10864 do
10865 {
10866 prev = PREV_SEC (tail);
10867 /* Set up this stub group. */
10868 htab->stub_group[tail->id].link_sec = curr;
10869 }
10870 while (tail != curr && (tail = prev) != NULL);
10871
10872 /* But wait, there's more! Input sections up to stub_group_size
10873 bytes before the stub section can be handled by it too.
10874 Don't do this if we have a really large section after the
10875 stubs, as adding more stubs increases the chance that
10876 branches may not reach into the stub section. */
10877 if (!stubs_always_before_branch && !big_sec)
10878 {
10879 total = 0;
10880 while (prev != NULL
10881 && ((total += tail->output_offset - prev->output_offset)
10882 < (ppc64_elf_section_data (prev) != NULL
10883 && ppc64_elf_section_data (prev)->has_14bit_branch
10884 ? stub14_group_size : stub_group_size))
10885 && htab->stub_group[prev->id].toc_off == curr_toc)
10886 {
10887 tail = prev;
10888 prev = PREV_SEC (tail);
10889 htab->stub_group[tail->id].link_sec = curr;
10890 }
10891 }
10892 tail = prev;
10893 }
10894 }
10895 while (list-- != htab->input_list);
10896 free (htab->input_list);
10897 #undef PREV_SEC
10898 }
10899
10900 static const unsigned char glink_eh_frame_cie[] =
10901 {
10902 0, 0, 0, 16, /* length. */
10903 0, 0, 0, 0, /* id. */
10904 1, /* CIE version. */
10905 'z', 'R', 0, /* Augmentation string. */
10906 4, /* Code alignment. */
10907 0x78, /* Data alignment. */
10908 65, /* RA reg. */
10909 1, /* Augmentation size. */
10910 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
10911 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
10912 };
10913
10914 /* Stripping output sections is normally done before dynamic section
10915 symbols have been allocated. This function is called later, and
10916 handles cases like htab->brlt which is mapped to its own output
10917 section. */
10918
10919 static void
10920 maybe_strip_output (struct bfd_link_info *info, asection *isec)
10921 {
10922 if (isec->size == 0
10923 && isec->output_section->size == 0
10924 && !bfd_section_removed_from_list (info->output_bfd,
10925 isec->output_section)
10926 && elf_section_data (isec->output_section)->dynindx == 0)
10927 {
10928 isec->output_section->flags |= SEC_EXCLUDE;
10929 bfd_section_list_remove (info->output_bfd, isec->output_section);
10930 info->output_bfd->section_count--;
10931 }
10932 }
10933
10934 /* Determine and set the size of the stub section for a final link.
10935
10936 The basic idea here is to examine all the relocations looking for
10937 PC-relative calls to a target that is unreachable with a "bl"
10938 instruction. */
10939
10940 bfd_boolean
10941 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
10942 bfd_boolean plt_static_chain)
10943 {
10944 bfd_size_type stub_group_size;
10945 bfd_boolean stubs_always_before_branch;
10946 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10947
10948 if (htab == NULL)
10949 return FALSE;
10950
10951 htab->plt_static_chain = plt_static_chain;
10952 stubs_always_before_branch = group_size < 0;
10953 if (group_size < 0)
10954 stub_group_size = -group_size;
10955 else
10956 stub_group_size = group_size;
10957
10958 group_sections (htab, stub_group_size, stubs_always_before_branch);
10959
10960 while (1)
10961 {
10962 bfd *input_bfd;
10963 unsigned int bfd_indx;
10964 asection *stub_sec;
10965
10966 htab->stub_iteration += 1;
10967
10968 for (input_bfd = info->input_bfds, bfd_indx = 0;
10969 input_bfd != NULL;
10970 input_bfd = input_bfd->link_next, bfd_indx++)
10971 {
10972 Elf_Internal_Shdr *symtab_hdr;
10973 asection *section;
10974 Elf_Internal_Sym *local_syms = NULL;
10975
10976 if (!is_ppc64_elf (input_bfd))
10977 continue;
10978
10979 /* We'll need the symbol table in a second. */
10980 symtab_hdr = &elf_symtab_hdr (input_bfd);
10981 if (symtab_hdr->sh_info == 0)
10982 continue;
10983
10984 /* Walk over each section attached to the input bfd. */
10985 for (section = input_bfd->sections;
10986 section != NULL;
10987 section = section->next)
10988 {
10989 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
10990
10991 /* If there aren't any relocs, then there's nothing more
10992 to do. */
10993 if ((section->flags & SEC_RELOC) == 0
10994 || (section->flags & SEC_ALLOC) == 0
10995 || (section->flags & SEC_LOAD) == 0
10996 || (section->flags & SEC_CODE) == 0
10997 || section->reloc_count == 0)
10998 continue;
10999
11000 /* If this section is a link-once section that will be
11001 discarded, then don't create any stubs. */
11002 if (section->output_section == NULL
11003 || section->output_section->owner != info->output_bfd)
11004 continue;
11005
11006 /* Get the relocs. */
11007 internal_relocs
11008 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
11009 info->keep_memory);
11010 if (internal_relocs == NULL)
11011 goto error_ret_free_local;
11012
11013 /* Now examine each relocation. */
11014 irela = internal_relocs;
11015 irelaend = irela + section->reloc_count;
11016 for (; irela < irelaend; irela++)
11017 {
11018 enum elf_ppc64_reloc_type r_type;
11019 unsigned int r_indx;
11020 enum ppc_stub_type stub_type;
11021 struct ppc_stub_hash_entry *stub_entry;
11022 asection *sym_sec, *code_sec;
11023 bfd_vma sym_value, code_value;
11024 bfd_vma destination;
11025 bfd_boolean ok_dest;
11026 struct ppc_link_hash_entry *hash;
11027 struct ppc_link_hash_entry *fdh;
11028 struct elf_link_hash_entry *h;
11029 Elf_Internal_Sym *sym;
11030 char *stub_name;
11031 const asection *id_sec;
11032 struct _opd_sec_data *opd;
11033 struct plt_entry *plt_ent;
11034
11035 r_type = ELF64_R_TYPE (irela->r_info);
11036 r_indx = ELF64_R_SYM (irela->r_info);
11037
11038 if (r_type >= R_PPC64_max)
11039 {
11040 bfd_set_error (bfd_error_bad_value);
11041 goto error_ret_free_internal;
11042 }
11043
11044 /* Only look for stubs on branch instructions. */
11045 if (r_type != R_PPC64_REL24
11046 && r_type != R_PPC64_REL14
11047 && r_type != R_PPC64_REL14_BRTAKEN
11048 && r_type != R_PPC64_REL14_BRNTAKEN)
11049 continue;
11050
11051 /* Now determine the call target, its name, value,
11052 section. */
11053 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11054 r_indx, input_bfd))
11055 goto error_ret_free_internal;
11056 hash = (struct ppc_link_hash_entry *) h;
11057
11058 ok_dest = FALSE;
11059 fdh = NULL;
11060 sym_value = 0;
11061 if (hash == NULL)
11062 {
11063 sym_value = sym->st_value;
11064 ok_dest = TRUE;
11065 }
11066 else if (hash->elf.root.type == bfd_link_hash_defined
11067 || hash->elf.root.type == bfd_link_hash_defweak)
11068 {
11069 sym_value = hash->elf.root.u.def.value;
11070 if (sym_sec->output_section != NULL)
11071 ok_dest = TRUE;
11072 }
11073 else if (hash->elf.root.type == bfd_link_hash_undefweak
11074 || hash->elf.root.type == bfd_link_hash_undefined)
11075 {
11076 /* Recognise an old ABI func code entry sym, and
11077 use the func descriptor sym instead if it is
11078 defined. */
11079 if (hash->elf.root.root.string[0] == '.'
11080 && (fdh = lookup_fdh (hash, htab)) != NULL)
11081 {
11082 if (fdh->elf.root.type == bfd_link_hash_defined
11083 || fdh->elf.root.type == bfd_link_hash_defweak)
11084 {
11085 sym_sec = fdh->elf.root.u.def.section;
11086 sym_value = fdh->elf.root.u.def.value;
11087 if (sym_sec->output_section != NULL)
11088 ok_dest = TRUE;
11089 }
11090 else
11091 fdh = NULL;
11092 }
11093 }
11094 else
11095 {
11096 bfd_set_error (bfd_error_bad_value);
11097 goto error_ret_free_internal;
11098 }
11099
11100 destination = 0;
11101 if (ok_dest)
11102 {
11103 sym_value += irela->r_addend;
11104 destination = (sym_value
11105 + sym_sec->output_offset
11106 + sym_sec->output_section->vma);
11107 }
11108
11109 code_sec = sym_sec;
11110 code_value = sym_value;
11111 opd = get_opd_info (sym_sec);
11112 if (opd != NULL)
11113 {
11114 bfd_vma dest;
11115
11116 if (hash == NULL && opd->adjust != NULL)
11117 {
11118 long adjust = opd->adjust[sym_value / 8];
11119 if (adjust == -1)
11120 continue;
11121 code_value += adjust;
11122 sym_value += adjust;
11123 }
11124 dest = opd_entry_value (sym_sec, sym_value,
11125 &code_sec, &code_value);
11126 if (dest != (bfd_vma) -1)
11127 {
11128 destination = dest;
11129 if (fdh != NULL)
11130 {
11131 /* Fixup old ABI sym to point at code
11132 entry. */
11133 hash->elf.root.type = bfd_link_hash_defweak;
11134 hash->elf.root.u.def.section = code_sec;
11135 hash->elf.root.u.def.value = code_value;
11136 }
11137 }
11138 }
11139
11140 /* Determine what (if any) linker stub is needed. */
11141 plt_ent = NULL;
11142 stub_type = ppc_type_of_stub (section, irela, &hash,
11143 &plt_ent, destination);
11144
11145 if (stub_type != ppc_stub_plt_call)
11146 {
11147 /* Check whether we need a TOC adjusting stub.
11148 Since the linker pastes together pieces from
11149 different object files when creating the
11150 _init and _fini functions, it may be that a
11151 call to what looks like a local sym is in
11152 fact a call needing a TOC adjustment. */
11153 if (code_sec != NULL
11154 && code_sec->output_section != NULL
11155 && (htab->stub_group[code_sec->id].toc_off
11156 != htab->stub_group[section->id].toc_off)
11157 && (code_sec->has_toc_reloc
11158 || code_sec->makes_toc_func_call))
11159 stub_type = ppc_stub_long_branch_r2off;
11160 }
11161
11162 if (stub_type == ppc_stub_none)
11163 continue;
11164
11165 /* __tls_get_addr calls might be eliminated. */
11166 if (stub_type != ppc_stub_plt_call
11167 && hash != NULL
11168 && (hash == htab->tls_get_addr
11169 || hash == htab->tls_get_addr_fd)
11170 && section->has_tls_reloc
11171 && irela != internal_relocs)
11172 {
11173 /* Get tls info. */
11174 unsigned char *tls_mask;
11175
11176 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
11177 irela - 1, input_bfd))
11178 goto error_ret_free_internal;
11179 if (*tls_mask != 0)
11180 continue;
11181 }
11182
11183 /* Support for grouping stub sections. */
11184 id_sec = htab->stub_group[section->id].link_sec;
11185
11186 /* Get the name of this stub. */
11187 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11188 if (!stub_name)
11189 goto error_ret_free_internal;
11190
11191 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
11192 stub_name, FALSE, FALSE);
11193 if (stub_entry != NULL)
11194 {
11195 /* The proper stub has already been created. */
11196 free (stub_name);
11197 continue;
11198 }
11199
11200 stub_entry = ppc_add_stub (stub_name, section, info);
11201 if (stub_entry == NULL)
11202 {
11203 free (stub_name);
11204 error_ret_free_internal:
11205 if (elf_section_data (section)->relocs == NULL)
11206 free (internal_relocs);
11207 error_ret_free_local:
11208 if (local_syms != NULL
11209 && (symtab_hdr->contents
11210 != (unsigned char *) local_syms))
11211 free (local_syms);
11212 return FALSE;
11213 }
11214
11215 stub_entry->stub_type = stub_type;
11216 if (stub_type != ppc_stub_plt_call)
11217 {
11218 stub_entry->target_value = code_value;
11219 stub_entry->target_section = code_sec;
11220 }
11221 else
11222 {
11223 stub_entry->target_value = sym_value;
11224 stub_entry->target_section = sym_sec;
11225 }
11226 stub_entry->h = hash;
11227 stub_entry->plt_ent = plt_ent;
11228 stub_entry->addend = irela->r_addend;
11229
11230 if (stub_entry->h != NULL)
11231 htab->stub_globals += 1;
11232 }
11233
11234 /* We're done with the internal relocs, free them. */
11235 if (elf_section_data (section)->relocs != internal_relocs)
11236 free (internal_relocs);
11237 }
11238
11239 if (local_syms != NULL
11240 && symtab_hdr->contents != (unsigned char *) local_syms)
11241 {
11242 if (!info->keep_memory)
11243 free (local_syms);
11244 else
11245 symtab_hdr->contents = (unsigned char *) local_syms;
11246 }
11247 }
11248
11249 /* We may have added some stubs. Find out the new size of the
11250 stub sections. */
11251 for (stub_sec = htab->stub_bfd->sections;
11252 stub_sec != NULL;
11253 stub_sec = stub_sec->next)
11254 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11255 {
11256 stub_sec->rawsize = stub_sec->size;
11257 stub_sec->size = 0;
11258 stub_sec->reloc_count = 0;
11259 stub_sec->flags &= ~SEC_RELOC;
11260 }
11261
11262 htab->brlt->size = 0;
11263 htab->brlt->reloc_count = 0;
11264 htab->brlt->flags &= ~SEC_RELOC;
11265 if (htab->relbrlt != NULL)
11266 htab->relbrlt->size = 0;
11267
11268 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
11269
11270 if (info->emitrelocations
11271 && htab->glink != NULL && htab->glink->size != 0)
11272 {
11273 htab->glink->reloc_count = 1;
11274 htab->glink->flags |= SEC_RELOC;
11275 }
11276
11277 if (htab->glink_eh_frame != NULL
11278 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
11279 && (htab->glink_eh_frame->flags & SEC_EXCLUDE) == 0)
11280 {
11281 bfd_size_type size = 0;
11282
11283 for (stub_sec = htab->stub_bfd->sections;
11284 stub_sec != NULL;
11285 stub_sec = stub_sec->next)
11286 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11287 size += 20;
11288 if (htab->glink != NULL && htab->glink->size != 0)
11289 size += 24;
11290 if (size != 0)
11291 size += sizeof (glink_eh_frame_cie);
11292 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
11293 htab->glink_eh_frame->size = size;
11294 }
11295
11296 for (stub_sec = htab->stub_bfd->sections;
11297 stub_sec != NULL;
11298 stub_sec = stub_sec->next)
11299 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11300 && stub_sec->rawsize != stub_sec->size)
11301 break;
11302
11303 /* Exit from this loop when no stubs have been added, and no stubs
11304 have changed size. */
11305 if (stub_sec == NULL
11306 && (htab->glink_eh_frame == NULL
11307 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
11308 break;
11309
11310 /* Ask the linker to do its stuff. */
11311 (*htab->layout_sections_again) ();
11312 }
11313
11314 maybe_strip_output (info, htab->brlt);
11315 if (htab->glink_eh_frame != NULL)
11316 maybe_strip_output (info, htab->glink_eh_frame);
11317
11318 return TRUE;
11319 }
11320
11321 /* Called after we have determined section placement. If sections
11322 move, we'll be called again. Provide a value for TOCstart. */
11323
11324 bfd_vma
11325 ppc64_elf_toc (bfd *obfd)
11326 {
11327 asection *s;
11328 bfd_vma TOCstart;
11329
11330 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11331 order. The TOC starts where the first of these sections starts. */
11332 s = bfd_get_section_by_name (obfd, ".got");
11333 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11334 s = bfd_get_section_by_name (obfd, ".toc");
11335 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11336 s = bfd_get_section_by_name (obfd, ".tocbss");
11337 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11338 s = bfd_get_section_by_name (obfd, ".plt");
11339 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11340 {
11341 /* This may happen for
11342 o references to TOC base (SYM@toc / TOC[tc0]) without a
11343 .toc directive
11344 o bad linker script
11345 o --gc-sections and empty TOC sections
11346
11347 FIXME: Warn user? */
11348
11349 /* Look for a likely section. We probably won't even be
11350 using TOCstart. */
11351 for (s = obfd->sections; s != NULL; s = s->next)
11352 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11353 | SEC_EXCLUDE))
11354 == (SEC_ALLOC | SEC_SMALL_DATA))
11355 break;
11356 if (s == NULL)
11357 for (s = obfd->sections; s != NULL; s = s->next)
11358 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
11359 == (SEC_ALLOC | SEC_SMALL_DATA))
11360 break;
11361 if (s == NULL)
11362 for (s = obfd->sections; s != NULL; s = s->next)
11363 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11364 == SEC_ALLOC)
11365 break;
11366 if (s == NULL)
11367 for (s = obfd->sections; s != NULL; s = s->next)
11368 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
11369 break;
11370 }
11371
11372 TOCstart = 0;
11373 if (s != NULL)
11374 TOCstart = s->output_section->vma + s->output_offset;
11375
11376 return TOCstart;
11377 }
11378
11379 /* Build all the stubs associated with the current output file.
11380 The stubs are kept in a hash table attached to the main linker
11381 hash table. This function is called via gldelf64ppc_finish. */
11382
11383 bfd_boolean
11384 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11385 struct bfd_link_info *info,
11386 char **stats)
11387 {
11388 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11389 asection *stub_sec;
11390 bfd_byte *p;
11391 int stub_sec_count = 0;
11392
11393 if (htab == NULL)
11394 return FALSE;
11395
11396 htab->emit_stub_syms = emit_stub_syms;
11397
11398 /* Allocate memory to hold the linker stubs. */
11399 for (stub_sec = htab->stub_bfd->sections;
11400 stub_sec != NULL;
11401 stub_sec = stub_sec->next)
11402 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11403 && stub_sec->size != 0)
11404 {
11405 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11406 if (stub_sec->contents == NULL)
11407 return FALSE;
11408 /* We want to check that built size is the same as calculated
11409 size. rawsize is a convenient location to use. */
11410 stub_sec->rawsize = stub_sec->size;
11411 stub_sec->size = 0;
11412 }
11413
11414 if (htab->glink != NULL && htab->glink->size != 0)
11415 {
11416 unsigned int indx;
11417 bfd_vma plt0;
11418
11419 /* Build the .glink plt call stub. */
11420 if (htab->emit_stub_syms)
11421 {
11422 struct elf_link_hash_entry *h;
11423 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11424 TRUE, FALSE, FALSE);
11425 if (h == NULL)
11426 return FALSE;
11427 if (h->root.type == bfd_link_hash_new)
11428 {
11429 h->root.type = bfd_link_hash_defined;
11430 h->root.u.def.section = htab->glink;
11431 h->root.u.def.value = 8;
11432 h->ref_regular = 1;
11433 h->def_regular = 1;
11434 h->ref_regular_nonweak = 1;
11435 h->forced_local = 1;
11436 h->non_elf = 0;
11437 }
11438 }
11439 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11440 if (info->emitrelocations)
11441 {
11442 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11443 if (r == NULL)
11444 return FALSE;
11445 r->r_offset = (htab->glink->output_offset
11446 + htab->glink->output_section->vma);
11447 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11448 r->r_addend = plt0;
11449 }
11450 p = htab->glink->contents;
11451 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
11452 bfd_put_64 (htab->glink->owner, plt0, p);
11453 p += 8;
11454 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
11455 p += 4;
11456 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
11457 p += 4;
11458 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
11459 p += 4;
11460 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
11461 p += 4;
11462 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
11463 p += 4;
11464 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
11465 p += 4;
11466 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
11467 p += 4;
11468 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
11469 p += 4;
11470 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
11471 p += 4;
11472 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
11473 p += 4;
11474 bfd_put_32 (htab->glink->owner, BCTR, p);
11475 p += 4;
11476 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11477 {
11478 bfd_put_32 (htab->glink->owner, NOP, p);
11479 p += 4;
11480 }
11481
11482 /* Build the .glink lazy link call stubs. */
11483 indx = 0;
11484 while (p < htab->glink->contents + htab->glink->size)
11485 {
11486 if (indx < 0x8000)
11487 {
11488 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
11489 p += 4;
11490 }
11491 else
11492 {
11493 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
11494 p += 4;
11495 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
11496 p += 4;
11497 }
11498 bfd_put_32 (htab->glink->owner,
11499 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
11500 indx++;
11501 p += 4;
11502 }
11503 htab->glink->rawsize = p - htab->glink->contents;
11504 }
11505
11506 if (htab->brlt->size != 0)
11507 {
11508 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
11509 htab->brlt->size);
11510 if (htab->brlt->contents == NULL)
11511 return FALSE;
11512 }
11513 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
11514 {
11515 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
11516 htab->relbrlt->size);
11517 if (htab->relbrlt->contents == NULL)
11518 return FALSE;
11519 }
11520
11521 if (htab->glink_eh_frame != NULL
11522 && htab->glink_eh_frame->size != 0)
11523 {
11524 bfd_vma val;
11525
11526 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
11527 if (p == NULL)
11528 return FALSE;
11529 htab->glink_eh_frame->contents = p;
11530
11531 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
11532
11533 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
11534 /* CIE length (rewrite in case little-endian). */
11535 bfd_put_32 (htab->elf.dynobj, sizeof (glink_eh_frame_cie) - 4, p);
11536 p += sizeof (glink_eh_frame_cie);
11537
11538 for (stub_sec = htab->stub_bfd->sections;
11539 stub_sec != NULL;
11540 stub_sec = stub_sec->next)
11541 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11542 {
11543 /* FDE length. */
11544 bfd_put_32 (htab->elf.dynobj, 16, p);
11545 p += 4;
11546 /* CIE pointer. */
11547 val = p - htab->glink_eh_frame->contents;
11548 bfd_put_32 (htab->elf.dynobj, val, p);
11549 p += 4;
11550 /* Offset to stub section. */
11551 val = (stub_sec->output_section->vma
11552 + stub_sec->output_offset);
11553 val -= (htab->glink_eh_frame->output_section->vma
11554 + htab->glink_eh_frame->output_offset);
11555 val -= p - htab->glink_eh_frame->contents;
11556 if (val + 0x80000000 > 0xffffffff)
11557 {
11558 info->callbacks->einfo
11559 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
11560 stub_sec->name);
11561 return FALSE;
11562 }
11563 bfd_put_32 (htab->elf.dynobj, val, p);
11564 p += 4;
11565 /* stub section size. */
11566 bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
11567 p += 4;
11568 /* Augmentation. */
11569 p += 1;
11570 /* Pad. */
11571 p += 3;
11572 }
11573 if (htab->glink != NULL && htab->glink->size != 0)
11574 {
11575 /* FDE length. */
11576 bfd_put_32 (htab->elf.dynobj, 20, p);
11577 p += 4;
11578 /* CIE pointer. */
11579 val = p - htab->glink_eh_frame->contents;
11580 bfd_put_32 (htab->elf.dynobj, val, p);
11581 p += 4;
11582 /* Offset to .glink. */
11583 val = (htab->glink->output_section->vma
11584 + htab->glink->output_offset
11585 + 8);
11586 val -= (htab->glink_eh_frame->output_section->vma
11587 + htab->glink_eh_frame->output_offset);
11588 val -= p - htab->glink_eh_frame->contents;
11589 if (val + 0x80000000 > 0xffffffff)
11590 {
11591 info->callbacks->einfo
11592 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
11593 htab->glink->name);
11594 return FALSE;
11595 }
11596 bfd_put_32 (htab->elf.dynobj, val, p);
11597 p += 4;
11598 /* .glink size. */
11599 bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
11600 p += 4;
11601 /* Augmentation. */
11602 p += 1;
11603
11604 *p++ = DW_CFA_advance_loc + 1;
11605 *p++ = DW_CFA_register;
11606 *p++ = 65;
11607 *p++ = 12;
11608 *p++ = DW_CFA_advance_loc + 4;
11609 *p++ = DW_CFA_restore_extended;
11610 *p++ = 65;
11611 }
11612 htab->glink_eh_frame->size = p - htab->glink_eh_frame->contents;
11613 }
11614
11615 /* Build the stubs as directed by the stub hash table. */
11616 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
11617
11618 if (htab->relbrlt != NULL)
11619 htab->relbrlt->reloc_count = 0;
11620
11621 for (stub_sec = htab->stub_bfd->sections;
11622 stub_sec != NULL;
11623 stub_sec = stub_sec->next)
11624 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11625 {
11626 stub_sec_count += 1;
11627 if (stub_sec->rawsize != stub_sec->size)
11628 break;
11629 }
11630
11631 if (stub_sec != NULL
11632 || htab->glink->rawsize != htab->glink->size
11633 || (htab->glink_eh_frame != NULL
11634 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
11635 {
11636 htab->stub_error = TRUE;
11637 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
11638 }
11639
11640 if (htab->stub_error)
11641 return FALSE;
11642
11643 if (stats != NULL)
11644 {
11645 *stats = bfd_malloc (500);
11646 if (*stats == NULL)
11647 return FALSE;
11648
11649 sprintf (*stats, _("linker stubs in %u group%s\n"
11650 " branch %lu\n"
11651 " toc adjust %lu\n"
11652 " long branch %lu\n"
11653 " long toc adj %lu\n"
11654 " plt call %lu"),
11655 stub_sec_count,
11656 stub_sec_count == 1 ? "" : "s",
11657 htab->stub_count[ppc_stub_long_branch - 1],
11658 htab->stub_count[ppc_stub_long_branch_r2off - 1],
11659 htab->stub_count[ppc_stub_plt_branch - 1],
11660 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
11661 htab->stub_count[ppc_stub_plt_call - 1]);
11662 }
11663 return TRUE;
11664 }
11665
11666 /* This function undoes the changes made by add_symbol_adjust. */
11667
11668 static bfd_boolean
11669 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11670 {
11671 struct ppc_link_hash_entry *eh;
11672
11673 if (h->root.type == bfd_link_hash_indirect)
11674 return TRUE;
11675
11676 eh = (struct ppc_link_hash_entry *) h;
11677 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
11678 return TRUE;
11679
11680 eh->elf.root.type = bfd_link_hash_undefined;
11681 return TRUE;
11682 }
11683
11684 void
11685 ppc64_elf_restore_symbols (struct bfd_link_info *info)
11686 {
11687 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11688
11689 if (htab != NULL)
11690 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
11691 }
11692
11693 /* What to do when ld finds relocations against symbols defined in
11694 discarded sections. */
11695
11696 static unsigned int
11697 ppc64_elf_action_discarded (asection *sec)
11698 {
11699 if (strcmp (".opd", sec->name) == 0)
11700 return 0;
11701
11702 if (strcmp (".toc", sec->name) == 0)
11703 return 0;
11704
11705 if (strcmp (".toc1", sec->name) == 0)
11706 return 0;
11707
11708 return _bfd_elf_default_action_discarded (sec);
11709 }
11710
11711 /* The RELOCATE_SECTION function is called by the ELF backend linker
11712 to handle the relocations for a section.
11713
11714 The relocs are always passed as Rela structures; if the section
11715 actually uses Rel structures, the r_addend field will always be
11716 zero.
11717
11718 This function is responsible for adjust the section contents as
11719 necessary, and (if using Rela relocs and generating a
11720 relocatable output file) adjusting the reloc addend as
11721 necessary.
11722
11723 This function does not have to worry about setting the reloc
11724 address or the reloc symbol index.
11725
11726 LOCAL_SYMS is a pointer to the swapped in local symbols.
11727
11728 LOCAL_SECTIONS is an array giving the section in the input file
11729 corresponding to the st_shndx field of each local symbol.
11730
11731 The global hash table entry for the global symbols can be found
11732 via elf_sym_hashes (input_bfd).
11733
11734 When generating relocatable output, this function must handle
11735 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
11736 going to be the section symbol corresponding to the output
11737 section, which means that the addend must be adjusted
11738 accordingly. */
11739
11740 static bfd_boolean
11741 ppc64_elf_relocate_section (bfd *output_bfd,
11742 struct bfd_link_info *info,
11743 bfd *input_bfd,
11744 asection *input_section,
11745 bfd_byte *contents,
11746 Elf_Internal_Rela *relocs,
11747 Elf_Internal_Sym *local_syms,
11748 asection **local_sections)
11749 {
11750 struct ppc_link_hash_table *htab;
11751 Elf_Internal_Shdr *symtab_hdr;
11752 struct elf_link_hash_entry **sym_hashes;
11753 Elf_Internal_Rela *rel;
11754 Elf_Internal_Rela *relend;
11755 Elf_Internal_Rela outrel;
11756 bfd_byte *loc;
11757 struct got_entry **local_got_ents;
11758 bfd_vma TOCstart;
11759 bfd_boolean ret = TRUE;
11760 bfd_boolean is_opd;
11761 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
11762 bfd_boolean is_power4 = FALSE;
11763 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
11764
11765 /* Initialize howto table if needed. */
11766 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
11767 ppc_howto_init ();
11768
11769 htab = ppc_hash_table (info);
11770 if (htab == NULL)
11771 return FALSE;
11772
11773 /* Don't relocate stub sections. */
11774 if (input_section->owner == htab->stub_bfd)
11775 return TRUE;
11776
11777 BFD_ASSERT (is_ppc64_elf (input_bfd));
11778
11779 local_got_ents = elf_local_got_ents (input_bfd);
11780 TOCstart = elf_gp (output_bfd);
11781 symtab_hdr = &elf_symtab_hdr (input_bfd);
11782 sym_hashes = elf_sym_hashes (input_bfd);
11783 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
11784
11785 rel = relocs;
11786 relend = relocs + input_section->reloc_count;
11787 for (; rel < relend; rel++)
11788 {
11789 enum elf_ppc64_reloc_type r_type;
11790 bfd_vma addend, orig_addend;
11791 bfd_reloc_status_type r;
11792 Elf_Internal_Sym *sym;
11793 asection *sec;
11794 struct elf_link_hash_entry *h_elf;
11795 struct ppc_link_hash_entry *h;
11796 struct ppc_link_hash_entry *fdh;
11797 const char *sym_name;
11798 unsigned long r_symndx, toc_symndx;
11799 bfd_vma toc_addend;
11800 unsigned char tls_mask, tls_gd, tls_type;
11801 unsigned char sym_type;
11802 bfd_vma relocation;
11803 bfd_boolean unresolved_reloc;
11804 bfd_boolean warned;
11805 unsigned int insn;
11806 unsigned int mask;
11807 struct ppc_stub_hash_entry *stub_entry;
11808 bfd_vma max_br_offset;
11809 bfd_vma from;
11810
11811 r_type = ELF64_R_TYPE (rel->r_info);
11812 r_symndx = ELF64_R_SYM (rel->r_info);
11813
11814 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
11815 symbol of the previous ADDR64 reloc. The symbol gives us the
11816 proper TOC base to use. */
11817 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
11818 && rel != relocs
11819 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
11820 && is_opd)
11821 r_symndx = ELF64_R_SYM (rel[-1].r_info);
11822
11823 sym = NULL;
11824 sec = NULL;
11825 h_elf = NULL;
11826 sym_name = NULL;
11827 unresolved_reloc = FALSE;
11828 warned = FALSE;
11829 orig_addend = rel->r_addend;
11830
11831 if (r_symndx < symtab_hdr->sh_info)
11832 {
11833 /* It's a local symbol. */
11834 struct _opd_sec_data *opd;
11835
11836 sym = local_syms + r_symndx;
11837 sec = local_sections[r_symndx];
11838 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
11839 sym_type = ELF64_ST_TYPE (sym->st_info);
11840 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
11841 opd = get_opd_info (sec);
11842 if (opd != NULL && opd->adjust != NULL)
11843 {
11844 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
11845 if (adjust == -1)
11846 relocation = 0;
11847 else
11848 {
11849 /* If this is a relocation against the opd section sym
11850 and we have edited .opd, adjust the reloc addend so
11851 that ld -r and ld --emit-relocs output is correct.
11852 If it is a reloc against some other .opd symbol,
11853 then the symbol value will be adjusted later. */
11854 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11855 rel->r_addend += adjust;
11856 else
11857 relocation += adjust;
11858 }
11859 }
11860 }
11861 else
11862 {
11863 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
11864 r_symndx, symtab_hdr, sym_hashes,
11865 h_elf, sec, relocation,
11866 unresolved_reloc, warned);
11867 sym_name = h_elf->root.root.string;
11868 sym_type = h_elf->type;
11869 }
11870 h = (struct ppc_link_hash_entry *) h_elf;
11871
11872 if (sec != NULL && elf_discarded_section (sec))
11873 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
11874 rel, relend,
11875 ppc64_elf_howto_table[r_type],
11876 contents);
11877
11878 if (info->relocatable)
11879 continue;
11880
11881 /* TLS optimizations. Replace instruction sequences and relocs
11882 based on information we collected in tls_optimize. We edit
11883 RELOCS so that --emit-relocs will output something sensible
11884 for the final instruction stream. */
11885 tls_mask = 0;
11886 tls_gd = 0;
11887 toc_symndx = 0;
11888 if (h != NULL)
11889 tls_mask = h->tls_mask;
11890 else if (local_got_ents != NULL)
11891 {
11892 struct plt_entry **local_plt = (struct plt_entry **)
11893 (local_got_ents + symtab_hdr->sh_info);
11894 unsigned char *lgot_masks = (unsigned char *)
11895 (local_plt + symtab_hdr->sh_info);
11896 tls_mask = lgot_masks[r_symndx];
11897 }
11898 if (tls_mask == 0
11899 && (r_type == R_PPC64_TLS
11900 || r_type == R_PPC64_TLSGD
11901 || r_type == R_PPC64_TLSLD))
11902 {
11903 /* Check for toc tls entries. */
11904 unsigned char *toc_tls;
11905
11906 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11907 &local_syms, rel, input_bfd))
11908 return FALSE;
11909
11910 if (toc_tls)
11911 tls_mask = *toc_tls;
11912 }
11913
11914 /* Check that tls relocs are used with tls syms, and non-tls
11915 relocs are used with non-tls syms. */
11916 if (r_symndx != STN_UNDEF
11917 && r_type != R_PPC64_NONE
11918 && (h == NULL
11919 || h->elf.root.type == bfd_link_hash_defined
11920 || h->elf.root.type == bfd_link_hash_defweak)
11921 && (IS_PPC64_TLS_RELOC (r_type)
11922 != (sym_type == STT_TLS
11923 || (sym_type == STT_SECTION
11924 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
11925 {
11926 if (tls_mask != 0
11927 && (r_type == R_PPC64_TLS
11928 || r_type == R_PPC64_TLSGD
11929 || r_type == R_PPC64_TLSLD))
11930 /* R_PPC64_TLS is OK against a symbol in the TOC. */
11931 ;
11932 else
11933 info->callbacks->einfo
11934 (!IS_PPC64_TLS_RELOC (r_type)
11935 ? _("%P: %H: %s used with TLS symbol %s\n")
11936 : _("%P: %H: %s used with non-TLS symbol %s\n"),
11937 input_bfd, input_section, rel->r_offset,
11938 ppc64_elf_howto_table[r_type]->name,
11939 sym_name);
11940 }
11941
11942 /* Ensure reloc mapping code below stays sane. */
11943 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
11944 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
11945 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
11946 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
11947 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
11948 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
11949 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
11950 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
11951 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
11952 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
11953 abort ();
11954
11955 switch (r_type)
11956 {
11957 default:
11958 break;
11959
11960 case R_PPC64_LO_DS_OPT:
11961 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
11962 if ((insn & (0x3f << 26)) != 58u << 26)
11963 abort ();
11964 insn += (14u << 26) - (58u << 26);
11965 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
11966 r_type = R_PPC64_TOC16_LO;
11967 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11968 break;
11969
11970 case R_PPC64_TOC16:
11971 case R_PPC64_TOC16_LO:
11972 case R_PPC64_TOC16_DS:
11973 case R_PPC64_TOC16_LO_DS:
11974 {
11975 /* Check for toc tls entries. */
11976 unsigned char *toc_tls;
11977 int retval;
11978
11979 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11980 &local_syms, rel, input_bfd);
11981 if (retval == 0)
11982 return FALSE;
11983
11984 if (toc_tls)
11985 {
11986 tls_mask = *toc_tls;
11987 if (r_type == R_PPC64_TOC16_DS
11988 || r_type == R_PPC64_TOC16_LO_DS)
11989 {
11990 if (tls_mask != 0
11991 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
11992 goto toctprel;
11993 }
11994 else
11995 {
11996 /* If we found a GD reloc pair, then we might be
11997 doing a GD->IE transition. */
11998 if (retval == 2)
11999 {
12000 tls_gd = TLS_TPRELGD;
12001 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12002 goto tls_ldgd_opt;
12003 }
12004 else if (retval == 3)
12005 {
12006 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12007 goto tls_ldgd_opt;
12008 }
12009 }
12010 }
12011 }
12012 break;
12013
12014 case R_PPC64_GOT_TPREL16_HI:
12015 case R_PPC64_GOT_TPREL16_HA:
12016 if (tls_mask != 0
12017 && (tls_mask & TLS_TPREL) == 0)
12018 {
12019 rel->r_offset -= d_offset;
12020 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12021 r_type = R_PPC64_NONE;
12022 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12023 }
12024 break;
12025
12026 case R_PPC64_GOT_TPREL16_DS:
12027 case R_PPC64_GOT_TPREL16_LO_DS:
12028 if (tls_mask != 0
12029 && (tls_mask & TLS_TPREL) == 0)
12030 {
12031 toctprel:
12032 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12033 insn &= 31 << 21;
12034 insn |= 0x3c0d0000; /* addis 0,13,0 */
12035 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12036 r_type = R_PPC64_TPREL16_HA;
12037 if (toc_symndx != 0)
12038 {
12039 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
12040 rel->r_addend = toc_addend;
12041 /* We changed the symbol. Start over in order to
12042 get h, sym, sec etc. right. */
12043 rel--;
12044 continue;
12045 }
12046 else
12047 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12048 }
12049 break;
12050
12051 case R_PPC64_TLS:
12052 if (tls_mask != 0
12053 && (tls_mask & TLS_TPREL) == 0)
12054 {
12055 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
12056 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
12057 if (insn == 0)
12058 abort ();
12059 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12060 /* Was PPC64_TLS which sits on insn boundary, now
12061 PPC64_TPREL16_LO which is at low-order half-word. */
12062 rel->r_offset += d_offset;
12063 r_type = R_PPC64_TPREL16_LO;
12064 if (toc_symndx != 0)
12065 {
12066 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
12067 rel->r_addend = toc_addend;
12068 /* We changed the symbol. Start over in order to
12069 get h, sym, sec etc. right. */
12070 rel--;
12071 continue;
12072 }
12073 else
12074 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12075 }
12076 break;
12077
12078 case R_PPC64_GOT_TLSGD16_HI:
12079 case R_PPC64_GOT_TLSGD16_HA:
12080 tls_gd = TLS_TPRELGD;
12081 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12082 goto tls_gdld_hi;
12083 break;
12084
12085 case R_PPC64_GOT_TLSLD16_HI:
12086 case R_PPC64_GOT_TLSLD16_HA:
12087 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12088 {
12089 tls_gdld_hi:
12090 if ((tls_mask & tls_gd) != 0)
12091 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12092 + R_PPC64_GOT_TPREL16_DS);
12093 else
12094 {
12095 rel->r_offset -= d_offset;
12096 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12097 r_type = R_PPC64_NONE;
12098 }
12099 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12100 }
12101 break;
12102
12103 case R_PPC64_GOT_TLSGD16:
12104 case R_PPC64_GOT_TLSGD16_LO:
12105 tls_gd = TLS_TPRELGD;
12106 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12107 goto tls_ldgd_opt;
12108 break;
12109
12110 case R_PPC64_GOT_TLSLD16:
12111 case R_PPC64_GOT_TLSLD16_LO:
12112 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12113 {
12114 unsigned int insn1, insn2, insn3;
12115 bfd_vma offset;
12116
12117 tls_ldgd_opt:
12118 offset = (bfd_vma) -1;
12119 /* If not using the newer R_PPC64_TLSGD/LD to mark
12120 __tls_get_addr calls, we must trust that the call
12121 stays with its arg setup insns, ie. that the next
12122 reloc is the __tls_get_addr call associated with
12123 the current reloc. Edit both insns. */
12124 if (input_section->has_tls_get_addr_call
12125 && rel + 1 < relend
12126 && branch_reloc_hash_match (input_bfd, rel + 1,
12127 htab->tls_get_addr,
12128 htab->tls_get_addr_fd))
12129 offset = rel[1].r_offset;
12130 if ((tls_mask & tls_gd) != 0)
12131 {
12132 /* IE */
12133 insn1 = bfd_get_32 (output_bfd,
12134 contents + rel->r_offset - d_offset);
12135 insn1 &= (1 << 26) - (1 << 2);
12136 insn1 |= 58 << 26; /* ld */
12137 insn2 = 0x7c636a14; /* add 3,3,13 */
12138 if (offset != (bfd_vma) -1)
12139 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12140 if ((tls_mask & TLS_EXPLICIT) == 0)
12141 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12142 + R_PPC64_GOT_TPREL16_DS);
12143 else
12144 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
12145 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12146 }
12147 else
12148 {
12149 /* LE */
12150 insn1 = 0x3c6d0000; /* addis 3,13,0 */
12151 insn2 = 0x38630000; /* addi 3,3,0 */
12152 if (tls_gd == 0)
12153 {
12154 /* Was an LD reloc. */
12155 if (toc_symndx)
12156 sec = local_sections[toc_symndx];
12157 for (r_symndx = 0;
12158 r_symndx < symtab_hdr->sh_info;
12159 r_symndx++)
12160 if (local_sections[r_symndx] == sec)
12161 break;
12162 if (r_symndx >= symtab_hdr->sh_info)
12163 r_symndx = STN_UNDEF;
12164 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12165 if (r_symndx != STN_UNDEF)
12166 rel->r_addend -= (local_syms[r_symndx].st_value
12167 + sec->output_offset
12168 + sec->output_section->vma);
12169 }
12170 else if (toc_symndx != 0)
12171 {
12172 r_symndx = toc_symndx;
12173 rel->r_addend = toc_addend;
12174 }
12175 r_type = R_PPC64_TPREL16_HA;
12176 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12177 if (offset != (bfd_vma) -1)
12178 {
12179 rel[1].r_info = ELF64_R_INFO (r_symndx,
12180 R_PPC64_TPREL16_LO);
12181 rel[1].r_offset = offset + d_offset;
12182 rel[1].r_addend = rel->r_addend;
12183 }
12184 }
12185 bfd_put_32 (output_bfd, insn1,
12186 contents + rel->r_offset - d_offset);
12187 if (offset != (bfd_vma) -1)
12188 {
12189 insn3 = bfd_get_32 (output_bfd,
12190 contents + offset + 4);
12191 if (insn3 == NOP
12192 || insn3 == CROR_151515 || insn3 == CROR_313131)
12193 {
12194 rel[1].r_offset += 4;
12195 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12196 insn2 = NOP;
12197 }
12198 bfd_put_32 (output_bfd, insn2, contents + offset);
12199 }
12200 if ((tls_mask & tls_gd) == 0
12201 && (tls_gd == 0 || toc_symndx != 0))
12202 {
12203 /* We changed the symbol. Start over in order
12204 to get h, sym, sec etc. right. */
12205 rel--;
12206 continue;
12207 }
12208 }
12209 break;
12210
12211 case R_PPC64_TLSGD:
12212 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12213 {
12214 unsigned int insn2, insn3;
12215 bfd_vma offset = rel->r_offset;
12216
12217 if ((tls_mask & TLS_TPRELGD) != 0)
12218 {
12219 /* IE */
12220 r_type = R_PPC64_NONE;
12221 insn2 = 0x7c636a14; /* add 3,3,13 */
12222 }
12223 else
12224 {
12225 /* LE */
12226 if (toc_symndx != 0)
12227 {
12228 r_symndx = toc_symndx;
12229 rel->r_addend = toc_addend;
12230 }
12231 r_type = R_PPC64_TPREL16_LO;
12232 rel->r_offset = offset + d_offset;
12233 insn2 = 0x38630000; /* addi 3,3,0 */
12234 }
12235 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12236 /* Zap the reloc on the _tls_get_addr call too. */
12237 BFD_ASSERT (offset == rel[1].r_offset);
12238 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12239 insn3 = bfd_get_32 (output_bfd,
12240 contents + offset + 4);
12241 if (insn3 == NOP
12242 || insn3 == CROR_151515 || insn3 == CROR_313131)
12243 {
12244 rel->r_offset += 4;
12245 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12246 insn2 = NOP;
12247 }
12248 bfd_put_32 (output_bfd, insn2, contents + offset);
12249 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
12250 {
12251 rel--;
12252 continue;
12253 }
12254 }
12255 break;
12256
12257 case R_PPC64_TLSLD:
12258 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12259 {
12260 unsigned int insn2, insn3;
12261 bfd_vma offset = rel->r_offset;
12262
12263 if (toc_symndx)
12264 sec = local_sections[toc_symndx];
12265 for (r_symndx = 0;
12266 r_symndx < symtab_hdr->sh_info;
12267 r_symndx++)
12268 if (local_sections[r_symndx] == sec)
12269 break;
12270 if (r_symndx >= symtab_hdr->sh_info)
12271 r_symndx = STN_UNDEF;
12272 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12273 if (r_symndx != STN_UNDEF)
12274 rel->r_addend -= (local_syms[r_symndx].st_value
12275 + sec->output_offset
12276 + sec->output_section->vma);
12277
12278 r_type = R_PPC64_TPREL16_LO;
12279 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12280 rel->r_offset = offset + d_offset;
12281 /* Zap the reloc on the _tls_get_addr call too. */
12282 BFD_ASSERT (offset == rel[1].r_offset);
12283 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12284 insn2 = 0x38630000; /* addi 3,3,0 */
12285 insn3 = bfd_get_32 (output_bfd,
12286 contents + offset + 4);
12287 if (insn3 == NOP
12288 || insn3 == CROR_151515 || insn3 == CROR_313131)
12289 {
12290 rel->r_offset += 4;
12291 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12292 insn2 = NOP;
12293 }
12294 bfd_put_32 (output_bfd, insn2, contents + offset);
12295 rel--;
12296 continue;
12297 }
12298 break;
12299
12300 case R_PPC64_DTPMOD64:
12301 if (rel + 1 < relend
12302 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12303 && rel[1].r_offset == rel->r_offset + 8)
12304 {
12305 if ((tls_mask & TLS_GD) == 0)
12306 {
12307 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12308 if ((tls_mask & TLS_TPRELGD) != 0)
12309 r_type = R_PPC64_TPREL64;
12310 else
12311 {
12312 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12313 r_type = R_PPC64_NONE;
12314 }
12315 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12316 }
12317 }
12318 else
12319 {
12320 if ((tls_mask & TLS_LD) == 0)
12321 {
12322 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12323 r_type = R_PPC64_NONE;
12324 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12325 }
12326 }
12327 break;
12328
12329 case R_PPC64_TPREL64:
12330 if ((tls_mask & TLS_TPREL) == 0)
12331 {
12332 r_type = R_PPC64_NONE;
12333 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12334 }
12335 break;
12336 }
12337
12338 /* Handle other relocations that tweak non-addend part of insn. */
12339 insn = 0;
12340 max_br_offset = 1 << 25;
12341 addend = rel->r_addend;
12342 switch (r_type)
12343 {
12344 default:
12345 break;
12346
12347 /* Branch taken prediction relocations. */
12348 case R_PPC64_ADDR14_BRTAKEN:
12349 case R_PPC64_REL14_BRTAKEN:
12350 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
12351 /* Fall thru. */
12352
12353 /* Branch not taken prediction relocations. */
12354 case R_PPC64_ADDR14_BRNTAKEN:
12355 case R_PPC64_REL14_BRNTAKEN:
12356 insn |= bfd_get_32 (output_bfd,
12357 contents + rel->r_offset) & ~(0x01 << 21);
12358 /* Fall thru. */
12359
12360 case R_PPC64_REL14:
12361 max_br_offset = 1 << 15;
12362 /* Fall thru. */
12363
12364 case R_PPC64_REL24:
12365 /* Calls to functions with a different TOC, such as calls to
12366 shared objects, need to alter the TOC pointer. This is
12367 done using a linkage stub. A REL24 branching to these
12368 linkage stubs needs to be followed by a nop, as the nop
12369 will be replaced with an instruction to restore the TOC
12370 base pointer. */
12371 fdh = h;
12372 if (h != NULL
12373 && h->oh != NULL
12374 && h->oh->is_func_descriptor)
12375 fdh = ppc_follow_link (h->oh);
12376 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12377 if (stub_entry != NULL
12378 && (stub_entry->stub_type == ppc_stub_plt_call
12379 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12380 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
12381 {
12382 bfd_boolean can_plt_call = FALSE;
12383
12384 if (rel->r_offset + 8 <= input_section->size)
12385 {
12386 unsigned long nop;
12387 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12388 if (nop == NOP
12389 || nop == CROR_151515 || nop == CROR_313131)
12390 {
12391 if (h != NULL
12392 && (h == htab->tls_get_addr_fd
12393 || h == htab->tls_get_addr)
12394 && !htab->no_tls_get_addr_opt)
12395 {
12396 /* Special stub used, leave nop alone. */
12397 }
12398 else
12399 bfd_put_32 (input_bfd, LD_R2_40R1,
12400 contents + rel->r_offset + 4);
12401 can_plt_call = TRUE;
12402 }
12403 }
12404
12405 if (!can_plt_call)
12406 {
12407 if (stub_entry->stub_type == ppc_stub_plt_call)
12408 {
12409 /* If this is a plain branch rather than a branch
12410 and link, don't require a nop. However, don't
12411 allow tail calls in a shared library as they
12412 will result in r2 being corrupted. */
12413 unsigned long br;
12414 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
12415 if (info->executable && (br & 1) == 0)
12416 can_plt_call = TRUE;
12417 else
12418 stub_entry = NULL;
12419 }
12420 else if (h != NULL
12421 && strcmp (h->elf.root.root.string,
12422 ".__libc_start_main") == 0)
12423 {
12424 /* Allow crt1 branch to go via a toc adjusting stub. */
12425 can_plt_call = TRUE;
12426 }
12427 else
12428 {
12429 if (strcmp (input_section->output_section->name,
12430 ".init") == 0
12431 || strcmp (input_section->output_section->name,
12432 ".fini") == 0)
12433 info->callbacks->einfo
12434 (_("%P: %H: automatic multiple TOCs "
12435 "not supported using your crt files; "
12436 "recompile with -mminimal-toc or upgrade gcc\n"),
12437 input_bfd, input_section, rel->r_offset);
12438 else
12439 info->callbacks->einfo
12440 (_("%P: %H: sibling call optimization to `%s' "
12441 "does not allow automatic multiple TOCs; "
12442 "recompile with -mminimal-toc or "
12443 "-fno-optimize-sibling-calls, "
12444 "or make `%s' extern\n"),
12445 input_bfd, input_section, rel->r_offset,
12446 sym_name,
12447 sym_name);
12448 bfd_set_error (bfd_error_bad_value);
12449 ret = FALSE;
12450 }
12451 }
12452
12453 if (can_plt_call
12454 && stub_entry->stub_type == ppc_stub_plt_call)
12455 unresolved_reloc = FALSE;
12456 }
12457
12458 if ((stub_entry == NULL
12459 || stub_entry->stub_type == ppc_stub_long_branch
12460 || stub_entry->stub_type == ppc_stub_plt_branch)
12461 && get_opd_info (sec) != NULL)
12462 {
12463 /* The branch destination is the value of the opd entry. */
12464 bfd_vma off = (relocation + addend
12465 - sec->output_section->vma
12466 - sec->output_offset);
12467 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12468 if (dest != (bfd_vma) -1)
12469 {
12470 relocation = dest;
12471 addend = 0;
12472 }
12473 }
12474
12475 /* If the branch is out of reach we ought to have a long
12476 branch stub. */
12477 from = (rel->r_offset
12478 + input_section->output_offset
12479 + input_section->output_section->vma);
12480
12481 if (stub_entry != NULL
12482 && (stub_entry->stub_type == ppc_stub_long_branch
12483 || stub_entry->stub_type == ppc_stub_plt_branch)
12484 && (r_type == R_PPC64_ADDR14_BRTAKEN
12485 || r_type == R_PPC64_ADDR14_BRNTAKEN
12486 || (relocation + addend - from + max_br_offset
12487 < 2 * max_br_offset)))
12488 /* Don't use the stub if this branch is in range. */
12489 stub_entry = NULL;
12490
12491 if (stub_entry != NULL)
12492 {
12493 /* Munge up the value and addend so that we call the stub
12494 rather than the procedure directly. */
12495 relocation = (stub_entry->stub_offset
12496 + stub_entry->stub_sec->output_offset
12497 + stub_entry->stub_sec->output_section->vma);
12498 addend = 0;
12499 }
12500
12501 if (insn != 0)
12502 {
12503 if (is_power4)
12504 {
12505 /* Set 'a' bit. This is 0b00010 in BO field for branch
12506 on CR(BI) insns (BO == 001at or 011at), and 0b01000
12507 for branch on CTR insns (BO == 1a00t or 1a01t). */
12508 if ((insn & (0x14 << 21)) == (0x04 << 21))
12509 insn |= 0x02 << 21;
12510 else if ((insn & (0x14 << 21)) == (0x10 << 21))
12511 insn |= 0x08 << 21;
12512 else
12513 break;
12514 }
12515 else
12516 {
12517 /* Invert 'y' bit if not the default. */
12518 if ((bfd_signed_vma) (relocation + addend - from) < 0)
12519 insn ^= 0x01 << 21;
12520 }
12521
12522 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12523 }
12524
12525 /* NOP out calls to undefined weak functions.
12526 We can thus call a weak function without first
12527 checking whether the function is defined. */
12528 else if (h != NULL
12529 && h->elf.root.type == bfd_link_hash_undefweak
12530 && h->elf.dynindx == -1
12531 && r_type == R_PPC64_REL24
12532 && relocation == 0
12533 && addend == 0)
12534 {
12535 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12536 continue;
12537 }
12538 break;
12539 }
12540
12541 /* Set `addend'. */
12542 tls_type = 0;
12543 switch (r_type)
12544 {
12545 default:
12546 info->callbacks->einfo
12547 (_("%P: %B: unknown relocation type %d for symbol %s\n"),
12548 input_bfd, (int) r_type, sym_name);
12549
12550 bfd_set_error (bfd_error_bad_value);
12551 ret = FALSE;
12552 continue;
12553
12554 case R_PPC64_NONE:
12555 case R_PPC64_TLS:
12556 case R_PPC64_TLSGD:
12557 case R_PPC64_TLSLD:
12558 case R_PPC64_GNU_VTINHERIT:
12559 case R_PPC64_GNU_VTENTRY:
12560 continue;
12561
12562 /* GOT16 relocations. Like an ADDR16 using the symbol's
12563 address in the GOT as relocation value instead of the
12564 symbol's value itself. Also, create a GOT entry for the
12565 symbol and put the symbol value there. */
12566 case R_PPC64_GOT_TLSGD16:
12567 case R_PPC64_GOT_TLSGD16_LO:
12568 case R_PPC64_GOT_TLSGD16_HI:
12569 case R_PPC64_GOT_TLSGD16_HA:
12570 tls_type = TLS_TLS | TLS_GD;
12571 goto dogot;
12572
12573 case R_PPC64_GOT_TLSLD16:
12574 case R_PPC64_GOT_TLSLD16_LO:
12575 case R_PPC64_GOT_TLSLD16_HI:
12576 case R_PPC64_GOT_TLSLD16_HA:
12577 tls_type = TLS_TLS | TLS_LD;
12578 goto dogot;
12579
12580 case R_PPC64_GOT_TPREL16_DS:
12581 case R_PPC64_GOT_TPREL16_LO_DS:
12582 case R_PPC64_GOT_TPREL16_HI:
12583 case R_PPC64_GOT_TPREL16_HA:
12584 tls_type = TLS_TLS | TLS_TPREL;
12585 goto dogot;
12586
12587 case R_PPC64_GOT_DTPREL16_DS:
12588 case R_PPC64_GOT_DTPREL16_LO_DS:
12589 case R_PPC64_GOT_DTPREL16_HI:
12590 case R_PPC64_GOT_DTPREL16_HA:
12591 tls_type = TLS_TLS | TLS_DTPREL;
12592 goto dogot;
12593
12594 case R_PPC64_GOT16:
12595 case R_PPC64_GOT16_LO:
12596 case R_PPC64_GOT16_HI:
12597 case R_PPC64_GOT16_HA:
12598 case R_PPC64_GOT16_DS:
12599 case R_PPC64_GOT16_LO_DS:
12600 dogot:
12601 {
12602 /* Relocation is to the entry for this symbol in the global
12603 offset table. */
12604 asection *got;
12605 bfd_vma *offp;
12606 bfd_vma off;
12607 unsigned long indx = 0;
12608 struct got_entry *ent;
12609
12610 if (tls_type == (TLS_TLS | TLS_LD)
12611 && (h == NULL
12612 || !h->elf.def_dynamic))
12613 ent = ppc64_tlsld_got (input_bfd);
12614 else
12615 {
12616
12617 if (h != NULL)
12618 {
12619 bfd_boolean dyn = htab->elf.dynamic_sections_created;
12620 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
12621 &h->elf)
12622 || (info->shared
12623 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
12624 /* This is actually a static link, or it is a
12625 -Bsymbolic link and the symbol is defined
12626 locally, or the symbol was forced to be local
12627 because of a version file. */
12628 ;
12629 else
12630 {
12631 indx = h->elf.dynindx;
12632 unresolved_reloc = FALSE;
12633 }
12634 ent = h->elf.got.glist;
12635 }
12636 else
12637 {
12638 if (local_got_ents == NULL)
12639 abort ();
12640 ent = local_got_ents[r_symndx];
12641 }
12642
12643 for (; ent != NULL; ent = ent->next)
12644 if (ent->addend == orig_addend
12645 && ent->owner == input_bfd
12646 && ent->tls_type == tls_type)
12647 break;
12648 }
12649
12650 if (ent == NULL)
12651 abort ();
12652 if (ent->is_indirect)
12653 ent = ent->got.ent;
12654 offp = &ent->got.offset;
12655 got = ppc64_elf_tdata (ent->owner)->got;
12656 if (got == NULL)
12657 abort ();
12658
12659 /* The offset must always be a multiple of 8. We use the
12660 least significant bit to record whether we have already
12661 processed this entry. */
12662 off = *offp;
12663 if ((off & 1) != 0)
12664 off &= ~1;
12665 else
12666 {
12667 /* Generate relocs for the dynamic linker, except in
12668 the case of TLSLD where we'll use one entry per
12669 module. */
12670 asection *relgot;
12671 bfd_boolean ifunc;
12672
12673 *offp = off | 1;
12674 relgot = NULL;
12675 ifunc = (h != NULL
12676 ? h->elf.type == STT_GNU_IFUNC
12677 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
12678 if ((info->shared || indx != 0)
12679 && (h == NULL
12680 || (tls_type == (TLS_TLS | TLS_LD)
12681 && !h->elf.def_dynamic)
12682 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12683 || h->elf.root.type != bfd_link_hash_undefweak))
12684 relgot = ppc64_elf_tdata (ent->owner)->relgot;
12685 else if (ifunc)
12686 relgot = htab->reliplt;
12687 if (relgot != NULL)
12688 {
12689 outrel.r_offset = (got->output_section->vma
12690 + got->output_offset
12691 + off);
12692 outrel.r_addend = addend;
12693 if (tls_type & (TLS_LD | TLS_GD))
12694 {
12695 outrel.r_addend = 0;
12696 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
12697 if (tls_type == (TLS_TLS | TLS_GD))
12698 {
12699 loc = relgot->contents;
12700 loc += (relgot->reloc_count++
12701 * sizeof (Elf64_External_Rela));
12702 bfd_elf64_swap_reloca_out (output_bfd,
12703 &outrel, loc);
12704 outrel.r_offset += 8;
12705 outrel.r_addend = addend;
12706 outrel.r_info
12707 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
12708 }
12709 }
12710 else if (tls_type == (TLS_TLS | TLS_DTPREL))
12711 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
12712 else if (tls_type == (TLS_TLS | TLS_TPREL))
12713 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
12714 else if (indx != 0)
12715 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
12716 else
12717 {
12718 if (ifunc)
12719 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12720 else
12721 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12722
12723 /* Write the .got section contents for the sake
12724 of prelink. */
12725 loc = got->contents + off;
12726 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
12727 loc);
12728 }
12729
12730 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
12731 {
12732 outrel.r_addend += relocation;
12733 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
12734 outrel.r_addend -= htab->elf.tls_sec->vma;
12735 }
12736 loc = relgot->contents;
12737 loc += (relgot->reloc_count++
12738 * sizeof (Elf64_External_Rela));
12739 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12740 }
12741
12742 /* Init the .got section contents here if we're not
12743 emitting a reloc. */
12744 else
12745 {
12746 relocation += addend;
12747 if (tls_type == (TLS_TLS | TLS_LD))
12748 relocation = 1;
12749 else if (tls_type != 0)
12750 {
12751 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
12752 if (tls_type == (TLS_TLS | TLS_TPREL))
12753 relocation += DTP_OFFSET - TP_OFFSET;
12754
12755 if (tls_type == (TLS_TLS | TLS_GD))
12756 {
12757 bfd_put_64 (output_bfd, relocation,
12758 got->contents + off + 8);
12759 relocation = 1;
12760 }
12761 }
12762
12763 bfd_put_64 (output_bfd, relocation,
12764 got->contents + off);
12765 }
12766 }
12767
12768 if (off >= (bfd_vma) -2)
12769 abort ();
12770
12771 relocation = got->output_section->vma + got->output_offset + off;
12772 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
12773 }
12774 break;
12775
12776 case R_PPC64_PLT16_HA:
12777 case R_PPC64_PLT16_HI:
12778 case R_PPC64_PLT16_LO:
12779 case R_PPC64_PLT32:
12780 case R_PPC64_PLT64:
12781 /* Relocation is to the entry for this symbol in the
12782 procedure linkage table. */
12783
12784 /* Resolve a PLT reloc against a local symbol directly,
12785 without using the procedure linkage table. */
12786 if (h == NULL)
12787 break;
12788
12789 /* It's possible that we didn't make a PLT entry for this
12790 symbol. This happens when statically linking PIC code,
12791 or when using -Bsymbolic. Go find a match if there is a
12792 PLT entry. */
12793 if (htab->plt != NULL)
12794 {
12795 struct plt_entry *ent;
12796 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
12797 if (ent->addend == orig_addend
12798 && ent->plt.offset != (bfd_vma) -1)
12799 {
12800 relocation = (htab->plt->output_section->vma
12801 + htab->plt->output_offset
12802 + ent->plt.offset);
12803 unresolved_reloc = FALSE;
12804 }
12805 }
12806 break;
12807
12808 case R_PPC64_TOC:
12809 /* Relocation value is TOC base. */
12810 relocation = TOCstart;
12811 if (r_symndx == STN_UNDEF)
12812 relocation += htab->stub_group[input_section->id].toc_off;
12813 else if (unresolved_reloc)
12814 ;
12815 else if (sec != NULL && sec->id <= htab->top_id)
12816 relocation += htab->stub_group[sec->id].toc_off;
12817 else
12818 unresolved_reloc = TRUE;
12819 goto dodyn;
12820
12821 /* TOC16 relocs. We want the offset relative to the TOC base,
12822 which is the address of the start of the TOC plus 0x8000.
12823 The TOC consists of sections .got, .toc, .tocbss, and .plt,
12824 in this order. */
12825 case R_PPC64_TOC16:
12826 case R_PPC64_TOC16_LO:
12827 case R_PPC64_TOC16_HI:
12828 case R_PPC64_TOC16_DS:
12829 case R_PPC64_TOC16_LO_DS:
12830 case R_PPC64_TOC16_HA:
12831 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
12832 break;
12833
12834 /* Relocate against the beginning of the section. */
12835 case R_PPC64_SECTOFF:
12836 case R_PPC64_SECTOFF_LO:
12837 case R_PPC64_SECTOFF_HI:
12838 case R_PPC64_SECTOFF_DS:
12839 case R_PPC64_SECTOFF_LO_DS:
12840 case R_PPC64_SECTOFF_HA:
12841 if (sec != NULL)
12842 addend -= sec->output_section->vma;
12843 break;
12844
12845 case R_PPC64_REL16:
12846 case R_PPC64_REL16_LO:
12847 case R_PPC64_REL16_HI:
12848 case R_PPC64_REL16_HA:
12849 break;
12850
12851 case R_PPC64_REL14:
12852 case R_PPC64_REL14_BRNTAKEN:
12853 case R_PPC64_REL14_BRTAKEN:
12854 case R_PPC64_REL24:
12855 break;
12856
12857 case R_PPC64_TPREL16:
12858 case R_PPC64_TPREL16_LO:
12859 case R_PPC64_TPREL16_HI:
12860 case R_PPC64_TPREL16_HA:
12861 case R_PPC64_TPREL16_DS:
12862 case R_PPC64_TPREL16_LO_DS:
12863 case R_PPC64_TPREL16_HIGHER:
12864 case R_PPC64_TPREL16_HIGHERA:
12865 case R_PPC64_TPREL16_HIGHEST:
12866 case R_PPC64_TPREL16_HIGHESTA:
12867 if (h != NULL
12868 && h->elf.root.type == bfd_link_hash_undefweak
12869 && h->elf.dynindx == -1)
12870 {
12871 /* Make this relocation against an undefined weak symbol
12872 resolve to zero. This is really just a tweak, since
12873 code using weak externs ought to check that they are
12874 defined before using them. */
12875 bfd_byte *p = contents + rel->r_offset - d_offset;
12876
12877 insn = bfd_get_32 (output_bfd, p);
12878 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
12879 if (insn != 0)
12880 bfd_put_32 (output_bfd, insn, p);
12881 break;
12882 }
12883 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
12884 if (info->shared)
12885 /* The TPREL16 relocs shouldn't really be used in shared
12886 libs as they will result in DT_TEXTREL being set, but
12887 support them anyway. */
12888 goto dodyn;
12889 break;
12890
12891 case R_PPC64_DTPREL16:
12892 case R_PPC64_DTPREL16_LO:
12893 case R_PPC64_DTPREL16_HI:
12894 case R_PPC64_DTPREL16_HA:
12895 case R_PPC64_DTPREL16_DS:
12896 case R_PPC64_DTPREL16_LO_DS:
12897 case R_PPC64_DTPREL16_HIGHER:
12898 case R_PPC64_DTPREL16_HIGHERA:
12899 case R_PPC64_DTPREL16_HIGHEST:
12900 case R_PPC64_DTPREL16_HIGHESTA:
12901 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
12902 break;
12903
12904 case R_PPC64_DTPMOD64:
12905 relocation = 1;
12906 addend = 0;
12907 goto dodyn;
12908
12909 case R_PPC64_TPREL64:
12910 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
12911 goto dodyn;
12912
12913 case R_PPC64_DTPREL64:
12914 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
12915 /* Fall thru */
12916
12917 /* Relocations that may need to be propagated if this is a
12918 dynamic object. */
12919 case R_PPC64_REL30:
12920 case R_PPC64_REL32:
12921 case R_PPC64_REL64:
12922 case R_PPC64_ADDR14:
12923 case R_PPC64_ADDR14_BRNTAKEN:
12924 case R_PPC64_ADDR14_BRTAKEN:
12925 case R_PPC64_ADDR16:
12926 case R_PPC64_ADDR16_DS:
12927 case R_PPC64_ADDR16_HA:
12928 case R_PPC64_ADDR16_HI:
12929 case R_PPC64_ADDR16_HIGHER:
12930 case R_PPC64_ADDR16_HIGHERA:
12931 case R_PPC64_ADDR16_HIGHEST:
12932 case R_PPC64_ADDR16_HIGHESTA:
12933 case R_PPC64_ADDR16_LO:
12934 case R_PPC64_ADDR16_LO_DS:
12935 case R_PPC64_ADDR24:
12936 case R_PPC64_ADDR32:
12937 case R_PPC64_ADDR64:
12938 case R_PPC64_UADDR16:
12939 case R_PPC64_UADDR32:
12940 case R_PPC64_UADDR64:
12941 dodyn:
12942 if ((input_section->flags & SEC_ALLOC) == 0)
12943 break;
12944
12945 if (NO_OPD_RELOCS && is_opd)
12946 break;
12947
12948 if ((info->shared
12949 && (h == NULL
12950 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12951 || h->elf.root.type != bfd_link_hash_undefweak)
12952 && (must_be_dyn_reloc (info, r_type)
12953 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
12954 || (ELIMINATE_COPY_RELOCS
12955 && !info->shared
12956 && h != NULL
12957 && h->elf.dynindx != -1
12958 && !h->elf.non_got_ref
12959 && !h->elf.def_regular)
12960 || (!info->shared
12961 && (h != NULL
12962 ? h->elf.type == STT_GNU_IFUNC
12963 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
12964 {
12965 bfd_boolean skip, relocate;
12966 asection *sreloc;
12967 bfd_vma out_off;
12968
12969 /* When generating a dynamic object, these relocations
12970 are copied into the output file to be resolved at run
12971 time. */
12972
12973 skip = FALSE;
12974 relocate = FALSE;
12975
12976 out_off = _bfd_elf_section_offset (output_bfd, info,
12977 input_section, rel->r_offset);
12978 if (out_off == (bfd_vma) -1)
12979 skip = TRUE;
12980 else if (out_off == (bfd_vma) -2)
12981 skip = TRUE, relocate = TRUE;
12982 out_off += (input_section->output_section->vma
12983 + input_section->output_offset);
12984 outrel.r_offset = out_off;
12985 outrel.r_addend = rel->r_addend;
12986
12987 /* Optimize unaligned reloc use. */
12988 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
12989 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
12990 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
12991 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
12992 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
12993 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
12994 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
12995 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
12996 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
12997
12998 if (skip)
12999 memset (&outrel, 0, sizeof outrel);
13000 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
13001 && !is_opd
13002 && r_type != R_PPC64_TOC)
13003 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
13004 else
13005 {
13006 /* This symbol is local, or marked to become local,
13007 or this is an opd section reloc which must point
13008 at a local function. */
13009 outrel.r_addend += relocation;
13010 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13011 {
13012 if (is_opd && h != NULL)
13013 {
13014 /* Lie about opd entries. This case occurs
13015 when building shared libraries and we
13016 reference a function in another shared
13017 lib. The same thing happens for a weak
13018 definition in an application that's
13019 overridden by a strong definition in a
13020 shared lib. (I believe this is a generic
13021 bug in binutils handling of weak syms.)
13022 In these cases we won't use the opd
13023 entry in this lib. */
13024 unresolved_reloc = FALSE;
13025 }
13026 if (!is_opd
13027 && r_type == R_PPC64_ADDR64
13028 && (h != NULL
13029 ? h->elf.type == STT_GNU_IFUNC
13030 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
13031 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13032 else
13033 {
13034 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13035
13036 /* We need to relocate .opd contents for ld.so.
13037 Prelink also wants simple and consistent rules
13038 for relocs. This make all RELATIVE relocs have
13039 *r_offset equal to r_addend. */
13040 relocate = TRUE;
13041 }
13042 }
13043 else
13044 {
13045 long indx = 0;
13046
13047 if (h != NULL
13048 ? h->elf.type == STT_GNU_IFUNC
13049 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13050 {
13051 info->callbacks->einfo
13052 (_("%P: %H: relocation %s for indirect "
13053 "function %s unsupported\n"),
13054 input_bfd, input_section, rel->r_offset,
13055 ppc64_elf_howto_table[r_type]->name,
13056 sym_name);
13057 ret = FALSE;
13058 }
13059 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
13060 ;
13061 else if (sec == NULL || sec->owner == NULL)
13062 {
13063 bfd_set_error (bfd_error_bad_value);
13064 return FALSE;
13065 }
13066 else
13067 {
13068 asection *osec;
13069
13070 osec = sec->output_section;
13071 indx = elf_section_data (osec)->dynindx;
13072
13073 if (indx == 0)
13074 {
13075 if ((osec->flags & SEC_READONLY) == 0
13076 && htab->elf.data_index_section != NULL)
13077 osec = htab->elf.data_index_section;
13078 else
13079 osec = htab->elf.text_index_section;
13080 indx = elf_section_data (osec)->dynindx;
13081 }
13082 BFD_ASSERT (indx != 0);
13083
13084 /* We are turning this relocation into one
13085 against a section symbol, so subtract out
13086 the output section's address but not the
13087 offset of the input section in the output
13088 section. */
13089 outrel.r_addend -= osec->vma;
13090 }
13091
13092 outrel.r_info = ELF64_R_INFO (indx, r_type);
13093 }
13094 }
13095
13096 sreloc = elf_section_data (input_section)->sreloc;
13097 if (!htab->elf.dynamic_sections_created)
13098 sreloc = htab->reliplt;
13099 if (sreloc == NULL)
13100 abort ();
13101
13102 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
13103 >= sreloc->size)
13104 abort ();
13105 loc = sreloc->contents;
13106 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
13107 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13108
13109 /* If this reloc is against an external symbol, it will
13110 be computed at runtime, so there's no need to do
13111 anything now. However, for the sake of prelink ensure
13112 that the section contents are a known value. */
13113 if (! relocate)
13114 {
13115 unresolved_reloc = FALSE;
13116 /* The value chosen here is quite arbitrary as ld.so
13117 ignores section contents except for the special
13118 case of .opd where the contents might be accessed
13119 before relocation. Choose zero, as that won't
13120 cause reloc overflow. */
13121 relocation = 0;
13122 addend = 0;
13123 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
13124 to improve backward compatibility with older
13125 versions of ld. */
13126 if (r_type == R_PPC64_ADDR64)
13127 addend = outrel.r_addend;
13128 /* Adjust pc_relative relocs to have zero in *r_offset. */
13129 else if (ppc64_elf_howto_table[r_type]->pc_relative)
13130 addend = (input_section->output_section->vma
13131 + input_section->output_offset
13132 + rel->r_offset);
13133 }
13134 }
13135 break;
13136
13137 case R_PPC64_COPY:
13138 case R_PPC64_GLOB_DAT:
13139 case R_PPC64_JMP_SLOT:
13140 case R_PPC64_JMP_IREL:
13141 case R_PPC64_RELATIVE:
13142 /* We shouldn't ever see these dynamic relocs in relocatable
13143 files. */
13144 /* Fall through. */
13145
13146 case R_PPC64_PLTGOT16:
13147 case R_PPC64_PLTGOT16_DS:
13148 case R_PPC64_PLTGOT16_HA:
13149 case R_PPC64_PLTGOT16_HI:
13150 case R_PPC64_PLTGOT16_LO:
13151 case R_PPC64_PLTGOT16_LO_DS:
13152 case R_PPC64_PLTREL32:
13153 case R_PPC64_PLTREL64:
13154 /* These ones haven't been implemented yet. */
13155
13156 info->callbacks->einfo
13157 (_("%P: %B: relocation %s is not supported for symbol %s\n"),
13158 input_bfd,
13159 ppc64_elf_howto_table[r_type]->name, sym_name);
13160
13161 bfd_set_error (bfd_error_invalid_operation);
13162 ret = FALSE;
13163 continue;
13164 }
13165
13166 /* Multi-instruction sequences that access the TOC can be
13167 optimized, eg. addis ra,r2,0; addi rb,ra,x;
13168 to nop; addi rb,r2,x; */
13169 switch (r_type)
13170 {
13171 default:
13172 break;
13173
13174 case R_PPC64_GOT_TLSLD16_HI:
13175 case R_PPC64_GOT_TLSGD16_HI:
13176 case R_PPC64_GOT_TPREL16_HI:
13177 case R_PPC64_GOT_DTPREL16_HI:
13178 case R_PPC64_GOT16_HI:
13179 case R_PPC64_TOC16_HI:
13180 /* These relocs would only be useful if building up an
13181 offset to later add to r2, perhaps in an indexed
13182 addressing mode instruction. Don't try to optimize.
13183 Unfortunately, the possibility of someone building up an
13184 offset like this or even with the HA relocs, means that
13185 we need to check the high insn when optimizing the low
13186 insn. */
13187 break;
13188
13189 case R_PPC64_GOT_TLSLD16_HA:
13190 case R_PPC64_GOT_TLSGD16_HA:
13191 case R_PPC64_GOT_TPREL16_HA:
13192 case R_PPC64_GOT_DTPREL16_HA:
13193 case R_PPC64_GOT16_HA:
13194 case R_PPC64_TOC16_HA:
13195 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
13196 && !ppc64_elf_tdata (input_bfd)->ha_relocs_not_using_r2)
13197 {
13198 bfd_byte *p = contents + (rel->r_offset & ~3);
13199 bfd_put_32 (input_bfd, NOP, p);
13200 }
13201 break;
13202
13203 case R_PPC64_GOT_TLSLD16_LO:
13204 case R_PPC64_GOT_TLSGD16_LO:
13205 case R_PPC64_GOT_TPREL16_LO_DS:
13206 case R_PPC64_GOT_DTPREL16_LO_DS:
13207 case R_PPC64_GOT16_LO:
13208 case R_PPC64_GOT16_LO_DS:
13209 case R_PPC64_TOC16_LO:
13210 case R_PPC64_TOC16_LO_DS:
13211 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
13212 && !ppc64_elf_tdata (input_bfd)->ha_relocs_not_using_r2)
13213 {
13214 bfd_byte *p = contents + (rel->r_offset & ~3);
13215 insn = bfd_get_32 (input_bfd, p);
13216 if ((insn & (0x3f << 26)) == 14u << 26 /* addi */
13217 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
13218 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
13219 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
13220 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
13221 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
13222 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
13223 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
13224 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
13225 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
13226 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
13227 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
13228 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
13229 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
13230 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
13231 && (insn & 3) != 1)
13232 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
13233 && ((insn & 3) == 0 || (insn & 3) == 3)))
13234 {
13235 insn &= ~(0x1f << 16);
13236 insn |= 2 << 16;
13237 bfd_put_32 (input_bfd, insn, p);
13238 }
13239 }
13240 break;
13241 }
13242
13243 /* Do any further special processing. */
13244 switch (r_type)
13245 {
13246 default:
13247 break;
13248
13249 case R_PPC64_ADDR16_HA:
13250 case R_PPC64_REL16_HA:
13251 case R_PPC64_ADDR16_HIGHERA:
13252 case R_PPC64_ADDR16_HIGHESTA:
13253 case R_PPC64_TOC16_HA:
13254 case R_PPC64_SECTOFF_HA:
13255 case R_PPC64_TPREL16_HA:
13256 case R_PPC64_DTPREL16_HA:
13257 case R_PPC64_TPREL16_HIGHER:
13258 case R_PPC64_TPREL16_HIGHERA:
13259 case R_PPC64_TPREL16_HIGHEST:
13260 case R_PPC64_TPREL16_HIGHESTA:
13261 case R_PPC64_DTPREL16_HIGHER:
13262 case R_PPC64_DTPREL16_HIGHERA:
13263 case R_PPC64_DTPREL16_HIGHEST:
13264 case R_PPC64_DTPREL16_HIGHESTA:
13265 /* It's just possible that this symbol is a weak symbol
13266 that's not actually defined anywhere. In that case,
13267 'sec' would be NULL, and we should leave the symbol
13268 alone (it will be set to zero elsewhere in the link). */
13269 if (sec == NULL)
13270 break;
13271 /* Fall thru */
13272
13273 case R_PPC64_GOT16_HA:
13274 case R_PPC64_PLTGOT16_HA:
13275 case R_PPC64_PLT16_HA:
13276 case R_PPC64_GOT_TLSGD16_HA:
13277 case R_PPC64_GOT_TLSLD16_HA:
13278 case R_PPC64_GOT_TPREL16_HA:
13279 case R_PPC64_GOT_DTPREL16_HA:
13280 /* Add 0x10000 if sign bit in 0:15 is set.
13281 Bits 0:15 are not used. */
13282 addend += 0x8000;
13283 break;
13284
13285 case R_PPC64_ADDR16_DS:
13286 case R_PPC64_ADDR16_LO_DS:
13287 case R_PPC64_GOT16_DS:
13288 case R_PPC64_GOT16_LO_DS:
13289 case R_PPC64_PLT16_LO_DS:
13290 case R_PPC64_SECTOFF_DS:
13291 case R_PPC64_SECTOFF_LO_DS:
13292 case R_PPC64_TOC16_DS:
13293 case R_PPC64_TOC16_LO_DS:
13294 case R_PPC64_PLTGOT16_DS:
13295 case R_PPC64_PLTGOT16_LO_DS:
13296 case R_PPC64_GOT_TPREL16_DS:
13297 case R_PPC64_GOT_TPREL16_LO_DS:
13298 case R_PPC64_GOT_DTPREL16_DS:
13299 case R_PPC64_GOT_DTPREL16_LO_DS:
13300 case R_PPC64_TPREL16_DS:
13301 case R_PPC64_TPREL16_LO_DS:
13302 case R_PPC64_DTPREL16_DS:
13303 case R_PPC64_DTPREL16_LO_DS:
13304 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13305 mask = 3;
13306 /* If this reloc is against an lq insn, then the value must be
13307 a multiple of 16. This is somewhat of a hack, but the
13308 "correct" way to do this by defining _DQ forms of all the
13309 _DS relocs bloats all reloc switches in this file. It
13310 doesn't seem to make much sense to use any of these relocs
13311 in data, so testing the insn should be safe. */
13312 if ((insn & (0x3f << 26)) == (56u << 26))
13313 mask = 15;
13314 if (((relocation + addend) & mask) != 0)
13315 {
13316 info->callbacks->einfo
13317 (_("%P: %H: error: %s not a multiple of %u\n"),
13318 input_bfd, input_section, rel->r_offset,
13319 ppc64_elf_howto_table[r_type]->name,
13320 mask + 1);
13321 bfd_set_error (bfd_error_bad_value);
13322 ret = FALSE;
13323 continue;
13324 }
13325 break;
13326 }
13327
13328 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13329 because such sections are not SEC_ALLOC and thus ld.so will
13330 not process them. */
13331 if (unresolved_reloc
13332 && !((input_section->flags & SEC_DEBUGGING) != 0
13333 && h->elf.def_dynamic))
13334 {
13335 info->callbacks->einfo
13336 (_("%P: %H: unresolvable %s relocation against symbol `%s'\n"),
13337 input_bfd, input_section, rel->r_offset,
13338 ppc64_elf_howto_table[(int) r_type]->name,
13339 h->elf.root.root.string);
13340 ret = FALSE;
13341 }
13342
13343 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
13344 input_bfd,
13345 input_section,
13346 contents,
13347 rel->r_offset,
13348 relocation,
13349 addend);
13350
13351 if (r != bfd_reloc_ok)
13352 {
13353 if (sym_name == NULL)
13354 sym_name = "(null)";
13355 if (r == bfd_reloc_overflow)
13356 {
13357 if (warned)
13358 continue;
13359 if (h != NULL
13360 && h->elf.root.type == bfd_link_hash_undefweak
13361 && ppc64_elf_howto_table[r_type]->pc_relative)
13362 {
13363 /* Assume this is a call protected by other code that
13364 detects the symbol is undefined. If this is the case,
13365 we can safely ignore the overflow. If not, the
13366 program is hosed anyway, and a little warning isn't
13367 going to help. */
13368
13369 continue;
13370 }
13371
13372 if (!((*info->callbacks->reloc_overflow)
13373 (info, (h ? &h->elf.root : NULL), sym_name,
13374 ppc64_elf_howto_table[r_type]->name,
13375 orig_addend, input_bfd, input_section, rel->r_offset)))
13376 return FALSE;
13377 }
13378 else
13379 {
13380 info->callbacks->einfo
13381 (_("%P: %H: %s reloc against `%s': error %d\n"),
13382 input_bfd, input_section, rel->r_offset,
13383 ppc64_elf_howto_table[r_type]->name,
13384 sym_name,
13385 (int) r);
13386 ret = FALSE;
13387 }
13388 }
13389 }
13390
13391 /* If we're emitting relocations, then shortly after this function
13392 returns, reloc offsets and addends for this section will be
13393 adjusted. Worse, reloc symbol indices will be for the output
13394 file rather than the input. Save a copy of the relocs for
13395 opd_entry_value. */
13396 if (is_opd && (info->emitrelocations || info->relocatable))
13397 {
13398 bfd_size_type amt;
13399 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13400 rel = bfd_alloc (input_bfd, amt);
13401 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13402 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13403 if (rel == NULL)
13404 return FALSE;
13405 memcpy (rel, relocs, amt);
13406 }
13407 return ret;
13408 }
13409
13410 /* Adjust the value of any local symbols in opd sections. */
13411
13412 static int
13413 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13414 const char *name ATTRIBUTE_UNUSED,
13415 Elf_Internal_Sym *elfsym,
13416 asection *input_sec,
13417 struct elf_link_hash_entry *h)
13418 {
13419 struct _opd_sec_data *opd;
13420 long adjust;
13421 bfd_vma value;
13422
13423 if (h != NULL)
13424 return 1;
13425
13426 opd = get_opd_info (input_sec);
13427 if (opd == NULL || opd->adjust == NULL)
13428 return 1;
13429
13430 value = elfsym->st_value - input_sec->output_offset;
13431 if (!info->relocatable)
13432 value -= input_sec->output_section->vma;
13433
13434 adjust = opd->adjust[value / 8];
13435 if (adjust == -1)
13436 return 2;
13437
13438 elfsym->st_value += adjust;
13439 return 1;
13440 }
13441
13442 /* Finish up dynamic symbol handling. We set the contents of various
13443 dynamic sections here. */
13444
13445 static bfd_boolean
13446 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13447 struct bfd_link_info *info,
13448 struct elf_link_hash_entry *h,
13449 Elf_Internal_Sym *sym)
13450 {
13451 struct ppc_link_hash_table *htab;
13452 struct plt_entry *ent;
13453 Elf_Internal_Rela rela;
13454 bfd_byte *loc;
13455
13456 htab = ppc_hash_table (info);
13457 if (htab == NULL)
13458 return FALSE;
13459
13460 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13461 if (ent->plt.offset != (bfd_vma) -1)
13462 {
13463 /* This symbol has an entry in the procedure linkage
13464 table. Set it up. */
13465 if (!htab->elf.dynamic_sections_created
13466 || h->dynindx == -1)
13467 {
13468 BFD_ASSERT (h->type == STT_GNU_IFUNC
13469 && h->def_regular
13470 && (h->root.type == bfd_link_hash_defined
13471 || h->root.type == bfd_link_hash_defweak));
13472 rela.r_offset = (htab->iplt->output_section->vma
13473 + htab->iplt->output_offset
13474 + ent->plt.offset);
13475 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13476 rela.r_addend = (h->root.u.def.value
13477 + h->root.u.def.section->output_offset
13478 + h->root.u.def.section->output_section->vma
13479 + ent->addend);
13480 loc = (htab->reliplt->contents
13481 + (htab->reliplt->reloc_count++
13482 * sizeof (Elf64_External_Rela)));
13483 }
13484 else
13485 {
13486 rela.r_offset = (htab->plt->output_section->vma
13487 + htab->plt->output_offset
13488 + ent->plt.offset);
13489 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13490 rela.r_addend = ent->addend;
13491 loc = (htab->relplt->contents
13492 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13493 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
13494 }
13495 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13496 }
13497
13498 if (h->needs_copy)
13499 {
13500 /* This symbol needs a copy reloc. Set it up. */
13501
13502 if (h->dynindx == -1
13503 || (h->root.type != bfd_link_hash_defined
13504 && h->root.type != bfd_link_hash_defweak)
13505 || htab->relbss == NULL)
13506 abort ();
13507
13508 rela.r_offset = (h->root.u.def.value
13509 + h->root.u.def.section->output_section->vma
13510 + h->root.u.def.section->output_offset);
13511 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13512 rela.r_addend = 0;
13513 loc = htab->relbss->contents;
13514 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
13515 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13516 }
13517
13518 /* Mark some specially defined symbols as absolute. */
13519 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13520 sym->st_shndx = SHN_ABS;
13521
13522 return TRUE;
13523 }
13524
13525 /* Used to decide how to sort relocs in an optimal manner for the
13526 dynamic linker, before writing them out. */
13527
13528 static enum elf_reloc_type_class
13529 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
13530 {
13531 enum elf_ppc64_reloc_type r_type;
13532
13533 r_type = ELF64_R_TYPE (rela->r_info);
13534 switch (r_type)
13535 {
13536 case R_PPC64_RELATIVE:
13537 return reloc_class_relative;
13538 case R_PPC64_JMP_SLOT:
13539 return reloc_class_plt;
13540 case R_PPC64_COPY:
13541 return reloc_class_copy;
13542 default:
13543 return reloc_class_normal;
13544 }
13545 }
13546
13547 /* Finish up the dynamic sections. */
13548
13549 static bfd_boolean
13550 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13551 struct bfd_link_info *info)
13552 {
13553 struct ppc_link_hash_table *htab;
13554 bfd *dynobj;
13555 asection *sdyn;
13556
13557 htab = ppc_hash_table (info);
13558 if (htab == NULL)
13559 return FALSE;
13560
13561 dynobj = htab->elf.dynobj;
13562 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13563
13564 if (htab->elf.dynamic_sections_created)
13565 {
13566 Elf64_External_Dyn *dyncon, *dynconend;
13567
13568 if (sdyn == NULL || htab->got == NULL)
13569 abort ();
13570
13571 dyncon = (Elf64_External_Dyn *) sdyn->contents;
13572 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
13573 for (; dyncon < dynconend; dyncon++)
13574 {
13575 Elf_Internal_Dyn dyn;
13576 asection *s;
13577
13578 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13579
13580 switch (dyn.d_tag)
13581 {
13582 default:
13583 continue;
13584
13585 case DT_PPC64_GLINK:
13586 s = htab->glink;
13587 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13588 /* We stupidly defined DT_PPC64_GLINK to be the start
13589 of glink rather than the first entry point, which is
13590 what ld.so needs, and now have a bigger stub to
13591 support automatic multiple TOCs. */
13592 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
13593 break;
13594
13595 case DT_PPC64_OPD:
13596 s = bfd_get_section_by_name (output_bfd, ".opd");
13597 if (s == NULL)
13598 continue;
13599 dyn.d_un.d_ptr = s->vma;
13600 break;
13601
13602 case DT_PPC64_OPDSZ:
13603 s = bfd_get_section_by_name (output_bfd, ".opd");
13604 if (s == NULL)
13605 continue;
13606 dyn.d_un.d_val = s->size;
13607 break;
13608
13609 case DT_PLTGOT:
13610 s = htab->plt;
13611 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13612 break;
13613
13614 case DT_JMPREL:
13615 s = htab->relplt;
13616 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13617 break;
13618
13619 case DT_PLTRELSZ:
13620 dyn.d_un.d_val = htab->relplt->size;
13621 break;
13622
13623 case DT_RELASZ:
13624 /* Don't count procedure linkage table relocs in the
13625 overall reloc count. */
13626 s = htab->relplt;
13627 if (s == NULL)
13628 continue;
13629 dyn.d_un.d_val -= s->size;
13630 break;
13631
13632 case DT_RELA:
13633 /* We may not be using the standard ELF linker script.
13634 If .rela.plt is the first .rela section, we adjust
13635 DT_RELA to not include it. */
13636 s = htab->relplt;
13637 if (s == NULL)
13638 continue;
13639 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
13640 continue;
13641 dyn.d_un.d_ptr += s->size;
13642 break;
13643 }
13644
13645 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
13646 }
13647 }
13648
13649 if (htab->got != NULL && htab->got->size != 0)
13650 {
13651 /* Fill in the first entry in the global offset table.
13652 We use it to hold the link-time TOCbase. */
13653 bfd_put_64 (output_bfd,
13654 elf_gp (output_bfd) + TOC_BASE_OFF,
13655 htab->got->contents);
13656
13657 /* Set .got entry size. */
13658 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
13659 }
13660
13661 if (htab->plt != NULL && htab->plt->size != 0)
13662 {
13663 /* Set .plt entry size. */
13664 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
13665 = PLT_ENTRY_SIZE;
13666 }
13667
13668 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
13669 brlt ourselves if emitrelocations. */
13670 if (htab->brlt != NULL
13671 && htab->brlt->reloc_count != 0
13672 && !_bfd_elf_link_output_relocs (output_bfd,
13673 htab->brlt,
13674 elf_section_data (htab->brlt)->rela.hdr,
13675 elf_section_data (htab->brlt)->relocs,
13676 NULL))
13677 return FALSE;
13678
13679 if (htab->glink != NULL
13680 && htab->glink->reloc_count != 0
13681 && !_bfd_elf_link_output_relocs (output_bfd,
13682 htab->glink,
13683 elf_section_data (htab->glink)->rela.hdr,
13684 elf_section_data (htab->glink)->relocs,
13685 NULL))
13686 return FALSE;
13687
13688
13689 if (htab->glink_eh_frame != NULL
13690 && htab->glink_eh_frame->sec_info_type == ELF_INFO_TYPE_EH_FRAME
13691 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
13692 htab->glink_eh_frame,
13693 htab->glink_eh_frame->contents))
13694 return FALSE;
13695
13696 /* We need to handle writing out multiple GOT sections ourselves,
13697 since we didn't add them to DYNOBJ. We know dynobj is the first
13698 bfd. */
13699 while ((dynobj = dynobj->link_next) != NULL)
13700 {
13701 asection *s;
13702
13703 if (!is_ppc64_elf (dynobj))
13704 continue;
13705
13706 s = ppc64_elf_tdata (dynobj)->got;
13707 if (s != NULL
13708 && s->size != 0
13709 && s->output_section != bfd_abs_section_ptr
13710 && !bfd_set_section_contents (output_bfd, s->output_section,
13711 s->contents, s->output_offset,
13712 s->size))
13713 return FALSE;
13714 s = ppc64_elf_tdata (dynobj)->relgot;
13715 if (s != NULL
13716 && s->size != 0
13717 && s->output_section != bfd_abs_section_ptr
13718 && !bfd_set_section_contents (output_bfd, s->output_section,
13719 s->contents, s->output_offset,
13720 s->size))
13721 return FALSE;
13722 }
13723
13724 return TRUE;
13725 }
13726
13727 #include "elf64-target.h"
This page took 0.603381 seconds and 4 git commands to generate.